1 /* GStreamer
2 *
3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
5 *
6 * gstv4l2.c: plugin for v4l2 elements
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #ifndef _GNU_SOURCE
29 # define _GNU_SOURCE /* O_CLOEXEC */
30 #endif
31
32 #include "gst/gst-i18n-plugin.h"
33
34 #include <gst/gst.h>
35
36 #include <fcntl.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41
42 #include "ext/videodev2.h"
43 #include "v4l2-utils.h"
44
45 #include "gstv4l2object.h"
46 #include "gstv4l2src.h"
47 #include "gstv4l2sink.h"
48 #include "gstv4l2radio.h"
49 #include "gstv4l2videodec.h"
50 #include "gstv4l2fwhtenc.h"
51 #include "gstv4l2h263enc.h"
52 #include "gstv4l2h264enc.h"
53 #include "gstv4l2h265enc.h"
54 #include "gstv4l2jpegenc.h"
55 #include "gstv4l2mpeg4enc.h"
56 #include "gstv4l2vp8enc.h"
57 #include "gstv4l2vp9enc.h"
58 #include "gstv4l2deviceprovider.h"
59 #include "gstv4l2transform.h"
60
61 /* used in gstv4l2object.c and v4l2_calls.c */
62 GST_DEBUG_CATEGORY (v4l2_debug);
63 #define GST_CAT_DEFAULT v4l2_debug
64
65 #ifdef GST_V4L2_ENABLE_PROBE
66 /* This is a minimalist probe, for speed, we only enumerate formats */
67 static GstCaps *
gst_v4l2_probe_template_caps(const gchar * device,gint video_fd,enum v4l2_buf_type type)68 gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
69 enum v4l2_buf_type type)
70 {
71 gint n;
72 struct v4l2_fmtdesc format;
73 GstCaps *caps;
74
75 GST_DEBUG ("Getting %s format enumerations", device);
76 caps = gst_caps_new_empty ();
77
78 for (n = 0;; n++) {
79 GstStructure *template;
80
81 memset (&format, 0, sizeof (format));
82
83 format.index = n;
84 format.type = type;
85
86 if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
87 break; /* end of enumeration */
88
89 GST_LOG ("index: %u", format.index);
90 GST_LOG ("type: %d", format.type);
91 GST_LOG ("flags: %08x", format.flags);
92 GST_LOG ("description: '%s'", format.description);
93 GST_LOG ("pixelformat: %" GST_FOURCC_FORMAT,
94 GST_FOURCC_ARGS (format.pixelformat));
95
96 template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
97
98 if (template) {
99 GstStructure *alt_t = NULL;
100
101 switch (format.pixelformat) {
102 case V4L2_PIX_FMT_RGB32:
103 alt_t = gst_structure_copy (template);
104 gst_structure_set (alt_t, "format", G_TYPE_STRING, "ARGB", NULL);
105 break;
106 case V4L2_PIX_FMT_BGR32:
107 alt_t = gst_structure_copy (template);
108 gst_structure_set (alt_t, "format", G_TYPE_STRING, "BGRA", NULL);
109 default:
110 break;
111 }
112
113 gst_caps_append_structure (caps, template);
114
115 if (alt_t)
116 gst_caps_append_structure (caps, alt_t);
117 }
118 }
119
120 return gst_caps_simplify (caps);
121 }
122
123 static gboolean
gst_v4l2_probe_and_register(GstPlugin * plugin)124 gst_v4l2_probe_and_register (GstPlugin * plugin)
125 {
126 GstV4l2Iterator *it;
127 gint video_fd = -1;
128 struct v4l2_capability vcap;
129 guint32 device_caps;
130
131 GST_DEBUG ("Probing devices");
132
133 it = gst_v4l2_iterator_new ();
134
135 while (gst_v4l2_iterator_next (it)) {
136 GstCaps *src_caps, *sink_caps;
137 gchar *basename;
138
139 if (video_fd >= 0)
140 close (video_fd);
141
142 video_fd = open (it->device_path, O_RDWR | O_CLOEXEC);
143
144 if (video_fd == -1) {
145 GST_DEBUG ("Failed to open %s: %s", it->device_path, g_strerror (errno));
146 continue;
147 }
148
149 memset (&vcap, 0, sizeof (vcap));
150
151 if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
152 GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
153 continue;
154 }
155
156 if (vcap.capabilities & V4L2_CAP_DEVICE_CAPS)
157 device_caps = vcap.device_caps;
158 else
159 device_caps = vcap.capabilities;
160
161 if (!GST_V4L2_IS_M2M (device_caps))
162 continue;
163
164 GST_DEBUG ("Probing '%s' located at '%s'",
165 it->device_name ? it->device_name : (const gchar *) vcap.driver,
166 it->device_path);
167
168 /* get sink supported format (no MPLANE for codec) */
169 sink_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
170 video_fd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
171 gst_v4l2_probe_template_caps (it->device_path, video_fd,
172 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE));
173
174 /* get src supported format */
175 src_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
176 video_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE),
177 gst_v4l2_probe_template_caps (it->device_path, video_fd,
178 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE));
179
180 /* Skip devices without any supported formats */
181 if (gst_caps_is_empty (sink_caps) || gst_caps_is_empty (src_caps)) {
182 gst_caps_unref (sink_caps);
183 gst_caps_unref (src_caps);
184 continue;
185 }
186
187 basename = g_path_get_basename (it->device_path);
188
189 /* Caps won't be freed if the subclass is not instantiated */
190 GST_MINI_OBJECT_FLAG_SET (sink_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
191 GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
192
193 if (gst_v4l2_is_video_dec (sink_caps, src_caps)) {
194 gst_v4l2_video_dec_register (plugin, basename, it->device_path,
195 sink_caps, src_caps);
196 } else if (gst_v4l2_is_video_enc (sink_caps, src_caps, NULL)) {
197 if (gst_v4l2_is_fwht_enc (sink_caps, src_caps))
198 gst_v4l2_fwht_enc_register (plugin, basename, it->device_path,
199 sink_caps, src_caps);
200
201 if (gst_v4l2_is_h264_enc (sink_caps, src_caps))
202 gst_v4l2_h264_enc_register (plugin, basename, it->device_path,
203 sink_caps, src_caps);
204
205 if (gst_v4l2_is_h265_enc (sink_caps, src_caps))
206 gst_v4l2_h265_enc_register (plugin, basename, it->device_path,
207 sink_caps, src_caps);
208
209 if (gst_v4l2_is_mpeg4_enc (sink_caps, src_caps))
210 gst_v4l2_mpeg4_enc_register (plugin, basename, it->device_path,
211 sink_caps, src_caps);
212
213 if (gst_v4l2_is_h263_enc (sink_caps, src_caps))
214 gst_v4l2_h263_enc_register (plugin, basename, it->device_path,
215 sink_caps, src_caps);
216
217 if (gst_v4l2_is_jpeg_enc (sink_caps, src_caps))
218 gst_v4l2_jpeg_enc_register (plugin, basename, it->device_path,
219 sink_caps, src_caps);
220
221 if (gst_v4l2_is_vp8_enc (sink_caps, src_caps))
222 gst_v4l2_vp8_enc_register (plugin, basename, it->device_path,
223 sink_caps, src_caps);
224
225 if (gst_v4l2_is_vp9_enc (sink_caps, src_caps))
226 gst_v4l2_vp9_enc_register (plugin, basename, it->device_path,
227 sink_caps, src_caps);
228 } else if (gst_v4l2_is_transform (sink_caps, src_caps)) {
229 gst_v4l2_transform_register (plugin, basename, it->device_path,
230 sink_caps, src_caps);
231 }
232 /* else if ( ... etc. */
233
234 gst_caps_unref (sink_caps);
235 gst_caps_unref (src_caps);
236 g_free (basename);
237 }
238
239 if (video_fd >= 0)
240 close (video_fd);
241
242 gst_v4l2_iterator_free (it);
243
244 return TRUE;
245 }
246 #endif
247
248 static gboolean
plugin_init(GstPlugin * plugin)249 plugin_init (GstPlugin * plugin)
250 {
251 const gchar *paths[] = { "/dev", "/dev/v4l2", NULL };
252 const gchar *names[] = { "video", NULL };
253
254 GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
255
256 /* Add some depedency, so the dynamic features get updated upon changes in
257 * /dev/video* */
258 gst_plugin_add_dependency (plugin,
259 NULL, paths, names, GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX);
260
261 if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
262 GST_TYPE_V4L2SRC) ||
263 !gst_element_register (plugin, "v4l2sink", GST_RANK_NONE,
264 GST_TYPE_V4L2SINK) ||
265 !gst_element_register (plugin, "v4l2radio", GST_RANK_NONE,
266 GST_TYPE_V4L2RADIO) ||
267 !gst_device_provider_register (plugin, "v4l2deviceprovider",
268 GST_RANK_PRIMARY, GST_TYPE_V4L2_DEVICE_PROVIDER)
269 /* etc. */
270 #ifdef GST_V4L2_ENABLE_PROBE
271 || !gst_v4l2_probe_and_register (plugin)
272 #endif
273 )
274 return FALSE;
275
276 #ifdef ENABLE_NLS
277 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
278 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
279 #endif /* ENABLE_NLS */
280
281 return TRUE;
282 }
283
284 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
285 GST_VERSION_MINOR,
286 video4linux2,
287 "elements for Video 4 Linux",
288 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
289