2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "v4l2-utils.h"
28 /**************************/
29 /* Common device iterator */
30 /**************************/
33 #include <gudev/gudev.h>
35 struct _GstV4l2GUdevIterator
37 GstV4l2Iterator parent;
44 gst_v4l2_iterator_new (void)
46 static const gchar *subsystems[] = { "video4linux", NULL };
47 struct _GstV4l2GUdevIterator *it;
49 it = g_slice_new0 (struct _GstV4l2GUdevIterator);
51 it->client = g_udev_client_new (subsystems);
52 it->devices = g_udev_client_query_by_subsystem (it->client, "video4linux");
54 return (GstV4l2Iterator *) it;
58 gst_v4l2_iterator_next (GstV4l2Iterator * _it)
60 struct _GstV4l2GUdevIterator *it = (struct _GstV4l2GUdevIterator *) _it;
61 const gchar *device_name;
64 g_object_unref (it->device);
67 it->parent.device_path = NULL;
68 it->parent.device_name = NULL;
70 if (it->devices == NULL)
73 it->device = it->devices->data;
74 it->devices = g_list_delete_link (it->devices, it->devices);
76 device_name = g_udev_device_get_property (it->device, "ID_V4L_PRODUCT");
78 device_name = g_udev_device_get_property (it->device, "ID_MODEL_ENC");
80 device_name = g_udev_device_get_property (it->device, "ID_MODEL");
82 it->parent.device_path = g_udev_device_get_device_file (it->device);
83 it->parent.device_name = device_name;
84 it->parent.sys_path = g_udev_device_get_sysfs_path (it->device);
90 gst_v4l2_iterator_free (GstV4l2Iterator * _it)
92 struct _GstV4l2GUdevIterator *it = (struct _GstV4l2GUdevIterator *) _it;
93 g_list_free_full (it->devices, g_object_unref);
94 gst_object_unref (it->client);
95 g_slice_free (struct _GstV4l2GUdevIterator, it);
100 struct _GstV4l2FsIterator
102 GstV4l2Iterator parent;
109 gst_v4l2_iterator_new (void)
111 struct _GstV4l2FsIterator *it;
113 it = g_slice_new0 (struct _GstV4l2FsIterator);
118 return (GstV4l2Iterator *) it;
122 gst_v4l2_iterator_next (GstV4l2Iterator * _it)
124 struct _GstV4l2FsIterator *it = (struct _GstV4l2FsIterator *) _it;
125 static const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
126 gchar *device = NULL;
128 g_free ((gchar *) it->parent.device_path);
129 it->parent.device_path = NULL;
131 while (device == NULL) {
134 if (it->video_idx >= 64) {
139 if (dev_base[it->base_idx] == NULL) {
144 device = g_strdup_printf ("%s%d", dev_base[it->base_idx], it->video_idx);
146 if (g_file_test (device, G_FILE_TEST_EXISTS)) {
147 it->parent.device_path = device;
155 return it->parent.device_path != NULL;
159 gst_v4l2_iterator_free (GstV4l2Iterator * _it)
161 struct _GstV4l2FsIterator *it = (struct _GstV4l2FsIterator *) _it;
162 g_free ((gchar *) it->parent.device_path);
163 g_slice_free (struct _GstV4l2FsIterator, it);
169 gst_v4l2_clear_error (GstV4l2Error * v4l2err)
172 g_clear_error (&v4l2err->error);
173 g_free (v4l2err->dbg_message);
174 v4l2err->dbg_message = NULL;
179 gst_v4l2_error (gpointer element, GstV4l2Error * v4l2err)
183 if (!v4l2err || !v4l2err->error)
186 error = v4l2err->error;
189 GST_WARNING_OBJECT (element, "error: %s", error->message);
191 if (v4l2err->dbg_message)
192 GST_WARNING_OBJECT (element, "error: %s", v4l2err->dbg_message);
194 gst_element_message_full (GST_ELEMENT (element), GST_MESSAGE_ERROR,
195 error->domain, error->code, error->message, v4l2err->dbg_message,
196 v4l2err->file, v4l2err->func, v4l2err->line);
198 error->message = NULL;
199 v4l2err->dbg_message = NULL;
201 gst_v4l2_clear_error (v4l2err);