2 * Copyright (C) 2015 Jan Schmidt <jan@centricular.com>
4 * gstdynamictypefactory.c: Implementation of GstDynamicTypeFactory
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gstdynamictypefactory
24 * @short_description: Represents a registered dynamically loadable GType
25 * @see_also: #GstPlugin, #GstPluginFeature.
27 * #GstDynamicTypeFactory is used to represent a type that can be
28 * automatically loaded the first time it is used. For example,
29 * a non-standard type for use in caps fields.
31 * In general, applications and plugins don't need to use the factory
32 * beyond registering the type in a plugin init function. Once that is
33 * done, the type is stored in the registry, and ready as soon as the
37 * <title>Registering a type for dynamic loading</title>
38 * <programlisting language="c">
41 * plugin_init (GstPlugin * plugin)
43 * return gst_dynamic_type_register (plugin, GST_TYPE_CUSTOM_CAPS_FIELD);
53 #include "gst_private.h"
55 #include <glib-object.h>
59 #include "glib-compat-private.h"
62 GST_DEBUG_CATEGORY_STATIC (dynamic_type_factory_debug);
63 #define GST_CAT_DEFAULT dynamic_type_factory_debug
67 GST_DEBUG_CATEGORY_INIT (dynamic_type_factory_debug, \
68 "GST_DYNAMIC_TYPE_FACTORY", GST_DEBUG_BOLD, \
69 "dynamic type factories allow automatically loading a type from a plugin"); \
72 G_DEFINE_TYPE_WITH_CODE (GstDynamicTypeFactory, gst_dynamic_type_factory,
73 GST_TYPE_PLUGIN_FEATURE, _do_init);
76 gst_dynamic_type_factory_class_init (GstDynamicTypeFactoryClass * klass)
81 gst_dynamic_type_factory_init (GstDynamicTypeFactory * factory)
85 static GstDynamicTypeFactory *
86 gst_dynamic_type_factory_find (const gchar * name)
88 GstPluginFeature *feature;
90 g_return_val_if_fail (name != NULL, NULL);
92 feature = gst_registry_find_feature (gst_registry_get (), name,
93 GST_TYPE_DYNAMIC_TYPE_FACTORY);
95 return GST_DYNAMIC_TYPE_FACTORY (feature);
101 gst_dynamic_type_factory_load (const gchar * factoryname)
103 GstDynamicTypeFactory *factory = gst_dynamic_type_factory_find (factoryname);
105 /* Called with a non-dynamic or unregistered type? */
110 GST_DYNAMIC_TYPE_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
115 GST_DEBUG_OBJECT (factory, "Loaded type %s", factoryname);
117 return factory->type;
120 static GstDynamicTypeFactory *
121 gst_dynamic_type_factory_create (GstRegistry * registry,
122 GstPlugin * plugin, const gchar * name)
124 GstDynamicTypeFactory *factory;
127 GST_DYNAMIC_TYPE_FACTORY_CAST (g_object_newv
128 (GST_TYPE_DYNAMIC_TYPE_FACTORY, 0, NULL));
129 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
130 GST_LOG_OBJECT (factory, "Created new dynamictypefactory for type %s", name);
132 if (plugin && plugin->desc.name) {
133 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
134 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
135 g_object_add_weak_pointer ((GObject *) plugin,
136 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
138 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
139 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
141 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
147 gst_dynamic_type_register (GstPlugin * plugin, GType dyn_type)
149 GstDynamicTypeFactory *factory;
151 GstPluginFeature *existing_feature;
152 GstRegistry *registry;
154 name = g_type_name (dyn_type);
155 g_return_val_if_fail (name != NULL, FALSE);
157 registry = gst_registry_get ();
159 /* check if feature already exists, if it exists there is no need to
160 * update it for this method of dynamic type */
161 existing_feature = gst_registry_lookup_feature (registry, name);
162 if (existing_feature) {
163 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
164 existing_feature, name);
165 existing_feature->loaded = TRUE;
166 GST_DYNAMIC_TYPE_FACTORY (existing_feature)->type = dyn_type;
167 gst_object_unref (existing_feature);
171 factory = gst_dynamic_type_factory_create (registry, plugin, name);
172 factory->type = dyn_type;
174 gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));