【GStreamer开发】GStreamer播放教程07——自定义playbin2的sink
目标
通过手动选择音频和视频的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, charchar *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);
剩下的就是均衡器的配置了。在这个例子中,两个高频波段设置了最大的衰减,这样就增强了低音修改不同的值来实际看看效果。
相关文章
- Java系统程序Bug解决方法论(一) - 教程简介
- 外行人也能看懂的Netty开发教程(1)-第一次接触
- 【GStreamer开发】GStreamer播放教程05——色彩平衡
- 【VS开发】免费打工仔:一个完善的ActiveX Web控件教程
- 【VS开发】免费打工仔:一个完善的ActiveX Web控件教程
- FL Studio20最详细的注册教程 附二十位序列号
- iMindMap12思维导图下载软件功能分享教程
- Spring入门详细教程(三)
- Guava教程
- WPF MVVM模式开发简明实现教程 6 其他绑定
- ArcGIS Runtime WPF(.net C#)开发简明教程 3 加载移动地图包MMPK
- 【一天一门编程语言】Go 语言程序设计极简教程
- Swoft 新手向教程 - 通过 Docker 搭建一个开发环境
- Elasticsearch教程(1) Query DSL入门
- Flink教程(5) DataStream 常用算子(下)
- Pico Neo3使用Unity开发简明教程
- Cocos Studio“回调特性“使用教程
- android开发教程之使用线程实现视图平滑滚动示例 改
- 【Unity游戏开发教程】零基础带你从小白到超神26——动画剪辑、挥拳和特效
- 很详细的Retrofit 2.0使用教程
- [ vulnhub靶机通关篇 ] 渗透测试综合靶场 DC-5 通关详解 (附靶机搭建教程)
- webrtc教程
- 【TypeScript教程】# 1:TS简介
- 阿里云centos7.6安装php7.3的详细教程
- Reveal使用教程
- 《Drools7.0.0.Final规则引擎教程》第4章 4.5RHS语法
- 《Drools7.0.0.Final规则引擎教程》第4章 4.4 LHS简介&Pattern