GStreamer基礎教程03 - 媒體型別與Pad
摘要
在上一篇文章中,我們介紹瞭如何將多個element連線起來構造一個pipline,進行資料傳輸。那麼GStreamer是通過何種方式保證element之間能正常的進行資料傳輸?今天就將介紹GStreamer是如何利用Pad來控制資料的傳輸。
Pad
我們知道,pad是element之間的資料的介面,一個src pad只能與一個sink pad相連。每個element可以通過pad過濾資料,接收自己支援的資料型別。Pad通過Pad Capabilities(簡稱為Pad Caps)來描述支援的資料型別。例如:
- 表示解析度為300x200,幀率為30fps的RGB視訊的Caps:
“video/x-raw,format=RGB,width=300,height=200,framerate=30/1”
- 表示取樣位寬為16位,取樣率44.1kHz,雙通道PCM音訊的Caps:
“audio/x-raw,format=S16LE,rate=44100,channels=2”
- 或者直接描述編碼資料格式Voribis,VP8:
“audio/x-vorbis” "video/x-vp8"
一個Pad可以支援多種型別的Caps(比如一個video sink可以同時支援RGB或YUV格式的資料),同時可以指定Caps支援的資料範圍(比如一個audio sink可以支援1~48k的取樣率)。但是,在一個Pipeline中,Pad之間所傳輸的資料型別必須是唯一的。GStreamer在進行element連線時,會通過協商(negotiation)的方式選擇一個雙方都支援的型別。
因此,為了能使兩個Element能夠正確的連線,雙方的Pad Caps之間必須有交集,從而在協商階段選擇相同的資料型別,這就是Pad Caps的主要作用。在實際使用中,我們可以通過gst-inspect工具檢視Element所支援的Pad Caps,從而才能知道在連接出錯時如何處理。
Pad Templates(模板)
我們曾使用gst_element_factory_make()介面建立Element,這個介面內部也會先建立一個Element 工廠,再通過工廠方法建立一個Element。由於大部分Element都需要建立類似的Pad,於是GStreame定義了Pad Template,Pad Template被包含中Element工廠中,在建立Element時,用於快速建立Pad。
由於Pad Template屬於Element工廠,所以我們可以直接使用gst-inspect檢視其屬性,但Element實際的Pad會根據Element所處的不同狀態來進行例項化,具體的Pad Caps會在協商後才會被確定。
Pad Templates Capabilities例子
我們看一個 “gst-inspect-1.0 alsasink”的例子(不同平臺會有差異):
Pad Templates: SINK template: 'sink' Availability: Always Capabilities: audio/x-raw format: S16LE layout: interleaved rate: [ 1, 48000 ] channels: [ 1, 2 ] audio/x-ac3 framed: true
alsasink只提供了一個sink template,可以建立sink pad,並且是一直存在的。支援兩種型別的音訊資料:16位的PCM(audio/x-raw),取樣率1~48k,1-2通道和AC3(audio/x-ac3)的幀資料。
再看一個 “gst-inspect-1.0 videotestsrc”的例子:
Pad Templates: SRC template: 'src' Availability: Always Capabilities: video/x-raw format: { I420, YV12, YUY2, UYVY, AYUV, RGBx, BGRx, xRGB, xBGR, RGBA, BGRA, ARGB, ABGR, RGB, BGR, Y41B, Y42B, YVYU, Y444, v210, v216, NV12, NV21, NV16, NV24, GRAY8, GRAY16_BE, GRAY16_LE, v308, RGB16, BGR16, RGB15, BGR15, UYVP, A420, RGB8P, YUV9, YVU9, IYU1, ARGB64, AYUV64, r210, I420_10LE, I420_10BE, I422_10LE, I422_10BE, Y444_10LE, Y444_10BE, GBR, GBR_10LE, GBR_10BE } width: [ 1, 2147483647 ] height: [ 1, 2147483647 ] framerate: [ 0/1, 2147483647/1 ] video/x-bayer format: { bggr, rggb, grbg, gbrg } width: [ 1, 2147483647 ] height: [ 1, 2147483647 ] framerate: [ 0/1, 2147483647/1 ]
videotestsrc只提供了一個src template用於建立src pad,pad支援多種格式,可以通過引數指定輸出的資料型別或Caps Filter指定。
Pad Availability(有效性)
上面的例子中顯示的Pad Template都是一直存在的(Availability: Always),建立的Pad也是一直有效的。但有些Element會根據輸入資料以及後續的Element動態增加或刪除Pad,因此GStreamer提供了3種Pad有效性的狀態:Always,Sometimes,On request。
Always Pad
在element被初始化後就存在的pad,被稱為always pad或static pad。
Sometimes Pad
根據輸入資料的不同而產生的pad,被稱為sometimes pad,常見於各種檔案格式解析器。例如用於解析mp4檔案的qtdemux:"gst-inspect-1.0 qtdemux"
Pad Templates: SINK template: 'sink' Availability: Always Capabilities: video/quicktime video/mj2 audio/x-m4a application/x-3gp SRC template: 'video_%u' Availability: Sometimes Capabilities: ANY SRC template: 'audio_%u' Availability: Sometimes Capabilities: ANY SRC template: 'subtitle_%u' Availability: Sometimes Capabilities: ANY
只有我們從mp4檔案中讀取資料時,我們才能知道這個檔案中包含多少音訊,視訊,字幕,所以這些src pad都是sometimes pad。
Request Pad
按需建立的pad被稱為request pad,常見於合併或生成多路資料。例如,用於1到N轉換的tee:"gst-inspect-1.0 tee"
Pad Templates: ... SRC template: 'src_%u' Availability: On request Has request_new_pad() function: gst_tee_request_new_pad Capabilities: ANY
當我們需要將同一路視訊流同時進行顯示和儲存,這時候我們就需要用到tee,在建立tee element的時候,我們不知道pipeline需要多少個src pad,需要後續element來請求一個src pad。
示例程式碼
GStreamer提供了gst-inspect工具來檢視element所提供的Pad Templates,但無法檢視element在不同狀態時其Pad所支援的資料型別,通過下面的程式碼,我們可以看到Pad Caps在不同狀態下的變化。
#include <gst/gst.h> /* Functions below print the Capabilities in a human-friendly format */ static gboolean print_field (GQuark field, const GValue * value, gpointer pfx) { gchar *str = gst_value_serialize (value); g_print ("%s %15s: %s\n", (gchar *) pfx, g_quark_to_string (field), str); g_free (str); return TRUE; } static void print_caps (const GstCaps * caps, const gchar * pfx) { guint i; g_return_if_fail (caps != NULL); if (gst_caps_is_any (caps)) { g_print ("%sANY\n", pfx); return; } if (gst_caps_is_empty (caps)) { g_print ("%sEMPTY\n", pfx); return; } for (i = 0; i < gst_caps_get_size (caps); i++) { GstStructure *structure = gst_caps_get_structure (caps, i); g_print ("%s%s\n", pfx, gst_structure_get_name (structure)); gst_structure_foreach (structure, print_field, (gpointer) pfx); } } /* Prints information about a Pad Template, including its Capabilities */ static void print_pad_templates_information (GstElementFactory * factory) { const GList *pads; GstStaticPadTemplate *padtemplate; g_print ("Pad Templates for %s:\n", gst_element_factory_get_longname (factory)); if (!gst_element_factory_get_num_pad_templates (factory)) { g_print (" none\n"); return; } pads = gst_element_factory_get_static_pad_templates (factory); while (pads) { padtemplate = pads->data; pads = g_list_next (pads); if (padtemplate->direction == GST_PAD_SRC) g_print (" SRC template: '%s'\n", padtemplate->name_template); else if (padtemplate->direction == GST_PAD_SINK) g_print (" SINK template: '%s'\n", padtemplate->name_template); else g_print (" UNKNOWN!!! template: '%s'\n", padtemplate->name_template); if (padtemplate->presence == GST_PAD_ALWAYS) g_print (" Availability: Always\n"); else if (padtemplate->presence == GST_PAD_SOMETIMES) g_print (" Availability: Sometimes\n"); else if (padtemplate->presence == GST_PAD_REQUEST) g_print (" Availability: On request\n"); else g_print (" Availability: UNKNOWN!!!\n"); if (padtemplate->static_caps.string) { GstCaps *caps; g_print (" Capabilities:\n"); caps = gst_static_caps_get (&padtemplate->static_caps); print_caps (caps, " "); gst_caps_unref (caps); } g_print ("\n"); } } /* Shows the CURRENT capabilities of the requested pad in the given element */ static void print_pad_capabilities (GstElement *element, gchar *pad_name) { GstPad *pad = NULL; GstCaps *caps = NULL; /* Retrieve pad */ pad = gst_element_get_static_pad (element, pad_name); if (!pad) { g_printerr ("Could not retrieve pad '%s'\n", pad_name); return; } /* Retrieve negotiated caps (or acceptable caps if negotiation is not finished yet) */ caps = gst_pad_get_current_caps (pad); if (!caps) caps = gst_pad_query_caps (pad, NULL); /* Print and free */ g_print ("Caps for the %s pad:\n", pad_name); print_caps (caps, " "); gst_caps_unref (caps); gst_object_unref (pad); } int main(int argc, char *argv[]) { GstElement *pipeline, *source, *sink; GstElementFactory *source_factory, *sink_factory; GstBus *bus; GstMessage *msg; GstStateChangeReturn ret; gboolean terminate = FALSE; /* Initialize GStreamer */ gst_init (&argc, &argv); /* Create the element factories */ source_factory = gst_element_factory_find ("audiotestsrc"); sink_factory = gst_element_factory_find ("autoaudiosink"); if (!source_factory || !sink_factory) { g_printerr ("Not all element factories could be created.\n"); return -1; } /* Print information about the pad templates of these factories */ print_pad_templates_information (source_factory); print_pad_templates_information (sink_factory); /* Ask the factories to instantiate actual elements */ source = gst_element_factory_create (source_factory, "source"); sink = gst_element_factory_create (sink_factory, "sink"); /* Create the empty pipeline */ pipeline = gst_pipeline_new ("test-pipeline"); if (!pipeline || !source || !sink) { g_printerr ("Not all elements could be created.\n"); return -1; } /* Build the pipeline */ gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL); if (gst_element_link (source, sink) != TRUE) { g_printerr ("Elements could not be linked.\n"); gst_object_unref (pipeline); return -1; } /* Print initial negotiated caps (in NULL state) */ g_print ("In NULL state:\n"); print_pad_capabilities (sink, "sink"); /* Start playing */ ret = gst_element_set_state (pipeline, GST_STATE_PLAYING); if (ret == GST_STATE_CHANGE_FAILURE) { g_printerr ("Unable to set the pipeline to the playing state (check the bus for error messages).\n"); } /* Wait until error, EOS or State Change */ bus = gst_element_get_bus (pipeline); do { msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_STATE_CHANGED); /* 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 (pipeline)) { GstState old_state, new_state, pending_state; gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); g_print ("\nPipeline state changed from %s to %s:\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state)); /* Print the current capabilities of the sink element */ print_pad_capabilities (sink, "sink"); } break; default: /* We should not reach here because we only asked for ERRORs, EOS and STATE_CHANGED */ g_printerr ("Unexpected message received.\n"); break; } gst_message_unref (msg); } } while (!terminate); /* Free resources */ gst_object_unref (bus); gst_element_set_state (pipeline, GST_STATE_NULL); gst_object_unref (pipeline); gst_object_unref (source_factory); gst_object_unref (sink_factory); return 0; }
將原始碼儲存為basic-tutorial-3.c,執行下列命令可得到編譯結果:
$ gcc basic-tutorial-3.c -o basic-tutorial-3 `pkg-config --cflags --libs gstreamer-1.0`
原始碼分析
輸出可讀資訊
print_field, print_caps and print_pad_templates_information實現類似功能,列印GStreamer的資料結構,可以檢視相應GStreamer GstCaps 介面瞭解更多資訊。
/* Shows the CURRENT capabilities of the requested pad in the given element */ static void print_pad_capabilities (GstElement *element, gchar *pad_name) { GstPad *pad = NULL; GstCaps *caps = NULL; /* Retrieve pad */ pad = gst_element_get_static_pad (element, pad_name); if (!pad) { g_printerr ("Could not retrieve pad '%s'\n", pad_name); return; } /* Retrieve negotiated caps (or acceptable caps if negotiation is not finished yet) */ caps = gst_pad_get_current_caps (pad); if (!caps) caps = gst_pad_query_caps (pad, NULL); /* Print and free */ g_print ("Caps for the %s pad:\n", pad_name); print_caps (caps, " "); gst_caps_unref (caps); gst_object_unref (pad); }
因為我們使用的source和sink都具有static(always)pad,所以這裡使用gst_element_get_static_pad()獲取Pad, 其他情況可以使用gst_element_foreach_pad()或gst_element_iterate_pads()獲取動態建立的Pad。
接著使用gst_pad_get_current_caps()獲取pad當前的caps,根據不同的element狀態會有不同的結果,甚至可能不存在caps。如果沒有,我們通過gst_pad_query_caps()獲取當前可以支援的caps,當element處於NULL狀態時,這個caps為Pad Template所支援的caps,其值可隨狀態變化而變化。
獲取Element工廠
/* Create the element factories */ source_factory = gst_element_factory_find ("audiotestsrc"); sink_factory = gst_element_factory_find ("autoaudiosink"); if (!source_factory || !sink_factory) { g_printerr ("Not all element factories could be created.\n"); return -1; } /* Print information about the pad templates of these factories */ print_pad_templates_information (source_factory); print_pad_templates_information (sink_factory); /* Ask the factories to instantiate actual elements */ source = gst_element_factory_create (source_factory, "source"); sink = gst_element_factory_create (sink_factory, "sink");
在使用gst_element_factory_make()介面建立element時,應用不需要關心element工廠。在這裡,由於Pad Template資料Element工程,因此我們首先根據工廠名建立了相應工廠例項(GstElementFactory ),再由其獲取Pad Template以及建立element。
此處使用gst_element_factory_find()查詢"audiotestsrc"工廠,再通過gst_element_factory_create()建立source element。以前使用的gst_element_factory_make()是gst_element_factory_find() + gst_element_factory_create()的簡化版。
處理State-Changed訊息
Pipeline的建立過程與其他示例相同,此例新增了狀態變化的處理。
case GST_MESSAGE_STATE_CHANGED: /* We are only interested in state-changed messages from the pipeline */ if (GST_MESSAGE_SRC (msg) == GST_OBJECT (pipeline)) { GstState old_state, new_state, pending_state; gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); g_print ("\nPipeline state changed from %s to %s:\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state)); /* Print the current capabilities of the sink element */ print_pad_capabilities (sink, "sink"); } break;
因為我們在gst_bus_timed_pop_filtered()中加入了GST_MESSAGE_STATE_CHANGED,所以我們會收到狀態變化的訊息。在狀態變化時,輸出sink element的pad caps中當前狀態的資訊。
輸出分析
Pad Templates for Audio test source: SRC template: 'src' Availability: Always Capabilities: audio/x-raw format: { S16LE, S32LE, F32LE, F64LE } layout: interleaved rate: [ 1, 2147483647 ] channels: [ 1, 2 ] Pad Templates for Auto audio sink: SINK template: 'sink' Availability: Always Capabilities: ANY
首先是“audiotestsrc”和“autoaudiosink”的pad templates資訊,這個與gst-inspect的輸出相同。
In NULL state: Caps for the sink pad: ANY
NULL狀態為Element的初始化狀態,此時,“autoaudiosink”的sink pad caps與Pad Template相同,支援所有的格式。
Pipeline state changed from NULL to READY: Caps for the sink pad: audio/x-raw format: { S16LE, S16BE, F32LE, F32BE, S32LE, S32BE, S24LE, S24BE, S24_32LE, S24_32BE, U8 } layout: interleaved rate: [ 1, 2147483647 ] channels: [ 1, 32 ] audio/x-alaw rate: [ 1, 2147483647 ] channels: [ 1, 32 ] audio/x-mulaw rate: [ 1, 2147483647 ] channels: [ 1, 32 ]
狀態從NULL轉到READY時,GStreamer會獲取音訊輸出裝置所支援的所有型別,這裡可以看到sink pad caps列出了輸出裝置所能支援的型別。
Pipeline state changed from READY to PAUSED: Caps for the sink pad: audio/x-raw format: S16LE layout: interleaved rate: 44100 channels: 1 Pipeline state changed from PAUSED to PLAYING: Caps for the sink pad: audio/x-raw format: S16LE layout: interleaved rate: 44100 channels: 1
狀態從READY轉到PAUSED時,GStreamer會協商一個所有element都支援的型別。當進入PLAYING狀態時,sink會採用協商後的型別進行資料傳輸。
總結
在本教程中,我們掌握了:
- 什麼是Pad Capabilities 和 Pad Template Capabilities。
- Pad有效性的類別。
- 如何通過gst_pad_get_current_caps() 和 gst_pad_query_caps()獲取當前的caps。
- Pad Capabilities在element不同狀態下的變化。
- 如何使用gst-inspect工具檢視element的Pad Caps。
引用
https://gstreamer.freedesktop.org/documentation/tutorials/basic/media-formats-and-pad-capabilities.html?gi-language=c
https://gstreamer.freedesktop.org/documentation/tutorials/basic/multithreading-and-pad-availability.html
https://gstreamer.freedesktop.org/documentation/gstreamer/gstcaps.html?gi-language=c
作者:John.Leng 出處:http://www.cnblogs.com/xleng/ 本文版權歸作者所有,歡迎轉載。商業轉載請聯絡作者獲得授權,非商業轉載請在文章頁面明顯位置給出原文連線.