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;
145 v4l2obj->skip_try_fmt_probes = TRUE;
148 if (v4l2obj->device_caps &
149 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)) {
150 /* We ignore M2M devices that are both capture and output for now
151 * The provider is not for them */
152 if (type != GST_V4L2_DEVICE_TYPE_INVALID)
155 type = GST_V4L2_DEVICE_TYPE_SINK;
157 /* We have opened as a capture as we didn't know, now that know,
159 if (v4l2obj->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
160 v4l2obj->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
162 v4l2obj->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
165 if (type == GST_V4L2_DEVICE_TYPE_INVALID)
168 caps = gst_v4l2_object_get_caps (v4l2obj, NULL);
172 if (gst_caps_is_empty (caps)) {
173 gst_caps_unref (caps);
177 device = gst_v4l2_device_new (device_path,
178 device_name ? device_name : (gchar *) v4l2obj->vcap.card, caps, type,
180 gst_caps_unref (caps);
184 gst_v4l2_close (v4l2obj);
189 gst_v4l2_object_destroy (v4l2obj);
192 gst_structure_free (props);
199 gst_v4l2_device_provider_probe (GstDeviceProvider * provider)
201 GstV4l2DeviceProvider *self = GST_V4L2_DEVICE_PROVIDER (provider);
203 GList *devices = NULL;
205 it = gst_v4l2_iterator_new ();
207 while (gst_v4l2_iterator_next (it)) {
209 GstV4l2Device *device;
211 props = gst_structure_new ("v4l2-proplist", "device.path", G_TYPE_STRING,
212 it->device_path, "udev-probed", G_TYPE_BOOLEAN, FALSE, NULL);
213 device = gst_v4l2_device_provider_probe_device (self, it->device_path, NULL,
217 gst_object_ref_sink (device);
218 devices = g_list_prepend (devices, device);
222 gst_v4l2_iterator_free (it);
230 gst_v4l2_device_provider_device_from_udev (GstV4l2DeviceProvider * provider,
231 GUdevDevice * udev_device)
233 GstV4l2Device *gstdev;
234 const gchar *device_path = g_udev_device_get_device_file (udev_device);
235 const gchar *device_name, *str;
238 props = gst_structure_new ("v4l2deviceprovider", "udev-probed",
239 G_TYPE_BOOLEAN, TRUE, NULL);
241 str = g_udev_device_get_property (udev_device, "ID_PATH");
242 if (!(str && *str)) {
243 str = g_udev_device_get_sysfs_path (udev_device);
246 gst_structure_set (props, "device.bus_path", G_TYPE_STRING, str, NULL);
248 if ((str = g_udev_device_get_sysfs_path (udev_device)) && *str)
249 gst_structure_set (props, "sysfs.path", G_TYPE_STRING, str, NULL);
251 if ((str = g_udev_device_get_property (udev_device, "ID_ID")) && *str)
252 gst_structure_set (props, "udev.id", G_TYPE_STRING, str, NULL);
254 if ((str = g_udev_device_get_property (udev_device, "ID_BUS")) && *str)
255 gst_structure_set (props, "device.bus", G_TYPE_STRING, str, NULL);
257 if ((str = g_udev_device_get_property (udev_device, "SUBSYSTEM")) && *str)
258 gst_structure_set (props, "device.subsystem", G_TYPE_STRING, str, NULL);
260 if ((str = g_udev_device_get_property (udev_device, "ID_VENDOR_ID")) && *str)
261 gst_structure_set (props, "device.vendor.id", G_TYPE_STRING, str, NULL);
263 str = g_udev_device_get_property (udev_device, "ID_VENDOR_FROM_DATABASE");
264 if (!(str && *str)) {
265 str = g_udev_device_get_property (udev_device, "ID_VENDOR_ENC");
266 if (!(str && *str)) {
267 str = g_udev_device_get_property (udev_device, "ID_VENDOR");
271 gst_structure_set (props, "device.vendor.name", G_TYPE_STRING, str, NULL);
273 if ((str = g_udev_device_get_property (udev_device, "ID_MODEL_ID")) && *str)
274 gst_structure_set (props, "device.product.id", G_TYPE_STRING, str, NULL);
276 device_name = g_udev_device_get_property (udev_device, "ID_V4L_PRODUCT");
277 if (!(device_name && *device_name)) {
279 g_udev_device_get_property (udev_device, "ID_MODEL_FROM_DATABASE");
280 if (!(device_name && *device_name)) {
281 device_name = g_udev_device_get_property (udev_device, "ID_MODEL_ENC");
282 if (!(device_name && *device_name)) {
283 device_name = g_udev_device_get_property (udev_device, "ID_MODEL");
287 if (device_name && *device_name)
288 gst_structure_set (props, "device.product.name", G_TYPE_STRING, device_name,
291 if ((str = g_udev_device_get_property (udev_device, "ID_SERIAL")) && *str)
292 gst_structure_set (props, "device.serial", G_TYPE_STRING, str, NULL);
294 if ((str = g_udev_device_get_property (udev_device, "ID_V4L_CAPABILITIES"))
296 gst_structure_set (props, "device.capabilities", G_TYPE_STRING, str, NULL);
298 gstdev = gst_v4l2_device_provider_probe_device (provider, device_path,
302 gstdev->syspath = g_strdup (g_udev_device_get_sysfs_path (udev_device));
304 return GST_DEVICE (gstdev);
308 uevent_cb (GUdevClient * client, const gchar * action, GUdevDevice * device,
309 GstV4l2DeviceProvider * self)
311 GstDeviceProvider *provider = GST_DEVICE_PROVIDER (self);
313 /* Not V4L2, ignoring */
314 if (g_udev_device_get_property_as_int (device, "ID_V4L_VERSION") != 2)
317 if (!strcmp (action, "add")) {
318 GstDevice *gstdev = NULL;
320 gstdev = gst_v4l2_device_provider_device_from_udev (self, device);
323 gst_device_provider_device_add (provider, gstdev);
324 } else if (!strcmp (action, "remove")) {
325 GstV4l2Device *gstdev = NULL;
328 GST_OBJECT_LOCK (self);
329 for (item = provider->devices; item; item = item->next) {
332 if (!strcmp (gstdev->syspath, g_udev_device_get_sysfs_path (device))) {
333 gst_object_ref (gstdev);
339 GST_OBJECT_UNLOCK (provider);
342 gst_device_provider_device_remove (provider, GST_DEVICE (gstdev));
343 g_object_unref (gstdev);
346 GST_WARNING ("Unhandled action %s", action);
351 provider_thread (gpointer data)
353 GstV4l2DeviceProvider *provider = data;
354 GMainContext *context = NULL;
355 GMainLoop *loop = NULL;
358 static const gchar *subsystems[] = { "video4linux", NULL };
360 GST_OBJECT_LOCK (provider);
361 if (provider->context)
362 context = g_main_context_ref (provider->context);
364 loop = g_main_loop_ref (provider->loop);
366 if (context == NULL || loop == NULL) {
367 provider->started = TRUE;
368 g_cond_broadcast (&provider->started_cond);
369 GST_OBJECT_UNLOCK (provider);
372 GST_OBJECT_UNLOCK (provider);
374 g_main_context_push_thread_default (context);
376 client = g_udev_client_new (subsystems);
378 g_signal_connect (client, "uevent", G_CALLBACK (uevent_cb), provider);
380 devices = g_udev_client_query_by_subsystem (client, "video4linux");
383 GUdevDevice *udev_device = devices->data;
386 devices = g_list_remove (devices, udev_device);
388 if (g_udev_device_get_property_as_int (udev_device, "ID_V4L_VERSION") == 2) {
390 gst_v4l2_device_provider_device_from_udev (provider, udev_device);
392 gst_device_provider_device_add (GST_DEVICE_PROVIDER (provider), gstdev);
395 g_object_unref (udev_device);
398 GST_OBJECT_LOCK (provider);
399 provider->started = TRUE;
400 g_cond_broadcast (&provider->started_cond);
401 GST_OBJECT_UNLOCK (provider);
403 g_main_loop_run (loop);
404 g_main_loop_unref (loop);
406 g_object_unref (client);
407 g_main_context_unref (context);
409 gst_object_unref (provider);
415 gst_v4l2_device_provider_start (GstDeviceProvider * provider)
417 GstV4l2DeviceProvider *self = GST_V4L2_DEVICE_PROVIDER (provider);
419 GST_OBJECT_LOCK (self);
420 g_assert (self->context == NULL);
422 self->context = g_main_context_new ();
423 self->loop = g_main_loop_new (self->context, FALSE);
425 self->thread = g_thread_new ("v4l2-device-provider", provider_thread,
426 g_object_ref (self));
428 while (self->started == FALSE)
429 g_cond_wait (&self->started_cond, GST_OBJECT_GET_LOCK (self));
431 GST_OBJECT_UNLOCK (self);
437 gst_v4l2_device_provider_stop (GstDeviceProvider * provider)
439 GstV4l2DeviceProvider *self = GST_V4L2_DEVICE_PROVIDER (provider);
440 GMainContext *context;
442 GSource *idle_stop_source;
444 GST_OBJECT_LOCK (self);
445 context = self->context;
447 self->context = NULL;
449 GST_OBJECT_UNLOCK (self);
451 if (!context || !loop)
454 idle_stop_source = g_idle_source_new ();
455 g_source_set_callback (idle_stop_source, (GSourceFunc) g_main_loop_quit, loop,
456 (GDestroyNotify) g_main_loop_unref);
457 g_source_attach (idle_stop_source, context);
458 g_source_unref (idle_stop_source);
459 g_main_context_unref (context);
461 g_thread_join (self->thread);
463 self->started = FALSE;
470 PROP_DEVICE_PATH = 1,
473 G_DEFINE_TYPE (GstV4l2Device, gst_v4l2_device, GST_TYPE_DEVICE);
475 static void gst_v4l2_device_get_property (GObject * object, guint prop_id,
476 GValue * value, GParamSpec * pspec);
477 static void gst_v4l2_device_set_property (GObject * object, guint prop_id,
478 const GValue * value, GParamSpec * pspec);
479 static void gst_v4l2_device_finalize (GObject * object);
480 static GstElement *gst_v4l2_device_create_element (GstDevice * device,
484 gst_v4l2_device_class_init (GstV4l2DeviceClass * klass)
486 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
487 GObjectClass *object_class = G_OBJECT_CLASS (klass);
489 dev_class->create_element = gst_v4l2_device_create_element;
491 object_class->get_property = gst_v4l2_device_get_property;
492 object_class->set_property = gst_v4l2_device_set_property;
493 object_class->finalize = gst_v4l2_device_finalize;
495 g_object_class_install_property (object_class, PROP_DEVICE_PATH,
496 g_param_spec_string ("device-path", "Device Path",
497 "The Path of the device node", "",
498 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
502 gst_v4l2_device_init (GstV4l2Device * device)
507 gst_v4l2_device_finalize (GObject * object)
509 GstV4l2Device *device = GST_V4L2_DEVICE (object);
511 g_free (device->device_path);
512 g_free (device->syspath);
514 G_OBJECT_CLASS (gst_v4l2_device_parent_class)->finalize (object);
518 gst_v4l2_device_create_element (GstDevice * device, const gchar * name)
520 GstV4l2Device *v4l2_dev = GST_V4L2_DEVICE (device);
523 elem = gst_element_factory_make (v4l2_dev->element, name);
524 g_object_set (elem, "device", v4l2_dev->device_path, NULL);
529 static GstV4l2Device *
530 gst_v4l2_device_new (const gchar * device_path, const gchar * device_name,
531 GstCaps * caps, GstV4l2DeviceType type, GstStructure * props)
533 GstV4l2Device *gstdev;
534 const gchar *element = NULL;
535 const gchar *klass = NULL;
537 g_return_val_if_fail (device_path, NULL);
538 g_return_val_if_fail (device_name, NULL);
539 g_return_val_if_fail (caps, NULL);
542 case GST_V4L2_DEVICE_TYPE_SOURCE:
544 klass = "Video/Source";
546 case GST_V4L2_DEVICE_TYPE_SINK:
547 element = "v4l2sink";
548 klass = "Video/Sink";
551 g_assert_not_reached ();
555 gstdev = g_object_new (GST_TYPE_V4L2_DEVICE, "device-path", device_path,
556 "display-name", device_name, "caps", caps, "device-class", klass,
557 "properties", props, NULL);
559 gstdev->element = element;
567 gst_v4l2_device_get_property (GObject * object, guint prop_id,
568 GValue * value, GParamSpec * pspec)
570 GstV4l2Device *device;
572 device = GST_V4L2_DEVICE_CAST (object);
575 case PROP_DEVICE_PATH:
576 g_value_set_string (value, device->device_path);
579 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586 gst_v4l2_device_set_property (GObject * object, guint prop_id,
587 const GValue * value, GParamSpec * pspec)
589 GstV4l2Device *device;
591 device = GST_V4L2_DEVICE_CAST (object);
594 case PROP_DEVICE_PATH:
595 device->device_path = g_value_dup_string (value);
598 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);