我正在创建一个结合了GStreamer和Qt的应用程序。看来,如果我在使用g_signal_connect将回调函数注册到GStreamer总线上的事件之前使用QObject::connect将信号连接到插槽,则永远不会调用g_signal_connect回调函数。如果我颠倒顺序。这是预期的吗?
例:
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();
}
Acquisitiontype.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 called\n");
}
}
}
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: %s\n", GST_OBJECT_NAME(message->src), err->message);
g_printerr("Debugging information: %s\n", 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),
。
关于c++ - 必须在QObject::connect之前调用g_signal_connect?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61217649/