2006-03-11 22:50:03 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
2017-02-26 00:47:03 +00:00
|
|
|
* 2006 Edgard Lima <edgard.lima@gmail.com>
|
2006-09-02 14:28:55 +00:00
|
|
|
*
|
|
|
|
* gstv4l2.c: plugin for v4l2 elements
|
2003-09-13 08:58:49 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2003-09-13 08:58:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2015-11-03 16:31:07 +00:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
# define _GNU_SOURCE /* O_CLOEXEC */
|
|
|
|
#endif
|
|
|
|
|
2004-01-20 01:51:47 +00:00
|
|
|
#include "gst/gst-i18n-plugin.h"
|
|
|
|
|
2006-03-11 22:50:03 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2014-05-03 01:38:30 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "ext/videodev2.h"
|
|
|
|
#include "v4l2-utils.h"
|
|
|
|
|
2006-05-11 17:59:59 +00:00
|
|
|
#include "gstv4l2object.h"
|
2003-09-13 08:58:49 +00:00
|
|
|
#include "gstv4l2src.h"
|
2009-08-04 07:14:20 +00:00
|
|
|
#include "gstv4l2sink.h"
|
2011-03-26 22:19:58 +00:00
|
|
|
#include "gstv4l2radio.h"
|
2013-11-28 22:14:18 +00:00
|
|
|
#include "gstv4l2videodec.h"
|
2017-05-23 18:40:56 +00:00
|
|
|
#include "gstv4l2h264enc.h"
|
2014-06-26 18:52:57 +00:00
|
|
|
#include "gstv4l2deviceprovider.h"
|
2014-03-04 23:31:27 +00:00
|
|
|
#include "gstv4l2transform.h"
|
2006-03-11 22:50:03 +00:00
|
|
|
|
2017-07-13 00:49:47 +00:00
|
|
|
/* used in gstv4l2object.c and v4l2_calls.c */
|
2006-09-02 14:45:04 +00:00
|
|
|
GST_DEBUG_CATEGORY (v4l2_debug);
|
2014-05-03 01:38:30 +00:00
|
|
|
#define GST_CAT_DEFAULT v4l2_debug
|
|
|
|
|
2015-11-13 17:35:59 +00:00
|
|
|
#ifdef GST_V4L2_ENABLE_PROBE
|
2014-05-03 01:38:30 +00:00
|
|
|
/* This is a minimalist probe, for speed, we only enumerate formats */
|
|
|
|
static GstCaps *
|
|
|
|
gst_v4l2_probe_template_caps (const gchar * device, gint video_fd,
|
|
|
|
enum v4l2_buf_type type)
|
|
|
|
{
|
|
|
|
gint n;
|
|
|
|
struct v4l2_fmtdesc format;
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
GST_DEBUG ("Getting %s format enumerations", device);
|
|
|
|
caps = gst_caps_new_empty ();
|
|
|
|
|
|
|
|
for (n = 0;; n++) {
|
|
|
|
GstStructure *template;
|
|
|
|
|
|
|
|
memset (&format, 0, sizeof (format));
|
|
|
|
|
|
|
|
format.index = n;
|
|
|
|
format.type = type;
|
|
|
|
|
|
|
|
if (ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
|
|
|
|
break; /* end of enumeration */
|
|
|
|
|
|
|
|
GST_LOG ("index: %u", format.index);
|
|
|
|
GST_LOG ("type: %d", format.type);
|
|
|
|
GST_LOG ("flags: %08x", format.flags);
|
|
|
|
GST_LOG ("description: '%s'", format.description);
|
|
|
|
GST_LOG ("pixelformat: %" GST_FOURCC_FORMAT,
|
|
|
|
GST_FOURCC_ARGS (format.pixelformat));
|
|
|
|
|
|
|
|
template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
|
|
|
|
|
2014-03-19 21:25:16 +00:00
|
|
|
if (template) {
|
|
|
|
GstStructure *alt_t = NULL;
|
|
|
|
|
|
|
|
switch (format.pixelformat) {
|
|
|
|
case V4L2_PIX_FMT_RGB32:
|
|
|
|
alt_t = gst_structure_copy (template);
|
|
|
|
gst_structure_set (alt_t, "format", G_TYPE_STRING, "ARGB", NULL);
|
|
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_BGR32:
|
|
|
|
alt_t = gst_structure_copy (template);
|
|
|
|
gst_structure_set (alt_t, "format", G_TYPE_STRING, "BGRA", NULL);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-03 01:38:30 +00:00
|
|
|
gst_caps_append_structure (caps, template);
|
2014-03-19 21:25:16 +00:00
|
|
|
|
|
|
|
if (alt_t)
|
|
|
|
gst_caps_append_structure (caps, alt_t);
|
|
|
|
}
|
2014-05-03 01:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gst_caps_simplify (caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_probe_and_register (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
GstV4l2Iterator *it;
|
|
|
|
gint video_fd = -1;
|
|
|
|
struct v4l2_capability vcap;
|
|
|
|
gboolean ret = TRUE;
|
2016-07-02 08:56:07 +00:00
|
|
|
guint32 device_caps;
|
2014-05-03 01:38:30 +00:00
|
|
|
|
|
|
|
it = gst_v4l2_iterator_new ();
|
|
|
|
|
|
|
|
while (gst_v4l2_iterator_next (it)) {
|
|
|
|
GstCaps *src_caps, *sink_caps;
|
|
|
|
gchar *basename;
|
|
|
|
|
2014-05-05 11:07:25 +00:00
|
|
|
if (video_fd >= 0)
|
2014-05-03 01:38:30 +00:00
|
|
|
close (video_fd);
|
|
|
|
|
2015-11-03 16:31:07 +00:00
|
|
|
video_fd = open (it->device_path, O_RDWR | O_CLOEXEC);
|
|
|
|
|
2014-05-03 01:38:30 +00:00
|
|
|
if (video_fd == -1) {
|
|
|
|
GST_DEBUG ("Failed to open %s: %s", it->device_path, g_strerror (errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&vcap, 0, sizeof (vcap));
|
|
|
|
|
|
|
|
if (ioctl (video_fd, VIDIOC_QUERYCAP, &vcap) < 0) {
|
|
|
|
GST_DEBUG ("Failed to get device capabilities: %s", g_strerror (errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-02 08:56:07 +00:00
|
|
|
if (vcap.capabilities & V4L2_CAP_DEVICE_CAPS)
|
|
|
|
device_caps = vcap.device_caps;
|
|
|
|
else
|
2016-07-07 22:53:54 +00:00
|
|
|
device_caps = vcap.capabilities;
|
2016-07-02 08:56:07 +00:00
|
|
|
|
|
|
|
if (!((device_caps & (V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE)) ||
|
2014-05-03 01:38:30 +00:00
|
|
|
/* But legacy driver may expose both CAPTURE and OUTPUT */
|
2016-07-02 08:56:07 +00:00
|
|
|
((device_caps &
|
2014-05-03 01:38:30 +00:00
|
|
|
(V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) &&
|
2016-07-02 08:56:07 +00:00
|
|
|
(device_caps &
|
2014-05-03 01:38:30 +00:00
|
|
|
(V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
GST_DEBUG ("Probing '%s' located at '%s'",
|
|
|
|
it->device_name ? it->device_name : (const gchar *) vcap.driver,
|
|
|
|
it->device_path);
|
|
|
|
|
|
|
|
/* get sink supported format (no MPLANE for codec) */
|
|
|
|
sink_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
|
|
|
|
video_fd, V4L2_BUF_TYPE_VIDEO_OUTPUT),
|
|
|
|
gst_v4l2_probe_template_caps (it->device_path, video_fd,
|
|
|
|
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE));
|
|
|
|
|
|
|
|
/* get src supported format */
|
|
|
|
src_caps = gst_caps_merge (gst_v4l2_probe_template_caps (it->device_path,
|
|
|
|
video_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE),
|
|
|
|
gst_v4l2_probe_template_caps (it->device_path, video_fd,
|
|
|
|
V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE));
|
|
|
|
|
2015-11-19 22:20:55 +00:00
|
|
|
/* Skip devices without any supported formats */
|
|
|
|
if (gst_caps_is_empty (sink_caps) || gst_caps_is_empty (src_caps)) {
|
|
|
|
gst_caps_unref (sink_caps);
|
|
|
|
gst_caps_unref (src_caps);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-03 01:38:30 +00:00
|
|
|
basename = g_path_get_basename (it->device_path);
|
|
|
|
|
|
|
|
if (gst_v4l2_is_video_dec (sink_caps, src_caps))
|
|
|
|
ret = gst_v4l2_video_dec_register (plugin, basename, it->device_path,
|
|
|
|
sink_caps, src_caps);
|
2017-05-23 18:40:56 +00:00
|
|
|
else if (gst_v4l2_is_h264_enc (sink_caps, src_caps))
|
|
|
|
ret = gst_v4l2_h264_enc_register (plugin, basename, it->device_path,
|
|
|
|
sink_caps, src_caps);
|
2014-03-04 23:31:27 +00:00
|
|
|
else if (gst_v4l2_is_transform (sink_caps, src_caps))
|
|
|
|
ret = gst_v4l2_transform_register (plugin, basename, it->device_path,
|
|
|
|
sink_caps, src_caps);
|
2014-05-03 01:38:30 +00:00
|
|
|
/* else if ( ... etc. */
|
|
|
|
|
|
|
|
gst_caps_unref (sink_caps);
|
|
|
|
gst_caps_unref (src_caps);
|
|
|
|
g_free (basename);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-05 11:07:25 +00:00
|
|
|
if (video_fd >= 0)
|
2014-05-03 01:38:30 +00:00
|
|
|
close (video_fd);
|
|
|
|
|
|
|
|
gst_v4l2_iterator_free (it);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-11-13 17:35:59 +00:00
|
|
|
#endif
|
2003-09-13 08:58:49 +00:00
|
|
|
|
2003-11-03 22:47:52 +00:00
|
|
|
static gboolean
|
2004-03-14 22:34:33 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2003-09-13 08:58:49 +00:00
|
|
|
{
|
2015-11-13 17:35:59 +00:00
|
|
|
const gchar *paths[] = { "/dev", "/dev/v4l2", NULL };
|
|
|
|
const gchar *names[] = { "video", NULL };
|
|
|
|
|
2006-03-11 22:50:03 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (v4l2_debug, "v4l2", 0, "V4L2 API calls");
|
|
|
|
|
2015-11-13 17:35:59 +00:00
|
|
|
/* Add some depedency, so the dynamic features get updated upon changes in
|
|
|
|
* /dev/video* */
|
|
|
|
gst_plugin_add_dependency (plugin,
|
|
|
|
NULL, paths, names, GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX);
|
|
|
|
|
2008-11-14 15:42:32 +00:00
|
|
|
if (!gst_element_register (plugin, "v4l2src", GST_RANK_PRIMARY,
|
2009-08-04 07:14:20 +00:00
|
|
|
GST_TYPE_V4L2SRC) ||
|
2010-02-12 16:11:30 +00:00
|
|
|
!gst_element_register (plugin, "v4l2sink", GST_RANK_NONE,
|
2009-08-04 07:14:20 +00:00
|
|
|
GST_TYPE_V4L2SINK) ||
|
2011-03-26 22:19:58 +00:00
|
|
|
!gst_element_register (plugin, "v4l2radio", GST_RANK_NONE,
|
|
|
|
GST_TYPE_V4L2RADIO) ||
|
2014-06-26 18:52:57 +00:00
|
|
|
!gst_device_provider_register (plugin, "v4l2deviceprovider",
|
2015-11-13 17:35:59 +00:00
|
|
|
GST_RANK_PRIMARY, GST_TYPE_V4L2_DEVICE_PROVIDER)
|
2014-05-02 22:18:26 +00:00
|
|
|
/* etc. */
|
2015-11-13 17:35:59 +00:00
|
|
|
#ifdef GST_V4L2_ENABLE_PROBE
|
|
|
|
|| !gst_v4l2_probe_and_register (plugin)
|
|
|
|
#endif
|
|
|
|
)
|
2003-09-13 08:58:49 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2004-01-19 15:45:55 +00:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
|
2008-08-07 16:11:00 +00:00
|
|
|
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
2004-01-19 15:45:55 +00:00
|
|
|
#endif /* ENABLE_NLS */
|
|
|
|
|
2003-09-13 08:58:49 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2012-04-05 15:36:38 +00:00
|
|
|
video4linux2,
|
2006-03-11 22:50:03 +00:00
|
|
|
"elements for Video 4 Linux",
|
2006-04-01 10:09:11 +00:00
|
|
|
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|