gst/gstelementfactory.c: Unref the factory after it was used the last time, not before.
[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 /**
25  * SECTION:gstelementfactory
26  * @short_description: Create GstElements from a factory
27  * @see_also: #GstElement, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
28  *
29  * #GstElementFactory is used to create instances of elements. A
30  * GstElementfactory can be added to a #GstPlugin as it is also a
31  * #GstPluginFeature.
32  *
33  * Use the gst_element_factory_find() and gst_element_factory_create()
34  * functions to create element instances or use gst_element_factory_make() as a
35  * convenient shortcut.
36  *
37  * The following code example shows you how to create a GstFileSrc element.
38  *
39  * <example>
40  * <title>Using an element factory</title>
41  * <programlisting language="c">
42  *   #include &lt;gst/gst.h&gt;
43  *   GstElement *src;
44  *   GstElementFactory *srcfactory;
45  *   gst_init(&amp;argc,&amp;argv);
46  *   srcfactory = gst_element_factory_find("filesrc");
47  *   g_return_if_fail(srcfactory != NULL);
48  *   src = gst_element_factory_create(srcfactory,"src");
49  *   g_return_if_fail(src != NULL);
50  *   ...
51  * </programlisting>
52  * </example>
53  *
54  * Last reviewed on 2005-11-23 (0.9.5)
55  */
56
57 #include "gst_private.h"
58
59 #include "gstelement.h"
60 #include "gstinfo.h"
61 #include "gsturi.h"
62 #include "gstregistry.h"
63
64 #include "glib-compat-private.h"
65
66 GST_DEBUG_CATEGORY_STATIC (element_factory_debug);
67 #define GST_CAT_DEFAULT element_factory_debug
68
69 static void gst_element_factory_class_init (GstElementFactoryClass * klass);
70 static void gst_element_factory_init (GstElementFactory * factory);
71 static void gst_element_factory_finalize (GObject * object);
72 void __gst_element_details_clear (GstElementDetails * dp);
73 static void gst_element_factory_cleanup (GstElementFactory * factory);
74
75 static GstPluginFeatureClass *parent_class = NULL;
76
77 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
78
79 GType
80 gst_element_factory_get_type (void)
81 {
82   static GType elementfactory_type = 0;
83
84   if (G_UNLIKELY (elementfactory_type == 0)) {
85     static const GTypeInfo elementfactory_info = {
86       sizeof (GstElementFactoryClass),
87       NULL,
88       NULL,
89       (GClassInitFunc) gst_element_factory_class_init,
90       NULL,
91       NULL,
92       sizeof (GstElementFactory),
93       0,
94       (GInstanceInitFunc) gst_element_factory_init,
95       NULL
96     };
97
98     elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
99         "GstElementFactory", &elementfactory_info, 0);
100     GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY",
101         GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
102         "element factories keep information about installed elements");
103   }
104   return elementfactory_type;
105 }
106 static void
107 gst_element_factory_class_init (GstElementFactoryClass * klass)
108 {
109   GObjectClass *gobject_class;
110   GstObjectClass *gstobject_class;
111   GstPluginFeatureClass *gstpluginfeature_class;
112
113   gobject_class = (GObjectClass *) klass;
114   gstobject_class = (GstObjectClass *) klass;
115   gstpluginfeature_class = (GstPluginFeatureClass *) klass;
116
117   parent_class = g_type_class_peek_parent (klass);
118
119   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_factory_finalize);
120 }
121
122 static void
123 gst_element_factory_init (GstElementFactory * factory)
124 {
125   factory->staticpadtemplates = NULL;
126   factory->numpadtemplates = 0;
127
128   factory->uri_type = GST_URI_UNKNOWN;
129   factory->uri_protocols = NULL;
130
131   factory->interfaces = NULL;
132 }
133
134 static void
135 gst_element_factory_finalize (GObject * object)
136 {
137   GstElementFactory *factory = GST_ELEMENT_FACTORY (object);
138
139   gst_element_factory_cleanup (factory);
140   G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 /**
144  * gst_element_factory_find:
145  * @name: name of factory to find
146  *
147  * Search for an element factory of the given name. Refs the returned
148  * element factory; caller is responsible for unreffing.
149  *
150  * Returns: #GstElementFactory if found, NULL otherwise
151  */
152 GstElementFactory *
153 gst_element_factory_find (const gchar * name)
154 {
155   GstPluginFeature *feature;
156
157   g_return_val_if_fail (name != NULL, NULL);
158
159   feature = gst_registry_find_feature (gst_registry_get_default (), name,
160       GST_TYPE_ELEMENT_FACTORY);
161   if (feature)
162     return GST_ELEMENT_FACTORY (feature);
163
164   /* this isn't an error, for instance when you query if an element factory is
165    * present */
166   GST_LOG ("no such element factory \"%s\"", name);
167   return NULL;
168 }
169
170 void
171 __gst_element_details_clear (GstElementDetails * dp)
172 {
173   g_free (dp->longname);
174   g_free (dp->klass);
175   g_free (dp->description);
176   g_free (dp->author);
177   memset (dp, 0, sizeof (GstElementDetails));
178 }
179
180 #define VALIDATE_SET(__dest, __src, __entry)                            \
181 G_STMT_START {                                                          \
182   if (g_utf8_validate (__src->__entry, -1, NULL)) {                     \
183     __dest->__entry = g_strdup (__src->__entry);                        \
184   } else {                                                              \
185     g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s",        \
186         __src->__entry);                                                \
187     __dest->__entry = g_strdup ("[ERROR: invalid UTF-8]");              \
188   }                                                                     \
189 } G_STMT_END
190
191 void
192 __gst_element_details_set (GstElementDetails * dest,
193     const GstElementDetails * src)
194 {
195   VALIDATE_SET (dest, src, longname);
196   VALIDATE_SET (dest, src, klass);
197   VALIDATE_SET (dest, src, description);
198   VALIDATE_SET (dest, src, author);
199 }
200
201 void
202 __gst_element_details_copy (GstElementDetails * dest,
203     const GstElementDetails * src)
204 {
205   __gst_element_details_clear (dest);
206   __gst_element_details_set (dest, src);
207 }
208
209 static void
210 gst_element_factory_cleanup (GstElementFactory * factory)
211 {
212   GList *item;
213
214   __gst_element_details_clear (&factory->details);
215   if (factory->type) {
216     g_type_class_unref (g_type_class_peek (factory->type));
217     factory->type = 0;
218   }
219
220   for (item = factory->staticpadtemplates; item; item = item->next) {
221     GstStaticPadTemplate *templ = item->data;
222     GstCaps *caps = (GstCaps *) & (templ->static_caps);
223
224     g_free ((gchar *) templ->static_caps.string);
225
226     /* FIXME: this is not threadsafe */
227     if (caps->refcount == 1) {
228       GstStructure *structure;
229       guint i;
230
231       for (i = 0; i < caps->structs->len; i++) {
232         structure = (GstStructure *) gst_caps_get_structure (caps, i);
233         gst_structure_set_parent_refcount (structure, NULL);
234         gst_structure_free (structure);
235       }
236       g_ptr_array_free (caps->structs, TRUE);
237       caps->refcount = 0;
238     }
239     g_free (templ);
240   }
241   g_list_free (factory->staticpadtemplates);
242   factory->staticpadtemplates = NULL;
243   factory->numpadtemplates = 0;
244   factory->uri_type = GST_URI_UNKNOWN;
245   if (factory->uri_protocols) {
246     g_strfreev (factory->uri_protocols);
247     factory->uri_protocols = NULL;
248   }
249
250   g_list_foreach (factory->interfaces, (GFunc) g_free, NULL);
251   g_list_free (factory->interfaces);
252   factory->interfaces = NULL;
253 }
254
255 /**
256  * gst_element_register:
257  * @plugin: #GstPlugin to register the element with, or NULL for a static
258  * element (note that passing NULL only works in GStreamer 0.10.13 and later)
259  * @name: name of elements of this type
260  * @rank: rank of element (higher rank means more importance when autoplugging)
261  * @type: GType of element to register
262  *
263  * Create a new elementfactory capable of instantiating objects of the
264  * @type and add the factory to @plugin.
265  *
266  * Returns: TRUE, if the registering succeeded, FALSE on error
267  */
268 gboolean
269 gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
270     GType type)
271 {
272   GstElementFactory *factory;
273   GType *interfaces;
274   guint n_interfaces, i;
275   GstElementClass *klass;
276   GList *item;
277
278   g_return_val_if_fail (name != NULL, FALSE);
279   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
280
281   factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
282   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
283   GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
284       g_type_name (type));
285
286   klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
287   if ((klass->details.longname == NULL) ||
288       (klass->details.klass == NULL) || (klass->details.author == NULL))
289     goto detailserror;
290
291   factory->type = type;
292   __gst_element_details_copy (&factory->details, &klass->details);
293   for (item = klass->padtemplates; item; item = item->next) {
294     GstPadTemplate *templ = item->data;
295     GstStaticPadTemplate *newt;
296
297     newt = g_new0 (GstStaticPadTemplate, 1);
298     newt->name_template = g_intern_string (templ->name_template);
299     newt->direction = templ->direction;
300     newt->presence = templ->presence;
301     newt->static_caps.string = gst_caps_to_string (templ->caps);
302     factory->staticpadtemplates =
303         g_list_append (factory->staticpadtemplates, newt);
304   }
305   factory->numpadtemplates = klass->numpadtemplates;
306   klass->elementfactory = factory;
307
308   /* special stuff for URI handling */
309   if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
310     GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
311         g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
312
313     if (!iface || (!iface->get_type && !iface->get_type_full) ||
314         (!iface->get_protocols && !iface->get_protocols_full))
315       goto urierror;
316     if (iface->get_type)
317       factory->uri_type = iface->get_type ();
318     else if (iface->get_type_full)
319       factory->uri_type = iface->get_type_full (factory->type);
320     if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
321       goto urierror;
322     if (iface->get_protocols)
323       factory->uri_protocols = g_strdupv (iface->get_protocols ());
324     else if (iface->get_protocols_full)
325       factory->uri_protocols = iface->get_protocols_full (factory->type);
326     if (!factory->uri_protocols)
327       goto urierror;
328   }
329
330   interfaces = g_type_interfaces (type, &n_interfaces);
331   for (i = 0; i < n_interfaces; i++) {
332     __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
333   }
334   g_free (interfaces);
335
336   if (plugin && plugin->desc.name) {
337     GST_PLUGIN_FEATURE (factory)->plugin_name = plugin->desc.name;
338   } else {
339     GST_PLUGIN_FEATURE (factory)->plugin_name = "NULL";
340   }
341   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
342   GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
343
344   gst_registry_add_feature (gst_registry_get_default (),
345       GST_PLUGIN_FEATURE (factory));
346
347   return TRUE;
348
349   /* ERRORS */
350 urierror:
351   {
352     GST_WARNING_OBJECT (factory, "error with uri handler!");
353     gst_element_factory_cleanup (factory);
354     return FALSE;
355   }
356
357 detailserror:
358   {
359     GST_WARNING_OBJECT (factory,
360         "The GstElementDetails don't seem to have been set properly");
361     gst_element_factory_cleanup (factory);
362     return FALSE;
363   }
364 }
365
366 /**
367  * gst_element_factory_create:
368  * @factory: factory to instantiate
369  * @name: name of new element
370  *
371  * Create a new element of the type defined by the given elementfactory.
372  * It will be given the name supplied, since all elements require a name as
373  * their first argument.
374  *
375  * Returns: new #GstElement or NULL if the element couldn't be created
376  */
377 GstElement *
378 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
379 {
380   GstElement *element;
381   GstElementClass *oclass;
382   GstElementFactory *newfactory;
383
384   g_return_val_if_fail (factory != NULL, NULL);
385
386   newfactory =
387       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
388           (factory)));
389
390   if (newfactory == NULL)
391     goto load_failed;
392
393   factory = newfactory;
394
395   if (name)
396     GST_INFO ("creating element \"%s\" named \"%s\"",
397         GST_PLUGIN_FEATURE_NAME (factory), GST_STR_NULL (name));
398   else
399     GST_INFO ("creating element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
400
401   if (factory->type == 0)
402     goto no_type;
403
404   /* create an instance of the element, cast so we don't assert on NULL */
405   element = GST_ELEMENT_CAST (g_object_new (factory->type, NULL));
406   if (G_UNLIKELY (element == NULL))
407     goto no_element;
408
409   /* fill in the pointer to the factory in the element class. The
410    * class will not be unreffed currently. 
411    * FIXME: This isn't safe and may leak a refcount on the factory if 2 threads
412    * create the first instance of an element at the same moment */
413   oclass = GST_ELEMENT_GET_CLASS (element);
414   if (G_UNLIKELY (oclass->elementfactory == NULL))
415     oclass->elementfactory = factory;
416   else
417     gst_object_unref (factory);
418
419   if (name)
420     gst_object_set_name (GST_OBJECT (element), name);
421
422   GST_DEBUG ("created element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
423
424   return element;
425
426   /* ERRORS */
427 load_failed:
428   {
429     GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
430     return NULL;
431   }
432 no_type:
433   {
434     GST_WARNING_OBJECT (factory, "factory has no type");
435     gst_object_unref (factory);
436     return NULL;
437   }
438 no_element:
439   {
440     GST_WARNING_OBJECT (factory, "could not create element");
441     gst_object_unref (factory);
442     return NULL;
443   }
444 }
445
446 /**
447  * gst_element_factory_make:
448  * @factoryname: a named factory to instantiate
449  * @name: name of new element
450  *
451  * Create a new element of the type defined by the given element factory.
452  * If name is NULL, then the element will receive a guaranteed unique name,
453  * consisting of the element factory name and a number.
454  * If name is given, it will be given the name supplied.
455  *
456  * Returns: new #GstElement or NULL if unable to create element
457  */
458 GstElement *
459 gst_element_factory_make (const gchar * factoryname, const gchar * name)
460 {
461   GstElementFactory *factory;
462   GstElement *element;
463
464   g_return_val_if_fail (factoryname != NULL, NULL);
465
466   GST_LOG ("gstelementfactory: make \"%s\" \"%s\"",
467       factoryname, GST_STR_NULL (name));
468
469   factory = gst_element_factory_find (factoryname);
470   if (factory == NULL)
471     goto no_factory;
472
473   GST_LOG_OBJECT (factory, "found factory %p", factory);
474   element = gst_element_factory_create (factory, name);
475   if (element == NULL)
476     goto create_failed;
477
478   gst_object_unref (factory);
479   return element;
480
481   /* ERRORS */
482 no_factory:
483   {
484     GST_INFO ("no such element factory \"%s\"!", factoryname);
485     return NULL;
486   }
487 create_failed:
488   {
489     GST_INFO_OBJECT (factory, "couldn't create instance!");
490     gst_object_unref (factory);
491     return NULL;
492   }
493 }
494
495 void
496 __gst_element_factory_add_static_pad_template (GstElementFactory * factory,
497     GstStaticPadTemplate * templ)
498 {
499   g_return_if_fail (factory != NULL);
500   g_return_if_fail (templ != NULL);
501
502   factory->staticpadtemplates =
503       g_list_append (factory->staticpadtemplates, templ);
504   factory->numpadtemplates++;
505 }
506
507 /**
508  * gst_element_factory_get_element_type:
509  * @factory: factory to get managed #GType from
510  *
511  * Get the #GType for elements managed by this factory. The type can
512  * only be retrieved if the element factory is loaded, which can be
513  * assured with gst_plugin_feature_load().
514  *
515  * Returns: the #GType for elements managed by this factory or 0 if
516  * the factory is not loaded.
517  */
518 GType
519 gst_element_factory_get_element_type (GstElementFactory * factory)
520 {
521   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
522
523   return factory->type;
524 }
525
526 /**
527  * gst_element_factory_get_longname:
528  * @factory: a #GstElementFactory
529  *
530  * Gets the longname for this factory
531  *
532  * Returns: the longname
533  */
534 G_CONST_RETURN gchar *
535 gst_element_factory_get_longname (GstElementFactory * factory)
536 {
537   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
538
539   return factory->details.longname;
540 }
541
542 /**
543  * gst_element_factory_get_klass:
544  * @factory: a #GstElementFactory
545  *
546  * Gets the class for this factory.
547  *
548  * Returns: the class
549  */
550 G_CONST_RETURN gchar *
551 gst_element_factory_get_klass (GstElementFactory * factory)
552 {
553   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
554
555   return factory->details.klass;
556 }
557
558 /**
559  * gst_element_factory_get_description:
560  * @factory: a #GstElementFactory
561  *
562  * Gets the description for this factory.
563  *
564  * Returns: the description
565  */
566 G_CONST_RETURN gchar *
567 gst_element_factory_get_description (GstElementFactory * factory)
568 {
569   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
570
571   return factory->details.description;
572 }
573
574 /**
575  * gst_element_factory_get_author:
576  * @factory: a #GstElementFactory
577  *
578  * Gets the author for this factory.
579  *
580  * Returns: the author
581  */
582 G_CONST_RETURN gchar *
583 gst_element_factory_get_author (GstElementFactory * factory)
584 {
585   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
586
587   return factory->details.author;
588 }
589
590 /**
591  * gst_element_factory_get_num_pad_templates:
592  * @factory: a #GstElementFactory
593  *
594  * Gets the number of pad_templates in this factory.
595  *
596  * Returns: the number of pad_templates
597  */
598 guint
599 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
600 {
601   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
602
603   return factory->numpadtemplates;
604 }
605
606 /**
607  * __gst_element_factory_add_interface:
608  * @elementfactory: The elementfactory to add the interface to
609  * @interfacename: Name of the interface
610  *
611  * Adds the given interfacename to the list of implemented interfaces of the
612  * element.
613  */
614 void
615 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
616     const gchar * interfacename)
617 {
618   g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
619   g_return_if_fail (interfacename != NULL);
620   g_return_if_fail (interfacename[0] != '\0');  /* no empty string */
621
622   elementfactory->interfaces =
623       g_list_prepend (elementfactory->interfaces, g_strdup (interfacename));
624 }
625
626 /**
627  * gst_element_factory_get_static_pad_templates:
628  * @factory: a #GstElementFactory
629  *
630  * Gets the #GList of #GstStaticPadTemplate for this factory.
631  *
632  * Returns: the padtemplates
633  */
634 G_CONST_RETURN GList *
635 gst_element_factory_get_static_pad_templates (GstElementFactory * factory)
636 {
637   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
638
639   return factory->staticpadtemplates;
640 }
641
642 /**
643  * gst_element_factory_get_uri_type:
644  * @factory: a #GstElementFactory
645  *
646  * Gets the type of URIs the element supports or GST_URI_UNKNOWN if none.
647  *
648  * Returns: type of URIs this element supports
649  */
650 gint
651 gst_element_factory_get_uri_type (GstElementFactory * factory)
652 {
653   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
654
655   return factory->uri_type;
656 }
657
658 /**
659  * gst_element_factory_get_uri_protocols:
660  * @factory: a #GstElementFactory
661  *
662  * Gets a NULL-terminated array of protocols this element supports or NULL if
663  * no protocols are supported. You may not change the contents of the returned
664  * array, as it is still owned by the element factory. Use g_strdupv() to
665  * make a copy of the protocol string array if you need to.
666  *
667  * Returns: the supported protocols or NULL
668  */
669 gchar **
670 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
671 {
672   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
673
674   return factory->uri_protocols;
675 }
676
677 /**
678  * gst_element_factory_has_interface:
679  * @factory: a #GstElementFactory
680  * @interfacename: an interface name
681  *
682  * Check if @factory implements the interface with name @interfacename.
683  *
684  * Returns: #TRUE when @factory implement the interface.
685  *
686  * Since: 0.10.14
687  */
688 gboolean
689 gst_element_factory_has_interface (GstElementFactory * factory,
690     const gchar * interfacename)
691 {
692   GList *walk;
693
694   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), FALSE);
695
696   for (walk = factory->interfaces; walk; walk = g_list_next (walk)) {
697     gchar *iname = (gchar *) walk->data;
698
699     if (!strcmp (iname, interfacename))
700       return TRUE;
701   }
702   return FALSE;
703 }