/* GStreamer
*
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
- * 2006 Edgard Lima <edgard.lima@indt.org.br>
+ * 2006 Edgard Lima <edgard.lima@gmail.com>
*
* gstv4l2src.c: Video4Linux2 source element
*
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
*/
/**
* <refsect2>
* <title>Example launch lines</title>
* |[
- * gst-launch v4l2src ! xvimagesink
+ * gst-launch-1.0 v4l2src ! xvimagesink
* ]| This pipeline shows the video captured from /dev/video0 tv card and for
* webcams.
* |[
- * gst-launch v4l2src ! jpegdec ! xvimagesink
+ * gst-launch-1.0 v4l2src ! jpegdec ! xvimagesink
* ]| This pipeline shows the video captured from a webcam that delivers jpeg
* images.
* </refsect2>
+ *
+ * Since 1.14, the use of libv4l2 has been disabled due to major bugs in the
+ * emulation layer. To enable usage of this library, set the environment
+ * variable GST_V4L2_USE_LIBV4L2=1.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
-#undef HAVE_XVIDEO
-
#include <string.h>
#include <sys/time.h>
-#include "v4l2src_calls.h"
#include <unistd.h>
+#include <gst/video/gstvideometa.h>
+#include <gst/video/gstvideopool.h>
+
+#include "gstv4l2src.h"
+
#include "gstv4l2colorbalance.h"
#include "gstv4l2tuner.h"
-#ifdef HAVE_XVIDEO
-#include "gstv4l2xoverlay.h"
-#endif
#include "gstv4l2vidorient.h"
#include "gst/gst-i18n-plugin.h"
GST_DEBUG_CATEGORY (v4l2src_debug);
#define GST_CAT_DEFAULT v4l2src_debug
-#define PROP_DEF_QUEUE_SIZE 2
-#define PROP_DEF_ALWAYS_COPY TRUE
-#define PROP_DEF_DECIMATE 1
-
#define DEFAULT_PROP_DEVICE "/dev/video0"
enum
{
PROP_0,
V4L2_STD_OBJECT_PROPS,
- PROP_QUEUE_SIZE,
- PROP_ALWAYS_COPY,
- PROP_DECIMATE
+#ifdef TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID
+ PROP_CAMERA_ID,
+#endif /* TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID */
+ PROP_LAST
+};
+
+/* signals and args */
+enum
+{
+ SIGNAL_PRE_SET_FORMAT,
+ LAST_SIGNAL
};
-GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SrcClass, gst_v4l2src);
+static guint gst_v4l2_signals[LAST_SIGNAL] = { 0 };
+
GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
-#ifdef HAVE_XVIDEO
-GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
-#endif
GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
static void gst_v4l2src_uri_handler_init (gpointer g_iface,
gpointer iface_data);
-static gboolean
-gst_v4l2src_iface_supported (GstImplementsInterface * iface, GType iface_type)
-{
- GstV4l2Object *v4l2object = GST_V4L2SRC (iface)->v4l2object;
-
-#ifdef HAVE_XVIDEO
- if (!(iface_type == GST_TYPE_TUNER ||
- iface_type == GST_TYPE_X_OVERLAY ||
- iface_type == GST_TYPE_COLOR_BALANCE ||
- iface_type == GST_TYPE_VIDEO_ORIENTATION))
- return FALSE;
-#else
- if (!(iface_type == GST_TYPE_TUNER ||
- iface_type == GST_TYPE_COLOR_BALANCE ||
- iface_type == GST_TYPE_VIDEO_ORIENTATION))
- return FALSE;
-#endif
-
- if (v4l2object->video_fd == -1)
- return FALSE;
-
-#ifdef HAVE_XVIDEO
- if (iface_type == GST_TYPE_X_OVERLAY && !GST_V4L2_IS_OVERLAY (v4l2object))
- return FALSE;
-#endif
-
- return TRUE;
-}
-
-static void
-gst_v4l2src_interface_init (GstImplementsInterfaceClass * klass)
-{
- /*
- * default virtual functions
- */
- klass->supported = gst_v4l2src_iface_supported;
-}
-
-static void
-gst_v4l2src_init_interfaces (GType type)
-{
- static const GInterfaceInfo urihandler_info = {
- gst_v4l2src_uri_handler_init,
- NULL,
- NULL
- };
-
- static const GInterfaceInfo v4l2iface_info = {
- (GInterfaceInitFunc) gst_v4l2src_interface_init,
- NULL,
- NULL,
- };
- static const GInterfaceInfo v4l2_tuner_info = {
- (GInterfaceInitFunc) gst_v4l2src_tuner_interface_init,
- NULL,
- NULL,
- };
-#ifdef HAVE_XVIDEO
- /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
- static const GInterfaceInfo v4l2_xoverlay_info = {
- (GInterfaceInitFunc) gst_v4l2src_xoverlay_interface_init,
- NULL,
- NULL,
- };
-#endif
- static const GInterfaceInfo v4l2_colorbalance_info = {
- (GInterfaceInitFunc) gst_v4l2src_color_balance_interface_init,
- NULL,
- NULL,
- };
- static const GInterfaceInfo v4l2_videoorientation_info = {
- (GInterfaceInitFunc) gst_v4l2src_video_orientation_interface_init,
- NULL,
- NULL,
- };
- static const GInterfaceInfo v4l2_propertyprobe_info = {
- (GInterfaceInitFunc) gst_v4l2src_property_probe_interface_init,
- NULL,
- NULL,
- };
-
- g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
- g_type_add_interface_static (type,
- GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
- g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
-#ifdef HAVE_XVIDEO
- g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
-#endif
- g_type_add_interface_static (type,
- GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
- g_type_add_interface_static (type,
- GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
- g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
- &v4l2_propertyprobe_info);
-}
-
-GST_BOILERPLATE_FULL (GstV4l2Src, gst_v4l2src, GstPushSrc, GST_TYPE_PUSH_SRC,
- gst_v4l2src_init_interfaces);
+#define gst_v4l2src_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
+ G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
+ G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
+ G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
+ gst_v4l2src_color_balance_interface_init);
+ G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
+ gst_v4l2src_video_orientation_interface_init));
-static void gst_v4l2src_dispose (GObject * object);
static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
/* element methods */
static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
static gboolean gst_v4l2src_stop (GstBaseSrc * src);
-static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
-static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src);
+static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
+static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
+ GstQuery * query);
static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
-static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
+static GstCaps *gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps,
+ GstStructure * pref_s);
static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
static void gst_v4l2src_set_property (GObject * object, guint prop_id,
static void gst_v4l2src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
-/* get_frame io methods */
-static GstFlowReturn
-gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf);
-static GstFlowReturn
-gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf);
-
-static void
-gst_v4l2src_base_init (gpointer g_class)
-{
- GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
- GstV4l2SrcClass *gstv4l2src_class = GST_V4L2SRC_CLASS (g_class);
- GstPadTemplate *pad_template;
-
- gstv4l2src_class->v4l2_class_devices = NULL;
-
- GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
-
- gst_element_class_set_details_simple (gstelement_class,
- "Video (video4linux2) Source", "Source/Video",
- "Reads frames from a Video4Linux2 device",
- "Edgard Lima <edgard.lima@indt.org.br>,"
- " Stefan Kost <ensonic@users.sf.net>");
-
- pad_template =
- gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
- gst_v4l2_object_get_all_caps ());
- gst_element_class_add_pad_template (gstelement_class, pad_template);
- gst_object_unref (pad_template);
-}
-
static void
gst_v4l2src_class_init (GstV4l2SrcClass * klass)
{
basesrc_class = GST_BASE_SRC_CLASS (klass);
pushsrc_class = GST_PUSH_SRC_CLASS (klass);
- gobject_class->dispose = gst_v4l2src_dispose;
gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
gobject_class->set_property = gst_v4l2src_set_property;
gobject_class->get_property = gst_v4l2src_get_property;
gst_v4l2_object_install_properties_helper (gobject_class,
DEFAULT_PROP_DEVICE);
- g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
- g_param_spec_uint ("queue-size", "Queue size",
- "Number of buffers to be enqueud in the driver in streaming mode",
- GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
- G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
- g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
- g_param_spec_boolean ("always-copy", "Always Copy",
- "If the buffer will or not be used directly from mmap",
- PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+#ifdef TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID
/**
- * GstV4l2Src:decimate
+ * GstV4l2Src:camera-id:
*
- * Only use every nth frame
+ * The value which is set by application will be used as a number of device node.
+ * ex) 1 -> /dev/video1
+ */
+ g_object_class_install_property (gobject_class, PROP_CAMERA_ID,
+ g_param_spec_uint ("camera-id", "Camera ID",
+ "Camera ID for device node", 0, G_MAXUINT, 0,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+#endif /* TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID */
+
+ /**
+ * GstV4l2Src::prepare-format:
+ * @v4l2src: the v4l2src instance
+ * @fd: the file descriptor of the current device
+ * @caps: the caps of the format being set
*
- * Since: 0.10.26
+ * This signal gets emitted before calling the v4l2 VIDIOC_S_FMT ioctl
+ * (set format). This allows for any custom configuration of the device to
+ * happen prior to the format being set.
+ * This is mostly useful for UVC H264 encoding cameras which need the H264
+ * Probe & Commit to happen prior to the normal Probe & Commit.
*/
- g_object_class_install_property (gobject_class, PROP_DECIMATE,
- g_param_spec_int ("decimate", "Decimate",
- "Only use every nth frame", 1, G_MAXINT,
- PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT] = g_signal_new ("prepare-format",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CAPS);
+
+ gst_element_class_set_static_metadata (element_class,
+ "Video (video4linux2) Source", "Source/Video",
+ "Reads frames from a Video4Linux2 device",
+ "Edgard Lima <edgard.lima@gmail.com>, "
+ "Stefan Kost <ensonic@users.sf.net>");
+
+ gst_element_class_add_pad_template
+ (element_class,
+ gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
+ gst_v4l2_object_get_all_caps ()));
basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
- basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
- basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
+ basesrc_class->decide_allocation =
+ GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
+
+ klass->v4l2_class_devices = NULL;
+
+ GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
}
static void
-gst_v4l2src_init (GstV4l2Src * v4l2src, GstV4l2SrcClass * klass)
+gst_v4l2src_init (GstV4l2Src * v4l2src)
{
/* fixme: give an update_fps_function */
v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
- V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
- gst_v4l2_get_input, gst_v4l2_set_input, NULL);
-
- /* number of buffers requested */
- v4l2src->num_buffers = PROP_DEF_QUEUE_SIZE;
-
- v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
- v4l2src->decimate = PROP_DEF_DECIMATE;
+ GST_OBJECT (GST_BASE_SRC_PAD (v4l2src)), V4L2_BUF_TYPE_VIDEO_CAPTURE,
+ DEFAULT_PROP_DEVICE, gst_v4l2_get_input, gst_v4l2_set_input, NULL);
- v4l2src->is_capturing = FALSE;
+ /* Avoid the slow probes */
+ v4l2src->v4l2object->skip_try_fmt_probes = TRUE;
gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
-
- v4l2src->fps_d = 0;
- v4l2src->fps_n = 0;
-}
-
-
-static void
-gst_v4l2src_dispose (GObject * object)
-{
- GstV4l2Src *v4l2src = GST_V4L2SRC (object);
-
- if (v4l2src->probed_caps) {
- gst_caps_unref (v4l2src->probed_caps);
- }
-
- G_OBJECT_CLASS (parent_class)->dispose (object);
}
if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
prop_id, value, pspec)) {
switch (prop_id) {
- case PROP_QUEUE_SIZE:
- v4l2src->num_buffers = g_value_get_uint (value);
- break;
- case PROP_ALWAYS_COPY:
- v4l2src->always_copy = g_value_get_boolean (value);
- break;
- case PROP_DECIMATE:
- v4l2src->decimate = g_value_get_int (value);
+#ifdef TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID
+ case PROP_CAMERA_ID:
+ g_free (v4l2src->v4l2object->videodev);
+
+ v4l2src->camera_id = g_value_get_uint (value);
+ v4l2src->v4l2object->videodev = g_strdup_printf ("/dev/video%u", v4l2src->camera_id);
+
+ GST_INFO_OBJECT(v4l2src, "videodev [%s]", v4l2src->v4l2object->videodev);
break;
+#endif /* TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID */
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
-
static void
gst_v4l2src_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
prop_id, value, pspec)) {
switch (prop_id) {
- case PROP_QUEUE_SIZE:
- g_value_set_uint (value, v4l2src->num_buffers);
- break;
- case PROP_ALWAYS_COPY:
- g_value_set_boolean (value, v4l2src->always_copy);
- break;
- case PROP_DECIMATE:
- g_value_set_int (value, v4l2src->decimate);
+#ifdef TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID
+ case PROP_CAMERA_ID:
+ g_value_set_uint (value, v4l2src->camera_id);
break;
+#endif /* TIZEN_FEATURE_V4L2SRC_SUPPORT_CAMERA_ID */
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
+struct PreferedCapsInfo
+{
+ gint width;
+ gint height;
+ gint fps_n;
+ gint fps_d;
+};
+
+static gboolean
+gst_vl42_src_fixate_fields (GQuark field_id, GValue * value, gpointer user_data)
+{
+ GstStructure *s = user_data;
+
+ if (field_id == g_quark_from_string ("interlace-mode"))
+ return TRUE;
+
+ if (field_id == g_quark_from_string ("colorimetry"))
+ return TRUE;
+
+ gst_structure_fixate_field (s, g_quark_to_string (field_id));
+
+ return TRUE;
+}
+
+static void
+gst_v4l2_src_fixate_struct_with_preference (GstStructure * s,
+ struct PreferedCapsInfo *pref)
+{
+ if (gst_structure_has_field (s, "width"))
+ gst_structure_fixate_field_nearest_int (s, "width", pref->width);
+
+ if (gst_structure_has_field (s, "height"))
+ gst_structure_fixate_field_nearest_int (s, "height", pref->height);
+
+ if (gst_structure_has_field (s, "framerate"))
+ gst_structure_fixate_field_nearest_fraction (s, "framerate", pref->fps_n,
+ pref->fps_d);
+
+ /* Finally, fixate everything else except the interlace-mode and colorimetry
+ * which still need further negotiation as it wasn't probed */
+ gst_structure_map_in_place (s, gst_vl42_src_fixate_fields, s);
+}
-/* this function is a bit of a last resort */
static void
-gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
+gst_v4l2_src_parse_fixed_struct (GstStructure * s,
+ gint * width, gint * height, gint * fps_n, gint * fps_d)
{
- GstStructure *structure;
- gint i;
+ if (gst_structure_has_field (s, "width") && width)
+ gst_structure_get_int (s, "width", width);
+
+ if (gst_structure_has_field (s, "height") && height)
+ gst_structure_get_int (s, "height", height);
+
+ if (gst_structure_has_field (s, "framerate") && fps_n && fps_d)
+ gst_structure_get_fraction (s, "framerate", fps_n, fps_d);
+}
+
+/* TODO Consider framerate */
+static gint
+gst_v4l2src_fixed_caps_compare (GstCaps * caps_a, GstCaps * caps_b,
+ struct PreferedCapsInfo *pref)
+{
+ GstStructure *a, *b;
+ gint aw = G_MAXINT, ah = G_MAXINT, ad = G_MAXINT;
+ gint bw = G_MAXINT, bh = G_MAXINT, bd = G_MAXINT;
+ gint ret;
+
+ a = gst_caps_get_structure (caps_a, 0);
+ b = gst_caps_get_structure (caps_b, 0);
+
+ gst_v4l2_src_parse_fixed_struct (a, &aw, &ah, NULL, NULL);
+ gst_v4l2_src_parse_fixed_struct (b, &bw, &bh, NULL, NULL);
+
+ /* When both are smaller then pref, just append to the end */
+ if ((bw < pref->width || bh < pref->height)
+ && (aw < pref->width || ah < pref->height)) {
+ ret = 1;
+ goto done;
+ }
+
+ /* If a is smaller then pref and not b, then a goes after b */
+ if (aw < pref->width || ah < pref->height) {
+ ret = 1;
+ goto done;
+ }
+
+ /* If b is smaller then pref and not a, then a goes before b */
+ if (bw < pref->width || bh < pref->height) {
+ ret = -1;
+ goto done;
+ }
+
+ /* Both are larger or equal to the preference, prefer the smallest */
+ ad = MAX (1, aw - pref->width) * MAX (1, ah - pref->height);
+ bd = MAX (1, bw - pref->width) * MAX (1, bh - pref->height);
+
+ /* Adjust slightly in case width/height matched the preference */
+ if (aw == pref->width)
+ ad -= 1;
+
+ if (ah == pref->height)
+ ad -= 1;
+
+ if (bw == pref->width)
+ bd -= 1;
+
+ if (bh == pref->height)
+ bd -= 1;
+
+ /* If the choices are equivalent, maintain the order */
+ if (ad == bd)
+ ret = 1;
+ else
+ ret = ad - bd;
+
+done:
+ GST_TRACE ("Placing %ix%i (%s) %s %ix%i (%s)", aw, ah,
+ gst_structure_get_string (a, "format"), ret > 0 ? "after" : "before", bw,
+ bh, gst_structure_get_string (b, "format"));
+ return ret;
+}
+
+static gboolean
+gst_v4l2src_set_format (GstV4l2Src * v4l2src, GstCaps * caps,
+ GstV4l2Error * error)
+{
+ GstV4l2Object *obj;
+
+ obj = v4l2src->v4l2object;
+
+ /* make sure we stop capturing and dealloc buffers */
+ if (!gst_v4l2_object_stop (obj))
+ return FALSE;
+
+ g_signal_emit (v4l2src, gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT], 0,
+ v4l2src->v4l2object->video_fd, caps);
+
+ return gst_v4l2_object_set_format (obj, caps, error);
+}
+
+static GstCaps *
+gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps, GstStructure * pref_s)
+{
+ /* Let's prefer a good resolutiion as of today's standard. */
+ struct PreferedCapsInfo pref = {
+ 3840, 2160, 120, 1
+ };
+ GstV4l2Src *v4l2src = GST_V4L2SRC (basesrc);
+ GstV4l2Object *obj = v4l2src->v4l2object;
+ GList *caps_list = NULL;
+ GstStructure *s;
+ gint i = G_MAXINT;
+ GstV4l2Error error = GST_V4L2_ERROR_INIT;
+ GstCaps *fcaps = NULL;
GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
+ /* We consider the first structure from peercaps to be a preference. This is
+ * useful for matching a reported native display, or simply to avoid
+ * transformation to happen downstream. */
+ if (pref_s) {
+ pref_s = gst_structure_copy (pref_s);
+ gst_v4l2_src_fixate_struct_with_preference (pref_s, &pref);
+ gst_v4l2_src_parse_fixed_struct (pref_s, &pref.width, &pref.height,
+ &pref.fps_n, &pref.fps_d);
+ gst_structure_free (pref_s);
+ }
+
+ GST_DEBUG_OBJECT (basesrc, "Prefered size %ix%i", pref.width, pref.height);
+
+ /* Sort the structures to get the caps that is nearest to our preferences,
+ * first. Use single struct caps for sorting so we preserve the features. */
+ for (i = 0; i < gst_caps_get_size (caps); i++) {
+ GstCaps *tmp = gst_caps_copy_nth (caps, i);
+
+ s = gst_caps_get_structure (tmp, 0);
+ gst_v4l2_src_fixate_struct_with_preference (s, &pref);
+
+ caps_list = g_list_insert_sorted_with_data (caps_list, tmp,
+ (GCompareDataFunc) gst_v4l2src_fixed_caps_compare, &pref);
+ }
+
+ gst_caps_unref (caps);
+ caps = gst_caps_new_empty ();
+
+ while (caps_list) {
+ GstCaps *tmp = caps_list->data;
+ caps_list = g_list_delete_link (caps_list, caps_list);
+ gst_caps_append (caps, tmp);
+ }
+
+ GST_DEBUG_OBJECT (basesrc, "sorted and normalized caps %" GST_PTR_FORMAT,
+ caps);
+
+ /* Each structure in the caps has been fixated, except for the
+ * interlace-mode and colorimetry. Now normalize the caps so we can
+ * enumerate the possibilities */
+ caps = gst_caps_normalize (caps);
+
for (i = 0; i < gst_caps_get_size (caps); ++i) {
- const GValue *v;
+ gst_v4l2_clear_error (&error);
+ if (fcaps)
+ gst_caps_unref (fcaps);
- structure = gst_caps_get_structure (caps, i);
+ fcaps = gst_caps_copy_nth (caps, i);
- /* FIXME such sizes? we usually fixate to something in the 320x200
- * range... */
- /* We are fixating to greater possble size (limited to GST_V4L2_MAX_SIZE)
- and the maximum framerate resolution for that size */
- gst_structure_fixate_field_nearest_int (structure, "width",
- GST_V4L2_MAX_SIZE);
- gst_structure_fixate_field_nearest_int (structure, "height",
- GST_V4L2_MAX_SIZE);
- gst_structure_fixate_field_nearest_fraction (structure, "framerate",
- G_MAXINT, 1);
+ /* try hard to avoid TRY_FMT since some UVC camera just crash when this
+ * is called at run-time. */
+ if (gst_v4l2_object_caps_is_subset (obj, fcaps)) {
+ gst_caps_unref (fcaps);
+ fcaps = gst_v4l2_object_get_current_caps (obj);
+ break;
+ }
- v = gst_structure_get_value (structure, "format");
- if (v && G_VALUE_TYPE (v) != GST_TYPE_FOURCC) {
- guint32 fourcc;
+ /* Just check if the format is acceptable, once we know
+ * no buffers should be outstanding we try S_FMT.
+ *
+ * Basesrc will do an allocation query that
+ * should indirectly reclaim buffers, after that we can
+ * set the format and then configure our pool */
+ if (gst_v4l2_object_try_format (obj, fcaps, &error)) {
+ /* make sure the caps changed before doing anything */
+ if (gst_v4l2_object_caps_equal (obj, fcaps))
+ break;
- g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
+ v4l2src->renegotiation_adjust = v4l2src->offset + 1;
+ v4l2src->pending_set_fmt = TRUE;
+ break;
+ }
- fourcc = gst_value_get_fourcc (gst_value_list_get_value (v, 0));
- gst_structure_set (structure, "format", GST_TYPE_FOURCC, fourcc, NULL);
+ /* Only EIVAL make sense, report any other errors, this way we don't keep
+ * probing if the device got disconnected, or if it's firmware stopped
+ * responding */
+ if (error.error->code != GST_RESOURCE_ERROR_SETTINGS) {
+ i = G_MAXINT;
+ break;
}
}
- GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
-}
+ if (i >= gst_caps_get_size (caps)) {
+ gst_v4l2_error (v4l2src, &error);
+ if (fcaps)
+ gst_caps_unref (fcaps);
+ gst_caps_unref (caps);
+ return NULL;
+ }
+
+ gst_caps_unref (caps);
+
+ GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, fcaps);
+ return fcaps;
+}
static gboolean
gst_v4l2src_negotiate (GstBaseSrc * basesrc)
gboolean result = FALSE;
/* first see what is possible on our source pad */
- thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
+ thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
- LOG_CAPS (basesrc, thiscaps);
/* nothing or anything is allowed, we're done */
if (thiscaps == NULL || gst_caps_is_any (thiscaps))
goto no_nego_needed;
- /* get the peer caps */
- peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
+ /* get the peer caps without a filter as we'll filter ourselves later on */
+ peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
- LOG_CAPS (basesrc, peercaps);
if (peercaps && !gst_caps_is_any (peercaps)) {
- GstCaps *icaps = NULL;
- int i;
-
/* Prefer the first caps we are compatible with that the peer proposed */
- for (i = 0; i < gst_caps_get_size (peercaps); i++) {
- /* get intersection */
- GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
-
- GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
- LOG_CAPS (basesrc, ipcaps);
-
- icaps = gst_caps_intersect (thiscaps, ipcaps);
- gst_caps_unref (ipcaps);
-
- if (!gst_caps_is_empty (icaps))
- break;
+ caps = gst_caps_intersect_full (peercaps, thiscaps,
+ GST_CAPS_INTERSECT_FIRST);
- gst_caps_unref (icaps);
- icaps = NULL;
- }
+ GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
- GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
- LOG_CAPS (basesrc, icaps);
- if (icaps) {
- /* If there are multiple intersections pick the one with the smallest
- * resolution strictly bigger then the first peer caps */
- if (gst_caps_get_size (icaps) > 1) {
- GstStructure *s = gst_caps_get_structure (peercaps, 0);
-
- int best = 0;
-
- int twidth, theight;
-
- int width = G_MAXINT, height = G_MAXINT;
-
- if (gst_structure_get_int (s, "width", &twidth)
- && gst_structure_get_int (s, "height", &theight)) {
-
- /* Walk the structure backwards to get the first entry of the
- * smallest resolution bigger (or equal to) the preferred resolution)
- */
- for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
- GstStructure *is = gst_caps_get_structure (icaps, i);
-
- int w, h;
-
- if (gst_structure_get_int (is, "width", &w)
- && gst_structure_get_int (is, "height", &h)) {
- if (w >= twidth && w <= width && h >= theight && h <= height) {
- width = w;
- height = h;
- best = i;
- }
- }
- }
- }
-
- caps = gst_caps_copy_nth (icaps, best);
- gst_caps_unref (icaps);
- } else {
- caps = icaps;
- }
- }
gst_caps_unref (thiscaps);
} else {
/* no peer or peer have ANY caps, work with our own caps then */
caps = thiscaps;
}
- if (peercaps)
- gst_caps_unref (peercaps);
- if (caps) {
- caps = gst_caps_make_writable (caps);
- gst_caps_truncate (caps);
+ if (caps) {
/* now fixate */
if (!gst_caps_is_empty (caps)) {
- gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
+ GstStructure *pref = NULL;
+
+ if (peercaps && !gst_caps_is_any (peercaps))
+ pref = gst_caps_get_structure (peercaps, 0);
+
+ caps = gst_v4l2src_fixate (basesrc, caps, pref);
+
+ /* Fixating may fail as we now set the selected format */
+ if (!caps) {
+ result = FALSE;
+ goto done;
+ }
+
GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
- LOG_CAPS (basesrc, caps);
if (gst_caps_is_any (caps)) {
/* hmm, still anything, so element can do anything and
result = TRUE;
} else if (gst_caps_is_fixed (caps)) {
/* yay, fixed caps, use those then */
- if (gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps))
- result = TRUE;
+ result = gst_base_src_set_caps (basesrc, caps);
}
}
gst_caps_unref (caps);
}
+
+done:
+ if (peercaps)
+ gst_caps_unref (peercaps);
+
return result;
no_nego_needed:
}
static GstCaps *
-gst_v4l2src_get_caps (GstBaseSrc * src)
+gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
{
- GstV4l2Src *v4l2src = GST_V4L2SRC (src);
- GstCaps *ret;
- GSList *walk;
- GSList *formats;
-
- if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object)) {
- /* FIXME: copy? */
- return
- gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
- (v4l2src)));
- }
-
- if (v4l2src->probed_caps)
- return gst_caps_ref (v4l2src->probed_caps);
-
- formats = gst_v4l2_object_get_format_list (v4l2src->v4l2object);
-
- ret = gst_caps_new_empty ();
-
- for (walk = formats; walk; walk = walk->next) {
- struct v4l2_fmtdesc *format;
-
- GstStructure *template;
-
- format = (struct v4l2_fmtdesc *) walk->data;
-
- template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
-
- if (template) {
- GstCaps *tmp;
+ GstV4l2Src *v4l2src;
+ GstV4l2Object *obj;
- tmp =
- gst_v4l2_object_probe_caps_for_format (v4l2src->v4l2object,
- format->pixelformat, template);
- if (tmp)
- gst_caps_append (ret, tmp);
+ v4l2src = GST_V4L2SRC (src);
+ obj = v4l2src->v4l2object;
- gst_structure_free (template);
- } else {
- GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
- }
+ if (!GST_V4L2_IS_OPEN (obj)) {
+ return gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (v4l2src));
}
- v4l2src->probed_caps = gst_caps_ref (ret);
-
- GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
-
- return ret;
+ return gst_v4l2_object_get_caps (obj, filter);
}
static gboolean
-gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
+gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
{
- GstV4l2Src *v4l2src;
- gint w = 0, h = 0;
- gboolean interlaced;
- struct v4l2_fmtdesc *format;
- guint fps_n, fps_d;
- guint size;
+ GstV4l2Src *src = GST_V4L2SRC (bsrc);
+ gboolean ret = TRUE;
- v4l2src = GST_V4L2SRC (src);
+ if (src->pending_set_fmt) {
+ GstCaps *caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (bsrc));
+ GstV4l2Error error = GST_V4L2_ERROR_INIT;
- /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
- if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object))
- return FALSE;
+ caps = gst_caps_make_writable (caps);
+ if (!(ret = gst_v4l2src_set_format (src, caps, &error)))
+ gst_v4l2_error (src, &error);
- /* make sure we stop capturing and dealloc buffers */
- if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)) {
- /* both will throw an element-error on failure */
- if (!gst_v4l2src_capture_stop (v4l2src))
- return FALSE;
- if (!gst_v4l2src_capture_deinit (v4l2src))
- return FALSE;
+ gst_caps_unref (caps);
+ src->pending_set_fmt = FALSE;
+ } else if (gst_buffer_pool_is_active (src->v4l2object->pool)) {
+ /* Trick basesrc into not deactivating the active pool. Renegotiating here
+ * would otherwise turn off and on the camera. */
+ GstAllocator *allocator;
+ GstAllocationParams params;
+ GstBufferPool *pool;
+
+ gst_base_src_get_allocator (bsrc, &allocator, ¶ms);
+ pool = gst_base_src_get_buffer_pool (bsrc);
+
+ if (gst_query_get_n_allocation_params (query))
+ gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
+ else
+ gst_query_add_allocation_param (query, allocator, ¶ms);
+
+ if (gst_query_get_n_allocation_pools (query))
+ gst_query_set_nth_allocation_pool (query, 0, pool,
+ src->v4l2object->info.size, 1, 0);
+ else
+ gst_query_add_allocation_pool (query, pool, src->v4l2object->info.size, 1,
+ 0);
+
+ if (pool)
+ gst_object_unref (pool);
+ if (allocator)
+ gst_object_unref (allocator);
+
+ return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
}
- /* we want our own v4l2 type of fourcc codes */
- if (!gst_v4l2_object_get_caps_info (v4l2src->v4l2object, caps, &format, &w,
- &h, &interlaced, &fps_n, &fps_d, &size)) {
- GST_INFO_OBJECT (v4l2src,
- "can't get capture format from caps %" GST_PTR_FORMAT, caps);
- return FALSE;
+ if (ret) {
+ ret = gst_v4l2_object_decide_allocation (src->v4l2object, query);
+ if (ret)
+ ret = GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
}
- GST_DEBUG_OBJECT (v4l2src, "trying to set_capture %dx%d at %d/%d fps, "
- "format %s", w, h, fps_n, fps_d, format->description);
+ if (ret) {
+ if (!gst_buffer_pool_set_active (src->v4l2object->pool, TRUE))
+ goto activate_failed;
+ }
- if (!gst_v4l2src_set_capture (v4l2src, format->pixelformat, w, h,
- interlaced, fps_n, fps_d))
- /* error already posted */
- return FALSE;
+ return ret;
- if (!gst_v4l2src_capture_init (v4l2src, caps))
+activate_failed:
+ {
+ GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
+ (_("Failed to allocate required memory.")),
+ ("Buffer pool activation failed"));
return FALSE;
-
- if (v4l2src->use_mmap) {
- v4l2src->get_frame = gst_v4l2src_get_mmap;
- } else {
- v4l2src->get_frame = gst_v4l2src_get_read;
}
-
- if (!gst_v4l2src_capture_start (v4l2src))
- return FALSE;
-
- /* now store the expected output size */
- v4l2src->frame_byte_size = size;
-
- return TRUE;
}
static gboolean
gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
{
GstV4l2Src *src;
-
+ GstV4l2Object *obj;
gboolean res = FALSE;
src = GST_V4L2SRC (bsrc);
+ obj = src->v4l2object;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_LATENCY:{
GstClockTime min_latency, max_latency;
+ guint32 fps_n, fps_d;
+ guint num_buffers = 0;
/* device must be open */
- if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
+ if (!GST_V4L2_IS_OPEN (obj)) {
GST_WARNING_OBJECT (src,
"Can't give latency since device isn't open !");
goto done;
}
+ fps_n = GST_V4L2_FPS_N (obj);
+ fps_d = GST_V4L2_FPS_D (obj);
+
/* we must have a framerate */
- if (src->fps_n <= 0 || src->fps_d <= 0) {
+ if (fps_n <= 0 || fps_d <= 0) {
GST_WARNING_OBJECT (src,
"Can't give latency since framerate isn't fixated !");
goto done;
}
/* min latency is the time to capture one frame */
- min_latency =
- gst_util_uint64_scale_int (GST_SECOND, src->fps_d, src->fps_n);
+ min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
/* max latency is total duration of the frame buffer */
- max_latency = src->num_buffers * min_latency;
+ if (obj->pool != NULL)
+ num_buffers = GST_V4L2_BUFFER_POOL_CAST (obj->pool)->max_latency;
+
+ if (num_buffers == 0)
+ max_latency = -1;
+ else
+ max_latency = num_buffers * min_latency;
GST_DEBUG_OBJECT (bsrc,
"report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
GstV4l2Src *v4l2src = GST_V4L2SRC (src);
v4l2src->offset = 0;
+ v4l2src->renegotiation_adjust = 0;
/* activate settings for first frame */
v4l2src->ctrl_time = 0;
- gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
+ gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
+
+ v4l2src->has_bad_timestamp = FALSE;
+ v4l2src->last_timestamp = 0;
return TRUE;
}
gst_v4l2src_unlock (GstBaseSrc * src)
{
GstV4l2Src *v4l2src = GST_V4L2SRC (src);
-
- GST_LOG_OBJECT (src, "Flushing");
- gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
-
- return TRUE;
+ return gst_v4l2_object_unlock (v4l2src->v4l2object);
}
static gboolean
{
GstV4l2Src *v4l2src = GST_V4L2SRC (src);
- GST_LOG_OBJECT (src, "No longer flushing");
- gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
+ v4l2src->last_timestamp = 0;
- return TRUE;
+ return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
}
static gboolean
gst_v4l2src_stop (GstBaseSrc * src)
{
GstV4l2Src *v4l2src = GST_V4L2SRC (src);
+ GstV4l2Object *obj = v4l2src->v4l2object;
- if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)
- && !gst_v4l2src_capture_stop (v4l2src))
- return FALSE;
-
- if (v4l2src->v4l2object->buffer != NULL) {
- if (!gst_v4l2src_capture_deinit (v4l2src))
+ if (GST_V4L2_IS_ACTIVE (obj)) {
+ if (!gst_v4l2_object_stop (obj))
return FALSE;
}
- v4l2src->fps_d = 0;
- v4l2src->fps_n = 0;
+ v4l2src->pending_set_fmt = FALSE;
return TRUE;
}
{
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GstV4l2Src *v4l2src = GST_V4L2SRC (element);
+ GstV4l2Object *obj = v4l2src->v4l2object;
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
/* open the device */
- if (!gst_v4l2_object_start (v4l2src->v4l2object))
+ if (!gst_v4l2_object_open (obj))
return GST_STATE_CHANGE_FAILURE;
break;
default:
switch (transition) {
case GST_STATE_CHANGE_READY_TO_NULL:
/* close the device */
- if (!gst_v4l2_object_stop (v4l2src->v4l2object))
+ if (!gst_v4l2_object_close (obj))
return GST_STATE_CHANGE_FAILURE;
- if (v4l2src->probed_caps) {
- gst_caps_unref (v4l2src->probed_caps);
- v4l2src->probed_caps = NULL;
- }
break;
default:
break;
}
static GstFlowReturn
-gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
+gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
{
- gint amount;
- gint ret;
-
- gint buffersize;
-
- buffersize = v4l2src->frame_byte_size;
- /* In case the size per frame is unknown assume it's a streaming format (e.g.
- * mpegts) and grab a reasonable default size instead */
- if (buffersize == 0)
- buffersize = GST_BASE_SRC (v4l2src)->blocksize;
-
- *buf = gst_buffer_new_and_alloc (buffersize);
+ GstV4l2Src *v4l2src = GST_V4L2SRC (src);
+ GstV4l2Object *obj = v4l2src->v4l2object;
+ GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL_CAST (obj->pool);
+ GstFlowReturn ret;
+ GstClock *clock;
+ GstClockTime abs_time, base_time, timestamp, duration;
+ GstClockTime delay;
+ GstMessage *qos_msg;
do {
- ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
- if (G_UNLIKELY (ret < 0)) {
- if (errno == EBUSY)
- goto stopped;
- if (errno == ENXIO) {
- GST_DEBUG_OBJECT (v4l2src,
- "v4l2 device doesn't support polling. Disabling");
- v4l2src->v4l2object->can_poll_device = FALSE;
- } else {
- if (errno != EAGAIN && errno != EINTR)
- goto select_error;
- }
- }
- amount =
- v4l2_read (v4l2src->v4l2object->video_fd, GST_BUFFER_DATA (*buf),
- buffersize);
- if (amount == buffersize) {
- break;
- } else if (amount == -1) {
- if (errno == EAGAIN || errno == EINTR) {
- continue;
- } else
- goto read_error;
- } else {
- /* short reads can happen if a signal interrupts the read */
- continue;
- }
- } while (TRUE);
+ ret = GST_BASE_SRC_CLASS (parent_class)->alloc (GST_BASE_SRC (src), 0,
+ obj->info.size, buf);
- /* we set the buffer metadata in gst_v4l2src_create() */
+ if (G_UNLIKELY (ret != GST_FLOW_OK))
+ goto alloc_failed;
- return GST_FLOW_OK;
+ ret = gst_v4l2_buffer_pool_process (pool, buf);
- /* ERRORS */
-select_error:
- {
- GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
- ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
- return GST_FLOW_ERROR;
- }
-stopped:
- {
- GST_DEBUG ("stop called");
- return GST_FLOW_WRONG_STATE;
+ } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
+
+ if (G_UNLIKELY (ret != GST_FLOW_OK))
+ goto error;
+
+ timestamp = GST_BUFFER_TIMESTAMP (*buf);
+ duration = obj->duration;
+
+ /* timestamps, LOCK to get clock and base time. */
+ /* FIXME: element clock and base_time is rarely changing */
+ GST_OBJECT_LOCK (v4l2src);
+ if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
+ /* we have a clock, get base time and ref clock */
+ base_time = GST_ELEMENT (v4l2src)->base_time;
+ gst_object_ref (clock);
+ } else {
+ /* no clock, can't set timestamps */
+ base_time = GST_CLOCK_TIME_NONE;
}
-read_error:
- {
- GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
- (_("Error reading %d bytes from device '%s'."),
- buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
- gst_buffer_unref (*buf);
- return GST_FLOW_ERROR;
+ GST_OBJECT_UNLOCK (v4l2src);
+
+ /* sample pipeline clock */
+ if (clock) {
+ abs_time = gst_clock_get_time (clock);
+ gst_object_unref (clock);
+ } else {
+ abs_time = GST_CLOCK_TIME_NONE;
}
-}
-static GstFlowReturn
-gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
-{
- GstBuffer *temp;
- GstFlowReturn ret;
- guint size;
- guint count = 0;
+retry:
+ if (!v4l2src->has_bad_timestamp && timestamp != GST_CLOCK_TIME_NONE) {
+ struct timespec now;
+ GstClockTime gstnow;
-again:
- ret = gst_v4l2src_grab_frame (v4l2src, &temp);
- if (G_UNLIKELY (ret != GST_FLOW_OK))
- goto done;
+ /* v4l2 specs say to use the system time although many drivers switched to
+ * the more desirable monotonic time. We first try to use the monotonic time
+ * and see how that goes */
+ clock_gettime (CLOCK_MONOTONIC, &now);
+ gstnow = GST_TIMESPEC_TO_TIME (now);
- if (v4l2src->frame_byte_size > 0) {
- size = GST_BUFFER_SIZE (temp);
+ if (timestamp > gstnow || (gstnow - timestamp) > (10 * GST_SECOND)) {
+ GTimeVal now;
- /* if size does not match what we expected, try again */
- if (size != v4l2src->frame_byte_size) {
- GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
- (_("Got unexpected frame size of %u instead of %u."),
- size, v4l2src->frame_byte_size), (NULL));
- gst_buffer_unref (temp);
- if (count++ > 50)
- goto size_error;
+ /* very large diff, fall back to system time */
+ g_get_current_time (&now);
+ gstnow = GST_TIMEVAL_TO_TIME (now);
+ }
- goto again;
+ /* Detect buggy drivers here, and stop using their timestamp. Failing any
+ * of these condition would imply a very buggy driver:
+ * - Timestamp in the future
+ * - Timestamp is going backward compare to last seen timestamp
+ * - Timestamp is jumping forward for less then a frame duration
+ * - Delay is bigger then the actual timestamp
+ * */
+ if (timestamp > gstnow) {
+ GST_WARNING_OBJECT (v4l2src,
+ "Timestamp in the future detected, ignoring driver timestamps");
+ v4l2src->has_bad_timestamp = TRUE;
+ goto retry;
}
- }
- *buf = temp;
-done:
- return ret;
+ if (v4l2src->last_timestamp > timestamp) {
+ GST_WARNING_OBJECT (v4l2src,
+ "Timestamp going backward, ignoring driver timestamps");
+ v4l2src->has_bad_timestamp = TRUE;
+ goto retry;
+ }
- /* ERRORS */
-size_error:
- {
- GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
- (_("Error reading %d bytes on device '%s'."),
- v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
- return GST_FLOW_ERROR;
+ delay = gstnow - timestamp;
+
+ if (delay > timestamp) {
+ GST_WARNING_OBJECT (v4l2src,
+ "Timestamp does not correlate with any clock, ignoring driver timestamps");
+ v4l2src->has_bad_timestamp = TRUE;
+ goto retry;
+ }
+
+ /* Save last timestamp for sanity checks */
+ v4l2src->last_timestamp = timestamp;
+
+ GST_DEBUG_OBJECT (v4l2src, "ts: %" GST_TIME_FORMAT " now %" GST_TIME_FORMAT
+ " delay %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
+ GST_TIME_ARGS (gstnow), GST_TIME_ARGS (delay));
+ } else {
+ /* we assume 1 frame latency otherwise */
+ if (GST_CLOCK_TIME_IS_VALID (duration))
+ delay = duration;
+ else
+ delay = 0;
}
-}
-static GstFlowReturn
-gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
-{
- GstV4l2Src *v4l2src = GST_V4L2SRC (src);
- int i;
- GstFlowReturn ret;
+ /* set buffer metadata */
- for (i = 0; i < v4l2src->decimate - 1; i++) {
- ret = v4l2src->get_frame (v4l2src, buf);
- if (ret != GST_FLOW_OK) {
- return ret;
- }
- gst_buffer_unref (*buf);
+ if (G_LIKELY (abs_time != GST_CLOCK_TIME_NONE)) {
+ /* the time now is the time of the clock minus the base time */
+ timestamp = abs_time - base_time;
+
+ /* adjust for delay in the device */
+ if (timestamp > delay)
+ timestamp -= delay;
+ else
+ timestamp = 0;
+ } else {
+ timestamp = GST_CLOCK_TIME_NONE;
}
- ret = v4l2src->get_frame (v4l2src, buf);
+ /* activate settings for next frame */
+ if (GST_CLOCK_TIME_IS_VALID (duration)) {
+ v4l2src->ctrl_time += duration;
+ } else {
+ /* this is not very good (as it should be the next timestamp),
+ * still good enough for linear fades (as long as it is not -1)
+ */
+ v4l2src->ctrl_time = timestamp;
+ }
+ gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
- /* set buffer metadata */
- if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
- GstClock *clock;
- GstClockTime timestamp;
+ GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT " out ts %" GST_TIME_FORMAT,
+ GST_TIME_ARGS (v4l2src->ctrl_time), GST_TIME_ARGS (timestamp));
+ /* use generated offset values only if there are not already valid ones
+ * set by the v4l2 device */
+ if (!GST_BUFFER_OFFSET_IS_VALID (*buf)
+ || !GST_BUFFER_OFFSET_END_IS_VALID (*buf)) {
GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
+ } else {
+ /* adjust raw v4l2 device sequence, will restart at null in case of renegotiation
+ * (streamoff/streamon) */
+ GST_BUFFER_OFFSET (*buf) += v4l2src->renegotiation_adjust;
+ GST_BUFFER_OFFSET_END (*buf) += v4l2src->renegotiation_adjust;
+ /* check for frame loss with given (from v4l2 device) buffer offset */
+ if ((v4l2src->offset != 0)
+ && (GST_BUFFER_OFFSET (*buf) != (v4l2src->offset + 1))) {
+ guint64 lost_frame_count = GST_BUFFER_OFFSET (*buf) - v4l2src->offset - 1;
+ GST_WARNING_OBJECT (v4l2src,
+ "lost frames detected: count = %" G_GUINT64_FORMAT " - ts: %"
+ GST_TIME_FORMAT, lost_frame_count, GST_TIME_ARGS (timestamp));
+
+ qos_msg = gst_message_new_qos (GST_OBJECT_CAST (v4l2src), TRUE,
+ GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, timestamp,
+ GST_CLOCK_TIME_IS_VALID (duration) ? lost_frame_count *
+ duration : GST_CLOCK_TIME_NONE);
+ gst_element_post_message (GST_ELEMENT_CAST (v4l2src), qos_msg);
- /* timestamps, LOCK to get clock and base time. */
- /* FIXME: element clock and base_time is rarely changing */
- GST_OBJECT_LOCK (v4l2src);
- if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
- /* we have a clock, get base time and ref clock */
- timestamp = GST_ELEMENT (v4l2src)->base_time;
- gst_object_ref (clock);
- } else {
- /* no clock, can't set timestamps */
- timestamp = GST_CLOCK_TIME_NONE;
- }
- GST_OBJECT_UNLOCK (v4l2src);
-
- if (G_LIKELY (clock)) {
- /* the time now is the time of the clock minus the base time */
- timestamp = gst_clock_get_time (clock) - timestamp;
- gst_object_unref (clock);
-
- /* if we have a framerate adjust timestamp for frame latency */
- if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
- if (timestamp > v4l2src->duration)
- timestamp -= v4l2src->duration;
- else
- timestamp = 0;
- }
}
+ v4l2src->offset = GST_BUFFER_OFFSET (*buf);
+ }
- /* activate settings for next frame */
- if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
- v4l2src->ctrl_time += v4l2src->duration;
+ GST_BUFFER_TIMESTAMP (*buf) = timestamp;
+ GST_BUFFER_DURATION (*buf) = duration;
+
+ return ret;
+
+ /* ERROR */
+alloc_failed:
+ {
+ if (ret != GST_FLOW_FLUSHING)
+ GST_ELEMENT_ERROR (src, RESOURCE, NO_SPACE_LEFT,
+ ("Failed to allocate a buffer"), (NULL));
+ return ret;
+ }
+error:
+ {
+ gst_buffer_replace (buf, NULL);
+ if (ret == GST_V4L2_FLOW_LAST_BUFFER) {
+ GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
+ ("Driver returned a buffer with no payload, this most likely "
+ "indicate a bug in the driver."), (NULL));
+ ret = GST_FLOW_ERROR;
} else {
- /* this is not very good (as it should be the next timestamp),
- * still good enough for linear fades (as long as it is not -1)
- */
- v4l2src->ctrl_time = timestamp;
+ GST_DEBUG_OBJECT (src, "error processing buffer %d (%s)", ret,
+ gst_flow_get_name (ret));
}
- gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
- GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
- GST_TIME_ARGS (v4l2src->ctrl_time));
-
- /* FIXME: use the timestamp from the buffer itself! */
- GST_BUFFER_TIMESTAMP (*buf) = timestamp;
- GST_BUFFER_DURATION (*buf) = v4l2src->duration;
+ return ret;
}
- return ret;
}
/* GstURIHandler interface */
static GstURIType
-gst_v4l2src_uri_get_type (void)
+gst_v4l2src_uri_get_type (GType type)
{
return GST_URI_SRC;
}
-static gchar **
-gst_v4l2src_uri_get_protocols (void)
+static const gchar *const *
+gst_v4l2src_uri_get_protocols (GType type)
{
- static gchar *protocols[] = { (char *) "v4l2", NULL };
+ static const gchar *protocols[] = { "v4l2", NULL };
return protocols;
}
-static const gchar *
+static gchar *
gst_v4l2src_uri_get_uri (GstURIHandler * handler)
{
GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
if (v4l2src->v4l2object->videodev != NULL) {
- gchar uri[256];
-
- /* need to return a const string, but also don't want to leak the generated
- * string, so just intern it - there's a limited number of video devices
- * after all */
- g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
- return g_intern_string (uri);
+ return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
}
- return "v4l2://";
+ return g_strdup ("v4l2://");
}
static gboolean
-gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
+gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
+ GError ** error)
{
GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
const gchar *device = DEFAULT_PROP_DEVICE;