Hello world
Base on basic-tutorial-1.c
初始化GStreamer
gst_init (&argc, &argv)
&argc, &argv是給命令列后綴留下的引數.
創建一個管道示例播放一個視頻
pipeline = gst_phrase_launch("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
gst_phrase_launch() 一個簡單的管道創建方法, 將文本形式管道名詞轉化為實際的管道.
playbin 一個特殊的元素型別, 用來連接元素和播放內容.
設定element播放狀態
/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
每一個element都有一個狀態, 這里的作用是設定pipeline為播放狀態.
退出
/* Wait until error or EOS */
bus = gst_element_get_bus (pipeline);
msg =
gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
gst_bus_timed_pop_filtered()為u等待程式結束時回傳,這里是回傳錯誤GST_MESSAGE_ERROR或者結束GST_MESSAGE_EOS.
Clean 清理
/* Free resources */
if (msg != NULL)
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
閱讀函式檔案以查詢是否需要釋放物件, 在這里gst_bus_timed_pop_filtered的函式解釋Return引數中有寫道需要用gst_object_unref清除.
編譯basic-tutorial-1.c
gcc basic-tutorial-1.c -o basic-tutorial-1 `pkg-config --cflags --libs gstreamer-1.0`
基本上是在gcc編譯器命令后加入pkg-config --cflags --libs gstreamer-1.0
GStramer 概念
base on basic-tutorial-2.c
Creat element 創建元素
用gst_element_factory_make('引數1','引數2')創建新元素.
引數1是元素型別, 例如playbin/uridecodebin/decodebin/filesrc.
引數2是我們為其取的名稱, 如果定義為NULL那么GStreamer會自動生產一個名稱.
source = gst_element_factory_make ("videotestsrc", "source");
sink = gst_element_factory_make ("autovideosink", "sink");
這里有兩個元素型別.
videotestsrc - 提供測驗畫面(Produces data).
autovideosink - 接識訓面(consumes data), 自動選擇最好的sink.
Create pipeline 創建管道
創建管道用gst_pipeline_new("引數").
/* 創建空管道 */
pipeline = gst_pipeline_new ("test-pipeline");
Add element to pipeline 把元素加入進管道
/* 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;
}
GST_BIN (pipeline)就是上面創建的空管道, 由于管道是一種特殊的Bin, 所以添加Bin的方法GST_BIN (Bin)也適用于管道.
gst_bin_add_many()添加多個元素到Bin, 結尾為NULL.
gst_bin_add()添加單個元素到Bin.
gst_element_link (source, sink)連接同一個Bin中的元素, source是源, sink是目的地, 順序很重要.
Properties 屬性設定
/* Modify the source's properties */
g_object_set (source, "pattern", 0, NULL);
g_object_set用來設定source(videotestsrc)元素的屬性, 對應的還有g_object_get()讀取屬性.
"pattern"屬性是元素的輸出型別, 對于videotestsrc來說不同數字會使測驗視頻樣式發生變化.
錯誤檢查
/* 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.\n");
gst_object_unref (pipeline);
return -1;
}
We check gst_element_set_state() return value for errors.
另外一段錯誤檢查代碼
/* 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);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
涉及到的幾個方法:
gst_message_parse_error()
gst_element_get_bus()
動態管道
base on basic-tutorial-3.c
定義結構體
typedef struct _CustomData
{
GstElement *pipeline;
GstElement *source;
GstElement *convert;
GstElement *resample;
GstElement *sink;
} CustomData;
這里將所有所需資訊作為區域變數放入一個分組結構中, 這種結構有利于函式回呼.
創建元素
/* Create the elements */
data.source = gst_element_factory_make ("uridecodebin", "source");
data.convert = gst_element_factory_make ("audioconvert", "convert");
data.resample = gst_element_factory_make ("audioresample", "resample");
data.sink = gst_element_factory_make ("autoaudiosink", "sink");
這里用到4個元素:
uridecodebin用來實體化幾個必要元素(Sources, demuxers and decoders), 用來將URL視頻轉化為原始音頻/視頻流.audioconverter用來轉換音頻格式.audioresample用來轉換采樣率.autoaudiosink將音頻呈現到聲卡.
創建管道
/* 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.resample, data.sink, NULL);
if (!gst_element_link_many (data.convert, data.resample, data.sink, NULL)) {
g_printerr ("Elements could not be linked.\n");
gst_object_unref (data.pipeline);
return -1;
}
這里我們將除源以外的所有元素鏈接好.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/6537.html
標籤:C
上一篇:c語言之倉庫資訊管理系統
下一篇:C語言--->指標
