Decodebin是playbin的实际自动插件后端,想要做更深层次的解码定制工作,可以使用Decodebin来实现,下面是官方的例程代码。
#include <gst/gst.h>
[.. my_bus_callback goes here ..]
GstElement *pipeline, *audio;
static void
cb_newpad (GstElement *decodebin,
GstPad *pad,
gpointer data)
{
GstCaps *caps;
GstStructure *str;
GstPad *audiopad;
/* only link once */
audiopad = gst_element_get_static_pad (audio, "sink");
if (GST_PAD_IS_LINKED (audiopad)) {
g_object_unref (audiopad);
return;
}
/* check media type */
caps = gst_pad_query_caps (pad, NULL);
str = gst_caps_get_structure (caps, 0);
if (!g_strrstr (gst_structure_get_name (str), "audio")) {
gst_caps_unref (caps);
gst_object_unref (audiopad);
return;
}
gst_caps_unref (caps);
/* link'n'play */
gst_pad_link (pad, audiopad);
g_object_unref (audiopad);
}
gint
main (gint argc,
gchar *argv[])
{
GMainLoop *loop;
GstElement *src, *dec, *conv, *sink;
GstPad *audiopad;
GstBus *bus;
/* init GStreamer */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* make sure we have input */
if (argc != 2) {
g_print ("Usage: %s <filename>\n", argv[0]);
return -1;
}
/* setup */
pipeline = gst_pipeline_new ("pipeline");
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, my_bus_callback, loop);
gst_object_unref (bus);
//创建一个filesrc元素,命名为source
src = gst_element_factory_make ("filesrc", "source");
//设置属性名,及文件源
g_object_set (G_OBJECT (src), "location", argv[1], NULL);
//创建一个decodebin元素
dec = gst_element_factory_make ("decodebin", "decoder");
//将dec的pad-added操作与cb_newpad函数做信号连接
//pad类似是元素上的端口
g_signal_connect (dec, "pad-added", G_CALLBACK (cb_newpad), NULL);
//将src(filesrc),dec(decodebin)两个元素加入到pipeline上
gst_bin_add_many (GST_BIN (pipeline), src, dec, NULL);
//将两个元素连接
gst_element_link (src, dec);
/* create audio output */
audio = gst_bin_new ("audiobin");
//创建一个audioconvert作为音频输出转换操作
conv = gst_element_factory_make ("audioconvert", "aconv");
//获取转换接口的pad
audiopad = gst_element_get_static_pad (conv, "sink");
//创建alsasink作为输出
sink = gst_element_factory_make ("alsasink", "sink");
//给bin添加元素
gst_bin_add_many (GST_BIN (audio), conv, sink, NULL);
//将conv与sink连接,audioconvert--->alsasink
gst_element_link (conv, sink);
//给audio添加conv的pad
gst_element_add_pad (audio,
gst_ghost_pad_new ("sink", audiopad));
//减少audiopad的引用计数,当计数为0则销毁
//永远不要在保持LOCK的情况下调用unref方法,因为这可能会死锁dispose函数
gst_object_unref (audiopad);
//将audio加入到pipeline
gst_bin_add (GST_BIN (pipeline), audio);
/* run */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
/* cleanup */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}