• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2013 Olivier Crete <olivier.crete@collabora.com>
3  *
4  * gstdevicemonitor.c: device monitor
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 /**
23  * SECTION:gstdevicemonitor
24  * @title: GstDeviceMonitor
25  * @short_description: A device monitor and prober
26  * @see_also: #GstDevice, #GstDeviceProvider
27  *
28  * Applications should create a #GstDeviceMonitor when they want
29  * to probe, list and monitor devices of a specific type. The
30  * #GstDeviceMonitor will create the appropriate
31  * #GstDeviceProvider objects and manage them. It will then post
32  * messages on its #GstBus for devices that have been added and
33  * removed.
34  *
35  * The device monitor will monitor all devices matching the filters that
36  * the application has set.
37  *
38  * The basic use pattern of a device monitor is as follows:
39  * |[
40  *   static gboolean
41  *   my_bus_func (GstBus * bus, GstMessage * message, gpointer user_data)
42  *   {
43  *      GstDevice *device;
44  *      gchar *name;
45  *
46  *      switch (GST_MESSAGE_TYPE (message)) {
47  *        case GST_MESSAGE_DEVICE_ADDED:
48  *          gst_message_parse_device_added (message, &device);
49  *          name = gst_device_get_display_name (device);
50  *          g_print("Device added: %s\n", name);
51  *          g_free (name);
52  *          gst_object_unref (device);
53  *          break;
54  *        case GST_MESSAGE_DEVICE_REMOVED:
55  *          gst_message_parse_device_removed (message, &device);
56  *          name = gst_device_get_display_name (device);
57  *          g_print("Device removed: %s\n", name);
58  *          g_free (name);
59  *          gst_object_unref (device);
60  *          break;
61  *        default:
62  *          break;
63  *      }
64  *
65  *      return G_SOURCE_CONTINUE;
66  *   }
67  *
68  *   GstDeviceMonitor *
69  *   setup_raw_video_source_device_monitor (void) {
70  *      GstDeviceMonitor *monitor;
71  *      GstBus *bus;
72  *      GstCaps *caps;
73  *
74  *      monitor = gst_device_monitor_new ();
75  *
76  *      bus = gst_device_monitor_get_bus (monitor);
77  *      gst_bus_add_watch (bus, my_bus_func, NULL);
78  *      gst_object_unref (bus);
79  *
80  *      caps = gst_caps_new_empty_simple ("video/x-raw");
81  *      gst_device_monitor_add_filter (monitor, "Video/Source", caps);
82  *      gst_caps_unref (caps);
83  *
84  *      gst_device_monitor_start (monitor);
85  *
86  *      return monitor;
87  *   }
88  * ]|
89  *
90  * Since: 1.4
91  */
92 
93 
94 #ifdef HAVE_CONFIG_H
95 #include "config.h"
96 #endif
97 
98 #include "gst_private.h"
99 #include "gstdevicemonitor.h"
100 
101 GST_DEBUG_CATEGORY_STATIC (devicemonitor_debug);
102 #define GST_CAT_DEFAULT devicemonitor_debug
103 
104 struct _GstDeviceMonitorPrivate
105 {
106   gboolean started;
107 
108   GstBus *bus;
109 
110   GPtrArray *providers;
111   GPtrArray *filters;
112 
113   GList *started_providers;
114 
115   guint last_id;
116   GList *hidden;
117   gboolean show_all;
118 };
119 
120 #define DEFAULT_SHOW_ALL        FALSE
121 
122 enum
123 {
124   PROP_SHOW_ALL = 1,
125 };
126 
127 G_DEFINE_TYPE_WITH_PRIVATE (GstDeviceMonitor, gst_device_monitor,
128     GST_TYPE_OBJECT);
129 
130 static void gst_device_monitor_dispose (GObject * object);
131 
132 static guint gst_device_monitor_add_filter_unlocked (GstDeviceMonitor * monitor,
133     const gchar * classes, GstCaps * caps);
134 
135 static void
136 provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
137     GstDeviceMonitor * monitor);
138 
139 static void
140 provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
141     GstDeviceMonitor * monitor);
142 
143 struct DeviceFilter
144 {
145   guint id;
146 
147   gchar **classesv;
148   GstCaps *caps;
149 };
150 
151 static struct DeviceFilter *
device_filter_copy(struct DeviceFilter * filter)152 device_filter_copy (struct DeviceFilter *filter)
153 {
154   struct DeviceFilter *copy = g_slice_new0 (struct DeviceFilter);
155 
156   copy->classesv = g_strdupv (filter->classesv);
157   copy->caps = filter->caps ? gst_caps_ref (filter->caps) : NULL;
158 
159   return copy;
160 }
161 
162 static void
device_filter_free(struct DeviceFilter * filter)163 device_filter_free (struct DeviceFilter *filter)
164 {
165   g_strfreev (filter->classesv);
166 
167   if (filter->caps)
168     gst_caps_unref (filter->caps);
169 
170   g_slice_free (struct DeviceFilter, filter);
171 }
172 
173 static void
gst_device_monitor_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)174 gst_device_monitor_get_property (GObject * object, guint prop_id,
175     GValue * value, GParamSpec * pspec)
176 {
177   GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
178 
179   switch (prop_id) {
180     case PROP_SHOW_ALL:
181       g_value_set_boolean (value,
182           gst_device_monitor_get_show_all_devices (monitor));
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186       break;
187   }
188 }
189 
190 static void
gst_device_monitor_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)191 gst_device_monitor_set_property (GObject * object, guint prop_id,
192     const GValue * value, GParamSpec * pspec)
193 {
194   GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
195 
196   switch (prop_id) {
197     case PROP_SHOW_ALL:
198       gst_device_monitor_set_show_all_devices (monitor,
199           g_value_get_boolean (value));
200       break;
201     default:
202       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203       break;
204   }
205 }
206 
207 
208 static void
gst_device_monitor_class_init(GstDeviceMonitorClass * klass)209 gst_device_monitor_class_init (GstDeviceMonitorClass * klass)
210 {
211   GObjectClass *object_class = G_OBJECT_CLASS (klass);
212 
213   object_class->get_property = gst_device_monitor_get_property;
214   object_class->set_property = gst_device_monitor_set_property;
215   object_class->dispose = gst_device_monitor_dispose;
216 
217   GST_DEBUG_CATEGORY_INIT (devicemonitor_debug, "devicemonitor", 0,
218       "debugging info for the device monitor");
219   g_object_class_install_property (object_class, PROP_SHOW_ALL,
220       g_param_spec_boolean ("show-all", "Show All",
221           "Show all devices, even those from hidden providers",
222           DEFAULT_SHOW_ALL, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
223 }
224 
225 /* must be called with monitor lock */
226 static gboolean
is_provider_hidden(GstDeviceMonitor * monitor,GList * hidden,GstDeviceProvider * provider)227 is_provider_hidden (GstDeviceMonitor * monitor, GList * hidden,
228     GstDeviceProvider * provider)
229 {
230   GstDeviceProviderFactory *factory;
231 
232   if (monitor->priv->show_all)
233     return FALSE;
234 
235   factory = gst_device_provider_get_factory (provider);
236   if (g_list_find_custom (hidden, GST_OBJECT_NAME (factory),
237           (GCompareFunc) g_strcmp0))
238     return TRUE;
239 
240   return FALSE;
241 }
242 
243 /* must be called with monitor lock */
244 static void
update_hidden_providers_list(GList ** hidden,GstDeviceProvider * provider)245 update_hidden_providers_list (GList ** hidden, GstDeviceProvider * provider)
246 {
247   gchar **obs;
248 
249   obs = gst_device_provider_get_hidden_providers (provider);
250   if (obs) {
251     gint i;
252 
253     for (i = 0; obs[i]; i++)
254       *hidden = g_list_prepend (*hidden, obs[i]);
255 
256     g_free (obs);
257   }
258 }
259 
260 static void
bus_sync_message(GstBus * bus,GstMessage * message,GstDeviceMonitor * monitor)261 bus_sync_message (GstBus * bus, GstMessage * message,
262     GstDeviceMonitor * monitor)
263 {
264   GstMessageType type = GST_MESSAGE_TYPE (message);
265 
266   if (type == GST_MESSAGE_DEVICE_ADDED || type == GST_MESSAGE_DEVICE_REMOVED ||
267       type == GST_MESSAGE_DEVICE_CHANGED) {
268     gboolean matches = TRUE;
269     GstDevice *device;
270     GstDeviceProvider *provider;
271 
272     if (type == GST_MESSAGE_DEVICE_ADDED)
273       gst_message_parse_device_added (message, &device);
274     else if (type == GST_MESSAGE_DEVICE_REMOVED)
275       gst_message_parse_device_removed (message, &device);
276     else
277       gst_message_parse_device_changed (message, &device, NULL);
278 
279     GST_OBJECT_LOCK (monitor);
280     provider =
281         GST_DEVICE_PROVIDER (gst_object_get_parent (GST_OBJECT (device)));
282     if (is_provider_hidden (monitor, monitor->priv->hidden, provider)) {
283       matches = FALSE;
284     } else {
285       guint i;
286 
287       for (i = 0; i < monitor->priv->filters->len; i++) {
288         struct DeviceFilter *filter =
289             g_ptr_array_index (monitor->priv->filters, i);
290         GstCaps *caps;
291 
292         caps = gst_device_get_caps (device);
293         matches = gst_caps_can_intersect (filter->caps, caps) &&
294             gst_device_has_classesv (device, filter->classesv);
295         gst_caps_unref (caps);
296         if (matches)
297           break;
298       }
299     }
300     GST_OBJECT_UNLOCK (monitor);
301 
302     gst_object_unref (provider);
303     gst_object_unref (device);
304 
305     if (matches)
306       gst_bus_post (monitor->priv->bus, gst_message_ref (message));
307   }
308 }
309 
310 
311 static void
gst_device_monitor_init(GstDeviceMonitor * self)312 gst_device_monitor_init (GstDeviceMonitor * self)
313 {
314   self->priv = gst_device_monitor_get_instance_private (self);
315 
316   self->priv->show_all = DEFAULT_SHOW_ALL;
317 
318   self->priv->bus = gst_bus_new ();
319   gst_bus_set_flushing (self->priv->bus, TRUE);
320 
321   self->priv->providers = g_ptr_array_new ();
322   self->priv->filters = g_ptr_array_new_with_free_func (
323       (GDestroyNotify) device_filter_free);
324 
325   self->priv->last_id = 1;
326 }
327 
328 
329 static void
gst_device_monitor_remove_provider(GstDeviceMonitor * self,guint i)330 gst_device_monitor_remove_provider (GstDeviceMonitor * self, guint i)
331 {
332   GstDeviceProvider *provider = g_ptr_array_index (self->priv->providers, i);
333   GstBus *bus;
334 
335   g_ptr_array_remove_index (self->priv->providers, i);
336 
337   bus = gst_device_provider_get_bus (provider);
338   g_signal_handlers_disconnect_by_func (bus, bus_sync_message, self);
339   gst_object_unref (bus);
340 
341   g_signal_handlers_disconnect_by_func (provider, provider_hidden, self);
342   g_signal_handlers_disconnect_by_func (provider, provider_unhidden, self);
343 
344   gst_object_unref (provider);
345 }
346 
347 static void
gst_device_monitor_dispose(GObject * object)348 gst_device_monitor_dispose (GObject * object)
349 {
350   GstDeviceMonitor *self = GST_DEVICE_MONITOR (object);
351 
352   g_return_if_fail (!self->priv->started);
353 
354   if (self->priv->providers) {
355     while (self->priv->providers->len)
356       gst_device_monitor_remove_provider (self, self->priv->providers->len - 1);
357     g_ptr_array_unref (self->priv->providers);
358     self->priv->providers = NULL;
359   }
360 
361   if (self->priv->filters) {
362     g_ptr_array_unref (self->priv->filters);
363     self->priv->filters = NULL;
364   }
365 
366   if (self->priv->hidden) {
367     g_list_free_full (self->priv->hidden, g_free);
368     self->priv->hidden = NULL;
369   }
370 
371   gst_object_replace ((GstObject **) & self->priv->bus, NULL);
372 
373   G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
374 }
375 
376 #if !GLIB_CHECK_VERSION(2, 60, 0)
377 #define g_queue_clear_full queue_clear_full
378 static void
queue_clear_full(GQueue * queue,GDestroyNotify free_func)379 queue_clear_full (GQueue * queue, GDestroyNotify free_func)
380 {
381   gpointer data;
382 
383   while ((data = g_queue_pop_head (queue)) != NULL)
384     free_func (data);
385 }
386 #endif
387 
388 /**
389  * gst_device_monitor_get_devices:
390  * @monitor: A #GstDeviceProvider
391  *
392  * Gets a list of devices from all of the relevant monitors. This may actually
393  * probe the hardware if the monitor is not currently started.
394  *
395  * Returns: (transfer full) (element-type GstDevice) (nullable): a #GList of
396  *   #GstDevice
397  *
398  * Since: 1.4
399  */
400 
401 GList *
gst_device_monitor_get_devices(GstDeviceMonitor * monitor)402 gst_device_monitor_get_devices (GstDeviceMonitor * monitor)
403 {
404   GQueue providers = G_QUEUE_INIT, filters = G_QUEUE_INIT;
405   GList *hidden = NULL;
406   GQueue devices = G_QUEUE_INIT;
407   GList *l;
408   guint i;
409 
410   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
411 
412   GST_OBJECT_LOCK (monitor);
413 
414   if (monitor->priv->filters->len == 0) {
415     GST_OBJECT_UNLOCK (monitor);
416     GST_WARNING_OBJECT (monitor, "No filters have been set");
417     return NULL;
418   }
419 
420   if (monitor->priv->providers->len == 0) {
421     GST_OBJECT_UNLOCK (monitor);
422     GST_WARNING_OBJECT (monitor, "No providers match the current filters");
423     return NULL;
424   }
425 
426   for (i = 0; i < monitor->priv->providers->len; i++) {
427     GstDeviceProvider *provider =
428         g_ptr_array_index (monitor->priv->providers, i);
429 
430     update_hidden_providers_list (&hidden, provider);
431   }
432 
433   /* Create a copy of all current providers and filters while keeping the lock
434    * and afterwards unlock and work with this snapshot */
435   for (i = 0; i < monitor->priv->providers->len; i++) {
436     GstDeviceProvider *provider =
437         g_ptr_array_index (monitor->priv->providers, i);
438 
439     if (!is_provider_hidden (monitor, hidden, provider)) {
440       g_queue_push_tail (&providers, gst_object_ref (provider));
441     }
442   }
443 
444   for (i = 0; i < monitor->priv->filters->len; i++) {
445     struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);
446 
447     g_queue_push_tail (&filters, device_filter_copy (filter));
448   }
449   GST_OBJECT_UNLOCK (monitor);
450 
451   for (l = providers.head; l; l = l->next) {
452     GstDeviceProvider *provider = l->data;
453     GList *tmpdev, *item, *filter_item;
454 
455     tmpdev = gst_device_provider_get_devices (provider);
456 
457     for (item = tmpdev; item; item = item->next) {
458       GstDevice *dev = GST_DEVICE (item->data);
459       GstCaps *caps = gst_device_get_caps (dev);
460 
461       for (filter_item = filters.head; filter_item;
462           filter_item = filter_item->next) {
463         struct DeviceFilter *filter = filter_item->data;
464 
465         if (gst_caps_can_intersect (filter->caps, caps) &&
466             gst_device_has_classesv (dev, filter->classesv)) {
467           g_queue_push_tail (&devices, gst_object_ref (dev));
468           break;
469         }
470       }
471       gst_caps_unref (caps);
472     }
473 
474     g_list_free_full (tmpdev, gst_object_unref);
475   }
476   g_list_free_full (hidden, g_free);
477 
478   g_queue_clear_full (&providers, (GDestroyNotify) gst_object_unref);
479   g_queue_clear_full (&filters, (GDestroyNotify) device_filter_free);
480 
481   return devices.head;
482 }
483 
484 /**
485  * gst_device_monitor_start:
486  * @monitor: A #GstDeviceMonitor
487  *
488  * Starts monitoring the devices, one this has succeeded, the
489  * %GST_MESSAGE_DEVICE_ADDED and %GST_MESSAGE_DEVICE_REMOVED messages
490  * will be emitted on the bus when the list of devices changes.
491  *
492  * Returns: %TRUE if the device monitoring could be started, i.e. at least a
493  *     single device provider was started successfully.
494  *
495  * Since: 1.4
496  */
497 
498 gboolean
gst_device_monitor_start(GstDeviceMonitor * monitor)499 gst_device_monitor_start (GstDeviceMonitor * monitor)
500 {
501   guint i;
502   GQueue pending = G_QUEUE_INIT;
503   GList *started = NULL;
504   GstDeviceProvider *provider;
505 
506   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
507 
508   GST_OBJECT_LOCK (monitor);
509 
510   if (monitor->priv->started) {
511     GST_OBJECT_UNLOCK (monitor);
512     GST_DEBUG_OBJECT (monitor, "Monitor started already");
513     return TRUE;
514   }
515   if (monitor->priv->filters->len == 0) {
516     GST_WARNING_OBJECT (monitor, "No filters have been set, will expose all "
517         "devices found");
518     gst_device_monitor_add_filter_unlocked (monitor, NULL, NULL);
519   }
520 
521   if (monitor->priv->providers->len == 0) {
522     GST_OBJECT_UNLOCK (monitor);
523     GST_WARNING_OBJECT (monitor, "No providers match the current filters");
524     return FALSE;
525   }
526 
527   monitor->priv->started = TRUE;
528 
529   gst_bus_set_flushing (monitor->priv->bus, FALSE);
530 
531   for (i = 0; i < monitor->priv->providers->len; i++) {
532     GstDeviceProvider *provider;
533 
534     provider = g_ptr_array_index (monitor->priv->providers, i);
535     g_queue_push_tail (&pending, gst_object_ref (provider));
536   }
537 
538   while ((provider = g_queue_pop_head (&pending))) {
539     GST_OBJECT_UNLOCK (monitor);
540 
541     if (gst_device_provider_start (provider)) {
542       started = g_list_prepend (started, provider);
543     } else {
544       gst_object_unref (provider);
545     }
546 
547     GST_OBJECT_LOCK (monitor);
548   }
549 
550   if (started) {
551     monitor->priv->started_providers = started;
552   } else {
553     gst_bus_set_flushing (monitor->priv->bus, TRUE);
554     monitor->priv->started = FALSE;
555   }
556 
557   GST_OBJECT_UNLOCK (monitor);
558 
559   return started != NULL;
560 }
561 
562 /**
563  * gst_device_monitor_stop:
564  * @monitor: A #GstDeviceProvider
565  *
566  * Stops monitoring the devices.
567  *
568  * Since: 1.4
569  */
570 void
gst_device_monitor_stop(GstDeviceMonitor * monitor)571 gst_device_monitor_stop (GstDeviceMonitor * monitor)
572 {
573   GList *started = NULL;
574 
575   g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
576 
577   gst_bus_set_flushing (monitor->priv->bus, TRUE);
578 
579   GST_OBJECT_LOCK (monitor);
580   if (!monitor->priv->started) {
581     GST_DEBUG_OBJECT (monitor, "Monitor was not started yet");
582     GST_OBJECT_UNLOCK (monitor);
583     return;
584   }
585 
586   started = monitor->priv->started_providers;
587   monitor->priv->started_providers = NULL;
588   monitor->priv->started = FALSE;
589   GST_OBJECT_UNLOCK (monitor);
590 
591   while (started) {
592     GstDeviceProvider *provider = started->data;
593 
594     gst_device_provider_stop (provider);
595 
596     started = g_list_delete_link (started, started);
597     gst_object_unref (provider);
598   }
599 }
600 
601 static void
provider_hidden(GstDeviceProvider * provider,const gchar * hidden,GstDeviceMonitor * monitor)602 provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
603     GstDeviceMonitor * monitor)
604 {
605   GST_OBJECT_LOCK (monitor);
606   monitor->priv->hidden =
607       g_list_prepend (monitor->priv->hidden, g_strdup (hidden));
608   GST_OBJECT_UNLOCK (monitor);
609 }
610 
611 static void
provider_unhidden(GstDeviceProvider * provider,const gchar * hidden,GstDeviceMonitor * monitor)612 provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
613     GstDeviceMonitor * monitor)
614 {
615   GList *find;
616 
617   GST_OBJECT_LOCK (monitor);
618   find =
619       g_list_find_custom (monitor->priv->hidden, hidden,
620       (GCompareFunc) g_strcmp0);
621   if (find) {
622     g_free (find->data);
623     monitor->priv->hidden = g_list_delete_link (monitor->priv->hidden, find);
624   }
625   GST_OBJECT_UNLOCK (monitor);
626 }
627 
628 /**
629  * gst_device_monitor_add_filter:
630  * @monitor: a device monitor
631  * @classes: (allow-none): device classes to use as filter or %NULL for any class
632  * @caps: (allow-none): the #GstCaps to filter or %NULL for ANY
633  *
634  * Adds a filter for which #GstDevice will be monitored, any device that matches
635  * all these classes and the #GstCaps will be returned.
636  *
637  * If this function is called multiple times to add more filters, each will be
638  * matched independently. That is, adding more filters will not further restrict
639  * what devices are matched.
640  *
641  * The #GstCaps supported by the device as returned by gst_device_get_caps() are
642  * not intersected with caps filters added using this function.
643  *
644  * Filters must be added before the #GstDeviceMonitor is started.
645  *
646  * Returns: The id of the new filter or 0 if no provider matched the filter's
647  *  classes.
648  *
649  * Since: 1.4
650  */
651 guint
gst_device_monitor_add_filter(GstDeviceMonitor * monitor,const gchar * classes,GstCaps * caps)652 gst_device_monitor_add_filter (GstDeviceMonitor * monitor,
653     const gchar * classes, GstCaps * caps)
654 {
655   guint id;
656 
657   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), 0);
658   g_return_val_if_fail (!monitor->priv->started, 0);
659 
660   GST_OBJECT_LOCK (monitor);
661   id = gst_device_monitor_add_filter_unlocked (monitor, classes, caps);
662   GST_OBJECT_UNLOCK (monitor);
663 
664   return id;
665 }
666 
667 static guint
gst_device_monitor_add_filter_unlocked(GstDeviceMonitor * monitor,const gchar * classes,GstCaps * caps)668 gst_device_monitor_add_filter_unlocked (GstDeviceMonitor * monitor,
669     const gchar * classes, GstCaps * caps)
670 {
671   GList *factories = NULL;
672   struct DeviceFilter *filter;
673   guint id = 0;
674   gboolean matched = FALSE;
675 
676   filter = g_slice_new0 (struct DeviceFilter);
677   filter->id = monitor->priv->last_id++;
678   if (caps)
679     filter->caps = gst_caps_ref (caps);
680   else
681     filter->caps = gst_caps_new_any ();
682   if (classes)
683     filter->classesv = g_strsplit (classes, "/", 0);
684 
685   factories = gst_device_provider_factory_list_get_device_providers (1);
686 
687   while (factories) {
688     GstDeviceProviderFactory *factory = factories->data;
689 
690     if (gst_device_provider_factory_has_classesv (factory, filter->classesv)) {
691       GstDeviceProvider *provider;
692 
693       provider = gst_device_provider_factory_get (factory);
694 
695       if (provider) {
696         guint i;
697 
698         for (i = 0; i < monitor->priv->providers->len; i++) {
699           if (g_ptr_array_index (monitor->priv->providers, i) == provider) {
700             gst_object_unref (provider);
701             provider = NULL;
702             matched = TRUE;
703             break;
704           }
705         }
706       }
707 
708       if (provider) {
709         GstBus *bus = gst_device_provider_get_bus (provider);
710 
711         update_hidden_providers_list (&monitor->priv->hidden, provider);
712         g_signal_connect (provider, "provider-hidden",
713             (GCallback) provider_hidden, monitor);
714         g_signal_connect (provider, "provider-unhidden",
715             (GCallback) provider_unhidden, monitor);
716 
717         matched = TRUE;
718         gst_bus_enable_sync_message_emission (bus);
719         g_signal_connect (bus, "sync-message",
720             G_CALLBACK (bus_sync_message), monitor);
721         gst_object_unref (bus);
722         g_ptr_array_add (monitor->priv->providers, provider);
723       }
724     }
725 
726     factories = g_list_remove (factories, factory);
727     gst_object_unref (factory);
728   }
729 
730   /* Ensure there is no leak here */
731   g_assert (factories == NULL);
732 
733   if (matched)
734     id = filter->id;
735   g_ptr_array_add (monitor->priv->filters, filter);
736 
737   return id;
738 }
739 
740 /**
741  * gst_device_monitor_remove_filter:
742  * @monitor: a device monitor
743  * @filter_id: the id of the filter
744  *
745  * Removes a filter from the #GstDeviceMonitor using the id that was returned
746  * by gst_device_monitor_add_filter().
747  *
748  * Returns: %TRUE of the filter id was valid, %FALSE otherwise
749  *
750  * Since: 1.4
751  */
752 gboolean
gst_device_monitor_remove_filter(GstDeviceMonitor * monitor,guint filter_id)753 gst_device_monitor_remove_filter (GstDeviceMonitor * monitor, guint filter_id)
754 {
755   guint i, j;
756   gboolean removed = FALSE;
757 
758   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
759   g_return_val_if_fail (!monitor->priv->started, FALSE);
760   g_return_val_if_fail (filter_id > 0, FALSE);
761 
762   GST_OBJECT_LOCK (monitor);
763   for (i = 0; i < monitor->priv->filters->len; i++) {
764     struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);
765 
766     if (filter->id == filter_id) {
767       g_ptr_array_remove_index (monitor->priv->filters, i);
768       removed = TRUE;
769       break;
770     }
771   }
772 
773   if (removed) {
774     for (i = 0; i < monitor->priv->providers->len; i++) {
775       GstDeviceProvider *provider =
776           g_ptr_array_index (monitor->priv->providers, i);
777       GstDeviceProviderFactory *factory =
778           gst_device_provider_get_factory (provider);
779       gboolean valid = FALSE;
780 
781       for (j = 0; j < monitor->priv->filters->len; j++) {
782         struct DeviceFilter *filter =
783             g_ptr_array_index (monitor->priv->filters, j);
784 
785         if (gst_device_provider_factory_has_classesv (factory,
786                 filter->classesv)) {
787           valid = TRUE;
788           break;
789         }
790       }
791 
792       if (!valid) {
793         gst_device_monitor_remove_provider (monitor, i);
794         i--;
795       }
796     }
797   }
798 
799   GST_OBJECT_UNLOCK (monitor);
800 
801   return removed;
802 }
803 
804 
805 
806 /**
807  * gst_device_monitor_new:
808  *
809  * Create a new #GstDeviceMonitor
810  *
811  * Returns: (transfer full): a new device monitor.
812  *
813  * Since: 1.4
814  */
815 GstDeviceMonitor *
gst_device_monitor_new(void)816 gst_device_monitor_new (void)
817 {
818   GstDeviceMonitor *monitor;
819 
820   monitor = g_object_new (GST_TYPE_DEVICE_MONITOR, NULL);
821 
822   /* Clear floating flag */
823   gst_object_ref_sink (monitor);
824 
825   return monitor;
826 }
827 
828 /**
829  * gst_device_monitor_get_bus:
830  * @monitor: a #GstDeviceProvider
831  *
832  * Gets the #GstBus of this #GstDeviceMonitor
833  *
834  * Returns: (transfer full): a #GstBus
835  *
836  * Since: 1.4
837  */
838 GstBus *
gst_device_monitor_get_bus(GstDeviceMonitor * monitor)839 gst_device_monitor_get_bus (GstDeviceMonitor * monitor)
840 {
841   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
842 
843   return gst_object_ref (monitor->priv->bus);
844 }
845 
846 /**
847  * gst_device_monitor_get_providers:
848  * @monitor: a #GstDeviceMonitor
849  *
850  * Get a list of the currently selected device provider factories.
851  *
852  * This
853  *
854  * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
855  *     A list of device provider factory names that are currently being
856  *     monitored by @monitor or %NULL when nothing is being monitored.
857  *
858  * Since: 1.6
859  */
860 gchar **
gst_device_monitor_get_providers(GstDeviceMonitor * monitor)861 gst_device_monitor_get_providers (GstDeviceMonitor * monitor)
862 {
863   guint i, len;
864   gchar **res = NULL;
865 
866   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
867 
868   GST_OBJECT_LOCK (monitor);
869   len = monitor->priv->providers->len;
870   if (len == 0)
871     goto done;
872 
873   res = g_new (gchar *, len + 1);
874 
875   for (i = 0; i < len; i++) {
876     GstDeviceProvider *provider =
877         g_ptr_array_index (monitor->priv->providers, i);
878     GstDeviceProviderFactory *factory =
879         gst_device_provider_get_factory (provider);
880 
881     res[i] = g_strdup (GST_OBJECT_NAME (factory));
882   }
883   res[i] = NULL;
884 
885 done:
886   GST_OBJECT_UNLOCK (monitor);
887 
888   return res;
889 }
890 
891 /**
892  * gst_device_monitor_set_show_all_devices:
893  * @monitor: a #GstDeviceMonitor
894  * @show_all: show all devices
895  *
896  * Set if all devices should be visible, even those devices from hidden
897  * providers. Setting @show_all to true might show some devices multiple times.
898  *
899  * Since: 1.6
900  */
901 void
gst_device_monitor_set_show_all_devices(GstDeviceMonitor * monitor,gboolean show_all)902 gst_device_monitor_set_show_all_devices (GstDeviceMonitor * monitor,
903     gboolean show_all)
904 {
905   g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
906 
907   GST_OBJECT_LOCK (monitor);
908   monitor->priv->show_all = show_all;
909   GST_OBJECT_UNLOCK (monitor);
910 }
911 
912 /**
913  * gst_device_monitor_get_show_all_devices:
914  * @monitor: a #GstDeviceMonitor
915  *
916  * Get if @monitor is currently showing all devices, even those from hidden
917  * providers.
918  *
919  * Returns: %TRUE when all devices will be shown.
920  *
921  * Since: 1.6
922  */
923 gboolean
gst_device_monitor_get_show_all_devices(GstDeviceMonitor * monitor)924 gst_device_monitor_get_show_all_devices (GstDeviceMonitor * monitor)
925 {
926   gboolean res;
927 
928   g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
929 
930   GST_OBJECT_LOCK (monitor);
931   res = monitor->priv->show_all;
932   GST_OBJECT_UNLOCK (monitor);
933 
934   return res;
935 }
936