3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
6 * gstv4l2src.c: Video4Linux2 source element
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.
25 * SECTION:element-v4l2src
27 * v4l2src can be used to capture video from v4l2 devices, like webcams and tv
31 * <title>Example launch lines</title>
33 * gst-launch-1.0 v4l2src ! xvimagesink
34 * ]| This pipeline shows the video captured from /dev/video0 tv card and for
37 * gst-launch-1.0 v4l2src ! jpegdec ! xvimagesink
38 * ]| This pipeline shows the video captured from a webcam that delivers jpeg
42 * Since 1.14, the use of libv4l2 has been disabled due to major bugs in the
43 * emulation layer. To enable usage of this library, set the environment
44 * variable GST_V4L2_USE_LIBV4L2=1.
55 #include <gst/video/gstvideometa.h>
56 #include <gst/video/gstvideopool.h>
58 #include "gstv4l2src.h"
60 #include "gstv4l2colorbalance.h"
61 #include "gstv4l2tuner.h"
62 #include "gstv4l2vidorient.h"
64 #include "gst/gst-i18n-plugin.h"
66 GST_DEBUG_CATEGORY (v4l2src_debug);
67 #define GST_CAT_DEFAULT v4l2src_debug
69 #define DEFAULT_PROP_DEVICE "/dev/video0"
74 V4L2_STD_OBJECT_PROPS,
78 /* signals and args */
81 SIGNAL_PRE_SET_FORMAT,
85 static guint gst_v4l2_signals[LAST_SIGNAL] = { 0 };
87 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
88 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
89 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
91 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
94 #define gst_v4l2src_parent_class parent_class
95 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
96 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
97 G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
98 G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
99 gst_v4l2src_color_balance_interface_init);
100 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
101 gst_v4l2src_video_orientation_interface_init));
103 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
105 /* element methods */
106 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
107 GstStateChange transition);
109 /* basesrc methods */
110 static gboolean gst_v4l2src_start (GstBaseSrc * src);
111 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
112 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
113 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
114 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
115 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
116 static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
118 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
119 static GstCaps *gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps,
120 GstStructure * pref_s);
121 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
123 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
124 const GValue * value, GParamSpec * pspec);
125 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
126 GValue * value, GParamSpec * pspec);
129 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
131 GObjectClass *gobject_class;
132 GstElementClass *element_class;
133 GstBaseSrcClass *basesrc_class;
134 GstPushSrcClass *pushsrc_class;
136 gobject_class = G_OBJECT_CLASS (klass);
137 element_class = GST_ELEMENT_CLASS (klass);
138 basesrc_class = GST_BASE_SRC_CLASS (klass);
139 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
141 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
142 gobject_class->set_property = gst_v4l2src_set_property;
143 gobject_class->get_property = gst_v4l2src_get_property;
145 element_class->change_state = gst_v4l2src_change_state;
147 gst_v4l2_object_install_properties_helper (gobject_class,
148 DEFAULT_PROP_DEVICE);
151 * GstV4l2Src::prepare-format:
152 * @v4l2src: the v4l2src instance
153 * @fd: the file descriptor of the current device
154 * @caps: the caps of the format being set
156 * This signal gets emitted before calling the v4l2 VIDIOC_S_FMT ioctl
157 * (set format). This allows for any custom configuration of the device to
158 * happen prior to the format being set.
159 * This is mostly useful for UVC H264 encoding cameras which need the H264
160 * Probe & Commit to happen prior to the normal Probe & Commit.
164 gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT] = g_signal_new ("prepare-format",
165 G_TYPE_FROM_CLASS (klass),
167 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CAPS);
169 gst_element_class_set_static_metadata (element_class,
170 "Video (video4linux2) Source", "Source/Video",
171 "Reads frames from a Video4Linux2 device",
172 "Edgard Lima <edgard.lima@gmail.com>, "
173 "Stefan Kost <ensonic@users.sf.net>");
175 gst_element_class_add_pad_template
177 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
178 gst_v4l2_object_get_all_caps ()));
180 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
181 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
182 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
183 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
184 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
185 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
186 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
187 basesrc_class->decide_allocation =
188 GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
190 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
192 klass->v4l2_class_devices = NULL;
194 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
198 gst_v4l2src_init (GstV4l2Src * v4l2src)
200 /* fixme: give an update_fps_function */
201 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
202 GST_OBJECT (GST_BASE_SRC_PAD (v4l2src)), V4L2_BUF_TYPE_VIDEO_CAPTURE,
203 DEFAULT_PROP_DEVICE, gst_v4l2_get_input, gst_v4l2_set_input, NULL);
205 /* Avoid the slow probes */
206 v4l2src->v4l2object->skip_try_fmt_probes = TRUE;
208 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
209 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
214 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
216 gst_v4l2_object_destroy (v4l2src->v4l2object);
218 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
223 gst_v4l2src_set_property (GObject * object,
224 guint prop_id, const GValue * value, GParamSpec * pspec)
226 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
228 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
229 prop_id, value, pspec)) {
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 gst_v4l2src_get_property (GObject * object,
240 guint prop_id, GValue * value, GParamSpec * pspec)
242 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
244 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
245 prop_id, value, pspec)) {
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 struct PreferedCapsInfo
263 gst_vl42_src_fixate_fields (GQuark field_id, GValue * value, gpointer user_data)
265 GstStructure *s = user_data;
267 if (field_id == g_quark_from_string ("interlace-mode"))
270 if (field_id == g_quark_from_string ("colorimetry"))
273 gst_structure_fixate_field (s, g_quark_to_string (field_id));
279 gst_v4l2_src_fixate_struct_with_preference (GstStructure * s,
280 struct PreferedCapsInfo *pref)
282 if (gst_structure_has_field (s, "width"))
283 gst_structure_fixate_field_nearest_int (s, "width", pref->width);
285 if (gst_structure_has_field (s, "height"))
286 gst_structure_fixate_field_nearest_int (s, "height", pref->height);
288 if (gst_structure_has_field (s, "framerate"))
289 gst_structure_fixate_field_nearest_fraction (s, "framerate", pref->fps_n,
292 /* Finally, fixate everything else except the interlace-mode and colorimetry
293 * which still need further negotiation as it wasn't probed */
294 gst_structure_map_in_place (s, gst_vl42_src_fixate_fields, s);
298 gst_v4l2_src_parse_fixed_struct (GstStructure * s,
299 gint * width, gint * height, gint * fps_n, gint * fps_d)
301 if (gst_structure_has_field (s, "width") && width)
302 gst_structure_get_int (s, "width", width);
304 if (gst_structure_has_field (s, "height") && height)
305 gst_structure_get_int (s, "height", height);
307 if (gst_structure_has_field (s, "framerate") && fps_n && fps_d)
308 gst_structure_get_fraction (s, "framerate", fps_n, fps_d);
311 /* TODO Consider framerate */
313 gst_v4l2src_fixed_caps_compare (GstStructure * a, GstStructure * b,
314 struct PreferedCapsInfo *pref)
316 gint aw = G_MAXINT, ah = G_MAXINT, ad = G_MAXINT;
317 gint bw = G_MAXINT, bh = G_MAXINT, bd = G_MAXINT;
319 gst_v4l2_src_parse_fixed_struct (a, &aw, &ah, NULL, NULL);
320 gst_v4l2_src_parse_fixed_struct (b, &bw, &bh, NULL, NULL);
322 /* When both are smaller then pref, just append to the end */
323 if ((bw < pref->width || bh < pref->height)
324 && (aw < pref->width || ah < pref->height))
327 /* If a is smaller then pref and not b, then a goes after b */
328 if (aw < pref->width || ah < pref->height)
331 /* If b is smaller then pref and not a, then a goes before b */
332 if (bw < pref->width || bh < pref->height)
335 /* Both are strictly larger then the preference, prefer the smallest */
336 ad = (aw - pref->width) * (ah - pref->height);
337 bd = (bw - pref->width) * (bh - pref->height);
343 gst_v4l2src_set_format (GstV4l2Src * v4l2src, GstCaps * caps,
344 GstV4l2Error * error)
348 obj = v4l2src->v4l2object;
350 /* make sure we stop capturing and dealloc buffers */
351 if (!gst_v4l2_object_stop (obj))
354 g_signal_emit (v4l2src, gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT], 0,
355 v4l2src->v4l2object->video_fd, caps);
357 return gst_v4l2_object_set_format (obj, caps, error);
361 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps, GstStructure * pref_s)
363 /* Let's prefer a good resolutiion as of today's standard. */
364 struct PreferedCapsInfo pref = {
367 GstV4l2Src *v4l2src = GST_V4L2SRC (basesrc);
368 GstV4l2Object *obj = v4l2src->v4l2object;
369 GList *caps_list = NULL;
372 GstV4l2Error error = GST_V4L2_ERROR_INIT;
373 GstCaps *fcaps = NULL;
375 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
377 caps = gst_caps_make_writable (caps);
379 /* We consider the first structure from peercaps to be a preference. This is
380 * useful for matching a reported native display, or simply to avoid
381 * transformation to happen downstream. */
383 pref_s = gst_structure_copy (pref_s);
384 gst_v4l2_src_fixate_struct_with_preference (pref_s, &pref);
385 gst_v4l2_src_parse_fixed_struct (pref_s, &pref.width, &pref.height,
386 &pref.fps_n, &pref.fps_d);
387 gst_structure_free (pref_s);
390 /* Sort the structures to get the caps that is nearest to our preferences,
392 while ((s = gst_caps_steal_structure (caps, 0))) {
393 gst_v4l2_src_fixate_struct_with_preference (s, &pref);
394 caps_list = g_list_insert_sorted_with_data (caps_list, s,
395 (GCompareDataFunc) gst_v4l2src_fixed_caps_compare, &pref);
400 caps_list = g_list_delete_link (caps_list, caps_list);
401 gst_caps_append_structure (caps, s);
404 GST_DEBUG_OBJECT (basesrc, "sorted and normalized caps %" GST_PTR_FORMAT,
407 /* Each structure in the caps has been fixated, except for the
408 * interlace-mode and colorimetry. Now normalize the caps so we can
409 * enumerate the possibilities */
410 caps = gst_caps_normalize (caps);
412 for (i = 0; i < gst_caps_get_size (caps); ++i) {
413 gst_v4l2_clear_error (&error);
415 gst_caps_unref (fcaps);
417 fcaps = gst_caps_copy_nth (caps, i);
419 if (GST_V4L2_IS_ACTIVE (obj)) {
420 /* Just check if the format is acceptable, once we know
421 * no buffers should be outstanding we try S_FMT.
423 * Basesrc will do an allocation query that
424 * should indirectly reclaim buffers, after that we can
425 * set the format and then configure our pool */
426 if (gst_v4l2_object_try_format (obj, fcaps, &error)) {
427 /* make sure the caps changed before doing anything */
428 if (gst_v4l2_object_caps_equal (obj, fcaps))
431 v4l2src->renegotiation_adjust = v4l2src->offset + 1;
432 v4l2src->pending_set_fmt = TRUE;
436 if (gst_v4l2src_set_format (v4l2src, fcaps, &error))
440 /* Only EIVAL make sense, report any other errors, this way we don't keep
441 * probing if the device got disconnected, or if it's firmware stopped
443 if (error.error->code != GST_RESOURCE_ERROR_SETTINGS) {
449 if (i >= gst_caps_get_size (caps)) {
450 gst_v4l2_error (v4l2src, &error);
452 gst_caps_unref (fcaps);
453 gst_caps_unref (caps);
457 gst_caps_unref (caps);
459 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, fcaps);
465 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
468 GstCaps *caps = NULL;
469 GstCaps *peercaps = NULL;
470 gboolean result = FALSE;
472 /* first see what is possible on our source pad */
473 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
474 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
476 /* nothing or anything is allowed, we're done */
477 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
480 /* get the peer caps without a filter as we'll filter ourselves later on */
481 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
482 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
483 if (peercaps && !gst_caps_is_any (peercaps)) {
484 /* Prefer the first caps we are compatible with that the peer proposed */
485 caps = gst_caps_intersect_full (peercaps, thiscaps,
486 GST_CAPS_INTERSECT_FIRST);
488 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
490 gst_caps_unref (thiscaps);
492 /* no peer or peer have ANY caps, work with our own caps then */
498 if (!gst_caps_is_empty (caps)) {
499 GstStructure *pref = NULL;
501 if (peercaps && !gst_caps_is_any (peercaps))
502 pref = gst_caps_get_structure (peercaps, 0);
504 caps = gst_v4l2src_fixate (basesrc, caps, pref);
506 /* Fixating may fail as we now set the selected format */
512 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
514 if (gst_caps_is_any (caps)) {
515 /* hmm, still anything, so element can do anything and
516 * nego is not needed */
518 } else if (gst_caps_is_fixed (caps)) {
519 /* yay, fixed caps, use those then */
520 result = gst_base_src_set_caps (basesrc, caps);
523 gst_caps_unref (caps);
528 gst_caps_unref (peercaps);
534 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
536 gst_caps_unref (thiscaps);
542 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
547 v4l2src = GST_V4L2SRC (src);
548 obj = v4l2src->v4l2object;
550 if (!GST_V4L2_IS_OPEN (obj)) {
551 return gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (v4l2src));
554 return gst_v4l2_object_get_caps (obj, filter);
558 gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
560 GstV4l2Src *src = GST_V4L2SRC (bsrc);
563 if (src->pending_set_fmt) {
564 GstCaps *caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (bsrc));
565 GstV4l2Error error = GST_V4L2_ERROR_INIT;
567 caps = gst_caps_make_writable (caps);
568 if (!(ret = gst_v4l2src_set_format (src, caps, &error)))
569 gst_v4l2_error (src, &error);
571 gst_caps_unref (caps);
572 src->pending_set_fmt = FALSE;
573 } else if (gst_buffer_pool_is_active (src->v4l2object->pool)) {
574 /* Trick basesrc into not deactivating the active pool. Renegotiating here
575 * would otherwise turn off and on the camera. */
576 GstAllocator *allocator;
577 GstAllocationParams params;
580 gst_base_src_get_allocator (bsrc, &allocator, ¶ms);
581 pool = gst_base_src_get_buffer_pool (bsrc);
583 if (gst_query_get_n_allocation_params (query))
584 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
586 gst_query_add_allocation_param (query, allocator, ¶ms);
588 if (gst_query_get_n_allocation_pools (query))
589 gst_query_set_nth_allocation_pool (query, 0, pool,
590 src->v4l2object->info.size, 1, 0);
592 gst_query_add_allocation_pool (query, pool, src->v4l2object->info.size, 1,
596 gst_object_unref (pool);
598 gst_object_unref (allocator);
600 return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
604 ret = gst_v4l2_object_decide_allocation (src->v4l2object, query);
606 ret = GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
610 if (!gst_buffer_pool_set_active (src->v4l2object->pool, TRUE))
611 goto activate_failed;
618 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
619 (_("Failed to allocate required memory.")),
620 ("Buffer pool activation failed"));
626 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
630 gboolean res = FALSE;
632 src = GST_V4L2SRC (bsrc);
633 obj = src->v4l2object;
635 switch (GST_QUERY_TYPE (query)) {
636 case GST_QUERY_LATENCY:{
637 GstClockTime min_latency, max_latency;
638 guint32 fps_n, fps_d;
639 guint num_buffers = 0;
641 /* device must be open */
642 if (!GST_V4L2_IS_OPEN (obj)) {
643 GST_WARNING_OBJECT (src,
644 "Can't give latency since device isn't open !");
648 fps_n = GST_V4L2_FPS_N (obj);
649 fps_d = GST_V4L2_FPS_D (obj);
651 /* we must have a framerate */
652 if (fps_n <= 0 || fps_d <= 0) {
653 GST_WARNING_OBJECT (src,
654 "Can't give latency since framerate isn't fixated !");
658 /* min latency is the time to capture one frame */
659 min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
661 /* max latency is total duration of the frame buffer */
662 if (obj->pool != NULL)
663 num_buffers = GST_V4L2_BUFFER_POOL_CAST (obj->pool)->max_latency;
665 if (num_buffers == 0)
668 max_latency = num_buffers * min_latency;
670 GST_DEBUG_OBJECT (bsrc,
671 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
672 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
674 /* we are always live, the min latency is 1 frame and the max latency is
675 * the complete buffer of frames. */
676 gst_query_set_latency (query, TRUE, min_latency, max_latency);
682 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
691 /* start and stop are not symmetric -- start will open the device, but not start
692 * capture. it's setcaps that will start capture, which is called via basesrc's
693 * negotiate method. stop will both stop capture and close the device.
696 gst_v4l2src_start (GstBaseSrc * src)
698 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
701 v4l2src->renegotiation_adjust = 0;
703 /* activate settings for first frame */
704 v4l2src->ctrl_time = 0;
705 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
707 v4l2src->has_bad_timestamp = FALSE;
708 v4l2src->last_timestamp = 0;
714 gst_v4l2src_unlock (GstBaseSrc * src)
716 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
717 return gst_v4l2_object_unlock (v4l2src->v4l2object);
721 gst_v4l2src_unlock_stop (GstBaseSrc * src)
723 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
725 v4l2src->last_timestamp = 0;
727 return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
731 gst_v4l2src_stop (GstBaseSrc * src)
733 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
734 GstV4l2Object *obj = v4l2src->v4l2object;
736 if (GST_V4L2_IS_ACTIVE (obj)) {
737 if (!gst_v4l2_object_stop (obj))
741 v4l2src->pending_set_fmt = FALSE;
746 static GstStateChangeReturn
747 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
749 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
750 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
751 GstV4l2Object *obj = v4l2src->v4l2object;
753 switch (transition) {
754 case GST_STATE_CHANGE_NULL_TO_READY:
755 /* open the device */
756 if (!gst_v4l2_object_open (obj))
757 return GST_STATE_CHANGE_FAILURE;
763 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
765 switch (transition) {
766 case GST_STATE_CHANGE_READY_TO_NULL:
767 /* close the device */
768 if (!gst_v4l2_object_close (obj))
769 return GST_STATE_CHANGE_FAILURE;
780 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
782 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
783 GstV4l2Object *obj = v4l2src->v4l2object;
784 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL_CAST (obj->pool);
787 GstClockTime abs_time, base_time, timestamp, duration;
792 ret = GST_BASE_SRC_CLASS (parent_class)->alloc (GST_BASE_SRC (src), 0,
793 obj->info.size, buf);
795 if (G_UNLIKELY (ret != GST_FLOW_OK))
798 ret = gst_v4l2_buffer_pool_process (pool, buf);
800 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
802 if (G_UNLIKELY (ret != GST_FLOW_OK))
805 timestamp = GST_BUFFER_TIMESTAMP (*buf);
806 duration = obj->duration;
808 /* timestamps, LOCK to get clock and base time. */
809 /* FIXME: element clock and base_time is rarely changing */
810 GST_OBJECT_LOCK (v4l2src);
811 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
812 /* we have a clock, get base time and ref clock */
813 base_time = GST_ELEMENT (v4l2src)->base_time;
814 gst_object_ref (clock);
816 /* no clock, can't set timestamps */
817 base_time = GST_CLOCK_TIME_NONE;
819 GST_OBJECT_UNLOCK (v4l2src);
821 /* sample pipeline clock */
823 abs_time = gst_clock_get_time (clock);
824 gst_object_unref (clock);
826 abs_time = GST_CLOCK_TIME_NONE;
830 if (!v4l2src->has_bad_timestamp && timestamp != GST_CLOCK_TIME_NONE) {
834 /* v4l2 specs say to use the system time although many drivers switched to
835 * the more desirable monotonic time. We first try to use the monotonic time
836 * and see how that goes */
837 clock_gettime (CLOCK_MONOTONIC, &now);
838 gstnow = GST_TIMESPEC_TO_TIME (now);
840 if (timestamp > gstnow || (gstnow - timestamp) > (10 * GST_SECOND)) {
843 /* very large diff, fall back to system time */
844 g_get_current_time (&now);
845 gstnow = GST_TIMEVAL_TO_TIME (now);
848 /* Detect buggy drivers here, and stop using their timestamp. Failing any
849 * of these condition would imply a very buggy driver:
850 * - Timestamp in the future
851 * - Timestamp is going backward compare to last seen timestamp
852 * - Timestamp is jumping forward for less then a frame duration
853 * - Delay is bigger then the actual timestamp
855 if (timestamp > gstnow) {
856 GST_WARNING_OBJECT (v4l2src,
857 "Timestamp in the future detected, ignoring driver timestamps");
858 v4l2src->has_bad_timestamp = TRUE;
862 if (v4l2src->last_timestamp > timestamp) {
863 GST_WARNING_OBJECT (v4l2src,
864 "Timestamp going backward, ignoring driver timestamps");
865 v4l2src->has_bad_timestamp = TRUE;
869 delay = gstnow - timestamp;
871 if (delay > timestamp) {
872 GST_WARNING_OBJECT (v4l2src,
873 "Timestamp does not correlate with any clock, ignoring driver timestamps");
874 v4l2src->has_bad_timestamp = TRUE;
878 /* Save last timestamp for sanity checks */
879 v4l2src->last_timestamp = timestamp;
881 GST_DEBUG_OBJECT (v4l2src, "ts: %" GST_TIME_FORMAT " now %" GST_TIME_FORMAT
882 " delay %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
883 GST_TIME_ARGS (gstnow), GST_TIME_ARGS (delay));
885 /* we assume 1 frame latency otherwise */
886 if (GST_CLOCK_TIME_IS_VALID (duration))
892 /* set buffer metadata */
894 if (G_LIKELY (abs_time != GST_CLOCK_TIME_NONE)) {
895 /* the time now is the time of the clock minus the base time */
896 timestamp = abs_time - base_time;
898 /* adjust for delay in the device */
899 if (timestamp > delay)
904 timestamp = GST_CLOCK_TIME_NONE;
907 /* activate settings for next frame */
908 if (GST_CLOCK_TIME_IS_VALID (duration)) {
909 v4l2src->ctrl_time += duration;
911 /* this is not very good (as it should be the next timestamp),
912 * still good enough for linear fades (as long as it is not -1)
914 v4l2src->ctrl_time = timestamp;
916 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
918 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT " out ts %" GST_TIME_FORMAT,
919 GST_TIME_ARGS (v4l2src->ctrl_time), GST_TIME_ARGS (timestamp));
921 /* use generated offset values only if there are not already valid ones
922 * set by the v4l2 device */
923 if (!GST_BUFFER_OFFSET_IS_VALID (*buf)
924 || !GST_BUFFER_OFFSET_END_IS_VALID (*buf)) {
925 GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
926 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
928 /* adjust raw v4l2 device sequence, will restart at null in case of renegotiation
929 * (streamoff/streamon) */
930 GST_BUFFER_OFFSET (*buf) += v4l2src->renegotiation_adjust;
931 GST_BUFFER_OFFSET_END (*buf) += v4l2src->renegotiation_adjust;
932 /* check for frame loss with given (from v4l2 device) buffer offset */
933 if ((v4l2src->offset != 0)
934 && (GST_BUFFER_OFFSET (*buf) != (v4l2src->offset + 1))) {
935 guint64 lost_frame_count = GST_BUFFER_OFFSET (*buf) - v4l2src->offset - 1;
936 GST_WARNING_OBJECT (v4l2src,
937 "lost frames detected: count = %" G_GUINT64_FORMAT " - ts: %"
938 GST_TIME_FORMAT, lost_frame_count, GST_TIME_ARGS (timestamp));
940 qos_msg = gst_message_new_qos (GST_OBJECT_CAST (v4l2src), TRUE,
941 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE, timestamp,
942 GST_CLOCK_TIME_IS_VALID (duration) ? lost_frame_count *
943 duration : GST_CLOCK_TIME_NONE);
944 gst_element_post_message (GST_ELEMENT_CAST (v4l2src), qos_msg);
947 v4l2src->offset = GST_BUFFER_OFFSET (*buf);
950 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
951 GST_BUFFER_DURATION (*buf) = duration;
958 if (ret != GST_FLOW_FLUSHING)
959 GST_ELEMENT_ERROR (src, RESOURCE, NO_SPACE_LEFT,
960 ("Failed to allocate a buffer"), (NULL));
965 gst_buffer_replace (buf, NULL);
966 if (ret == GST_V4L2_FLOW_LAST_BUFFER) {
967 GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
968 ("Driver returned a buffer with no payload, this most likely "
969 "indicate a bug in the driver."), (NULL));
970 ret = GST_FLOW_ERROR;
972 GST_DEBUG_OBJECT (src, "error processing buffer %d (%s)", ret,
973 gst_flow_get_name (ret));
980 /* GstURIHandler interface */
982 gst_v4l2src_uri_get_type (GType type)
987 static const gchar *const *
988 gst_v4l2src_uri_get_protocols (GType type)
990 static const gchar *protocols[] = { "v4l2", NULL };
996 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
998 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1000 if (v4l2src->v4l2object->videodev != NULL) {
1001 return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
1004 return g_strdup ("v4l2://");
1008 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1011 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1012 const gchar *device = DEFAULT_PROP_DEVICE;
1014 if (strcmp (uri, "v4l2://") != 0) {
1017 g_object_set (v4l2src, "device", device, NULL);
1024 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1026 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1028 iface->get_type = gst_v4l2src_uri_get_type;
1029 iface->get_protocols = gst_v4l2src_uri_get_protocols;
1030 iface->get_uri = gst_v4l2src_uri_get_uri;
1031 iface->set_uri = gst_v4l2src_uri_set_uri;