首页 > 解决方案 > 用 C++ 包装一个 C 插件

问题描述

我正在创建一个gstreamer 插件。因为 gstreamer 是 C 语言,所以它使用自己的类和类似接口的系统来声明插件类型。

如何将这种 C 机制封装在 C++ GstPlugin抽象类中,这样开发人员就无需深入研究这些机制?

这个问题不是 gstreamer 特有的,任何 C 程序加载用 C++ 开发的插件并且必须使用extern "C"来公开静态函数而不进行修改(使模板的使用看似不可能)都是相同的。

使用 gstreamer 编写插件看起来像这样

#include <gst/gst.h>

/* Definition of structure storing data for this element. */
typedef struct _GstMyFilter {
  GstElement element;
  GstPad *sinkpad, *srcpad;
  gboolean silent;
} GstMyFilter;

/* Standard definition defining a class for this element. */
typedef struct _GstMyFilterClass {
  GstElementClass parent_class;
} GstMyFilterClass;

/* Standard function returning type information. */
GType gst_my_filter_get_type (void);

然后这个宏定义原型来调用初始化 gstreamer 类型然后初始化 gstreamer 对象。

#include "filter.h"

G_DEFINE_TYPE (GstMyFilter, gst_my_filter, GST_TYPE_ELEMENT);

现已注册以下内容:

void gst_my_filter_init(GstMyFilter * filter);
void gst_my_filter_class_init(GstMyFilter * klass);

我想隐藏这种机制,以便使用我的代码的开发人员只需继承 GstPlugin 抽象类,而不必声明这些方法或GstMyFilterClassGstMyFilter类型。

标签: c++cgstreamer

解决方案


推荐阅读