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