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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 v4l2src ! xvimagesink
34 * ]| This pipeline shows the video captured from /dev/video0 tv card and for
37 * gst-launch 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 PROP_DEF_ALWAYS_COPY TRUE
71 #define PROP_DEF_DECIMATE 1
73 #define DEFAULT_PROP_DEVICE "/dev/video0"
78 V4L2_STD_OBJECT_PROPS,
83 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
84 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
86 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
88 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
90 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
93 #define gst_v4l2src_parent_class parent_class
94 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
95 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
96 G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
98 /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
99 G_IMPLEMENT_INTERFACE (GST_TYPE_X_OVERLAY,
100 gst_v4l2src_xoverlay_interface_init);
102 G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
103 gst_v4l2src_color_balance_interface_init);
104 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
105 gst_v4l2src_video_orientation_interface_init));
107 static void gst_v4l2src_dispose (GObject * object);
108 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
110 /* element methods */
111 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
112 GstStateChange transition);
114 /* basesrc methods */
115 static gboolean gst_v4l2src_start (GstBaseSrc * src);
116 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
117 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
118 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
119 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
120 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
121 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
122 static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
124 static GstFlowReturn gst_v4l2src_fill (GstPushSrc * src, GstBuffer * out);
125 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
126 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
128 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
129 const GValue * value, GParamSpec * pspec);
130 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
131 GValue * value, GParamSpec * pspec);
134 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
136 GObjectClass *gobject_class;
137 GstElementClass *element_class;
138 GstBaseSrcClass *basesrc_class;
139 GstPushSrcClass *pushsrc_class;
141 gobject_class = G_OBJECT_CLASS (klass);
142 element_class = GST_ELEMENT_CLASS (klass);
143 basesrc_class = GST_BASE_SRC_CLASS (klass);
144 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
146 gobject_class->dispose = gst_v4l2src_dispose;
147 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
148 gobject_class->set_property = gst_v4l2src_set_property;
149 gobject_class->get_property = gst_v4l2src_get_property;
151 element_class->change_state = gst_v4l2src_change_state;
153 gst_v4l2_object_install_properties_helper (gobject_class,
154 DEFAULT_PROP_DEVICE);
155 g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
156 g_param_spec_boolean ("always-copy", "Always Copy",
157 "If the buffer will or not be used directly from mmap",
158 PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 * GstV4l2Src:decimate
162 * Only use every nth frame
166 g_object_class_install_property (gobject_class, PROP_DECIMATE,
167 g_param_spec_int ("decimate", "Decimate",
168 "Only use every nth frame", 1, G_MAXINT,
169 PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 gst_element_class_set_details_simple (element_class,
172 "Video (video4linux2) Source", "Source/Video",
173 "Reads frames from a Video4Linux2 device",
174 "Edgard Lima <edgard.lima@indt.org.br>, "
175 "Stefan Kost <ensonic@users.sf.net>");
177 gst_element_class_add_pad_template
179 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
180 gst_v4l2_object_get_all_caps ()));
182 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
183 basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
184 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
185 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
186 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
187 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
188 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
189 basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
190 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
191 basesrc_class->decide_allocation =
192 GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
194 pushsrc_class->fill = GST_DEBUG_FUNCPTR (gst_v4l2src_fill);
196 klass->v4l2_class_devices = NULL;
198 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
202 gst_v4l2src_init (GstV4l2Src * v4l2src)
204 /* fixme: give an update_fps_function */
205 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
206 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
207 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
209 v4l2src->v4l2object->always_copy = PROP_DEF_ALWAYS_COPY;
210 v4l2src->decimate = PROP_DEF_DECIMATE;
212 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
213 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
217 gst_v4l2src_dispose (GObject * object)
219 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
221 if (v4l2src->probed_caps) {
222 gst_caps_unref (v4l2src->probed_caps);
225 G_OBJECT_CLASS (parent_class)->dispose (object);
230 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
232 gst_v4l2_object_destroy (v4l2src->v4l2object);
234 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
239 gst_v4l2src_set_property (GObject * object,
240 guint prop_id, const GValue * value, GParamSpec * pspec)
242 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
244 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
245 prop_id, value, pspec)) {
247 case PROP_ALWAYS_COPY:
248 v4l2src->v4l2object->always_copy = g_value_get_boolean (value);
251 v4l2src->decimate = g_value_get_int (value);
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)) {
269 case PROP_ALWAYS_COPY:
270 g_value_set_boolean (value, v4l2src->v4l2object->always_copy);
273 g_value_set_int (value, v4l2src->decimate);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 /* this function is a bit of a last resort */
284 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
286 GstStructure *structure;
289 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
291 for (i = 0; i < gst_caps_get_size (caps); ++i) {
292 structure = gst_caps_get_structure (caps, i);
294 /* We are fixating to a resonable 320x200 resolution
295 and the maximum framerate resolution for that size */
296 gst_structure_fixate_field_nearest_int (structure, "width", 320);
297 gst_structure_fixate_field_nearest_int (structure, "height", 200);
298 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
300 gst_structure_fixate_field (structure, "format");
303 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
305 GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
310 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
313 GstCaps *caps = NULL;
314 GstCaps *peercaps = NULL;
315 gboolean result = FALSE;
317 /* first see what is possible on our source pad */
318 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
319 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
320 LOG_CAPS (basesrc, thiscaps);
322 /* nothing or anything is allowed, we're done */
323 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
326 /* get the peer caps */
327 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
328 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
329 LOG_CAPS (basesrc, peercaps);
330 if (peercaps && !gst_caps_is_any (peercaps)) {
331 GstCaps *icaps = NULL;
334 /* Prefer the first caps we are compatible with that the peer proposed */
335 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
336 /* get intersection */
337 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
339 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
340 LOG_CAPS (basesrc, ipcaps);
342 icaps = gst_caps_intersect (thiscaps, ipcaps);
343 gst_caps_unref (ipcaps);
345 if (!gst_caps_is_empty (icaps))
348 gst_caps_unref (icaps);
352 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
353 LOG_CAPS (basesrc, icaps);
355 /* If there are multiple intersections pick the one with the smallest
356 * resolution strictly bigger then the first peer caps */
357 if (gst_caps_get_size (icaps) > 1) {
358 GstStructure *s = gst_caps_get_structure (peercaps, 0);
361 int width = G_MAXINT, height = G_MAXINT;
363 if (gst_structure_get_int (s, "width", &twidth)
364 && gst_structure_get_int (s, "height", &theight)) {
366 /* Walk the structure backwards to get the first entry of the
367 * smallest resolution bigger (or equal to) the preferred resolution)
369 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
370 GstStructure *is = gst_caps_get_structure (icaps, i);
373 if (gst_structure_get_int (is, "width", &w)
374 && gst_structure_get_int (is, "height", &h)) {
375 if (w >= twidth && w <= width && h >= theight && h <= height) {
384 caps = gst_caps_copy_nth (icaps, best);
385 gst_caps_unref (icaps);
390 gst_caps_unref (thiscaps);
391 gst_caps_unref (peercaps);
393 /* no peer or peer have ANY caps, work with our own caps then */
397 caps = gst_caps_make_writable (caps);
398 gst_caps_truncate (caps);
401 if (!gst_caps_is_empty (caps)) {
402 gst_v4l2src_fixate (basesrc, caps);
403 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
404 LOG_CAPS (basesrc, caps);
406 if (gst_caps_is_any (caps)) {
407 /* hmm, still anything, so element can do anything and
408 * nego is not needed */
410 } else if (gst_caps_is_fixed (caps)) {
411 /* yay, fixed caps, use those then */
412 result = gst_base_src_set_caps (basesrc, caps);
415 gst_caps_unref (caps);
421 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
423 gst_caps_unref (thiscaps);
429 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
437 v4l2src = GST_V4L2SRC (src);
438 obj = v4l2src->v4l2object;
440 if (!GST_V4L2_IS_OPEN (obj)) {
443 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
447 if (v4l2src->probed_caps)
448 return gst_caps_ref (v4l2src->probed_caps);
450 formats = gst_v4l2_object_get_format_list (obj);
452 ret = gst_caps_new_empty ();
454 for (walk = formats; walk; walk = walk->next) {
455 struct v4l2_fmtdesc *format;
456 GstStructure *template;
458 format = (struct v4l2_fmtdesc *) walk->data;
460 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
466 gst_v4l2_object_probe_caps_for_format (obj,
467 format->pixelformat, template);
469 gst_caps_append (ret, tmp);
471 gst_structure_free (template);
473 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
477 v4l2src->probed_caps = gst_caps_ref (ret);
479 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
485 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
490 v4l2src = GST_V4L2SRC (src);
491 obj = v4l2src->v4l2object;
493 /* make sure we stop capturing and dealloc buffers */
494 if (!gst_v4l2_object_stop (obj))
497 if (!gst_v4l2_object_set_format (obj, caps))
498 /* error already posted */
505 gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
510 guint size, min, max, prefix, alignment;
512 src = GST_V4L2SRC (bsrc);
513 obj = src->v4l2object;
515 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
518 GST_DEBUG_OBJECT (src, "allocation: size:%u min:%u max:%u prefix:%u "
519 "align:%u pool:%" GST_PTR_FORMAT, size, min, max, prefix, alignment,
523 /* if there is a min-buffers suggestion, use it. We add 1 because we need 1
524 * buffer extra to capture while the other two buffers are downstream */
534 /* no downstream pool, use our own then */
535 GST_DEBUG_OBJECT (src,
536 "read/write mode: no downstream pool, using our own");
537 pool = GST_BUFFER_POOL_CAST (obj->pool);
538 size = obj->sizeimage;
540 /* in READ/WRITE mode, prefer a downstream pool because our own pool
541 * doesn't help much, we have to write to it as well */
542 GST_DEBUG_OBJECT (src, "read/write mode: using downstream pool");
543 /* use the bigest size, when we use our own pool we can't really do any
544 * other size than what the hardware gives us but for downstream pools
546 size = MAX (size, obj->sizeimage);
549 case GST_V4L2_IO_MMAP:
550 case GST_V4L2_IO_USERPTR:
551 /* in streaming mode, prefer our own pool */
552 pool = GST_BUFFER_POOL_CAST (obj->pool);
553 size = obj->sizeimage;
554 GST_DEBUG_OBJECT (src,
555 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
557 case GST_V4L2_IO_AUTO:
559 GST_WARNING_OBJECT (src, "unhandled mode");
564 GstStructure *config;
567 config = gst_buffer_pool_get_config (pool);
568 gst_buffer_pool_config_get (config, &caps, NULL, NULL, NULL, NULL, NULL);
569 gst_buffer_pool_config_set (config, caps, size, min, max, prefix,
572 /* if downstream supports video metadata, add this to the pool config */
573 if (gst_query_has_allocation_meta (query, GST_VIDEO_META_API))
574 gst_buffer_pool_config_add_option (config,
575 GST_BUFFER_POOL_OPTION_VIDEO_META);
577 gst_buffer_pool_set_config (pool, config);
580 gst_query_set_allocation_params (query, size, min, max, prefix,
587 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
591 gboolean res = FALSE;
593 src = GST_V4L2SRC (bsrc);
594 obj = src->v4l2object;
596 switch (GST_QUERY_TYPE (query)) {
597 case GST_QUERY_LATENCY:{
598 GstClockTime min_latency, max_latency;
599 guint32 fps_n, fps_d;
601 /* device must be open */
602 if (!GST_V4L2_IS_OPEN (obj)) {
603 GST_WARNING_OBJECT (src,
604 "Can't give latency since device isn't open !");
608 fps_n = GST_V4L2_FPS_N (obj);
609 fps_d = GST_V4L2_FPS_D (obj);
611 /* we must have a framerate */
612 if (fps_n <= 0 || fps_d <= 0) {
613 GST_WARNING_OBJECT (src,
614 "Can't give latency since framerate isn't fixated !");
618 /* min latency is the time to capture one frame */
619 min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
621 /* max latency is total duration of the frame buffer */
623 GST_V4L2_BUFFER_POOL_CAST (obj->pool)->max_buffers * min_latency;
625 GST_DEBUG_OBJECT (bsrc,
626 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
627 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
629 /* we are always live, the min latency is 1 frame and the max latency is
630 * the complete buffer of frames. */
631 gst_query_set_latency (query, TRUE, min_latency, max_latency);
637 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
646 /* start and stop are not symmetric -- start will open the device, but not start
647 * capture. it's setcaps that will start capture, which is called via basesrc's
648 * negotiate method. stop will both stop capture and close the device.
651 gst_v4l2src_start (GstBaseSrc * src)
653 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
657 /* activate settings for first frame */
658 v4l2src->ctrl_time = 0;
659 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
665 gst_v4l2src_unlock (GstBaseSrc * src)
667 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
668 return gst_v4l2_object_unlock (v4l2src->v4l2object);
672 gst_v4l2src_unlock_stop (GstBaseSrc * src)
674 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
675 return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
679 gst_v4l2src_stop (GstBaseSrc * src)
681 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
682 GstV4l2Object *obj = v4l2src->v4l2object;
684 if (GST_V4L2_IS_ACTIVE (obj)) {
685 if (!gst_v4l2_object_stop (obj))
691 static GstStateChangeReturn
692 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
694 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
695 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
696 GstV4l2Object *obj = v4l2src->v4l2object;
698 switch (transition) {
699 case GST_STATE_CHANGE_NULL_TO_READY:
700 /* open the device */
701 if (!gst_v4l2_object_open (obj))
702 return GST_STATE_CHANGE_FAILURE;
708 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
710 switch (transition) {
711 case GST_STATE_CHANGE_READY_TO_NULL:
712 /* close the device */
713 if (!gst_v4l2_object_close (obj))
714 return GST_STATE_CHANGE_FAILURE;
716 if (v4l2src->probed_caps) {
717 gst_caps_unref (v4l2src->probed_caps);
718 v4l2src->probed_caps = NULL;
729 gst_v4l2src_fill (GstPushSrc * src, GstBuffer * buf)
731 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
732 GstV4l2Object *obj = v4l2src->v4l2object;
735 GstClockTime timestamp, duration;
739 /* decimate, just capture and throw away frames */
740 for (i = 0; i < v4l2src->decimate - 1; i++) {
741 ret = gst_v4l2_buffer_pool_process (obj, buf);
742 if (ret != GST_FLOW_OK) {
745 gst_buffer_unref (*buf);
750 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL_CAST (obj->pool), buf);
752 if (G_UNLIKELY (ret != GST_FLOW_OK))
755 /* set buffer metadata */
756 GST_BUFFER_OFFSET (buf) = v4l2src->offset++;
757 GST_BUFFER_OFFSET_END (buf) = v4l2src->offset;
759 /* timestamps, LOCK to get clock and base time. */
760 /* FIXME: element clock and base_time is rarely changing */
761 GST_OBJECT_LOCK (v4l2src);
762 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
763 /* we have a clock, get base time and ref clock */
764 timestamp = GST_ELEMENT (v4l2src)->base_time;
765 gst_object_ref (clock);
767 /* no clock, can't set timestamps */
768 timestamp = GST_CLOCK_TIME_NONE;
770 GST_OBJECT_UNLOCK (v4l2src);
772 duration = obj->duration;
774 if (G_LIKELY (clock)) {
775 /* the time now is the time of the clock minus the base time */
776 timestamp = gst_clock_get_time (clock) - timestamp;
777 gst_object_unref (clock);
779 /* if we have a framerate adjust timestamp for frame latency */
780 if (GST_CLOCK_TIME_IS_VALID (duration)) {
781 if (timestamp > duration)
782 timestamp -= duration;
788 /* activate settings for next frame */
789 if (GST_CLOCK_TIME_IS_VALID (duration)) {
790 v4l2src->ctrl_time += duration;
792 /* this is not very good (as it should be the next timestamp),
793 * still good enough for linear fades (as long as it is not -1)
795 v4l2src->ctrl_time = timestamp;
797 gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
798 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
799 GST_TIME_ARGS (v4l2src->ctrl_time));
801 /* FIXME: use the timestamp from the buffer itself! */
802 GST_BUFFER_TIMESTAMP (buf) = timestamp;
803 GST_BUFFER_DURATION (buf) = duration;
810 GST_ERROR_OBJECT (src, "error processing buffer");
816 /* GstURIHandler interface */
818 gst_v4l2src_uri_get_type (GType type)
823 static const gchar *const *
824 gst_v4l2src_uri_get_protocols (GType type)
826 static const gchar *protocols[] = { "v4l2", NULL };
832 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
834 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
836 if (v4l2src->v4l2object->videodev != NULL) {
837 return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
840 return g_strdup ("v4l2://");
844 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
847 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
848 const gchar *device = DEFAULT_PROP_DEVICE;
850 if (strcmp (uri, "v4l2://") != 0) {
853 g_object_set (v4l2src, "device", device, NULL);
860 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
862 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
864 iface->get_type = gst_v4l2src_uri_get_type;
865 iface->get_protocols = gst_v4l2src_uri_get_protocols;
866 iface->get_uri = gst_v4l2src_uri_get_uri;
867 iface->set_uri = gst_v4l2src_uri_set_uri;