Patrick
Patrick

Reputation: 187

g_signal_connect must be called before QObject::connect?

I am creating an application that combines GStreamer and Qt. It appears that if I use QObject::connect to connect a signal to a slot before I use g_signal_connect to register a callback function to events on the GStreamer bus, then the g_signal_connect callback function is never called. If I reverse the order it is. Is this expected?

Example:

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);
}

acquisitiontype.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;
};

If this is run as is, then the callback is called. If AcquisitionType acquisition("224.1.1.1", 5004); is moved to after button->show() then it is not.

Upvotes: 1

Views: 496

Answers (1)

Patrick
Patrick

Reputation: 187

It seems that I needed to change "timeout", 1000000000, to "timeout", G_GUINT64_CONSTANT(1000000000),.

Upvotes: 1

Related Questions