GStreamer学习笔记(一)

简介:
本文介绍如何使用GStreamer 编写一个简单的MP3播放器。

1,需要使用mad解码插件,因此需要先安装gstreamer0.10-plugins-ugly

2,编写mp3播放器

下面来看看如何利用GStreamer框架提供的组件,来实现一个简单的MP3播放器。数据源元件负责从磁盘上读取数据,过滤器元件负责对数据进行解码,而接受器元件则负责将解码后的数据写入声卡。

如果想要在程序中应用GStreamer提供的各种功能,首先必须在主函数中调用gst_init()来完成相应的初始化工作,以便将用户从命令行输入的参数传递给GStreamer函数库。一个典型的GStreamer应用程序的初始化如下所示:

#include <gst/gst.h>
int main (int argc, char *argv[])
{
  gst_init (&argc, &argv);
  /*  */
}
      接下去需要创建三个元件并连接成管道,由于所有GStreamer元件都具有相同的基类GstElement,因此能够采用如下方式进行定义:

  GstElement *pipeline, *filesrc, *decoder, *audiosink;
      管道在GStreamer框架中是用来容纳和管理元件的,下面的代码将创建一条名为pipeline的新管道:

  /* 创建用来容纳元件的新管道 */
  pipeline = gst_pipeline_new ("pipeline");
      数据源元件负责从磁盘文件中读取数据,它具有名为location的属性,用来指明文件在磁盘上的位置。使用标准的GObject属性机制可以为元件设置相应的属性:

/* 创建数据源元件 */
filesrc = gst_element_factory_make ("filesrc", "disk_source");
g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
      过滤器元件负责完成对MP3格式的数据进行解码,最简单的办法是安装mad这一插件,借助它来完成相应的解码工作:

/* 创建过滤器元件 */
decoder = gst_element_factory_make ("mad", "decoder"); 
      接收器元件负责将解码后的数据利用声卡播放出来:

/* 创建接收器元件 */
audiosink = gst_element_factory_make ("audiosink", "play_audio")
      已经创建好的三个元件需要全部添加到管道中,并按顺序连接起来:

/* 添加元件到管道中 */
gst_bin_add_many (GST_BIN (pipeline), filesrc, decoder, audiosink, NULL);
/* 通过衬垫连接元件 */
gst_element_link_many (filesrc, decoder, audiosink, NULL); 
所有准备工作都做好之后,就可以通过将管道的状态切换到PLAYING状态,来启动整个管道的数据处理流程:
/* 启动管道 */
gst_element_set_state (pipeline, GST_STATE_PLAYING); 
      这里加入一个消息处理函数bus_call来监视产生的消息

/* 终止管道 */
gst_element_set_state (pipeline, GST_STATE_NULL);
/* 释放资源 */
gst_object_unref (GST_OBJECT (pipeline));
3,完整的源代码如下所示:

复制代码
#include <gst/gst.h>
#include <glib.h>
//定义消息处理函数,
static gboolean bus_call(GstBus *bus,GstMessage *msg,gpointer data)
{
    GMainLoop *loop = (GMainLoop *) data;//这个是主循环的指针,在接受EOS消息时退出循环
    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:
        {
                           gchar *debug;
                           GError *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 <mp3 filename>\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.Exiting.\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;
}
复制代码
4,编译运行

gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) -g test2.c -o test2
./test2 /home/phinecos/test.mp3


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2009/06/07/1498166.html,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
Linux 编译器 Android开发
linux交叉编译nss3,nspr库精讲
linux交叉编译nss3,nspr库精讲
57 1
|
4月前
|
存储 编解码 Ubuntu
gstreamer部分命令
gstreamer部分命令
|
4月前
|
开发工具 git
vs2017下gstreamer编译
vs2017下gstreamer编译
|
9月前
实战--ffmpeg与sdl实现最简单的视频播放器
实战--ffmpeg与sdl实现最简单的视频播放器
69 0
|
11月前
|
编解码 文件存储 iOS开发
FFmpeg学习笔记汇总(下)
FFmpeg学习笔记汇总
156 0
|
11月前
|
编解码 Ubuntu 网络协议
FFmpeg学习笔记汇总
FFmpeg学习笔记汇总
133 0
|
Unix Linux API
开源库介绍:libusb 及其使用
开源库介绍:libusb 及其使用
2541 0
开源库介绍:libusb 及其使用
|
Linux Windows
LINUX编译alsa
LINUX编译alsa
94 0
|
存储 Linux 开发工具
rockchip的yocto编译环境的搭建
rockchip的yocto编译环境的搭建
702 0
rockchip的yocto编译环境的搭建
|
编解码 算法 Ubuntu
libx264开发笔记(一):libx264介绍、海思平台移植编译
libx264开发笔记(一):libx264介绍、海思平台移植编译
libx264开发笔记(一):libx264介绍、海思平台移植编译