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