GStreamer部署-Linux(ubuntu14.04)(已测试)

GStreamer部署-Linux(ubuntu14.04)(已测试)

标签:GStreamer


  • 环境

    ubuntu14.04

  • apt-get 命令方式安装

    sudo apt-get install libgstreamer0.10-dev gstreamer-tools gstreamer0.10-tools gstreamer0.10-doc

    sudo apt-get install gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-plugins-bad-multiverse

    apt-get install libgstreamer* // 该命令的目的是安装头文件;注意’*’

  • mp3播放器demo代码

    
    #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;
    }
  • 生成可执行文件

    上述代码保存在 test.c 文件中,之后执行下面的命令进行编译(网上很多其他的编译命令会报错):

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

    之后,当前目录下会生成一个test的可执行文件。
    【补充】网上很多都是: gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) test.c -o test ,这样就会产生因为命令的先后顺序带来的错误。原因如下:

    对于C/C++编译而言,读取编译选项是按照从左到右的顺序执行的。那么当编译器遇到源文件的时候,就开始对源文件中用到的函数进行解析,找到相对应的函数的函数体或者说是实现。这个过程是按照先遇到不能解析的函数,然后在源文件选项后面的一些选项中寻找可能的函数体的信息,是这样的一个顺序进行的。那么我们可以发现对于错误的命令,由于包含函数体或者函数定义信息的编译选项出现在源文件之前,那么当编译器在源文件中遇到不能解析的函数时,在源文件之后的选项中寻找相关的信息,那么,就出现了编译错误,也就是无法找到相关的函数定义。

  • 执行

    ./test test.mp3

    执行该命令之后就可以播放对应的mp3文件了(其中的test.mp3文件是事先准备好的)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章