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.
24 * @short_description: Object representing a device
25 * @see_also: #GstDeviceMonitor
27 * #GstDevice are objects representing a device, they contain
28 * relevant metadata about the device, such as its class and the #GstCaps
29 * representing the media types it can produce or handle.
31 * #GstDevice are created by #GstDeviceMonitor objects which can be
32 * aggregated by #GstGlobalDeviceMonitor objects.
41 #include "gst_private.h"
43 #include "gstdevice.h"
47 PROP_DISPLAY_NAME = 1,
58 struct _GstDevicePrivate
66 static guint signals[LAST_SIGNAL];
68 G_DEFINE_ABSTRACT_TYPE (GstDevice, gst_device, GST_TYPE_OBJECT);
70 static void gst_device_get_property (GObject * object, guint property_id,
71 GValue * value, GParamSpec * pspec);
72 static void gst_device_set_property (GObject * object, guint property_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_device_finalize (GObject * object);
78 gst_device_class_init (GstDeviceClass * klass)
80 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 g_type_class_add_private (klass, sizeof (GstDevicePrivate));
84 object_class->get_property = gst_device_get_property;
85 object_class->set_property = gst_device_set_property;
86 object_class->finalize = gst_device_finalize;
88 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
89 g_param_spec_string ("display-name", "Display Name",
90 "The user-friendly name of the device", "",
91 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
92 g_object_class_install_property (object_class, PROP_CAPS,
93 g_param_spec_boxed ("caps", "Device Caps",
94 "The possible caps of a device", GST_TYPE_CAPS,
95 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
96 g_object_class_install_property (object_class, PROP_KLASS,
97 g_param_spec_string ("klass", "Device Class",
98 "The Class of the device", "",
99 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
101 signals[REMOVED] = g_signal_new ("removed", G_TYPE_FROM_CLASS (klass),
102 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
106 gst_device_init (GstDevice * device)
108 device->priv = G_TYPE_INSTANCE_GET_PRIVATE (device, GST_TYPE_DEVICE,
113 gst_device_finalize (GObject * object)
115 GstDevice *device = GST_DEVICE (object);
117 gst_caps_replace (&device->priv->caps, NULL);
119 g_free (device->priv->display_name);
120 g_free (device->priv->klass);
122 G_OBJECT_CLASS (gst_device_parent_class)->finalize (object);
126 gst_device_get_property (GObject * object, guint prop_id,
127 GValue * value, GParamSpec * pspec)
129 GstDevice *gstdevice;
131 gstdevice = GST_DEVICE_CAST (object);
134 case PROP_DISPLAY_NAME:
135 g_value_take_string (value, gst_device_get_display_name (gstdevice));
138 if (gstdevice->priv->caps)
139 g_value_take_boxed (value, gst_device_get_caps (gstdevice));
142 g_value_take_string (value, gst_device_get_klass (gstdevice));
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 gst_device_set_property (GObject * object, guint prop_id,
153 const GValue * value, GParamSpec * pspec)
155 GstDevice *gstdevice;
157 gstdevice = GST_DEVICE_CAST (object);
160 case PROP_DISPLAY_NAME:
161 gstdevice->priv->display_name = g_value_dup_string (value);
164 gst_caps_replace (&gstdevice->priv->caps, g_value_get_boxed (value));
167 gstdevice->priv->klass = g_value_dup_string (value);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 * gst_device_create_element:
177 * @device: a #GstDevice
178 * @name: (allow-none): name of new element, or %NULL to automatically
179 * create a unique name.
181 * Creates the element with all of the required paramaters set to use
184 * Returns: (transfer full): a new #GstElement configured to use this device
189 gst_device_create_element (GstDevice * device, const gchar * name)
191 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
193 if (klass->create_element)
194 return klass->create_element (device, name);
200 * gst_device_get_caps:
201 * @device: a #GstDevice
203 * Getter for the #GstCaps that this device supports.
205 * Returns: The #GstCaps supported by this device. Unref with
206 * gst_caps_unref() when done.
211 gst_device_get_caps (GstDevice * device)
213 if (device->priv->caps)
214 return gst_caps_ref (device->priv->caps);
220 * gst_device_get_display_name:
221 * @device: a #GstDevice
223 * Gets the user-friendly name of the device.
225 * Returns: The device name. Free with g_free() after use.
230 gst_device_get_display_name (GstDevice * device)
233 g_strdup (device->priv->display_name ? device->priv->display_name : "");
237 * gst_device_get_klass:
238 * @device: a #GstDevice
240 * Gets the "class" of a device. This is a "/" separated list of
241 * classes that represent this device. They are a subset of the
242 * classes of the #GstDeviceMonitor that produced this device.
244 * Returns: The device class. Free with g_free() after use.
249 gst_device_get_klass (GstDevice * device)
251 return g_strdup (device->priv->klass ? device->priv->klass : "");
255 * gst_device_reconfigure_element:
256 * @device: a #GstDevice
257 * @element: a #GstElement
259 * Tries to reconfigure an existing element to use the device. If this
260 * function fails, then one must destroy the element and create a new one
261 * using gst_device_create_element().
263 * Note: This should only be implemented for elements can change their
264 * device in the PLAYING state.
266 * Returns: %TRUE if the element could be reconfigured to use this device,
272 gst_device_reconfigure_element (GstDevice * device, GstElement * element)
274 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
276 if (klass->reconfigure_element)
277 return klass->reconfigure_element (device, element);
283 * gst_device_has_classesv:
284 * @device: a #GstDevice
285 * @classes: (array zero-terminated=1): a %NULL terminated array of klasses to match, only match if all
286 * classes are matched
288 * Check if @factory matches all of the given classes
290 * Returns: %TRUE if @device matches.
295 gst_device_has_classesv (GstDevice * device, gchar ** classes)
297 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
300 for (; classes[0]; classes++) {
304 if (classes[0] == '\0')
307 found = strstr (device->priv->klass, classes[0]);
311 if (found != device->priv->klass && *(found - 1) != '/')
314 len = strlen (classes[0]);
315 if (found[len] != 0 && found[len] != '/')
323 * gst_device_has_classes:
324 * @device: a #GstDevice
325 * @classes: a "/" separate list of klasses to match, only match if all classes
328 * Check if @device matches all of the given classes
330 * Returns: %TRUE if @device matches.
335 gst_device_has_classes (GstDevice * device, const gchar * classes)
340 classesv = g_strsplit (classes, "/", 0);
342 res = gst_device_has_classesv (device, classesv);
344 g_strfreev (classesv);