播放教程7:自定义playbin sink
目标
playbin
可以通过手动选择其音频和视频sink进行进一步定制。这允许应用程序仅依赖playbin
提取和解码媒体数据然后自行管理数据的渲染/演示。这篇教程展示了:
-
如何替换
playbin
的sink。 -
如何使用一条复杂的pipeline作为sink。
介绍
playbin
的两个属性允许用户选择自己想要的audio和video sinks:audio-sink
和video-sink
。应用程序仅需要初始化适当的GstElement
并将其传递给playbin
的这两个属性。
然而这个属性仅允许使用单个element作为sink。如果需要使用更加复杂的pipeline,例如一个均衡器加上一个audio sink,它们需要被包裹在一个bin中,这样对于playbin
来说,这个bin看起来就像一个独立的element。
一个Bin(GstBin
)是一个封装了部分pipeline的容器,通过bin这部分pipeline元素能够以一个独立的element的形式管理。例如,我们在所有教程中使用的GstPipeline
实际是就是一个GstBin
,只是它不再与其他外部element交互,即GstPipeline
是最上(外)层的GstBin
。Bin中的elements通过一个GstGhostPad
与外部elements连接,GstGhostPad
是一个仅将数据简单的从外部pad传递给指定的内部pad的接口。
图1:带有两个元素和一个 Ghost Pad.
GstBin也是element的一类,所以element可以用的地方,Bin也都能用,特别是,可以作为playbin的sink。
一个均衡的播放器
将此代码复制到名为playback-tutorial-7.c
文本文件中。
playback-tutorial-7.c
#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 ("playbin uri=https://gstreamer.freedesktop.org/data/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 playbin'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;
}
如果您需要帮助来编译此代码,请参阅 构建教程部分为您的平台:Mac或 Windows或在Linux上使用此特定命令:
gcc playback-tutorial-7.c -o playback-tutorial-7 `pkg-config --cflags --libs gstreamer-1.0`
如果您需要帮助来运行此代码,请参阅为您的平台运行教程部分:Mac OS X、Windows、for iOS或安卓。
本教程会打开一个窗口,显示一部电影,并附带音频。媒体是从Internet上获取的,因此窗口可能需要几秒钟才能出现,具体取决于您的连接速度。较高的频段已经衰减,因此电影声音应该有更强大的低音分量。<
所需库:
gstreamer-1.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的elements。我们使用了一个equalizer-3bands
和一个autoaudiosink
插件,这两个插件中间插入了一个audioconvert
,因为我们并不确定audio sink会需要什么样的capabilities(因为audio sink可能有硬件依赖)。
/* 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);
将所有的新建的elements加到一个bin中,并像链接pipeline elements一样连接他们。
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);
现在我们需要为这个bin创建一个GstGhostPad
,从而让这个bin能够与外部连接。这个Ghost Pad将与bin中的equalizer-3bands
的sink-pad连接,这个sink-pad可以使用gst_element_get_static_pad()
获取到。
Ghost Pad可以使用gst_ghost_pad_new()
创建(它将指向我们指定的GstBin内部的pad),并使用gst_pad_set_active()
使其生效。然后使用gst_element_add_pad()
将其加入bin中,将Ghost Pad的控制权转移给bin,然后我们不需要关系它的释放。
到此为止,我们拥有了一个功能性的sink-bin,我们能像使用audio sink一样使用它。我们只需要将其与playbin
连接:
/* Set playbin's audio sink to be our sink bin */
g_object_set (GST_OBJECT (pipeline), "audio-sink", bin, NULL);
只需将playbin
上的audio-sink
属性设置为新创建的sink bin即可。
/* 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);
剩下的就是均衡器的配置了。在这个例子中,两个高频波段设置了最大的衰减,这样就增强了低音修改不同的值来实际看看效果。
执行
使用多种方法之一构建视频箱而不是音频箱 GStreamer提供有趣的视频过滤器,比如solarize
, vertigotv
或effectv插件中的任何effectv
。记住使用颜色空间转换元素 videoconvert
如果caps不兼容,管道无法链接。
结论
本教程显示:
-
如何通过
audio-sink
和video-sink
属性指定playbin
的sink element。 -
如何将多个elements包裹为一个
GstBin
,使其能够像使用单个element一样被playbin
使用。