• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gstv4l2elements.h"
44 #include "v4l2-utils.h"
45 
46 #include "gstv4l2object.h"
47 #include "gstv4l2src.h"
48 #include "gstv4l2sink.h"
49 #include "gstv4l2radio.h"
50 #include "gstv4l2videodec.h"
51 #include "gstv4l2fwhtenc.h"
52 #include "gstv4l2h263enc.h"
53 #include "gstv4l2h264enc.h"
54 #include "gstv4l2h265enc.h"
55 #include "gstv4l2jpegenc.h"
56 #include "gstv4l2mpeg4enc.h"
57 #include "gstv4l2vp8enc.h"
58 #include "gstv4l2vp9enc.h"
59 #include "gstv4l2transform.h"
60 
61 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
62 #define GST_CAT_DEFAULT v4l2_debug
63 
64 #ifdef GST_V4L2_ENABLE_PROBE
65 /* This is a minimalist probe, for speed, we only enumerate formats */
66 static GstCaps *
gst_v4l2_probe_template_caps(const gchar * device,gint video_fd,enum v4l2_buf_type type)67 gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
68     enum v4l2_buf_type type)
69 {
70   gint n;
71   struct v4l2_fmtdesc format;
72   GstCaps *caps;
73 
74   GST_DEBUG ("Getting %s format enumerations", device);
75   caps = gst_caps_new_empty ();
76 
77   for (n = 0;; n++) {
78     GstStructure *template;
79 
80     memset (&format, 0, sizeof (format));
81 
82     format.index = n;
83     format.type = type;
84 
85     if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
86       break;                    /* end of enumeration */
87 
88     GST_LOG ("index:       %u", format.index);
89     GST_LOG ("type:        %d", format.type);
90     GST_LOG ("flags:       %08x", format.flags);
91     GST_LOG ("description: '%s'", format.description);
92     GST_LOG ("pixelformat: %" GST_FOURCC_FORMAT,
93         GST_FOURCC_ARGS (format.pixelformat));
94 
95     template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
96 
97     if (template) {
98       GstStructure *alt_t = NULL;
99 
100       switch (format.pixelformat) {
101         case V4L2_PIX_FMT_RGB32:
102           alt_t = gst_structure_copy (template);
103           gst_structure_set (alt_t, "format", G_TYPE_STRING, "ARGB", NULL);
104           break;
105         case V4L2_PIX_FMT_BGR32:
106           alt_t = gst_structure_copy (template);
107           gst_structure_set (alt_t, "format", G_TYPE_STRING, "BGRA", NULL);
108         default:
109           break;
110       }
111 
112       gst_caps_append_structure (caps, template);
113 
114       if (alt_t)
115         gst_caps_append_structure (caps, alt_t);
116     }
117   }
118 
119   return gst_caps_simplify (caps);
120 }
121 
122 static gboolean
gst_v4l2_probe_and_register(GstPlugin * plugin)123 gst_v4l2_probe_and_register (GstPlugin * plugin)
124 {
125   GstV4l2Iterator *it;
126   gint video_fd = -1;
127   struct v4l2_capability vcap;
128   guint32 device_caps;
129 
130   v4l2_element_init (plugin);
131 
132   GST_DEBUG ("Probing devices");
133 
134   it = gst_v4l2_iterator_new ();
135 
136   while (gst_v4l2_iterator_next (it)) {
137     GstCaps *src_caps, *sink_caps;
138     gchar *basename;
139 
140     if (video_fd >= 0)
141       close (video_fd);
142 
143     video_fd = open (it->device_path, O_RDWR | O_CLOEXEC);
144 
145     if (video_fd == -1) {
146       GST_DEBUG ("Failed to open %s: %s", it->device_path, g_strerror (errno));
147       continue;
148     }
149 
150     memset (&vcap, 0, sizeof (vcap));
151 
152     if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
153       GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
154       continue;
155     }
156 
157     if (vcap.capabilities & V4L2_CAP_DEVICE_CAPS)
158       device_caps = vcap.device_caps;
159     else
160       device_caps = vcap.capabilities;
161 
162     if (!GST_V4L2_IS_M2M (device_caps))
163       continue;
164 
165     GST_DEBUG ("Probing '%s' located at '%s'",
166         it->device_name ? it->device_name : (const gchar *) vcap.driver,
167         it->device_path);
168 
169     /* get sink supported format (no MPLANE for codec) */
170     sink_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
171             video_fd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
172         gst_v4l2_probe_template_caps (it->device_path, video_fd,
173             V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE));
174 
175     /* get src supported format */
176     src_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
177             video_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE),
178         gst_v4l2_probe_template_caps (it->device_path, video_fd,
179             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE));
180 
181     /* Skip devices without any supported formats */
182     if (gst_caps_is_empty (sink_caps) || gst_caps_is_empty (src_caps)) {
183       gst_caps_unref (sink_caps);
184       gst_caps_unref (src_caps);
185       continue;
186     }
187 
188     basename = g_path_get_basename (it->device_path);
189 
190     /* Caps won't be freed if the subclass is not instantiated */
191     GST_MINI_OBJECT_FLAG_SET (sink_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
192     GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
193 
194     if (gst_v4l2_is_video_dec (sink_caps, src_caps)) {
195       gst_v4l2_video_dec_register (plugin, basename, it->device_path,
196           video_fd, sink_caps, src_caps);
197     } else if (gst_v4l2_is_video_enc (sink_caps, src_caps, NULL)) {
198       if (gst_v4l2_is_fwht_enc (sink_caps, src_caps))
199         gst_v4l2_fwht_enc_register (plugin, basename, it->device_path,
200             sink_caps, src_caps);
201 
202       if (gst_v4l2_is_h264_enc (sink_caps, src_caps))
203         gst_v4l2_h264_enc_register (plugin, basename, it->device_path,
204             video_fd, sink_caps, src_caps);
205 
206       if (gst_v4l2_is_h265_enc (sink_caps, src_caps))
207         gst_v4l2_h265_enc_register (plugin, basename, it->device_path,
208             video_fd, sink_caps, src_caps);
209 
210       if (gst_v4l2_is_mpeg4_enc (sink_caps, src_caps))
211         gst_v4l2_mpeg4_enc_register (plugin, basename, it->device_path,
212             video_fd, sink_caps, src_caps);
213 
214       if (gst_v4l2_is_h263_enc (sink_caps, src_caps))
215         gst_v4l2_h263_enc_register (plugin, basename, it->device_path,
216             sink_caps, src_caps);
217 
218       if (gst_v4l2_is_jpeg_enc (sink_caps, src_caps))
219         gst_v4l2_jpeg_enc_register (plugin, basename, it->device_path,
220             sink_caps, src_caps);
221 
222       if (gst_v4l2_is_vp8_enc (sink_caps, src_caps))
223         gst_v4l2_vp8_enc_register (plugin, basename, it->device_path,
224             video_fd, sink_caps, src_caps);
225 
226       if (gst_v4l2_is_vp9_enc (sink_caps, src_caps))
227         gst_v4l2_vp9_enc_register (plugin, basename, it->device_path,
228             video_fd, sink_caps, src_caps);
229     } else if (gst_v4l2_is_transform (sink_caps, src_caps)) {
230       gst_v4l2_transform_register (plugin, basename, it->device_path,
231           sink_caps, src_caps);
232     }
233     /* else if ( ... etc. */
234 
235     gst_caps_unref (sink_caps);
236     gst_caps_unref (src_caps);
237     g_free (basename);
238   }
239 
240   if (video_fd >= 0)
241     close (video_fd);
242 
243   gst_v4l2_iterator_free (it);
244 
245   return TRUE;
246 }
247 #endif
248 
249 static gboolean
plugin_init(GstPlugin * plugin)250 plugin_init (GstPlugin * plugin)
251 {
252   gboolean ret = FALSE;
253   const gchar *paths[] = { "/dev", "/dev/v4l2", NULL };
254   const gchar *names[] = { "video", NULL };
255 
256   /* Add some dependency, 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 #ifdef GST_V4L2_ENABLE_PROBE
262   ret |= gst_v4l2_probe_and_register (plugin);
263 #endif
264 
265   ret |= GST_ELEMENT_REGISTER (v4l2src, plugin);
266   ret |= GST_ELEMENT_REGISTER (v4l2sink, plugin);
267   ret |= GST_ELEMENT_REGISTER (v4l2radio, plugin);
268   ret |= GST_DEVICE_PROVIDER_REGISTER (v4l2deviceprovider, plugin);
269 
270   return ret;
271 }
272 
273 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
274     GST_VERSION_MINOR,
275     video4linux2,
276     "elements for Video 4 Linux",
277     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
278