2 * Copyright (C) 2012 Olivier Crete <olivier.crete@collabora.com>
4 * gstv4l2deviceprovider.c: V4l2 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.
26 #include "gstv4l2deviceprovider.h"
33 #include "gstv4l2object.h"
34 #include "v4l2-utils.h"
37 #include <gudev/gudev.h>
40 static GstV4l2Device *gst_v4l2_device_new (const gchar * device_path,
41 const gchar * device_name, GstCaps * caps, GstV4l2DeviceType type,
42 GstStructure * props);
45 G_DEFINE_TYPE (GstV4l2DeviceProvider, gst_v4l2_device_provider,
46 GST_TYPE_DEVICE_PROVIDER);
48 static void gst_v4l2_device_provider_finalize (GObject * object);
49 static GList *gst_v4l2_device_provider_probe (GstDeviceProvider * provider);
52 static gboolean gst_v4l2_device_provider_start (GstDeviceProvider * provider);
53 static void gst_v4l2_device_provider_stop (GstDeviceProvider * provider);
58 gst_v4l2_device_provider_class_init (GstV4l2DeviceProviderClass * klass)
60 GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
61 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
63 dm_class->probe = gst_v4l2_device_provider_probe;
66 dm_class->start = gst_v4l2_device_provider_start;
67 dm_class->stop = gst_v4l2_device_provider_stop;
70 gobject_class->finalize = gst_v4l2_device_provider_finalize;
72 gst_device_provider_class_set_static_metadata (dm_class,
73 "Video (video4linux2) Device Provider", "Source/Sink/Video",
74 "List and monitor video4linux2 source and sink devices",
75 "Olivier Crete <olivier.crete@collabora.com>");
79 gst_v4l2_device_provider_init (GstV4l2DeviceProvider * provider)
82 g_cond_init (&provider->started_cond);
87 gst_v4l2_device_provider_finalize (GObject * object)
90 GstV4l2DeviceProvider *provider = GST_V4L2_DEVICE_PROVIDER (object);
92 g_cond_clear (&provider->started_cond);
95 G_OBJECT_CLASS (gst_v4l2_device_provider_parent_class)->finalize (object);
98 static GstV4l2Device *
99 gst_v4l2_device_provider_probe_device (GstV4l2DeviceProvider * provider,
100 const gchar * device_path, const gchar * device_name, GstStructure * props)
102 GstV4l2Object *v4l2obj = NULL;
104 GstV4l2Device *device = NULL;
106 GstV4l2DeviceType type = GST_V4L2_DEVICE_TYPE_INVALID;
108 g_return_val_if_fail (props != NULL, NULL);
110 if (stat (device_path, &st) == -1)
113 if (!S_ISCHR (st.st_mode))
116 v4l2obj = gst_v4l2_object_new (NULL, GST_OBJECT (provider),
117 V4L2_BUF_TYPE_VIDEO_CAPTURE, device_path, NULL, NULL, NULL);
119 if (!gst_v4l2_open (v4l2obj))
122 gst_structure_set (props, "device.api", G_TYPE_STRING, "v4l2", NULL);
123 gst_structure_set (props, "device.path", G_TYPE_STRING, device_path, NULL);
125 gst_structure_set (props, "v4l2.device.driver", G_TYPE_STRING,
126 v4l2obj->vcap.driver, NULL);
127 gst_structure_set (props, "v4l2.device.card", G_TYPE_STRING,
128 v4l2obj->vcap.card, NULL);
129 gst_structure_set (props, "v4l2.device.bus_info", G_TYPE_STRING,
130 v4l2obj->vcap.bus_info, NULL);
131 gst_structure_set (props, "v4l2.device.version", G_TYPE_UINT,
132 v4l2obj->vcap.version, NULL);
133 gst_structure_set (props, "v4l2.device.capabilities", G_TYPE_UINT,
134 v4l2obj->vcap.capabilities, NULL);
135 gst_structure_set (props, "v4l2.device.device_caps", G_TYPE_UINT,
136 v4l2obj->vcap.device_caps, NULL);
138 if (v4l2obj->device_caps &
139 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) {
140 /* We ignore touch sensing devices; those are't really video */
141 if (v4l2obj->device_caps & V4L2_CAP_TOUCH)
144 type = GST_V4L2_DEVICE_TYPE_SOURCE;
147 if (v4l2obj->device_caps &
148 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)) {
149 /* We ignore M2M devices that are both capture and output for now
150 * The provider is not for them */
151 if (type != GST_V4L2_DEVICE_TYPE_INVALID)
154 type = GST_V4L2_DEVICE_TYPE_SINK;
156 /* We have opened as a capture as we didn't know, now that know,
158 if (v4l2obj->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
159 v4l2obj->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
161 v4l2obj->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
164 if (type == GST_V4L2_DEVICE_TYPE_INVALID)
167 caps = gst_v4l2_object_get_caps (v4l2obj, NULL);
171 if (gst_caps_is_empty (caps)) {
172 gst_caps_unref (caps);
176 device = gst_v4l2_device_new (device_path,
177 device_name ? device_name : (gchar *) v4l2obj->vcap.card, caps, type,
179 gst_caps_unref (caps);
183 gst_v4l2_close (v4l2obj);
188 gst_v4l2_object_destroy (v4l2obj);
191 gst_structure_free (props);
198 gst_v4l2_device_provider_probe (GstDeviceProvider * provider)
200 GstV4l2DeviceProvider *self = GST_V4L2_DEVICE_PROVIDER (provider);
202 GList *devices = NULL;
204 it = gst_v4l2_iterator_new ();
206 while (gst_v4l2_iterator_next (it)) {
208 GstV4l2Device *device;
210 props = gst_structure_new ("v4l2-proplist", "device.path", G_TYPE_STRING,
211 it->device_path, "udev-probed", G_TYPE_BOOLEAN, FALSE, NULL);
212 device = gst_v4l2_device_provider_probe_device (self, it->device_path, NULL,
216 gst_object_ref_sink (device);
217 devices = g_list_prepend (devices, device);
221 gst_v4l2_iterator_free (it);
229 gst_v4l2_device_provider_device_from_udev (GstV4l2DeviceProvider * provider,
230 GUdevDevice * udev_device)
232 GstV4l2Device *gstdev;
233 const gchar *device_path = g_udev_device_get_device_file (udev_device);
234 const gchar *device_name, *str;
237 props = gst_structure_new ("v4l2deviceprovider", "udev-probed",
238 G_TYPE_BOOLEAN, TRUE, NULL);
240 str = g_udev_device_get_property (udev_device, "ID_PATH");
241 if (!(str && *str)) {
242 str = g_udev_device_get_sysfs_path (udev_device);
245 gst_structure_set (props, "device.bus_path", G_TYPE_STRING, str, NULL);
247 if ((str = g_udev_device_get_sysfs_path (udev_device)) && *str)
248 gst_structure_set (props, "sysfs.path", G_TYPE_STRING, str, NULL);
250 if ((str = g_udev_device_get_property (udev_device, "ID_ID")) && *str)
251 gst_structure_set (props, "udev.id", G_TYPE_STRING, str, NULL);
253 if ((str = g_udev_device_get_property (udev_device, "ID_BUS")) && *str)
254 gst_structure_set (props, "device.bus", G_TYPE_STRING, str, NULL);
256 if ((str = g_udev_device_get_property (udev_device, "SUBSYSTEM")) && *str)
257 gst_structure_set (props, "device.subsystem", G_TYPE_STRING, str, NULL);
259 if ((str = g_udev_device_get_property (udev_device, "ID_VENDOR_ID")) && *str)
260 gst_structure_set (props, "device.vendor.id", G_TYPE_STRING, str, NULL);
262 str = g_udev_device_get_property (udev_device, "ID_VENDOR_FROM_DATABASE");
263 if (!(str && *str)) {
264 str = g_udev_device_get_property (udev_device, "ID_VENDOR_ENC");
265 if (!(str && *str)) {
266 str = g_udev_device_get_property (udev_device, "ID_VENDOR");
270 gst_structure_set (props, "device.vendor.name", G_TYPE_STRING, str, NULL);
272 if ((str = g_udev_device_get_property (udev_device, "ID_MODEL_ID")) && *str)
273 gst_structure_set (props, "device.product.id", G_TYPE_STRING, str, NULL);
275 device_name = g_udev_device_get_property (udev_device, "ID_V4L_PRODUCT");
276 if (!(device_name && *device_name)) {
278 g_udev_device_get_property (udev_device, "ID_MODEL_FROM_DATABASE");
279 if (!(device_name && *device_name)) {
280 device_name = g_udev_device_get_property (udev_device, "ID_MODEL_ENC");
281 if (!(device_name && *device_name)) {
282 device_name = g_udev_device_get_property (udev_device, "ID_MODEL");
286 if (device_name && *device_name)
287 gst_structure_set (props, "device.product.name", G_TYPE_STRING, device_name,
290 if ((str = g_udev_device_get_property (udev_device, "ID_SERIAL")) && *str)
291 gst_structure_set (props, "device.serial", G_TYPE_STRING, str, NULL);
293 if ((str = g_udev_device_get_property (udev_device, "ID_V4L_CAPABILITIES"))
295 gst_structure_set (props, "device.capabilities", G_TYPE_STRING, str, NULL);
297 gstdev = gst_v4l2_device_provider_probe_device (provider, device_path,
301 gstdev->syspath = g_strdup (g_udev_device_get_sysfs_path (udev_device));
303 return GST_DEVICE (gstdev);
307 uevent_cb (GUdevClient * client, const gchar * action, GUdevDevice * device,
308 GstV4l2DeviceProvider * self)
310 GstDeviceProvider *provider = GST_DEVICE_PROVIDER (self);
312 /* Not V4L2, ignoring */
313 if (g_udev_device_get_property_as_int (device, "ID_V4L_VERSION") != 2)
316 if (!strcmp (action, "add")) {
317 GstDevice *gstdev = NULL;
319 gstdev = gst_v4l2_device_provider_device_from_udev (self, device);
322 gst_device_provider_device_add (provider, gstdev);
323 } else if (!strcmp (action, "remove")) {
324 GstV4l2Device *gstdev = NULL;
327 GST_OBJECT_LOCK (self);
328 for (item = provider->devices; item; item = item->next) {
331 if (!strcmp (gstdev->syspath, g_udev_device_get_sysfs_path (device))) {
332 gst_object_ref (gstdev);
338 GST_OBJECT_UNLOCK (provider);
341 gst_device_provider_device_remove (provider, GST_DEVICE (gstdev));
342 g_object_unref (gstdev);
345 GST_WARNING ("Unhandled action %s", action);
350 provider_thread (gpointer data)
352 GstV4l2DeviceProvider *provider = data;
353 GMainContext *context = NULL;
354 GMainLoop *loop = NULL;
357 static const gchar *subsystems[] = { "video4linux", NULL };
359 GST_OBJECT_LOCK (provider);
360 if (provider->context)
361 context = g_main_context_ref (provider->context);
363 loop = g_main_loop_ref (provider->loop);
365 if (context == NULL || loop == NULL) {
366 provider->started = TRUE;
367 g_cond_broadcast (&provider->started_cond);
368 GST_OBJECT_UNLOCK (provider);
371 GST_OBJECT_UNLOCK (provider);
373 g_main_context_push_thread_default (context);
375 client = g_udev_client_new (subsystems);
377 g_signal_connect (client, "uevent", G_CALLBACK (uevent_cb), provider);
379 devices = g_udev_client_query_by_subsystem (client, "video4linux");
382 GUdevDevice *udev_device = devices->data;
385 devices = g_list_remove (devices, udev_device);
387 if (g_udev_device_get_property_as_int (udev_device, "ID_V4L_VERSION") == 2) {
389 gst_v4l2_device_provider_device_from_udev (provider, udev_device);
391 gst_device_provider_device_add (GST_DEVICE_PROVIDER (provider), gstdev);
394 g_object_unref (udev_device);
397 GST_OBJECT_LOCK (provider);
398 provider->started = TRUE;
399 g_cond_broadcast (&provider->started_cond);
400 GST_OBJECT_UNLOCK (provider);
402 g_main_loop_run (loop);
403 g_main_loop_unref (loop);
405 g_object_unref (client);
406 g_main_context_unref (context);
408 gst_object_unref (provider);
414 gst_v4l2_device_provider_start (GstDeviceProvider * provider)
416 GstV4l2DeviceProvider *self = GST_V4L2_DEVICE_PROVIDER (provider);
418 GST_OBJECT_LOCK (self);
419 g_assert (self->context == NULL);
421 self->context = g_main_context_new ();
422 self->loop = g_main_loop_new (self->context, FALSE);
424 self->thread = g_thread_new ("v4l2-device-provider", provider_thread,
425 g_object_ref (self));
427 while (self->started == FALSE)
428 g_cond_wait (&self->started_cond, GST_OBJECT_GET_LOCK (self));
430 GST_OBJECT_UNLOCK (self);
436 gst_v4l2_device_provider_stop (GstDeviceProvider * provider)
438 GstV4l2DeviceProvider *self = GST_V4L2_DEVICE_PROVIDER (provider);
439 GMainContext *context;
441 GSource *idle_stop_source;
443 GST_OBJECT_LOCK (self);
444 context = self->context;
446 self->context = NULL;
448 GST_OBJECT_UNLOCK (self);
450 if (!context || !loop)
453 idle_stop_source = g_idle_source_new ();
454 g_source_set_callback (idle_stop_source, (GSourceFunc) g_main_loop_quit, loop,
455 (GDestroyNotify) g_main_loop_unref);
456 g_source_attach (idle_stop_source, context);
457 g_source_unref (idle_stop_source);
458 g_main_context_unref (context);
460 g_thread_join (self->thread);
462 self->started = FALSE;
469 PROP_DEVICE_PATH = 1,
472 G_DEFINE_TYPE (GstV4l2Device, gst_v4l2_device, GST_TYPE_DEVICE);
474 static void gst_v4l2_device_get_property (GObject * object, guint prop_id,
475 GValue * value, GParamSpec * pspec);
476 static void gst_v4l2_device_set_property (GObject * object, guint prop_id,
477 const GValue * value, GParamSpec * pspec);
478 static void gst_v4l2_device_finalize (GObject * object);
479 static GstElement *gst_v4l2_device_create_element (GstDevice * device,
483 gst_v4l2_device_class_init (GstV4l2DeviceClass * klass)
485 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
486 GObjectClass *object_class = G_OBJECT_CLASS (klass);
488 dev_class->create_element = gst_v4l2_device_create_element;
490 object_class->get_property = gst_v4l2_device_get_property;
491 object_class->set_property = gst_v4l2_device_set_property;
492 object_class->finalize = gst_v4l2_device_finalize;
494 g_object_class_install_property (object_class, PROP_DEVICE_PATH,
495 g_param_spec_string ("device-path", "Device Path",
496 "The Path of the device node", "",
497 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
501 gst_v4l2_device_init (GstV4l2Device * device)
506 gst_v4l2_device_finalize (GObject * object)
508 GstV4l2Device *device = GST_V4L2_DEVICE (object);
510 g_free (device->device_path);
511 g_free (device->syspath);
513 G_OBJECT_CLASS (gst_v4l2_device_parent_class)->finalize (object);
517 gst_v4l2_device_create_element (GstDevice * device, const gchar * name)
519 GstV4l2Device *v4l2_dev = GST_V4L2_DEVICE (device);
522 elem = gst_element_factory_make (v4l2_dev->element, name);
523 g_object_set (elem, "device", v4l2_dev->device_path, NULL);
528 static GstV4l2Device *
529 gst_v4l2_device_new (const gchar * device_path, const gchar * device_name,
530 GstCaps * caps, GstV4l2DeviceType type, GstStructure * props)
532 GstV4l2Device *gstdev;
533 const gchar *element = NULL;
534 const gchar *klass = NULL;
536 g_return_val_if_fail (device_path, NULL);
537 g_return_val_if_fail (device_name, NULL);
538 g_return_val_if_fail (caps, NULL);
541 case GST_V4L2_DEVICE_TYPE_SOURCE:
543 klass = "Video/Source";
545 case GST_V4L2_DEVICE_TYPE_SINK:
546 element = "v4l2sink";
547 klass = "Video/Sink";
550 g_assert_not_reached ();
554 gstdev = g_object_new (GST_TYPE_V4L2_DEVICE, "device-path", device_path,
555 "display-name", device_name, "caps", caps, "device-class", klass,
556 "properties", props, NULL);
558 gstdev->element = element;
566 gst_v4l2_device_get_property (GObject * object, guint prop_id,
567 GValue * value, GParamSpec * pspec)
569 GstV4l2Device *device;
571 device = GST_V4L2_DEVICE_CAST (object);
574 case PROP_DEVICE_PATH:
575 g_value_set_string (value, device->device_path);
578 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
585 gst_v4l2_device_set_property (GObject * object, guint prop_id,
586 const GValue * value, GParamSpec * pspec)
588 GstV4l2Device *device;
590 device = GST_V4L2_DEVICE_CAST (object);
593 case PROP_DEVICE_PATH:
594 device->device_path = g_value_dup_string (value);
597 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);