- implement FLOATING flag on caps/props
[platform/upstream/gstreamer.git] / gst / gstelementfactory.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstelementfactory.c: GstElementFactory object, support routines
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* #define DEBUG_ENABLED */
24 #include "gst_private.h"
25
26 #include "gstelement.h"
27 #include "gstregistry.h"
28 #include "gstlog.h"
29
30 static void             gst_element_factory_class_init          (GstElementFactoryClass *klass);
31 static void             gst_element_factory_init                (GstElementFactory *factory);
32
33 static void             gst_element_factory_unload_thyself      (GstPluginFeature *feature);
34
35 static GstPluginFeatureClass *parent_class = NULL;
36 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
37
38 GType 
39 gst_element_factory_get_type (void) 
40 {
41   static GType elementfactory_type = 0;
42
43   if (!elementfactory_type) {
44     static const GTypeInfo elementfactory_info = {
45       sizeof (GstElementFactoryClass),
46       NULL,
47       NULL,
48       (GClassInitFunc) gst_element_factory_class_init,
49       NULL,
50       NULL,
51       sizeof(GstElementFactory),
52       0,
53       (GInstanceInitFunc) gst_element_factory_init,
54       NULL
55     };
56     elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE, 
57                                                   "GstElementFactory", &elementfactory_info, 0);
58   }
59   return elementfactory_type;
60 }
61
62 static void
63 gst_element_factory_class_init (GstElementFactoryClass *klass)
64 {
65   GObjectClass *gobject_class;
66   GstObjectClass *gstobject_class;
67   GstPluginFeatureClass *gstpluginfeature_class;
68
69   gobject_class = (GObjectClass*)klass;
70   gstobject_class = (GstObjectClass*)klass;
71   gstpluginfeature_class = (GstPluginFeatureClass*) klass;
72
73   parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);
74
75   gstpluginfeature_class->unload_thyself =      GST_DEBUG_FUNCPTR (gst_element_factory_unload_thyself);
76
77 }
78
79 static void
80 gst_element_factory_init (GstElementFactory *factory)
81 {
82   factory->padtemplates = NULL;
83   factory->numpadtemplates = 0;
84 }
85
86 /**
87  * gst_element_factory_find:
88  * @name: name of factory to find
89  *
90  * Search for an element factory of the given name.
91  *
92  * Returns: #GstElementFactory if found, NULL otherwise
93  */
94 GstElementFactory*
95 gst_element_factory_find (const gchar *name)
96 {
97   GstPluginFeature *feature;
98
99   g_return_val_if_fail(name != NULL, NULL);
100
101   feature = gst_registry_pool_find_feature (name, GST_TYPE_ELEMENT_FACTORY);
102   if (feature)
103     return GST_ELEMENT_FACTORY (feature);
104
105   /* this should be an ERROR */
106   GST_DEBUG (GST_CAT_ELEMENT_FACTORY,"no such elementfactory \"%s\"", name);
107   return NULL;
108 }
109
110 static void
111 gst_element_details_free (GstElementDetails *dp)
112 {
113   g_free (dp->longname);
114   g_free (dp->klass);
115   g_free (dp->description);
116   g_free (dp->version);
117   g_free (dp->author);
118   g_free (dp->copyright);
119   g_free (dp);
120 }
121
122 static void
123 gst_element_factory_cleanup (GstElementFactory *factory)
124 {
125   GList *padtemplates;
126
127   if (factory->details_dynamic) {
128     gst_element_details_free (factory->details);
129     factory->details_dynamic = FALSE;
130   }
131
132   padtemplates = factory->padtemplates;
133
134   while (padtemplates) {
135     GstPadTemplate *oldtempl = GST_PAD_TEMPLATE (padtemplates->data);
136      
137     gst_object_unref (GST_OBJECT (oldtempl));
138
139     padtemplates = g_list_next (padtemplates);
140   }
141   g_list_free (factory->padtemplates);
142
143   factory->padtemplates = NULL;
144   factory->numpadtemplates = 0;
145
146   g_free (GST_PLUGIN_FEATURE (factory)->name);
147 }
148
149 /**
150  * gst_element_factory_new:
151  * @name: name of new elementfactory
152  * @type: GType of new element
153  * @details: #GstElementDetails structure with element details
154  *
155  * Create a new elementfactory capable of insantiating objects of the
156  * given type.
157  *
158  * Returns: new elementfactory
159  */
160 GstElementFactory*
161 gst_element_factory_new (const gchar *name, GType type,
162                         GstElementDetails *details)
163 {
164   GstElementFactory *factory;
165
166   g_return_val_if_fail (name != NULL, NULL);
167   g_return_val_if_fail (type, NULL);
168   g_return_val_if_fail (details, NULL);
169
170   factory = gst_element_factory_find (name);
171
172   if (!factory)
173     factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
174   else {
175     gst_element_factory_cleanup (factory);
176   }
177
178   factory->details = details;
179   factory->details_dynamic = FALSE;
180
181   if (!factory->type)
182     factory->type = type;
183   else if (factory->type != type)
184     g_critical ("`%s' requested type change (!)", name);
185
186   GST_PLUGIN_FEATURE (factory)->name = g_strdup (name);
187
188   return factory;
189 }
190
191 /**
192  * gst_element_factory_create:
193  * @factory: factory to instantiate
194  * @name: name of new element
195  *
196  * Create a new element of the type defined by the given elementfactory.
197  * It will be given the name supplied, since all elements require a name as
198  * their first argument.
199  *
200  * Returns: new #GstElement
201  */
202 GstElement*
203 gst_element_factory_create (GstElementFactory *factory,
204                            const gchar *name)
205 {
206   GstElement *element;
207   GstElementClass *oclass;
208
209   g_return_val_if_fail (factory != NULL, NULL);
210
211   if (!gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory)))
212     return NULL;
213
214   GST_DEBUG (GST_CAT_ELEMENT_FACTORY,
215              "creating element from factory \"%s\" (name \"%s\", type %d)", 
216              GST_PLUGIN_FEATURE_NAME (factory), name, (gint) factory->type);
217
218   if (factory->type == 0) {
219       g_critical ("Factory for `%s' has no type",
220                   GST_PLUGIN_FEATURE_NAME (factory));
221       return NULL;
222   }
223
224   /* create an instance of the element */
225   element = GST_ELEMENT (g_object_new (factory->type, NULL));
226   g_assert (element != NULL);
227
228   /* attempt to set the elemenfactory class pointer if necessary */
229   oclass = GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS (element));
230   if (oclass->elementfactory == NULL) {
231     GST_DEBUG (GST_CAT_ELEMENT_FACTORY, "class %s", GST_PLUGIN_FEATURE_NAME (factory));
232     oclass->elementfactory = factory;
233
234     /* copy pad template pointers to the element class, 
235      * allow for custom padtemplates */
236     oclass->padtemplates = g_list_concat (oclass->padtemplates, 
237                     g_list_copy (factory->padtemplates));
238     oclass->numpadtemplates += factory->numpadtemplates;
239   }
240
241   gst_object_set_name (GST_OBJECT (element), name);
242
243   return element;
244 }
245
246 /**
247  * gst_element_factory_make:
248  * @factoryname: a named factory to instantiate
249  * @name: name of new element
250  *
251  * Create a new element of the type defined by the given element factory.
252  * If name is NULL, then the element will receive a guaranteed unique name,
253  * consisting of the element factory name and a number.
254  * If name is given, it will be given the name supplied.
255  *
256  * Returns: new #GstElement (or NULL if unable to create element)
257  */
258 GstElement*
259 gst_element_factory_make (const gchar *factoryname, const gchar *name)
260 {
261   GstElementFactory *factory;
262   GstElement *element;
263
264   g_return_val_if_fail (factoryname != NULL, NULL);
265
266   GST_DEBUG (GST_CAT_ELEMENT_FACTORY, "gstelementfactory: make \"%s\" \"%s\"", 
267              factoryname, name);
268
269   /* gst_plugin_load_element_factory (factoryname); */
270   factory = gst_element_factory_find (factoryname);
271   if (factory == NULL) {
272     GST_INFO (GST_CAT_ELEMENT_FACTORY,"no such element factory \"%s\"!",
273               factoryname);
274     return NULL;
275   }
276   element = gst_element_factory_create (factory, name);
277   if (element == NULL) {
278     GST_INFO (GST_CAT_ELEMENT_FACTORY,
279               "couldn't create instance of element factory \"%s\"!",
280               factoryname);
281     return NULL;
282   }
283
284   return element;
285 }
286
287 /**
288  * gst_element_factory_make_or_warn:
289  * @factoryname: a named factory to instantiate
290  * @name: name of new element
291  *
292  * Create a new element of the type defined by the given element factory
293  * using #gst_element_factory_make.
294  * Will use g_warning if the element could not be created.
295  *
296  * Returns: new #GstElement (or NULL if unable to create element)
297  */
298 GstElement*
299 gst_element_factory_make_or_warn (const gchar *factoryname, const gchar *name)
300 {
301   GstElement *element;
302   
303   element = gst_element_factory_make (factoryname, name);
304
305   if (element == NULL) 
306     g_warning ("Could not create element from factory %s !\n", factoryname);
307
308   return element;
309 }
310     
311 /**
312  * gst_element_factory_add_pad_template :
313  * @elementfactory: factory to add the src id to
314  * @templ: the padtemplate to add
315  *
316  * Add the given padtemplate to this elementfactory.
317  */
318 void
319 gst_element_factory_add_pad_template (GstElementFactory *factory,
320                                       GstPadTemplate *templ)
321 {
322   g_return_if_fail (factory != NULL);
323   g_return_if_fail (templ != NULL);
324
325   gst_object_ref (GST_OBJECT (templ));
326   gst_object_sink (GST_OBJECT (templ));
327
328   factory->padtemplates = g_list_append (factory->padtemplates, templ);
329   factory->numpadtemplates++;
330 }
331
332 /**
333  * gst_element_factory_can_src_caps :
334  * @factory: factory to query
335  * @caps: the caps to check
336  *
337  * Checks if the factory can source the given capability.
338  *
339  * Returns: true if it can src the capabilities
340  */
341 gboolean
342 gst_element_factory_can_src_caps (GstElementFactory *factory,
343                                  GstCaps *caps)
344 {
345   GList *templates;
346
347   g_return_val_if_fail(factory != NULL, FALSE);
348   g_return_val_if_fail(caps != NULL, FALSE);
349
350   templates = factory->padtemplates;
351
352   while (templates) {
353     GstPadTemplate *template = (GstPadTemplate *)templates->data;
354
355     if (template->direction == GST_PAD_SRC) {
356       if (gst_caps_is_always_compatible (GST_PAD_TEMPLATE_CAPS (template), caps))
357         return TRUE;
358     }
359     templates = g_list_next (templates);
360   }
361
362   return FALSE;
363 }
364
365 /**
366  * gst_element_factory_can_sink_caps :
367  * @factory: factory to query
368  * @caps: the caps to check
369  *
370  * Checks if the factory can sink the given capability.
371  *
372  * Returns: true if it can sink the capabilities
373  */
374 gboolean
375 gst_element_factory_can_sink_caps (GstElementFactory *factory,
376                                   GstCaps *caps)
377 {
378   GList *templates;
379
380   g_return_val_if_fail(factory != NULL, FALSE);
381   g_return_val_if_fail(caps != NULL, FALSE);
382
383   templates = factory->padtemplates;
384
385   while (templates) {
386     GstPadTemplate *template = (GstPadTemplate *)templates->data;
387
388     if (template->direction == GST_PAD_SINK) {
389       if (gst_caps_is_always_compatible (caps, GST_PAD_TEMPLATE_CAPS (template)))
390         return TRUE;
391     }
392     templates = g_list_next (templates);
393   }
394
395   return FALSE;
396 }
397
398 /**
399  * gst_element_factory_set_rank  :
400  * @factory: factory to rank
401  * @rank: rank value - higher number means more priority rank
402  *
403  * Specifies a rank for the element so that 
404  * autoplugging uses the most appropriate elements.
405  *
406  */
407 void
408 gst_element_factory_set_rank (GstElementFactory *factory, guint16 rank)
409 {
410   g_return_if_fail (factory != NULL);
411   factory->rank = rank;
412 }
413
414 static void
415 gst_element_factory_unload_thyself (GstPluginFeature *feature)
416 {
417   GstElementFactory *factory;
418
419   factory = GST_ELEMENT_FACTORY (feature);
420
421   factory->type = 0;
422 }