e6b304eea7b84fb511ea859acf896683b0f2cf5f
[platform/upstream/gstreamer.git] / gst / gstdynamictypefactory.c
1 /* GStreamer
2  * Copyright (C) 2015 Jan Schmidt <jan@centricular.com>
3  *
4  * gstdynamictypefactory.c: Implementation of GstDynamicTypeFactory
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gstdynamictypefactory
24  * @short_description: Represents a registered dynamically loadable GType
25  * @see_also: #GstPlugin, #GstPluginFeature.
26  *
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.
30  *
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
34  * registry is loaded.
35  *
36  * <example>
37  * <title>Registering a type for dynamic loading</title>
38  * <programlisting language="c">
39  *
40  * static gboolean
41  * plugin_init (GstPlugin * plugin)
42  * {
43  *   return gst_dynamic_type_register (plugin, GST_TYPE_CUSTOM_CAPS_FIELD);
44  * }
45  * </programlisting>
46  * </example>
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "gst_private.h"
54
55 #include <glib-object.h>
56
57 #include "gst.h"
58
59 #include "glib-compat-private.h"
60
61
62 GST_DEBUG_CATEGORY_STATIC (dynamic_type_factory_debug);
63 #define GST_CAT_DEFAULT dynamic_type_factory_debug
64
65 #define _do_init \
66 { \
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"); \
70 }
71
72 G_DEFINE_TYPE_WITH_CODE (GstDynamicTypeFactory, gst_dynamic_type_factory,
73     GST_TYPE_PLUGIN_FEATURE, _do_init);
74
75 static void
76 gst_dynamic_type_factory_class_init (GstDynamicTypeFactoryClass * klass)
77 {
78 }
79
80 static void
81 gst_dynamic_type_factory_init (GstDynamicTypeFactory * factory)
82 {
83 }
84
85 static GstDynamicTypeFactory *
86 gst_dynamic_type_factory_find (const gchar * name)
87 {
88   GstPluginFeature *feature;
89
90   g_return_val_if_fail (name != NULL, NULL);
91
92   feature = gst_registry_find_feature (gst_registry_get (), name,
93       GST_TYPE_DYNAMIC_TYPE_FACTORY);
94   if (feature)
95     return GST_DYNAMIC_TYPE_FACTORY (feature);
96
97   return NULL;
98 }
99
100 GType
101 gst_dynamic_type_factory_load (const gchar * factoryname)
102 {
103   GstDynamicTypeFactory *factory = gst_dynamic_type_factory_find (factoryname);
104
105   /* Called with a non-dynamic or unregistered type? */
106   if (factory == NULL)
107     return FALSE;
108
109   factory =
110       GST_DYNAMIC_TYPE_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
111           (factory)));
112   if (factory == NULL)
113     return 0;
114
115   GST_DEBUG_OBJECT (factory, "Loaded type %s", factoryname);
116
117   return factory->type;
118 }
119
120 static GstDynamicTypeFactory *
121 gst_dynamic_type_factory_create (GstRegistry * registry,
122     GstPlugin * plugin, const gchar * name)
123 {
124   GstDynamicTypeFactory *factory;
125
126   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);
131
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);
137   } else {
138     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
139     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
140   }
141   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
142
143   return factory;
144 }
145
146 gboolean
147 gst_dynamic_type_register (GstPlugin * plugin, GType dyn_type)
148 {
149   GstDynamicTypeFactory *factory;
150   const gchar *name;
151   GstPluginFeature *existing_feature;
152   GstRegistry *registry;
153
154   name = g_type_name (dyn_type);
155   g_return_val_if_fail (name != NULL, FALSE);
156
157   registry = gst_registry_get ();
158
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);
168     return TRUE;
169   }
170
171   factory = gst_dynamic_type_factory_create (registry, plugin, name);
172   factory->type = dyn_type;
173
174   gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
175
176   return TRUE;
177 }