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