[v4l2] Change feature name
[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 "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 *
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 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
103         case V4L2_PIX_FMT_YUV420:
104           alt_t = gst_structure_copy (template);
105           gst_structure_set (alt_t, "format", G_TYPE_STRING, "S420", NULL);
106           break;
107         case V4L2_PIX_FMT_NV12:
108           alt_t = gst_structure_copy (template);
109           gst_structure_set (alt_t, "format", G_TYPE_STRING, "SN12", NULL);
110           break;
111 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
112         case V4L2_PIX_FMT_RGB32:
113           alt_t = gst_structure_copy (template);
114           gst_structure_set (alt_t, "format", G_TYPE_STRING, "ARGB", NULL);
115           break;
116         case V4L2_PIX_FMT_BGR32:
117           alt_t = gst_structure_copy (template);
118           gst_structure_set (alt_t, "format", G_TYPE_STRING, "BGRA", NULL);
119         default:
120           break;
121       }
122
123       gst_caps_append_structure (caps, template);
124
125       if (alt_t)
126         gst_caps_append_structure (caps, alt_t);
127     }
128   }
129
130   return gst_caps_simplify (caps);
131 }
132
133 static gboolean
134 gst_v4l2_probe_and_register (GstPlugin * plugin)
135 {
136   GstV4l2Iterator *it;
137   gint video_fd = -1;
138   struct v4l2_capability vcap;
139   guint32 device_caps;
140
141   GST_DEBUG ("Probing devices");
142
143   it = gst_v4l2_iterator_new ();
144
145   while (gst_v4l2_iterator_next (it)) {
146     GstCaps *src_caps, *sink_caps;
147     gchar *basename;
148
149     if (video_fd >= 0)
150       close (video_fd);
151
152     video_fd = open (it->device_path, O_RDWR | O_CLOEXEC);
153
154     if (video_fd == -1) {
155       GST_DEBUG ("Failed to open %s: %s", it->device_path, g_strerror (errno));
156       continue;
157     }
158
159     memset (&vcap, 0, sizeof (vcap));
160
161     if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
162       GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
163       continue;
164     }
165
166     if (vcap.capabilities & V4L2_CAP_DEVICE_CAPS)
167       device_caps = vcap.device_caps;
168     else
169       device_caps = vcap.capabilities;
170
171     if (!GST_V4L2_IS_M2M (device_caps))
172       continue;
173
174     GST_DEBUG ("Probing '%s' located at '%s'",
175         it->device_name ? it->device_name : (const gchar *) vcap.driver,
176         it->device_path);
177
178     /* get sink supported format (no MPLANE for codec) */
179     sink_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
180             video_fd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
181         gst_v4l2_probe_template_caps (it->device_path, video_fd,
182             V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE));
183
184     /* get src supported format */
185     src_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
186             video_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE),
187         gst_v4l2_probe_template_caps (it->device_path, video_fd,
188             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE));
189
190     /* Skip devices without any supported formats */
191     if (gst_caps_is_empty (sink_caps) || gst_caps_is_empty (src_caps)) {
192       gst_caps_unref (sink_caps);
193       gst_caps_unref (src_caps);
194       continue;
195     }
196
197     basename = g_path_get_basename (it->device_path);
198
199     /* Caps won't be freed if the subclass is not instantiated */
200     GST_MINI_OBJECT_FLAG_SET (sink_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
201     GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
202
203     if (gst_v4l2_is_video_dec (sink_caps, src_caps)) {
204       gst_v4l2_video_dec_register (plugin, basename, it->device_path,
205           sink_caps, src_caps);
206     } else if (gst_v4l2_is_video_enc (sink_caps, src_caps, NULL)) {
207       if (gst_v4l2_is_fwht_enc (sink_caps, src_caps))
208         gst_v4l2_fwht_enc_register (plugin, basename, it->device_path,
209             sink_caps, src_caps);
210
211       if (gst_v4l2_is_h264_enc (sink_caps, src_caps))
212         gst_v4l2_h264_enc_register (plugin, basename, it->device_path,
213             sink_caps, src_caps);
214
215       if (gst_v4l2_is_h265_enc (sink_caps, src_caps))
216         gst_v4l2_h265_enc_register (plugin, basename, it->device_path,
217             sink_caps, src_caps);
218
219       if (gst_v4l2_is_mpeg4_enc (sink_caps, src_caps))
220         gst_v4l2_mpeg4_enc_register (plugin, basename, it->device_path,
221             sink_caps, src_caps);
222
223       if (gst_v4l2_is_h263_enc (sink_caps, src_caps))
224         gst_v4l2_h263_enc_register (plugin, basename, it->device_path,
225             sink_caps, src_caps);
226
227       if (gst_v4l2_is_jpeg_enc (sink_caps, src_caps))
228         gst_v4l2_jpeg_enc_register (plugin, basename, it->device_path,
229             sink_caps, src_caps);
230
231       if (gst_v4l2_is_vp8_enc (sink_caps, src_caps))
232         gst_v4l2_vp8_enc_register (plugin, basename, it->device_path,
233             sink_caps, src_caps);
234
235       if (gst_v4l2_is_vp9_enc (sink_caps, src_caps))
236         gst_v4l2_vp9_enc_register (plugin, basename, it->device_path,
237             sink_caps, src_caps);
238     } else if (gst_v4l2_is_transform (sink_caps, src_caps)) {
239       gst_v4l2_transform_register (plugin, basename, it->device_path,
240           sink_caps, src_caps);
241     }
242     /* else if ( ... etc. */
243
244     gst_caps_unref (sink_caps);
245     gst_caps_unref (src_caps);
246     g_free (basename);
247   }
248
249   if (video_fd >= 0)
250     close (video_fd);
251
252   gst_v4l2_iterator_free (it);
253
254   return TRUE;
255 }
256 #endif
257
258 static gboolean
259 plugin_init (GstPlugin * plugin)
260 {
261 #ifndef TIZEN_FEATURE_DISABLE_V4L2_DEPENDENCY
262   const gchar *paths[] = { "/dev", "/dev/v4l2", NULL };
263   const gchar *names[] = { "video", NULL };
264 #endif /* TIZEN_FEATURE_DISABLE_V4L2_DEPENDENCY */
265
266   GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
267 #ifndef TIZEN_FEATURE_DISABLE_V4L2_DEPENDENCY
268   /* Add some depedency, so the dynamic features get updated upon changes in
269    * /dev/video* */
270   gst_plugin_add_dependency (plugin,
271       NULL, paths, names, GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX);
272 #endif /* TIZEN_FEATURE_DISABLE_V4L2_DEPENDENCY */
273
274   if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
275           GST_TYPE_V4L2SRC) ||
276       !gst_element_register (plugin, "v4l2sink", GST_RANK_NONE,
277           GST_TYPE_V4L2SINK) ||
278       !gst_element_register (plugin, "v4l2radio", GST_RANK_NONE,
279           GST_TYPE_V4L2RADIO) ||
280       !gst_device_provider_register (plugin, "v4l2deviceprovider",
281           GST_RANK_PRIMARY, GST_TYPE_V4L2_DEVICE_PROVIDER)
282       /* etc. */
283 #ifdef GST_V4L2_ENABLE_PROBE
284       || !gst_v4l2_probe_and_register (plugin)
285 #endif
286       )
287     return FALSE;
288
289 #ifdef ENABLE_NLS
290   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
291   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
292 #endif /* ENABLE_NLS */
293
294   return TRUE;
295 }
296
297 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
298     GST_VERSION_MINOR,
299     video4linux2,
300     "elements for Video 4 Linux",
301     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)