此博客是在gstreamer官网学习并总结的学习概要,具体参考gstreamer官网教程:基础教程 2:GStreamer 概念
实例
依然先运行官网给的实例,代码在下面(也可以在gstreamer代码中找到):
示例代码
#include <gst/gst.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
int
tutorial_main (int argc, char *argv[])
{
GstElement *pipeline, *source, *sink;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* 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");
if (!pipeline || !source || !sink) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* 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;
}
/* Modify the source's properties */
g_object_set (source, "pattern", 0, NULL);
/* 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;
}
/* 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);
/* 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);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
/* Free resources */
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
}
运行结果
概念
元素(elements)
元素(elements)是gstreamer里面的基础模块,一个最简单的pipeline如下:
source元素解析视频数据,通过filter元素,发送给sink模块播放
元素类型可参考链接: 简易元素
GstBin
bin是一个可以包含其他元素的元素(它的类型就是GstElement),
如何派生出来的:
GObject
╰──GInitiallyUnowned
╰──GstObject
╰──GstElement
╰──GstBin
╰──GstPipeline
从上面可以看出来,pipeline是bin的一种,同时它们也都是特殊的element。它们本质上都是由GObject派生出来的。
BUS
它是由元素生成的,用来向应用程序传递message的对象,它传递的message会依次发送给应用程序。应用程序需要一直关注bus,来获取对应的消息,可以通过函数gst_bus_timed_pop_filtered()
来获取,也可以使用信号来获取。
代码解析
元素创建
/* Create the elements */
source = gst_element_factory_make ("videotestsrc", "source");
sink = gst_element_factory_make ("autovideosink", "sink");
通过调用函数gst_element_factory_make()
可以创建一个元素,第一个入参是元素类型,第二个参数是元素名,如果元素名是NULL,系统会自动指定一个唯一的名字。
创建pipeline
/* Create the empty pipeline */
pipeline = gst_pipeline_new ("test-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是一种特殊的bin,所以适用于bin的接口也适用于pipeline。
函数gst_bin_add_many()
用来给bin加入多个元素,如果只加入一个元素,则可以调用函数gst_bin_add()
。
函数 gst_element_link()
用来关联两个元素,需要注意的是:要用来关联的两个元素必须在同一个bin里面,所以要先调用gst_bin_add()
再关联两个元素。
配置元素
/* Modify the source's properties */
g_object_set (source, "pattern", 0, NULL);
配置source元素的属性,属性名为pattern,属性值为0。
原生代码配置的属性结果已经放在上面的图里,修改这个属性值为1,则可以得到另一种结果:
如图所示,修改属性值之后,显示的图案变成了纯雪花。
元素公开的所有属性的名称和可能值可以使用 Basic tutorial 10: GStreamer tools 中描述的 gst-inspect-1.0 工具找到。
错误检查
/* 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返回失败则退出。
/* 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);
/* 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);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
通过gst_bus_timed_pop_filtered()
获取对应的事件,如果是ERROR,可以调用gst_message_parse_error()
函数获取具体错误。
课后练习
添加VertigoTV模块,这个模块具有旋转和缩放功能,此处,还加入了videoconvert模块,防止出现错误。
添加上述两个模块之后,显示的视频有了动态模糊效果。通过修改VertigoTV模块的speed参数,还可以修改动态模糊的速度。代码如下:
#include <gst/gst.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
int
tutorial_main (int argc, char *argv[])
{
GstElement *pipeline, *source, *sink, *vto, *vct;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Create the elements */
source = gst_element_factory_make ("videotestsrc", "source");
sink = gst_element_factory_make ("autovideosink", "sink");
/* my elements */
vto = gst_element_factory_make ("vertigotv", "myvto");
vct = gst_element_factory_make ("videoconvert", "myvct");
/* Create the empty pipeline */
pipeline = gst_pipeline_new ("test-pipeline");
if (!pipeline || !source || !sink || !vto || !vct) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* Build the pipeline */
//gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
gst_bin_add(GST_BIN (pipeline), source);
gst_bin_add(GST_BIN (pipeline), sink);
gst_bin_add(GST_BIN (pipeline), vto);
gst_bin_add(GST_BIN (pipeline), vct);
if (gst_element_link (source, vto) != TRUE) {
g_printerr ("Elements[source, vto] could not be linked.\n");
gst_object_unref (pipeline);
return -1;
}
if (gst_element_link (vto, vct) != TRUE) {
g_printerr ("Elements[vto, vct] could not be linked.\n");
gst_object_unref (pipeline);
return -1;
}
if (gst_element_link (vct, sink) != TRUE) {
g_printerr ("Elements[vct, sink] could not be linked.\n");
gst_object_unref (pipeline);
return -1;
}
/* Modify the source's properties */
g_object_set (source, "pattern", 0, NULL);
/* test: modify vto's speed */
g_object_set (vto, "speed", 0.1, NULL);
/* 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;
}
/* 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);
/* 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);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
/* Free resources */
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
}