3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
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
53 #include <gst/video/gstvideometa.h>
54 #include <gst/video/gstvideopool.h>
56 #include "gstv4l2src.h"
58 #include "gstv4l2colorbalance.h"
59 #include "gstv4l2tuner.h"
61 #include "gstv4l2xoverlay.h"
63 #include "gstv4l2vidorient.h"
65 #include "gst/gst-i18n-plugin.h"
67 GST_DEBUG_CATEGORY (v4l2src_debug);
68 #define GST_CAT_DEFAULT v4l2src_debug
70 #define DEFAULT_PROP_DEVICE "/dev/video0"
75 V4L2_STD_OBJECT_PROPS,
79 /* signals and args */
82 SIGNAL_PRE_SET_FORMAT,
86 static guint gst_v4l2_signals[LAST_SIGNAL] = { 0 };
88 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
89 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
91 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
93 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
95 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
98 #define gst_v4l2src_parent_class parent_class
99 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
100 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
101 G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
103 /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
104 G_IMPLEMENT_INTERFACE (GST_TYPE_X_OVERLAY,
105 gst_v4l2src_xoverlay_interface_init);
107 G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
108 gst_v4l2src_color_balance_interface_init);
109 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
110 gst_v4l2src_video_orientation_interface_init));
112 static void gst_v4l2src_dispose (GObject * object);
113 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
115 /* element methods */
116 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
117 GstStateChange transition);
119 /* basesrc methods */
120 static gboolean gst_v4l2src_start (GstBaseSrc * src);
121 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
122 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
123 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
124 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
125 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
126 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
127 static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
129 static GstFlowReturn gst_v4l2src_fill (GstPushSrc * src, GstBuffer * out);
130 static GstCaps *gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
131 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
133 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
134 const GValue * value, GParamSpec * pspec);
135 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
136 GValue * value, GParamSpec * pspec);
139 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
141 GObjectClass *gobject_class;
142 GstElementClass *element_class;
143 GstBaseSrcClass *basesrc_class;
144 GstPushSrcClass *pushsrc_class;
146 gobject_class = G_OBJECT_CLASS (klass);
147 element_class = GST_ELEMENT_CLASS (klass);
148 basesrc_class = GST_BASE_SRC_CLASS (klass);
149 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
151 gobject_class->dispose = gst_v4l2src_dispose;
152 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
153 gobject_class->set_property = gst_v4l2src_set_property;
154 gobject_class->get_property = gst_v4l2src_get_property;
156 element_class->change_state = gst_v4l2src_change_state;
158 gst_v4l2_object_install_properties_helper (gobject_class,
159 DEFAULT_PROP_DEVICE);
162 * GstV4l2Src::prepare-format:
163 * @v4l2src: the v4l2src instance
164 * @fd: the file descriptor of the current device
165 * @caps: the caps of the format being set
167 * This signal gets emitted before calling the v4l2 VIDIOC_S_FMT ioctl
168 * (set format). This allows for any custom configuration of the device to
169 * happen prior to the format being set.
170 * This is mostly useful for UVC H264 encoding cameras which need the H264
171 * Probe & Commit to happen prior to the normal Probe & Commit.
175 gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT] = g_signal_new ("prepare-format",
176 G_TYPE_FROM_CLASS (klass),
178 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CAPS);
180 gst_element_class_set_static_metadata (element_class,
181 "Video (video4linux2) Source", "Source/Video",
182 "Reads frames from a Video4Linux2 device",
183 "Edgard Lima <edgard.lima@indt.org.br>, "
184 "Stefan Kost <ensonic@users.sf.net>");
186 gst_element_class_add_pad_template
188 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
189 gst_v4l2_object_get_all_caps ()));
191 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
192 basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
193 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
194 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
195 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
196 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
197 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
198 basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
199 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
200 basesrc_class->decide_allocation =
201 GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
203 pushsrc_class->fill = GST_DEBUG_FUNCPTR (gst_v4l2src_fill);
205 klass->v4l2_class_devices = NULL;
207 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
211 gst_v4l2src_init (GstV4l2Src * v4l2src)
213 /* fixme: give an update_fps_function */
214 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
215 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
216 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
218 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
219 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
223 gst_v4l2src_dispose (GObject * object)
225 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
227 if (v4l2src->probed_caps) {
228 gst_caps_unref (v4l2src->probed_caps);
231 G_OBJECT_CLASS (parent_class)->dispose (object);
236 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
238 gst_v4l2_object_destroy (v4l2src->v4l2object);
240 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
245 gst_v4l2src_set_property (GObject * object,
246 guint prop_id, const GValue * value, GParamSpec * pspec)
248 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
250 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
251 prop_id, value, pspec)) {
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 gst_v4l2src_get_property (GObject * object,
262 guint prop_id, GValue * value, GParamSpec * pspec)
264 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
266 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
267 prop_id, value, pspec)) {
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 /* this function is a bit of a last resort */
278 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
280 GstStructure *structure;
283 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
285 caps = gst_caps_make_writable (caps);
287 for (i = 0; i < gst_caps_get_size (caps); ++i) {
288 structure = gst_caps_get_structure (caps, i);
290 /* We are fixating to a resonable 320x200 resolution
291 and the maximum framerate resolution for that size */
292 gst_structure_fixate_field_nearest_int (structure, "width", 320);
293 gst_structure_fixate_field_nearest_int (structure, "height", 200);
294 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
296 gst_structure_fixate_field (structure, "format");
299 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
301 caps = GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
308 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
311 GstCaps *caps = NULL;
312 GstCaps *peercaps = NULL;
313 gboolean result = FALSE;
315 /* first see what is possible on our source pad */
316 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
317 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
318 LOG_CAPS (basesrc, thiscaps);
320 /* nothing or anything is allowed, we're done */
321 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
324 /* get the peer caps */
325 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
326 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
327 LOG_CAPS (basesrc, peercaps);
328 if (peercaps && !gst_caps_is_any (peercaps)) {
329 GstCaps *icaps = NULL;
332 /* Prefer the first caps we are compatible with that the peer proposed */
333 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
334 /* get intersection */
335 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
337 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
338 LOG_CAPS (basesrc, ipcaps);
340 icaps = gst_caps_intersect (thiscaps, ipcaps);
341 gst_caps_unref (ipcaps);
343 if (!gst_caps_is_empty (icaps))
346 gst_caps_unref (icaps);
350 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
351 LOG_CAPS (basesrc, icaps);
353 /* If there are multiple intersections pick the one with the smallest
354 * resolution strictly bigger then the first peer caps */
355 if (gst_caps_get_size (icaps) > 1) {
356 GstStructure *s = gst_caps_get_structure (peercaps, 0);
359 int width = G_MAXINT, height = G_MAXINT;
361 if (gst_structure_get_int (s, "width", &twidth)
362 && gst_structure_get_int (s, "height", &theight)) {
364 /* Walk the structure backwards to get the first entry of the
365 * smallest resolution bigger (or equal to) the preferred resolution)
367 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
368 GstStructure *is = gst_caps_get_structure (icaps, i);
371 if (gst_structure_get_int (is, "width", &w)
372 && gst_structure_get_int (is, "height", &h)) {
373 if (w >= twidth && w <= width && h >= theight && h <= height) {
382 caps = gst_caps_copy_nth (icaps, best);
383 gst_caps_unref (icaps);
388 gst_caps_unref (thiscaps);
390 /* no peer or peer have ANY caps, work with our own caps then */
394 gst_caps_unref (peercaps);
396 caps = gst_caps_truncate (caps);
399 if (!gst_caps_is_empty (caps)) {
400 caps = gst_v4l2src_fixate (basesrc, caps);
401 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
402 LOG_CAPS (basesrc, caps);
404 if (gst_caps_is_any (caps)) {
405 /* hmm, still anything, so element can do anything and
406 * nego is not needed */
408 } else if (gst_caps_is_fixed (caps)) {
409 /* yay, fixed caps, use those then */
410 result = gst_base_src_set_caps (basesrc, caps);
413 gst_caps_unref (caps);
419 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
421 gst_caps_unref (thiscaps);
427 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
435 v4l2src = GST_V4L2SRC (src);
436 obj = v4l2src->v4l2object;
438 if (!GST_V4L2_IS_OPEN (obj)) {
439 return gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (v4l2src));
442 if (v4l2src->probed_caps)
443 return gst_caps_ref (v4l2src->probed_caps);
445 formats = gst_v4l2_object_get_format_list (obj);
447 ret = gst_caps_new_empty ();
449 for (walk = formats; walk; walk = walk->next) {
450 struct v4l2_fmtdesc *format;
451 GstStructure *template;
453 format = (struct v4l2_fmtdesc *) walk->data;
455 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
461 gst_v4l2_object_probe_caps_for_format (obj,
462 format->pixelformat, template);
464 gst_caps_append (ret, tmp);
466 gst_structure_free (template);
468 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
472 v4l2src->probed_caps = gst_caps_ref (ret);
474 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
480 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
485 v4l2src = GST_V4L2SRC (src);
486 obj = v4l2src->v4l2object;
488 /* make sure we stop capturing and dealloc buffers */
489 if (!gst_v4l2_object_stop (obj))
492 g_signal_emit (v4l2src, gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT], 0,
493 v4l2src->v4l2object->video_fd, caps);
495 if (!gst_v4l2_object_set_format (obj, caps))
496 /* error already posted */
503 gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
508 guint size, min, max;
511 src = GST_V4L2SRC (bsrc);
512 obj = src->v4l2object;
514 if (gst_query_get_n_allocation_pools (query) > 0) {
515 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
524 GST_DEBUG_OBJECT (src, "allocation: size:%u min:%u max:%u pool:%"
525 GST_PTR_FORMAT, size, min, max, pool);
528 /* if there is a min-buffers suggestion, use it. We add 1 because we need 1
529 * buffer extra to capture while the other two buffers are downstream */
539 /* no downstream pool, use our own then */
540 GST_DEBUG_OBJECT (src,
541 "read/write mode: no downstream pool, using our own");
542 pool = GST_BUFFER_POOL_CAST (obj->pool);
543 size = obj->sizeimage;
545 /* in READ/WRITE mode, prefer a downstream pool because our own pool
546 * doesn't help much, we have to write to it as well */
547 GST_DEBUG_OBJECT (src, "read/write mode: using downstream pool");
548 /* use the bigest size, when we use our own pool we can't really do any
549 * other size than what the hardware gives us but for downstream pools
551 size = MAX (size, obj->sizeimage);
554 case GST_V4L2_IO_MMAP:
555 case GST_V4L2_IO_USERPTR:
556 case GST_V4L2_IO_DMABUF:
557 /* in streaming mode, prefer our own pool */
558 pool = GST_BUFFER_POOL_CAST (obj->pool);
559 size = obj->sizeimage;
560 GST_DEBUG_OBJECT (src,
561 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
563 case GST_V4L2_IO_AUTO:
565 GST_WARNING_OBJECT (src, "unhandled mode");
570 GstStructure *config;
573 config = gst_buffer_pool_get_config (pool);
574 gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL);
575 gst_buffer_pool_config_set_params (config, caps, size, min, max);
577 /* if downstream supports video metadata, add this to the pool config */
578 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
579 GST_DEBUG_OBJECT (pool, "activate Video Meta");
580 gst_buffer_pool_config_add_option (config,
581 GST_BUFFER_POOL_OPTION_VIDEO_META);
584 gst_buffer_pool_set_config (pool, config);
588 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
590 gst_query_add_allocation_pool (query, pool, size, min, max);
592 return GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
596 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
600 gboolean res = FALSE;
602 src = GST_V4L2SRC (bsrc);
603 obj = src->v4l2object;
605 switch (GST_QUERY_TYPE (query)) {
606 case GST_QUERY_LATENCY:{
607 GstClockTime min_latency, max_latency;
608 guint32 fps_n, fps_d;
609 guint num_buffers = 0;
611 /* device must be open */
612 if (!GST_V4L2_IS_OPEN (obj)) {
613 GST_WARNING_OBJECT (src,
614 "Can't give latency since device isn't open !");
618 fps_n = GST_V4L2_FPS_N (obj);
619 fps_d = GST_V4L2_FPS_D (obj);
621 /* we must have a framerate */
622 if (fps_n <= 0 || fps_d <= 0) {
623 GST_WARNING_OBJECT (src,
624 "Can't give latency since framerate isn't fixated !");
628 /* min latency is the time to capture one frame */
629 min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
631 /* max latency is total duration of the frame buffer */
632 if (obj->pool != NULL)
633 num_buffers = GST_V4L2_BUFFER_POOL_CAST (obj->pool)->num_buffers;
635 if (num_buffers == 0)
638 max_latency = num_buffers * min_latency;
640 GST_DEBUG_OBJECT (bsrc,
641 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
642 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
644 /* we are always live, the min latency is 1 frame and the max latency is
645 * the complete buffer of frames. */
646 gst_query_set_latency (query, TRUE, min_latency, max_latency);
652 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
661 /* start and stop are not symmetric -- start will open the device, but not start
662 * capture. it's setcaps that will start capture, which is called via basesrc's
663 * negotiate method. stop will both stop capture and close the device.
666 gst_v4l2src_start (GstBaseSrc * src)
668 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
672 /* activate settings for first frame */
673 v4l2src->ctrl_time = 0;
674 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
680 gst_v4l2src_unlock (GstBaseSrc * src)
682 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
683 return gst_v4l2_object_unlock (v4l2src->v4l2object);
687 gst_v4l2src_unlock_stop (GstBaseSrc * src)
689 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
690 return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
694 gst_v4l2src_stop (GstBaseSrc * src)
696 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
697 GstV4l2Object *obj = v4l2src->v4l2object;
699 if (GST_V4L2_IS_ACTIVE (obj)) {
700 if (!gst_v4l2_object_stop (obj))
706 static GstStateChangeReturn
707 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
709 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
710 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
711 GstV4l2Object *obj = v4l2src->v4l2object;
713 switch (transition) {
714 case GST_STATE_CHANGE_NULL_TO_READY:
715 /* open the device */
716 if (!gst_v4l2_object_open (obj))
717 return GST_STATE_CHANGE_FAILURE;
723 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
725 switch (transition) {
726 case GST_STATE_CHANGE_READY_TO_NULL:
727 /* close the device */
728 if (!gst_v4l2_object_close (obj))
729 return GST_STATE_CHANGE_FAILURE;
731 if (v4l2src->probed_caps) {
732 gst_caps_unref (v4l2src->probed_caps);
733 v4l2src->probed_caps = NULL;
744 gst_v4l2src_fill (GstPushSrc * src, GstBuffer * buf)
746 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
747 GstV4l2Object *obj = v4l2src->v4l2object;
750 GstClockTime abs_time, base_time, timestamp, duration;
754 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL_CAST (obj->pool), buf);
756 if (G_UNLIKELY (ret != GST_FLOW_OK))
760 timestamp = GST_BUFFER_TIMESTAMP (buf);
761 duration = obj->duration;
763 /* timestamps, LOCK to get clock and base time. */
764 /* FIXME: element clock and base_time is rarely changing */
765 GST_OBJECT_LOCK (v4l2src);
766 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
767 /* we have a clock, get base time and ref clock */
768 base_time = GST_ELEMENT (v4l2src)->base_time;
769 gst_object_ref (clock);
771 /* no clock, can't set timestamps */
772 base_time = GST_CLOCK_TIME_NONE;
774 GST_OBJECT_UNLOCK (v4l2src);
776 /* sample pipeline clock */
778 abs_time = gst_clock_get_time (clock);
779 gst_object_unref (clock);
781 abs_time = GST_CLOCK_TIME_NONE;
784 if (timestamp != GST_CLOCK_TIME_NONE) {
788 /* v4l2 specs say to use the system time although many drivers switched to
789 * the more desirable monotonic time. We first try to use the monotonic time
790 * and see how that goes */
791 clock_gettime (CLOCK_MONOTONIC, &now);
792 gstnow = GST_TIMESPEC_TO_TIME (now);
794 if (gstnow < timestamp && (timestamp - gstnow) > (10 * GST_SECOND)) {
797 /* very large diff, fall back to system time */
798 g_get_current_time (&now);
799 gstnow = GST_TIMEVAL_TO_TIME (now);
802 if (gstnow > timestamp) {
803 delay = gstnow - timestamp;
808 GST_DEBUG_OBJECT (v4l2src, "ts: %" GST_TIME_FORMAT " now %" GST_TIME_FORMAT
809 " delay %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
810 GST_TIME_ARGS (gstnow), GST_TIME_ARGS (delay));
812 /* we assume 1 frame latency otherwise */
813 if (GST_CLOCK_TIME_IS_VALID (duration))
819 /* set buffer metadata */
820 GST_BUFFER_OFFSET (buf) = v4l2src->offset++;
821 GST_BUFFER_OFFSET_END (buf) = v4l2src->offset;
823 if (G_LIKELY (abs_time != GST_CLOCK_TIME_NONE)) {
824 /* the time now is the time of the clock minus the base time */
825 timestamp = abs_time - base_time;
827 /* adjust for delay in the device */
828 if (timestamp > delay)
833 timestamp = GST_CLOCK_TIME_NONE;
836 /* activate settings for next frame */
837 if (GST_CLOCK_TIME_IS_VALID (duration)) {
838 v4l2src->ctrl_time += duration;
840 /* this is not very good (as it should be the next timestamp),
841 * still good enough for linear fades (as long as it is not -1)
843 v4l2src->ctrl_time = timestamp;
845 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
847 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT " out ts %" GST_TIME_FORMAT,
848 GST_TIME_ARGS (v4l2src->ctrl_time), GST_TIME_ARGS (timestamp));
850 GST_BUFFER_TIMESTAMP (buf) = timestamp;
851 GST_BUFFER_DURATION (buf) = duration;
858 GST_DEBUG_OBJECT (src, "error processing buffer %d (%s)", ret,
859 gst_flow_get_name (ret));
865 /* GstURIHandler interface */
867 gst_v4l2src_uri_get_type (GType type)
872 static const gchar *const *
873 gst_v4l2src_uri_get_protocols (GType type)
875 static const gchar *protocols[] = { "v4l2", NULL };
881 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
883 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
885 if (v4l2src->v4l2object->videodev != NULL) {
886 return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
889 return g_strdup ("v4l2://");
893 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
896 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
897 const gchar *device = DEFAULT_PROP_DEVICE;
899 if (strcmp (uri, "v4l2://") != 0) {
902 g_object_set (v4l2src, "device", device, NULL);
909 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
911 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
913 iface->get_type = gst_v4l2src_uri_get_type;
914 iface->get_protocols = gst_v4l2src_uri_get_protocols;
915 iface->get_uri = gst_v4l2src_uri_get_uri;
916 iface->set_uri = gst_v4l2src_uri_set_uri;