GStreamer 是基於管道(Pipeline)架構的開源跨平台多媒體框架,廣泛用於音視頻播放、編輯、流媒體服務、VoIP、攝像頭采集等場景。本文匯總架構核心、安裝方法、官方教程與最小播放器代碼結構。
| 概念 | 說明 |
|---|---|
| Element(元件) | 處理媒體數據的基本單元:源、解碼器、過濾器、編碼器、接收器。擁有 Pad(墊片)。 |
| Pad(墊片) | 元件的輸入/輸出接口,分 Source Pad(輸出)和 Sink Pad(輸入),通過 Capabilities(能力)協商格式。 |
| Bin(容器) | 元件的容器,可包含多個元件,本身也是元件。Pipeline 是頂層 Bin。 |
| Pipeline(管道) | 頂層容器,管理數據流向、時鐘、總線、狀態機。狀態:NULL→READY→PAUSED→PLAYING。 |
| Buffer(緩衝區) | 數據塊載體,含內存指針、時間戳、偏移、標誌(關鍵幀、Delta 幀等)。 |
| Event(事件) | 控制信息:Seek、EOS、Flush、Caps 協商、QoS、Segment。 |
| Query(查詢) | 請求信息:位置、時長、格式、轉換、延遲、接受分配器。 |
| Bus(總線) | 管道內異步消息傳遞機制:錯誤、警告、EOS、狀態變更、標籤、緩衝統計。應用通過 gst_bus_add_watch 監聽。 |
| Clock(時鐘) | 管道統一時間基準,元件同步播放進度。 |
# 核心庫與開發包
sudo apt update && sudo apt install \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-good1.0-dev \
gstreamer1.0-tools gstreamer1.0-plugins-ugly
# 驗證
gst-inspect-1.0 --version
yum install gstreamer1-devel gstreamer1-plugins-base-devel \
gstreamer1-plugins-good gstreamer1-plugins-bad gstreamer1-plugins-ugly
bin/ 加入 PATH,或在項目中配置 PKG_CONFIG_PATH 指向 lib/pkgconfig/。pacman -S mingw-w64-x86_64-gstreamer mingw-w64-x86_64-gst-plugins-basebrew install gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly
#include
#include
static gboolean bus_cb(GstBus *bus, GstMessage *msg, gpointer data) {
GMainLoop *loop = data;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS:
g_print("播放結束\n"); g_main_loop_quit(loop); break;
case GST_MESSAGE_ERROR: {
GError *err; gchar *dbg;
gst_message_parse_error(msg, &err, &dbg);
g_printerr("錯誤: %s\n", err->message);
g_error_free(err); g_free(dbg);
g_main_loop_quit(loop); break;
}
default: break;
}
return TRUE;
}
int main(int argc, char *argv[]) {
GMainLoop *loop;
GstElement *pipeline, *source, *decode, *sink;
GstBus *bus;
gst_init(&argc, &argv);
if (argc != 2) { g_printerr("用法: %s <媒體文件/URI>\n", argv[0]); return -1; }
loop = g_main_loop_new(NULL, FALSE);
pipeline = gst_pipeline_new("player");
source = gst_element_factory_make("filesrc", "src");
decode = gst_element_factory_make("decodebin", "dec"); // 自動解碼
sink = gst_element_factory_make("autovideosink", "vsink");
g_object_set(source, "location", argv[1], NULL);
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
gst_bus_add_watch(bus, bus_cb, loop);
gst_object_unref(bus);
gst_bin_add_many(GST_BIN(pipeline), source, decode, sink, NULL);
gst_element_link(source, decode);
// decodebin 動態產生 pad,需連接 pad-added 信號動態鏈接 sink
g_signal_connect(decode, "pad-added", G_CALLBACK(on_pad_added), sink);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_print("運行中...\n");
g_main_loop_run(loop);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return 0;
}
// 動態連接 decodebin 的 src pad 到 sink
static void on_pad_added(GstElement *decode, GstPad *pad, gpointer data) {
GstElement *sink = data;
GstPad *sink_pad = gst_element_get_static_pad(sink, "sink");
if (gst_pad_is_linked(sink_pad)) { gst_object_unref(sink_pad); return; }
gst_pad_link(pad, sink_pad);
gst_object_unref(sink_pad);
}
gcc player.c -o player $(pkg-config --cflags --libs gstreamer-1.0 gstreamer-video-1.0)
GstElement/GstBaseTransform,實現特定過濾/編解碼邏輯。vaapi/nvdec/omx/d3d11 插件利用 GPU 解碼/編碼。rtsp-server/webrtc/srt/hls 插件構建推拉流服務。nle(非線性編輯)組合、轉場、特效、多軌混音。掌握 Pipeline-Element-Pad-Bus 核心模型,熟練使用 gst-inspect-1.0、gst-launch-1.0 調試管道拓撲,是 GStreamer 開發的入門鑰匙。