第一个例程
#include <gst/gst.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
int
tutorial_main (int argc, char *argv[])
{
GstElement *pipeline;
GstBus *bus;
GstMessage *msg;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Build the pipeline */
pipeline =
gst_parse_launch
("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
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);
/* See next tutorial for proper error message handling/parsing */
if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
g_error ("An error occurred! Re-run with the GST_DEBUG=*:WARN environment "
"variable set for more details.");
}
/* Free resources */
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
return 0;
}
int
main (int argc, char *argv[])
{
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
return gst_macos_main (tutorial_main, argc, argv, NULL);
#else
return tutorial_main (argc, argv);
#endif
}
关键函数gst_parse_launch和playbin
gst_parse_launch构建管道函数
Gstreamer是一个设计处理多媒体流的框架。媒体从 源(source)元素(生产者)传播到接收(sink)元素(消费者),经过一系列的中间元素执行各种任务。中间的元素被称为管道(pipeline)。
在Gsteamer里,你构建管道通过聚集一些独立的元素。当管道足够简单时,不需要一些高级功能,你可以通过gst_parse_launch走捷径,构建管道。
这个函数需要一个文本表达的管道,并把它变成真实的管道,非常方便。事实上,这个函数非常方便,至于有一个工具构建它,将会非常熟悉它。gstreamer工具gst-launch-1.0和gst-launch-1.0语法。
playbin
通过构建管道调用playbin组成单个元素。
playbin即可作为source也可以作为sink一个特殊的元素,亦可是一整个管道。内部的,它能够创建和链接所有必要的元素来播放你的媒体。无需担心。
不允许手动管道所具有的控件粒度,满足广泛的应用范围。包括了这个指导。
在第一个hello world例子中,我们只用一个参数playbin,设置我们想要播放的媒体URI。无论它是一个http:// 还是一个 **file://**的URI,playbin将透明地恰当实例化gstreamer源。
/* Build the pipeline */
pipeline =
gst_parse_launch
("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
NULL);
如果错误输入URL或者文件不存在,gstreamer将一些提醒机制。每一个gstreamer元素有一相关的状态,你可以或多或少地把它想象成你常用的 DVD 播放器中的播放/暂停按钮。足以说明,播放不会开始,除非设置管道播放PALYING状态。如下
/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
这行将等待一个错误出现或者流的结束被发现。
gst_element_get_bus()恢复管道的总线,
gst_bus_timed_pop_filtered()会阻塞直到你接收到一个ERROR或者EOS(End Of Stream)在总线上经过。
总线概念将在下一章讲解。
/* 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);
/* See next tutorial for proper error message handling/parsing */
if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
g_error ("An error occurred! Re-run with the GST_DEBUG=*:WARN environment "
"variable set for more details.");
}
就是这样! 从现在开始,GStreamer 负责一切。
释放资源
在终端结束之前,需要清理自己资源。
/* Free resources */
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gst_message_unref()
总结
- 初始化gsteamer用法gst_init()
- 根据文本描述快速构建管道用法gst_parse_launch().
- 创建自动化回放管道playbin.
- 开始回放gsteamer信号用法gst_element_set_state().
- Gsteamer处理事情gst_element_get_bus() and gst_bus_timed_pop_filtered().