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 struct _GstDeviceMonitorPrivate
107 GPtrArray *providers;
117 #define DEFAULT_SHOW_ALL FALSE
124 G_DEFINE_TYPE (GstDeviceMonitor, gst_device_monitor, GST_TYPE_OBJECT);
126 static void gst_device_monitor_dispose (GObject * object);
137 device_filter_free (struct DeviceFilter *filter)
139 g_strfreev (filter->classesv);
140 gst_caps_unref (filter->caps);
142 g_slice_free (struct DeviceFilter, filter);
146 gst_device_monitor_get_property (GObject * object, guint prop_id,
147 GValue * value, GParamSpec * pspec)
149 GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
153 g_value_set_boolean (value,
154 gst_device_monitor_get_show_all_devices (monitor));
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 gst_device_monitor_set_property (GObject * object, guint prop_id,
164 const GValue * value, GParamSpec * pspec)
166 GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);
170 gst_device_monitor_set_show_all_devices (monitor,
171 g_value_get_boolean (value));
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 gst_device_monitor_class_init (GstDeviceMonitorClass * klass)
183 GObjectClass *object_class = G_OBJECT_CLASS (klass);
185 g_type_class_add_private (klass, sizeof (GstDeviceMonitorPrivate));
187 object_class->get_property = gst_device_monitor_get_property;
188 object_class->set_property = gst_device_monitor_set_property;
189 object_class->dispose = gst_device_monitor_dispose;
191 g_object_class_install_property (object_class, PROP_SHOW_ALL,
192 g_param_spec_boolean ("show-all", "Show All",
193 "Show all devices, even those from hidden providers",
194 DEFAULT_SHOW_ALL, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
197 /* must be called with monitor lock */
199 is_provider_hidden (GstDeviceMonitor * monitor, GList * hidden,
200 GstDeviceProvider * provider)
202 GstDeviceProviderFactory *factory;
204 if (monitor->priv->show_all)
207 factory = gst_device_provider_get_factory (provider);
208 if (g_list_find_custom (hidden, GST_OBJECT_NAME (factory),
209 (GCompareFunc) g_strcmp0))
215 /* must be called with monitor lock */
217 update_hidden_providers_list (GList ** hidden, GstDeviceProvider * provider)
221 obs = gst_device_provider_get_hidden_providers (provider);
225 for (i = 0; obs[i]; i++)
226 *hidden = g_list_prepend (*hidden, obs[i]);
233 bus_sync_message (GstBus * bus, GstMessage * message,
234 GstDeviceMonitor * monitor)
236 GstMessageType type = GST_MESSAGE_TYPE (message);
238 if (type == GST_MESSAGE_DEVICE_ADDED || type == GST_MESSAGE_DEVICE_REMOVED) {
239 gboolean matches = TRUE;
241 GstDeviceProvider *provider;
243 if (type == GST_MESSAGE_DEVICE_ADDED)
244 gst_message_parse_device_added (message, &device);
246 gst_message_parse_device_removed (message, &device);
248 GST_OBJECT_LOCK (monitor);
250 GST_DEVICE_PROVIDER (gst_object_get_parent (GST_OBJECT (device)));
251 if (is_provider_hidden (monitor, monitor->priv->hidden, provider)) {
256 for (i = 0; i < monitor->priv->filters->len; i++) {
257 struct DeviceFilter *filter =
258 g_ptr_array_index (monitor->priv->filters, i);
261 caps = gst_device_get_caps (device);
262 matches = gst_caps_can_intersect (filter->caps, caps) &&
263 gst_device_has_classesv (device, filter->classesv);
264 gst_caps_unref (caps);
269 GST_OBJECT_UNLOCK (monitor);
271 gst_object_unref (provider);
272 gst_object_unref (device);
275 gst_bus_post (monitor->priv->bus, gst_message_ref (message));
281 gst_device_monitor_init (GstDeviceMonitor * self)
283 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
284 GST_TYPE_DEVICE_MONITOR, GstDeviceMonitorPrivate);
286 self->priv->show_all = DEFAULT_SHOW_ALL;
288 self->priv->bus = gst_bus_new ();
289 gst_bus_set_flushing (self->priv->bus, TRUE);
291 self->priv->providers = g_ptr_array_new ();
292 self->priv->filters = g_ptr_array_new_with_free_func (
293 (GDestroyNotify) device_filter_free);
295 self->priv->last_id = 1;
300 gst_device_monitor_remove (GstDeviceMonitor * self, guint i)
302 GstDeviceProvider *provider = g_ptr_array_index (self->priv->providers, i);
305 g_ptr_array_remove_index (self->priv->providers, i);
307 bus = gst_device_provider_get_bus (provider);
308 g_signal_handlers_disconnect_by_func (bus, bus_sync_message, self);
309 gst_object_unref (bus);
311 gst_object_unref (provider);
315 gst_device_monitor_dispose (GObject * object)
317 GstDeviceMonitor *self = GST_DEVICE_MONITOR (object);
319 g_return_if_fail (!self->priv->started);
321 if (self->priv->providers) {
322 while (self->priv->providers->len)
323 gst_device_monitor_remove (self, self->priv->providers->len - 1);
324 g_ptr_array_unref (self->priv->providers);
325 self->priv->providers = NULL;
328 if (self->priv->filters) {
329 g_ptr_array_unref (self->priv->filters);
330 self->priv->filters = NULL;
333 gst_object_replace ((GstObject **) & self->priv->bus, NULL);
335 G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
339 * gst_device_monitor_get_devices:
340 * @monitor: A #GstDeviceProvider
342 * Gets a list of devices from all of the relevant monitors. This may actually
343 * probe the hardware if the monitor is not currently started.
345 * Returns: (transfer full) (element-type GstDevice) (nullable): a #GList of
352 gst_device_monitor_get_devices (GstDeviceMonitor * monitor)
354 GList *devices = NULL, *hidden = NULL;
358 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
360 GST_OBJECT_LOCK (monitor);
362 if (monitor->priv->filters->len == 0) {
363 GST_OBJECT_UNLOCK (monitor);
364 GST_WARNING_OBJECT (monitor, "No filters have been set");
368 if (monitor->priv->providers->len == 0) {
369 GST_OBJECT_UNLOCK (monitor);
370 GST_WARNING_OBJECT (monitor, "No providers match the current filters");
376 g_list_free_full (devices, gst_object_unref);
377 g_list_free_full (hidden, g_free);
381 cookie = monitor->priv->cookie;
383 for (i = 0; i < monitor->priv->providers->len; i++) {
385 GstDeviceProvider *provider =
386 gst_object_ref (g_ptr_array_index (monitor->priv->providers, i));
389 if (!is_provider_hidden (monitor, hidden, provider)) {
390 GST_OBJECT_UNLOCK (monitor);
392 tmpdev = gst_device_provider_get_devices (provider);
394 GST_OBJECT_LOCK (monitor);
395 update_hidden_providers_list (&hidden, provider);
401 for (item = tmpdev; item; item = item->next) {
402 GstDevice *dev = GST_DEVICE (item->data);
403 GstCaps *caps = gst_device_get_caps (dev);
406 for (j = 0; j < monitor->priv->filters->len; j++) {
407 struct DeviceFilter *filter =
408 g_ptr_array_index (monitor->priv->filters, j);
410 if (gst_caps_can_intersect (filter->caps, caps) &&
411 gst_device_has_classesv (dev, filter->classesv)) {
412 devices = g_list_prepend (devices, gst_object_ref (dev));
416 gst_caps_unref (caps);
419 g_list_free_full (tmpdev, gst_object_unref);
420 gst_object_unref (provider);
422 if (monitor->priv->cookie != cookie)
425 g_list_free_full (hidden, g_free);
427 GST_OBJECT_UNLOCK (monitor);
429 return g_list_reverse (devices);
433 * gst_device_monitor_start:
434 * @monitor: A #GstDeviceMonitor
436 * Starts monitoring the devices, one this has succeeded, the
437 * %GST_MESSAGE_DEVICE_ADDED and %GST_MESSAGE_DEVICE_REMOVED messages
438 * will be emitted on the bus when the list of devices changes.
440 * Returns: %TRUE if the device monitoring could be started
446 gst_device_monitor_start (GstDeviceMonitor * monitor)
449 GList *pending = NULL, *started = NULL, *removed = NULL;
451 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
453 GST_OBJECT_LOCK (monitor);
455 if (monitor->priv->filters->len == 0) {
456 GST_OBJECT_UNLOCK (monitor);
457 GST_WARNING_OBJECT (monitor, "No filters have been set, will expose all "
459 gst_device_monitor_add_filter (monitor, NULL, NULL);
460 GST_OBJECT_LOCK (monitor);
463 if (monitor->priv->providers->len == 0) {
464 GST_OBJECT_UNLOCK (monitor);
465 GST_WARNING_OBJECT (monitor, "No providers match the current filters");
469 gst_bus_set_flushing (monitor->priv->bus, FALSE);
472 cookie = monitor->priv->cookie;
474 g_list_free_full (pending, gst_object_unref);
479 for (i = 0; i < monitor->priv->providers->len; i++) {
480 GstDeviceProvider *provider;
483 provider = g_ptr_array_index (monitor->priv->providers, i);
485 find = g_list_find (removed, provider);
487 /* this was already started, move to started list */
488 removed = g_list_remove_link (removed, find);
489 started = g_list_concat (started, find);
491 /* not started, add to pending list */
492 pending = g_list_append (pending, gst_object_ref (provider));
495 g_list_free_full (removed, gst_object_unref);
499 GstDeviceProvider *provider = pending->data;
501 if (gst_device_provider_can_monitor (provider)) {
502 GST_OBJECT_UNLOCK (monitor);
504 if (!gst_device_provider_start (provider))
507 GST_OBJECT_LOCK (monitor);
509 started = g_list_prepend (started, provider);
510 pending = g_list_delete_link (pending, pending);
512 if (monitor->priv->cookie != cookie)
515 monitor->priv->started = TRUE;
516 GST_OBJECT_UNLOCK (monitor);
518 g_list_free_full (started, gst_object_unref);
524 GST_OBJECT_LOCK (monitor);
525 gst_bus_set_flushing (monitor->priv->bus, TRUE);
526 GST_OBJECT_UNLOCK (monitor);
529 GstDeviceProvider *provider = started->data;
531 gst_device_provider_stop (provider);
532 gst_object_unref (provider);
534 started = g_list_delete_link (started, started);
541 * gst_device_monitor_stop:
542 * @monitor: A #GstDeviceProvider
544 * Stops monitoring the devices.
549 gst_device_monitor_stop (GstDeviceMonitor * monitor)
552 GList *started = NULL;
554 g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
556 gst_bus_set_flushing (monitor->priv->bus, TRUE);
558 GST_OBJECT_LOCK (monitor);
559 for (i = 0; i < monitor->priv->providers->len; i++) {
560 GstDeviceProvider *provider =
561 g_ptr_array_index (monitor->priv->providers, i);
563 started = g_list_prepend (started, gst_object_ref (provider));
565 GST_OBJECT_UNLOCK (monitor);
568 GstDeviceProvider *provider = started->data;
570 if (gst_device_provider_can_monitor (provider))
571 gst_device_provider_stop (provider);
573 started = g_list_delete_link (started, started);
574 gst_object_unref (provider);
577 GST_OBJECT_LOCK (monitor);
578 monitor->priv->started = FALSE;
579 GST_OBJECT_UNLOCK (monitor);
584 provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
585 GstDeviceMonitor * monitor)
587 GST_OBJECT_LOCK (monitor);
588 monitor->priv->hidden =
589 g_list_prepend (monitor->priv->hidden, g_strdup (hidden));
590 GST_OBJECT_UNLOCK (monitor);
594 provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
595 GstDeviceMonitor * monitor)
599 GST_OBJECT_LOCK (monitor);
601 g_list_find_custom (monitor->priv->hidden, hidden,
602 (GCompareFunc) g_strcmp0);
605 monitor->priv->hidden = g_list_delete_link (monitor->priv->hidden, find);
607 GST_OBJECT_UNLOCK (monitor);
611 * gst_device_monitor_add_filter:
612 * @monitor: a device monitor
613 * @classes: (allow-none): device classes to use as filter or %NULL for any class
614 * @caps: (allow-none): the #GstCaps to filter or %NULL for ANY
616 * Adds a filter for which #GstDevice will be monitored, any device that matches
617 * all these classes and the #GstCaps will be returned.
619 * If this function is called multiple times to add more filters, each will be
620 * matched independently. That is, adding more filters will not further restrict
621 * what devices are matched.
623 * The #GstCaps supported by the device as returned by gst_device_get_caps() are
624 * not intersected with caps filters added using this function.
626 * Filters must be added before the #GstDeviceMonitor is started.
628 * Returns: The id of the new filter or 0 if no provider matched the filter's
634 gst_device_monitor_add_filter (GstDeviceMonitor * monitor,
635 const gchar * classes, GstCaps * caps)
637 GList *factories = NULL;
638 struct DeviceFilter *filter;
640 gboolean matched = FALSE;
642 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), 0);
643 g_return_val_if_fail (!monitor->priv->started, 0);
645 GST_OBJECT_LOCK (monitor);
647 filter = g_slice_new0 (struct DeviceFilter);
648 filter->id = monitor->priv->last_id++;
650 filter->caps = gst_caps_ref (caps);
652 filter->caps = gst_caps_new_any ();
654 filter->classesv = g_strsplit (classes, "/", 0);
656 factories = gst_device_provider_factory_list_get_device_providers (1);
659 GstDeviceProviderFactory *factory = factories->data;
661 if (gst_device_provider_factory_has_classesv (factory, filter->classesv)) {
662 GstDeviceProvider *provider;
664 provider = gst_device_provider_factory_get (factory);
669 for (i = 0; i < monitor->priv->providers->len; i++) {
670 if (g_ptr_array_index (monitor->priv->providers, i) == provider) {
671 gst_object_unref (provider);
680 GstBus *bus = gst_device_provider_get_bus (provider);
682 update_hidden_providers_list (&monitor->priv->hidden, provider);
683 g_signal_connect (provider, "provider-hidden",
684 (GCallback) provider_hidden, monitor);
685 g_signal_connect (provider, "provider-unhidden",
686 (GCallback) provider_unhidden, monitor);
689 gst_bus_enable_sync_message_emission (bus);
690 g_signal_connect (bus, "sync-message",
691 G_CALLBACK (bus_sync_message), monitor);
692 gst_object_unref (bus);
693 g_ptr_array_add (monitor->priv->providers, provider);
694 monitor->priv->cookie++;
698 factories = g_list_remove (factories, factory);
699 gst_object_unref (factory);
702 /* Ensure there is no leak here */
703 g_assert (factories == NULL);
707 g_ptr_array_add (monitor->priv->filters, filter);
709 GST_OBJECT_UNLOCK (monitor);
715 * gst_device_monitor_remove_filter:
716 * @monitor: a device monitor
717 * @filter_id: the id of the filter
719 * Removes a filter from the #GstDeviceMonitor using the id that was returned
720 * by gst_device_monitor_add_filter().
722 * Returns: %TRUE of the filter id was valid, %FALSE otherwise
727 gst_device_monitor_remove_filter (GstDeviceMonitor * monitor, guint filter_id)
730 gboolean removed = FALSE;
732 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
733 g_return_val_if_fail (!monitor->priv->started, FALSE);
734 g_return_val_if_fail (filter_id > 0, FALSE);
736 GST_OBJECT_LOCK (monitor);
737 for (i = 0; i < monitor->priv->filters->len; i++) {
738 struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);
740 if (filter->id == filter_id) {
741 g_ptr_array_remove_index (monitor->priv->filters, i);
748 for (i = 0; i < monitor->priv->providers->len; i++) {
749 GstDeviceProvider *provider =
750 g_ptr_array_index (monitor->priv->providers, i);
751 GstDeviceProviderFactory *factory =
752 gst_device_provider_get_factory (provider);
753 gboolean valid = FALSE;
755 for (j = 0; j < monitor->priv->filters->len; j++) {
756 struct DeviceFilter *filter =
757 g_ptr_array_index (monitor->priv->filters, j);
759 if (gst_device_provider_factory_has_classesv (factory,
767 monitor->priv->cookie++;
768 gst_device_monitor_remove (monitor, i);
774 GST_OBJECT_UNLOCK (monitor);
782 * gst_device_monitor_new:
784 * Create a new #GstDeviceMonitor
786 * Returns: (transfer full): a new device monitor.
791 gst_device_monitor_new (void)
793 GstDeviceMonitor *monitor;
795 monitor = g_object_new (GST_TYPE_DEVICE_MONITOR, NULL);
797 /* Clear floating flag */
798 gst_object_ref_sink (monitor);
804 * gst_device_monitor_get_bus:
805 * @monitor: a #GstDeviceProvider
807 * Gets the #GstBus of this #GstDeviceMonitor
809 * Returns: (transfer full): a #GstBus
814 gst_device_monitor_get_bus (GstDeviceMonitor * monitor)
816 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
818 return gst_object_ref (monitor->priv->bus);
822 * gst_device_monitor_get_providers:
823 * @monitor: a #GstDeviceMonitor
825 * Get a list of the currently selected device provider factories.
829 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
830 * A list of device provider factory names that are currently being
831 * monitored by @monitor or %NULL when nothing is being monitored.
836 gst_device_monitor_get_providers (GstDeviceMonitor * monitor)
841 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
843 GST_OBJECT_LOCK (monitor);
844 len = monitor->priv->providers->len;
848 res = g_new (gchar *, len + 1);
850 for (i = 0; i < len; i++) {
851 GstDeviceProvider *provider =
852 g_ptr_array_index (monitor->priv->providers, i);
853 GstDeviceProviderFactory *factory =
854 gst_device_provider_get_factory (provider);
856 res[i] = g_strdup (GST_OBJECT_NAME (factory));
861 GST_OBJECT_UNLOCK (monitor);
867 * gst_device_monitor_set_show_all_devices:
868 * @monitor: a #GstDeviceMonitor
869 * @show_all: show all devices
871 * Set if all devices should be visible, even those devices from hidden
872 * providers. Setting @show_all to true might show some devices multiple times.
877 gst_device_monitor_set_show_all_devices (GstDeviceMonitor * monitor,
880 g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
882 GST_OBJECT_LOCK (monitor);
883 monitor->priv->show_all = show_all;
884 GST_OBJECT_UNLOCK (monitor);
888 * gst_device_monitor_get_show_all_devices:
889 * @monitor: a #GstDeviceMonitor
891 * Get if @monitor is curretly showing all devices, even those from hidden
894 * Returns: %TRUE when all devices will be shown.
899 gst_device_monitor_get_show_all_devices (GstDeviceMonitor * monitor)
903 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
905 GST_OBJECT_LOCK (monitor);
906 res = monitor->priv->show_all;
907 GST_OBJECT_UNLOCK (monitor);