v4l2object: Don't use mmap64 if off_t is 64-bit
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2-utils.c
1 /*
2  * Copyright (C) 2014 Collabora Ltd.
3  *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
4  *
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.
9  *
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.
14  *
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.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "v4l2-utils.h"
27
28 /**************************/
29 /* Common device iterator */
30 /**************************/
31
32 #ifdef HAVE_GUDEV
33 #include <gudev/gudev.h>
34
35 struct _GstV4l2GUdevIterator
36 {
37   GstV4l2Iterator parent;
38   GList *devices;
39   GUdevDevice *device;
40   GUdevClient *client;
41 };
42
43 GstV4l2Iterator *
44 gst_v4l2_iterator_new (void)
45 {
46   static const gchar *subsystems[] = { "video4linux", NULL };
47   struct _GstV4l2GUdevIterator *it;
48
49   it = g_slice_new0 (struct _GstV4l2GUdevIterator);
50
51   it->client = g_udev_client_new (subsystems);
52   it->devices = g_udev_client_query_by_subsystem (it->client, "video4linux");
53
54   return (GstV4l2Iterator *) it;
55 }
56
57 gboolean
58 gst_v4l2_iterator_next (GstV4l2Iterator * _it)
59 {
60   struct _GstV4l2GUdevIterator *it = (struct _GstV4l2GUdevIterator *) _it;
61   const gchar *device_name;
62
63   if (it->device)
64     g_object_unref (it->device);
65
66   it->device = NULL;
67   it->parent.device_path = NULL;
68   it->parent.device_name = NULL;
69
70   if (it->devices == NULL)
71     return FALSE;
72
73   it->device = it->devices->data;
74   it->devices = g_list_delete_link (it->devices, it->devices);
75
76   device_name = g_udev_device_get_property (it->device, "ID_V4L_PRODUCT");
77   if (!device_name)
78     device_name = g_udev_device_get_property (it->device, "ID_MODEL_ENC");
79   if (!device_name)
80     device_name = g_udev_device_get_property (it->device, "ID_MODEL");
81
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);
85
86   return TRUE;
87 }
88
89 void
90 gst_v4l2_iterator_free (GstV4l2Iterator * _it)
91 {
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);
96 }
97
98 #else /* No GUDEV */
99
100 struct _GstV4l2FsIterator
101 {
102   GstV4l2Iterator parent;
103   gint base_idx;
104   gint video_idx;
105   gchar *device;
106 };
107
108 GstV4l2Iterator *
109 gst_v4l2_iterator_new (void)
110 {
111   struct _GstV4l2FsIterator *it;
112
113   it = g_slice_new0 (struct _GstV4l2FsIterator);
114   it->base_idx = 0;
115   it->video_idx = -1;
116   it->device = NULL;
117
118   return (GstV4l2Iterator *) it;
119 }
120
121 gboolean
122 gst_v4l2_iterator_next (GstV4l2Iterator * _it)
123 {
124   struct _GstV4l2FsIterator *it = (struct _GstV4l2FsIterator *) _it;
125   static const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
126   gchar *device = NULL;
127
128   g_free ((gchar *) it->parent.device_path);
129   it->parent.device_path = NULL;
130
131   while (device == NULL) {
132     it->video_idx++;
133
134     if (it->video_idx >= 64) {
135       it->video_idx = 0;
136       it->base_idx++;
137     }
138
139     if (dev_base[it->base_idx] == NULL) {
140       it->video_idx = 0;
141       break;
142     }
143
144     device = g_strdup_printf ("%s%d", dev_base[it->base_idx], it->video_idx);
145
146     if (g_file_test (device, G_FILE_TEST_EXISTS)) {
147       it->parent.device_path = device;
148       break;
149     }
150
151     g_free (device);
152     device = NULL;
153   }
154
155   return it->parent.device_path != NULL;
156 }
157
158 void
159 gst_v4l2_iterator_free (GstV4l2Iterator * _it)
160 {
161   struct _GstV4l2FsIterator *it = (struct _GstV4l2FsIterator *) _it;
162   g_free ((gchar *) it->parent.device_path);
163   g_slice_free (struct _GstV4l2FsIterator, it);
164 }
165
166 #endif
167
168 void
169 gst_v4l2_clear_error (GstV4l2Error * v4l2err)
170 {
171   if (v4l2err) {
172     g_clear_error (&v4l2err->error);
173     g_free (v4l2err->dbg_message);
174     v4l2err->dbg_message = NULL;
175   }
176 }
177
178 void
179 gst_v4l2_error (gpointer element, GstV4l2Error * v4l2err)
180 {
181   GError *error;
182
183   if (!v4l2err || !v4l2err->error)
184     return;
185
186   error = v4l2err->error;
187
188   if (error->message)
189     GST_WARNING_OBJECT (element, "error: %s", error->message);
190
191   if (v4l2err->dbg_message)
192     GST_WARNING_OBJECT (element, "error: %s", v4l2err->dbg_message);
193
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);
197
198   error->message = NULL;
199   v4l2err->dbg_message = NULL;
200
201   gst_v4l2_clear_error (v4l2err);
202 }