GStreamer播放教程07——自定义playbin的sink

来源:互联网 发布:网络骗多少钱可立案 编辑:程序博客网 时间:2024/05/21 13:58

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

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

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

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


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

gcc playback-tutorial-7.c -o playback-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: Mac OS X, Windows, for iOS or for android.

This tutorial opens a window and displays a movie, with accompanying audio. The media is fetched from the Internet, so the window might take a few seconds to appear, depending on your connection speed. The higher frequency bands have been attenuated, so the movie sound should have a more powerful bass component.<

Required libraries: gstreamer-1.0


目标

      通过手动选择音频和视频的sink,playbin2可以进一步定制。这允许使用playbin2的应用在解码后可以自行做最终的渲染和显示。本教程展示了:

      如何替换playbin2选择的sink

      如何使用一个复杂的pipeline来作为sink


介绍

      playbin2有两个属性:audio-sink和video-sink。应用只需要实例化合适的element然后通过这两个属性传给playbin2就行了。

      这个方法只能使用一个简单地element来做sink。如果遇到的情况不是简单地element而是一个复杂的pipeline呢(比如均衡器加一个音频sink),就需要用Bin来包装一下,让playbin2感觉还是一个element。

      一个Bin就是一个容器,可以容纳一部分的pipeline,但操作的时候作为一个element。例如,我们在所有教程里面用得GstPipeline就是一个不会和外面的element做交互的GstBin。在Bin里面的element通过虚拟Pad(Ghost Pads)来和外面的element交互。这也就是说,Bin上的一个pad仅仅实现把外面的element上的pad的数据传到内部的element的pad上。


      GstBin也是element的一类,所以element可以用的地方,Bin也都能用,特别是,可以作为playbin2的sink。


一个均衡的播放器

#include <gst/gst.h>  int main(int argc, char *argv[]) {  GstElement *pipeline, *bin, *equalizer, *convert, *sink;  GstPad *pad, *ghost_pad;  GstBus *bus;  GstMessage *msg;    /* Initialize GStreamer */  gst_init (&argc, &argv);    /* Build the pipeline */  pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);    /* Create the elements inside the sink bin */  equalizer = gst_element_factory_make ("equalizer-3bands", "equalizer");  convert = gst_element_factory_make ("audioconvert", "convert");  sink = gst_element_factory_make ("autoaudiosink", "audio_sink");  if (!equalizer || !convert || !sink) {    g_printerr ("Not all elements could be created.\n");    return -1;  }    /* Create the sink bin, add the elements and link them */  bin = gst_bin_new ("audio_sink_bin");  gst_bin_add_many (GST_BIN (bin), equalizer, convert, sink, NULL);  gst_element_link_many (equalizer, convert, sink, NULL);  pad = gst_element_get_static_pad (equalizer, "sink");  ghost_pad = gst_ghost_pad_new ("sink", pad);  gst_pad_set_active (ghost_pad, TRUE);  gst_element_add_pad (bin, ghost_pad);  gst_object_unref (pad);    /* Configure the equalizer */  g_object_set (G_OBJECT (equalizer), "band1", (gdouble)-24.0, NULL);  g_object_set (G_OBJECT (equalizer), "band2", (gdouble)-24.0, NULL);    /* Set playbin2's audio sink to be our sink bin */  g_object_set (GST_OBJECT (pipeline), "audio-sink", bin, NULL);    /* Start playing */  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);    /* 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 inside the sink bin */  equalizer = gst_element_factory_make ("equalizer-3bands", "equalizer");  convert = gst_element_factory_make ("audioconvert", "convert");  sink = gst_element_factory_make ("autoaudiosink", "audio_sink");  if (!equalizer || !convert || !sink) {    g_printerr ("Not all elements could be created.\n");    return -1;  }

      生成所有的sink bin所需要的element。我们使用了一个equalizer-3bands和一个autoaudiosink,中间还用audioconvert连接起来。

  /* Create the sink bin, add the elements and link them */  bin = gst_bin_new ("audio_sink_bin");  gst_bin_add_many (GST_BIN (bin), equalizer, convert, sink, NULL);  gst_element_link_many (equalizer, convert, sink, NULL);

      这几句把新的element加到Bin里面然后连接起来,就和在pipeline中一样。

  pad = gst_element_get_static_pad (equalizer, "sink");  ghost_pad = gst_ghost_pad_new ("sink", pad);  gst_pad_set_active (ghost_pad, TRUE);  gst_element_add_pad (bin, ghost_pad);  gst_object_unref (pad);

      现在我们需要生成虚拟Pad,这一部分使在Bin里面的pipeline可以连接到外面。这个虚拟Pad会和内部的一个element的Pad连接起来,我们会用gst_element_get_static_pad()来获得这个Pad。如果是一个RequestPad而不是Always Pad的话,那么我们就用get_element_request_pad()方法,具体请参考《GStreamer基础教程07——多线程和Pad的有效性》

      虚拟Pad用gst_ghost_pad_new()来创建,用gst_pad_set_active()来激活。然后用gst_element_add_pad()来加入Bin。

      最后,我们用gst_object_unref()来释放获得的均衡器的sink pad。

      在这点上,我们有一个有sink功能的Bin,可以在playbin2里面用作音频sink,我们只需要设置一下playbin2的相关属性就行了。

  /* Set playbin2's audio sink to be our sink bin */  g_object_set (GST_OBJECT (pipeline), "audio-sink", bin, NULL);

      这和用一个sink element设置playbin2的audio-sink属性是完全一样的。

  /* Configure the equalizer */  g_object_set (G_OBJECT (equalizer), "band1", (gdouble)-24.0, NULL);  g_object_set (G_OBJECT (equalizer), "band2", (gdouble)-24.0, NULL);

      剩下的就是均衡器的配置了。在这个例子中,两个高频波段设置了最大的衰减,这样就增强了低音修改不同的值来实际看看效果。


Conclusion

This tutorial has shown:

  • How to set your own sinks to playbin using the audio-sink and video-sink properties.
  • How to wrap a piece of pipeline into a GstBin so it can be used as a sink-bin by playbin.

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


0 0