GStreamer——教程——播放教程7:Custom playbin sinks

播放教程7:自定义playbin sink

目标

playbin可以通过手动选择其音频和视频sink进行进一步定制。这允许应用程序仅依赖playbin提取和解码媒体数据然后自行管理数据的渲染/演示。这篇教程展示了:

  • 如何替换playbin的sink。

  • 如何使用一条复杂的pipeline作为sink。

介绍

playbin的两个属性允许用户选择自己想要的audio和video sinks:audio-sinkvideo-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 XWindows、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-sinkvideo-sink属性指定playbin的sink element。

  • 如何将多个elements包裹为一个GstBin,使其能够像使用单个element一样被playbin使用。

  • 26
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值