introspection: Assorted minor introspection and documentation fixes
[platform/upstream/gstreamer.git] / gst / gstdeviceproviderfactory.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  * gstdeviceproviderfactory.c: GstDeviceProviderFactory object, support routines
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstdeviceproviderfactory
26  * @short_description: Create GstDeviceProviders from a factory
27  * @see_also: #GstDeviceProvider, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
28  *
29  * #GstDeviceProviderFactory is used to create instances of device providers. A
30  * GstDeviceProviderfactory can be added to a #GstPlugin as it is also a
31  * #GstPluginFeature.
32  *
33  * Use the gst_device_provider_factory_find() and
34  * gst_device_provider_factory_create() functions to create device
35  * provider instances or use gst_device_provider_factory_make() as a
36  * convenient shortcut.
37  *
38  * Since: 1.4
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include "gst_private.h"
46
47 #include "gstdeviceproviderfactory.h"
48 #include "gst.h"
49
50 #include "glib-compat-private.h"
51
52 GST_DEBUG_CATEGORY_STATIC (device_provider_factory_debug);
53 #define GST_CAT_DEFAULT device_provider_factory_debug
54
55 static void gst_device_provider_factory_finalize (GObject * object);
56 static void gst_device_provider_factory_cleanup (GstDeviceProviderFactory *
57     factory);
58
59 /* static guint gst_device_provider_factory_signals[LAST_SIGNAL] = { 0 }; */
60
61 /* this is defined in gstelement.c */
62 extern GQuark __gst_deviceproviderclass_factory;
63
64 #define _do_init \
65 { \
66   GST_DEBUG_CATEGORY_INIT (device_provider_factory_debug, "GST_DEVICE_PROVIDER_FACTORY", \
67       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, \
68       "device provider factories keep information about installed device providers"); \
69 }
70
71 G_DEFINE_TYPE_WITH_CODE (GstDeviceProviderFactory, gst_device_provider_factory,
72     GST_TYPE_PLUGIN_FEATURE, _do_init);
73
74 static void
75 gst_device_provider_factory_class_init (GstDeviceProviderFactoryClass * klass)
76 {
77   GObjectClass *gobject_class = (GObjectClass *) klass;
78
79   gobject_class->finalize = gst_device_provider_factory_finalize;
80 }
81
82 static void
83 gst_device_provider_factory_init (GstDeviceProviderFactory * factory)
84 {
85 }
86
87 static void
88 gst_device_provider_factory_finalize (GObject * object)
89 {
90   GstDeviceProviderFactory *factory = GST_DEVICE_PROVIDER_FACTORY (object);
91   GstDeviceProvider *provider;
92
93   gst_device_provider_factory_cleanup (factory);
94
95   provider = g_atomic_pointer_get (&factory->provider);
96   if (provider)
97     gst_object_unref (provider);
98
99   G_OBJECT_CLASS (gst_device_provider_factory_parent_class)->finalize (object);
100 }
101
102 /**
103  * gst_device_provider_factory_find:
104  * @name: name of factory to find
105  *
106  * Search for an device provider factory of the given name. Refs the returned
107  * device provider factory; caller is responsible for unreffing.
108  *
109  * Returns: (transfer full) (nullable): #GstDeviceProviderFactory if
110  * found, %NULL otherwise
111  *
112  * Since: 1.4
113  */
114 GstDeviceProviderFactory *
115 gst_device_provider_factory_find (const gchar * name)
116 {
117   GstPluginFeature *feature;
118
119   g_return_val_if_fail (name != NULL, NULL);
120
121   feature = gst_registry_find_feature (gst_registry_get (), name,
122       GST_TYPE_DEVICE_PROVIDER_FACTORY);
123   if (feature)
124     return GST_DEVICE_PROVIDER_FACTORY (feature);
125
126   /* this isn't an error, for instance when you query if an device provider factory is
127    * present */
128   GST_LOG ("no such device provider factory \"%s\"", name);
129
130   return NULL;
131 }
132
133 static void
134 gst_device_provider_factory_cleanup (GstDeviceProviderFactory * factory)
135 {
136   if (factory->metadata) {
137     gst_structure_free ((GstStructure *) factory->metadata);
138     factory->metadata = NULL;
139   }
140   if (factory->type) {
141     factory->type = G_TYPE_INVALID;
142   }
143 }
144
145 #define CHECK_METADATA_FIELD(klass, name, key)                                 \
146   G_STMT_START {                                                               \
147     const gchar *metafield = gst_device_provider_class_get_metadata (klass, key);      \
148     if (G_UNLIKELY (metafield == NULL || *metafield == '\0')) {                \
149       g_warning ("Device provider factory metadata for '%s' has no valid %s field", name, key);    \
150       goto detailserror;                                                       \
151     } \
152   } G_STMT_END;
153
154 /**
155  * gst_device_provider_register:
156  * @plugin: (allow-none): #GstPlugin to register the device provider with, or %NULL for
157  *     a static device provider.
158  * @name: name of device providers of this type
159  * @rank: rank of device provider (higher rank means more importance when autoplugging)
160  * @type: GType of device provider to register
161  *
162  * Create a new device providerfactory capable of instantiating objects of the
163  * @type and add the factory to @plugin.
164  *
165  * Returns: %TRUE, if the registering succeeded, %FALSE on error
166  *
167  * Since: 1.4
168  */
169 gboolean
170 gst_device_provider_register (GstPlugin * plugin, const gchar * name,
171     guint rank, GType type)
172 {
173   GstPluginFeature *existing_feature;
174   GstRegistry *registry;
175   GstDeviceProviderFactory *factory;
176   GstDeviceProviderClass *klass;
177
178   g_return_val_if_fail (name != NULL, FALSE);
179   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_DEVICE_PROVIDER), FALSE);
180
181   registry = gst_registry_get ();
182
183   /* check if feature already exists, if it exists there is no need to update it
184    * when the registry is getting updated, outdated plugins and all their
185    * features are removed and readded.
186    */
187   existing_feature = gst_registry_lookup_feature (registry, name);
188   if (existing_feature) {
189     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
190         existing_feature, name);
191     factory = GST_DEVICE_PROVIDER_FACTORY_CAST (existing_feature);
192     factory->type = type;
193     existing_feature->loaded = TRUE;
194     g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
195     gst_object_unref (existing_feature);
196     return TRUE;
197   }
198
199   factory =
200       GST_DEVICE_PROVIDER_FACTORY_CAST (g_object_newv
201       (GST_TYPE_DEVICE_PROVIDER_FACTORY, 0, NULL));
202   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
203   GST_LOG_OBJECT (factory, "Created new device providerfactory for type %s",
204       g_type_name (type));
205
206   /* provide info needed during class structure setup */
207   g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
208   klass = GST_DEVICE_PROVIDER_CLASS (g_type_class_ref (type));
209
210   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
211   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
212   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
213   CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);
214
215   factory->type = type;
216   factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
217
218   if (plugin && plugin->desc.name) {
219     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
220     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
221     g_object_add_weak_pointer ((GObject *) plugin,
222         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
223   } else {
224     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
225     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
226   }
227   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
228   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
229
230   gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
231
232   return TRUE;
233
234   /* ERRORS */
235 detailserror:
236   {
237     gst_device_provider_factory_cleanup (factory);
238     return FALSE;
239   }
240 }
241
242 /**
243  * gst_device_provider_factory_get:
244  * @factory: factory to instantiate
245  *
246  * Returns the device provider of the type defined by the given device
247  * providerfactory.
248  *
249  * Returns: (transfer full) (nullable): the #GstDeviceProvider or %NULL
250  * if the device provider couldn't be created
251  *
252  * Since: 1.4
253  */
254 GstDeviceProvider *
255 gst_device_provider_factory_get (GstDeviceProviderFactory * factory)
256 {
257   GstDeviceProvider *device_provider;
258   GstDeviceProviderClass *oclass;
259   GstDeviceProviderFactory *newfactory;
260
261   g_return_val_if_fail (factory != NULL, NULL);
262
263   newfactory =
264       GST_DEVICE_PROVIDER_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
265           (factory)));
266
267   if (newfactory == NULL)
268     goto load_failed;
269
270   factory = newfactory;
271
272   GST_INFO ("getting device provider \"%s\"", GST_OBJECT_NAME (factory));
273
274   if (factory->type == 0)
275     goto no_type;
276
277   device_provider = g_atomic_pointer_get (&newfactory->provider);
278   if (device_provider)
279     return gst_object_ref (device_provider);
280
281   /* create an instance of the device provider, cast so we don't assert on NULL
282    * also set name as early as we can
283    */
284   device_provider = GST_DEVICE_PROVIDER_CAST (g_object_newv (factory->type, 0,
285           NULL));
286   if (G_UNLIKELY (device_provider == NULL))
287     goto no_device_provider;
288
289   /* fill in the pointer to the factory in the device provider class. The
290    * class will not be unreffed currently.
291    * Be thread safe as there might be 2 threads creating the first instance of
292    * an device provider at the same moment
293    */
294   oclass = GST_DEVICE_PROVIDER_GET_CLASS (device_provider);
295   if (!g_atomic_pointer_compare_and_exchange (&oclass->factory, NULL, factory))
296     gst_object_unref (factory);
297
298   gst_object_ref_sink (device_provider);
299
300   /* We use an atomic to make sure we don't create two in parallel */
301   if (!g_atomic_pointer_compare_and_exchange (&newfactory->provider, NULL,
302           device_provider)) {
303     gst_object_unref (device_provider);
304
305     device_provider = g_atomic_pointer_get (&newfactory->provider);
306   }
307
308   GST_DEBUG ("created device provider \"%s\"", GST_OBJECT_NAME (factory));
309
310   return gst_object_ref (device_provider);
311
312   /* ERRORS */
313 load_failed:
314   {
315     GST_WARNING_OBJECT (factory,
316         "loading plugin containing feature %s returned NULL!",
317         GST_OBJECT_NAME (factory));
318     return NULL;
319   }
320 no_type:
321   {
322     GST_WARNING_OBJECT (factory, "factory has no type");
323     gst_object_unref (factory);
324     return NULL;
325   }
326 no_device_provider:
327   {
328     GST_WARNING_OBJECT (factory, "could not create device provider");
329     gst_object_unref (factory);
330     return NULL;
331   }
332 }
333
334 /**
335  * gst_device_provider_factory_get_by_name:
336  * @factoryname: a named factory to instantiate
337  *
338  * Returns the device provider of the type defined by the given device
339  * provider factory.
340  *
341  * Returns: (transfer full) (nullable): a #GstDeviceProvider or %NULL
342  * if unable to create device provider
343  *
344  * Since: 1.4
345  */
346 GstDeviceProvider *
347 gst_device_provider_factory_get_by_name (const gchar * factoryname)
348 {
349   GstDeviceProviderFactory *factory;
350   GstDeviceProvider *device_provider;
351
352   g_return_val_if_fail (factoryname != NULL, NULL);
353   g_return_val_if_fail (gst_is_initialized (), NULL);
354
355   GST_LOG ("gstdeviceproviderfactory: get_by_name \"%s\"", factoryname);
356
357   factory = gst_device_provider_factory_find (factoryname);
358   if (factory == NULL)
359     goto no_factory;
360
361   GST_LOG_OBJECT (factory, "found factory %p", factory);
362   device_provider = gst_device_provider_factory_get (factory);
363   if (device_provider == NULL)
364     goto create_failed;
365
366   gst_object_unref (factory);
367   return device_provider;
368
369   /* ERRORS */
370 no_factory:
371   {
372     GST_INFO ("no such device provider factory \"%s\"!", factoryname);
373     return NULL;
374   }
375 create_failed:
376   {
377     GST_INFO_OBJECT (factory, "couldn't create instance!");
378     gst_object_unref (factory);
379     return NULL;
380   }
381 }
382
383 /**
384  * gst_device_provider_factory_get_device_provider_type:
385  * @factory: factory to get managed #GType from
386  *
387  * Get the #GType for device providers managed by this factory. The type can
388  * only be retrieved if the device provider factory is loaded, which can be
389  * assured with gst_plugin_feature_load().
390  *
391  * Returns: the #GType for device providers managed by this factory.
392  *
393  * Since: 1.4
394  */
395 GType
396 gst_device_provider_factory_get_device_provider_type (GstDeviceProviderFactory *
397     factory)
398 {
399   g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory),
400       G_TYPE_INVALID);
401
402   return factory->type;
403 }
404
405 /**
406  * gst_device_provider_factory_get_metadata:
407  * @factory: a #GstDeviceProviderFactory
408  * @key: a key
409  *
410  * Get the metadata on @factory with @key.
411  *
412  * Returns: (nullable): the metadata with @key on @factory or %NULL
413  * when there was no metadata with the given @key.
414  *
415  * Since: 1.4
416  */
417 const gchar *
418 gst_device_provider_factory_get_metadata (GstDeviceProviderFactory * factory,
419     const gchar * key)
420 {
421   return gst_structure_get_string ((GstStructure *) factory->metadata, key);
422 }
423
424 /**
425  * gst_device_provider_factory_get_metadata_keys:
426  * @factory: a #GstDeviceProviderFactory
427  *
428  * Get the available keys for the metadata on @factory.
429  *
430  * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
431  * a %NULL-terminated array of key strings, or %NULL when there is no
432  * metadata. Free with g_strfreev() when no longer needed.
433  *
434  * Since: 1.4
435  */
436 gchar **
437 gst_device_provider_factory_get_metadata_keys (GstDeviceProviderFactory *
438     factory)
439 {
440   GstStructure *metadata;
441   gchar **arr;
442   gint i, num;
443
444   g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), NULL);
445
446   metadata = (GstStructure *) factory->metadata;
447   if (metadata == NULL)
448     return NULL;
449
450   num = gst_structure_n_fields (metadata);
451   if (num == 0)
452     return NULL;
453
454   arr = g_new (gchar *, num + 1);
455   for (i = 0; i < num; ++i) {
456     arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
457   }
458   arr[i] = NULL;
459   return arr;
460 }
461
462 /**
463  * gst_device_provider_factory_has_classesv:
464  * @factory: a #GstDeviceProviderFactory
465  * @classes: (array zero-terminated=1) (allow-none): a %NULL terminated array
466  *   of classes to match, only match if all classes are matched
467  *
468  * Check if @factory matches all of the given classes
469  *
470  * Returns: %TRUE if @factory matches.
471  *
472  * Since: 1.4
473  */
474 gboolean
475 gst_device_provider_factory_has_classesv (GstDeviceProviderFactory * factory,
476     gchar ** classes)
477 {
478   const gchar *klass;
479
480   g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), FALSE);
481
482   klass = gst_device_provider_factory_get_metadata (factory,
483       GST_ELEMENT_METADATA_KLASS);
484
485   if (klass == NULL) {
486     GST_ERROR_OBJECT (factory,
487         "device provider factory is missing klass identifiers");
488     return FALSE;
489   }
490
491   for (; classes != NULL && classes[0] != NULL; classes++) {
492     const gchar *found;
493     guint len;
494
495     if (classes[0] == '\0')
496       continue;
497
498     found = strstr (klass, classes[0]);
499
500     if (!found)
501       return FALSE;
502     if (found != klass && *(found - 1) != '/')
503       return FALSE;
504
505     len = strlen (classes[0]);
506     if (found[len] != 0 && found[len] != '/')
507       return FALSE;
508   }
509
510   return TRUE;
511 }
512
513 /**
514  * gst_device_provider_factory_has_classes:
515  * @factory: a #GstDeviceProviderFactory
516  * @classes: (allow-none): a "/" separate list of classes to match, only match
517  *     if all classes are matched
518  *
519  * Check if @factory matches all of the given @classes
520  *
521  * Returns: %TRUE if @factory matches or if @classes is %NULL.
522  *
523  * Since: 1.4
524  */
525 gboolean
526 gst_device_provider_factory_has_classes (GstDeviceProviderFactory * factory,
527     const gchar * classes)
528 {
529   gchar **classesv;
530   gboolean res;
531
532   if (classes == NULL)
533     return TRUE;
534
535   classesv = g_strsplit (classes, "/", 0);
536
537   res = gst_device_provider_factory_has_classesv (factory, classesv);
538
539   g_strfreev (classesv);
540
541   return res;
542 }
543
544 static gboolean
545 device_provider_filter (GstPluginFeature * feature, GstRank * minrank)
546 {
547   /* we only care about device provider factories */
548   if (G_UNLIKELY (!GST_IS_DEVICE_PROVIDER_FACTORY (feature)))
549     return FALSE;
550
551   return (gst_plugin_feature_get_rank (feature) >= *minrank);
552 }
553
554 /**
555  * gst_device_provider_factory_list_get_device_providers:
556  * @minrank: Minimum rank
557  *
558  * Get a list of factories with a rank greater or equal to @minrank.
559  * The list of factories is returned by decreasing rank.
560  *
561  * Returns: (transfer full) (element-type Gst.DeviceProviderFactory):
562  * a #GList of #GstDeviceProviderFactory device providers. Use
563  * gst_plugin_feature_list_free() after usage.
564  *
565  * Since: 1.4
566  */
567 GList *
568 gst_device_provider_factory_list_get_device_providers (GstRank minrank)
569 {
570   GList *result;
571
572   /* get the feature list using the filter */
573   result = gst_registry_feature_filter (gst_registry_get (),
574       (GstPluginFeatureFilter) device_provider_filter, FALSE, &minrank);
575
576   /* sort on rank and name */
577   result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
578
579   return result;
580 }