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.
26 #include "gst_private.h"
28 #include "gstdevice.h"
32 PROP_DISPLAY_NAME = 1,
43 struct _GstDevicePrivate
51 static guint signals[LAST_SIGNAL];
53 G_DEFINE_ABSTRACT_TYPE (GstDevice, gst_device, GST_TYPE_OBJECT);
55 static void gst_device_get_property (GObject * object, guint property_id,
56 GValue * value, GParamSpec * pspec);
57 static void gst_device_set_property (GObject * object, guint property_id,
58 const GValue * value, GParamSpec * pspec);
59 static void gst_device_finalize (GObject * object);
63 gst_device_class_init (GstDeviceClass * klass)
65 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67 g_type_class_add_private (klass, sizeof (GstDevicePrivate));
69 object_class->get_property = gst_device_get_property;
70 object_class->set_property = gst_device_set_property;
71 object_class->finalize = gst_device_finalize;
73 g_object_class_install_property (object_class, PROP_DISPLAY_NAME,
74 g_param_spec_string ("display-name", "Display Name",
75 "The user-friendly name of the device", "",
76 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
77 g_object_class_install_property (object_class, PROP_CAPS,
78 g_param_spec_boxed ("caps", "Device Caps",
79 "The possible caps of a device", GST_TYPE_CAPS,
80 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
81 g_object_class_install_property (object_class, PROP_KLASS,
82 g_param_spec_string ("klass", "Device Class",
83 "The Class of the device", "",
84 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
86 signals[REMOVED] = g_signal_new ("removed", G_TYPE_FROM_CLASS (klass),
87 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
91 gst_device_init (GstDevice * device)
93 device->priv = G_TYPE_INSTANCE_GET_PRIVATE (device, GST_TYPE_DEVICE,
98 gst_device_finalize (GObject * object)
100 GstDevice *device = GST_DEVICE (object);
102 gst_caps_replace (&device->priv->caps, NULL);
104 g_free (device->priv->display_name);
105 g_free (device->priv->klass);
107 G_OBJECT_CLASS (gst_device_parent_class)->finalize (object);
111 gst_device_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec)
114 GstDevice *gstdevice;
116 gstdevice = GST_DEVICE_CAST (object);
119 case PROP_DISPLAY_NAME:
120 g_value_take_string (value, gst_device_get_display_name (gstdevice));
123 if (gstdevice->priv->caps)
124 g_value_take_boxed (value, gst_device_get_caps (gstdevice));
127 g_value_take_string (value, gst_device_get_klass (gstdevice));
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137 gst_device_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec)
140 GstDevice *gstdevice;
142 gstdevice = GST_DEVICE_CAST (object);
145 case PROP_DISPLAY_NAME:
146 gstdevice->priv->display_name = g_value_dup_string (value);
149 gst_caps_replace (&gstdevice->priv->caps, g_value_get_boxed (value));
152 gstdevice->priv->klass = g_value_dup_string (value);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 * gst_device_create_element:
162 * @device: a #GstDevice
163 * @name: (allow-none): name of new element, or NULL to automatically
164 * create a unique name.
166 * Returns: (transfer full): a new #GstElement configured to use this device
171 gst_device_create_element (GstDevice * device, const gchar * name)
173 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
175 if (klass->create_element)
176 return klass->create_element (device, name);
182 * gst_device_get_caps:
183 * @device: a #GstDevice
185 * Getter for the #GstCaps that this device supports.
187 * Returns: The #GstCaps supported by this device. Unref with
188 * gst_caps_unref() when done.
193 gst_device_get_caps (GstDevice * device)
195 if (device->priv->caps)
196 return gst_caps_ref (device->priv->caps);
202 * gst_device_get_display_name:
203 * @device: a #GstDevice
205 * Gets the user-friendly name of the device.
207 * Returns: The device name. Free with g_free() after use.
212 gst_device_get_display_name (GstDevice * device)
215 g_strdup (device->priv->display_name ? device->priv->display_name : "");
219 * gst_device_get_klass:
220 * @device: a #GstDevice
222 * Gets the "class" of a device. This is a "/" separated list of
223 * classes that represent this device. They are a subset of the
224 * classes of the #GstDeviceMonitor that produced this device.
226 * Returns: The device class. Free with g_free() after use.
231 gst_device_get_klass (GstDevice * device)
233 return g_strdup (device->priv->klass ? device->priv->klass : "");
237 * gst_device_reconfigure_element:
238 * @device: a #GstDevice
239 * @element: a #GstElement
241 * Tries to reconfigure an existing element to use the device. If this
242 * function fails, then one must destroy the element and create a new one
243 * using gst_device_create_element().
245 * Note: This should only be implemented for elements can change their
246 * device in the PLAYING state.
248 * Returns: %TRUE if the element could be reconfigured to use this device,
254 gst_device_reconfigure_element (GstDevice * device, GstElement * element)
256 GstDeviceClass *klass = GST_DEVICE_GET_CLASS (device);
258 if (klass->reconfigure_element)
259 return klass->reconfigure_element (device, element);
265 * gst_device_has_classesv:
266 * @device: a #GstDevice
267 * @classes: a %NULL terminated array of klasses to match, only match if all
268 * classes are matched
270 * Check if @factory matches all of the given classes
272 * Returns: %TRUE if @device matches.
277 gst_device_has_classesv (GstDevice * device, gchar ** classes)
279 g_return_val_if_fail (GST_IS_DEVICE (device), FALSE);
282 for (; classes[0]; classes++) {
286 if (classes[0] == '\0')
289 found = strstr (device->priv->klass, classes[0]);
293 if (found != device->priv->klass && *(found - 1) != '/')
296 len = strlen (classes[0]);
297 if (found[len] != 0 && found[len] != '/')
305 * gst_device_has_classes:
306 * @device: a #GstDevice
307 * @classes: a "/" separate list of klasses to match, only match if all classes
310 * Check if @device matches all of the given classes
312 * Returns: %TRUE if @device matches.
317 gst_device_has_classes (GstDevice * device, const gchar * classes)
322 classesv = g_strsplit (classes, "/", 0);
324 res = gst_device_has_classesv (device, classesv);
326 g_strfreev (classesv);