v4l2object: fix typo in comment
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2.c
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 "gstv4l2h263enc.h"
51 #include "gstv4l2h264enc.h"
52 #include "gstv4l2mpeg4enc.h"
53 #include "gstv4l2vp8enc.h"
54 #include "gstv4l2vp9enc.h"
55 #include "gstv4l2deviceprovider.h"
56 #include "gstv4l2transform.h"
57
58 /* used in gstv4l2object.c and v4l2_calls.c */
59 GST_DEBUG_CATEGORY (v4l2_debug);
60 #define GST_CAT_DEFAULT v4l2_debug
61
62 #ifdef GST_V4L2_ENABLE_PROBE
63 /* This is a minimalist probe, for speed, we only enumerate formats */
64 static GstCaps *
65 gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
66     enum v4l2_buf_type type)
67 {
68   gint n;
69   struct v4l2_fmtdesc format;
70   GstCaps *caps;
71
72   GST_DEBUG ("Getting %s format enumerations", device);
73   caps = gst_caps_new_empty ();
74
75   for (n = 0;; n++) {
76     GstStructure *template;
77
78     memset (&format, 0, sizeof (format));
79
80     format.index = n;
81     format.type = type;
82
83     if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
84       break;                    /* end of enumeration */
85
86     GST_LOG ("index:       %u", format.index);
87     GST_LOG ("type:        %d", format.type);
88     GST_LOG ("flags:       %08x", format.flags);
89     GST_LOG ("description: '%s'", format.description);
90     GST_LOG ("pixelformat: %" GST_FOURCC_FORMAT,
91         GST_FOURCC_ARGS (format.pixelformat));
92
93     template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
94
95     if (template) {
96       GstStructure *alt_t = NULL;
97
98       switch (format.pixelformat) {
99         case V4L2_PIX_FMT_RGB32:
100           alt_t = gst_structure_copy (template);
101           gst_structure_set (alt_t, "format", G_TYPE_STRING, "ARGB", NULL);
102           break;
103         case V4L2_PIX_FMT_BGR32:
104           alt_t = gst_structure_copy (template);
105           gst_structure_set (alt_t, "format", G_TYPE_STRING, "BGRA", NULL);
106         default:
107           break;
108       }
109
110       gst_caps_append_structure (caps, template);
111
112       if (alt_t)
113         gst_caps_append_structure (caps, alt_t);
114     }
115   }
116
117   return gst_caps_simplify (caps);
118 }
119
120 static gboolean
121 gst_v4l2_probe_and_register (GstPlugin * plugin)
122 {
123   GstV4l2Iterator *it;
124   gint video_fd = -1;
125   struct v4l2_capability vcap;
126   guint32 device_caps;
127
128   it = gst_v4l2_iterator_new ();
129
130   while (gst_v4l2_iterator_next (it)) {
131     GstCaps *src_caps, *sink_caps;
132     gchar *basename;
133
134     if (video_fd >= 0)
135       close (video_fd);
136
137     video_fd = open (it->device_path, O_RDWR | O_CLOEXEC);
138
139     if (video_fd == -1) {
140       GST_DEBUG ("Failed to open %s: %s", it->device_path, g_strerror (errno));
141       continue;
142     }
143
144     memset (&vcap, 0, sizeof (vcap));
145
146     if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
147       GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
148       continue;
149     }
150
151     if (vcap.capabilities & V4L2_CAP_DEVICE_CAPS)
152       device_caps = vcap.device_caps;
153     else
154       device_caps = vcap.capabilities;
155
156     if (!GST_V4L2_IS_M2M (device_caps))
157       continue;
158
159     GST_DEBUG ("Probing '%s' located at '%s'",
160         it->device_name ? it->device_name : (const gchar *) vcap.driver,
161         it->device_path);
162
163     /* get sink supported format (no MPLANE for codec) */
164     sink_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
165             video_fd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
166         gst_v4l2_probe_template_caps (it->device_path, video_fd,
167             V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE));
168
169     /* get src supported format */
170     src_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
171             video_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE),
172         gst_v4l2_probe_template_caps (it->device_path, video_fd,
173             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE));
174
175     /* Skip devices without any supported formats */
176     if (gst_caps_is_empty (sink_caps) || gst_caps_is_empty (src_caps)) {
177       gst_caps_unref (sink_caps);
178       gst_caps_unref (src_caps);
179       continue;
180     }
181
182     basename = g_path_get_basename (it->device_path);
183
184     if (gst_v4l2_is_video_dec (sink_caps, src_caps)) {
185       gst_v4l2_video_dec_register (plugin, basename, it->device_path,
186           sink_caps, src_caps);
187     } else if (gst_v4l2_is_video_enc (sink_caps, src_caps, NULL)) {
188       if (gst_v4l2_is_h264_enc (sink_caps, src_caps))
189         gst_v4l2_h264_enc_register (plugin, basename, it->device_path,
190             sink_caps, src_caps);
191
192       if (gst_v4l2_is_mpeg4_enc (sink_caps, src_caps))
193         gst_v4l2_mpeg4_enc_register (plugin, basename, it->device_path,
194             sink_caps, src_caps);
195
196       if (gst_v4l2_is_h263_enc (sink_caps, src_caps))
197         gst_v4l2_h263_enc_register (plugin, basename, it->device_path,
198             sink_caps, src_caps);
199
200       if (gst_v4l2_is_vp8_enc (sink_caps, src_caps))
201         gst_v4l2_vp8_enc_register (plugin, basename, it->device_path,
202             sink_caps, src_caps);
203
204       if (gst_v4l2_is_vp9_enc (sink_caps, src_caps))
205         gst_v4l2_vp9_enc_register (plugin, basename, it->device_path,
206             sink_caps, src_caps);
207     } else if (gst_v4l2_is_transform (sink_caps, src_caps)) {
208       gst_v4l2_transform_register (plugin, basename, it->device_path,
209           sink_caps, src_caps);
210     }
211     /* else if ( ... etc. */
212
213     gst_caps_unref (sink_caps);
214     gst_caps_unref (src_caps);
215     g_free (basename);
216   }
217
218   if (video_fd >= 0)
219     close (video_fd);
220
221   gst_v4l2_iterator_free (it);
222
223   return TRUE;
224 }
225 #endif
226
227 static gboolean
228 plugin_init (GstPlugin * plugin)
229 {
230   const gchar *paths[] = { "/dev", "/dev/v4l2", NULL };
231   const gchar *names[] = { "video", NULL };
232
233   GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
234
235   /* Add some depedency, so the dynamic features get updated upon changes in
236    * /dev/video* */
237   gst_plugin_add_dependency (plugin,
238       NULL, paths, names, GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX);
239
240   if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
241           GST_TYPE_V4L2SRC) ||
242       !gst_element_register (plugin, "v4l2sink", GST_RANK_NONE,
243           GST_TYPE_V4L2SINK) ||
244       !gst_element_register (plugin, "v4l2radio", GST_RANK_NONE,
245           GST_TYPE_V4L2RADIO) ||
246       !gst_device_provider_register (plugin, "v4l2deviceprovider",
247           GST_RANK_PRIMARY, GST_TYPE_V4L2_DEVICE_PROVIDER)
248       /* etc. */
249 #ifdef GST_V4L2_ENABLE_PROBE
250       || !gst_v4l2_probe_and_register (plugin)
251 #endif
252       )
253     return FALSE;
254
255 #ifdef ENABLE_NLS
256   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
257   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
258 #endif /* ENABLE_NLS */
259
260   return TRUE;
261 }
262
263 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
264     GST_VERSION_MINOR,
265     video4linux2,
266     "elements for Video 4 Linux",
267     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)