rtsp-server: fix memory leak
[platform/upstream/gstreamer.git] / subprojects / gstreamer / 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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:gstelementfactory
26  * @title: GstElementFactory
27  * @short_description: Create GstElements from a factory
28  * @see_also: #GstElement, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
29  *
30  * #GstElementFactory is used to create instances of elements. A
31  * GstElementFactory can be added to a #GstPlugin as it is also a
32  * #GstPluginFeature.
33  *
34  * Use the gst_element_factory_find() and gst_element_factory_create()
35  * functions to create element instances or use gst_element_factory_make() as a
36  * convenient shortcut.
37  *
38  * The following code example shows you how to create a GstFileSrc element.
39  *
40  * ## Using an element factory
41  * |[<!-- language="C" -->
42  *   #include <gst/gst.h>
43  *
44  *   GstElement *src;
45  *   GstElementFactory *srcfactory;
46  *
47  *   gst_init (&argc, &argv);
48  *
49  *   srcfactory = gst_element_factory_find ("filesrc");
50  *   g_return_if_fail (srcfactory != NULL);
51  *   src = gst_element_factory_create (srcfactory, "src");
52  *   g_return_if_fail (src != NULL);
53  *   ...
54  * ]|
55  */
56
57 #include "gst_private.h"
58
59 #include "gstelement.h"
60 #include "gstelementmetadata.h"
61 #include "gstinfo.h"
62 #include "gsturi.h"
63 #include "gstregistry.h"
64 #include "gst.h"
65
66 #include "glib-compat-private.h"
67
68 #include <gobject/gvaluecollector.h>
69
70 GST_DEBUG_CATEGORY_STATIC (element_factory_debug);
71 #define GST_CAT_DEFAULT element_factory_debug
72
73 static void gst_element_factory_finalize (GObject * object);
74 static void gst_element_factory_cleanup (GstElementFactory * factory);
75
76 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
77
78 /* this is defined in gstelement.c */
79 extern GQuark __gst_elementclass_factory;
80 extern GQuark __gst_elementclass_skip_doc;
81
82 #define _do_init \
83 { \
84   GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY", \
85       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, \
86       "element factories keep information about installed elements"); \
87 }
88
89 G_DEFINE_TYPE_WITH_CODE (GstElementFactory, gst_element_factory,
90     GST_TYPE_PLUGIN_FEATURE, _do_init);
91
92 #ifdef TIZEN_PROFILE_TV
93 static GMutex pool_mutex;
94 static GList *pool = NULL;
95 #endif
96
97 static void
98 gst_element_factory_class_init (GstElementFactoryClass * klass)
99 {
100   GObjectClass *gobject_class = (GObjectClass *) klass;
101
102   gobject_class->finalize = gst_element_factory_finalize;
103 }
104
105 static void
106 gst_element_factory_init (GstElementFactory * factory)
107 {
108   factory->staticpadtemplates = NULL;
109   factory->numpadtemplates = 0;
110
111   factory->uri_type = GST_URI_UNKNOWN;
112   factory->uri_protocols = NULL;
113
114   factory->interfaces = NULL;
115 }
116
117 static void
118 gst_element_factory_finalize (GObject * object)
119 {
120   GstElementFactory *factory = GST_ELEMENT_FACTORY (object);
121
122   gst_element_factory_cleanup (factory);
123   G_OBJECT_CLASS (gst_element_factory_parent_class)->finalize (object);
124 }
125
126 /**
127  * gst_element_factory_find:
128  * @name: name of factory to find
129  *
130  * Search for an element factory of the given name. Refs the returned
131  * element factory; caller is responsible for unreffing.
132  *
133  * Returns: (transfer full) (nullable): #GstElementFactory if found,
134  * %NULL otherwise
135  */
136 GstElementFactory *
137 gst_element_factory_find (const gchar * name)
138 {
139   GstPluginFeature *feature;
140
141   g_return_val_if_fail (name != NULL, NULL);
142
143   feature = gst_registry_find_feature (gst_registry_get (), name,
144       GST_TYPE_ELEMENT_FACTORY);
145   if (feature)
146     return GST_ELEMENT_FACTORY (feature);
147
148   /* this isn't an error, for instance when you query if an element factory is
149    * present */
150   GST_LOG ("no such element factory \"%s\"", name);
151   return NULL;
152 }
153
154 static void
155 gst_element_factory_cleanup (GstElementFactory * factory)
156 {
157   GList *item;
158
159   if (factory->metadata) {
160     gst_structure_free ((GstStructure *) factory->metadata);
161     factory->metadata = NULL;
162   }
163   if (factory->type) {
164     factory->type = G_TYPE_INVALID;
165   }
166
167   for (item = factory->staticpadtemplates; item; item = item->next) {
168     GstStaticPadTemplate *templ = item->data;
169
170     gst_static_caps_cleanup (&templ->static_caps);
171     g_slice_free (GstStaticPadTemplate, templ);
172   }
173   g_list_free (factory->staticpadtemplates);
174   factory->staticpadtemplates = NULL;
175   factory->numpadtemplates = 0;
176   factory->uri_type = GST_URI_UNKNOWN;
177   if (factory->uri_protocols) {
178     g_strfreev (factory->uri_protocols);
179     factory->uri_protocols = NULL;
180   }
181
182   g_list_free (factory->interfaces);
183   factory->interfaces = NULL;
184 }
185
186 #define CHECK_METADATA_FIELD(klass, name, key)                                 \
187   G_STMT_START {                                                               \
188     const gchar *metafield = gst_element_class_get_metadata (klass, key);      \
189     if (G_UNLIKELY (metafield == NULL || *metafield == '\0')) {                \
190       g_warning ("Element factory metadata for '%s' has no valid %s field", name, key);    \
191       goto detailserror;                                                       \
192     } \
193   } G_STMT_END;
194
195 /**
196  * gst_element_register:
197  * @plugin: (allow-none): #GstPlugin to register the element with, or %NULL for
198  *     a static element.
199  * @name: name of elements of this type
200  * @rank: rank of element (higher rank means more importance when autoplugging)
201  * @type: GType of element to register
202  *
203  * Create a new elementfactory capable of instantiating objects of the
204  * @type and add the factory to @plugin.
205  *
206  * Returns: %TRUE, if the registering succeeded, %FALSE on error
207  */
208 gboolean
209 gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
210     GType type)
211 {
212   GstPluginFeature *existing_feature;
213   GstRegistry *registry;
214   GstElementFactory *factory;
215   GType *interfaces;
216   guint n_interfaces, i;
217   GstElementClass *klass;
218   GList *item;
219
220   g_return_val_if_fail (name != NULL, FALSE);
221   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
222
223   registry = gst_registry_get ();
224
225   /* check if feature already exists, if it exists there is no need to update it
226    * when the registry is getting updated, outdated plugins and all their
227    * features are removed and readded.
228    */
229   existing_feature = gst_registry_lookup_feature (registry, name);
230   if (existing_feature && existing_feature->plugin == plugin) {
231     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
232         existing_feature, name);
233     factory = GST_ELEMENT_FACTORY_CAST (existing_feature);
234     factory->type = type;
235     existing_feature->loaded = TRUE;
236     g_type_set_qdata (type, __gst_elementclass_factory, factory);
237     gst_object_unref (existing_feature);
238     return TRUE;
239   } else if (existing_feature) {
240     gst_object_unref (existing_feature);
241   }
242
243   factory = g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL);
244   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
245   GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
246       g_type_name (type));
247
248   /* provide info needed during class structure setup */
249   g_type_set_qdata (type, __gst_elementclass_factory, factory);
250   klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
251
252   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
253   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
254   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
255   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);
256
257   factory->type = type;
258   factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
259
260   for (item = klass->padtemplates; item; item = item->next) {
261     GstPadTemplate *templ = item->data;
262     GstStaticPadTemplate *newt;
263     gchar *caps_string = gst_caps_to_string (templ->caps);
264
265     newt = g_slice_new (GstStaticPadTemplate);
266     newt->name_template = g_intern_string (templ->name_template);
267     newt->direction = templ->direction;
268     newt->presence = templ->presence;
269     newt->static_caps.caps = NULL;
270     newt->static_caps.string = g_intern_string (caps_string);
271     factory->staticpadtemplates =
272         g_list_append (factory->staticpadtemplates, newt);
273
274     g_free (caps_string);
275   }
276   factory->numpadtemplates = klass->numpadtemplates;
277
278   /* special stuff for URI handling */
279   if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
280     GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
281         g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
282
283     if (!iface || !iface->get_type || !iface->get_protocols)
284       goto urierror;
285     if (iface->get_type)
286       factory->uri_type = iface->get_type (factory->type);
287     if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
288       goto urierror;
289     if (iface->get_protocols) {
290       const gchar *const *protocols;
291
292       protocols = iface->get_protocols (factory->type);
293       factory->uri_protocols = g_strdupv ((gchar **) protocols);
294     }
295     if (!factory->uri_protocols)
296       goto urierror;
297   }
298
299   interfaces = g_type_interfaces (type, &n_interfaces);
300   for (i = 0; i < n_interfaces; i++) {
301     __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
302   }
303   g_free (interfaces);
304
305   if (plugin && plugin->desc.name) {
306     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
307     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
308     g_object_add_weak_pointer ((GObject *) plugin,
309         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
310   } else {
311     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
312     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
313   }
314   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
315   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
316
317   gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
318
319   return TRUE;
320
321   /* ERRORS */
322 urierror:
323   {
324     GST_WARNING_OBJECT (factory, "error with uri handler!");
325     gst_element_factory_cleanup (factory);
326     return FALSE;
327   }
328
329 detailserror:
330   {
331     gst_element_factory_cleanup (factory);
332     return FALSE;
333   }
334 }
335
336 /**
337  * gst_element_type_set_skip_documentation:
338  * @type: a #GType of element
339  *
340  * Marks @type as "documentation should be skipped".
341  * Can be useful for dynamically registered element to be excluded from
342  * plugin documentation system.
343  *
344  * Example:
345  * ```c
346  * GType my_type;
347  * GTypeInfo my_type_info;
348  *
349  * // Fill "my_type_info"
350  * ...
351  *
352  * my_type = g_type_register_static (GST_TYPE_MY_ELEMENT, "my-type-name",
353  *    &my_type_info, 0);
354  * gst_element_type_set_skip_documentation (my_type);
355  * gst_element_register (plugin, "my-plugin-feature-name", rank, my_type);
356  * ```
357  *
358  * Since: 1.20
359  */
360 void
361 gst_element_type_set_skip_documentation (GType type)
362 {
363   g_return_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT));
364
365   g_type_set_qdata (type, __gst_elementclass_skip_doc, GINT_TO_POINTER (1));
366 }
367
368 /**
369  * gst_element_factory_get_skip_documentation:
370  * @factory: a #GstElementFactory to query documentation skip
371  *
372  * Queries whether registered element managed by @factory needs to
373  * be excluded from documentation system or not.
374  *
375  * Returns: %TRUE if documentation should be skipped
376  *
377  * Since: 1.20
378  */
379 gboolean
380 gst_element_factory_get_skip_documentation (GstElementFactory * factory)
381 {
382   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), TRUE);
383
384   if (g_type_get_qdata (factory->type, __gst_elementclass_skip_doc))
385     return TRUE;
386
387   return FALSE;
388 }
389
390 static gboolean
391 gst_element_factory_property_valist_to_array (const gchar * first,
392     va_list properties, GType object_type, guint * n, const gchar ** names[],
393     GValue ** values)
394 {
395   GObjectClass *class;
396   const gchar *name;
397   guint n_params = 0;
398   guint n_params_alloc = 16;
399   GValue *values_array;
400   const gchar **names_array;
401
402   if (!first)
403     return FALSE;
404
405   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), FALSE);
406
407   class = g_type_class_ref (object_type);
408   if (!class)
409     return FALSE;
410
411   name = first;
412   names_array = g_new0 (const gchar *, n_params_alloc);
413   values_array = g_new0 (GValue, n_params_alloc);
414
415   do {
416     gchar *error = NULL;
417     GParamSpec *pspec;
418
419     pspec = g_object_class_find_property (class, name);
420     if (!pspec)
421       goto cleanup;
422
423     if (G_UNLIKELY (n_params == n_params_alloc)) {
424       n_params_alloc *= 2u;
425       names_array =
426           g_realloc (names_array, sizeof (const gchar *) * n_params_alloc);
427       values_array = g_realloc (values_array, sizeof (GValue) * n_params_alloc);
428       memset (&values_array[n_params], 0,
429           sizeof (GValue) * (n_params_alloc - n_params));
430     }
431
432     names_array[n_params] = name;
433
434     G_VALUE_COLLECT_INIT (&values_array[n_params], pspec->value_type,
435         properties, 0, &error);
436
437     if (error) {
438       g_critical ("%s", error);
439       g_free (error);
440       goto cleanup;
441     }
442
443     ++n_params;
444     name = va_arg (properties, const gchar *);
445   } while (name);
446
447   *n = n_params;
448   *names = names_array;
449   *values = values_array;
450   g_type_class_unref (class);
451   return TRUE;
452
453 cleanup:
454   g_free (names_array);
455   g_free (values_array);
456   g_type_class_unref (class);
457   return FALSE;
458 }
459
460 /**
461  * gst_element_factory_create_with_properties:
462  * @factory: factory to instantiate
463  * @n: count of properties
464  * @names: (nullable) (array length=n): array of properties names
465  * @values: (nullable) (array length=n): array of associated properties values
466  *
467  * Create a new element of the type defined by the given elementfactory.
468  * The supplied list of properties, will be passed at object construction.
469  *
470  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
471  *     if the element couldn't be created
472  *
473  * Since: 1.20
474  */
475 GstElement *
476 gst_element_factory_create_with_properties (GstElementFactory * factory,
477     guint n, const gchar * names[], const GValue values[])
478 {
479   GstElement *element;
480   GstElementClass *oclass;
481   GstElementFactory *newfactory;
482
483   g_return_val_if_fail (factory != NULL, NULL);
484
485   newfactory =
486       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
487           (factory)));
488
489   if (newfactory == NULL)
490     goto load_failed;
491
492   factory = newfactory;
493
494   GST_INFO ("creating element \"%s\"", GST_OBJECT_NAME (factory));
495
496   if (factory->type == G_TYPE_INVALID)
497     goto no_type;
498
499   element = (GstElement *) g_object_new_with_properties (factory->type, n,
500       names, values);
501   if (G_UNLIKELY (!element)) {
502     gst_object_unref (factory);
503     g_return_val_if_fail (element != NULL, NULL);
504   }
505
506   /* fill in the pointer to the factory in the element class. The
507    * class will not be unreffed currently.
508    * Be thread safe as there might be 2 threads creating the first instance of
509    * an element at the same moment
510    */
511   oclass = GST_ELEMENT_GET_CLASS (element);
512   if (!g_atomic_pointer_compare_and_exchange (&oclass->elementfactory,
513           (GstElementFactory *) NULL, factory))
514     gst_object_unref (factory);
515   else
516     /* This ref will never be dropped as the class is never destroyed */
517     GST_OBJECT_FLAG_SET (factory, GST_OBJECT_FLAG_MAY_BE_LEAKED);
518
519   if (!g_object_is_floating ((GObject *) element)) {
520     /* The reference we receive here should be floating, but we can't force
521      * it at our level. Simply raise a critical to make the issue obvious to bindings
522      * users / developers */
523     g_critical ("The created element should be floating, "
524         "this is probably caused by faulty bindings");
525   }
526
527
528   GST_DEBUG ("created element \"%s\"", GST_OBJECT_NAME (factory));
529
530   return element;
531
532   /* ERRORS */
533 load_failed:
534   {
535     GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
536     return NULL;
537   }
538 no_type:
539   {
540     GST_WARNING_OBJECT (factory, "factory has no type");
541     gst_object_unref (factory);
542     return NULL;
543   }
544 }
545
546 /**
547  * gst_element_factory_create_valist:
548  * @factory: factory to instantiate
549  * @first: (nullable): name of the first property
550  * @properties: (nullable): list of properties
551  *
552  * Create a new element of the type defined by the given elementfactory.
553  * The supplied list of properties, will be passed at object construction.
554  *
555  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
556  *     if the element couldn't be created
557  *
558  * Since: 1.20
559  */
560 GstElement *
561 gst_element_factory_create_valist (GstElementFactory * factory,
562     const gchar * first, va_list properties)
563 {
564   GstElementFactory *newfactory;
565   GstElement *element;
566   const gchar **names = NULL;
567   GValue *values = NULL;
568   guint n = 0;
569
570   g_return_val_if_fail (factory != NULL, NULL);
571
572   newfactory =
573       GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
574           (factory)));
575
576   if (newfactory == NULL)
577     goto load_failed;
578
579   factory = newfactory;
580
581   if (factory->type == G_TYPE_INVALID)
582     goto no_type;
583
584   if (!first) {
585     element =
586         gst_element_factory_create_with_properties (factory, 0, NULL, NULL);
587     goto out;
588   }
589
590   if (!gst_element_factory_property_valist_to_array (first, properties,
591           factory->type, &n, &names, &values)) {
592     GST_ERROR_OBJECT (factory, "property parsing failed");
593     element = NULL;
594     goto out;
595   }
596
597   element = gst_element_factory_create_with_properties (factory, n, names,
598       values);
599
600   g_free (names);
601   while (n--)
602     g_value_unset (&values[n]);
603   g_free (values);
604
605 out:
606   gst_object_unref (factory);
607   return element;
608
609   /* ERRORS */
610 load_failed:
611   {
612     GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
613     return NULL;
614   }
615 no_type:
616   {
617     GST_WARNING_OBJECT (factory, "factory has no type");
618     gst_object_unref (factory);
619     return NULL;
620   }
621 }
622
623 /**
624  * gst_element_factory_create_full:
625  * @factory: factory to instantiate
626  * @first: (nullable): name of the first property
627  * @...: (nullable): %NULL terminated list of properties
628  *
629  * Create a new element of the type defined by the given elementfactory.
630  * The supplied list of properties, will be passed at object construction.
631  *
632  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
633  *     if the element couldn't be created
634  *
635  * Since: 1.20
636  */
637 GstElement *
638 gst_element_factory_create_full (GstElementFactory * factory,
639     const gchar * first, ...)
640 {
641   GstElement *element;
642   va_list properties;
643
644   va_start (properties, first);
645   element = gst_element_factory_create_valist (factory, first, properties);
646   va_end (properties);
647
648   return element;
649 }
650
651 /**
652  * gst_element_factory_create:
653  * @factory: factory to instantiate
654  * @name: (nullable): name of new element, or %NULL to automatically create
655  *    a unique name
656  *
657  * Create a new element of the type defined by the given elementfactory.
658  * It will be given the name supplied, since all elements require a name as
659  * their first argument.
660  *
661  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
662  *     if the element couldn't be created
663  */
664 GstElement *
665 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
666 {
667   if (name)
668     return gst_element_factory_create_full (factory, "name", name, NULL);
669   else
670     return gst_element_factory_create_with_properties (factory, 0, NULL, NULL);
671 }
672
673 /**
674  * gst_element_factory_make_with_properties:
675  * @factoryname: a named factory to instantiate
676  * @n: count of properties
677  * @names: (nullable) (array length=n): array of properties names
678  * @values: (nullable) (array length=n): array of associated properties values
679  *
680  * Create a new element of the type defined by the given elementfactory.
681  * The supplied list of properties, will be passed at object construction.
682  *
683  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
684  *     if the element couldn't be created
685  *
686  * Since: 1.20
687  */
688 GstElement *
689 gst_element_factory_make_with_properties (const gchar * factoryname,
690     guint n, const gchar * names[], const GValue values[])
691 {
692   GstElementFactory *factory;
693   GstElement *element;
694
695   g_return_val_if_fail (factoryname != NULL, NULL);
696   g_return_val_if_fail (gst_is_initialized (), NULL);
697
698   GST_LOG ("gstelementfactory: make \"%s\"", factoryname);
699
700   factory = gst_element_factory_find (factoryname);
701   if (factory == NULL)
702     goto no_factory;
703
704   GST_LOG_OBJECT (factory, "found factory %p", factory);
705   element = gst_element_factory_create_with_properties (factory, n, names,
706       values);
707   if (element == NULL)
708     goto create_failed;
709
710   gst_object_unref (factory);
711
712   return element;
713
714   /* ERRORS */
715 no_factory:
716   {
717     GST_WARNING ("no such element factory \"%s\"!", factoryname);
718     return NULL;
719   }
720 create_failed:
721   {
722     GST_INFO_OBJECT (factory, "couldn't create instance!");
723     gst_object_unref (factory);
724     return NULL;
725   }
726 }
727
728 /**
729  * gst_element_factory_make_valist:
730  * @factoryname: a named factory to instantiate
731  * @first: (nullable): name of first property
732  * @properties: (nullable): list of properties
733  *
734  * Create a new element of the type defined by the given element factory.
735  * The supplied list of properties, will be passed at object construction.
736  *
737  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
738  * if unable to create element
739  *
740  * Since: 1.20
741  */
742 GstElement *
743 gst_element_factory_make_valist (const gchar * factoryname,
744     const gchar * first, va_list properties)
745 {
746   GstElementFactory *factory;
747   GstElement *element;
748
749   g_return_val_if_fail (factoryname != NULL, NULL);
750   g_return_val_if_fail (gst_is_initialized (), NULL);
751
752   GST_LOG ("gstelementfactory: make \"%s\"", factoryname);
753
754   factory = gst_element_factory_find (factoryname);
755   if (factory == NULL)
756     goto no_factory;
757
758   GST_LOG_OBJECT (factory, "found factory %p", factory);
759   element = gst_element_factory_create_valist (factory, first, properties);
760   if (element == NULL)
761     goto create_failed;
762
763   gst_object_unref (factory);
764
765   return element;
766
767   /* ERRORS */
768 no_factory:
769   {
770     GST_WARNING ("no such element factory \"%s\"!", factoryname);
771     return NULL;
772   }
773 create_failed:
774   {
775     GST_INFO_OBJECT (factory, "couldn't create instance!");
776     gst_object_unref (factory);
777     return NULL;
778   }
779 }
780
781 /**
782  * gst_element_factory_make_full:
783  * @factoryname: a named factory to instantiate
784  * @first: (nullable): name of first property
785  * @...: (nullable): %NULL terminated list of properties
786  *
787  * Create a new element of the type defined by the given element factory.
788  * The supplied list of properties, will be passed at object construction.
789  *
790  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
791  * if unable to create element
792  *
793  * Since: 1.20
794  */
795 GstElement *
796 gst_element_factory_make_full (const gchar * factoryname,
797     const gchar * first, ...)
798 {
799   GstElement *element;
800   va_list properties;
801
802   va_start (properties, first);
803
804   element = gst_element_factory_make_valist (factoryname, first, properties);
805
806   va_end (properties);
807   return element;
808 }
809
810 /**
811  * gst_element_factory_make:
812  * @factoryname: a named factory to instantiate
813  * @name: (nullable): name of new element, or %NULL to automatically create
814  *    a unique name
815  *
816  * Create a new element of the type defined by the given element factory.
817  * If name is %NULL, then the element will receive a guaranteed unique name,
818  * consisting of the element factory name and a number.
819  * If name is given, it will be given the name supplied.
820  *
821  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
822  * if unable to create element
823  */
824 GstElement *
825 gst_element_factory_make (const gchar * factoryname, const gchar * name)
826 {
827   if (name)
828     return gst_element_factory_make_full (factoryname, "name", name, NULL);
829   else
830     return gst_element_factory_make_with_properties (factoryname, 0, NULL,
831         NULL);
832 }
833
834 void
835 __gst_element_factory_add_static_pad_template (GstElementFactory * factory,
836     GstStaticPadTemplate * templ)
837 {
838   g_return_if_fail (factory != NULL);
839   g_return_if_fail (templ != NULL);
840
841   factory->staticpadtemplates =
842       g_list_append (factory->staticpadtemplates, templ);
843   factory->numpadtemplates++;
844 }
845
846 /**
847  * gst_element_factory_get_element_type:
848  * @factory: factory to get managed #GType from
849  *
850  * Get the #GType for elements managed by this factory. The type can
851  * only be retrieved if the element factory is loaded, which can be
852  * assured with gst_plugin_feature_load().
853  *
854  * Returns: the #GType for elements managed by this factory or 0 if
855  * the factory is not loaded.
856  */
857 GType
858 gst_element_factory_get_element_type (GstElementFactory * factory)
859 {
860   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
861
862   return factory->type;
863 }
864
865 /**
866  * gst_element_factory_get_metadata:
867  * @factory: a #GstElementFactory
868  * @key: a key
869  *
870  * Get the metadata on @factory with @key.
871  *
872  * Returns: (nullable): the metadata with @key on @factory or %NULL
873  * when there was no metadata with the given @key.
874  */
875 const gchar *
876 gst_element_factory_get_metadata (GstElementFactory * factory,
877     const gchar * key)
878 {
879   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
880
881   return gst_structure_get_string ((GstStructure *) factory->metadata, key);
882 }
883
884 /**
885  * gst_element_factory_get_metadata_keys:
886  * @factory: a #GstElementFactory
887  *
888  * Get the available keys for the metadata on @factory.
889  *
890  * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
891  * a %NULL-terminated array of key strings, or %NULL when there is no
892  * metadata. Free with g_strfreev() when no longer needed.
893  */
894 gchar **
895 gst_element_factory_get_metadata_keys (GstElementFactory * factory)
896 {
897   GstStructure *metadata;
898   gchar **arr;
899   gint i, num;
900
901   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
902
903   metadata = (GstStructure *) factory->metadata;
904   if (metadata == NULL)
905     return NULL;
906
907   num = gst_structure_n_fields (metadata);
908   if (num == 0)
909     return NULL;
910
911   arr = g_new (gchar *, num + 1);
912   for (i = 0; i < num; ++i) {
913     arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
914   }
915   arr[i] = NULL;
916   return arr;
917 }
918
919 /**
920  * gst_element_factory_get_num_pad_templates:
921  * @factory: a #GstElementFactory
922  *
923  * Gets the number of pad_templates in this factory.
924  *
925  * Returns: the number of pad_templates
926  */
927 guint
928 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
929 {
930   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
931
932   return factory->numpadtemplates;
933 }
934
935 /**
936  * __gst_element_factory_add_interface:
937  * @elementfactory: The elementfactory to add the interface to
938  * @interfacename: Name of the interface
939  *
940  * Adds the given interfacename to the list of implemented interfaces of the
941  * element.
942  */
943 void
944 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
945     const gchar * interfacename)
946 {
947   g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
948   g_return_if_fail (interfacename != NULL);
949   g_return_if_fail (interfacename[0] != '\0');  /* no empty string */
950
951   elementfactory->interfaces =
952       g_list_prepend (elementfactory->interfaces,
953       (gpointer) g_intern_string (interfacename));
954 }
955
956 /**
957  * gst_element_factory_get_static_pad_templates:
958  * @factory: a #GstElementFactory
959  *
960  * Gets the #GList of #GstStaticPadTemplate for this factory.
961  *
962  * Returns: (transfer none) (element-type Gst.StaticPadTemplate): the
963  *     static pad templates
964  */
965 const GList *
966 gst_element_factory_get_static_pad_templates (GstElementFactory * factory)
967 {
968   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
969
970   return factory->staticpadtemplates;
971 }
972
973 /**
974  * gst_element_factory_get_uri_type:
975  * @factory: a #GstElementFactory
976  *
977  * Gets the type of URIs the element supports or #GST_URI_UNKNOWN if none.
978  *
979  * Returns: type of URIs this element supports
980  */
981 GstURIType
982 gst_element_factory_get_uri_type (GstElementFactory * factory)
983 {
984   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
985
986   return factory->uri_type;
987 }
988
989 /**
990  * gst_element_factory_get_uri_protocols:
991  * @factory: a #GstElementFactory
992  *
993  * Gets a %NULL-terminated array of protocols this element supports or %NULL if
994  * no protocols are supported. You may not change the contents of the returned
995  * array, as it is still owned by the element factory. Use g_strdupv() to
996  * make a copy of the protocol string array if you need to.
997  *
998  * Returns: (transfer none) (array zero-terminated=1): the supported protocols
999  *     or %NULL
1000  */
1001 const gchar *const *
1002 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
1003 {
1004   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
1005
1006   return (const gchar * const *) factory->uri_protocols;
1007 }
1008
1009 /**
1010  * gst_element_factory_has_interface:
1011  * @factory: a #GstElementFactory
1012  * @interfacename: an interface name
1013  *
1014  * Check if @factory implements the interface with name @interfacename.
1015  *
1016  * Returns: %TRUE when @factory implement the interface.
1017  */
1018 gboolean
1019 gst_element_factory_has_interface (GstElementFactory * factory,
1020     const gchar * interfacename)
1021 {
1022   GList *walk;
1023
1024   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), FALSE);
1025
1026   for (walk = factory->interfaces; walk; walk = g_list_next (walk)) {
1027     gchar *iname = (gchar *) walk->data;
1028
1029     if (!strcmp (iname, interfacename))
1030       return TRUE;
1031   }
1032   return FALSE;
1033 }
1034
1035
1036 typedef struct
1037 {
1038   GstElementFactoryListType type;
1039   GstRank minrank;
1040 } FilterData;
1041
1042
1043 /**
1044  * gst_element_factory_list_is_type:
1045  * @factory: a #GstElementFactory
1046  * @type: a #GstElementFactoryListType
1047  *
1048  * Check if @factory is of the given types.
1049  *
1050  * Returns: %TRUE if @factory is of @type.
1051  */
1052 gboolean
1053 gst_element_factory_list_is_type (GstElementFactory * factory,
1054     GstElementFactoryListType type)
1055 {
1056   gboolean res = FALSE;
1057   const gchar *klass;
1058
1059   klass =
1060       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1061
1062   if (klass == NULL) {
1063     GST_ERROR_OBJECT (factory, "element factory is missing klass identifiers");
1064     return res;
1065   }
1066
1067   /* Filter by element type first, as soon as it matches
1068    * one type, we skip all other tests */
1069   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_SINK))
1070     res = (strstr (klass, "Sink") != NULL);
1071
1072   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_SRC))
1073     res = (strstr (klass, "Source") != NULL);
1074
1075   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DECODER))
1076     res = (strstr (klass, "Decoder") != NULL);
1077
1078   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_ENCODER))
1079     res = (strstr (klass, "Encoder") != NULL);
1080
1081   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_MUXER))
1082     res = (strstr (klass, "Muxer") != NULL);
1083
1084   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DEMUXER))
1085     res = (strstr (klass, "Demux") != NULL);
1086
1087   /* FIXME : We're actually parsing two Classes here... */
1088   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_PARSER))
1089     res = ((strstr (klass, "Parser") != NULL)
1090         && (strstr (klass, "Codec") != NULL));
1091
1092   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DEPAYLOADER))
1093     res = (strstr (klass, "Depayloader") != NULL);
1094
1095   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_PAYLOADER))
1096     res = (strstr (klass, "Payloader") != NULL);
1097
1098   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_FORMATTER))
1099     res = (strstr (klass, "Formatter") != NULL);
1100
1101   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DECRYPTOR))
1102     res = (strstr (klass, "Decryptor") != NULL);
1103
1104   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_ENCRYPTOR))
1105     res = (strstr (klass, "Encryptor") != NULL);
1106
1107   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_HARDWARE))
1108     res = (strstr (klass, "Hardware") != NULL);
1109
1110   /* Filter by media type now, we only test if it
1111    * matched any of the types above or only checking the media
1112    * type was requested. */
1113   if ((res || !(type & (GST_ELEMENT_FACTORY_TYPE_MAX_ELEMENTS - 1)))
1114       && (type & (GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO |
1115               GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
1116               GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE |
1117               GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE |
1118               GST_ELEMENT_FACTORY_TYPE_MEDIA_METADATA)))
1119     res = ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO)
1120         && (strstr (klass, "Audio") != NULL))
1121         || ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO)
1122         && (strstr (klass, "Video") != NULL))
1123         || ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE)
1124         && (strstr (klass, "Image") != NULL)) ||
1125         ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE)
1126         && (strstr (klass, "Subtitle") != NULL)) ||
1127         ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_METADATA)
1128         && (strstr (klass, "Metadata") != NULL));
1129
1130   return res;
1131 }
1132
1133 static gboolean
1134 element_filter (GstPluginFeature * feature, FilterData * data)
1135 {
1136   gboolean res;
1137
1138   /* we only care about element factories */
1139   if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
1140     return FALSE;
1141
1142   res = (gst_plugin_feature_get_rank (feature) >= data->minrank) &&
1143       gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (feature),
1144       data->type);
1145
1146   return res;
1147 }
1148
1149 /**
1150  * gst_element_factory_list_get_elements:
1151  * @type: a #GstElementFactoryListType
1152  * @minrank: Minimum rank
1153  *
1154  * Get a list of factories that match the given @type. Only elements
1155  * with a rank greater or equal to @minrank will be returned.
1156  * The list of factories is returned by decreasing rank.
1157  *
1158  * Returns: (transfer full) (element-type Gst.ElementFactory): a #GList of
1159  *     #GstElementFactory elements. Use gst_plugin_feature_list_free() after
1160  *     usage.
1161  */
1162 GList *
1163 gst_element_factory_list_get_elements (GstElementFactoryListType type,
1164     GstRank minrank)
1165 {
1166   GList *result;
1167   FilterData data;
1168
1169   /* prepare type */
1170   data.type = type;
1171   data.minrank = minrank;
1172
1173   /* get the feature list using the filter */
1174   result = gst_registry_feature_filter (gst_registry_get (),
1175       (GstPluginFeatureFilter) element_filter, FALSE, &data);
1176
1177   /* sort on rank and name */
1178   result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
1179
1180   return result;
1181 }
1182
1183 /**
1184  * gst_element_factory_list_filter:
1185  * @list: (transfer none) (element-type Gst.ElementFactory): a #GList of
1186  *     #GstElementFactory to filter
1187  * @caps: a #GstCaps
1188  * @direction: a #GstPadDirection to filter on
1189  * @subsetonly: whether to filter on caps subsets or not.
1190  *
1191  * Filter out all the elementfactories in @list that can handle @caps in
1192  * the given direction.
1193  *
1194  * If @subsetonly is %TRUE, then only the elements whose pads templates
1195  * are a complete superset of @caps will be returned. Else any element
1196  * whose pad templates caps can intersect with @caps will be returned.
1197  *
1198  * Returns: (transfer full) (element-type Gst.ElementFactory): a #GList of
1199  *     #GstElementFactory elements that match the given requisites.
1200  *     Use #gst_plugin_feature_list_free after usage.
1201  */
1202 GList *
1203 gst_element_factory_list_filter (GList * list,
1204     const GstCaps * caps, GstPadDirection direction, gboolean subsetonly)
1205 {
1206   GQueue results = G_QUEUE_INIT;
1207
1208   GST_DEBUG ("finding factories");
1209
1210   /* loop over all the factories */
1211   for (; list; list = list->next) {
1212     GstElementFactory *factory;
1213     const GList *templates;
1214     GList *walk;
1215
1216     factory = (GstElementFactory *) list->data;
1217
1218     GST_DEBUG ("Trying %s",
1219         gst_plugin_feature_get_name ((GstPluginFeature *) factory));
1220
1221     /* get the templates from the element factory */
1222     templates = gst_element_factory_get_static_pad_templates (factory);
1223     for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
1224       GstStaticPadTemplate *templ = walk->data;
1225
1226       /* we only care about the sink templates */
1227       if (templ->direction == direction) {
1228         GstCaps *tmpl_caps;
1229
1230         /* try to intersect the caps with the caps of the template */
1231         tmpl_caps = gst_static_caps_get (&templ->static_caps);
1232
1233         /* FIXME, intersect is not the right method, we ideally want to check
1234          * for a subset here */
1235
1236         /* check if the intersection is empty */
1237         if ((subsetonly && gst_caps_is_subset (caps, tmpl_caps)) ||
1238             (!subsetonly && gst_caps_can_intersect (caps, tmpl_caps))) {
1239           /* non empty intersection, we can use this element */
1240           g_queue_push_tail (&results, gst_object_ref (factory));
1241           gst_caps_unref (tmpl_caps);
1242           break;
1243         }
1244         gst_caps_unref (tmpl_caps);
1245       }
1246     }
1247   }
1248   return results.head;
1249 }
1250
1251 #ifdef TIZEN_PROFILE_TV
1252 /**
1253  * gst_element_factory_pool_push:
1254  *
1255  * Pushes the element to the pool. This function increase the refcount of @element.
1256  * And the pool will take a reference forever.
1257  * MT safe.
1258  */
1259 static gboolean
1260 gst_element_factory_pool_push (GstElement * element)
1261 {
1262   gboolean ret = FALSE;
1263   g_return_val_if_fail (element != NULL, FALSE);
1264   g_return_val_if_fail (GST_IS_ELEMENT (element) == TRUE, FALSE);
1265   g_return_val_if_fail (GST_IS_BIN (element) == FALSE, FALSE);
1266   g_return_val_if_fail (GST_IS_PIPELINE (element) == FALSE, FALSE);
1267
1268   g_mutex_lock (&pool_mutex);
1269   if (!g_list_find (pool, (gconstpointer) element)) {
1270     gst_object_ref (element);
1271     pool = g_list_prepend (pool, element);
1272     ret = TRUE;
1273   }
1274   //GST_INFO("ret[ %d ], [ %s / %p ] - refcount[ %d ], state[ %d ]", ret, GST_ELEMENT_NAME(element), element, GST_OBJECT_REFCOUNT(element), GST_STATE (element));
1275   g_mutex_unlock (&pool_mutex);
1276   return ret;
1277 }
1278
1279 static gint
1280 is_available_element (gconstpointer data, gconstpointer factory_)
1281 {
1282   GstElement *element = GST_ELEMENT_CAST (data);
1283   const GstElementFactory *factory = (const GstElementFactory *) factory_;
1284   gboolean found = FALSE;
1285   if (gst_element_get_factory (element) == factory
1286       && GST_ELEMENT_PARENT (element) == NULL
1287       && GST_OBJECT_REFCOUNT (element) == 1
1288       && (GST_STATE (element) == GST_STATE_NULL
1289           || GST_STATE (element) == GST_STATE_VOID_PENDING)) {
1290     found = TRUE;
1291   }
1292   //GST_DEBUG("PARENT[ %p ], [ %s / %p ] - refcount[ %d ], state[ %d ]", GST_ELEMENT_PARENT(element), GST_ELEMENT_NAME(element), element, GST_OBJECT_REFCOUNT(element), GST_STATE (element));
1293
1294   if (found)
1295     return 0;
1296
1297   return -1;
1298 }
1299
1300 /**
1301  * gst_element_factory_pool_get:
1302  *
1303  * Get a element which is created by given @factoryname
1304  * You can search any element by this API before you use gst_element_factory_create.
1305  * Returns: (transfer full): a pointer to the #GstElement.
1306  *         This function guarantee that the refcount of returned #GstElement is 1,
1307  *         and it's state is NULL-STATE.
1308  *
1309  * MT safe.
1310  */
1311 static GstElement *
1312 gst_element_factory_pool_get (GstElementFactory * factory,
1313     const gchar * new_element_name)
1314 {
1315   GstElement *element = NULL;
1316   GList *found = NULL;
1317   g_return_val_if_fail (factory != NULL, NULL);
1318   g_return_val_if_fail (pool != NULL, NULL);
1319
1320   g_mutex_lock (&pool_mutex);
1321   found = g_list_find_custom (pool, factory, is_available_element);
1322   if (found) {
1323     element = GST_ELEMENT_CAST (found->data);
1324     gst_object_ref (element);
1325     if (new_element_name)
1326       gst_element_set_name (element, new_element_name);
1327   }
1328   g_mutex_unlock (&pool_mutex);
1329   return element;
1330 }
1331
1332 /* First, Try to get element from pool. */
1333 GstElement *
1334 gst_element_factory_get (GstElementFactory * factory, const gchar * name)
1335 {
1336   GstElement *element = NULL;
1337   g_return_val_if_fail (factory != NULL, NULL);
1338   element = gst_element_factory_pool_get (factory, name);
1339   if (!element) {
1340     element = gst_element_factory_create (factory, name);
1341     if (!element) {
1342       GST_ERROR ("[ %s / %p ],  new element name[ %s ]",
1343           GST_OBJECT_NAME (factory), factory, name);
1344       return NULL;
1345     }
1346     gst_element_factory_pool_push (element);
1347   } else {
1348     gst_set_family_id_to_child (element, 0);
1349   }
1350
1351   //GST_INFO("[ %s / %p ] - refcount[ %d ], state[ %d ]", GST_ELEMENT_NAME(element), element, GST_OBJECT_REFCOUNT(element), GST_STATE (element));
1352   return element;
1353 }
1354 #endif