最近需要用GSstreamer,由于之前没接触过,所以做个笔记,防止自己以后忘
在Ubuntu上安装GStreamer
apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good
gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc
gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3
gstreamer1.0-qt5 gstreamer1.0-pulseaudio
依赖的开发环境是gcc编译器和文本编译器,并且将此字符串添加到gcc命令中
pkg-config --cflags --libs gstreamer-1.0
由于我使用GStreamer主要是用视频库,所以我在上述字符串的gstreamer-1.0之后添加其他软件包(视频库的gstreamer-video-1.0)。
创建GStreamer应用
第一个GStreamer命令必须是下面的命令:
/* Initialize GStreamer */
gst_init (&argc, &argv);
它的作用是初始化应用的所有内部结构,用面向对象的术语,可以理解成是创建一个实例对象。
先整一个简单的例子:
GStreamer的Hello Word
关键Element的create
GstElement *pipeline, *source, *sink;
/* Create the elements */
source = gst_element_factory_make ("videotestsrc", "source");
sink = gst_element_factory_make ("autovideosink", "sink");
/* Create the empty pipeline */
pipeline = gst_pipeline_new ("test-pipeline");
gst_element_factory_make():
第一个参数是要创建的element的类型
第二个参数是我们想创建的element的名字,这个名字并非是必须的
GStreamer中的所有elements都必须在使用之前包含到pipeline中(还没搞懂为什么,暂时就理解为这是规定吧)
创建pipeline使用gst_pipeline_new()。
然后就是构建pipeline:
/* Build the pipeline */
gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
if (gst_element_link (source, sink) != TRUE) {
g_printerr ("Elements could not be linked.\n");
gst_object_unref (pipeline);
return -1;
}
一个pipeline就是一个特定类型的可以包含其他element的bin,而且所有可以用在bin上的方法也都可以用在pipeline上。在这个例子中,我调用了gst_bin_add_many()方法在pipeline中加入element。这个方法会接受一系列的element作为输入参数,最后由NULL来终止。
增加单个element的方法是gst_bin_add()
这个时候,这些刚增加的elements还没有互相连接起来。我们用gst_element_link()方法来把element连接起来,这个方法的第一个参数是源,第二个参数是目标,这个顺序不能搞错,因为这确定了数据的流向。记住只有在同一个bin里面的element才能连接起来,所以一定要把element在连接之前加入到pipeline中。
设置属性
/* Modify the source's properties */
g_object_set (source, "pattern", 0, NULL);
g_object_get()获得属性,用g_object_set()设置属性
g_object_set()方法接受一个用NULL结束的属性名称/属性值的组成的对,所以可以一次同时修改多项属性。上面的代码就修改了videotestsrc的“pattern”属性。
element的名字和取值范围,使用gst-inspect工具可以查询到
动态pipeline
动态的概念,就是不像上面那样非得在运行前就把pipeline全部创建好,而是在信息可用时动态地构建pipeline。
负责打开音视频的容器文件的element叫做demuxer,常见的容器格式包括MKV、QT、MOV、Ogg还有ASF、WMV、WMA等等。在一个容器中可能包含多个流(比如:一路视频,两路音频),demuxer会把他们分离开来,然后从不同的输出口送出来。这样在pipeline里面的不同的分支可以处理不同的数据。
GStreamer中使用术语pad描述element的接口。直接上图:
看图说话:数据从sink pad进入一个element,从source pad流出一个element;source element仅包含source pad,sink element仅包含sink pad,而filter element两种pad都包含。
一个demuxer包含一个sink pad和多个source pad,数据从sink pad输入然后每个流都有一个source pad
一个完整的pipeline结构示意图:
demuxer开始时是没有source pad给其他element连接用的。解决方法是只管建立pipeline,让source和demuxer连接起来,然后开始运行。当demuxer接收到数据之后它就有了足够的信息生成source pad。这时我们就可以继续把其他部分和demuxer新生成的pad连接起来,生成一个完整的pipeline
示例代码及注解:
#include <gst/gst.h>
/* Structure to contain all our information, so we can pass it to callbacks */
typedef struct _CustomData {
GstElement *pipeline;
GstElement *source;
GstElement *convert;
GstElement *resample;
GstElement *sink;
} CustomData; # 定义一个简单的数据结构
/* Handler for the pad-added signal */
static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);
int main(int argc, char *argv[]) {
CustomData data;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
gboolean terminate = FALSE;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Create the elements */
data.source = gst_element_factory_make ("uridecodebin", "source");
/*uridecodebin自己会在内部初始化必要的element,然后把一个URI变成一个原始音视频流输出,它差不多做了playbin2的一半工作。因为它自己带着demuxer,所以它的source pad没有初始化,我们等会儿会用到*/
data.convert = gst_element_factory_make ("audioconvert", "convert");
data.resample = gst_element_factory_make ("audioresample", "resample");
data.sink = gst_element_factory_make ("autoaudiosink", "sink");
/* Create the empty pipeline */
data.pipeline = gst_pipeline_new ("test-pipeline");
if (!data.pipeline || !data.source || !data.convert || !data.resample || !data.sink) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* Build the pipeline. Note that we are NOT linking the source at this
* point. We will do it later. */
gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.convert, data.resample, data.sink, NULL);
if (!gst_element_link_many (data.convert, data.resample, data.sink, NULL)) {
/*注意,我们没有把source连接起来——因为这个时候还没有source pad。我们把转换element和sink element连接起来后暂时就放在那里,等待后面在处理*/
g_printerr ("Elements could not be linked.\n");
gst_object_unref (data.pipeline);
return -1;
}
/* Set the URI to play */
g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
/* Connect to the pad-added signal */
/*g_signal_connect()把“pad-added”信号和我们的源(uridecodebin)联系了起来,并且注册了一个回调函数。GStreamer把&data这个指针的内容传给回调函数,这样CustomData这个数据结构中的数据也就传递了过去*/
g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
/* Start playing */
ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (data.pipeline);
return -1;
}
/* Listen to the bus */
bus = gst_element_get_bus (data.pipeline);
do {
/*开始监听ERROR或者EOS*/
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
/* Parse message */
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg, &err, &debug_info);
g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
terminate = TRUE;
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
terminate = TRUE;
break;
case GST_MESSAGE_STATE_CHANGED:
/* We are only interested in state-changed messages from the pipeline */
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
g_print ("Pipeline state changed from %s to %s:\n",
gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
}
break;
default:
/* We should not reach here */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
} while (!terminate);
/* Free resources */
gst_object_unref (bus);
gst_element_set_state (data.pipeline, GST_STATE_NULL);
gst_object_unref (data.pipeline);
return 0;
}
/* This function will be called by the pad-added signal */
static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {
GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");
GstPadLinkReturn ret;
GstCaps *new_pad_caps = NULL;
GstStructure *new_pad_struct = NULL;
const gchar *new_pad_type = NULL;
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
/* If our converter is already linked, we have nothing to do here */
if (gst_pad_is_linked (sink_pad)) {
g_print ("We are already linked. Ignoring.\n");
goto exit;
}
/* Check the new pad's type */
new_pad_caps = gst_pad_get_current_caps (new_pad);
/*gst_pad_get_caps()方法会获得pad的capability(也就是pad支持的数据类型),是被封装起来的GstCaps结构。一个pad可以有多个capability,GstCaps可以包含多个GstStructure,每个都描述了一个不同的capability*/
new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
/*在这个例子中,我们的pad需要的capability是声音,则使用gst_caps_get_structure()方法来获得GstStructure*/
new_pad_type = gst_structure_get_name (new_pad_struct);
/*用gst_structure_get_name()方法来获得structure的名字——最主要的描述部分。如果名字不是由audio/x-raw开始的,就意味着不是一个解码的音频数据,也就不是我们所需要的,反之,就是我们需要连接的*/
if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) {
g_print ("It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type);
goto exit;
}
/* Attempt the link */
ret = gst_pad_link (new_pad, sink_pad);
if (GST_PAD_LINK_FAILED (ret)) {
g_print ("Type is '%s' but link failed.\n", new_pad_type);
} else {
g_print ("Link succeeded (type '%s').\n", new_pad_type);
}
exit:
/* Unreference the new pad's caps, if we got them */
if (new_pad_caps != NULL)
gst_caps_unref (new_pad_caps);
/* Unreference the sink pad */
gst_object_unref (sink_pad);
}
pipeline状态
NULL | 元素的NULL状态或初始状态。 |
---|---|
READY | 元素已准备好,进入“暂停”状态。 |
PAUSED | 元素已暂停,就可以接受和处理数据了。但是,sink element仅接受一个缓冲区然后阻塞。 |
PLAYING | 元素正在播放,时钟正在运行,数据正在流动。 |
状态迁移只能在相邻的状态里迁移,也就是说,你不能从NULL一下跳到PLAYING。你必须经过READY和PAUSED状态。如果你把pipeline设到PLAYING状态,GStreamer自动会经过中间状态的过渡。