這種樣式固定且發展悠久的框架,要從零開始寫一個插件肯定是不現實的事情,我們需要一點工具來起頭。官方的插件開發文章雖然有寫到模板生成工具,但那一份已經過時了,現在比較適當的做法是使用gst-plugins-bad這個倉庫裡面有個tools/gst-element-maker來生成模板。另外補充一個小故事,就是GStreamer在發展的過程因為實在太多插件了,品質參差不齊,所以官方把插件依照一套標準分為四大類,base、good、bad、ugly,bad跟ugly不見得是插件寫得很爛,原因有可能是缺乏文件或是完整的測試,但還是有許多被廣泛使用的元件被放在這兩個分類。最後官方把所有插件倉庫統一到gstreamer內的subprojects
為了安全起見,我們下載跟容器內版本一致的tag。
# 容器內
cd ~
git clone -b 1.24 --depth 1 https://gitlab.freedesktop.org/gstreamer/gstreamer.git
使用工具前我們要先想好要開發哪一類型的元件(Source/Filter/Sink),這次的目標是寫一個Filter元件,功能很簡單,就是把RGB通道的數值調換,既簡單又能直接看到成果
我們先看一下工具的說明
# 一定要在gst-plugins-bad專案的目錄下
cd ~/gstreamer/subprojects/gst-plugins-bad
./tools/gst-element-maker --help
Usage: element-maker [OPTIONS] ELEMENT_NAME BASE_CLASS
Create a GStreamer element that subclasses BASE_CLASS.
Options:
--help Print this information
--prefix PREFIX Use PREFIX instead of "gst"
Example: 'element-maker my_element basetransform' will create the files
gstmyelement.c and gstmyelement.h that implement GstMyElement, a
subclass of GstBaseTransform, as an element named myelement.
再來是要給元件起個名字,這邊就把它叫做ironman_color_magic,使用工具的時候名稱採用snake_case,生成的模板會用PascalCase命名struct。再來,因為我們是搭配DeepStream使用,輸入輸出的Caps是video/x-raw(memory:NVMM),這東西框架沒有內建更高階的類別,所以只能去繼承GstBaseTransform
我們使用這個指令去生成插件
./tools/gst-element-maker ironman_color_magic basetransform
完成之後會顯示資訊
Plugin Details:
Name ironmancolormagic
Description FIXME plugin description
Filename ./gstironmancolormagic.so
Version 0.0.FIXME
License LGPL
Source module FIXME_package
Binary package FIXME_package_name
Origin URL http://FIXME.org/
ironmancolormagic: FIXME Long name
1 features:
+-- 1 elements
接著把生成的.c和.h檔案複製到自己的專案目錄下
mkdir -p /ironman/sources/gst/colormagic/src
cp gstironmancolormagic.c gstironmancolormagic.h /ironman/sources/gst/colormagic/src
模板產生的時候會幫你寫好很多用不到的函式,我們只要保留這些就好
static void gst_ironman_color_magic_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_ironman_color_magic_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static void gst_ironman_color_magic_finalize (GObject * object);
static gboolean gst_ironman_color_magic_start (GstBaseTransform * trans);
static gboolean gst_ironman_color_magic_stop (GstBaseTransform * trans);
static GstFlowReturn gst_ironman_color_magic_transform_ip (GstBaseTransform * trans, GstBuffer * buf);
然後到gst_ironman_color_magic_class_init把沒有宣告的函式都刪除
先在/ironman/sources/gst/colormagic/src底下建立Makefile
TARGET := libgstironmancolormagic.so
CC := gcc
PKGS := gstreamer-1.0 gstreamer-base-1.0
CFLAGS := -Wall -Wextra -fPIC $(shell pkg-config --cflags $(PKGS))
LDFLAGS := -shared $(shell pkg-config --libs $(PKGS))
SRCS := gstironmancolormagic.c
OBJS := $(SRCS:.c=.o)
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS) $(TARGET)
.PHONY: all clean
接著執行指令
make
用gst-inspect-1.0查看一下是否跟建立模板時看到的資訊相同
gst-inspect-1.0 ./libgstironmancolormagic.so
今天的任務就到這裡啦,因為整個插件要修改的資訊不算少,我們每天推進一點進度。大家也可以先看一下生成的模板長什麼樣子,程式碼還不少,有很多FIXME等著我們去修改