*
// QT使用GStreamer需要添加在pro文件
CONFIG += link_pkgconfig
PKGCONFIG += gstreamer-1.0
*
*
gcc test_music.c -o test_music `pkg-config --cflags --libs gstreamer-1.0`
gst-launch -v alsasrc device=hw:0,0 ! alsasink // 录音
*
gst_init (&argc, &argv);
// 1.初始化所有内部数据结构 2.检查所有可用的插件 3.运行所有的命令行选项
*
pipeline = gst_parse_launch ("playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
GStreamer是设计来处理多媒体流的框架。媒体流经过一系列的中间element,从source element流到sink element。这些相互作用的element构成了一整个的pipeline。
使用GStreamer时你常常需要使用独立的elements来手动搭建一个pipeline,但是,在比较简单的情况下,我们也可以使用gst_parse_launch()。这个函数原本是描述一个pipeline的,但也可以很方便的用来建立一个pipeline。
playbin2是一个特殊的element,它既是一个source也是一个sink,同时也能处理整个pipeline的事务。在内部,他创建和链接了所有播放你的媒体所必须的elements,你完全不必担心
*
gst_element_set_state (pipeline, GST_STATE_PLAYING);
每一个GStreamer的element有一个状态,你可以理解成常见的DVD播放器上得播放/暂停按钮。播放器必须设置pipeline为PLAYING状态才能真正开始播放,
*
source = gst_element_factory_make ("videotestsrc", "source");// 建立element
sink = gst_element_factory_make ("autovideosink", "sink");
新的element的建立可以使用gst_element_factory_make()。这个API的第一个参数是要创建的element的类型,第二个参数是我们想创建的element的名字,这个名字并非是必须的,但在调试中会非常有用,如果你传入的时NULL,那么GStreamer会自动创建一个名字。
*
pipeline = gst_pipeline_new ("test-pipeline"); // 建立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中。
*
g_object_set (source, "pattern", 0, NULL); //属性
绝大部分的GStreamer elements有可以定制化的属性:只读的属性会显示element的内部状态,可写的属性会影响element的行为。我们用g_object_get()方法来获得属性,用g_object_set()方法来设置属性。
g_object_set()方法接受一个用NULL结束的属性名称/属性值的组成的对,所以可以一次同时修改多项属性。
*
data.source = gst_element_factory_make ("uridecodebin", "source");
data.convert = gst_element_factory_make ("audioconvert", "convert");
data.sink = gst_element_factory_make ("autoaudiosink", "sink");
我们像前面一样创建一个个element。uridecodebin自己会在内部初始化必要的element,然后把一个URI变成一个原始音视频流输出,它差不多做了playbin2的一半工作。因为它自己带着demuxer,所以它的source pad没有初始化,我们等会会用到。
audioconvert在不同的音频格式转换时很有用。这里用这个element是为了确保应用的平台无关性。
autoaudiosink和上一篇教程里面的autovideosink是非常相似的,只是操作的时音频流。这个element的输出就是直接送往声卡的音频流。
*
if (!gst_element_link (data.convert, data.sink)) {
g_printerr ("Elements could not be linked.\n");
gst_object_unref (data.pipeline);
return -1;
}
这里我们把转换element和sink element连接起来,注意,我们没有把source连接起来——因为这个时候还没有source pad。我们把转换element和sink element连接起来后暂时就放在那里,等待后面在处理。
*
// 使用URI通过设置属性的方法设置好
g_object_set (data.source, "uri", "http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
*
g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
GSignal是GStreamer的一个重要部分。它会让你在你感兴趣的事情发生时收到通知。信号是通过名字来区分的,每个GObject都有它自己的信号。
在这段代码里面,我们使用g_signal_connect()方法把“pad-added”信号和我们的源(uridecodebin)联系了起来,并且注册了一个回调函数。GStreamer把&data这个指针的内容传给回调函数,这样CustomData这个数据结构中的数据也就传递了过去。
这个信号是有GstElement产生的,可以在相关的文档中找到或者用gst-inspect方法来查到。
我们现在准备开始运行了!和前面的教程一样,把pipeline置成PLAYING状态,然后开始监听ERROR或者EOS。
*
pipeline = gst_pipeline_new ("test-pipeline"); // 建立pipeline
*
*
pipeline = gst_pipeline_new ("test-pipeline"); // 建立pipeline
*
*
pipeline = gst_pipeline_new ("test-pipeline"); // 建立pipeline
*
*
pipeline = gst_pipeline_new ("test-pipeline"); // 建立pipeline
*
*
/* Start playing */ //错误检查
ret = gst_element_set_state (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 (pipeline);
return -1;
}
*
pipeline = gst_pipeline_new ("test-pipeline"); // 建立pipeline
*
bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
gst_element_get_bus()会得到pipeline的总线,然后gst_bus_timed_pop_filtered()会阻塞直到你遇到一个错误或者流播放结束。
*
if (msg != NULL)
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
在你使用了一个函数后,一定要记得查阅文档来确定是否需要释放资源。在这个例子中,gst_bus_timed_pop_filtered()会返回一个message,这个需要调用gst_message_unref()来释放(下一讲会继续介绍)。
gst_element_get_bus()会对总线增加一个引用,所以也需要调用get_object_unref()来释放。设置pipeline为NULL状态会让它释放掉所有的资源,最后,释放掉pipeline自身。