Gstreamer自定义Plugin及调用自定义Plugin
Gstreamer支持开发者自己创建Plugin,创建后的Plugin可以通过工具gst-inspect-1.0查看,并在代码中调用自定义的plugin。
Gstreamer 官网中给出了Plugin创建教程,但实际上如果按照教程一步步走,最后会因编译失败的问题无法编译出Plugin的lib库(至少目前在Ubuntu20.04是这样)
官网Plugin教程地址:
https://gstreamer.freedesktop.org/documentation/plugin-development/basics/boiler.html?gi-language=c
这里结合Gstreamer 官网的教程,记录以下内容:
- 自定义Gstreamer Plugin
- 通过工具(gst-inspect-1.0)查看自定义Plugin信息
- 调用自定义的Plugin
系统:Ubuntu
Gstream版本:1.0
可以通过下述命令在Ubuntu上安装Gstreamer开发环境
apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
编译的时候,通过pkg可以直接查询Gstreamer的头文件、lib等参数。
gcc xxxxx `pkg-config --cflags --libs gstreamer-1.0`
关于Gstreamer的安装,官网给出了很详细的教程,参考官网即可。
自定义Gstreamer Plugin
下载Gstreamer提供的Plugin创建工具。
git clone https://gitlab.freedesktop.org/gstreamer/gst-template.git
下载完之后目录大概是这样的。gst-plugin/tools/目录下的make_element就是用于创建Plugin的工具。
Gsteamer通过Plugin的方式,集成了大量的Element,每个Element提供了特定的功能,将这些Element连接在一起组成了Pipleline。
目前如果直接使用make_element,并使用gstreamer1.0版本编译生成的工具生成的源文件是编译不过的。所以通过git,将gst-template这个仓库切到特定提交。
我切到到了这个Commit。
然后执行make_element创建自定义的Plugin
cd gst-template/gst-plugin/src
../tools/make_element MyTestFilter
执行后,在“gst-template/gst-plugin/src”这个目下多出来两个文件 gstmytestfliter.h 和gstmytestfliter.c。这个两个就是MyTestFilter这个Plugin,对应的源码实现。简单分析一下源码。
gstmytestfliter.h这个头文件中,需要手动修改一处内容,否会会编译不过。
/*
* GStreamer
* Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
* Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
* Copyright (C) 2020 Niels De Graef <niels.degraef@gmail.com>
* Copyright (C) 2023 <<user@hostname.org>>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_MYTESTFLITER_H__
#define __GST_MYTESTFLITER_H__
#include <gst/gst.h>
G_BEGIN_DECLS
#define GST_TYPE_MYTESTFLITER (gst_my_test_fliter_get_type())
G_DECLARE_FINAL_TYPE (GstMyTestFliter, gst_my_test_fliter,
GST, MYTESTFLITER, GstElement)
// 注意:
// 上一行底代码中的MYTESTFLITER是手动修改后的
// 工具生成的值是PLUGIN_TEMPLATE,如果不修改,编译时会失败(找不到GST_MYTESTFLITER这个定义)
struct _GstMyTestFliter
{
GstElement element;
GstPad *sinkpad, *srcpad;
gboolean silent;
};
G_END_DECLS
#endif /* __GST_MYTESTFLITER_H__ */
gstmytestfliter.c:源码中主要关注两个函数,gst_my_test_fliter_class_init 用于初始化PluginClass(类似C++类的构造函数); gst_my_test_fliter_init 用于初始化Element,当Plugin被创建时会调用这个函数。
/*
* GStreamer
* Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
* Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
* Copyright (C) 2023 <<user@hostname.org>>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distrib