gstreamer:创建组件、管道和总线,实现简单的播放器(Makefile,代码测试通过)
老本行了。
- 安装
sudo apt-get install -y \gstreamer1.0-tools gstreamer1.0-libav \gstreamer1.0-plugins-base gstreamer1.0-plugins-ugly \gstreamer1.0-plugins-good gstreamer1.0-plugins-bad
- 代码(已测试):
#include <gst/gst.h>
#include <stdio.h>
#include <stdlib.h>int main (int argc, char *argv[])
{GstElement *pipeline, *source, *sink; GstBus *bus;GstMessage *msg;GstStateChangeReturn ret;// 步骤一:初始化gstgst_init (&argc, &argv);// 步骤二:创建组件source = gst_element_factory_make("videotestsrc", "source");sink = gst_element_factory_make("autovideosink", "sink");// 步骤三:创建空管道pipeline = gst_pipeline_new("test-pipeline");if(!pipeline || !source || !sink){g_printerr("Not all elements could be created.\n");return -1;}// 步骤四:管道连接组件,此处连接:源组件、接收组件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;}// 步骤五:修改源组件属性g_object_set(source, "pattern", 0, NULL);// 步骤六:设置组件状态PALYING,开始播放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;}// 步骤七:获取bus总线,阻塞函数直至总线触发错误或流结束后继续bus = gst_element_get_bus(pipeline);msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);// 步骤八:处理返回消息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:g_printerr("Unexpected message received.\n");break;}gst_message_unref(msg);}// 步骤九:释放资源gst_object_unref(bus);gst_element_set_state(pipeline, GST_STATE_NULL);gst_object_unref(pipeline);return 0;
}
- Makefile
CFLAGS = `pkg-config --cflags gstreamer-1.0`
LDFLAGS = `pkg-config --libs gstreamer-1.0`test: test.ogcc $^ -o $@ $(LDFLAGS)%.o: %.cgcc -g $(CFLAGS) -o $@ -c $^clean:rm test *.o
- 编译运行
make
./test