2 * Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
4 * gstdevice.c: Device discovery
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.
25 * @short_description: Object representing a device
26 * @see_also: #GstDeviceProvider
28 * #GstDevice are objects representing a device, they contain
29 * relevant metadata about the device, such as its class and the #GstCaps
30 * representing the media types it can produce or handle.
32 * #GstDevice are created by #GstDeviceProvider objects which can be
33 * aggregated by #GstDeviceMonitor objects.
42 #include "gst_private.h"
44 #include "gstdevice.h"
48 PROP_DISPLAY_NAME = 1,
60 struct _GstDevicePrivate
65 GstStructure *properties;
69 static guint signals[LAST_SIGNAL];
71 G_DEFINE_ABSTRACT_TYPE (GstDevice, gst_device, GST_TYPE_OBJECT);
73 static void gst_device_get_property (GObject * object, guint property_id,
74 GValue * value, GParamSpec * pspec);
75 static void gst_device_set_property (GObject * object, guint property_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_device_finalize (GObject * object);
81 gst_device_class_init (GstDeviceClass * klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 g_type_class_add_private (klass, sizeof (GstDevicePrivate));
87 object_class->get_property = gst_device_get_property;
88 object_class->set_property = gst_device_set_property;
89 object_class->finalize = gst_device_finalize;
91 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
92 g_param_spec_string ("display-name", "Display Name",
93 "The user-friendly name of the device", "",
94 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
95 g_object_class_install_property (object_class, PROP_CAPS,
96 g_param_spec_boxed ("caps", "Device Caps",
97 "The possible caps of a device", GST_TYPE_CAPS,
98 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
99 g_object_class_install_property (object_class, PROP_DEVICE_CLASS,
100 g_param_spec_string ("device-class", "Device Class",
101 "The Class of the device", "",
102 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
103 g_object_class_install_property (object_class, PROP_PROPERTIES,
104 g_param_spec_boxed ("properties", "Properties",
105 "The extra properties of the device", GST_TYPE_STRUCTURE,
106 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
108 signals[REMOVED] = g_signal_new ("removed", G_TYPE_FROM_CLASS (klass),
109 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
113 gst_device_init (GstDevice * device)
115 device->priv = G_TYPE_INSTANCE_GET_PRIVATE (device, GST_TYPE_DEVICE,
120 gst_device_finalize (GObject * object)
122 GstDevice *device = GST_DEVICE (object);
124 gst_caps_replace (&device->priv->caps, NULL);
126 if (device->priv->properties)
127 gst_structure_free (device->priv->properties);
128 g_free (device->priv->display_name);
129 g_free (device->priv->device_class);
131 G_OBJECT_CLASS (gst_device_parent_class)->finalize (object);
135 gst_device_get_property (GObject * object, guint prop_id,
136 GValue * value, GParamSpec * pspec)
138 GstDevice *gstdevice;
140 gstdevice = GST_DEVICE_CAST (object);
143 case PROP_DISPLAY_NAME:
144 g_value_take_string (value, gst_device_get_display_name (gstdevice));
147 if (gstdevice->priv->caps)
148 g_value_take_boxed (value, gst_device_get_caps (gstdevice));
150 case PROP_DEVICE_CLASS:
151 g_value_take_string (value, gst_device_get_device_class (gstdevice));
153 case PROP_PROPERTIES:
154 if (gstdevice->priv->properties)
155 g_value_take_boxed (value, gst_device_get_properties (gstdevice));
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gst_device_set_property (GObject * object, guint prop_id,
166 const GValue * value, GParamSpec * pspec)
168 GstDevice *gstdevice;
170 gstdevice = GST_DEVICE_CAST (object);
173 case PROP_DISPLAY_NAME:
174 gstdevice->priv->display_name = g_value_dup_string (value);
177 gst_caps_replace (&gstdevice->priv->caps, g_value_get_boxed (value));
179 case PROP_DEVICE_CLASS:
180 gstdevice->priv->device_class = g_value_dup_string (value);
182 case PROP_PROPERTIES:
183 if (gstdevice->priv->properties)
184 gst_structure_free (gstdevice->priv->properties);
185 gstdevice->priv->properties = g_value_dup_boxed (value);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 * gst_device_create_element:
195 * @device: a #GstDevice
196 * @name: (allow-none): name of new element, or %NULL to automatically
197 * create a unique name.
199 * Creates the element with all of the required parameters set to use
202 * Returns: (transfer full) (nullable): a new #GstElement configured to use
208 gst_device_create_element (GstDevice * device, const gchar * name)
210 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
212 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
214 if (klass->create_element)
215 return klass->create_element (device, name);
221 * gst_device_get_caps:
222 * @device: a #GstDevice
224 * Getter for the #GstCaps that this device supports.
226 * Returns: (nullable): The #GstCaps supported by this device. Unref with
227 * gst_caps_unref() when done.
232 gst_device_get_caps (GstDevice * device)
234 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
236 if (device->priv->caps)
237 return gst_caps_ref (device->priv->caps);
243 * gst_device_get_display_name:
244 * @device: a #GstDevice
246 * Gets the user-friendly name of the device.
248 * Returns: The device name. Free with g_free() after use.
253 gst_device_get_display_name (GstDevice * device)
255 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
258 g_strdup (device->priv->display_name ? device->priv->display_name : "");
262 * gst_device_get_device_class:
263 * @device: a #GstDevice
265 * Gets the "class" of a device. This is a "/" separated list of
266 * classes that represent this device. They are a subset of the
267 * classes of the #GstDeviceProvider that produced this device.
269 * Returns: The device class. Free with g_free() after use.
274 gst_device_get_device_class (GstDevice * device)
276 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
278 if (device->priv->device_class != NULL)
279 return g_strdup (device->priv->device_class);
281 return g_strdup ("");
285 * gst_device_get_properties:
286 * @device: a #GstDevice
288 * Gets the extra properties of a device.
290 * Returns: (nullable): The extra properties or %NULL when there are none.
291 * Free with gst_structure_free() after use.
296 gst_device_get_properties (GstDevice * device)
298 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
300 if (device->priv->properties != NULL)
301 return gst_structure_copy (device->priv->properties);
307 * gst_device_reconfigure_element:
308 * @device: a #GstDevice
309 * @element: a #GstElement
311 * Tries to reconfigure an existing element to use the device. If this
312 * function fails, then one must destroy the element and create a new one
313 * using gst_device_create_element().
315 * Note: This should only be implemented for elements can change their
316 * device in the PLAYING state.
318 * Returns: %TRUE if the element could be reconfigured to use this device,
324 gst_device_reconfigure_element (GstDevice * device, GstElement * element)
326 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
328 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
330 if (klass->reconfigure_element)
331 return klass->reconfigure_element (device, element);
337 * gst_device_has_classesv:
338 * @device: a #GstDevice
339 * @classes: (array zero-terminated=1): a %NULL terminated array of classes
340 * to match, only match if all classes are matched
342 * Check if @factory matches all of the given classes
344 * Returns: %TRUE if @device matches.
349 gst_device_has_classesv (GstDevice * device, gchar ** classes)
351 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
356 for (; classes[0]; classes++) {
357 const gchar *klass = classes[0];
364 found = strstr (device->priv->device_class, klass);
368 if (found != device->priv->device_class && *(found - 1) != '/')
371 len = strlen (klass);
372 if (found[len] != 0 && found[len] != '/')
380 * gst_device_has_classes:
381 * @device: a #GstDevice
382 * @classes: a "/"-separated list of device classes to match, only match if
383 * all classes are matched
385 * Check if @device matches all of the given classes
387 * Returns: %TRUE if @device matches.
392 gst_device_has_classes (GstDevice * device, const gchar * classes)
397 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
402 classesv = g_strsplit (classes, "/", 0);
404 res = gst_device_has_classesv (device, classesv);
406 g_strfreev (classesv);