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 "gstv4l2devicemonitor.h"
48 /* used in v4l2_calls.c and v4l2src_calls.c */
49 GST_DEBUG_CATEGORY (v4l2_debug);
50 #define GST_CAT_DEFAULT v4l2_debug
52 /* This is a minimalist probe, for speed, we only enumerate formats */
54 gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
55 enum v4l2_buf_type type)
58 struct v4l2_fmtdesc format;
61 GST_DEBUG ("Getting %s format enumerations", device);
62 caps = gst_caps_new_empty ();
65 GstStructure *template;
67 memset (&format, 0, sizeof (format));
72 if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
73 break; /* end of enumeration */
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));
82 template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
85 gst_caps_append_structure (caps, template);
88 return gst_caps_simplify (caps);
92 gst_v4l2_probe_and_register (GstPlugin * plugin)
96 struct v4l2_capability vcap;
99 it = gst_v4l2_iterator_new ();
101 while (gst_v4l2_iterator_next (it)) {
102 GstCaps *src_caps, *sink_caps;
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));
114 memset (&vcap, 0, sizeof (vcap));
116 if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
117 GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
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)) &&
127 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
130 GST_DEBUG ("Probing '%s' located at '%s'",
131 it->device_name ? it->device_name : (const gchar *) vcap.driver,
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));
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));
146 basename = g_path_get_basename (it->device_path);
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. */
153 gst_caps_unref (sink_caps);
154 gst_caps_unref (src_caps);
164 gst_v4l2_iterator_free (it);
170 plugin_init (GstPlugin * plugin)
172 GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
174 if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
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) ||
183 !gst_v4l2_probe_and_register (plugin))
187 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
188 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
189 #endif /* ENABLE_NLS */
194 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
197 "elements for Video 4 Linux",
198 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)