g_signal_connect必须在QObject::connect之前调用



我正在创建一个结合了GStreamer和Qt的应用程序。如果在使用g_signal_connect将回调函数注册到GStreamer总线上的事件之前,使用QObject::connect将信号连接到插槽,则永远不会调用g_signal_nect回调函数。如果我颠倒顺序,那就是。这是意料之中的事吗?

示例:

main.cpp

#include <QApplication>
#include <QPushButton>
#include "acquisitiontype.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);

AcquisitionType acquisition("224.1.1.1", 5004);
QPushButton* button = new QPushButton("click me");
QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
button->show();

return app.exec();
}

收购pe.cpp

#include "acquisitiontype.h"

void AcquisitionType::udp_source_timeout_callback(GstBus* bus, GstMessage* message, gstreamer_data* user_data) {
(void) bus;
(void) user_data;
const GstStructure* st = gst_message_get_structure(message);

if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ELEMENT) {
if (gst_structure_has_name(st, "GstUDPSrcTimeout")) {
printf("callback calledn");
}
}
}
void AcquisitionType::bus_error_callback(GstBus* bus, GstMessage* message, gstreamer_data* user_data) {
(void) bus;
(void) user_data;
GError* err;
gchar* debug_info;

gst_message_parse_error(message, &err, &debug_info);
g_printerr("Error received from element %s: %sn", GST_OBJECT_NAME(message->src), err->message);
g_printerr("Debugging information: %sn", debug_info ? debug_info : "none");
g_clear_error(&err);
g_free(debug_info);
exit(-1);
}
AcquisitionType::AcquisitionType(char const* address, gint port) {
GstStateChangeReturn ret;
GstBus* bus;

gst_init(NULL, NULL);

data.udp_source = gst_element_factory_make("udpsrc", "udp_source");
g_object_set(G_OBJECT(data.udp_source),
"address", address,
"port", port,
"caps", gst_caps_new_empty_simple("application/x-rtp"),
"timeout", 1000000000,
NULL);

data.sink = gst_element_factory_make("fakesink", "sink");

data.pipeline = gst_pipeline_new("pipeline");

if (
!data.pipeline ||
!data.udp_source ||
!data.sink
)
{
g_printerr("Not all elements could be created.n");
exit(-1);
}

gst_bin_add_many(
GST_BIN(data.pipeline),
data.udp_source,
data.sink,
NULL);

if (gst_element_link_many(
data.udp_source,
data.sink,
NULL) != TRUE)
{
g_printerr("Elements could not be linked.n");
gst_object_unref(data.pipeline);
exit(-1);
}

bus = gst_element_get_bus(data.pipeline);
gst_bus_add_signal_watch(bus);
g_signal_connect(G_OBJECT(bus), "message::error", (GCallback) bus_error_callback, &data);
g_signal_connect(G_OBJECT(bus), "message::element", (GCallback) udp_source_timeout_callback, &data);
gst_object_unref(bus);

ret = gst_element_set_state(data.pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr("Unable to set the pipeline to the playing state.n");
gst_object_unref(data.pipeline);
exit(-1);
}
}
AcquisitionType::~AcquisitionType() {
GstBus* bus;

gst_element_set_state(data.pipeline, GST_STATE_NULL);
bus = gst_element_get_bus(data.pipeline);
gst_bus_remove_signal_watch(bus);
gst_object_unref(bus);
gst_object_unref(data.pipeline);
}

收购类型

#include <gst/gst.h>
#include <QObject>
class AcquisitionType;
struct gstreamer_data {
GstElement* pipeline;
GstElement* udp_source;
GstElement* sink;
};

class AcquisitionType : public QObject
{
Q_OBJECT
public:
AcquisitionType(char const* address, gint port);
~AcquisitionType();
private:
static void bus_error_callback(GstBus* bus, GstMessage* message, gstreamer_data* user_data);
static void udp_source_timeout_callback(GstBus* bus, GstMessage* message, gstreamer_data* user_data);
gstreamer_data data;
};

如果按原样运行,则会调用回调。如果AcquisitionType acquisition("224.1.1.1", 5004);被移动到button->show()之后,则它不是。

我似乎需要将"timeout", 1000000000,更改为"timeout", G_GUINT64_CONSTANT(1000000000),

相关内容

  • 没有找到相关文章

最新更新