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