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->v4l2object->num_buffers = PROP_DEF_QUEUE_SIZE;
220 v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
221 v4l2src->decimate = PROP_DEF_DECIMATE;
223 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
224 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
228 gst_v4l2src_dispose (GObject * object)
230 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
232 if (v4l2src->probed_caps) {
233 gst_caps_unref (v4l2src->probed_caps);
236 G_OBJECT_CLASS (parent_class)->dispose (object);
241 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
243 gst_v4l2_object_destroy (v4l2src->v4l2object);
245 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
250 gst_v4l2src_set_property (GObject * object,
251 guint prop_id, const GValue * value, GParamSpec * pspec)
253 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
255 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
256 prop_id, value, pspec)) {
258 case PROP_QUEUE_SIZE:
259 v4l2src->v4l2object->num_buffers = g_value_get_uint (value);
261 case PROP_ALWAYS_COPY:
262 v4l2src->always_copy = g_value_get_boolean (value);
265 v4l2src->decimate = g_value_get_int (value);
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275 gst_v4l2src_get_property (GObject * object,
276 guint prop_id, GValue * value, GParamSpec * pspec)
278 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
280 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
281 prop_id, value, pspec)) {
283 case PROP_QUEUE_SIZE:
284 g_value_set_uint (value, v4l2src->v4l2object->num_buffers);
286 case PROP_ALWAYS_COPY:
287 g_value_set_boolean (value, v4l2src->always_copy);
290 g_value_set_int (value, v4l2src->decimate);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 /* this function is a bit of a last resort */
301 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
303 GstStructure *structure;
306 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
308 for (i = 0; i < gst_caps_get_size (caps); ++i) {
311 structure = gst_caps_get_structure (caps, i);
313 /* We are fixating to a resonable 320x200 resolution
314 and the maximum framerate resolution for that size */
315 gst_structure_fixate_field_nearest_int (structure, "width", 320);
316 gst_structure_fixate_field_nearest_int (structure, "height", 200);
317 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
320 v = gst_structure_get_value (structure, "format");
321 if (v && G_VALUE_TYPE (v) != G_TYPE_STRING) {
324 g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
326 format = g_value_get_string (gst_value_list_get_value (v, 0));
327 gst_structure_set (structure, "format", G_TYPE_STRING, format, NULL);
331 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
336 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
339 GstCaps *caps = NULL;
340 GstCaps *peercaps = NULL;
341 gboolean result = FALSE;
343 /* first see what is possible on our source pad */
344 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
345 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
346 LOG_CAPS (basesrc, thiscaps);
348 /* nothing or anything is allowed, we're done */
349 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
352 /* get the peer caps */
353 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
354 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
355 LOG_CAPS (basesrc, peercaps);
356 if (peercaps && !gst_caps_is_any (peercaps)) {
357 GstCaps *icaps = NULL;
360 /* Prefer the first caps we are compatible with that the peer proposed */
361 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
362 /* get intersection */
363 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
365 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
366 LOG_CAPS (basesrc, ipcaps);
368 icaps = gst_caps_intersect (thiscaps, ipcaps);
369 gst_caps_unref (ipcaps);
371 if (!gst_caps_is_empty (icaps))
374 gst_caps_unref (icaps);
378 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
379 LOG_CAPS (basesrc, icaps);
381 /* If there are multiple intersections pick the one with the smallest
382 * resolution strictly bigger then the first peer caps */
383 if (gst_caps_get_size (icaps) > 1) {
384 GstStructure *s = gst_caps_get_structure (peercaps, 0);
387 int width = G_MAXINT, height = G_MAXINT;
389 if (gst_structure_get_int (s, "width", &twidth)
390 && gst_structure_get_int (s, "height", &theight)) {
392 /* Walk the structure backwards to get the first entry of the
393 * smallest resolution bigger (or equal to) the preferred resolution)
395 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
396 GstStructure *is = gst_caps_get_structure (icaps, i);
399 if (gst_structure_get_int (is, "width", &w)
400 && gst_structure_get_int (is, "height", &h)) {
401 if (w >= twidth && w <= width && h >= theight && h <= height) {
410 caps = gst_caps_copy_nth (icaps, best);
411 gst_caps_unref (icaps);
416 gst_caps_unref (thiscaps);
417 gst_caps_unref (peercaps);
419 /* no peer or peer have ANY caps, work with our own caps then */
423 caps = gst_caps_make_writable (caps);
424 gst_caps_truncate (caps);
427 if (!gst_caps_is_empty (caps)) {
428 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
429 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
430 LOG_CAPS (basesrc, caps);
432 if (gst_caps_is_any (caps)) {
433 /* hmm, still anything, so element can do anything and
434 * nego is not needed */
436 } else if (gst_caps_is_fixed (caps)) {
437 /* yay, fixed caps, use those then */
438 gst_pad_push_event (GST_BASE_SRC_PAD (basesrc),
439 gst_event_new_caps (caps));
440 result = gst_v4l2src_set_caps (basesrc, caps);
443 gst_caps_unref (caps);
449 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
451 gst_caps_unref (thiscaps);
457 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
465 v4l2src = GST_V4L2SRC (src);
466 obj = v4l2src->v4l2object;
468 if (!GST_V4L2_IS_OPEN (obj)) {
471 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
475 if (v4l2src->probed_caps)
476 return gst_caps_ref (v4l2src->probed_caps);
478 formats = gst_v4l2_object_get_format_list (obj);
480 ret = gst_caps_new_empty ();
482 for (walk = formats; walk; walk = walk->next) {
483 struct v4l2_fmtdesc *format;
484 GstStructure *template;
486 format = (struct v4l2_fmtdesc *) walk->data;
488 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
494 gst_v4l2_object_probe_caps_for_format (obj,
495 format->pixelformat, template);
497 gst_caps_append (ret, tmp);
499 gst_structure_free (template);
501 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
505 v4l2src->probed_caps = gst_caps_ref (ret);
507 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
513 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
518 v4l2src = GST_V4L2SRC (src);
519 obj = v4l2src->v4l2object;
521 /* make sure we stop capturing and dealloc buffers */
522 if (!gst_v4l2_object_stop (obj))
525 if (!gst_v4l2_object_set_format (obj, caps))
526 /* error already posted */
530 v4l2src->get_frame = gst_v4l2src_get_mmap;
532 v4l2src->get_frame = gst_v4l2src_get_read;
535 if (!gst_v4l2_object_start (obj))
538 /* now store the expected output size */
539 v4l2src->frame_byte_size = obj->size;
545 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
549 gboolean res = FALSE;
551 src = GST_V4L2SRC (bsrc);
553 switch (GST_QUERY_TYPE (query)) {
554 case GST_QUERY_LATENCY:{
555 GstClockTime min_latency, max_latency;
556 guint32 fps_n, fps_d;
558 /* device must be open */
559 if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
560 GST_WARNING_OBJECT (src,
561 "Can't give latency since device isn't open !");
565 fps_n = GST_V4L2_FPS_N (src->v4l2object);
566 fps_d = GST_V4L2_FPS_D (src->v4l2object);
568 /* we must have a framerate */
569 if (fps_n <= 0 || fps_d <= 0) {
570 GST_WARNING_OBJECT (src,
571 "Can't give latency since framerate isn't fixated !");
575 /* min latency is the time to capture one frame */
576 min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
578 /* max latency is total duration of the frame buffer */
579 max_latency = src->v4l2object->num_buffers * min_latency;
581 GST_DEBUG_OBJECT (bsrc,
582 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
583 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
585 /* we are always live, the min latency is 1 frame and the max latency is
586 * the complete buffer of frames. */
587 gst_query_set_latency (query, TRUE, min_latency, max_latency);
593 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
602 /* start and stop are not symmetric -- start will open the device, but not start
603 * capture. it's setcaps that will start capture, which is called via basesrc's
604 * negotiate method. stop will both stop capture and close the device.
607 gst_v4l2src_start (GstBaseSrc * src)
609 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
613 /* activate settings for first frame */
614 v4l2src->ctrl_time = 0;
615 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
621 gst_v4l2src_unlock (GstBaseSrc * src)
623 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
625 GST_LOG_OBJECT (src, "Flushing");
626 gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
632 gst_v4l2src_unlock_stop (GstBaseSrc * src)
634 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
636 GST_LOG_OBJECT (src, "No longer flushing");
637 gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
643 gst_v4l2src_stop (GstBaseSrc * src)
645 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
646 GstV4l2Object *obj = v4l2src->v4l2object;
648 if (GST_V4L2_IS_ACTIVE (obj)) {
649 if (!gst_v4l2_object_stop (obj))
655 static GstStateChangeReturn
656 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
658 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
659 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
660 GstV4l2Object *obj = v4l2src->v4l2object;
662 switch (transition) {
663 case GST_STATE_CHANGE_NULL_TO_READY:
664 /* open the device */
665 if (!gst_v4l2_object_open (obj))
666 return GST_STATE_CHANGE_FAILURE;
672 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
674 switch (transition) {
675 case GST_STATE_CHANGE_READY_TO_NULL:
676 /* close the device */
677 if (!gst_v4l2_object_close (obj))
678 return GST_STATE_CHANGE_FAILURE;
680 if (v4l2src->probed_caps) {
681 gst_caps_unref (v4l2src->probed_caps);
682 v4l2src->probed_caps = NULL;
693 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
701 buffersize = v4l2src->frame_byte_size;
702 /* In case the size per frame is unknown assume it's a streaming format (e.g.
703 * mpegts) and grab a reasonable default size instead */
705 buffersize = GST_BASE_SRC (v4l2src)->blocksize;
707 *buf = gst_buffer_new_and_alloc (buffersize);
708 data = gst_buffer_map (*buf, NULL, NULL, GST_MAP_WRITE);
711 ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
712 if (G_UNLIKELY (ret < 0)) {
715 if (errno == ENXIO) {
716 GST_DEBUG_OBJECT (v4l2src,
717 "v4l2 device doesn't support polling. Disabling");
718 v4l2src->v4l2object->can_poll_device = FALSE;
720 if (errno != EAGAIN && errno != EINTR)
724 amount = v4l2_read (v4l2src->v4l2object->video_fd, data, buffersize);
726 if (amount == buffersize) {
728 } else if (amount == -1) {
729 if (errno == EAGAIN || errno == EINTR) {
734 /* short reads can happen if a signal interrupts the read */
739 gst_buffer_unmap (*buf, data, amount);
741 /* we set the buffer metadata in gst_v4l2src_create() */
748 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
749 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
750 res = GST_FLOW_ERROR;
755 GST_DEBUG ("stop called");
756 res = GST_FLOW_WRONG_STATE;
761 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
762 (_("Error reading %d bytes from device '%s'."),
763 buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
764 res = GST_FLOW_ERROR;
769 gst_buffer_unmap (*buf, data, 0);
770 gst_buffer_unref (*buf);
776 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
784 ret = gst_v4l2src_grab_frame (v4l2src, &temp);
785 if (G_UNLIKELY (ret != GST_FLOW_OK))
788 if (v4l2src->frame_byte_size > 0) {
789 size = gst_buffer_get_size (temp);
791 /* if size does not match what we expected, try again */
792 if (size != v4l2src->frame_byte_size) {
793 GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
794 (_("Got unexpected frame size of %u instead of %u."),
795 size, v4l2src->frame_byte_size), (NULL));
796 gst_buffer_unref (temp);
811 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
812 (_("Error reading %d bytes on device '%s'."),
813 v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
814 return GST_FLOW_ERROR;
819 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
821 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
825 if (v4l2src->get_frame == NULL)
828 /* decimate, just capture and throw away frames */
829 for (i = 0; i < v4l2src->decimate - 1; i++) {
830 ret = v4l2src->get_frame (v4l2src, buf);
831 if (ret != GST_FLOW_OK) {
834 gst_buffer_unref (*buf);
837 ret = v4l2src->get_frame (v4l2src, buf);
839 /* set buffer metadata */
840 if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
842 GstClockTime timestamp, duration;
844 GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
845 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
847 /* timestamps, LOCK to get clock and base time. */
848 /* FIXME: element clock and base_time is rarely changing */
849 GST_OBJECT_LOCK (v4l2src);
850 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
851 /* we have a clock, get base time and ref clock */
852 timestamp = GST_ELEMENT (v4l2src)->base_time;
853 gst_object_ref (clock);
855 /* no clock, can't set timestamps */
856 timestamp = GST_CLOCK_TIME_NONE;
858 GST_OBJECT_UNLOCK (v4l2src);
860 duration = v4l2src->v4l2object->duration;
862 if (G_LIKELY (clock)) {
863 /* the time now is the time of the clock minus the base time */
864 timestamp = gst_clock_get_time (clock) - timestamp;
865 gst_object_unref (clock);
867 /* if we have a framerate adjust timestamp for frame latency */
868 if (GST_CLOCK_TIME_IS_VALID (duration)) {
869 if (timestamp > duration)
870 timestamp -= duration;
876 /* activate settings for next frame */
877 if (GST_CLOCK_TIME_IS_VALID (duration)) {
878 v4l2src->ctrl_time += duration;
880 /* this is not very good (as it should be the next timestamp),
881 * still good enough for linear fades (as long as it is not -1)
883 v4l2src->ctrl_time = timestamp;
885 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
886 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
887 GST_TIME_ARGS (v4l2src->ctrl_time));
889 /* FIXME: use the timestamp from the buffer itself! */
890 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
891 GST_BUFFER_DURATION (*buf) = duration;
898 GST_DEBUG_OBJECT (src, "we are not negotiated");
899 return GST_FLOW_NOT_NEGOTIATED;
904 /* GstURIHandler interface */
906 gst_v4l2src_uri_get_type (GType type)
912 gst_v4l2src_uri_get_protocols (GType type)
914 static gchar *protocols[] = { (char *) "v4l2", NULL };
920 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
922 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
924 if (v4l2src->v4l2object->videodev != NULL) {
927 /* need to return a const string, but also don't want to leak the generated
928 * string, so just intern it - there's a limited number of video devices
930 g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
931 return g_intern_string (uri);
938 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
940 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
941 const gchar *device = DEFAULT_PROP_DEVICE;
943 if (strcmp (uri, "v4l2://") != 0) {
946 g_object_set (v4l2src, "device", device, NULL);
953 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
955 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
957 iface->get_type = gst_v4l2src_uri_get_type;
958 iface->get_protocols = gst_v4l2src_uri_get_protocols;
959 iface->get_uri = gst_v4l2src_uri_get_uri;
960 iface->set_uri = gst_v4l2src_uri_set_uri;