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