Search code examples
c++cgstreamer

GStreamer pipeline hangs on gst_element_get_state


I have following very basic code using GStreamer library (GStreamer v1.8.1 on Xubuntu 16.04 if it important)

#include <gst/gst.h>

int main(int argc, char *argv[])
{
    gst_init(&argc, &argv);

    const gchar* pd =
        "filesrc location=some.mp4 ! qtdemux name=d "
        "d.video_0 ! fakesink "
        "d.audio_0 ! fakesink ";

    GError* error = nullptr;
    GstElement *pipeline = gst_parse_launch(pd, &error);

    GstState state; GstState pending;
    switch(gst_element_set_state(pipeline, GST_STATE_PAUSED)) {
        case GST_STATE_CHANGE_FAILURE:
        case GST_STATE_CHANGE_NO_PREROLL:
            return -1;
        case GST_STATE_CHANGE_ASYNC: {
            gst_element_get_state(pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
        }
        case GST_STATE_CHANGE_SUCCESS:
            break;
    }

    GMainLoop* loop = g_main_loop_new(nullptr, false);
    g_main_loop_run(loop);

    gst_object_unref(pipeline);

    return 0;
}

The problem is when I try run this code it hangs on

gst_element_get_state(pipeline, &state, &pending, GST_CLOCK_TIME_NONE);

The question is - why it hangs? Especially if take into account, if I remove d.audio_0 ! fakesink from pipeline description it doesn't hang.


Solution

  • It is good practice to always add queues (or a multiqueue) after elements that produces multiple output branches in the pipeline e.g. demuxers.

    The reason is that sinks will block waiting for other sinks to receive the first buffer (preroll). With a single thread, as your code, it will block the only thread available to push data into the sinks. A single thread is going from the demuxers to both sinks, once 1 blocks the there is no way for data to arrive on the second sink.

    Using queues will spawn new threads and each sink will have a dedicated one.