3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
6 * gstv4l2.c: plugin for v4l2 elements
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.
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.
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.
28 #include "gst/gst-i18n-plugin.h"
35 #include <sys/types.h>
38 #include "ext/videodev2.h"
39 #include "v4l2-utils.h"
41 #include "gstv4l2object.h"
42 #include "gstv4l2src.h"
43 #include "gstv4l2sink.h"
44 #include "gstv4l2radio.h"
45 #include "gstv4l2videodec.h"
46 #include "gstv4l2deviceprovider.h"
47 #include "gstv4l2transform.h"
49 /* used in v4l2_calls.c and v4l2src_calls.c */
50 GST_DEBUG_CATEGORY (v4l2_debug);
51 #define GST_CAT_DEFAULT v4l2_debug
53 /* This is a minimalist probe, for speed, we only enumerate formats */
55 gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
56 enum v4l2_buf_type type)
59 struct v4l2_fmtdesc format;
62 GST_DEBUG ("Getting %s format enumerations", device);
63 caps = gst_caps_new_empty ();
66 GstStructure *template;
68 memset (&format, 0, sizeof (format));
73 if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
74 break; /* end of enumeration */
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));
83 template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
86 GstStructure *alt_t = NULL;
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);
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);
100 gst_caps_append_structure (caps, template);
103 gst_caps_append_structure (caps, alt_t);
107 return gst_caps_simplify (caps);
111 gst_v4l2_probe_and_register (GstPlugin * plugin)
115 struct v4l2_capability vcap;
118 it = gst_v4l2_iterator_new ();
120 while (gst_v4l2_iterator_next (it)) {
121 GstCaps *src_caps, *sink_caps;
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));
133 memset (&vcap, 0, sizeof (vcap));
135 if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
136 GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
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)) &&
146 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
149 GST_DEBUG ("Probing '%s' located at '%s'",
150 it->device_name ? it->device_name : (const gchar *) vcap.driver,
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));
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));
165 basename = g_path_get_basename (it->device_path);
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. */
175 gst_caps_unref (sink_caps);
176 gst_caps_unref (src_caps);
186 gst_v4l2_iterator_free (it);
192 plugin_init (GstPlugin * plugin)
194 GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
196 if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
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_provider_register (plugin, "v4l2deviceprovider",
203 GST_RANK_PRIMARY, GST_TYPE_V4L2_DEVICE_PROVIDER) ||
205 !gst_v4l2_probe_and_register (plugin))
209 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
210 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
211 #endif /* ENABLE_NLS */
216 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
219 "elements for Video 4 Linux",
220 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)