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) {
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)) {
253 } else if (monitor->priv->filters->len) {
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);
271 GST_OBJECT_UNLOCK (monitor);
273 gst_object_unref (provider);
274 gst_object_unref (device);
277 gst_bus_post (monitor->priv->bus, gst_message_ref (message));
283 gst_device_monitor_init (GstDeviceMonitor * self)
285 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
286 GST_TYPE_DEVICE_MONITOR, GstDeviceMonitorPrivate);
288 self->priv->show_all = DEFAULT_SHOW_ALL;
290 self->priv->bus = gst_bus_new ();
291 gst_bus_set_flushing (self->priv->bus, TRUE);
293 self->priv->providers = g_ptr_array_new ();
294 self->priv->filters = g_ptr_array_new_with_free_func (
295 (GDestroyNotify) device_filter_free);
297 self->priv->last_id = 1;
302 gst_device_monitor_remove (GstDeviceMonitor * self, guint i)
304 GstDeviceProvider *provider = g_ptr_array_index (self->priv->providers, i);
307 g_ptr_array_remove_index (self->priv->providers, i);
309 bus = gst_device_provider_get_bus (provider);
310 g_signal_handlers_disconnect_by_func (bus, bus_sync_message, self);
311 gst_object_unref (bus);
313 gst_object_unref (provider);
317 gst_device_monitor_dispose (GObject * object)
319 GstDeviceMonitor *self = GST_DEVICE_MONITOR (object);
321 g_return_if_fail (!self->priv->started);
323 if (self->priv->providers) {
324 while (self->priv->providers->len)
325 gst_device_monitor_remove (self, self->priv->providers->len - 1);
326 g_ptr_array_unref (self->priv->providers);
327 self->priv->providers = NULL;
330 if (self->priv->filters) {
331 g_ptr_array_unref (self->priv->filters);
332 self->priv->filters = NULL;
335 gst_object_replace ((GstObject **) & self->priv->bus, NULL);
337 G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
341 * gst_device_monitor_get_devices:
342 * @monitor: A #GstDeviceProvider
344 * Gets a list of devices from all of the relevant monitors. This may actually
345 * probe the hardware if the monitor is not currently started.
347 * Returns: (transfer full) (element-type GstDevice): a #GList of
354 gst_device_monitor_get_devices (GstDeviceMonitor * monitor)
356 GList *devices = NULL, *hidden = NULL;
360 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
362 GST_OBJECT_LOCK (monitor);
364 if (monitor->priv->filters->len == 0) {
365 GST_OBJECT_UNLOCK (monitor);
366 GST_WARNING_OBJECT (monitor, "No filters have been set");
370 if (monitor->priv->providers->len == 0) {
371 GST_OBJECT_UNLOCK (monitor);
372 GST_WARNING_OBJECT (monitor, "No providers match the current filters");
378 g_list_free_full (devices, gst_object_unref);
379 g_list_free_full (hidden, g_free);
383 cookie = monitor->priv->cookie;
385 for (i = 0; i < monitor->priv->providers->len; i++) {
387 GstDeviceProvider *provider =
388 gst_object_ref (g_ptr_array_index (monitor->priv->providers, i));
391 if (!is_provider_hidden (monitor, hidden, provider)) {
392 GST_OBJECT_UNLOCK (monitor);
394 tmpdev = gst_device_provider_get_devices (provider);
396 GST_OBJECT_LOCK (monitor);
397 update_hidden_providers_list (&hidden, provider);
403 for (item = tmpdev; item; item = item->next) {
404 GstDevice *dev = GST_DEVICE (item->data);
405 GstCaps *caps = gst_device_get_caps (dev);
408 for (j = 0; j < monitor->priv->filters->len; j++) {
409 struct DeviceFilter *filter =
410 g_ptr_array_index (monitor->priv->filters, j);
412 if (gst_caps_can_intersect (filter->caps, caps) &&
413 gst_device_has_classesv (dev, filter->classesv)) {
414 devices = g_list_prepend (devices, gst_object_ref (dev));
418 gst_caps_unref (caps);
421 g_list_free_full (tmpdev, gst_object_unref);
422 gst_object_unref (provider);
424 if (monitor->priv->cookie != cookie)
427 g_list_free_full (hidden, g_free);
429 GST_OBJECT_UNLOCK (monitor);
431 return g_list_reverse (devices);
435 * gst_device_monitor_start:
436 * @monitor: A #GstDeviceMonitor
438 * Starts monitoring the devices, one this has succeeded, the
439 * %GST_MESSAGE_DEVICE_ADDED and %GST_MESSAGE_DEVICE_REMOVED messages
440 * will be emitted on the bus when the list of devices changes.
442 * Returns: %TRUE if the device monitoring could be started
448 gst_device_monitor_start (GstDeviceMonitor * monitor)
451 GList *pending = NULL, *started = NULL, *removed = NULL;
453 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
455 GST_OBJECT_LOCK (monitor);
457 if (monitor->priv->filters->len == 0) {
458 GST_OBJECT_UNLOCK (monitor);
459 GST_WARNING_OBJECT (monitor, "No filters have been set, will expose all "
461 gst_device_monitor_add_filter (monitor, NULL, NULL);
462 GST_OBJECT_LOCK (monitor);
465 if (monitor->priv->providers->len == 0) {
466 GST_OBJECT_UNLOCK (monitor);
467 GST_WARNING_OBJECT (monitor, "No providers match the current filters");
471 gst_bus_set_flushing (monitor->priv->bus, FALSE);
474 cookie = monitor->priv->cookie;
476 g_list_free_full (pending, gst_object_unref);
481 for (i = 0; i < monitor->priv->providers->len; i++) {
482 GstDeviceProvider *provider;
485 provider = g_ptr_array_index (monitor->priv->providers, i);
487 find = g_list_find (removed, provider);
489 /* this was already started, move to started list */
490 removed = g_list_remove_link (removed, find);
491 started = g_list_concat (started, find);
493 /* not started, add to pending list */
494 pending = g_list_append (pending, gst_object_ref (provider));
497 g_list_free_full (removed, gst_object_unref);
501 GstDeviceProvider *provider = pending->data;
503 if (gst_device_provider_can_monitor (provider)) {
504 GST_OBJECT_UNLOCK (monitor);
506 if (!gst_device_provider_start (provider))
509 GST_OBJECT_LOCK (monitor);
511 started = g_list_prepend (started, provider);
512 pending = g_list_delete_link (pending, pending);
514 if (monitor->priv->cookie != cookie)
517 monitor->priv->started = TRUE;
518 GST_OBJECT_UNLOCK (monitor);
520 g_list_free_full (started, gst_object_unref);
526 GST_OBJECT_LOCK (monitor);
527 gst_bus_set_flushing (monitor->priv->bus, TRUE);
528 GST_OBJECT_UNLOCK (monitor);
531 GstDeviceProvider *provider = started->data;
533 gst_device_provider_stop (provider);
534 gst_object_unref (provider);
536 started = g_list_delete_link (started, started);
543 * gst_device_monitor_stop:
544 * @monitor: A #GstDeviceProvider
546 * Stops monitoring the devices.
551 gst_device_monitor_stop (GstDeviceMonitor * monitor)
554 GList *started = NULL;
556 g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
558 gst_bus_set_flushing (monitor->priv->bus, TRUE);
560 GST_OBJECT_LOCK (monitor);
561 for (i = 0; i < monitor->priv->providers->len; i++) {
562 GstDeviceProvider *provider =
563 g_ptr_array_index (monitor->priv->providers, i);
565 started = g_list_prepend (started, gst_object_ref (provider));
567 GST_OBJECT_UNLOCK (monitor);
570 GstDeviceProvider *provider = started->data;
572 if (gst_device_provider_can_monitor (provider))
573 gst_device_provider_stop (provider);
575 started = g_list_delete_link (started, started);
576 gst_object_unref (provider);
579 GST_OBJECT_LOCK (monitor);
580 monitor->priv->started = FALSE;
581 GST_OBJECT_UNLOCK (monitor);
586 provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
587 GstDeviceMonitor * monitor)
589 GST_OBJECT_LOCK (monitor);
590 monitor->priv->hidden =
591 g_list_prepend (monitor->priv->hidden, g_strdup (hidden));
592 GST_OBJECT_UNLOCK (monitor);
596 provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
597 GstDeviceMonitor * monitor)
601 GST_OBJECT_LOCK (monitor);
603 g_list_find_custom (monitor->priv->hidden, hidden,
604 (GCompareFunc) g_strcmp0);
607 monitor->priv->hidden = g_list_delete_link (monitor->priv->hidden, find);
609 GST_OBJECT_UNLOCK (monitor);
613 * gst_device_monitor_add_filter:
614 * @monitor: a device monitor
615 * @classes: (allow-none): device classes to use as filter or %NULL for any class
616 * @caps: (allow-none): the #GstCaps to filter or %NULL for ANY
618 * Adds a filter for which #GstDevice will be monitored, any device that matches
619 * all these classes and the #GstCaps will be returned.
621 * If this function is called multiple times to add more filters, each will be
622 * matched independently. That is, adding more filters will not further restrict
623 * what devices are matched.
625 * The #GstCaps supported by the device as returned by gst_device_get_caps() are
626 * not intersected with caps filters added using this function.
628 * Filters must be added before the #GstDeviceMonitor is started.
630 * Returns: The id of the new filter or 0 if no provider matched the filter's
636 gst_device_monitor_add_filter (GstDeviceMonitor * monitor,
637 const gchar * classes, GstCaps * caps)
639 GList *factories = NULL;
640 struct DeviceFilter *filter;
642 gboolean matched = FALSE;
644 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), 0);
645 g_return_val_if_fail (!monitor->priv->started, 0);
647 GST_OBJECT_LOCK (monitor);
649 filter = g_slice_new0 (struct DeviceFilter);
650 filter->id = monitor->priv->last_id++;
652 filter->caps = gst_caps_ref (caps);
654 filter->caps = gst_caps_new_any ();
656 filter->classesv = g_strsplit (classes, "/", 0);
658 factories = gst_device_provider_factory_list_get_device_providers (1);
661 GstDeviceProviderFactory *factory = factories->data;
663 if (gst_device_provider_factory_has_classesv (factory, filter->classesv)) {
664 GstDeviceProvider *provider;
666 provider = gst_device_provider_factory_get (factory);
671 for (i = 0; i < monitor->priv->providers->len; i++) {
672 if (g_ptr_array_index (monitor->priv->providers, i) == provider) {
673 gst_object_unref (provider);
682 GstBus *bus = gst_device_provider_get_bus (provider);
684 update_hidden_providers_list (&monitor->priv->hidden, provider);
685 g_signal_connect (provider, "provider-hidden",
686 (GCallback) provider_hidden, monitor);
687 g_signal_connect (provider, "provider-unhidden",
688 (GCallback) provider_unhidden, monitor);
691 gst_bus_enable_sync_message_emission (bus);
692 g_signal_connect (bus, "sync-message",
693 G_CALLBACK (bus_sync_message), monitor);
694 gst_object_unref (bus);
695 g_ptr_array_add (monitor->priv->providers, provider);
696 monitor->priv->cookie++;
700 factories = g_list_remove (factories, factory);
701 gst_object_unref (factory);
704 /* Ensure there is no leak here */
705 g_assert (factories == NULL);
709 g_ptr_array_add (monitor->priv->filters, filter);
711 GST_OBJECT_UNLOCK (monitor);
717 * gst_device_monitor_remove_filter:
718 * @monitor: a device monitor
719 * @filter_id: the id of the filter
721 * Removes a filter from the #GstDeviceMonitor using the id that was returned
722 * by gst_device_monitor_add_filter().
724 * Returns: %TRUE of the filter id was valid, %FALSE otherwise
729 gst_device_monitor_remove_filter (GstDeviceMonitor * monitor, guint filter_id)
732 gboolean removed = FALSE;
734 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
735 g_return_val_if_fail (!monitor->priv->started, FALSE);
736 g_return_val_if_fail (filter_id > 0, FALSE);
738 GST_OBJECT_LOCK (monitor);
739 for (i = 0; i < monitor->priv->filters->len; i++) {
740 struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);
742 if (filter->id == filter_id) {
743 g_ptr_array_remove_index (monitor->priv->filters, i);
750 for (i = 0; i < monitor->priv->providers->len; i++) {
751 GstDeviceProvider *provider =
752 g_ptr_array_index (monitor->priv->providers, i);
753 GstDeviceProviderFactory *factory =
754 gst_device_provider_get_factory (provider);
755 gboolean valid = FALSE;
757 for (j = 0; j < monitor->priv->filters->len; j++) {
758 struct DeviceFilter *filter =
759 g_ptr_array_index (monitor->priv->filters, j);
761 if (gst_device_provider_factory_has_classesv (factory,
769 monitor->priv->cookie++;
770 gst_device_monitor_remove (monitor, i);
776 GST_OBJECT_UNLOCK (monitor);
784 * gst_device_monitor_new:
786 * Create a new #GstDeviceMonitor
788 * Returns: (transfer full): a new device monitor.
793 gst_device_monitor_new (void)
795 GstDeviceMonitor *monitor;
797 monitor = g_object_new (GST_TYPE_DEVICE_MONITOR, NULL);
799 /* Clear floating flag */
800 gst_object_ref_sink (monitor);
806 * gst_device_monitor_get_bus:
807 * @monitor: a #GstDeviceProvider
809 * Gets the #GstBus of this #GstDeviceMonitor
811 * Returns: (transfer full): a #GstBus
816 gst_device_monitor_get_bus (GstDeviceMonitor * monitor)
818 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
820 return gst_object_ref (monitor->priv->bus);
824 * gst_device_monitor_get_providers:
825 * @monitor: a #GstDeviceMonitor
827 * Get a list of the currently selected device provider factories.
831 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
832 * A list of device provider factory names that are currently being
833 * monitored by @monitor or %NULL when nothing is being monitored.
838 gst_device_monitor_get_providers (GstDeviceMonitor * monitor)
843 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
845 GST_OBJECT_LOCK (monitor);
846 len = monitor->priv->providers->len;
850 res = g_new (gchar *, len + 1);
852 for (i = 0; i < len; i++) {
853 GstDeviceProvider *provider =
854 g_ptr_array_index (monitor->priv->providers, i);
855 GstDeviceProviderFactory *factory =
856 gst_device_provider_get_factory (provider);
858 res[i] = g_strdup (GST_OBJECT_NAME (factory));
863 GST_OBJECT_UNLOCK (monitor);
869 * gst_device_monitor_set_show_all_devices:
870 * @monitor: a #GstDeviceMonitor
871 * @show_all: show all devices
873 * Set if all devices should be visible, even those devices from hidden
874 * providers. Setting @show_all to true might show some devices multiple times.
879 gst_device_monitor_set_show_all_devices (GstDeviceMonitor * monitor,
882 g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
884 GST_OBJECT_LOCK (monitor);
885 monitor->priv->show_all = show_all;
886 GST_OBJECT_UNLOCK (monitor);
890 * gst_device_monitor_get_show_all_devices:
891 * @monitor: a #GstDeviceMonitor
893 * Get if @monitor is curretly showing all devices, even those from hidden
896 * Returns: %TRUE when all devices will be shown.
901 gst_device_monitor_get_show_all_devices (GstDeviceMonitor * monitor)
905 g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
907 GST_OBJECT_LOCK (monitor);
908 res = monitor->priv->show_all;
909 GST_OBJECT_UNLOCK (monitor);