• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
gst_element_factory_class_init(GstElementFactoryClass * klass)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
gst_element_factory_init(GstElementFactory * factory)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
gst_element_factory_finalize(GObject * object)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 *
gst_element_factory_find(const gchar * name)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
gst_element_factory_cleanup(GstElementFactory * factory)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
gst_element_register(GstPlugin * plugin,const gchar * name,guint rank,GType type)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
gst_element_type_set_skip_documentation(GType type)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
gst_element_factory_get_skip_documentation(GstElementFactory * factory)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
gst_element_factory_property_valist_to_array(const gchar * first,va_list properties,GType object_type,guint * n,const gchar ** names[],GValue ** values)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 *
gst_element_factory_create_with_properties(GstElementFactory * factory,guint n,const gchar * names[],const GValue values[])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 == 0)
492     goto no_type;
493 
494   element = (GstElement *) g_object_new_with_properties (factory->type, n,
495       names, values);
496 
497   if (G_UNLIKELY (element == NULL))
498     goto no_element;
499 
500   /* fill in the pointer to the factory in the element class. The
501    * class will not be unreffed currently.
502    * Be thread safe as there might be 2 threads creating the first instance of
503    * an element at the same moment
504    */
505   oclass = GST_ELEMENT_GET_CLASS (element);
506   if (!g_atomic_pointer_compare_and_exchange (&oclass->elementfactory,
507           (GstElementFactory *) NULL, factory))
508     gst_object_unref (factory);
509   else
510     /* This ref will never be dropped as the class is never destroyed */
511     GST_OBJECT_FLAG_SET (factory, GST_OBJECT_FLAG_MAY_BE_LEAKED);
512 
513   if (!g_object_is_floating ((GObject *) element)) {
514     /* The reference we receive here should be floating, but we can't force
515      * it at our level. Simply raise a critical to make the issue obvious to bindings
516      * users / developers */
517     g_critical ("The created element should be floating, "
518         "this is probably caused by faulty bindings");
519   }
520 
521 
522   GST_DEBUG ("created element \"%s\"", GST_OBJECT_NAME (factory));
523 
524   return element;
525 
526   /* ERRORS */
527 load_failed:
528   {
529     GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
530     return NULL;
531   }
532 no_type:
533   {
534     GST_WARNING_OBJECT (factory, "factory has no type");
535     gst_object_unref (factory);
536     return NULL;
537   }
538 no_element:
539   {
540     GST_WARNING_OBJECT (factory, "could not create element");
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 *
gst_element_factory_create_valist(GstElementFactory * factory,const gchar * first,va_list properties)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   g_return_val_if_fail (newfactory != NULL, NULL);
577   g_return_val_if_fail (newfactory->type != 0, NULL);
578 
579   factory = newfactory;
580 
581   if (!first) {
582     element =
583         gst_element_factory_create_with_properties (factory, 0, NULL, NULL);
584     goto out;
585   }
586 
587   if (!gst_element_factory_property_valist_to_array (first, properties,
588           factory->type, &n, &names, &values)) {
589     GST_ERROR_OBJECT (factory, "property parsing failed");
590     element = NULL;
591     goto out;
592   }
593 
594   element = gst_element_factory_create_with_properties (factory, n, names,
595       values);
596 
597   g_free (names);
598   while (n--)
599     g_value_unset (&values[n]);
600   g_free (values);
601 
602 out:
603   gst_object_unref (factory);
604   return element;
605 }
606 
607 /**
608  * gst_element_factory_create_full:
609  * @factory: factory to instantiate
610  * @first: (nullable): name of the first property
611  * @...: (nullable): %NULL terminated list of properties
612  *
613  * Create a new element of the type defined by the given elementfactory.
614  * The supplied list of properties, will be passed at object construction.
615  *
616  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
617  *     if the element couldn't be created
618  *
619  * Since: 1.20
620  */
621 GstElement *
gst_element_factory_create_full(GstElementFactory * factory,const gchar * first,...)622 gst_element_factory_create_full (GstElementFactory * factory,
623     const gchar * first, ...)
624 {
625   GstElement *element;
626   va_list properties;
627 
628   va_start (properties, first);
629   element = gst_element_factory_create_valist (factory, first, properties);
630   va_end (properties);
631 
632   return element;
633 }
634 
635 /**
636  * gst_element_factory_create:
637  * @factory: factory to instantiate
638  * @name: (nullable): name of new element, or %NULL to automatically create
639  *    a unique name
640  *
641  * Create a new element of the type defined by the given elementfactory.
642  * It will be given the name supplied, since all elements require a name as
643  * their first argument.
644  *
645  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
646  *     if the element couldn't be created
647  */
648 GstElement *
gst_element_factory_create(GstElementFactory * factory,const gchar * name)649 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
650 {
651   if (name)
652     return gst_element_factory_create_full (factory, "name", name, NULL);
653   else
654     return gst_element_factory_create_with_properties (factory, 0, NULL, NULL);
655 }
656 
657 /**
658  * gst_element_factory_make_with_properties:
659  * @factoryname: a named factory to instantiate
660  * @n: count of properties
661  * @names: (nullable) (array length=n): array of properties names
662  * @values: (nullable) (array length=n): array of associated properties values
663  *
664  * Create a new element of the type defined by the given elementfactory.
665  * The supplied list of properties, will be passed at object construction.
666  *
667  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
668  *     if the element couldn't be created
669  *
670  * Since: 1.20
671  */
672 GstElement *
gst_element_factory_make_with_properties(const gchar * factoryname,guint n,const gchar * names[],const GValue values[])673 gst_element_factory_make_with_properties (const gchar * factoryname,
674     guint n, const gchar * names[], const GValue values[])
675 {
676   GstElementFactory *factory;
677   GstElement *element;
678 
679   g_return_val_if_fail (factoryname != NULL, NULL);
680   g_return_val_if_fail (gst_is_initialized (), NULL);
681 
682   GST_LOG ("gstelementfactory: make \"%s\"", factoryname);
683 
684   factory = gst_element_factory_find (factoryname);
685   if (factory == NULL)
686     goto no_factory;
687 
688   GST_LOG_OBJECT (factory, "found factory %p", factory);
689   element = gst_element_factory_create_with_properties (factory, n, names,
690       values);
691   if (element == NULL)
692     goto create_failed;
693 
694   gst_object_unref (factory);
695 
696   return element;
697 
698   /* ERRORS */
699 no_factory:
700   {
701     GST_WARNING ("no such element factory \"%s\"!", factoryname);
702     return NULL;
703   }
704 create_failed:
705   {
706     GST_INFO_OBJECT (factory, "couldn't create instance!");
707     gst_object_unref (factory);
708     return NULL;
709   }
710 }
711 
712 /**
713  * gst_element_factory_make_valist:
714  * @factoryname: a named factory to instantiate
715  * @first: (nullable): name of first property
716  * @properties: (nullable): list of properties
717  *
718  * Create a new element of the type defined by the given element factory.
719  * The supplied list of properties, will be passed at object construction.
720  *
721  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
722  * if unable to create element
723  *
724  * Since: 1.20
725  */
726 GstElement *
gst_element_factory_make_valist(const gchar * factoryname,const gchar * first,va_list properties)727 gst_element_factory_make_valist (const gchar * factoryname,
728     const gchar * first, va_list properties)
729 {
730   GstElementFactory *factory;
731   GstElement *element;
732 
733   g_return_val_if_fail (factoryname != NULL, NULL);
734   g_return_val_if_fail (gst_is_initialized (), NULL);
735 
736   GST_LOG ("gstelementfactory: make \"%s\"", factoryname);
737 
738   factory = gst_element_factory_find (factoryname);
739   if (factory == NULL)
740     goto no_factory;
741 
742   GST_LOG_OBJECT (factory, "found factory %p", factory);
743   element = gst_element_factory_create_valist (factory, first, properties);
744   if (element == NULL)
745     goto create_failed;
746 
747   gst_object_unref (factory);
748 
749   return element;
750 
751   /* ERRORS */
752 no_factory:
753   {
754     GST_DEBUG ("no such element factory \"%s\"!", factoryname);
755     return NULL;
756   }
757 create_failed:
758   {
759     GST_INFO_OBJECT (factory, "couldn't create instance!");
760     gst_object_unref (factory);
761     return NULL;
762   }
763 }
764 
765 /**
766  * gst_element_factory_make_full:
767  * @factoryname: a named factory to instantiate
768  * @first: (nullable): name of first property
769  * @...: (nullable): %NULL terminated list of properties
770  *
771  * Create a new element of the type defined by the given element factory.
772  * The supplied list of properties, will be passed at object construction.
773  *
774  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
775  * if unable to create element
776  *
777  * Since: 1.20
778  */
779 GstElement *
gst_element_factory_make_full(const gchar * factoryname,const gchar * first,...)780 gst_element_factory_make_full (const gchar * factoryname,
781     const gchar * first, ...)
782 {
783   GstElement *element;
784   va_list properties;
785 
786   va_start (properties, first);
787 
788   element = gst_element_factory_make_valist (factoryname, first, properties);
789 
790   va_end (properties);
791   return element;
792 }
793 
794 /**
795  * gst_element_factory_make:
796  * @factoryname: a named factory to instantiate
797  * @name: (nullable): name of new element, or %NULL to automatically create
798  *    a unique name
799  *
800  * Create a new element of the type defined by the given element factory.
801  * If name is %NULL, then the element will receive a guaranteed unique name,
802  * consisting of the element factory name and a number.
803  * If name is given, it will be given the name supplied.
804  *
805  * Returns: (transfer floating) (nullable): new #GstElement or %NULL
806  * if unable to create element
807  */
808 GstElement *
gst_element_factory_make(const gchar * factoryname,const gchar * name)809 gst_element_factory_make (const gchar * factoryname, const gchar * name)
810 {
811   if (name)
812     return gst_element_factory_make_full (factoryname, "name", name, NULL);
813   else
814     return gst_element_factory_make_with_properties (factoryname, 0, NULL,
815         NULL);
816 }
817 
818 void
__gst_element_factory_add_static_pad_template(GstElementFactory * factory,GstStaticPadTemplate * templ)819 __gst_element_factory_add_static_pad_template (GstElementFactory * factory,
820     GstStaticPadTemplate * templ)
821 {
822   g_return_if_fail (factory != NULL);
823   g_return_if_fail (templ != NULL);
824 
825   factory->staticpadtemplates =
826       g_list_append (factory->staticpadtemplates, templ);
827   factory->numpadtemplates++;
828 }
829 
830 /**
831  * gst_element_factory_get_element_type:
832  * @factory: factory to get managed #GType from
833  *
834  * Get the #GType for elements managed by this factory. The type can
835  * only be retrieved if the element factory is loaded, which can be
836  * assured with gst_plugin_feature_load().
837  *
838  * Returns: the #GType for elements managed by this factory or 0 if
839  * the factory is not loaded.
840  */
841 GType
gst_element_factory_get_element_type(GstElementFactory * factory)842 gst_element_factory_get_element_type (GstElementFactory * factory)
843 {
844   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
845 
846   return factory->type;
847 }
848 
849 /**
850  * gst_element_factory_get_metadata:
851  * @factory: a #GstElementFactory
852  * @key: a key
853  *
854  * Get the metadata on @factory with @key.
855  *
856  * Returns: (nullable): the metadata with @key on @factory or %NULL
857  * when there was no metadata with the given @key.
858  */
859 const gchar *
gst_element_factory_get_metadata(GstElementFactory * factory,const gchar * key)860 gst_element_factory_get_metadata (GstElementFactory * factory,
861     const gchar * key)
862 {
863   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
864 
865   return gst_structure_get_string ((GstStructure *) factory->metadata, key);
866 }
867 
868 /**
869  * gst_element_factory_get_metadata_keys:
870  * @factory: a #GstElementFactory
871  *
872  * Get the available keys for the metadata on @factory.
873  *
874  * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
875  * a %NULL-terminated array of key strings, or %NULL when there is no
876  * metadata. Free with g_strfreev() when no longer needed.
877  */
878 gchar **
gst_element_factory_get_metadata_keys(GstElementFactory * factory)879 gst_element_factory_get_metadata_keys (GstElementFactory * factory)
880 {
881   GstStructure *metadata;
882   gchar **arr;
883   gint i, num;
884 
885   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
886 
887   metadata = (GstStructure *) factory->metadata;
888   if (metadata == NULL)
889     return NULL;
890 
891   num = gst_structure_n_fields (metadata);
892   if (num == 0)
893     return NULL;
894 
895   arr = g_new (gchar *, num + 1);
896   for (i = 0; i < num; ++i) {
897     arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
898   }
899   arr[i] = NULL;
900   return arr;
901 }
902 
903 /**
904  * gst_element_factory_get_num_pad_templates:
905  * @factory: a #GstElementFactory
906  *
907  * Gets the number of pad_templates in this factory.
908  *
909  * Returns: the number of pad_templates
910  */
911 guint
gst_element_factory_get_num_pad_templates(GstElementFactory * factory)912 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
913 {
914   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
915 
916   return factory->numpadtemplates;
917 }
918 
919 /**
920  * __gst_element_factory_add_interface:
921  * @elementfactory: The elementfactory to add the interface to
922  * @interfacename: Name of the interface
923  *
924  * Adds the given interfacename to the list of implemented interfaces of the
925  * element.
926  */
927 void
__gst_element_factory_add_interface(GstElementFactory * elementfactory,const gchar * interfacename)928 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
929     const gchar * interfacename)
930 {
931   g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
932   g_return_if_fail (interfacename != NULL);
933   g_return_if_fail (interfacename[0] != '\0');  /* no empty string */
934 
935   elementfactory->interfaces =
936       g_list_prepend (elementfactory->interfaces,
937       (gpointer) g_intern_string (interfacename));
938 }
939 
940 /**
941  * gst_element_factory_get_static_pad_templates:
942  * @factory: a #GstElementFactory
943  *
944  * Gets the #GList of #GstStaticPadTemplate for this factory.
945  *
946  * Returns: (transfer none) (element-type Gst.StaticPadTemplate): the
947  *     static pad templates
948  */
949 const GList *
gst_element_factory_get_static_pad_templates(GstElementFactory * factory)950 gst_element_factory_get_static_pad_templates (GstElementFactory * factory)
951 {
952   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
953 
954   return factory->staticpadtemplates;
955 }
956 
957 /**
958  * gst_element_factory_get_uri_type:
959  * @factory: a #GstElementFactory
960  *
961  * Gets the type of URIs the element supports or #GST_URI_UNKNOWN if none.
962  *
963  * Returns: type of URIs this element supports
964  */
965 GstURIType
gst_element_factory_get_uri_type(GstElementFactory * factory)966 gst_element_factory_get_uri_type (GstElementFactory * factory)
967 {
968   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
969 
970   return factory->uri_type;
971 }
972 
973 /**
974  * gst_element_factory_get_uri_protocols:
975  * @factory: a #GstElementFactory
976  *
977  * Gets a %NULL-terminated array of protocols this element supports or %NULL if
978  * no protocols are supported. You may not change the contents of the returned
979  * array, as it is still owned by the element factory. Use g_strdupv() to
980  * make a copy of the protocol string array if you need to.
981  *
982  * Returns: (transfer none) (array zero-terminated=1): the supported protocols
983  *     or %NULL
984  */
985 const gchar *const *
gst_element_factory_get_uri_protocols(GstElementFactory * factory)986 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
987 {
988   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
989 
990   return (const gchar * const *) factory->uri_protocols;
991 }
992 
993 /**
994  * gst_element_factory_has_interface:
995  * @factory: a #GstElementFactory
996  * @interfacename: an interface name
997  *
998  * Check if @factory implements the interface with name @interfacename.
999  *
1000  * Returns: %TRUE when @factory implement the interface.
1001  */
1002 gboolean
gst_element_factory_has_interface(GstElementFactory * factory,const gchar * interfacename)1003 gst_element_factory_has_interface (GstElementFactory * factory,
1004     const gchar * interfacename)
1005 {
1006   GList *walk;
1007 
1008   g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), FALSE);
1009 
1010   for (walk = factory->interfaces; walk; walk = g_list_next (walk)) {
1011     gchar *iname = (gchar *) walk->data;
1012 
1013     if (!strcmp (iname, interfacename))
1014       return TRUE;
1015   }
1016   return FALSE;
1017 }
1018 
1019 
1020 typedef struct
1021 {
1022   GstElementFactoryListType type;
1023   GstRank minrank;
1024 } FilterData;
1025 
1026 
1027 /**
1028  * gst_element_factory_list_is_type:
1029  * @factory: a #GstElementFactory
1030  * @type: a #GstElementFactoryListType
1031  *
1032  * Check if @factory is of the given types.
1033  *
1034  * Returns: %TRUE if @factory is of @type.
1035  */
1036 gboolean
gst_element_factory_list_is_type(GstElementFactory * factory,GstElementFactoryListType type)1037 gst_element_factory_list_is_type (GstElementFactory * factory,
1038     GstElementFactoryListType type)
1039 {
1040   gboolean res = FALSE;
1041   const gchar *klass;
1042 
1043   klass =
1044       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1045 
1046   if (klass == NULL) {
1047     GST_ERROR_OBJECT (factory, "element factory is missing klass identifiers");
1048     return res;
1049   }
1050 
1051   /* Filter by element type first, as soon as it matches
1052    * one type, we skip all other tests */
1053   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_SINK))
1054     res = (strstr (klass, "Sink") != NULL);
1055 
1056   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_SRC))
1057     res = (strstr (klass, "Source") != NULL);
1058 
1059   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DECODER))
1060     res = (strstr (klass, "Decoder") != NULL);
1061 
1062   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_ENCODER))
1063     res = (strstr (klass, "Encoder") != NULL);
1064 
1065   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_MUXER))
1066     res = (strstr (klass, "Muxer") != NULL);
1067 
1068   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DEMUXER))
1069     res = (strstr (klass, "Demux") != NULL);
1070 
1071   /* FIXME : We're actually parsing two Classes here... */
1072   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_PARSER))
1073     res = ((strstr (klass, "Parser") != NULL)
1074         && (strstr (klass, "Codec") != NULL));
1075 
1076   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DEPAYLOADER))
1077     res = (strstr (klass, "Depayloader") != NULL);
1078 
1079   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_PAYLOADER))
1080     res = (strstr (klass, "Payloader") != NULL);
1081 
1082   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_FORMATTER))
1083     res = (strstr (klass, "Formatter") != NULL);
1084 
1085   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_DECRYPTOR))
1086     res = (strstr (klass, "Decryptor") != NULL);
1087 
1088   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_ENCRYPTOR))
1089     res = (strstr (klass, "Encryptor") != NULL);
1090 
1091   if (!res && (type & GST_ELEMENT_FACTORY_TYPE_HARDWARE))
1092     res = (strstr (klass, "Hardware") != NULL);
1093 
1094   /* Filter by media type now, we only test if it
1095    * matched any of the types above or only checking the media
1096    * type was requested. */
1097   if ((res || !(type & (GST_ELEMENT_FACTORY_TYPE_MAX_ELEMENTS - 1)))
1098       && (type & (GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO |
1099               GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
1100               GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE |
1101               GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE |
1102               GST_ELEMENT_FACTORY_TYPE_MEDIA_METADATA)))
1103     res = ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO)
1104         && (strstr (klass, "Audio") != NULL))
1105         || ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO)
1106         && (strstr (klass, "Video") != NULL))
1107         || ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE)
1108         && (strstr (klass, "Image") != NULL)) ||
1109         ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE)
1110         && (strstr (klass, "Subtitle") != NULL)) ||
1111         ((type & GST_ELEMENT_FACTORY_TYPE_MEDIA_METADATA)
1112         && (strstr (klass, "Metadata") != NULL));
1113 
1114   return res;
1115 }
1116 
1117 static gboolean
element_filter(GstPluginFeature * feature,FilterData * data)1118 element_filter (GstPluginFeature * feature, FilterData * data)
1119 {
1120   gboolean res;
1121 
1122   /* we only care about element factories */
1123   if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
1124     return FALSE;
1125 
1126   res = (gst_plugin_feature_get_rank (feature) >= data->minrank) &&
1127       gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (feature),
1128       data->type);
1129 
1130   return res;
1131 }
1132 
1133 /**
1134  * gst_element_factory_list_get_elements:
1135  * @type: a #GstElementFactoryListType
1136  * @minrank: Minimum rank
1137  *
1138  * Get a list of factories that match the given @type. Only elements
1139  * with a rank greater or equal to @minrank will be returned.
1140  * The list of factories is returned by decreasing rank.
1141  *
1142  * Returns: (transfer full) (element-type Gst.ElementFactory): a #GList of
1143  *     #GstElementFactory elements. Use gst_plugin_feature_list_free() after
1144  *     usage.
1145  */
1146 GList *
gst_element_factory_list_get_elements(GstElementFactoryListType type,GstRank minrank)1147 gst_element_factory_list_get_elements (GstElementFactoryListType type,
1148     GstRank minrank)
1149 {
1150   GList *result;
1151   FilterData data;
1152 
1153   /* prepare type */
1154   data.type = type;
1155   data.minrank = minrank;
1156 
1157   /* get the feature list using the filter */
1158   result = gst_registry_feature_filter (gst_registry_get (),
1159       (GstPluginFeatureFilter) element_filter, FALSE, &data);
1160 
1161   /* sort on rank and name */
1162   result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
1163 
1164   return result;
1165 }
1166 
1167 /**
1168  * gst_element_factory_list_filter:
1169  * @list: (transfer none) (element-type Gst.ElementFactory): a #GList of
1170  *     #GstElementFactory to filter
1171  * @caps: a #GstCaps
1172  * @direction: a #GstPadDirection to filter on
1173  * @subsetonly: whether to filter on caps subsets or not.
1174  *
1175  * Filter out all the elementfactories in @list that can handle @caps in
1176  * the given direction.
1177  *
1178  * If @subsetonly is %TRUE, then only the elements whose pads templates
1179  * are a complete superset of @caps will be returned. Else any element
1180  * whose pad templates caps can intersect with @caps will be returned.
1181  *
1182  * Returns: (transfer full) (element-type Gst.ElementFactory): a #GList of
1183  *     #GstElementFactory elements that match the given requisites.
1184  *     Use #gst_plugin_feature_list_free after usage.
1185  */
1186 GList *
gst_element_factory_list_filter(GList * list,const GstCaps * caps,GstPadDirection direction,gboolean subsetonly)1187 gst_element_factory_list_filter (GList * list,
1188     const GstCaps * caps, GstPadDirection direction, gboolean subsetonly)
1189 {
1190   GQueue results = G_QUEUE_INIT;
1191 
1192   GST_DEBUG ("finding factories");
1193 
1194   /* loop over all the factories */
1195   for (; list; list = list->next) {
1196     GstElementFactory *factory;
1197     const GList *templates;
1198     GList *walk;
1199 
1200     factory = (GstElementFactory *) list->data;
1201 
1202     GST_DEBUG ("Trying %s",
1203         gst_plugin_feature_get_name ((GstPluginFeature *) factory));
1204 
1205     /* get the templates from the element factory */
1206     templates = gst_element_factory_get_static_pad_templates (factory);
1207     for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
1208       GstStaticPadTemplate *templ = walk->data;
1209 
1210       /* we only care about the sink templates */
1211       if (templ->direction == direction) {
1212         GstCaps *tmpl_caps;
1213 
1214         /* try to intersect the caps with the caps of the template */
1215         tmpl_caps = gst_static_caps_get (&templ->static_caps);
1216 
1217         /* FIXME, intersect is not the right method, we ideally want to check
1218          * for a subset here */
1219 
1220         /* check if the intersection is empty */
1221         if ((subsetonly && gst_caps_is_subset (caps, tmpl_caps)) ||
1222             (!subsetonly && gst_caps_can_intersect (caps, tmpl_caps))) {
1223           /* non empty intersection, we can use this element */
1224           g_queue_push_tail (&results, gst_object_ref (factory));
1225           gst_caps_unref (tmpl_caps);
1226           break;
1227         }
1228         gst_caps_unref (tmpl_caps);
1229       }
1230     }
1231   }
1232   return results.head;
1233 }
1234