gstreamer注册分析

参考:https://blog.csdn.net/houxiaoni01/article/details/81386809
1、plugin注册过程
从plugin_init()函数说起,每个插件都有 一个plugin_init函数,比如:gstcoreelementsplugin

static gboolean
plugin_init (GstPlugin * plugin)
{
  gboolean ret = FALSE;

  ret |= GST_ELEMENT_REGISTER (capsfilter, plugin);
  ret |= GST_ELEMENT_REGISTER (clocksync, plugin);
  ret |= GST_ELEMENT_REGISTER (concat, plugin);
  ret |= GST_ELEMENT_REGISTER (dataurisrc, plugin);
  ret |= GST_ELEMENT_REGISTER (downloadbuffer, plugin);
...
  return ret;
}
# 所有的一切都是这个宏定义里面的一个static的全局变量开始
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, coreelements,
    "GStreamer core elements", plugin_init, VERSION, GST_LICENSE,
    GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);

上面GST_PLUGIN_DEFINE 是什么定义呢,在gstplugin.h中有详细定义:

#define GST_PLUGIN_DEFINE(major,minor,name,description,init,version,license,package,origin) \
G_BEGIN_DECLS \
GST_PLUGIN_EXPORT const GstPluginDesc * G_PASTE(gst_plugin_, G_PASTE(name, _get_desc)) (void); \
GST_PLUGIN_EXPORT void G_PASTE(gst_plugin_, G_PASTE(name, _register)) (void); \
\
static const GstPluginDesc gst_plugin_desc = { \
  major, \
  minor, \
  G_STRINGIFY(name), \
  (gchar *) description, \
  init, \
  version, \
  license, \
  PACKAGE, \
  package, \
  origin, \
  __GST_PACKAGE_RELEASE_DATETIME, \
  GST_PADDING_INIT \
};                                       \
\
const GstPluginDesc * \
G_PASTE(gst_plugin_, G_PASTE(name, _get_desc)) (void) \
{ \
    return &gst_plugin_desc; \
} \
\
void \
G_PASTE(gst_plugin_, G_PASTE(name, _register)) (void) \
{ \
  gst_plugin_register_static (major, minor, G_STRINGIFY(name), \
      description, init, version, license, \
      PACKAGE, package, origin); \
} \
G_END_DECLS

G_PASTE(gst_plugin_, G_PASTE(name, _get_desc))说明,其实就是字符串叠加:gst_plugin_name_get_desc,那么上面其实就是定义了一个结构体成员和两个函数:

#定义的结构体成员变量,是staticstatic const GstPluginDesc gst_plugin_desc = {
...
}
# 下面这两个函数不是staticconst GstPluginDesc gst_plugin_name_get_desc(){
 return &gst_plugin_desc;
}

void *gst_plugin_name_register(){
 gst_plugin_register_static (major, minor, G_STRINGIFY(name), 
      description, init, version, license, 
      PACKAGE, package, origin); 
}

接下来看另外一个宏:

GST_ELEMENT_REGISTER (capsfilter, plugin);
#define GST_ELEMENT_REGISTER(element, plugin) G_PASTE(gst_element_register_, element) (plugin)
#那么就是
gst_element_register_capsfilter(plugin)

GST_ELEMENT_REGISTER_DECLARE (capsfilter);
#define GST_ELEMENT_REGISTER_DECLARE(element) gboolean G_PASTE(gst_element_register_, element) (GstPlugin * plugin); 
gboolean  gst_element_register_capsfilter(GstPlugin * plugin);

#每个插件内部都会定义这个函数
GST_ELEMENT_REGISTER_DEFINE (capsfilter, "capsfilter", GST_RANK_NONE,GST_TYPE_CAPS_FILTER);
#define GST_ELEMENT_REGISTER_DEFINE(e, e_n, r, t) _GST_ELEMENT_REGISTER_DEFINE_BEGIN(e) _GST_ELEMENT_REGISTER_DEFINE_END(e_n, r, t)
#define _GST_ELEMENT_REGISTER_DEFINE_BEGIN(element) \
G_BEGIN_DECLS \
gboolean G_PASTE (gst_element_register_, element) (GstPlugin * plugin) \
{ \
  {

/**
 * _GST_ELEMENT_REGISTER_DEFINE_END: (attributes doc.skip=true)
 */
#define _GST_ELEMENT_REGISTER_DEFINE_END(element_name, rank, type) \
  } \
  return gst_element_register (plugin, element_name, rank, type); \
} \
G_END_DECLS

#那么就是
gboolean  gst_element_register_capsfilter(GstPlugin * plugin){
 return gst_element_register (plugin, element_name, rank, type);
}

综上所述,调用流程是:

gst_plugin_name_register()
	gst_plugin_register_static ()
		gst_plugin_register_func()
			desc->plugin_init->plugin_init()
				gst_element_register_name()
					gst_element_register ()
	gst_registry_add_plugin()

另外分析几个主要结构体:

struct _GstPlugin {
  GstObject       object;

  /*< private >*/
  GstPluginDesc	desc;

  gchar *	filename;
  gchar *	basename;       /* base name (non-dir part) of plugin path */

  GModule *	module;		/* contains the module if plugin is loaded */

  off_t         file_size;
  time_t        file_mtime;
  gboolean      registered;     /* TRUE when the registry has seen a filename
                                 * that matches the plugin's basename */

  GstPluginPrivate *priv;

