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
49 #include "v4l2src_calls.h"
52 #include "gstv4l2colorbalance.h"
53 #include "gstv4l2tuner.h"
54 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
55 #include "gstv4l2xoverlay.h"
57 #include "gstv4l2vidorient.h"
59 #include "gst/gst-i18n-plugin.h"
61 GST_DEBUG_CATEGORY (v4l2src_debug);
62 #define GST_CAT_DEFAULT v4l2src_debug
64 #define PROP_DEF_QUEUE_SIZE 2
65 #define PROP_DEF_ALWAYS_COPY TRUE
67 #define DEFAULT_PROP_DEVICE "/dev/video0"
72 V4L2_STD_OBJECT_PROPS,
77 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SrcClass, gst_v4l2src);
78 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
79 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
80 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
81 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
83 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
85 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
89 gst_v4l2src_iface_supported (GstImplementsInterface * iface, GType iface_type)
91 GstV4l2Object *v4l2object = GST_V4L2SRC (iface)->v4l2object;
93 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
94 g_assert (iface_type == GST_TYPE_TUNER ||
95 iface_type == GST_TYPE_X_OVERLAY ||
96 iface_type == GST_TYPE_COLOR_BALANCE ||
97 iface_type == GST_TYPE_VIDEO_ORIENTATION);
99 g_assert (iface_type == GST_TYPE_TUNER ||
100 iface_type == GST_TYPE_COLOR_BALANCE ||
101 iface_type == GST_TYPE_VIDEO_ORIENTATION);
104 if (v4l2object->video_fd == -1)
107 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
108 if (iface_type == GST_TYPE_X_OVERLAY && !GST_V4L2_IS_OVERLAY (v4l2object))
116 gst_v4l2src_interface_init (GstImplementsInterfaceClass * klass)
119 * default virtual functions
121 klass->supported = gst_v4l2src_iface_supported;
125 gst_v4l2src_init_interfaces (GType type)
127 static const GInterfaceInfo urihandler_info = {
128 gst_v4l2src_uri_handler_init,
133 static const GInterfaceInfo v4l2iface_info = {
134 (GInterfaceInitFunc) gst_v4l2src_interface_init,
138 static const GInterfaceInfo v4l2_tuner_info = {
139 (GInterfaceInitFunc) gst_v4l2src_tuner_interface_init,
143 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
144 static const GInterfaceInfo v4l2_xoverlay_info = {
145 (GInterfaceInitFunc) gst_v4l2src_xoverlay_interface_init,
150 static const GInterfaceInfo v4l2_colorbalance_info = {
151 (GInterfaceInitFunc) gst_v4l2src_color_balance_interface_init,
155 static const GInterfaceInfo v4l2_videoorientation_info = {
156 (GInterfaceInitFunc) gst_v4l2src_video_orientation_interface_init,
160 static const GInterfaceInfo v4l2_propertyprobe_info = {
161 (GInterfaceInitFunc) gst_v4l2src_property_probe_interface_init,
166 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
167 g_type_add_interface_static (type,
168 GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
169 g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
170 #if 0 /* overlay is still not implemented #ifdef HAVE_XVIDEO */
171 g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
173 g_type_add_interface_static (type,
174 GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
175 g_type_add_interface_static (type,
176 GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
177 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
178 &v4l2_propertyprobe_info);
181 GST_BOILERPLATE_FULL (GstV4l2Src, gst_v4l2src, GstPushSrc, GST_TYPE_PUSH_SRC,
182 gst_v4l2src_init_interfaces);
184 static void gst_v4l2src_dispose (GObject * object);
185 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
187 /* element methods */
188 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
189 GstStateChange transition);
191 /* basesrc methods */
192 static gboolean gst_v4l2src_start (GstBaseSrc * src);
193 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
194 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
195 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
196 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
197 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src);
198 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
199 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
200 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
201 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
203 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
204 const GValue * value, GParamSpec * pspec);
205 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
206 GValue * value, GParamSpec * pspec);
208 /* get_frame io methods */
210 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf);
212 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf);
215 gst_v4l2src_base_init (gpointer g_class)
217 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
218 GstV4l2SrcClass *gstv4l2src_class = GST_V4L2SRC_CLASS (g_class);
220 gstv4l2src_class->v4l2_class_devices = NULL;
222 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
224 gst_element_class_set_details_simple (gstelement_class,
225 "Video (video4linux2) Source", "Source/Video",
226 "Reads frames from a video4linux2 (BT8x8) device",
227 "Edgard Lima <edgard.lima@indt.org.br>,"
228 " Stefan Kost <ensonic@users.sf.net>");
230 gst_element_class_add_pad_template
232 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
233 gst_v4l2_object_get_all_caps ()));
237 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
239 GObjectClass *gobject_class;
240 GstElementClass *element_class;
241 GstBaseSrcClass *basesrc_class;
242 GstPushSrcClass *pushsrc_class;
244 gobject_class = G_OBJECT_CLASS (klass);
245 element_class = GST_ELEMENT_CLASS (klass);
246 basesrc_class = GST_BASE_SRC_CLASS (klass);
247 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
249 gobject_class->dispose = gst_v4l2src_dispose;
250 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
251 gobject_class->set_property = gst_v4l2src_set_property;
252 gobject_class->get_property = gst_v4l2src_get_property;
254 element_class->change_state = gst_v4l2src_change_state;
256 gst_v4l2_object_install_properties_helper (gobject_class,
257 DEFAULT_PROP_DEVICE);
258 g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
259 g_param_spec_uint ("queue-size", "Queue size",
260 "Number of buffers to be enqueud in the driver in streaming mode",
261 GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
263 g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
264 g_param_spec_boolean ("always-copy", "Always Copy",
265 "If the buffer will or not be used directly from mmap",
266 PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE));
268 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
269 basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
270 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
271 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
272 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
273 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
274 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
275 basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
276 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
278 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
282 gst_v4l2src_init (GstV4l2Src * v4l2src, GstV4l2SrcClass * klass)
284 /* fixme: give an update_fps_function */
285 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
286 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
287 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
289 /* number of buffers requested */
290 v4l2src->num_buffers = PROP_DEF_QUEUE_SIZE;
292 v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
294 v4l2src->is_capturing = FALSE;
296 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
297 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
305 gst_v4l2src_dispose (GObject * object)
307 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
309 if (v4l2src->probed_caps) {
310 gst_caps_unref (v4l2src->probed_caps);
313 G_OBJECT_CLASS (parent_class)->dispose (object);
318 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
320 gst_v4l2_object_destroy (v4l2src->v4l2object);
322 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
327 gst_v4l2src_set_property (GObject * object,
328 guint prop_id, const GValue * value, GParamSpec * pspec)
330 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
332 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
333 prop_id, value, pspec)) {
335 case PROP_QUEUE_SIZE:
336 v4l2src->num_buffers = g_value_get_uint (value);
338 case PROP_ALWAYS_COPY:
339 v4l2src->always_copy = g_value_get_boolean (value);
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 gst_v4l2src_get_property (GObject * object,
351 guint prop_id, GValue * value, GParamSpec * pspec)
353 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
355 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
356 prop_id, value, pspec)) {
358 case PROP_QUEUE_SIZE:
359 g_value_set_uint (value, v4l2src->num_buffers);
361 case PROP_ALWAYS_COPY:
362 g_value_set_boolean (value, v4l2src->always_copy);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372 /* this function is a bit of a last resort */
374 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
376 GstStructure *structure;
379 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
381 for (i = 0; i < gst_caps_get_size (caps); ++i) {
384 structure = gst_caps_get_structure (caps, i);
386 /* FIXME such sizes? we usually fixate to something in the 320x200
388 /* We are fixating to greater possble size (limited to GST_V4L2_MAX_SIZE)
389 and the maximum framerate resolution for that size */
390 gst_structure_fixate_field_nearest_int (structure, "width",
392 gst_structure_fixate_field_nearest_int (structure, "height",
394 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
397 v = gst_structure_get_value (structure, "format");
398 if (v && G_VALUE_TYPE (v) != GST_TYPE_FOURCC) {
401 g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
403 fourcc = gst_value_get_fourcc (gst_value_list_get_value (v, 0));
404 gst_structure_set (structure, "format", GST_TYPE_FOURCC, fourcc, NULL);
408 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
413 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
416 GstCaps *caps = NULL;
417 GstCaps *peercaps = NULL;
418 gboolean result = FALSE;
420 /* first see what is possible on our source pad */
421 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
422 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
423 LOG_CAPS (basesrc, thiscaps);
425 /* nothing or anything is allowed, we're done */
426 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
429 /* get the peer caps */
430 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
431 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
432 LOG_CAPS (basesrc, peercaps);
433 if (peercaps && !gst_caps_is_any (peercaps)) {
434 GstCaps *icaps = NULL;
437 /* Prefer the first caps we are compatible with that the peer proposed */
438 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
439 /* get intersection */
440 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
442 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
443 LOG_CAPS (basesrc, ipcaps);
445 icaps = gst_caps_intersect (thiscaps, ipcaps);
446 gst_caps_unref (ipcaps);
448 if (!gst_caps_is_empty (icaps))
451 gst_caps_unref (icaps);
455 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
456 LOG_CAPS (basesrc, icaps);
458 /* If there are multiple intersections pick the one with the smallest
459 * resolution strictly bigger then the first peer caps */
460 if (gst_caps_get_size (icaps) > 1) {
461 GstStructure *s = gst_caps_get_structure (peercaps, 0);
467 int width = G_MAXINT, height = G_MAXINT;
469 if (gst_structure_get_int (s, "width", &twidth)
470 && gst_structure_get_int (s, "height", &theight)) {
472 /* Walk the structure backwards to get the first entry of the
473 * smallest resolution bigger (or equal to) the preferred resolution)
475 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
476 GstStructure *is = gst_caps_get_structure (icaps, i);
480 if (gst_structure_get_int (is, "width", &w)
481 && gst_structure_get_int (is, "height", &h)) {
482 if (w >= twidth && w <= width && h >= theight && h <= height) {
491 caps = gst_caps_copy_nth (icaps, best);
492 gst_caps_unref (icaps);
497 gst_caps_unref (thiscaps);
498 gst_caps_unref (peercaps);
500 /* no peer or peer have ANY caps, work with our own caps then */
504 caps = gst_caps_make_writable (caps);
505 gst_caps_truncate (caps);
508 if (!gst_caps_is_empty (caps)) {
509 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
510 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
511 LOG_CAPS (basesrc, caps);
513 if (gst_caps_is_any (caps)) {
514 /* hmm, still anything, so element can do anything and
515 * nego is not needed */
517 } else if (gst_caps_is_fixed (caps)) {
518 /* yay, fixed caps, use those then */
519 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
523 gst_caps_unref (caps);
529 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
531 gst_caps_unref (thiscaps);
537 gst_v4l2src_get_caps (GstBaseSrc * src)
539 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
544 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object)) {
547 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
551 if (v4l2src->probed_caps)
552 return gst_caps_ref (v4l2src->probed_caps);
554 formats = gst_v4l2_object_get_format_list (v4l2src->v4l2object);
556 ret = gst_caps_new_empty ();
558 for (walk = v4l2src->v4l2object->formats; walk; walk = walk->next) {
559 struct v4l2_fmtdesc *format;
561 GstStructure *template;
563 format = (struct v4l2_fmtdesc *) walk->data;
565 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
571 gst_v4l2_object_probe_caps_for_format (v4l2src->v4l2object,
572 format->pixelformat, template);
574 gst_caps_append (ret, tmp);
576 gst_structure_free (template);
578 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
582 v4l2src->probed_caps = gst_caps_ref (ret);
584 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
590 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
594 struct v4l2_fmtdesc *format;
598 v4l2src = GST_V4L2SRC (src);
600 /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
601 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object))
604 /* make sure we stop capturing and dealloc buffers */
605 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)) {
606 /* both will throw an element-error on failure */
607 if (!gst_v4l2src_capture_stop (v4l2src))
609 if (!gst_v4l2src_capture_deinit (v4l2src))
613 /* we want our own v4l2 type of fourcc codes */
614 if (!gst_v4l2_object_get_caps_info (v4l2src->v4l2object, caps, &format, &w,
615 &h, &fps_n, &fps_d, &size)) {
616 GST_DEBUG_OBJECT (v4l2src,
617 "can't get capture format from caps %" GST_PTR_FORMAT, caps);
621 GST_DEBUG_OBJECT (v4l2src, "trying to set_capture %dx%d at %d/%d fps, "
622 "format %s", w, h, fps_n, fps_d, format->description);
624 if (!gst_v4l2src_set_capture (v4l2src, format->pixelformat, w, h, fps_n,
626 /* error already posted */
629 if (!gst_v4l2src_capture_init (v4l2src, caps))
632 if (v4l2src->use_mmap) {
633 v4l2src->get_frame = gst_v4l2src_get_mmap;
635 v4l2src->get_frame = gst_v4l2src_get_read;
638 if (!gst_v4l2src_capture_start (v4l2src))
641 /* now store the expected output size */
642 v4l2src->frame_byte_size = size;
648 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
652 gboolean res = FALSE;
654 src = GST_V4L2SRC (bsrc);
656 switch (GST_QUERY_TYPE (query)) {
657 case GST_QUERY_LATENCY:{
658 GstClockTime min_latency, max_latency;
660 /* device must be open */
661 if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
662 GST_WARNING_OBJECT (src,
663 "Can't give latency since device isn't open !");
667 /* we must have a framerate */
668 if (src->fps_n <= 0 || src->fps_d <= 0) {
669 GST_WARNING_OBJECT (src,
670 "Can't give latency since framerate isn't fixated !");
674 /* min latency is the time to capture one frame */
676 gst_util_uint64_scale_int (GST_SECOND, src->fps_d, src->fps_n);
678 /* max latency is total duration of the frame buffer */
679 max_latency = src->num_buffers * min_latency;
681 GST_DEBUG_OBJECT (bsrc,
682 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
683 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
685 /* we are always live, the min latency is 1 frame and the max latency is
686 * the complete buffer of frames. */
687 gst_query_set_latency (query, TRUE, min_latency, max_latency);
693 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
702 /* start and stop are not symmetric -- start will open the device, but not start
703 * capture. it's setcaps that will start capture, which is called via basesrc's
704 * negotiate method. stop will both stop capture and close the device.
707 gst_v4l2src_start (GstBaseSrc * src)
709 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
717 gst_v4l2src_unlock (GstBaseSrc * src)
719 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
721 GST_LOG_OBJECT (src, "Flushing");
722 gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
728 gst_v4l2src_unlock_stop (GstBaseSrc * src)
730 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
732 GST_LOG_OBJECT (src, "No longer flushing");
733 gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
739 gst_v4l2src_stop (GstBaseSrc * src)
741 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
743 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)
744 && !gst_v4l2src_capture_stop (v4l2src))
747 if (v4l2src->v4l2object->buffer != NULL) {
748 if (!gst_v4l2src_capture_deinit (v4l2src))
758 static GstStateChangeReturn
759 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
761 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
762 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
764 switch (transition) {
765 case GST_STATE_CHANGE_NULL_TO_READY:
766 /* open the device */
767 if (!gst_v4l2_object_start (v4l2src->v4l2object))
768 return GST_STATE_CHANGE_FAILURE;
774 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
776 switch (transition) {
777 case GST_STATE_CHANGE_READY_TO_NULL:
778 /* close the device */
779 if (!gst_v4l2_object_stop (v4l2src->v4l2object))
780 return GST_STATE_CHANGE_FAILURE;
782 if (v4l2src->probed_caps) {
783 gst_caps_unref (v4l2src->probed_caps);
784 v4l2src->probed_caps = NULL;
795 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
802 buffersize = v4l2src->frame_byte_size;
804 *buf = gst_buffer_new_and_alloc (buffersize);
807 ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
808 if (G_UNLIKELY (ret < 0)) {
811 if (errno == ENXIO) {
812 GST_DEBUG_OBJECT (v4l2src,
813 "v4l2 device doesn't support polling. Disabling");
814 v4l2src->v4l2object->can_poll_device = FALSE;
816 if (errno != EAGAIN && errno != EINTR)
821 v4l2_read (v4l2src->v4l2object->video_fd, GST_BUFFER_DATA (*buf),
823 if (amount == buffersize) {
825 } else if (amount == -1) {
826 if (errno == EAGAIN || errno == EINTR) {
831 /* short reads can happen if a signal interrupts the read */
836 /* we set the buffer metadata in gst_v4l2src_create() */
843 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
844 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
845 return GST_FLOW_ERROR;
849 GST_DEBUG ("stop called");
850 return GST_FLOW_WRONG_STATE;
854 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
855 (_("Error reading %d bytes from device '%s'."),
856 buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
857 gst_buffer_unref (*buf);
858 return GST_FLOW_ERROR;
863 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
871 ret = gst_v4l2src_grab_frame (v4l2src, &temp);
872 if (G_UNLIKELY (ret != GST_FLOW_OK))
875 if (v4l2src->frame_byte_size > 0) {
876 size = GST_BUFFER_SIZE (temp);
878 /* if size does not match what we expected, try again */
879 if (size != v4l2src->frame_byte_size) {
880 GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
881 (_("Got unexpected frame size of %u instead of %u."),
882 size, v4l2src->frame_byte_size), (NULL));
883 gst_buffer_unref (temp);
898 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
899 (_("Error reading %d bytes on device '%s'."),
900 v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
901 return GST_FLOW_ERROR;
906 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
908 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
911 ret = v4l2src->get_frame (v4l2src, buf);
912 /* set buffer metadata */
913 if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
915 GstClockTime timestamp;
916 GstClockTime duration = GST_CLOCK_TIME_NONE;
918 GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
919 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
921 /* timestamps, LOCK to get clock and base time. */
922 GST_OBJECT_LOCK (v4l2src);
923 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
924 /* we have a clock, get base time and ref clock */
925 timestamp = GST_ELEMENT (v4l2src)->base_time;
926 gst_object_ref (clock);
928 /* no clock, can't set timestamps */
929 timestamp = GST_CLOCK_TIME_NONE;
931 GST_OBJECT_UNLOCK (v4l2src);
934 /* the time now is the time of the clock minus the base time */
935 timestamp = gst_clock_get_time (clock) - timestamp;
936 gst_object_unref (clock);
938 /* if we have a framerate adjust timestamp for frame latency */
939 if (v4l2src->fps_n > 0 && v4l2src->fps_d > 0) {
940 GstClockTime latency;
942 latency = gst_util_uint64_scale_int (GST_SECOND, v4l2src->fps_d,
945 if (timestamp > latency)
946 timestamp -= latency;
954 /* FIXME: use the timestamp from the buffer itself! */
955 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
956 GST_BUFFER_DURATION (*buf) = duration;
962 /* GstURIHandler interface */
964 gst_v4l2src_uri_get_type (void)
970 gst_v4l2src_uri_get_protocols (void)
972 static gchar *protocols[] = { (char *) "v4l2", NULL };
978 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
980 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
982 if (v4l2src->v4l2object->videodev != NULL) {
985 /* need to return a const string, but also don't want to leak the generated
986 * string, so just intern it - there's a limited number of video devices
988 g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
989 return g_intern_string (uri);
996 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
998 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
999 const gchar *device = DEFAULT_PROP_DEVICE;
1001 if (strcmp (uri, "v4l2://") != 0) {
1004 g_object_set (v4l2src, "device", device, NULL);
1011 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1013 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1015 iface->get_type = gst_v4l2src_uri_get_type;
1016 iface->get_protocols = gst_v4l2src_uri_get_protocols;
1017 iface->get_uri = gst_v4l2src_uri_get_uri;
1018 iface->set_uri = gst_v4l2src_uri_set_uri;