第一個例程
#include <gst/gst.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
int
tutorial_main (int argc, char *argv[])
{
GstElement *pipeline;
GstBus *bus;
GstMessage *msg;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Build the pipeline */
pipeline =
gst_parse_launch
("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
NULL);
/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
/* 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);
/* See next tutorial for proper error message handling/parsing */
if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
g_error ("An error occurred! Re-run with the GST_DEBUG=*:WARN environment "
"variable set for more details.");
}
/* Free resources */
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
return 0;
}
int
main (int argc, char *argv[])
{
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
return gst_macos_main (tutorial_main, argc, argv, NULL);
#else
return tutorial_main (argc, argv);
#endif
}
關鍵函數gst_parse_launch和playbin
gst_parse_launch建構管道函數
Gstreamer是一個設計處理多媒體流的架構。媒體從 源(source)元素(生産者)傳播到接收(sink)元素(消費者),經過一系列的中間元素執行各種任務。中間的元素被稱為管道(pipeline)。
在Gsteamer裡,你建構管道通過聚集一些獨立的元素。當管道足夠簡單時,不需要一些進階功能,你可以通過gst_parse_launch走捷徑,建構管道。
這個函數需要一個文本表達的管道,并把它變成真實的管道,非常友善。事實上,這個函數非常友善,至于有一個工具建構它,将會非常熟悉它。gstreamer工具gst-launch-1.0和gst-launch-1.0文法。
playbin
通過建構管道調用playbin組成單個元素。
playbin即可作為source也可以作為sink一個特殊的元素,亦可是一整個管道。内部的,它能夠建立和連結所有必要的元素來播放你的媒體。無需擔心。
不允許手動管道所具有的控件粒度,滿足廣泛的應用範圍。包括了這個指導。
在第一個hello world例子中,我們隻用一個參數playbin,設定我們想要播放的媒體URI。無論它是一個http:// 還是一個 **file://**的URI,playbin将透明地恰當執行個體化gstreamer源。
/* Build the pipeline */
pipeline =
gst_parse_launch
("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
NULL);
如果錯誤輸入URL或者檔案不存在,gstreamer将一些提醒機制。每一個gstreamer元素有一相關的狀态,你可以或多或少地把它想象成你常用的 DVD 播放器中的播放/暫停按鈕。足以說明,播放不會開始,除非設定管道播放PALYING狀态。如下
/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
這行将等待一個錯誤出現或者流的結束被發現。
gst_element_get_bus()恢複管道的總線,
gst_bus_timed_pop_filtered()會阻塞直到你接收到一個ERROR或者EOS(End Of Stream)在總線上經過。
總線概念将在下一章講解。
/* 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);
/* See next tutorial for proper error message handling/parsing */
if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
g_error ("An error occurred! Re-run with the GST_DEBUG=*:WARN environment "
"variable set for more details.");
}
就是這樣! 從現在開始,GStreamer 負責一切。
釋放資源
在終端結束之前,需要清理自己資源。
/* Free resources */
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gst_message_unref()
總結
- 初始化gsteamer用法gst_init()
- 根據文本描述快速建構管道用法gst_parse_launch().
- 建立自動化回放管道playbin.
- 開始回放gsteamer信号用法gst_element_set_state().
- Gsteamer處理事情gst_element_get_bus() and gst_bus_timed_pop_filtered().