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

问题描述 投票:0回答:1

我正在创建一个结合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();
}

采集类型.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);
}

采集类型.h

#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() 那么它就不是。

c++ qt qt5 gstreamer
1个回答
0
投票

似乎我需要改变 "timeout", 1000000000,"timeout", G_GUINT64_CONSTANT(1000000000),.

© www.soinside.com 2019 - 2024. All rights reserved.