GStreamer自动处理多线程,但在某些情况下,您可能需要手动分离线程。本教程展示了如何做到这一点,此外,完成了关于Pad可用性的说明。更准确地说,本文件解释了:
如何为管道的某些部分创建新的执行线程
Pad的可用性是多少
如何复制流
1. 引言
(1) 多线程
GStreamer是一个多线程框架。这意味着,在内部,它会根据需要创建和销毁线程,例如,将流与应用程序线程分离。此外,插件也可以自由创建线程来进行自己的处理,例如,一个视频解码器可以创建4个线程来充分利用一个具有4个内核的CPU。
除此之外,当构建管道时,应用程序可以显式指定分支(管道的一部分)在不同的线程上运行(例如,让音频和视频解码器同时执行)。
这是使用queue元素完成的,其工作原理如下。接收板只是将数据排队并返回控制。在另一个线程上,数据将出列并被推送到下游。此元素还用于缓冲,如后面的流教程所示。队列的大小可以通过属性控制。
(2) 管道的例子
此示例构建以下管道:

信号源是一个合成音频信号(一个连续的音调),使用一个tee元素分割(它通过它的源Pad发送它通过它的接收器Pad接收的所有信号)。然后,一个分支将信号发送到声卡,另一个分支呈现波形的视频并将其发送到屏幕。
如图所示,队列创建一个新线程,因此此管道在3个线程中运行。具有多个接收器的管道通常需要多线程,因为要进行同步,接收器通常会阻塞执行,直到所有其他接收器都准备就绪,并且如果只有一个线程被第一个接收器阻塞,则它们无法准备就绪。
(3) 请求Pad
在基础教程3:动态管道中,我们看到了一个元素(uridecodebin),它没有开始使用的pad,它们显示为数据开始流动,元素了解了媒体。这些被称为Sometimes Pads,与通常总是可用的Pad形成对比,通常被称为Always Pads。
第三种pad是请求pad,它是按需创建的。典型的例子是tee元素,它有一个sink pad而没有初始源pad:需要请求它们,然后tee添加它们。这样,输入流可以被复制任意次数。缺点是,将元素与请求pad链接并不像本例的演练所显示的那样自动,因为链接总是pad。
此外,要在播放或暂停状态下请求(或释放)Pad,您需要采取本教程中未描述的其他注意事项(Pad阻塞)。不过,在空或就绪状态下请求(或释放)Pad是安全的。
别再耽搁了,让我们看看代码。
2. 简单的多线程示例
将此代码复制到名为basic-tutorial-7.c的文本文件中(或在GStreamer安装中找到它)。
basic-tutorial-7.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <gst/gst.h> int main(int argc, char *argv[]) { GstElement *pipeline, *audio_source, *tee, *audio_queue, *audio_convert, *audio_resample, *audio_sink; GstElement *video_queue, *visual, *video_convert, *video_sink; GstBus *bus; GstMessage *msg; GstPad *tee_audio_pad, *tee_video_pad; GstPad *queue_audio_pad, *queue_video_pad; /* Initialize GStreamer */ gst_init (&argc, &argv); /* Create the elements */ audio_source = gst_element_factory_make ("audiotestsrc", "audio_source"); tee = gst_element_factory_make ("tee", "tee"); audio_queue = gst_element_factory_make ("queue", "audio_queue"); audio_convert = gst_element_factory_make ("audioconvert", "audio_convert"); audio_resample = gst_element_factory_make ("audioresample", "audio_resample"); audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink"); video_queue = gst_element_factory_make ("queue", "video_queue"); visual = gst_element_factory_make ("wavescope", "visual"); video_convert = gst_element_factory_make ("videoconvert", "csp"); video_sink = gst_element_factory_make ("autovideosink", "video_sink"); /* Create the empty pipeline */ pipeline = gst_pipeline_new ("test-pipeline"); if (!pipeline || !audio_source || !tee || !audio_queue || !audio_convert || !audio_resample || !audio_sink || !video_queue || !visual || !video_convert || !video_sink) { g_printerr ("Not all elements could be created.\n"); return -1; } /* Configure elements */ g_object_set (audio_source, "freq", 215.0f, NULL); g_object_set (visual, "shader", 0, "style", 1, NULL); /* Link all elements that can be automatically linked because they have "Always" pads */ gst_bin_add_many (GST_BIN (pipeline), audio_source, tee, audio_queue, audio_convert, audio_resample, audio_sink, video_queue, visual, video_convert, video_sink, NULL); if (gst_element_link_many (audio_source, tee, NULL) != TRUE || gst_element_link_many (audio_queue, audio_convert, audio_resample, audio_sink, NULL) != TRUE || gst_element_link_many (video_queue, visual, video_convert, video_sink, NULL) != TRUE) { g_printerr ("Elements could not be linked.\n"); gst_object_unref (pipeline); return -1; } /* Manually link the Tee, which has "Request" pads */ tee_audio_pad = gst_element_get_request_pad (tee, "src_%u"); g_print ("Obtained request pad %s for audio branch.\n", gst_pad_get_name (tee_audio_pad)); queue_audio_pad = gst_element_get_static_pad (audio_queue, "sink"); tee_video_pad = gst_element_get_request_pad (tee, "src_%u"); g_print ("Obtained request pad %s for video branch.\n", gst_pad_get_name (tee_video_pad)); queue_video_pad = gst_element_get_static_pad (video_queue, "sink"); if (gst_pad_link (tee_audio_pad, queue_audio_pad) != GST_PAD_LINK_OK || gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK) { g_printerr ("Tee could not be linked.\n"); gst_object_unref (pipeline); return -1; } gst_object_unref (queue_audio_pad); gst_object_unref (queue_video_pad); /* Start playing the pipeline */ gst_element_set_state (pipeline, GST_STATE_PLAYING); /* Wait until error or EOS */ bus = gst_element_get_bus (pipeline); msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS); /* Release the request pads from the Tee, and unref them */ gst_element_release_request_pad (tee, tee_audio_pad); gst_element_release_request_pad (tee, tee_video_pad); gst_object_unref (tee_audio_pad); gst_object_unref (tee_video_pad); /* Free resources */ if (msg != NULL) gst_message_unref (msg); gst_object_unref (bus); gst_element_set_state (pipeline, GST_STATE_NULL); gst_object_unref (pipeline); return 0; } |
3. 代码详解
1 2 3 4 5 6 7 8 9 10 11 | /* Create the elements */ audio_source = gst_element_factory_make ("audiotestsrc", "audio_source"); tee = gst_element_factory_make ("tee", "tee"); audio_queue = gst_element_factory_make ("queue", "audio_queue"); audio_convert = gst_element_factory_make ("audioconvert", "audio_convert"); audio_resample = gst_element_factory_make ("audioresample", "audio_resample"); audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink"); video_queue = gst_element_factory_make ("queue", "video_queue"); visual = gst_element_factory_make ("wavescope", "visual"); video_convert = gst_element_factory_make ("videoconvert", "video_convert"); video_sink = gst_element_factory_make ("autovideosink", "video_sink"); |
上图中的所有元素都在这里实例化:
转换元素(
1 2 3 | /* Configure elements */ g_object_set (audio_source, "freq", 215.0f, NULL); g_object_set (visual, "shader", 0, "style", 1, NULL); |
为了更好地演示,进行了一些小的调整:
1 2 3 4 5 6 7 8 9 10 | /* Link all elements that can be automatically linked because they have "Always" pads */ gst_bin_add_many (GST_BIN (pipeline), audio_source, tee, audio_queue, audio_convert, audio_sink, video_queue, visual, video_convert, video_sink, NULL); if (gst_element_link_many (audio_source, tee, NULL) != TRUE || gst_element_link_many (audio_queue, audio_convert, audio_sink, NULL) != TRUE || gst_element_link_many (video_queue, visual, video_convert, video_sink, NULL) != TRUE) { g_printerr ("Elements could not be linked.\n"); gst_object_unref (pipeline); return -1; } |
这个代码块将所有元素添加到管道中,然后链接那些可以自动链接的元素(如注释所示,带有Always Pads的元素)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* Manually link the Tee, which has "Request" pads */ tee_audio_pad = gst_element_get_request_pad (tee, "src_%u"); g_print ("Obtained request pad %s for audio branch.\n", gst_pad_get_name (tee_audio_pad)); queue_audio_pad = gst_element_get_static_pad (audio_queue, "sink"); tee_video_pad = gst_element_get_request_pad (tee, "src_%u"); g_print ("Obtained request pad %s for video branch.\n", gst_pad_get_name (tee_video_pad)); queue_video_pad = gst_element_get_static_pad (video_queue, "sink"); if (gst_pad_link (tee_audio_pad, queue_audio_pad) != GST_PAD_LINK_OK || gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK) { g_printerr ("Tee could not be linked.\n"); gst_object_unref (pipeline); return -1; } gst_object_unref (queue_audio_pad); gst_object_unref (queue_video_pad); |
要链接请求板,需要通过“请求”它们到元素来获取它们。元素可以生成不同类型的请求Pad,因此,在请求它们时,必须提供所需的Pad模板名称。在tee元素的文档中,我们看到它有两个pad模板,名为“sink”(用于它的sink pad)和“src_%u”(用于请求pad)。我们使用gst_element_get_request_pad()从tee(用于音频和视频分支)请求两个pad。
然后,我们从这些请求pad需要链接到的下游元素获取pad。这些都是普通的Always pad,因此我们使用gst_element_get_static_pad()获得它们。
最后,我们将pads与gst_pad_link()链接。这是gst_element_link()和gst_element_link_many()在内部使用的函数。
我们获得的水槽垫需要用gst_object_unref()释放。在程序结束时,当我们不再需要请求板时,请求板将被释放。
然后我们将管道设置为照常播放,并等待生成错误消息或EOS。唯一要做的就是清理请求的焊盘:
1 2 3 4 5 | /* Release the request pads from the Tee, and unref them */ gst_element_release_request_pad (tee, tee_audio_pad); gst_element_release_request_pad (tee, tee_video_pad); gst_object_unref (tee_audio_pad); gst_object_unref (tee_video_pad); |
4. 小结
本教程显示:
- 如何使用队列元素使部分管道在不同的线程上运行。
- 什么是请求板以及如何将元素与请求板链接,使用gst_element_get_Request_Pad()、gst_Pad_link()和gst_element_release_Request_Pad()。
- 如何使用tee元素在不同的分支中使用相同的流。
下一个教程将在此基础上构建,以展示如何手动将数据注入正在运行的管道并从中提取数据。欢迎阅读本课程,咱们下次见!