通过示例二可以学习:
1、什么是GStreamer对象及怎么创建
2、如何将元素互相连接
3、如何自定义用户的行为
4、如何观察总线的错误情况并从GStreamer消息中提取信息
#include <gst/gst.h>
/*
通过示例二可以学习:
1、什么是GStreamer对象及怎么创建
2、如何将元素互相连接
3、如何自定义用户的行为
4、如何观察总线的错误情况并从GStreamer消息中提取信息
*/
int
main(int argc, char* argv[])
{
GstElement* pipeline, * source, * sink;
GstBus* bus;
GstMessage* msg;
GstStateChangeReturn ret;
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Create the elements */
//创建新的元素 第一个参数是创建的元素类型;第二个参数是给这个特定实例起的名字。
//第二个参数:这个名字是非必须的,如果传入的为NULL,则GStreamer会自动创建一个名字。
source = gst_element_factory_make("videotestsrc", "source");
sink = gst_element_factory_make("autovideosink", "sink");
/* Create the empty pipeline */
//GStreamer中的所有元素通常必须包含在管道中才能使用,管道负责一些时钟和消息传递功能。
pipeline = gst_pipeline_new("test-pipeline");
//有一个创建不成功,就报错。
if (!pipeline || !source || !sink) {
g_printerr("Not all elements could be created.\n");
return -1;
}
/* Build the pipeline */
//将GstElement添加到pipeline中,无序添加
gst_bin_add_many(GST_BIN(pipeline), source, sink, NULL);
//这些元素还没有相互关联,使用 gst_element_link 函数进行关联。第一个参数是源,第二个参数是目标。
//只有位于同一个 bin 的元素可以连接在一起,所以链接之前要把他们添加到管道中。
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 */
//GStreamer 元素都是一种特殊的 GObject,大多数 GStreamer 元素具有可自定义的属性:
//可以修改命名属性以更改元素的行为(可写属性)或查询以了解元素的内部状态(可读属性)。
//以下代码改变了 videotestsrc 的“pattern”属性
g_object_set(source, "pattern", 0, NULL);
/* Start playing */
//检查 gst_element_set_state 的返回值是否有误
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;
}