2011-11-04 20 views
6

我試圖使用的GStreamer從文件播放MP4視頻。我已成功使用playbin2播放的文件,並從命令提示符下使用:的GStreamer:如何連接動態墊

gst-launch filesrc location=bbb.mp4 ! decodebin2 ! autovideosink 

我期待在未來,我將需要創建更復雜的管道,因此爲什麼我試圖「計劃」的管道。在我的計劃,我試圖複製上面的管道,但是我有我懷疑是關係到動態或「有時」源decodebin2的焊盤連接到autovideo下沉的問題。我使用這些元素只是爲了讓事情儘可能簡單。

static void on_new_decoded_pad(GstElement* object, 
          GstPad* arg0, 
          gboolean arg1, 
          gpointer user_data) 
{ 
    // dynamically connect decoderbin2 src pad to autovideosink sink pad 
} 

static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data) 
{ 
    // handle bus messages 
} 

int main(int argc, char *argv[]) 
{ 
    GMainLoop *loop; 
    GstElement *pipeline, *source, *decodebin, *videosink; 
    GstBus *bus; 

    gst_init (&argc, &argv); 
    loop = g_main_loop_new (NULL, FALSE); 

    pipeline = gst_pipeline_new ("pipeline"); 
    source = gst_element_factory_make("filesrc",  "source"); 
    decodebin = gst_element_factory_make("decodebin2", "decodebin"); 
    videosink = gst_element_factory_make("autovideosink", "videosink"); 

    /* check elements were created successfully */ 
    if (!pipeline || !source || !decodebin || !videosink) { 
     // Failed to create element. Exit Program 
     return -1; 
    } 

    /* apply properties to elements before adding to pipeline */ 
    gchar * filename = "bbb.mp4"; 
    g_object_set(G_OBJECT(source), "location", filename, NULL); 

    /* add a message handler */ 
    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); 
    gst_bus_add_watch (bus, bus_call, loop); 
    gst_object_unref (bus); 

    /* add elements to pipeline (and bin if necessary) before linking them */ 
    gst_bin_add_many(GST_BIN (pipeline), 
        source, 
        decodebin, 
        videosink, 
        NULL); 

    gst_element_link_pads(source, "src", decodebin, "sink"); 

    /* decodebins src pad is a sometimes pad - it gets created dynamically */ 
    g_signal_connect(decodebin, "new-decoded-pad", G_CALLBACK(on_new_decoded_pad), videosink); 

    /* run pipeline */ 
    gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING); 

    g_main_loop_run(loop); 

    gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_NULL); 
    gst_object_unref (pipeline); 

    return 0; 
} 

我希望當我運行這個程序,會發生什麼,是爲on_new_decoded_pa​​d獲得通過一個回調函數,這是該行成立名爲:

g_signal_connect(decodebin, "new-decoded-pad", G_CALLBACK(on_new_decoded_pad), videosink); 

,並可以讓我適當地連接焊盤。但它永遠不會被調用。實際上,程序似乎完全通過,然後退出(主循環什麼都不做)。

我會很感激,如果有人能指出我做了什麼不對的問候回調或說明需要爲了完成這個例子使用的元件,以播放mp4什麼。

問候。

回答

3

見我的RTP電話例子 [here]。在那裏使用Rtpbin。希望這可以幫助。

+0

obalex,感謝您的應對哪怕是有點晚了(我可以用這個真的做了5個月前:P)不過,我有一個快速瀏覽一下你的代碼,它看起來非常好寫的。我認爲這對其他人很有用+1。 – user975326