80b28f4c7623a1856ec9e3a776880c2bdb243c86
[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  *                    2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5  *
6  * gstelementfactory.c: GstElementFactory object, support routines
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:gstelementfactory
25  * @short_description: Create GstElements from a factory
26  * @see_also: #GstElement, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
27  *
28  * GstElementFactory is used to create instances of elements. A GstElementfactory
29  * can be added to a #GstPlugin as it is also a #GstPluginFeature.
30  * 
31  * Use gst_element_factory_new() to create a new factory which can be added to a
32  * plugin with gst_plugin_add_feature().
33  * 
34  * gst_element_factory_add_pad_template() is used to add a padtemplate to the factory.
35  * This function will enable the application to query for elementfactories that handle
36  * a specific media type.
37  * 
38  * Use the gst_element_factory_find() and gst_element_factory_create() functions
39  * to create element instances or use gst_element_factory_make() as a convenient 
40  * shortcut.
41  * 
42  * The following code example shows you how to create a GstFileSrc element.
43  *
44  * <example>
45  * <title>Using an element factory</title>
46  * <programlisting language="c">
47  *   #include &lt;gst/gst.h&gt;
48  *   
49  *   GstElement *src;
50  *   GstElementFactory *srcfactory;
51  *   
52  *   gst_init(&amp;argc,&amp;argv);
53  *   
54  *   srcfactory = gst_element_factory_find("filesrc");
55  *   g_return_if_fail(srcfactory != NULL);
56  *   
57  *   src = gst_element_factory_create(srcfactory,"src");
58  *   g_return_if_fail(src != NULL);
59  *   ...
60  * </programlisting>
61  * </example>
62  * 
63  * An elementfactory can be assigned a rank with gst_element_factory_set_rank() 
64  * so that the autopluggers can select a plugin more appropriatly
65  */
66
67 #include "gst_private.h"
68
69 #include "gstelement.h"
70 #include "gstinfo.h"
71 #include "gsturi.h"
72 #include "gstregistry.h"
73
74 GST_DEBUG_CATEGORY_STATIC (element_factory_debug);
75 #define GST_CAT_DEFAULT element_factory_debug
76
77 static void gst_element_factory_class_init (GstElementFactoryClass * klass);
78 static void gst_element_factory_init (GstElementFactory * factory);
79 static void gst_element_factory_finalize (GObject * object);
80 void __gst_element_details_clear (GstElementDetails * dp);
81 static void gst_element_factory_cleanup (GstElementFactory * factory);
82
83 static GstPluginFeatureClass *parent_class = NULL;
84
85 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
86
87 GType
88 gst_element_factory_get_type (void)
89 {
90   static GType elementfactory_type = 0;
91
92   if (!elementfactory_type) {
93     static const GTypeInfo elementfactory_info = {
94       sizeof (GstElementFactoryClass),
95       NULL,
96       NULL,
97       (GClassInitFunc) gst_element_factory_class_init,
98       NULL,
99       NULL,
100       sizeof (GstElementFactory),
101       0,
102       (GInstanceInitFunc) gst_element_factory_init,
103       NULL
104     };
105
106     elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
107         "GstElementFactory", &elementfactory_info, 0);
108     GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY",
109         GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
110         "element factories keep information about installed elements");
111   }
112   return elementfactory_type;
113 }
114 static void
115 gst_element_factory_class_init (GstElementFactoryClass * klass)
116 {
117   GObjectClass *gobject_class;
118   GstObjectClass *gstobject_class;
119   GstPluginFeatureClass *gstpluginfeature_class;
120
121   gobject_class = (GObjectClass *) klass;
122   gstobject_class = (GstObjectClass *) klass;
123   gstpluginfeature_class = (GstPluginFeatureClass *) klass;
124
125   parent_class = g_type_class_peek_parent (klass);
126
127   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_factory_finalize);
128 }
129
130 static void
131 gst_element_factory_init (GstElementFactory * factory)
132 {
133   factory->staticpadtemplates = NULL;
134   factory->numpadtemplates = 0;
135
136   factory->uri_type = GST_URI_UNKNOWN;
137   factory->uri_protocols = NULL;
138
139   factory->interfaces = NULL;
140 }
141
142 static void
143 gst_element_factory_finalize (GObject * object)
144 {
145   GstElementFactory *factory = GST_ELEMENT_FACTORY (object);
146
147   gst_element_factory_cleanup (factory);
148   G_OBJECT_CLASS (parent_class)->finalize (object);
149 }
150
151 /**
152  * gst_element_factory_find:
153  * @name: name of factory to find
154  *
155  * Search for an element factory of the given name. Refs the returned
156  * element factory; caller is responsible for unreffing.
157  *
158  * Returns: #GstElementFactory if found, NULL otherwise
159  */
160 GstElementFactory *
161 gst_element_factory_find (const gchar * name)
162 {
163   GstPluginFeature *feature;
164
165   g_return_val_if_fail (name != NULL, NULL);
166
167   feature = gst_registry_find_feature (gst_registry_get_default (), name,
168       GST_TYPE_ELEMENT_FACTORY);
169   if (feature)
170     return GST_ELEMENT_FACTORY (feature);
171
172   /* this isn't an error, for instance when you query if an element factory is
173    * present */
174   GST_LOG ("no such element factory \"%s\"", name);
175   return NULL;
176 }
177
178 void
179 __gst_element_details_clear (GstElementDetails * dp)
180 {
181   g_free (dp->longname);
182   dp->longname = NULL;
183   g_free (dp->klass);
184   dp->klass = NULL;
185   g_free (dp->description);
186   dp->description = NULL;
187   g_free (dp->author);
188   dp->author = NULL;
189 }
190
191 #define VALIDATE_SET(__dest, __src, __entry)                            \
192 G_STMT_START {                                                          \
193   if (g_utf8_validate (__src->__entry, -1, NULL)) {                     \
194     __dest->__entry = g_strdup (__src->__entry);                        \
195   } else {                                                              \
196     g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s",        \
197         __src->__entry);                                                \
198     __dest->__entry = g_strdup ("[ERROR: invalid UTF-8]");              \
199   }                                                                     \
200 } G_STMT_END
201
202 void
203 __gst_element_details_set (GstElementDetails * dest,
204     const GstElementDetails * src)
205 {
206   VALIDATE_SET (dest, src, longname);
207   VALIDATE_SET (dest, src, klass);
208   VALIDATE_SET (dest, src, description);
209   VALIDATE_SET (dest, src, author);
210 }
211
212 void
213 __gst_element_details_copy (GstElementDetails * dest,
214     const GstElementDetails * src)
215 {
216   __gst_element_details_clear (dest);
217   __gst_element_details_set (dest, src);
218 }
219
220 static void
221 gst_element_factory_cleanup (GstElementFactory * factory)
222 {
223   GList *item;
224
225   __gst_element_details_clear (&factory->details);
226   if (factory->type) {
227     g_type_class_unref (g_type_class_peek (factory->type));
228     factory->type = 0;
229   }
230
231   for (item = factory->staticpadtemplates; item; item = item->next) {
232     GstStaticPadTemplate *templ = item->data;
233
234     g_free (templ->name_template);
235     g_free ((gchar *) templ->static_caps.string);
236     memset (&(templ->static_caps), 0, sizeof (GstStaticCaps));
237     g_free (templ);
238   }
239   g_list_free (factory->staticpadtemplates);
240   factory->staticpadtemplates = NULL;
241   factory->numpadtemplates = 0;
242   factory->uri_type = GST_URI_UNKNOWN;
243   if (factory->uri_protocols) {
244     g_strfreev (factory->uri_protocols);
245     factory->uri_protocols = NULL;
246   }
247
248   g_list_foreach (factory->interfaces, (GFunc) g_free, NULL);
249   g_list_free (factory->interfaces);
250   factory->interfaces = NULL;
251 }
252
253 /**
254  * gst_element_register:
255  * @plugin: #GstPlugin to register the element with
256  * @name: name of elements of this type
257  * @rank: rank of element (higher rank means more importance when autoplugging)
258  * @type: GType of element to register
259  *
260  * Create a new elementfactory capable of instantiating objects of the
261  * given type.
262  *
263  * Returns: TRUE, if the registering succeeded, FALSE on error
264  */
265 gboolean
266 gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
267     GType type)
268 {
269   GstElementFactory *factory;
270   GType *interfaces;
271   guint n_interfaces, i;
272   GstElementClass *klass;
273   GList *item;
274
275   g_return_val_if_fail (name != NULL, FALSE);
276   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
277
278   factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
279   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
280   GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
281       g_type_name (type));
282
283   klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
284   factory->type = type;
285   __gst_element_details_copy (&factory->details, &klass->details);
286   for (item = klass->padtemplates; item; item = item->next) {
287     GstPadTemplate *templ = item->data;
288     GstStaticPadTemplate *newt;
289
290     newt = g_new0 (GstStaticPadTemplate, 1);
291     newt->name_template = g_strdup (templ->name_template);
292     newt->direction = templ->direction;
293     newt->presence = templ->presence;
294     newt->static_caps.string = gst_caps_to_string (templ->caps);
295     factory->staticpadtemplates =
296         g_list_append (factory->staticpadtemplates, newt);
297   }
298   factory->numpadtemplates = klass->numpadtemplates;
299   klass->elementfactory = factory;
300
301   /* special stuff for URI handling */
302   if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
303     GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
304         g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
305
306     if (!iface || !iface->get_type || !iface->get_protocols)
307       goto error;
308     factory->uri_type = iface->get_type ();
309     if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
310       goto error;
311     factory->uri_protocols = g_strdupv (iface->get_protocols ());
312     if (!factory->uri_protocols)
313       goto error;
314   }
315
316   interfaces = g_type_interfaces (type, &n_interfaces);
317   for (i = 0; i < n_interfaces; i++) {
318     __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
319   }
320   g_free (interfaces);
321
322   GST_PLUGIN_FEATURE (factory)->plugin_name = g_strdup (plugin->desc.name);
323   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
324   GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
325
326   gst_registry_add_feature (gst_registry_get_default (),
327       GST_PLUGIN_FEATURE (factory));
328
329   return TRUE;
330
331 error:
332   gst_element_factory_cleanup (factory);
333   return FALSE;
334 }
335
336 /**
337  * gst_element_factory_create:
338  * @factory: factory to instantiate
339  * @name: name of new element
340  *
341  * Create a new element of the type defined by the given elementfactory.
342  * It will be given the name supplied, since all elements require a name as
343  * their first argument.
344  *
345  * Returns: new #GstElement or NULL if the element couldn't be created
346  */
347 GstElement *
348 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
349 {
350   GstElement *element;
351   GstElementClass *oclass;
352   GstElementFactory *newfactory;
353
354   g_return_val_if_fail (factory != NULL, NULL);
355
356   gst_object_ref (factory);
357
358   newfactory =
359       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
360           (factory)));
361   if (newfactory == NULL) {
362     GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
363     return NULL;
364   } else {
365     gst_object_unref (factory);
366     factory = newfactory;
367   }
368
369   if (name)
370     GST_INFO ("creating element \"%s\" named \"%s\"",
371         GST_PLUGIN_FEATURE_NAME (factory), GST_STR_NULL (name));
372   else
373     GST_INFO ("creating element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
374
375 #if 0
376   if (factory->type == 0) {
377     g_critical ("Plugin didn't set object type in feature.");
378
379     return NULL;
380   }
381 #endif
382
383   /* FIXME: the object class gets a pointer to the factory that might
384    * be disposed at the end of this call if it was newly loaded;
385    * to fix that, we should ref and then unref in an object class finalize,
386    * which we don't have currently. */
387   oclass = GST_ELEMENT_CLASS (g_type_class_ref (factory->type));
388   if (oclass->elementfactory == NULL)
389     oclass->elementfactory = factory;
390
391   /* create an instance of the element */
392   element = GST_ELEMENT (g_object_new (factory->type, NULL));
393   g_assert (element != NULL);
394
395   g_type_class_unref (oclass);
396
397   if (name)
398     gst_object_set_name (GST_OBJECT (element), name);
399
400   GST_DEBUG ("created element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
401
402   return element;
403 }
404
405 /**
406  * gst_element_factory_make:
407  * @factoryname: a named factory to instantiate
408  * @name: name of new element
409  *
410  * Create a new element of the type defined by the given element factory.
411  * If name is NULL, then the element will receive a guaranteed unique name,
412  * consisting of the element factory name and a number.
413  * If name is given, it will be given the name supplied.
414  *
415  * Returns: new #GstElement or NULL if unable to create element
416  */
417 GstElement *
418 gst_element_factory_make (const gchar * factoryname, const gchar * name)
419 {
420   GstElementFactory *factory;
421   GstElement *element;
422
423   g_return_val_if_fail (factoryname != NULL, NULL);
424
425   GST_LOG ("gstelementfactory: make \"%s\" \"%s\"",
426       factoryname, GST_STR_NULL (name));
427
428   /* gst_plugin_load_element_factory (factoryname); */
429   factory = gst_element_factory_find (factoryname);
430   if (factory == NULL) {
431     GST_INFO ("no such element factory \"%s\"!", factoryname);
432     return NULL;
433   }
434   GST_LOG ("gstelementfactory: found factory %p", factory);
435   element = gst_element_factory_create (factory, name);
436   gst_object_unref (factory);
437   if (element == NULL) {
438     GST_INFO_OBJECT (factory, "couldn't create instance!");
439     return NULL;
440   }
441
442   return element;
443 }
444
445 void
446 __gst_element_factory_add_static_pad_template (GstElementFactory * factory,
447     GstStaticPadTemplate * templ)
448 {
449   g_return_if_fail (factory != NULL);
450   g_return_if_fail (templ != NULL);
451
452   factory->staticpadtemplates =
453       g_list_append (factory->staticpadtemplates, templ);
454   factory->numpadtemplates++;
455 }
456
457 /**
458  * gst_element_factory_get_element_type:
459  * @factory: factory to get managed #GType from
460  * 
461  * Get the #GType for elements managed by this factory
462  *
463  * Returns: the #GType for elements managed by this factory
464  */
465 GType
466 gst_element_factory_get_element_type (GstElementFactory * factory)
467 {
468   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
469
470   return factory->type;
471 }
472
473 /**
474  * gst_element_factory_get_longname:
475  * @factory: a #GstElementFactory
476  * 
477  * Gets the longname for this factory
478  *
479  * Returns: the longname
480  */
481 G_CONST_RETURN gchar *
482 gst_element_factory_get_longname (GstElementFactory * factory)
483 {
484   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
485
486   return factory->details.longname;
487 }
488
489 /**
490  * gst_element_factory_get_klass:
491  * @factory: a #GstElementFactory
492  * 
493  * Gets the class for this factory.
494  *
495  * Returns: the class
496  */
497 G_CONST_RETURN gchar *
498 gst_element_factory_get_klass (GstElementFactory * factory)
499 {
500   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
501
502   return factory->details.klass;
503 }
504
505 /**
506  * gst_element_factory_get_description:
507  * @factory: a #GstElementFactory
508  * 
509  * Gets the description for this factory.
510  *
511  * Returns: the description
512  */
513 G_CONST_RETURN gchar *
514 gst_element_factory_get_description (GstElementFactory * factory)
515 {
516   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
517
518   return factory->details.description;
519 }
520
521 /**
522  * gst_element_factory_get_author:
523  * @factory: a #GstElementFactory
524  * 
525  * Gets the author for this factory.
526  *
527  * Returns: the author
528  */
529 G_CONST_RETURN gchar *
530 gst_element_factory_get_author (GstElementFactory * factory)
531 {
532   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
533
534   return factory->details.author;
535 }
536
537 /**
538  * gst_element_factory_get_num_pad_templates:
539  * @factory: a #GstElementFactory
540  * 
541  * Gets the number of pad_templates in this factory.
542  *
543  * Returns: the number of pad_templates
544  */
545 guint
546 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
547 {
548   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
549
550   return factory->numpadtemplates;
551 }
552
553 /**
554  * __gst_element_factory_add_interface:
555  * @elementfactory: The elementfactory to add the interface to
556  * @interfacename: Name of the interface
557  *
558  * Adds the given interfacename to the list of implemented interfaces of the
559  * element.
560  */
561 void
562 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
563     const gchar * interfacename)
564 {
565   g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
566   g_return_if_fail (interfacename != NULL);
567   g_return_if_fail (interfacename[0] != '\0');  /* no empty string */
568
569   elementfactory->interfaces =
570       g_list_prepend (elementfactory->interfaces, g_strdup (interfacename));
571 }
572
573 /**
574  * gst_element_factory_get_static_pad_templates:
575  * @factory: a #GstElementFactory
576  * 
577  * Gets the #GList of padtemplates for this factory.
578  *
579  * Returns: the padtemplates
580  */
581 G_CONST_RETURN GList *
582 gst_element_factory_get_static_pad_templates (GstElementFactory * factory)
583 {
584   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
585
586   return factory->staticpadtemplates;
587 }
588
589 /**
590  * gst_element_factory_get_uri_type:
591  * @factory: a #GstElementFactory
592  * 
593  * Gets the type of URIs the element supports or GST_URI_UNKNOWN if none.
594  *
595  * Returns: type of URIs this element supports
596  */
597 guint
598 gst_element_factory_get_uri_type (GstElementFactory * factory)
599 {
600   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
601
602   return factory->uri_type;
603 }
604
605 /**
606  * gst_element_factory_get_uri_protocols:
607  * @factory: a #GstElementFactory
608  * 
609  * Gets a NULL-terminated array of protocols this element supports or NULL, if
610  * no protocols are supported. You may not change the contents of the returned
611  * array as it is still ownt by the element factory. Use g_strdupv() if you want to.
612  *
613  * Returns: the supported protocols or NULL
614  */
615 gchar **
616 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
617 {
618   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
619
620   return factory->uri_protocols;
621 }