tee: Check for the removed pad flag also in the slow pushing path
[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  * @title: GstDynamicTypeFactory
25  * @short_description: Represents a registered dynamically loadable GType
26  * @see_also: #GstPlugin, #GstPluginFeature.
27  *
28  * #GstDynamicTypeFactory is used to represent a type that can be
29  * automatically loaded the first time it is used. For example,
30  * a non-standard type for use in caps fields.
31  *
32  * In general, applications and plugins don't need to use the factory
33  * beyond registering the type in a plugin init function. Once that is
34  * done, the type is stored in the registry, and ready as soon as the
35  * registry is loaded.
36  *
37  * ## Registering a type for dynamic loading
38  *
39  * |[<!-- language="C" -->
40  *
41  * static gboolean
42  * plugin_init (GstPlugin * plugin)
43  * {
44  *   return gst_dynamic_type_register (plugin, GST_TYPE_CUSTOM_CAPS_FIELD);
45  * }
46  * ]|
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 = g_object_new (GST_TYPE_DYNAMIC_TYPE_FACTORY, NULL);
127   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
128   GST_LOG_OBJECT (factory, "Created new dynamictypefactory for type %s", name);
129
130   if (plugin && plugin->desc.name) {
131     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
132     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
133     g_object_add_weak_pointer ((GObject *) plugin,
134         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
135   } else {
136     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
137     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
138   }
139   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
140
141   return factory;
142 }
143
144 gboolean
145 gst_dynamic_type_register (GstPlugin * plugin, GType dyn_type)
146 {
147   GstDynamicTypeFactory *factory;
148   const gchar *name;
149   GstPluginFeature *existing_feature;
150   GstRegistry *registry;
151
152   name = g_type_name (dyn_type);
153   g_return_val_if_fail (name != NULL, FALSE);
154
155   registry = gst_registry_get ();
156
157   /* check if feature already exists, if it exists there is no need to
158    * update it for this method of dynamic type */
159   existing_feature = gst_registry_lookup_feature (registry, name);
160   if (existing_feature) {
161     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
162         existing_feature, name);
163     existing_feature->loaded = TRUE;
164     GST_DYNAMIC_TYPE_FACTORY (existing_feature)->type = dyn_type;
165     gst_object_unref (existing_feature);
166     return TRUE;
167   }
168
169   factory = gst_dynamic_type_factory_create (registry, plugin, name);
170   factory->type = dyn_type;
171
172   gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
173
174   return TRUE;
175 }