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
51 #include "v4l2src_calls.h"
54 #include "gstv4l2colorbalance.h"
55 #include "gstv4l2tuner.h"
57 #include "gstv4l2xoverlay.h"
59 #include "gstv4l2vidorient.h"
61 #include "gst/gst-i18n-plugin.h"
63 GST_DEBUG_CATEGORY (v4l2src_debug);
64 #define GST_CAT_DEFAULT v4l2src_debug
66 #define PROP_DEF_QUEUE_SIZE 2
67 #define PROP_DEF_ALWAYS_COPY TRUE
68 #define PROP_DEF_DECIMATE 1
70 #define DEFAULT_PROP_DEVICE "/dev/video0"
75 V4L2_STD_OBJECT_PROPS,
81 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SrcClass, gst_v4l2src);
82 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
83 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
85 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
87 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
89 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
92 #define gst_v4l2src_parent_class parent_class
93 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
94 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
95 G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
97 /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
98 G_IMPLEMENT_INTERFACE (GST_TYPE_X_OVERLAY,
99 gst_v4l2src_xoverlay_interface_init);
101 G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
102 gst_v4l2src_color_balance_interface_init);
103 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
104 gst_v4l2src_video_orientation_interface_init);
105 G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
106 gst_v4l2src_property_probe_interface_init));
108 static void gst_v4l2src_dispose (GObject * object);
109 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
111 /* element methods */
112 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
113 GstStateChange transition);
115 /* basesrc methods */
116 static gboolean gst_v4l2src_start (GstBaseSrc * src);
117 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
118 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
119 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
120 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
121 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
122 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
123 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
124 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
125 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
127 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
132 /* get_frame io methods */
134 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf);
136 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf);
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);
160 g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
161 g_param_spec_uint ("queue-size", "Queue size",
162 "Number of buffers to be enqueud in the driver in streaming mode",
163 GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
166 g_param_spec_boolean ("always-copy", "Always Copy",
167 "If the buffer will or not be used directly from mmap",
168 PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 * GstV4l2Src:decimate
172 * Only use every nth frame
176 g_object_class_install_property (gobject_class, PROP_DECIMATE,
177 g_param_spec_int ("decimate", "Decimate",
178 "Only use every nth frame", 1, G_MAXINT,
179 PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 gst_element_class_set_details_simple (element_class,
182 "Video (video4linux2) Source", "Source/Video",
183 "Reads frames from a Video4Linux2 device",
184 "Edgard Lima <edgard.lima@indt.org.br>, "
185 "Stefan Kost <ensonic@users.sf.net>");
187 gst_element_class_add_pad_template
189 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
190 gst_v4l2_object_get_all_caps ()));
192 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
193 basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
194 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
195 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
196 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
197 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
198 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
199 basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
200 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
202 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
204 klass->v4l2_class_devices = NULL;
206 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
210 gst_v4l2src_init (GstV4l2Src * v4l2src)
212 /* fixme: give an update_fps_function */
213 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
214 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
215 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
217 /* number of buffers requested */
218 v4l2src->num_buffers = PROP_DEF_QUEUE_SIZE;
220 v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
221 v4l2src->decimate = PROP_DEF_DECIMATE;
223 v4l2src->is_capturing = FALSE;
225 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
226 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
234 gst_v4l2src_dispose (GObject * object)
236 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
238 if (v4l2src->probed_caps) {
239 gst_caps_unref (v4l2src->probed_caps);
242 G_OBJECT_CLASS (parent_class)->dispose (object);
247 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
249 gst_v4l2_object_destroy (v4l2src->v4l2object);
251 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
256 gst_v4l2src_set_property (GObject * object,
257 guint prop_id, const GValue * value, GParamSpec * pspec)
259 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
261 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
262 prop_id, value, pspec)) {
264 case PROP_QUEUE_SIZE:
265 v4l2src->num_buffers = g_value_get_uint (value);
267 case PROP_ALWAYS_COPY:
268 v4l2src->always_copy = g_value_get_boolean (value);
271 v4l2src->decimate = g_value_get_int (value);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281 gst_v4l2src_get_property (GObject * object,
282 guint prop_id, GValue * value, GParamSpec * pspec)
284 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
286 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
287 prop_id, value, pspec)) {
289 case PROP_QUEUE_SIZE:
290 g_value_set_uint (value, v4l2src->num_buffers);
292 case PROP_ALWAYS_COPY:
293 g_value_set_boolean (value, v4l2src->always_copy);
296 g_value_set_int (value, v4l2src->decimate);
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 /* this function is a bit of a last resort */
307 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
309 GstStructure *structure;
312 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
314 for (i = 0; i < gst_caps_get_size (caps); ++i) {
317 structure = gst_caps_get_structure (caps, i);
319 /* We are fixating to a resonable 320x200 resolution
320 and the maximum framerate resolution for that size */
321 gst_structure_fixate_field_nearest_int (structure, "width", 320);
322 gst_structure_fixate_field_nearest_int (structure, "height", 200);
323 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
326 v = gst_structure_get_value (structure, "format");
327 if (v && G_VALUE_TYPE (v) != G_TYPE_STRING) {
330 g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
332 format = g_value_get_string (gst_value_list_get_value (v, 0));
333 gst_structure_set (structure, "format", G_TYPE_STRING, format, NULL);
337 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
342 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
345 GstCaps *caps = NULL;
346 GstCaps *peercaps = NULL;
347 gboolean result = FALSE;
349 /* first see what is possible on our source pad */
350 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
351 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
352 LOG_CAPS (basesrc, thiscaps);
354 /* nothing or anything is allowed, we're done */
355 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
358 /* get the peer caps */
359 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
360 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
361 LOG_CAPS (basesrc, peercaps);
362 if (peercaps && !gst_caps_is_any (peercaps)) {
363 GstCaps *icaps = NULL;
366 /* Prefer the first caps we are compatible with that the peer proposed */
367 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
368 /* get intersection */
369 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
371 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
372 LOG_CAPS (basesrc, ipcaps);
374 icaps = gst_caps_intersect (thiscaps, ipcaps);
375 gst_caps_unref (ipcaps);
377 if (!gst_caps_is_empty (icaps))
380 gst_caps_unref (icaps);
384 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
385 LOG_CAPS (basesrc, icaps);
387 /* If there are multiple intersections pick the one with the smallest
388 * resolution strictly bigger then the first peer caps */
389 if (gst_caps_get_size (icaps) > 1) {
390 GstStructure *s = gst_caps_get_structure (peercaps, 0);
393 int width = G_MAXINT, height = G_MAXINT;
395 if (gst_structure_get_int (s, "width", &twidth)
396 && gst_structure_get_int (s, "height", &theight)) {
398 /* Walk the structure backwards to get the first entry of the
399 * smallest resolution bigger (or equal to) the preferred resolution)
401 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
402 GstStructure *is = gst_caps_get_structure (icaps, i);
405 if (gst_structure_get_int (is, "width", &w)
406 && gst_structure_get_int (is, "height", &h)) {
407 if (w >= twidth && w <= width && h >= theight && h <= height) {
416 caps = gst_caps_copy_nth (icaps, best);
417 gst_caps_unref (icaps);
422 gst_caps_unref (thiscaps);
423 gst_caps_unref (peercaps);
425 /* no peer or peer have ANY caps, work with our own caps then */
429 caps = gst_caps_make_writable (caps);
430 gst_caps_truncate (caps);
433 if (!gst_caps_is_empty (caps)) {
434 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
435 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
436 LOG_CAPS (basesrc, caps);
438 if (gst_caps_is_any (caps)) {
439 /* hmm, still anything, so element can do anything and
440 * nego is not needed */
442 } else if (gst_caps_is_fixed (caps)) {
443 /* yay, fixed caps, use those then */
444 gst_pad_push_event (GST_BASE_SRC_PAD (basesrc),
445 gst_event_new_caps (caps));
446 result = gst_v4l2src_set_caps (basesrc, caps);
449 gst_caps_unref (caps);
455 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
457 gst_caps_unref (thiscaps);
463 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
465 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
470 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object)) {
473 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
477 if (v4l2src->probed_caps)
478 return gst_caps_ref (v4l2src->probed_caps);
480 formats = gst_v4l2_object_get_format_list (v4l2src->v4l2object);
482 ret = gst_caps_new_empty ();
484 for (walk = formats; walk; walk = walk->next) {
485 struct v4l2_fmtdesc *format;
486 GstStructure *template;
488 format = (struct v4l2_fmtdesc *) walk->data;
490 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
496 gst_v4l2_object_probe_caps_for_format (v4l2src->v4l2object,
497 format->pixelformat, template);
499 gst_caps_append (ret, tmp);
501 gst_structure_free (template);
503 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
507 v4l2src->probed_caps = gst_caps_ref (ret);
509 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
515 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
519 v4l2src = GST_V4L2SRC (src);
521 /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
522 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object))
525 /* make sure we stop capturing and dealloc buffers */
526 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)) {
527 /* both will throw an element-error on failure */
528 if (!gst_v4l2src_capture_stop (v4l2src))
530 if (!gst_v4l2src_capture_deinit (v4l2src))
534 if (!gst_v4l2src_set_capture (v4l2src, caps))
535 /* error already posted */
538 if (!gst_v4l2src_capture_init (v4l2src))
541 if (v4l2src->use_mmap) {
542 v4l2src->get_frame = gst_v4l2src_get_mmap;
544 v4l2src->get_frame = gst_v4l2src_get_read;
547 if (!gst_v4l2src_capture_start (v4l2src))
550 /* now store the expected output size */
551 v4l2src->frame_byte_size = v4l2src->v4l2object->size;
557 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
561 gboolean res = FALSE;
563 src = GST_V4L2SRC (bsrc);
565 switch (GST_QUERY_TYPE (query)) {
566 case GST_QUERY_LATENCY:{
567 GstClockTime min_latency, max_latency;
569 /* device must be open */
570 if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
571 GST_WARNING_OBJECT (src,
572 "Can't give latency since device isn't open !");
576 /* we must have a framerate */
577 if (src->fps_n <= 0 || src->fps_d <= 0) {
578 GST_WARNING_OBJECT (src,
579 "Can't give latency since framerate isn't fixated !");
583 /* min latency is the time to capture one frame */
585 gst_util_uint64_scale_int (GST_SECOND, src->fps_d, src->fps_n);
587 /* max latency is total duration of the frame buffer */
588 max_latency = src->num_buffers * min_latency;
590 GST_DEBUG_OBJECT (bsrc,
591 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
592 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
594 /* we are always live, the min latency is 1 frame and the max latency is
595 * the complete buffer of frames. */
596 gst_query_set_latency (query, TRUE, min_latency, max_latency);
602 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
611 /* start and stop are not symmetric -- start will open the device, but not start
612 * capture. it's setcaps that will start capture, which is called via basesrc's
613 * negotiate method. stop will both stop capture and close the device.
616 gst_v4l2src_start (GstBaseSrc * src)
618 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
622 /* activate settings for first frame */
623 v4l2src->ctrl_time = 0;
624 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
630 gst_v4l2src_unlock (GstBaseSrc * src)
632 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
634 GST_LOG_OBJECT (src, "Flushing");
635 gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
641 gst_v4l2src_unlock_stop (GstBaseSrc * src)
643 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
645 GST_LOG_OBJECT (src, "No longer flushing");
646 gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
652 gst_v4l2src_stop (GstBaseSrc * src)
654 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
656 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)
657 && !gst_v4l2src_capture_stop (v4l2src))
660 if (v4l2src->v4l2object->buffer != NULL) {
661 if (!gst_v4l2src_capture_deinit (v4l2src))
671 static GstStateChangeReturn
672 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
674 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
675 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
677 switch (transition) {
678 case GST_STATE_CHANGE_NULL_TO_READY:
679 /* open the device */
680 if (!gst_v4l2_object_start (v4l2src->v4l2object))
681 return GST_STATE_CHANGE_FAILURE;
687 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
689 switch (transition) {
690 case GST_STATE_CHANGE_READY_TO_NULL:
691 /* close the device */
692 if (!gst_v4l2_object_stop (v4l2src->v4l2object))
693 return GST_STATE_CHANGE_FAILURE;
695 if (v4l2src->probed_caps) {
696 gst_caps_unref (v4l2src->probed_caps);
697 v4l2src->probed_caps = NULL;
708 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
716 buffersize = v4l2src->frame_byte_size;
717 /* In case the size per frame is unknown assume it's a streaming format (e.g.
718 * mpegts) and grab a reasonable default size instead */
720 buffersize = GST_BASE_SRC (v4l2src)->blocksize;
722 *buf = gst_buffer_new_and_alloc (buffersize);
723 data = gst_buffer_map (*buf, NULL, NULL, GST_MAP_WRITE);
726 ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
727 if (G_UNLIKELY (ret < 0)) {
730 if (errno == ENXIO) {
731 GST_DEBUG_OBJECT (v4l2src,
732 "v4l2 device doesn't support polling. Disabling");
733 v4l2src->v4l2object->can_poll_device = FALSE;
735 if (errno != EAGAIN && errno != EINTR)
739 amount = v4l2_read (v4l2src->v4l2object->video_fd, data, buffersize);
741 if (amount == buffersize) {
743 } else if (amount == -1) {
744 if (errno == EAGAIN || errno == EINTR) {
749 /* short reads can happen if a signal interrupts the read */
754 gst_buffer_unmap (*buf, data, amount);
756 /* we set the buffer metadata in gst_v4l2src_create() */
763 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
764 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
765 res = GST_FLOW_ERROR;
770 GST_DEBUG ("stop called");
771 res = GST_FLOW_WRONG_STATE;
776 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
777 (_("Error reading %d bytes from device '%s'."),
778 buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
779 res = GST_FLOW_ERROR;
784 gst_buffer_unmap (*buf, data, 0);
785 gst_buffer_unref (*buf);
791 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
799 ret = gst_v4l2src_grab_frame (v4l2src, &temp);
800 if (G_UNLIKELY (ret != GST_FLOW_OK))
803 if (v4l2src->frame_byte_size > 0) {
804 size = gst_buffer_get_size (temp);
806 /* if size does not match what we expected, try again */
807 if (size != v4l2src->frame_byte_size) {
808 GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
809 (_("Got unexpected frame size of %u instead of %u."),
810 size, v4l2src->frame_byte_size), (NULL));
811 gst_buffer_unref (temp);
826 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
827 (_("Error reading %d bytes on device '%s'."),
828 v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
829 return GST_FLOW_ERROR;
834 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
836 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
840 if (v4l2src->get_frame == NULL)
843 /* decimate, just capture and throw away frames */
844 for (i = 0; i < v4l2src->decimate - 1; i++) {
845 ret = v4l2src->get_frame (v4l2src, buf);
846 if (ret != GST_FLOW_OK) {
849 gst_buffer_unref (*buf);
852 ret = v4l2src->get_frame (v4l2src, buf);
854 /* set buffer metadata */
855 if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
857 GstClockTime timestamp;
859 GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
860 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
862 /* timestamps, LOCK to get clock and base time. */
863 /* FIXME: element clock and base_time is rarely changing */
864 GST_OBJECT_LOCK (v4l2src);
865 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
866 /* we have a clock, get base time and ref clock */
867 timestamp = GST_ELEMENT (v4l2src)->base_time;
868 gst_object_ref (clock);
870 /* no clock, can't set timestamps */
871 timestamp = GST_CLOCK_TIME_NONE;
873 GST_OBJECT_UNLOCK (v4l2src);
875 if (G_LIKELY (clock)) {
876 /* the time now is the time of the clock minus the base time */
877 timestamp = gst_clock_get_time (clock) - timestamp;
878 gst_object_unref (clock);
880 /* if we have a framerate adjust timestamp for frame latency */
881 if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
882 if (timestamp > v4l2src->duration)
883 timestamp -= v4l2src->duration;
889 /* activate settings for next frame */
890 if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
891 v4l2src->ctrl_time += v4l2src->duration;
893 /* this is not very good (as it should be the next timestamp),
894 * still good enough for linear fades (as long as it is not -1)
896 v4l2src->ctrl_time = timestamp;
898 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
899 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
900 GST_TIME_ARGS (v4l2src->ctrl_time));
902 /* FIXME: use the timestamp from the buffer itself! */
903 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
904 GST_BUFFER_DURATION (*buf) = v4l2src->duration;
911 GST_DEBUG_OBJECT (src, "we are not negotiated");
912 return GST_FLOW_NOT_NEGOTIATED;
917 /* GstURIHandler interface */
919 gst_v4l2src_uri_get_type (GType type)
925 gst_v4l2src_uri_get_protocols (GType type)
927 static gchar *protocols[] = { (char *) "v4l2", NULL };
933 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
935 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
937 if (v4l2src->v4l2object->videodev != NULL) {
940 /* need to return a const string, but also don't want to leak the generated
941 * string, so just intern it - there's a limited number of video devices
943 g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
944 return g_intern_string (uri);
951 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
953 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
954 const gchar *device = DEFAULT_PROP_DEVICE;
956 if (strcmp (uri, "v4l2://") != 0) {
959 g_object_set (v4l2src, "device", device, NULL);
966 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
968 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
970 iface->get_type = gst_v4l2src_uri_get_type;
971 iface->get_protocols = gst_v4l2src_uri_get_protocols;
972 iface->get_uri = gst_v4l2src_uri_get_uri;
973 iface->set_uri = gst_v4l2src_uri_set_uri;