d4871f8d7f22d59e9c9cb2a292274384ce663745
[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@indt.org.br>
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 #include "gst/gst-i18n-plugin.h"
29
30 #include <gst/gst.h>
31
32 #include <fcntl.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37
38 #include "ext/videodev2.h"
39 #include "v4l2-utils.h"
40
41 #include "gstv4l2object.h"
42 #include "gstv4l2src.h"
43 #include "gstv4l2sink.h"
44 #include "gstv4l2radio.h"
45 #include "gstv4l2videodec.h"
46 #include "gstv4l2devicemonitor.h"
47 #include "gstv4l2transform.h"
48
49 /* used in v4l2_calls.c and v4l2src_calls.c */
50 GST_DEBUG_CATEGORY (v4l2_debug);
51 #define GST_CAT_DEFAULT v4l2_debug
52
53 /* This is a minimalist probe, for speed, we only enumerate formats */
54 static GstCaps *
55 gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
56     enum v4l2_buf_type type)
57 {
58   gint n;
59   struct v4l2_fmtdesc format;
60   GstCaps *caps;
61
62   GST_DEBUG ("Getting %s format enumerations", device);
63   caps = gst_caps_new_empty ();
64
65   for (n = 0;; n++) {
66     GstStructure *template;
67
68     memset (&format, 0, sizeof (format));
69
70     format.index = n;
71     format.type = type;
72
73     if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
74       break;                    /* end of enumeration */
75
76     GST_LOG ("index:       %u", format.index);
77     GST_LOG ("type:        %d", format.type);
78     GST_LOG ("flags:       %08x", format.flags);
79     GST_LOG ("description: '%s'", format.description);
80     GST_LOG ("pixelformat: %" GST_FOURCC_FORMAT,
81         GST_FOURCC_ARGS (format.pixelformat));
82
83     template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
84
85     if (template) {
86       GstStructure *alt_t = NULL;
87
88       switch (format.pixelformat) {
89         case V4L2_PIX_FMT_RGB32:
90           alt_t = gst_structure_copy (template);
91           gst_structure_set (alt_t, "format", G_TYPE_STRING, "ARGB", NULL);
92           break;
93         case V4L2_PIX_FMT_BGR32:
94           alt_t = gst_structure_copy (template);
95           gst_structure_set (alt_t, "format", G_TYPE_STRING, "BGRA", NULL);
96         default:
97           break;
98       }
99
100       gst_caps_append_structure (caps, template);
101
102       if (alt_t)
103         gst_caps_append_structure (caps, alt_t);
104     }
105   }
106
107   return gst_caps_simplify (caps);
108 }
109
110 static gboolean
111 gst_v4l2_probe_and_register (GstPlugin * plugin)
112 {
113   GstV4l2Iterator *it;
114   gint video_fd = -1;
115   struct v4l2_capability vcap;
116   gboolean ret = TRUE;
117
118   it = gst_v4l2_iterator_new ();
119
120   while (gst_v4l2_iterator_next (it)) {
121     GstCaps *src_caps, *sink_caps;
122     gchar *basename;
123
124     if (video_fd >= 0)
125       close (video_fd);
126
127     video_fd = open (it->device_path, O_RDWR);
128     if (video_fd == -1) {
129       GST_DEBUG ("Failed to open %s: %s", it->device_path, g_strerror (errno));
130       continue;
131     }
132
133     memset (&vcap, 0, sizeof (vcap));
134
135     if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
136       GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
137       continue;
138     }
139
140     if (!((vcap.capabilities & (V4L2_CAP_VIDEO_M2M |
141                     V4L2_CAP_VIDEO_M2M_MPLANE)) ||
142             /* But legacy driver may expose both CAPTURE and OUTPUT */
143             ((vcap.capabilities &
144                     (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
145                 (vcap.capabilities &
146                     (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
147       continue;
148
149     GST_DEBUG ("Probing '%s' located at '%s'",
150         it->device_name ? it->device_name : (const gchar *) vcap.driver,
151         it->device_path);
152
153     /* get sink supported format (no MPLANE for codec) */
154     sink_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
155             video_fd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
156         gst_v4l2_probe_template_caps (it->device_path, video_fd,
157             V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE));
158
159     /* get src supported format */
160     src_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
161             video_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE),
162         gst_v4l2_probe_template_caps (it->device_path, video_fd,
163             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE));
164
165     basename = g_path_get_basename (it->device_path);
166
167     if (gst_v4l2_is_video_dec (sink_caps, src_caps))
168       ret = gst_v4l2_video_dec_register (plugin, basename, it->device_path,
169           sink_caps, src_caps);
170     else if (gst_v4l2_is_transform (sink_caps, src_caps))
171       ret = gst_v4l2_transform_register (plugin, basename, it->device_path,
172           sink_caps, src_caps);
173     /* else if ( ... etc. */
174
175     gst_caps_unref (sink_caps);
176     gst_caps_unref (src_caps);
177     g_free (basename);
178
179     if (!ret)
180       break;
181   }
182
183   if (video_fd >= 0)
184     close (video_fd);
185
186   gst_v4l2_iterator_free (it);
187
188   return ret;
189 }
190
191 static gboolean
192 plugin_init (GstPlugin * plugin)
193 {
194   GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
195
196   if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
197           GST_TYPE_V4L2SRC) ||
198       !gst_element_register (plugin, "v4l2sink", GST_RANK_NONE,
199           GST_TYPE_V4L2SINK) ||
200       !gst_element_register (plugin, "v4l2radio", GST_RANK_NONE,
201           GST_TYPE_V4L2RADIO) ||
202       !gst_device_monitor_register (plugin, "v4l2monitor",
203           GST_RANK_PRIMARY, GST_TYPE_V4L2_DEVICE_MONITOR) ||
204       /* etc. */
205       !gst_v4l2_probe_and_register (plugin))
206     return FALSE;
207
208 #ifdef ENABLE_NLS
209   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
210   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
211 #endif /* ENABLE_NLS */
212
213   return TRUE;
214 }
215
216 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
217     GST_VERSION_MINOR,
218     video4linux2,
219     "elements for Video 4 Linux",
220     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)