2 * Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
4 * gstdeviceprovider.c: Device probing and monitoring
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:gstdeviceprovider
24 * @title: GstDeviceProvider
25 * @short_description: A device provider
26 * @see_also: #GstDevice, #GstDeviceMonitor
28 * A #GstDeviceProvider subclass is provided by a plugin that handles devices
29 * if there is a way to programmatically list connected devices. It can also
30 * optionally provide updates to the list of connected devices.
32 * Each #GstDeviceProvider subclass is a singleton, a plugin should
33 * normally provide a single subclass for all devices.
35 * Applications would normally use a #GstDeviceMonitor to monitor devices
36 * from all relevant providers.
45 #include "gst_private.h"
47 #include "gstdeviceprovider.h"
49 #include "gstelementmetadata.h"
52 struct _GstDeviceProviderPrivate
58 gboolean started_count;
60 GList *hidden_providers;
70 static guint gst_device_provider_signals[LAST_SIGNAL] = { 0 };
72 /* this is used in gstelementfactory.c:gst_element_register() */
73 GQuark __gst_deviceproviderclass_factory = 0;
75 static void gst_device_provider_class_init (GstDeviceProviderClass * klass);
76 static void gst_device_provider_init (GstDeviceProvider * element);
77 static void gst_device_provider_base_class_init (gpointer g_class);
78 static void gst_device_provider_dispose (GObject * object);
79 static void gst_device_provider_finalize (GObject * object);
81 static gpointer gst_device_provider_parent_class = NULL;
82 static gint private_offset = 0;
85 gst_device_provider_get_type (void)
87 static volatile gsize gst_device_provider_type = 0;
89 if (g_once_init_enter (&gst_device_provider_type)) {
91 static const GTypeInfo element_info = {
92 sizeof (GstDeviceProviderClass),
93 gst_device_provider_base_class_init,
94 NULL, /* base_class_finalize */
95 (GClassInitFunc) gst_device_provider_class_init,
98 sizeof (GstDeviceProvider),
100 (GInstanceInitFunc) gst_device_provider_init,
104 _type = g_type_register_static (GST_TYPE_OBJECT, "GstDeviceProvider",
105 &element_info, G_TYPE_FLAG_ABSTRACT);
108 g_type_add_instance_private (_type, sizeof (GstDeviceProviderPrivate));
110 __gst_deviceproviderclass_factory =
111 g_quark_from_static_string ("GST_DEVICEPROVIDERCLASS_FACTORY");
112 g_once_init_leave (&gst_device_provider_type, _type);
114 return gst_device_provider_type;
117 static inline gpointer
118 gst_device_provider_get_instance_private (GstDeviceProvider * self)
120 return (G_STRUCT_MEMBER_P (self, private_offset));
124 gst_device_provider_base_class_init (gpointer g_class)
126 GstDeviceProviderClass *klass = GST_DEVICE_PROVIDER_CLASS (g_class);
128 /* Copy the element details here so elements can inherit the
129 * details from their base class and classes only need to set
130 * the details in class_init instead of base_init */
132 klass->metadata ? gst_structure_copy (klass->metadata) :
133 gst_structure_new_empty ("metadata");
135 klass->factory = g_type_get_qdata (G_TYPE_FROM_CLASS (klass),
136 __gst_deviceproviderclass_factory);
140 gst_device_provider_class_init (GstDeviceProviderClass * klass)
142 GObjectClass *gobject_class = (GObjectClass *) klass;
144 gst_device_provider_parent_class = g_type_class_peek_parent (klass);
146 if (private_offset != 0)
147 g_type_class_adjust_private_offset (klass, &private_offset);
149 gobject_class->dispose = gst_device_provider_dispose;
150 gobject_class->finalize = gst_device_provider_finalize;
152 gst_device_provider_signals[PROVIDER_HIDDEN] =
153 g_signal_new ("provider-hidden", G_TYPE_FROM_CLASS (klass),
154 G_SIGNAL_RUN_FIRST, 0, NULL,
155 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_STRING);
157 gst_device_provider_signals[PROVIDER_UNHIDDEN] =
158 g_signal_new ("provider-unhidden", G_TYPE_FROM_CLASS (klass),
159 G_SIGNAL_RUN_FIRST, 0, NULL,
160 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_STRING);
164 gst_device_provider_init (GstDeviceProvider * provider)
166 provider->priv = gst_device_provider_get_instance_private (provider);
168 g_mutex_init (&provider->priv->start_lock);
170 provider->priv->bus = gst_bus_new ();
171 gst_bus_set_flushing (provider->priv->bus, TRUE);
176 gst_device_provider_dispose (GObject * object)
178 GstDeviceProvider *provider = GST_DEVICE_PROVIDER (object);
180 gst_object_replace ((GstObject **) & provider->priv->bus, NULL);
182 GST_OBJECT_LOCK (provider);
183 g_list_free_full (provider->devices, (GDestroyNotify) gst_object_unparent);
184 provider->devices = NULL;
185 GST_OBJECT_UNLOCK (provider);
187 G_OBJECT_CLASS (gst_device_provider_parent_class)->dispose (object);
191 gst_device_provider_finalize (GObject * object)
193 GstDeviceProvider *provider = GST_DEVICE_PROVIDER (object);
195 g_mutex_clear (&provider->priv->start_lock);
197 G_OBJECT_CLASS (gst_device_provider_parent_class)->finalize (object);
201 * gst_device_provider_class_add_metadata:
202 * @klass: class to set metadata for
203 * @key: the key to set
204 * @value: the value to set
206 * Set @key with @value as metadata in @klass.
211 gst_device_provider_class_add_metadata (GstDeviceProviderClass * klass,
212 const gchar * key, const gchar * value)
214 g_return_if_fail (GST_IS_DEVICE_PROVIDER_CLASS (klass));
215 g_return_if_fail (key != NULL);
216 g_return_if_fail (value != NULL);
218 gst_structure_set ((GstStructure *) klass->metadata,
219 key, G_TYPE_STRING, value, NULL);
223 * gst_device_provider_class_add_static_metadata:
224 * @klass: class to set metadata for
225 * @key: the key to set
226 * @value: (transfer full): the value to set
228 * Set @key with @value as metadata in @klass.
230 * Same as gst_device_provider_class_add_metadata(), but @value must be a static string
231 * or an inlined string, as it will not be copied. (GStreamer plugins will
232 * be made resident once loaded, so this function can be used even from
233 * dynamically loaded plugins.)
238 gst_device_provider_class_add_static_metadata (GstDeviceProviderClass * klass,
239 const gchar * key, const gchar * value)
241 GValue val = G_VALUE_INIT;
243 g_return_if_fail (GST_IS_DEVICE_PROVIDER_CLASS (klass));
244 g_return_if_fail (key != NULL);
245 g_return_if_fail (value != NULL);
247 g_value_init (&val, G_TYPE_STRING);
248 g_value_set_static_string (&val, value);
249 gst_structure_take_value ((GstStructure *) klass->metadata, key, &val);
253 * gst_device_provider_class_set_metadata:
254 * @klass: class to set metadata for
255 * @longname: The long English name of the device provider. E.g. "File Sink"
256 * @classification: String describing the type of device provider, as an
257 * unordered list separated with slashes ('/'). See draft-klass.txt of the
259 * for more details and common types. E.g: "Sink/File"
260 * @description: Sentence describing the purpose of the device provider.
261 * E.g: "Write stream to a file"
262 * @author: Name and contact details of the author(s). Use \n to separate
263 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
265 * Sets the detailed information for a #GstDeviceProviderClass.
267 * > This function is for use in _class_init functions only.
272 gst_device_provider_class_set_metadata (GstDeviceProviderClass * klass,
273 const gchar * longname, const gchar * classification,
274 const gchar * description, const gchar * author)
276 g_return_if_fail (GST_IS_DEVICE_PROVIDER_CLASS (klass));
277 g_return_if_fail (longname != NULL && *longname != '\0');
278 g_return_if_fail (classification != NULL && *classification != '\0');
279 g_return_if_fail (description != NULL && *description != '\0');
280 g_return_if_fail (author != NULL && *author != '\0');
282 gst_structure_id_set ((GstStructure *) klass->metadata,
283 GST_QUARK (ELEMENT_METADATA_LONGNAME), G_TYPE_STRING, longname,
284 GST_QUARK (ELEMENT_METADATA_KLASS), G_TYPE_STRING, classification,
285 GST_QUARK (ELEMENT_METADATA_DESCRIPTION), G_TYPE_STRING, description,
286 GST_QUARK (ELEMENT_METADATA_AUTHOR), G_TYPE_STRING, author, NULL);
290 * gst_device_provider_class_set_static_metadata:
291 * @klass: class to set metadata for
292 * @longname: (transfer full): The long English name of the element. E.g. "File Sink"
293 * @classification: (transfer full): String describing the type of element, as
294 * an unordered list separated with slashes ('/'). See draft-klass.txt of the
295 * design docs for more details and common types. E.g: "Sink/File"
296 * @description: (transfer full): Sentence describing the purpose of the
297 * element. E.g: "Write stream to a file"
298 * @author: (transfer full): Name and contact details of the author(s). Use \n
299 * to separate multiple author metadata. E.g: "Joe Bloggs <joe.blogs at
302 * Sets the detailed information for a #GstDeviceProviderClass.
304 * > This function is for use in _class_init functions only.
306 * Same as gst_device_provider_class_set_metadata(), but @longname, @classification,
307 * @description, and @author must be static strings or inlined strings, as
308 * they will not be copied. (GStreamer plugins will be made resident once
309 * loaded, so this function can be used even from dynamically loaded plugins.)
314 gst_device_provider_class_set_static_metadata (GstDeviceProviderClass * klass,
315 const gchar * longname, const gchar * classification,
316 const gchar * description, const gchar * author)
318 GstStructure *s = (GstStructure *) klass->metadata;
319 GValue val = G_VALUE_INIT;
321 g_return_if_fail (GST_IS_DEVICE_PROVIDER_CLASS (klass));
322 g_return_if_fail (longname != NULL && *longname != '\0');
323 g_return_if_fail (classification != NULL && *classification != '\0');
324 g_return_if_fail (description != NULL && *description != '\0');
325 g_return_if_fail (author != NULL && *author != '\0');
327 g_value_init (&val, G_TYPE_STRING);
329 g_value_set_static_string (&val, longname);
330 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_LONGNAME), &val);
332 g_value_set_static_string (&val, classification);
333 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_KLASS), &val);
335 g_value_set_static_string (&val, description);
336 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_DESCRIPTION),
339 g_value_set_static_string (&val, author);
340 gst_structure_id_take_value (s, GST_QUARK (ELEMENT_METADATA_AUTHOR), &val);
344 * gst_device_provider_class_get_metadata:
345 * @klass: class to get metadata for
346 * @key: the key to get
348 * Get metadata with @key in @klass.
350 * Returns: (nullable): the metadata for @key.
355 gst_device_provider_class_get_metadata (GstDeviceProviderClass * klass,
358 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_CLASS (klass), NULL);
359 g_return_val_if_fail (key != NULL, NULL);
361 return gst_structure_get_string ((GstStructure *) klass->metadata, key);
365 * gst_device_provider_get_metadata:
366 * @provider: provider to get metadata for
367 * @key: the key to get
369 * Get metadata with @key in @provider.
371 * Returns: the metadata for @key.
376 gst_device_provider_get_metadata (GstDeviceProvider * provider,
379 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), NULL);
380 g_return_val_if_fail (key != NULL, NULL);
383 gst_device_provider_class_get_metadata (GST_DEVICE_PROVIDER_GET_CLASS
388 * gst_device_provider_get_devices:
389 * @provider: A #GstDeviceProvider
391 * Gets a list of devices that this provider understands. This may actually
392 * probe the hardware if the provider is not currently started.
394 * Returns: (transfer full) (element-type GstDevice): a #GList of
401 gst_device_provider_get_devices (GstDeviceProvider * provider)
403 GstDeviceProviderClass *klass;
404 GList *devices = NULL;
408 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), NULL);
409 klass = GST_DEVICE_PROVIDER_GET_CLASS (provider);
411 g_mutex_lock (&provider->priv->start_lock);
412 started = (provider->priv->started_count > 0);
415 GST_OBJECT_LOCK (provider);
416 for (item = provider->devices; item; item = item->next)
417 devices = g_list_prepend (devices, gst_object_ref (item->data));
418 GST_OBJECT_UNLOCK (provider);
419 } else if (klass->probe)
420 devices = klass->probe (provider);
422 g_mutex_unlock (&provider->priv->start_lock);
428 * gst_device_provider_start:
429 * @provider: A #GstDeviceProvider
431 * Starts providering the devices. This will cause #GST_MESSAGE_DEVICE_ADDED
432 * and #GST_MESSAGE_DEVICE_REMOVED messages to be posted on the provider's bus
433 * when devices are added or removed from the system.
435 * Since the #GstDeviceProvider is a singleton,
436 * gst_device_provider_start() may already have been called by another
437 * user of the object, gst_device_provider_stop() needs to be called the same
440 * Returns: %TRUE if the device providering could be started
446 gst_device_provider_start (GstDeviceProvider * provider)
448 GstDeviceProviderClass *klass;
449 gboolean ret = FALSE;
451 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), FALSE);
452 klass = GST_DEVICE_PROVIDER_GET_CLASS (provider);
454 g_mutex_lock (&provider->priv->start_lock);
456 if (provider->priv->started_count > 0) {
457 provider->priv->started_count++;
463 ret = klass->start (provider);
466 provider->priv->started_count++;
467 gst_bus_set_flushing (provider->priv->bus, FALSE);
472 g_mutex_unlock (&provider->priv->start_lock);
478 * gst_device_provider_stop:
479 * @provider: A #GstDeviceProvider
481 * Decreases the use-count by one. If the use count reaches zero, this
482 * #GstDeviceProvider will stop providering the devices. This needs to be
483 * called the same number of times that gst_device_provider_start() was called.
489 gst_device_provider_stop (GstDeviceProvider * provider)
491 GstDeviceProviderClass *klass;
493 g_return_if_fail (GST_IS_DEVICE_PROVIDER (provider));
494 klass = GST_DEVICE_PROVIDER_GET_CLASS (provider);
496 g_mutex_lock (&provider->priv->start_lock);
498 if (provider->priv->started_count == 1) {
499 gst_bus_set_flushing (provider->priv->bus, TRUE);
501 klass->stop (provider);
502 GST_OBJECT_LOCK (provider);
503 g_list_free_full (provider->devices, (GDestroyNotify) gst_object_unparent);
504 provider->devices = NULL;
505 GST_OBJECT_UNLOCK (provider);
506 } else if (provider->priv->started_count < 1) {
508 ("Trying to stop a GstDeviceProvider %s which is already stopped",
509 GST_OBJECT_NAME (provider));
512 provider->priv->started_count--;
513 g_mutex_unlock (&provider->priv->start_lock);
518 * gst_device_provider_get_factory:
519 * @provider: a #GstDeviceProvider to request the device provider factory of.
521 * Retrieves the factory that was used to create this device provider.
523 * Returns: (transfer none): the #GstDeviceProviderFactory used for
524 * creating this device provider. no refcounting is needed.
528 GstDeviceProviderFactory *
529 gst_device_provider_get_factory (GstDeviceProvider * provider)
531 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), NULL);
533 return GST_DEVICE_PROVIDER_GET_CLASS (provider)->factory;
537 * gst_device_provider_can_provider:
538 * @provider: a #GstDeviceProvider
540 * If this function returns %TRUE, then the device provider can provider if
541 * devices are added or removed. Otherwise, it can only do static probing.
543 * Returns: %TRUE if the #GstDeviceProvider support providering, %FALSE otherwise
546 gst_device_provider_can_monitor (GstDeviceProvider * provider)
548 GstDeviceProviderClass *klass;
550 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), FALSE);
551 klass = GST_DEVICE_PROVIDER_GET_CLASS (provider);
560 * gst_device_provider_get_bus:
561 * @provider: a #GstDeviceProvider
563 * Gets the #GstBus of this #GstDeviceProvider
565 * Returns: (transfer full): a #GstBus
570 gst_device_provider_get_bus (GstDeviceProvider * provider)
572 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), NULL);
574 return gst_object_ref (provider->priv->bus);
578 * gst_device_provider_device_add:
579 * @provider: a #GstDeviceProvider
580 * @device: (transfer floating): a #GstDevice that has been added
582 * Posts a message on the provider's #GstBus to inform applications that
583 * a new device has been added.
585 * This is for use by subclasses.
587 * @device's reference count will be incremented, and any floating reference
588 * will be removed (see gst_object_ref_sink()).
593 gst_device_provider_device_add (GstDeviceProvider * provider,
598 g_return_if_fail (GST_IS_DEVICE_PROVIDER (provider));
599 g_return_if_fail (GST_IS_DEVICE (device));
601 if (!gst_object_set_parent (GST_OBJECT (device), GST_OBJECT (provider))) {
602 GST_WARNING_OBJECT (provider, "Could not parent device %p to provider,"
603 " it already has a parent", device);
607 GST_OBJECT_LOCK (provider);
608 /* Take an additional reference so we can be sure nobody removed it from the
609 * provider in the meantime and we can safely emit the message */
610 gst_object_ref (device);
611 provider->devices = g_list_prepend (provider->devices, device);
612 GST_OBJECT_UNLOCK (provider);
614 message = gst_message_new_device_added (GST_OBJECT (provider), device);
615 gst_bus_post (provider->priv->bus, message);
616 gst_object_unref (device);
621 * gst_device_provider_device_remove:
622 * @provider: a #GstDeviceProvider
623 * @device: a #GstDevice that has been removed
625 * Posts a message on the provider's #GstBus to inform applications that
626 * a device has been removed.
628 * This is for use by subclasses.
633 gst_device_provider_device_remove (GstDeviceProvider * provider,
639 g_return_if_fail (GST_IS_DEVICE_PROVIDER (provider));
640 g_return_if_fail (GST_IS_DEVICE (device));
642 GST_OBJECT_LOCK (provider);
643 item = g_list_find (provider->devices, device);
645 provider->devices = g_list_delete_link (provider->devices, item);
647 GST_OBJECT_UNLOCK (provider);
649 message = gst_message_new_device_removed (GST_OBJECT (provider), device);
650 g_signal_emit_by_name (device, "removed");
651 gst_bus_post (provider->priv->bus, message);
653 gst_object_unparent (GST_OBJECT (device));
657 * gst_device_provider_get_hidden_providers:
658 * @provider: a #GstDeviceProvider
660 * Get the provider factory names of the #GstDeviceProvider instances that
661 * are hidden by @provider.
663 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
664 * a list of hidden providers factory names or %NULL when
665 * nothing is hidden by @provider. Free with g_strfreev.
670 gst_device_provider_get_hidden_providers (GstDeviceProvider * provider)
676 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER (provider), NULL);
678 GST_OBJECT_LOCK (provider);
679 len = g_list_length (provider->priv->hidden_providers);
683 res = g_new (gchar *, len + 1);
684 for (i = 0, walk = provider->priv->hidden_providers; walk;
685 walk = g_list_next (walk), i++)
686 res[i] = g_strdup (walk->data);
690 GST_OBJECT_UNLOCK (provider);
696 * gst_device_provider_hide_provider:
697 * @provider: a #GstDeviceProvider
698 * @name: a provider factory name
700 * Make @provider hide the devices from the factory with @name.
702 * This function is used when @provider will also provide the devices reported
703 * by provider factory @name. A monitor should stop monitoring the
704 * device provider with @name to avoid duplicate devices.
709 gst_device_provider_hide_provider (GstDeviceProvider * provider,
713 const gchar *hidden_name = NULL;
715 g_return_if_fail (GST_IS_DEVICE_PROVIDER (provider));
716 g_return_if_fail (name != NULL);
718 GST_OBJECT_LOCK (provider);
720 g_list_find_custom (provider->priv->hidden_providers, name,
721 (GCompareFunc) g_strcmp0);
724 provider->priv->hidden_providers =
725 g_list_prepend (provider->priv->hidden_providers, g_strdup (name));
727 GST_OBJECT_UNLOCK (provider);
730 g_signal_emit (provider, gst_device_provider_signals[PROVIDER_HIDDEN],
735 * gst_device_provider_unhide_provider:
736 * @provider: a #GstDeviceProvider
737 * @name: a provider factory name
739 * Make @provider unhide the devices from factory @name.
741 * This function is used when @provider will no longer provide the devices
742 * reported by provider factory @name. A monitor should start
743 * monitoring the devices from provider factory @name in order to see
749 gst_device_provider_unhide_provider (GstDeviceProvider * provider,
753 gchar *unhidden_name = NULL;
755 g_return_if_fail (GST_IS_DEVICE_PROVIDER (provider));
756 g_return_if_fail (name != NULL);
758 GST_OBJECT_LOCK (provider);
760 g_list_find_custom (provider->priv->hidden_providers, name,
761 (GCompareFunc) g_strcmp0);
763 unhidden_name = find->data;
764 provider->priv->hidden_providers =
765 g_list_delete_link (provider->priv->hidden_providers, find);
767 GST_OBJECT_UNLOCK (provider);
770 g_signal_emit (provider,
771 gst_device_provider_signals[PROVIDER_UNHIDDEN], 0, unhidden_name);
772 g_free (unhidden_name);
777 * gst_device_provider_device_changed:
778 * @device: (transfer none): the new version of @changed_device
779 * @changed_device: (transfer floating): the old version of the device that has been udpated
781 * This function is used when @changed_device was modified into its new form
782 * @device. This will post a `DEVICE_CHANGED` message on the bus to let
783 * the application know that the device was modified. #GstDevice is immutable
784 * for MT. safety purposes so this is an "atomic" way of letting the application
785 * know when a device was modified.
790 gst_device_provider_device_changed (GstDeviceProvider * provider,
791 GstDevice * device, GstDevice * changed_device)
796 g_return_if_fail (GST_IS_DEVICE_PROVIDER (provider));
797 g_return_if_fail (GST_IS_DEVICE (device));
798 g_return_if_fail (GST_IS_DEVICE (changed_device));
800 GST_OBJECT_LOCK (provider);
801 dev_lst = g_list_find (provider->devices, changed_device);
803 GST_ERROR_OBJECT (provider,
804 "Trying to update a device we do not have in our own list!");
806 GST_OBJECT_UNLOCK (provider);
810 if (!gst_object_set_parent (GST_OBJECT (device), GST_OBJECT (provider))) {
811 GST_OBJECT_UNLOCK (provider);
812 GST_WARNING_OBJECT (provider, "Could not parent device %p to provider,"
813 " it already has a parent", device);
816 dev_lst->data = device;
817 GST_OBJECT_UNLOCK (provider);
820 gst_message_new_device_changed (GST_OBJECT (provider), device,
822 gst_bus_post (provider->priv->bus, message);
823 gst_object_unparent (GST_OBJECT (changed_device));