  gpointer _gst_reserved[GST_PADDING];
};

struct _GstPluginDesc {
  gint major_version;
  gint minor_version;
  const gchar *name;
  const gchar *description;
  GstPluginInitFunc plugin_init;
  const gchar *version;
  const gchar *license;
  const gchar *source;
  const gchar *package;
  const gchar *origin;
  const gchar *release_datetime;
  /*< private >*/
  gpointer _gst_reserved[GST_PADDING];
};

struct _GstRegistry {
  GstObject      object;

  /*< private >*/
  GstRegistryPrivate *priv;
};

struct _GstRegistryClass {
  GstObjectClass        parent_class;
};

struct _GstRegistryPrivate
{
  GList *plugins;
  GList *features;

  guint n_plugins;
#if 0
  GList *paths;
#endif

  int cache_file;

  /* hash to speedup _lookup_feature_locked() */
  GHashTable *feature_hash;
  /* hash to speedup _lookup */
  GHashTable *basename_hash;

  /* updated whenever the feature list changes */
  guint32 cookie;
  /* speedup for searching features */
  GList *element_factory_list;
  guint32 efl_cookie;
  GList *typefind_factory_list;
  guint32 tfl_cookie;
  GList *device_provider_factory_list;
  guint32 dmfl_cookie;
};



注册器有两个非常重要的全局变量:

static GMutex _gst_registry_mutex;
static GstRegistry *_gst_registry_default = NULL;

下面主要分析gst_element_register ()主要做了些什么?

/**
 * gst_element_register:
 * @plugin: (allow-none): #GstPlugin to register the element with, or %NULL for
 *     a static element.
 * @name: name of elements of this type
 * @rank: rank of element (higher rank means more importance when autoplugging)
 * @type: GType of element to register
 *
 * Create a new elementfactory capable of instantiating objects of the
 * @type and add the factory to @plugin.
 *
 * Returns: %TRUE, if the registering succeeded, %FALSE on error
 */
gboolean
gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
    GType type)
{
  GstPluginFeature *existing_feature;
  GstRegistry *registry;
  GstElementFactory *factory;
  GType *interfaces;
  guint n_interfaces, i;
  GstElementClass *klass;
  GList *item;

  g_return_val_if_fail (name != NULL, FALSE);
  g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);

  registry = gst_registry_get ();

  /* check if feature already exists, if it exists there is no need to update it
   * when the registry is getting updated, outdated plugins and all their
   * features are removed and readded.
   */
  existing_feature = gst_registry_lookup_feature (registry, name);
  if (existing_feature && existing_feature->plugin == plugin) {
    GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
        existing_feature, name);
    factory = GST_ELEMENT_FACTORY_CAST (existing_feature);
    factory->type = type;
    existing_feature->loaded = TRUE;
    g_type_set_qdata (type, __gst_elementclass_factory, factory);
    gst_object_unref (existing_feature);
    return TRUE;
  } else if (existing_feature) {
    gst_object_unref (existing_feature);
  }

  factory = g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL);
  gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
  GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
      g_type_name (type));

  /* provide info needed during class structure setup */
  g_type_set_qdata (type, __gst_elementclass_factory, factory);
  klass = GST_ELEMENT_CLASS (g_type_class_ref (type));

  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);

  factory->type = type;
  factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);

  for (item = klass->padtemplates; item; item = item->next) {
    GstPadTemplate *templ = item->data;
    GstStaticPadTemplate *newt;
    gchar *caps_string = gst_caps_to_string (templ->caps);

    newt = g_slice_new (GstStaticPadTemplate);
    newt->name_template = g_intern_string (templ->name_template);
    newt->direction = templ->direction;
    newt->presence = templ->presence;
    newt->static_caps.caps = NULL;
    newt->static_caps.string = g_intern_string (caps_string);
    factory->staticpadtemplates =
        g_list_append (factory->staticpadtemplates, newt);

    g_free (caps_string);
  }
  factory->numpadtemplates = klass->numpadtemplates;

  /* special stuff for URI handling */
  if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
    GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
        g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);

    if (!iface || !iface->get_type || !iface->get_protocols)
      goto urierror;
    if (iface->get_type)
      factory->uri_type = iface->get_type (factory->type);
    if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
      goto urierror;
    if (iface->get_protocols) {
      const gchar *const *protocols;

      protocols = iface->get_protocols (factory->type);
      factory->uri_protocols = g_strdupv ((gchar **) protocols);
    }
    if (!factory->uri_protocols)
      goto urierror;
  }

  interfaces = g_type_interfaces (type, &n_interfaces);
  for (i = 0; i < n_interfaces; i++) {
    __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
  }
  g_free (interfaces);

  if (plugin && plugin->desc.name) {
    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
    GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
    g_object_add_weak_pointer ((GObject *) plugin,
        (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
  } else {
    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
    GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
  }
  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
  GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;

  gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));

  return TRUE;

  /* ERRORS */
urierror:
  {
    GST_WARNING_OBJECT (factory, "error with uri handler!");
    gst_element_factory_cleanup (factory);
    return FALSE;
  }

detailserror:
  {
    gst_element_factory_cleanup (factory);
    return FALSE;
  }
}

注意,每个模块唯一对外的两个函数就是:

const GstPluginDesc gst_plugin_name_get_desc()void *gst_plugin_name_register()

gst_plugin_name_register()是什么时候被调用的呢?

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值