運行效果
?
簡介
?????上一個教程演示了GUI 工具包集成(gtk)。本教程介紹媒體格式和Pad功能。Pad Capabilities 是 GStreamer 的一個基本元素,盡管大多數它們不可見,因為框架會處理它們 自然而然。這個有點理論性的教程展示了:
??????????? 什么是 Pad 功能。
??????????? 如何檢索它們。
??????????? 何時檢索它們。
??????????? 為什么你需要了解它們。
?
?????如前所述,Pads 允許信息進入和離開 一個元素。Pad 的功能(或簡稱 Caps),那么, 指定哪些類型的信息可以通過 Pad 傳輸。為 示例,“分辨率為 320x200 像素和 30 幀的 RGB 視頻 per second“或”16 bits per sample audio, 5.1 channels at 44100 samples” 每秒“,甚至是 MP3 或 H264 等壓縮格式。Pads 可以支持多種功能(例如,視頻接收器可以 支持不同類型的 RGB 或 YUV 格式的視頻)和功能可以是 指定為范圍 (例如,音頻接收器可以支持示例 速率為每秒 1 到 48000 個樣本)。但是,實際的 從 Pad 到 Pad 傳輸的信息必須只有一個 Well Specify 類型。通過稱為協商的過程,兩個鏈接的 Pad 就 一個通用類型,因此 Pad 的 Capabilities 變得固定(它們只有一個類型并且不包含范圍)。演練 下面的示例代碼應該清楚地說明了這一切。為了將兩個元素鏈接在一起,它們必須共享一個 common subset of Capabilities (否則它們不可能 相互理解)。這是 Capabilities 的主要目標。作為應用程序開發人員,您通常會通過鏈接 元素一起(如果使用 all-in-all 元素,則程度較小 喜歡 )。在這種情況下,您需要知道 Pad Caps(因為它們 都熟悉地引用了)的元素,或者至少知道什么 它們是 GStreamer 拒絕將兩個元素與協商聯系起來的時候 錯誤。
?
GStreamer相關運行庫
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/gstreamer-1.0/gst
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/gstreamer-1.0
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/glib-2.0
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/lib/glib-2.0/includeLIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/gstreamer-1.0.lib
LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/glib-2.0.lib
LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/gobject-2.0.lib
?
完整源碼
#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);}
}/* 打印有關pod模板的信息,包括其功能 */
static void print_pad_templates_information (GstElementFactory * factory)
{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; }GstStaticPadTemplate *padtemplate;const GList *pads = gst_element_factory_get_static_pad_templates (factory);while (pads){padtemplate = (GstStaticPadTemplate*)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);elseg_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");elseg_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");}
}/* 顯示給定元素中請求pod的能力 */
static void print_pad_capabilities (GstElement *element, gchar *pad_name)
{/* 取回pad */GstPad *pad = gst_element_get_static_pad (element, pad_name);if (!pad) { g_printerr ("Could not retrieve pad '%s'\n", pad_name); return; }/* 獲取當前在 pad 上配置的具有最后一個 GST_EVENT_CAPS 事件的功能。(如果協商尚未完成,則檢索可接受的上限) */GstCaps *caps = gst_pad_get_current_caps (pad);if (!caps){caps = gst_pad_query_caps (pad, NULL);}/* 打印后釋放 */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[])
{/* 初始化GStreamer */gst_init (&argc, &argv);/* 創建元素工廠 */GstElementFactory *source_factory = gst_element_factory_find ("audiotestsrc");GstElementFactory *sink_factory = gst_element_factory_find ("autoaudiosink");if (!source_factory || !sink_factory) { g_printerr ("Not all element factories could be created.\n"); return -1; }/* 打印這些工廠的pod模板信息 */print_pad_templates_information (source_factory);print_pad_templates_information (sink_factory);/* 要求工廠實例化實際元素 */GstElement *source = gst_element_factory_create (source_factory, "source");GstElement *sink = gst_element_factory_create (sink_factory, "sink");/* 創建空管道 */GstElement *pipeline = gst_pipeline_new ("test-pipeline");if (!pipeline || !source || !sink) { g_printerr ("Not all elements could be created.\n"); return -1; }/* 構建管道 */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; }/* 打印初始能力(處于NULL狀態) */g_print ("In NULL state:\n");print_pad_capabilities (sink, (gchar *)"sink");/* 開始播放 */GstStateChangeReturn 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"); }gboolean terminate = FALSE;GstMessage *msg;/* 等待錯誤、EOS或狀態更改 */GstBus *bus = gst_element_get_bus (pipeline);do{// 等待獲取符合條件的消息msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_STATE_CHANGED));/* 解析消息 */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:/* 我們只對來自管道的狀態更改消息感興趣 */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));/* 打印sink元素的當前功能 */print_pad_capabilities (sink, (gchar *)("sink"));}break;default:/* 通常不會運行到這里 */g_printerr ("Unexpected message received.\n"); break;}gst_message_unref (msg);}} while (!terminate);/* 釋放資源 */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;
}
?
關注
筆者 - jxd