2 * Copyright (C) 2013 Olivier Crete <olivier.crete@collabora.com>
4 * gstdevicemonitor.c: device monitor
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.
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.
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.
23 * SECTION:gstdevicemonitor
24 * @title: GstDeviceMonitor
25 * @short_description: A device monitor and prober
26 * @see_also: #GstDevice, #GstDeviceProvider
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
35 * The device monitor will monitor all devices matching the filters that
36 * the application has set.
38 * The basic use pattern of a device monitor is as follows:
41 * my_bus_func (GstBus * bus, GstMessage * message, gpointer user_data)
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);
52 * gst_object_unref (device);
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);
59 * gst_object_unref (device);
65 * return G_SOURCE_CONTINUE;
69 * setup_raw_video_source_device_monitor (void) {
70 * GstDeviceMonitor *monitor;
74 * monitor = gst_device_monitor_new ();
76 * bus = gst_device_monitor_get_bus (monitor);
77 * gst_bus_add_watch (bus, my_bus_func, NULL);
78 * gst_object_unref (bus);
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);
84 * gst_device_monitor_start (monitor);
98 #include "gst_private.h"
99 #include "gstdevicemonitor.h"
101 GST_DEBUG_CATEGORY_STATIC (devicemonitor_debug);
102 #define GST_CAT_DEFAULT devicemonitor_debug
104 struct _GstDeviceMonitorPrivate
110 GPtrArray *providers;
113 GList *started_providers;
120 #define DEFAULT_SHOW_ALL FALSE
127 G_DEFINE_TYPE_WITH_PRIVATE (GstDeviceMonitor, gst_device_monitor,
130 static void gst_device_monitor_dispose (GObject * object);
132 static guint gst_device_monitor_add_filter_unlocked (GstDeviceMonitor * monitor,
133 const gchar * classes, GstCaps * caps);
136 provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
137 GstDeviceMonitor * monitor);
140 provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
141 GstDeviceMonitor * monitor);
151 static struct DeviceFilter *
152 device_filter_copy (struct DeviceFilter *filter)
154 struct DeviceFilter *copy = g_slice_new0 (struct DeviceFilter);
156 copy->classesv = g_strdupv (filter->classesv);
157 copy->caps = filter->caps ? gst_caps_ref (filter->caps) : NULL;
163 device_filter_free (struct DeviceFilter *filter)
165 g_strfreev (filter->classesv);
168 gst_caps_unref (filter->caps);
170 g_slice_free (struct DeviceFilter, filter);
174 gst_device_monitor_get_property (GObject * object, guint prop_id,
175 GValue * value, GParamSpec * pspec)
177 GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
181 g_value_set_boolean (value,
182 gst_device_monitor_get_show_all_devices (monitor));
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 gst_device_monitor_set_property (GObject * object, guint prop_id,
192 const GValue * value, GParamSpec * pspec)
194 GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
198 gst_device_monitor_set_show_all_devices (monitor,
199 g_value_get_boolean (value));
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 gst_device_monitor_class_init (GstDeviceMonitorClass * klass)
211 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
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));
225 /* must be called with monitor lock */
227 is_provider_hidden (GstDeviceMonitor * monitor, GList * hidden,
228 GstDeviceProvider * provider)
230 GstDeviceProviderFactory *factory;
232 if (monitor->priv->show_all)
235 factory = gst_device_provider_get_factory (provider);
236 if (g_list_find_custom (hidden, GST_OBJECT_NAME (factory),
237 (GCompareFunc) g_strcmp0))
243 /* must be called with monitor lock */
245 update_hidden_providers_list (GList ** hidden, GstDeviceProvider * provider)
249 obs = gst_device_provider_get_hidden_providers (provider);
253 for (i = 0; obs[i]; i++)
254 *hidden = g_list_prepend (*hidden, obs[i]);
260 static GstBusSyncReply
261 bus_sync_message (GstBus * bus, GstMessage * message,
262 GstDeviceMonitor * monitor)
264 GstMessageType type = GST_MESSAGE_TYPE (message);
266 if (type == GST_MESSAGE_DEVICE_ADDED || type == GST_MESSAGE_DEVICE_REMOVED ||
267 type == GST_MESSAGE_DEVICE_CHANGED) {
268 gboolean matches = TRUE;
270 GstDeviceProvider *provider;
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);
277 gst_message_parse_device_changed (message, &device, NULL);
279 GST_OBJECT_LOCK (monitor);
281 GST_DEVICE_PROVIDER (gst_object_get_parent (GST_OBJECT (device)));
282 if (is_provider_hidden (monitor, monitor->priv->hidden, provider)) {
287 for (i = 0; i < monitor->priv->filters->len; i++) {
288 struct DeviceFilter *filter =
289 g_ptr_array_index (monitor->priv->filters, i);
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);
300 GST_OBJECT_UNLOCK (monitor);
302 gst_object_unref (provider);
303 gst_object_unref (device);
306 gst_bus_post (monitor->priv->bus, gst_message_ref (message));
309 gst_message_unref (message);
316 gst_device_monitor_init (GstDeviceMonitor * self)
318 self->priv = gst_device_monitor_get_instance_private (self);
320 self->priv->show_all = DEFAULT_SHOW_ALL;
322 self->priv->bus = gst_bus_new ();
323 gst_bus_set_flushing (self->priv->bus, TRUE);
325 self->priv->providers = g_ptr_array_new ();
326 self->priv->filters = g_ptr_array_new_with_free_func (
327 (GDestroyNotify) device_filter_free);
329 self->priv->last_id = 1;
334 gst_device_monitor_remove_provider (GstDeviceMonitor * self, guint i)
336 GstDeviceProvider *provider = g_ptr_array_index (self->priv->providers, i);
339 g_ptr_array_remove_index (self->priv->providers, i);
341 bus = gst_device_provider_get_bus (provider);
342 gst_bus_set_sync_handler (bus, NULL, NULL, NULL);
343 gst_object_unref (bus);
345 g_signal_handlers_disconnect_by_func (provider, provider_hidden, self);
346 g_signal_handlers_disconnect_by_func (provider, provider_unhidden, self);
348 gst_object_unref (provider);
352 gst_device_monitor_dispose (GObject * object)
354 GstDeviceMonitor *self = GST_DEVICE_MONITOR (object);
356 g_return_if_fail (!self->priv->started);
358 if (self->priv->providers) {
359 while (self->priv->providers->len)
360 gst_device_monitor_remove_provider (self, self->priv->providers->len - 1);
361 g_ptr_array_unref (self->priv->providers);
362 self->priv->providers = NULL;
365 if (self->priv->filters) {
366 g_ptr_array_unref (self->priv->filters);
367 self->priv->filters = NULL;
370 if (self->priv->hidden) {
371 g_list_free_full (self->priv->hidden, g_free);
372 self->priv->hidden = NULL;
375 gst_object_replace ((GstObject **) & self->priv->bus, NULL);
377 G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
381 * gst_device_monitor_get_devices:
382 * @monitor: A #GstDeviceProvider
384 * Gets a list of devices from all of the relevant monitors. This may actually
385 * probe the hardware if the monitor is not currently started.
387 * Returns: (transfer full) (element-type GstDevice) (nullable): a #GList of
394 gst_device_monitor_get_devices (GstDeviceMonitor * monitor)
396 GQueue providers = G_QUEUE_INIT, filters = G_QUEUE_INIT;
397 GList *hidden = NULL;
398 GQueue devices = G_QUEUE_INIT;
402 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
404 GST_OBJECT_LOCK (monitor);
406 if (monitor->priv->filters->len == 0) {
407 GST_OBJECT_UNLOCK (monitor);
408 GST_WARNING_OBJECT (monitor, "No filters have been set");
412 if (monitor->priv->providers->len == 0) {
413 GST_OBJECT_UNLOCK (monitor);
414 GST_WARNING_OBJECT (monitor, "No providers match the current filters");
418 for (i = 0; i < monitor->priv->providers->len; i++) {
419 GstDeviceProvider *provider =
420 g_ptr_array_index (monitor->priv->providers, i);
422 update_hidden_providers_list (&hidden, provider);
425 /* Create a copy of all current providers and filters while keeping the lock
426 * and afterwards unlock and work with this snapshot */
427 for (i = 0; i < monitor->priv->providers->len; i++) {
428 GstDeviceProvider *provider =
429 g_ptr_array_index (monitor->priv->providers, i);
431 if (!is_provider_hidden (monitor, hidden, provider)) {
432 g_queue_push_tail (&providers, gst_object_ref (provider));
436 for (i = 0; i < monitor->priv->filters->len; i++) {
437 struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);
439 g_queue_push_tail (&filters, device_filter_copy (filter));
441 GST_OBJECT_UNLOCK (monitor);
443 for (l = providers.head; l; l = l->next) {
444 GstDeviceProvider *provider = l->data;
445 GList *tmpdev, *item, *filter_item;
447 tmpdev = gst_device_provider_get_devices (provider);
449 for (item = tmpdev; item; item = item->next) {
450 GstDevice *dev = GST_DEVICE (item->data);
451 GstCaps *caps = gst_device_get_caps (dev);
453 for (filter_item = filters.head; filter_item;
454 filter_item = filter_item->next) {
455 struct DeviceFilter *filter = filter_item->data;
457 if (gst_caps_can_intersect (filter->caps, caps) &&
458 gst_device_has_classesv (dev, filter->classesv)) {
459 g_queue_push_tail (&devices, gst_object_ref (dev));
463 gst_caps_unref (caps);
466 g_list_free_full (tmpdev, gst_object_unref);
468 g_list_free_full (hidden, g_free);
470 g_queue_clear_full (&providers, (GDestroyNotify) gst_object_unref);
471 g_queue_clear_full (&filters, (GDestroyNotify) device_filter_free);
477 * gst_device_monitor_start:
478 * @monitor: A #GstDeviceMonitor
480 * Starts monitoring the devices, one this has succeeded, the
481 * %GST_MESSAGE_DEVICE_ADDED and %GST_MESSAGE_DEVICE_REMOVED messages
482 * will be emitted on the bus when the list of devices changes.
484 * Returns: %TRUE if the device monitoring could be started, i.e. at least a
485 * single device provider was started successfully.
491 gst_device_monitor_start (GstDeviceMonitor * monitor)
494 GQueue pending = G_QUEUE_INIT;
495 GList *started = NULL;
496 GstDeviceProvider *provider;
498 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
500 GST_OBJECT_LOCK (monitor);
502 if (monitor->priv->started) {
503 GST_OBJECT_UNLOCK (monitor);
504 GST_DEBUG_OBJECT (monitor, "Monitor started already");
507 if (monitor->priv->filters->len == 0) {
508 GST_WARNING_OBJECT (monitor, "No filters have been set, will expose all "
510 gst_device_monitor_add_filter_unlocked (monitor, NULL, NULL);
513 if (monitor->priv->providers->len == 0) {
514 GST_OBJECT_UNLOCK (monitor);
515 GST_WARNING_OBJECT (monitor, "No providers match the current filters");
519 monitor->priv->started = TRUE;
521 gst_bus_set_flushing (monitor->priv->bus, FALSE);
523 for (i = 0; i < monitor->priv->providers->len; i++) {
524 GstDeviceProvider *provider;
526 provider = g_ptr_array_index (monitor->priv->providers, i);
527 g_queue_push_tail (&pending, gst_object_ref (provider));
530 while ((provider = g_queue_pop_head (&pending))) {
531 GST_OBJECT_UNLOCK (monitor);
533 if (gst_device_provider_start (provider)) {
534 started = g_list_prepend (started, provider);
536 gst_object_unref (provider);
539 GST_OBJECT_LOCK (monitor);
543 monitor->priv->started_providers = started;
545 gst_bus_set_flushing (monitor->priv->bus, TRUE);
546 monitor->priv->started = FALSE;
549 GST_OBJECT_UNLOCK (monitor);
551 return started != NULL;
555 * gst_device_monitor_stop:
556 * @monitor: A #GstDeviceProvider
558 * Stops monitoring the devices.
563 gst_device_monitor_stop (GstDeviceMonitor * monitor)
565 GList *started = NULL;
567 g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
569 gst_bus_set_flushing (monitor->priv->bus, TRUE);
571 GST_OBJECT_LOCK (monitor);
572 if (!monitor->priv->started) {
573 GST_DEBUG_OBJECT (monitor, "Monitor was not started yet");
574 GST_OBJECT_UNLOCK (monitor);
578 started = monitor->priv->started_providers;
579 monitor->priv->started_providers = NULL;
580 monitor->priv->started = FALSE;
581 GST_OBJECT_UNLOCK (monitor);
584 GstDeviceProvider *provider = started->data;
586 gst_device_provider_stop (provider);
588 started = g_list_delete_link (started, started);
589 gst_object_unref (provider);
594 provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
595 GstDeviceMonitor * monitor)
597 GST_OBJECT_LOCK (monitor);
598 monitor->priv->hidden =
599 g_list_prepend (monitor->priv->hidden, g_strdup (hidden));
600 GST_OBJECT_UNLOCK (monitor);
604 provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
605 GstDeviceMonitor * monitor)
609 GST_OBJECT_LOCK (monitor);
611 g_list_find_custom (monitor->priv->hidden, hidden,
612 (GCompareFunc) g_strcmp0);
615 monitor->priv->hidden = g_list_delete_link (monitor->priv->hidden, find);
617 GST_OBJECT_UNLOCK (monitor);
621 * gst_device_monitor_add_filter:
622 * @monitor: a device monitor
623 * @classes: (allow-none): device classes to use as filter or %NULL for any class
624 * @caps: (allow-none): the #GstCaps to filter or %NULL for ANY
626 * Adds a filter for which #GstDevice will be monitored, any device that matches
627 * all these classes and the #GstCaps will be returned.
629 * If this function is called multiple times to add more filters, each will be
630 * matched independently. That is, adding more filters will not further restrict
631 * what devices are matched.
633 * The #GstCaps supported by the device as returned by gst_device_get_caps() are
634 * not intersected with caps filters added using this function.
636 * Filters must be added before the #GstDeviceMonitor is started.
638 * Returns: The id of the new filter or 0 if no provider matched the filter's
644 gst_device_monitor_add_filter (GstDeviceMonitor * monitor,
645 const gchar * classes, GstCaps * caps)
649 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), 0);
650 g_return_val_if_fail (!monitor->priv->started, 0);
652 GST_OBJECT_LOCK (monitor);
653 id = gst_device_monitor_add_filter_unlocked (monitor, classes, caps);
654 GST_OBJECT_UNLOCK (monitor);
660 gst_device_monitor_add_filter_unlocked (GstDeviceMonitor * monitor,
661 const gchar * classes, GstCaps * caps)
663 GList *factories = NULL;
664 struct DeviceFilter *filter;
666 gboolean matched = FALSE;
668 filter = g_slice_new0 (struct DeviceFilter);
669 filter->id = monitor->priv->last_id++;
671 filter->caps = gst_caps_ref (caps);
673 filter->caps = gst_caps_new_any ();
675 filter->classesv = g_strsplit (classes, "/", 0);
677 factories = gst_device_provider_factory_list_get_device_providers (1);
680 GstDeviceProviderFactory *factory = factories->data;
682 if (gst_device_provider_factory_has_classesv (factory, filter->classesv)) {
683 GstDeviceProvider *provider;
685 provider = gst_device_provider_factory_get (factory);
690 for (i = 0; i < monitor->priv->providers->len; i++) {
691 if (g_ptr_array_index (monitor->priv->providers, i) == provider) {
692 gst_object_unref (provider);
701 GstBus *bus = gst_device_provider_get_bus (provider);
703 update_hidden_providers_list (&monitor->priv->hidden, provider);
704 g_signal_connect (provider, "provider-hidden",
705 (GCallback) provider_hidden, monitor);
706 g_signal_connect (provider, "provider-unhidden",
707 (GCallback) provider_unhidden, monitor);
710 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_message,
712 gst_object_unref (bus);
713 g_ptr_array_add (monitor->priv->providers, provider);
717 factories = g_list_remove (factories, factory);
718 gst_object_unref (factory);
721 /* Ensure there is no leak here */
722 g_assert (factories == NULL);
726 g_ptr_array_add (monitor->priv->filters, filter);
732 * gst_device_monitor_remove_filter:
733 * @monitor: a device monitor
734 * @filter_id: the id of the filter
736 * Removes a filter from the #GstDeviceMonitor using the id that was returned
737 * by gst_device_monitor_add_filter().
739 * Returns: %TRUE of the filter id was valid, %FALSE otherwise
744 gst_device_monitor_remove_filter (GstDeviceMonitor * monitor, guint filter_id)
747 gboolean removed = FALSE;
749 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
750 g_return_val_if_fail (!monitor->priv->started, FALSE);
751 g_return_val_if_fail (filter_id > 0, FALSE);
753 GST_OBJECT_LOCK (monitor);
754 for (i = 0; i < monitor->priv->filters->len; i++) {
755 struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);
757 if (filter->id == filter_id) {
758 g_ptr_array_remove_index (monitor->priv->filters, i);
765 for (i = 0; i < monitor->priv->providers->len; i++) {
766 GstDeviceProvider *provider =
767 g_ptr_array_index (monitor->priv->providers, i);
768 GstDeviceProviderFactory *factory =
769 gst_device_provider_get_factory (provider);
770 gboolean valid = FALSE;
772 for (j = 0; j < monitor->priv->filters->len; j++) {
773 struct DeviceFilter *filter =
774 g_ptr_array_index (monitor->priv->filters, j);
776 if (gst_device_provider_factory_has_classesv (factory,
784 gst_device_monitor_remove_provider (monitor, i);
790 GST_OBJECT_UNLOCK (monitor);
798 * gst_device_monitor_new:
800 * Create a new #GstDeviceMonitor
802 * Returns: (transfer full): a new device monitor.
807 gst_device_monitor_new (void)
809 GstDeviceMonitor *monitor;
811 monitor = g_object_new (GST_TYPE_DEVICE_MONITOR, NULL);
813 /* Clear floating flag */
814 gst_object_ref_sink (monitor);
820 * gst_device_monitor_get_bus:
821 * @monitor: a #GstDeviceProvider
823 * Gets the #GstBus of this #GstDeviceMonitor
825 * Returns: (transfer full): a #GstBus
830 gst_device_monitor_get_bus (GstDeviceMonitor * monitor)
832 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
834 return gst_object_ref (monitor->priv->bus);
838 * gst_device_monitor_get_providers:
839 * @monitor: a #GstDeviceMonitor
841 * Get a list of the currently selected device provider factories.
845 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
846 * A list of device provider factory names that are currently being
847 * monitored by @monitor or %NULL when nothing is being monitored.
852 gst_device_monitor_get_providers (GstDeviceMonitor * monitor)
857 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
859 GST_OBJECT_LOCK (monitor);
860 len = monitor->priv->providers->len;
864 res = g_new (gchar *, len + 1);
866 for (i = 0; i < len; i++) {
867 GstDeviceProvider *provider =
868 g_ptr_array_index (monitor->priv->providers, i);
869 GstDeviceProviderFactory *factory =
870 gst_device_provider_get_factory (provider);
872 res[i] = g_strdup (GST_OBJECT_NAME (factory));
877 GST_OBJECT_UNLOCK (monitor);
883 * gst_device_monitor_set_show_all_devices:
884 * @monitor: a #GstDeviceMonitor
885 * @show_all: show all devices
887 * Set if all devices should be visible, even those devices from hidden
888 * providers. Setting @show_all to true might show some devices multiple times.
893 gst_device_monitor_set_show_all_devices (GstDeviceMonitor * monitor,
896 g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
898 GST_OBJECT_LOCK (monitor);
899 monitor->priv->show_all = show_all;
900 GST_OBJECT_UNLOCK (monitor);
904 * gst_device_monitor_get_show_all_devices:
905 * @monitor: a #GstDeviceMonitor
907 * Get if @monitor is currently showing all devices, even those from hidden
910 * Returns: %TRUE when all devices will be shown.
915 gst_device_monitor_get_show_all_devices (GstDeviceMonitor * monitor)
919 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
921 GST_OBJECT_LOCK (monitor);
922 res = monitor->priv->show_all;
923 GST_OBJECT_UNLOCK (monitor);