GStreamer基础教程07——多线程和Pad的有效性

来源:互联网 发布:淘宝为什么要交保证金 编辑:程序博客网 时间:2024/06/04 21:40

原文:https://gstreamer.freedesktop.org/documentation/tutorials/basic/index.html

译文原文:http://blog.csdn.net/sakulafly/article/details/21318313

原码:git clone git://anongit.freedesktop.org/gstreamer/gst-docs

编译方式:gcc basic-tutorial-7.c -o basic-tutorial-7`pkg-config --cflags --libs gstreamer-1.0`


Need help?

If you need help to compile this code, refer to the Building the tutorials section for your platform: Linux, Mac OS X or Windows, or use this specific command on Linux:

gcc basic-tutorial-7.c -o basic-tutorial-7 `pkg-config --cflags --libs gstreamer-1.0`

If you need help to run this code, refer to the Running the tutorials section for your platform: Linux, Mac OS X or Windows.

This tutorial plays an audible tone through the audio card and opens a window with a waveform representation of the tone. The waveform should be a sinusoid, but due to the refreshing of the window might not appear so.

Required libraries: gstreamer-1.0


目标

      GStreamer会自动处理多线程这部分,但在有些情况下,你需要手动对线程做解耦。本教程会教你怎样才能做到这一点,另外也展示了Pad的有效性。主要内容包括:

      如何针对部分的pipeline建立一个新的线程

      什么是Pad的有效性

      如何复制流


介绍

多线程

      GStreamer是一个支持多线程的框架。这就说明,如果有必要它会在内部自动创建/销毁线程。比如:在应用线程中把流解出来。而且,plugin在自身也可以任意的创建线程,比如一个视频解码器为了充分利用4核CPU的能力,可以创建4个线程。

      这里最重要的是,当应用在建立pipeline时可以明确一个branch(一部分pipeline)在另一个线程中运行(比如,让音频解码和视频解码同时运行)。

      这个可以用queue element来达到这个目的,运行的时候sink pad仅仅负责把数据放到queue里面,同时在另一个线程里把数据从queue里面取出并向下发送。这个element同样可以用来做缓冲,这点在后面讲述流的教程时可以看到。Queue的大小事可以用设置属性的方法来设置的。

一个pipeline例子

      这个例子建立的pipeline如下图所示。


      这里的源是一个合成的音频信号(一个连续的tone),这个音频信号会被tee element分成两路。一路发送信号到声卡,另一个就在屏幕上渲染一个波形。

      从图上看,queue会创建一个新的线程,所以整个pipeline有3个线程在运行。通常来说,有多于一个的sink element时就需要多个线程。这是因为在同步时,sink通常是阻塞起来等待所有其他的sink都准备好,如果仅仅只有一个线程是无法做到这一点的。

Request pads

      在《GStreamer基础教程03——动态pipeline》里面,我们看见了一个初始时没有pad的element(uridecodebin),pad会在数据流到element时才会出现。这种pad被成为Sometimes Pad,平时的那种一直存在的pad被称为Always Pad。

      第三种pad称为Request Pad,这是根据需要来建立的。经典的例子就是tee element——有1个输入pad而没有输出pad,需要有申请,tee  element才会生成。通过这种方法,输入流可以被复制成多份。和Always Pad比起来,Request Pad因为并非一直存在,所以是不能自动连接element的,这点在下面的例子中可以看到。

      另外,在PLAYING或PAUSED状态下去获得pad需要注意(Pad阻塞,本教程没有讲到这点),在NULL和READY状态去获得pad就没有这个问题。

      闲话少说,上代码。


简单地多线程例子

#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;  GstPadTemplate *tee_src_pad_template;  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 ("ffmpegcolorspace", "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", 3, 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_src_pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (tee), "src%d");  tee_audio_pad = gst_element_request_pad (tee, tee_src_pad_template, NULL, NULL);  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_request_pad (tee, tee_src_pad_template, NULL, NULL);  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;}

工作流程

  /* 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 ("ffmpegcolorspace", "csp");  video_sink = gst_element_factory_make ("autovideosink", "video_sink");
      上面图中所有的element都在这里创建了。

      audiotestsrc会创建一个合成的tone,wavescope会像示波器一样用一个音频信号来渲染出一个波形。我们前面已经用过autoaudiosink和autovideosink这两个element了。

      转换element(audio convert,audioresample和ffmpegcolorspace)也是必须的,它们可以保证pipeline可以正确地连接。事实上,音频和视频的sink的Caps是由硬件确定的,所以你在设计时是不知道audiotestsrc和wavescope是否可以匹配上。如果Caps能够匹配,这些element的行为就类似于直通——对信号不做任何修改,这对于效率的影响基本可以忽略不计。

  /* Configure elements */  g_object_set (audio_source, "freq", 215.0f, NULL);  g_object_set (visual, "shader", 0, "style", 3, NULL);
      为了更好的演示做了小小的调整:audiotestsrc的“freq”属性设置成215Hz,wavescope设置“shader”和“style”,让波形连续。用gst-inspect可以更好的了解这几个element的属性。

  /* 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;  }
      这块代码在pipeline里加入了所有的element并且把可以自动连接的element都连接了起来(就是Always Pad)。

  /* Manually link the Tee, which has "Request" pads */  tee_src_pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (tee), "src%d");  tee_audio_pad = gst_element_request_pad (tee, tee_src_pad_template, NULL, NULL);  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_request_pad (tee, tee_src_pad_template, NULL, NULL);  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);

      为了连接Request Pad,需要获得对element的“requesting”。一个element可能可以创建不同种类的Request Pad,所以,当请求Pad生成时,必须提供想要的Pad模板。Pad模板可以用gst_element_class_get_pad_template()方法来获得,而且用它们的名字来区分开。在tee element的文档里面我们可以看到两个pad模板,分别被称为“sink”(sink pad)和“src%d”(Request Pad)。

      一旦我们有了Pad模板,我们就用gst_element_request_pad()方法向tee请求两个Pad——分别给音频分支和视频分支。

      然后我们去获得下游的element需要连接Request Pad的Pad,这些通常都是Always Pad,所以我们用get_element_get_static_pad()方法去获得。

      最后,我们用gst_pad_link()方法把pad连接起来。在gst_element_link()和gst_element_link_many()方法里面也是调用这个函数来连接的。

      我们获得的这个sink pad需要通过gst_object_unref()来释放。Request Pad是在我们不需要的时候释放,也就是在程序的最后。

      就像平常一样,我们设置pipeline到PLAYING状态,等待一个错误消息或者EOS消息到达。剩下的所有事情就是清楚request pad。

  /* 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);

      gst_element_release_request_pad()可以释放tee的pad,但还需要调用gst_object_unref()才行。


Conclusion

This tutorial has shown:

  • How to make parts of a pipeline run on a different thread by using queue elements.
  • What is a Request Pad and how to link elements with request pads, with gst_element_class_get_pad_template(), gst_element_request_pad(), gst_pad_link() and gst_element_release_request_pad().
  • How to have the same stream available in different branches by using tee elements.
The next tutorial builds on top of this one to show how data can be manually injected into and extracted from a running pipeline.

It has been a pleasure having you here, and see you soon!

0 0