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