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文件是事先準備好的)。

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