Search code examples
multithreadinggstreamerglibmainloop

Running every GStreamer pipeline into a separate (GLib) thread


All of the GStreamer samples are initializing GLib main thread through some form of:

loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);

As far as I understood this main loop is used for all the signals processing. Also Bus messages are processed in it.

So I'm a little bit concerned what will happen if I run multiple pipelines simultaneously. Or there is an issue/improper implementation in some of them.

Most probably for heavy loads the best solution is pipelines to be separated to multiple processes, thus mitigating all the possible problems with memory leaks, hangs, dead locks etc., without affecting the main application.

Anyway at least running them into separate threads will be beneficial.

Obviously it is possible to be started more than one GLib main thread, with creation of the GMainContext first. But I cannot understand (apparently I'm missing knowledge) how after that to "assign" them to pipelines or signaling to them, etc. For example in "g_signal_connect" and "g_signal_emit" is not specified on which "main" thread to be executed.

Some posts here claim it is possible (GStreamer supports different main thread), but I wasn't able to find details.

Similar problem is discussed in this thread but to be honest I wasn't able to understand it.

In this StackOverflow post is discussed how timeouts could be attached to different GLib main threads. I suppose that something similar could be made and for the GStreamer pipelines and objects, but I'm not sure.

Could someone enlighten me a little bit?


Solution

  • I'm posting here the answer of the same question in the GStreamer-devel forum: http://gstreamer-devel.966125.n4.nabble.com/Running-every-GStremer-pipeline-into-a-separate-GLib-thread-td4694469.html

    citate:

    GMainLoop is optional with GStreamer (convenient but optional). You can use the GstBus API directly. As for signals, these are synchronous, and not using a messageé.

    If you decide to use a GMainLoop, you will only need one to handle asynchronous messages, as all message gets serialized into the loop queue. Multiple pipeline is were that becomes convenient as you don't have to deal with multiple GstBus object.

    Streamer splits the streaming into seperate threads already. The mainloop thread is always free for other task (like UI task).

    :end of citate