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_WITH_PRIVATE (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 object_class->get_property = gst_device_get_property;
86 object_class->set_property = gst_device_set_property;
87 object_class->finalize = gst_device_finalize;
89 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
90 g_param_spec_string ("display-name", "Display Name",
91 "The user-friendly name of the device", "",
92 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
93 g_object_class_install_property (object_class, PROP_CAPS,
94 g_param_spec_boxed ("caps", "Device Caps",
95 "The possible caps of a device", GST_TYPE_CAPS,
96 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
97 g_object_class_install_property (object_class, PROP_DEVICE_CLASS,
98 g_param_spec_string ("device-class", "Device Class",
99 "The Class of the device", "",
100 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
101 g_object_class_install_property (object_class, PROP_PROPERTIES,
102 g_param_spec_boxed ("properties", "Properties",
103 "The extra properties of the device", GST_TYPE_STRUCTURE,
104 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
106 signals[REMOVED] = g_signal_new ("removed", G_TYPE_FROM_CLASS (klass),
107 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
111 gst_device_init (GstDevice * device)
113 device->priv = gst_device_get_instance_private (device);
117 gst_device_finalize (GObject * object)
119 GstDevice *device = GST_DEVICE (object);
121 gst_caps_replace (&device->priv->caps, NULL);
123 if (device->priv->properties)
124 gst_structure_free (device->priv->properties);
125 g_free (device->priv->display_name);
126 g_free (device->priv->device_class);
128 G_OBJECT_CLASS (gst_device_parent_class)->finalize (object);
132 gst_device_get_property (GObject * object, guint prop_id,
133 GValue * value, GParamSpec * pspec)
135 GstDevice *gstdevice;
137 gstdevice = GST_DEVICE_CAST (object);
140 case PROP_DISPLAY_NAME:
141 g_value_take_string (value, gst_device_get_display_name (gstdevice));
144 if (gstdevice->priv->caps)
145 g_value_take_boxed (value, gst_device_get_caps (gstdevice));
147 case PROP_DEVICE_CLASS:
148 g_value_take_string (value, gst_device_get_device_class (gstdevice));
150 case PROP_PROPERTIES:
151 if (gstdevice->priv->properties)
152 g_value_take_boxed (value, gst_device_get_properties (gstdevice));
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 gst_device_set_property (GObject * object, guint prop_id,
163 const GValue * value, GParamSpec * pspec)
165 GstDevice *gstdevice;
167 gstdevice = GST_DEVICE_CAST (object);
170 case PROP_DISPLAY_NAME:
171 gstdevice->priv->display_name = g_value_dup_string (value);
174 gst_caps_replace (&gstdevice->priv->caps, g_value_get_boxed (value));
176 case PROP_DEVICE_CLASS:
177 gstdevice->priv->device_class = g_value_dup_string (value);
179 case PROP_PROPERTIES:
180 if (gstdevice->priv->properties)
181 gst_structure_free (gstdevice->priv->properties);
182 gstdevice->priv->properties = g_value_dup_boxed (value);
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 * gst_device_create_element:
192 * @device: a #GstDevice
193 * @name: (allow-none): name of new element, or %NULL to automatically
194 * create a unique name.
196 * Creates the element with all of the required parameters set to use
199 * Returns: (transfer full) (nullable): a new #GstElement configured to use
205 gst_device_create_element (GstDevice * device, const gchar * name)
207 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
209 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
211 if (klass->create_element)
212 return klass->create_element (device, name);
218 * gst_device_get_caps:
219 * @device: a #GstDevice
221 * Getter for the #GstCaps that this device supports.
223 * Returns: (nullable): The #GstCaps supported by this device. Unref with
224 * gst_caps_unref() when done.
229 gst_device_get_caps (GstDevice * device)
231 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
233 if (device->priv->caps)
234 return gst_caps_ref (device->priv->caps);
240 * gst_device_get_display_name:
241 * @device: a #GstDevice
243 * Gets the user-friendly name of the device.
245 * Returns: The device name. Free with g_free() after use.
250 gst_device_get_display_name (GstDevice * device)
252 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
255 g_strdup (device->priv->display_name ? device->priv->display_name : "");
259 * gst_device_get_device_class:
260 * @device: a #GstDevice
262 * Gets the "class" of a device. This is a "/" separated list of
263 * classes that represent this device. They are a subset of the
264 * classes of the #GstDeviceProvider that produced this device.
266 * Returns: The device class. Free with g_free() after use.
271 gst_device_get_device_class (GstDevice * device)
273 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
275 if (device->priv->device_class != NULL)
276 return g_strdup (device->priv->device_class);
278 return g_strdup ("");
282 * gst_device_get_properties:
283 * @device: a #GstDevice
285 * Gets the extra properties of a device.
287 * Returns: (nullable): The extra properties or %NULL when there are none.
288 * Free with gst_structure_free() after use.
293 gst_device_get_properties (GstDevice * device)
295 g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
297 if (device->priv->properties != NULL)
298 return gst_structure_copy (device->priv->properties);
304 * gst_device_reconfigure_element:
305 * @device: a #GstDevice
306 * @element: a #GstElement
308 * Tries to reconfigure an existing element to use the device. If this
309 * function fails, then one must destroy the element and create a new one
310 * using gst_device_create_element().
312 * Note: This should only be implemented for elements can change their
313 * device in the PLAYING state.
315 * Returns: %TRUE if the element could be reconfigured to use this device,
321 gst_device_reconfigure_element (GstDevice * device, GstElement * element)
323 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
325 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
327 if (klass->reconfigure_element)
328 return klass->reconfigure_element (device, element);
334 * gst_device_has_classesv:
335 * @device: a #GstDevice
336 * @classes: (array zero-terminated=1): a %NULL terminated array of classes
337 * to match, only match if all classes are matched
339 * Check if @factory matches all of the given classes
341 * Returns: %TRUE if @device matches.
346 gst_device_has_classesv (GstDevice * device, gchar ** classes)
348 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
353 for (; classes[0]; classes++) {
354 const gchar *klass = classes[0];
361 found = strstr (device->priv->device_class, klass);
365 if (found != device->priv->device_class && *(found - 1) != '/')
368 len = strlen (klass);
369 if (found[len] != 0 && found[len] != '/')
377 * gst_device_has_classes:
378 * @device: a #GstDevice
379 * @classes: a "/"-separated list of device classes to match, only match if
380 * all classes are matched
382 * Check if @device matches all of the given classes
384 * Returns: %TRUE if @device matches.
389 gst_device_has_classes (GstDevice * device, const gchar * classes)
394 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
399 classesv = g_strsplit (classes, "/", 0);
401 res = gst_device_has_classesv (device, classesv);
403 g_strfreev (classesv);