Linux中gstreamer构建的简单方法
2014年09月20日 14:46:39 Linux ⁄ 共 2129字 暂无评论 ⁄ 被围观 2,060次

在博文“Gstreamer在Ubuntu上的安装和MP3的播放”中,写了在ubuntu上从头到尾构建gstreamer的详细过程,那是我在一次小项目培训中和队友一起努力了将近一周的成果,网上参考资料倒是很多,我们的步骤也是正确的,但由于机器的配置以及下载包的版本等问题,最后成功率很低,只有10%,不过心里还是挺高兴的。

 

下面着些是构建gstreamer的捷径,成功率在我们做后是100%,希望能给你带来帮助。不过前面的方法还是自己能动手做一遍好,虽然时间花费多,失败率高,不过做后感觉收获还是挺多的哈,O(∩_∩)O~

 

一、ubuntu下安装gstreamer库

libstreamer0.10-0
libstreamer0.10-dev
libstreamer0.10-0-dbg
二、程序源码

#include <gst/gst.h>
#include <glib.h>

//定义消息处理函数
static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = (GMainLoop *)data; //主循环指针,接受EOS消息时退出

gchar *debug;
GError *error;

switch (GST_MESSAGE_TYPE(msg))
{
case GST_MESSAGE_EOS:
g_print("End of stream.\n");
g_main_loop_quit(loop);
break;
case GST_MESSAGE_ERROR:

gst_message_parse_error(msg, &error, &debug);

g_free(debug);
g_printerr("Error: %s\n", error->message);
g_error_free(error);
g_main_loop_quit(loop);
break;
default:
break;
}

return TRUE;
}

int main(int argc, char *argv[])
{
GMainLoop *loop;
GstElement *pipeline, *source, *decoder, *sink; //定义组件
GstBus *bus;

gst_init(&argc, &argv);

loop = g_main_loop_new(NULL, FALSE); //创建主循环,在执行g_main_loop_run后正式开始循环

if (argc != 2)
{
g_printerr("Usage: %s  mp3filename\n", argv[0]);
return -1;
}

//创建管道和组件
pipeline = gst_pipeline_new("audio-player");
source = gst_element_factory_make("filesrc", "file-source");
decoder = gst_element_factory_make("mad", "mad-decoder");
sink = gst_element_factory_make("autoaudiosink", "audio-output");

if (!pipeline || !source || !decoder || !sink)
{
g_printerr("One element could not be created.\nExiting....\n");
return -1;
}

//设置source的location参数,即文件的地址
g_object_set(G_OBJECT(source), "location", argv[1], NULL);

//得到管道的消息总线
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));

//添加消息监视器
gst_bus_add_watch(bus, bus_call, loop);
gst_object_unref(bus);

//把组件添加到管道中,管道是一个特殊的组件,可以更好的让数据流动
gst_bin_add_many(GST_BIN(pipeline), source, decoder, sink, NULL);

//依次链接组件
gst_element_link_many(source, decoder, sink, NULL);

//开始播放
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_print("Running......\n");

//开始循环
g_main_loop_run(loop);
g_print("Returned, stopping playback\n");
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(pipeline));

return 0;
}

 

三、编译运行

gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) -g -o player player.c

./player test.mp3

Running......

给我留言

留言无头像?