在之前的文章中,咱們瞭解到了2種播放文件的方式:一種是在知道了文件的類型及編碼方式後,手動建立所需Element並構造Pipeline;另外一種是直接使用playbin,由playbin內部動態建立所需Element並鏈接Pipeline。很明顯使用playbin的方式更加靈活,咱們不須要在一開始就建立各類Pipeline,只需由playbin內部根據文件類型,自動構造Pipeline。 在瞭解了Pad的做用後,本文經過一個例子來了解如何經過Pad事件動態的鏈接Pipeline,爲了解playbin內部是如何動態建立Pipeline打下基礎。html
在本章的例子中,咱們在將Pipeline設置爲PLAYING狀態以前,不會將全部的Element都鏈接起來,這種處理方式是能夠的,但須要額外的處理。若是在設置PLAYING狀態後不作任何操做,數據沒法到達Sink,Pipeline會直接拋出一個錯誤並退出。若是在收到相應事件後,對其進行處理,並將Pipeline鏈接起來,Pipeline就能夠正常工做。ios
咱們常見的媒體,音頻和視頻都是經過某一種容器格式被包含中同一個文件中。播放時,咱們須要將音視頻數據分離出來,一般將具有這種功能的模塊稱爲分離器(demuxer)。web
GStreamer針對常見的容器提供了相應的demuxer,若是一個容器文件中包含多種媒體數據(例如:一路視頻,兩路音頻),這種狀況下,demuxer會爲些數據分別建立不一樣的Source Pad,每個Source Pad能夠被認爲一個處理分支,能夠建立多個分支分別處理相應的數據。ide
gst-launch-1.0 filesrc location=sintel_trailer-480p.ogv ! oggdemux name=demux ! queue ! vorbisdec ! autoaudiosink demux. ! queue ! theoradec ! videoconvert ! autovideosink
經過上面的命令播放文件時,會建立具備2個分支的Pipeline:函數
使用demuxer須要注意的一點是:demuxer只有在收到足夠的數據時才能肯定容器中包含哪些媒體信息,所以demuxer開始沒有Source Pad,因此其餘的Element沒法在Pipeline建立時就鏈接到demuxer。
解決這種問題的辦法是:在建立Pipeline時,咱們只將Source Element到demuxer之間的Elements鏈接好,而後設置Pipeline狀態爲PLAYING,當demuxer收到足夠的數據能夠肯定文件總包含哪些媒體流時,demuxer會建立相應的Source Pad,並經過事件告訴應用程序。咱們能夠經過監聽demuxer的事件,在新的Source Pad被建立時,咱們根據數據類型,建立相應的Element,再將其鏈接到Source Pad,造成完整的Pipeline。工具
爲了簡化邏輯,咱們在本示例中會忽略視頻的Source Pad,僅鏈接音頻的Source Pad。源碼分析
#include <gst/gst.h> /* Structure to contain all our information, so we can pass it to callbacks */ typedef struct _CustomData { GstElement *pipeline; GstElement *source; GstElement *convert; GstElement *sink; } CustomData; /* Handler for the pad-added signal */ static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data); int main(int argc, char *argv[]) { CustomData data; GstBus *bus; GstMessage *msg; GstStateChangeReturn ret; gboolean terminate = FALSE; /* Initialize GStreamer */ gst_init (&argc, &argv); /* Create the elements */ data.source = gst_element_factory_make ("uridecodebin", "source"); data.convert = gst_element_factory_make ("audioconvert", "convert"); data.sink = gst_element_factory_make ("autoaudiosink", "sink"); /* Create the empty pipeline */ data.pipeline = gst_pipeline_new ("test-pipeline"); if (!data.pipeline || !data.source || !data.convert || !data.sink) { g_printerr ("Not all elements could be created.\n"); return -1; } /* Build the pipeline. Note that we are NOT linking the source at this * point. We will do it later. */ gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.convert , data.sink, NULL); if (!gst_element_link (data.convert, data.sink)) { g_printerr ("Elements could not be linked.\n"); gst_object_unref (data.pipeline); return -1; } /* Set the URI to play */ g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL); /* Connect to the pad-added signal */ g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data); /* Start playing */ ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING); if (ret == GST_STATE_CHANGE_FAILURE) { g_printerr ("Unable to set the pipeline to the playing state.\n"); gst_object_unref (data.pipeline); return -1; } /* Listen to the bus */ bus = gst_element_get_bus (data.pipeline); do { msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS); /* Parse message */ if (msg != NULL) { GError *err; gchar *debug_info; switch (GST_MESSAGE_TYPE (msg)) { case GST_MESSAGE_ERROR: gst_message_parse_error (msg, &err, &debug_info); g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message); g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none"); g_clear_error (&err); g_free (debug_info); terminate = TRUE; break; case GST_MESSAGE_EOS: g_print ("End-Of-Stream reached.\n"); terminate = TRUE; break; case GST_MESSAGE_STATE_CHANGED: /* We are only interested in state-changed messages from the pipeline */ if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) { GstState old_state, new_state, pending_state; gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); g_print ("Pipeline state changed from %s to %s:\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state)); } break; default: /* We should not reach here */ g_printerr ("Unexpected message received.\n"); break; } gst_message_unref (msg); } } while (!terminate); /* Free resources */ gst_object_unref (bus); gst_element_set_state (data.pipeline, GST_STATE_NULL); gst_object_unref (data.pipeline); return 0; } /* This function will be called by the pad-added signal */ static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) { GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink"); GstPadLinkReturn ret; GstCaps *new_pad_caps = NULL; GstStructure *new_pad_struct = NULL; const gchar *new_pad_type = NULL; g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src)); /* If our converter is already linked, we have nothing to do here */ if (gst_pad_is_linked (sink_pad)) { g_print ("We are already linked. Ignoring.\n"); goto exit; } /* Check the new pad's type */ new_pad_caps = gst_pad_get_current_caps (new_pad); new_pad_struct = gst_caps_get_structure (new_pad_caps, 0); new_pad_type = gst_structure_get_name (new_pad_struct); if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) { g_print ("It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type); goto exit; } /* Attempt the link */ ret = gst_pad_link (new_pad, sink_pad); if (GST_PAD_LINK_FAILED (ret)) { g_print ("Type is '%s' but link failed.\n", new_pad_type); } else { g_print ("Link succeeded (type '%s').\n", new_pad_type); } exit: /* Unreference the new pad's caps, if we got them */ if (new_pad_caps != NULL) gst_caps_unref (new_pad_caps); /* Unreference the sink pad */ gst_object_unref (sink_pad); }
將源碼保存爲basic-tutorial-4.c,執行下列命令可獲得編譯結果:
gcc basic-tutorial-4.c -o basic-tutorial-4 `pkg-config --cflags --libs gstreamer-1.0`學習
/* Create the elements */ data.source = gst_element_factory_make ("uridecodebin", "source"); data.convert = gst_element_factory_make ("audioconvert", "convert"); data.sink = gst_element_factory_make ("autoaudiosink", "sink");
首先建立了所需的Element:ui
if (!gst_element_link (data.convert, data.sink)) { g_printerr ("Elements could not be linked.\n"); gst_object_unref (data.pipeline); return -1; }
接着將converter和sink鏈接起來,注意,這裏咱們沒有鏈接source與convert,是由於uridecode bin在Pipeline初始階段尚未Source Pad。this
/* Set the URI to play */ g_object_set (data.source, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
這裏設置了播放文件的uri,uridecodebin會自動解析該地址,並讀取媒體數據。
/* Connect to the pad-added signal */ g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);
GSignals在GStreamer中扮演着相當重要的角色。信號使你能在你所關心到事件發生後獲得通知。在GLib中的信號經過信號名來進行識別,每一個GObject對象都有其本身的信號。
在上面這行代碼中,咱們經過g_signal_connect將pad_added_handler回調鏈接到uridecodebin的「pad-added」信號上,同時附帶回調函數的私有參數。GStreamer不會處理咱們傳入到data指針,只會將其做爲參數傳遞給回調函數,這是傳遞私有數據給回調函數的經常使用方式。
一個GstElement可能會發出多個信號,可使用gst-inspect工具查看具體到信號及參數。
在咱們鏈接了「pad-added」的信號後,咱們就能夠將Pipeline的狀態設置爲PLAYING並按原有方式處理本身所關心到消息。
當Source Element收集到足夠到信息,能產生數據時,它會建立Source Pad而且觸發「pad-added」信號。這時,咱們的回調函數就會被調用。
static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {
這裏是咱們實現到回調函數,爲何咱們的回調函數須要定義成這種格式呢?
由於咱們的回調函數是爲了處理信號所攜帶到信息,因此必須用符合信號的數據類型,不然不能正確處處理相應數據。經過gst-inspect查看uridecodebin能夠看到信號所須要到回調函數格式:
$ gst-inspect-1.0 uridecodebin ... Element Signals: "pad-added" : void user_function (GstElement* object, GstPad* arg0, gpointer user_data); ...
GstPad *sink_pad = gst_element_get_static_pad (data->convert, "sink");
咱們首先從CustomData中取得convert指針,並經過gst_element_get_static_pad()獲取其Sink Pad。咱們須要將這個Sink Pad鏈接到uridecodebin新建立的new_pad中。
/* If our converter is already linked, we have nothing to do here */ if (gst_pad_is_linked (sink_pad)) { g_print ("We are already linked. Ignoring.\n"); goto exit; }
因爲uridecodebin可能會建立多個Pad,在每次有Pad被建立時,咱們的回調函數都會被調用。上面這段代碼就是爲了不重複鏈接Pad。
/* Check the new pad's type */ new_pad_caps = gst_pad_get_current_caps (new_pad, NULL); new_pad_struct = gst_caps_get_structure (new_pad_caps, 0); new_pad_type = gst_structure_get_name (new_pad_struct); if (!g_str_has_prefix (new_pad_type, "audio/x-raw")) { g_print ("It has type '%s' which is not raw audio. Ignoring.\n", new_pad_type); goto exit; }
因爲咱們在當前示例中只處理audio相關的數據(咱們開始只建立了autoaudiosink),因此咱們這裏對Pad所產生的數據類型進行了過濾,對於非音頻Pad(視頻及字幕)直接忽略。
gst_pad_get_current_caps()能夠獲取當前Pad的能力(這裏是new_pad輸出數據的能力),全部的能力被存儲在GstCaps結構體中。Pad所支持的全部Caps能夠經過gst_pad_query_caps()獲得,因爲一個Pad可能包含多個Caps,所以GstCaps能夠包含一個或多個GstStructure,每一個都表明所支持的不一樣數據的能力。經過gst_pad_get_current_caps()獲取到的當前Caps只會包含一個GstStructure用於表示惟一的數據類型,若是沒法獲取到當前所使用到Caps,該函數會直接返回NULL。
因爲咱們已知在本例中new_pad只包含一個音頻Cap,因此咱們直接經過gst_caps_get_structure()來取得第一個GstStructure。接着再經過gst_structure_get_name() 獲取該Cap支持的數據類型,若是不是音頻(audio/x-raw),咱們直接忽略。
/* Attempt the link */ ret = gst_pad_link (new_pad, sink_pad); if (GST_PAD_LINK_FAILED (ret)) { g_print ("Type is '%s' but link failed.\n", new_pad_type); } else { g_print ("Link succeeded (type '%s').\n", new_pad_type); }
對於音頻的Source Pad,咱們使用gst_pad_link()將其與Sink Pad進行鏈接,使用方式與gst_element_link()相同,指定Source和Sink Pad,其所屬的Element必須位於同一個Bin或Pipeline。
到目前爲止,咱們完成了Pipeline的創建,數據會繼續在後續的Element中進行音頻的播放,直到產生ERROR或EOS。
咱們已經知道Pipeline在咱們將狀態設置爲PLAYING以前是不會進入播放狀態,實際上PLAYING狀態只是GStreamer狀態中的一個,GStreamer總共包含4個狀態:
GStreamer的狀態必須按上面的順序進行切換,例如:不能直接從NULL切換到PLAYING狀態,NULL必須依次切換到READY,PAUSED後才能切換到PLAYING狀態,當咱們直接設置Pipeline的狀態爲PLAYING時,GStreamer內部會依次爲咱們切換到PLAYING狀態。
case GST_MESSAGE_STATE_CHANGED: /* We are only interested in state-changed messages from the pipeline */ if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) { GstState old_state, new_state, pending_state; gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); g_print ("Pipeline state changed from %s to %s:\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state)); } break;
在本教程中,咱們學習了:
https://gstreamer.freedesktop.org/documentation/tutorials/basic/dynamic-pipelines.html?gi-language=c
https://gstreamer.freedesktop.org/documentation/additional/design/states.html?gi-language=c