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,
93 gst_v4l2src_iface_supported (GstImplementsInterface * iface, GType iface_type)
95 GstV4l2Object *v4l2object = GST_V4L2SRC (iface)->v4l2object;
98 g_assert (iface_type == GST_TYPE_TUNER ||
99 iface_type == GST_TYPE_X_OVERLAY ||
100 iface_type == GST_TYPE_COLOR_BALANCE ||
101 iface_type == GST_TYPE_VIDEO_ORIENTATION);
103 g_assert (iface_type == GST_TYPE_TUNER ||
104 iface_type == GST_TYPE_COLOR_BALANCE ||
105 iface_type == GST_TYPE_VIDEO_ORIENTATION);
108 if (v4l2object->video_fd == -1)
112 if (iface_type == GST_TYPE_X_OVERLAY && !GST_V4L2_IS_OVERLAY (v4l2object))
120 gst_v4l2src_interface_init (GstImplementsInterfaceClass * klass)
123 * default virtual functions
125 klass->supported = gst_v4l2src_iface_supported;
129 gst_v4l2src_init_interfaces (GType type)
131 static const GInterfaceInfo urihandler_info = {
132 gst_v4l2src_uri_handler_init,
137 static const GInterfaceInfo v4l2iface_info = {
138 (GInterfaceInitFunc) gst_v4l2src_interface_init,
142 static const GInterfaceInfo v4l2_tuner_info = {
143 (GInterfaceInitFunc) gst_v4l2src_tuner_interface_init,
148 /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
149 static const GInterfaceInfo v4l2_xoverlay_info = {
150 (GInterfaceInitFunc) gst_v4l2src_xoverlay_interface_init,
155 static const GInterfaceInfo v4l2_colorbalance_info = {
156 (GInterfaceInitFunc) gst_v4l2src_color_balance_interface_init,
160 static const GInterfaceInfo v4l2_videoorientation_info = {
161 (GInterfaceInitFunc) gst_v4l2src_video_orientation_interface_init,
165 static const GInterfaceInfo v4l2_propertyprobe_info = {
166 (GInterfaceInitFunc) gst_v4l2src_property_probe_interface_init,
171 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
172 g_type_add_interface_static (type,
173 GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
174 g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
176 g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
178 g_type_add_interface_static (type,
179 GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
180 g_type_add_interface_static (type,
181 GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
182 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
183 &v4l2_propertyprobe_info);
186 GST_BOILERPLATE_FULL (GstV4l2Src, gst_v4l2src, GstPushSrc, GST_TYPE_PUSH_SRC,
187 gst_v4l2src_init_interfaces);
189 static void gst_v4l2src_dispose (GObject * object);
190 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
192 /* element methods */
193 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
194 GstStateChange transition);
196 /* basesrc methods */
197 static gboolean gst_v4l2src_start (GstBaseSrc * src);
198 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
199 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
200 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
201 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
202 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src);
203 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
204 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
205 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
206 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
208 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
209 const GValue * value, GParamSpec * pspec);
210 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
211 GValue * value, GParamSpec * pspec);
213 /* get_frame io methods */
215 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf);
217 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf);
220 gst_v4l2src_base_init (gpointer g_class)
222 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
223 GstV4l2SrcClass *gstv4l2src_class = GST_V4L2SRC_CLASS (g_class);
224 GstPadTemplate *pad_template;
226 gstv4l2src_class->v4l2_class_devices = NULL;
228 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
230 gst_element_class_set_details_simple (gstelement_class,
231 "Video (video4linux2) Source", "Source/Video",
232 "Reads frames from a Video4Linux2 device",
233 "Edgard Lima <edgard.lima@indt.org.br>,"
234 " Stefan Kost <ensonic@users.sf.net>");
237 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
238 gst_v4l2_object_get_all_caps ());
239 gst_element_class_add_pad_template (gstelement_class, pad_template);
240 gst_object_unref (pad_template);
244 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
246 GObjectClass *gobject_class;
247 GstElementClass *element_class;
248 GstBaseSrcClass *basesrc_class;
249 GstPushSrcClass *pushsrc_class;
251 gobject_class = G_OBJECT_CLASS (klass);
252 element_class = GST_ELEMENT_CLASS (klass);
253 basesrc_class = GST_BASE_SRC_CLASS (klass);
254 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
256 gobject_class->dispose = gst_v4l2src_dispose;
257 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
258 gobject_class->set_property = gst_v4l2src_set_property;
259 gobject_class->get_property = gst_v4l2src_get_property;
261 element_class->change_state = gst_v4l2src_change_state;
263 gst_v4l2_object_install_properties_helper (gobject_class,
264 DEFAULT_PROP_DEVICE);
265 g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
266 g_param_spec_uint ("queue-size", "Queue size",
267 "Number of buffers to be enqueud in the driver in streaming mode",
268 GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
271 g_param_spec_boolean ("always-copy", "Always Copy",
272 "If the buffer will or not be used directly from mmap",
273 PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275 * GstV4l2Src:decimate
277 * Only use every nth frame
281 g_object_class_install_property (gobject_class, PROP_DECIMATE,
282 g_param_spec_int ("decimate", "Decimate",
283 "Only use every nth frame", 1, G_MAXINT,
284 PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
287 basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
288 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
289 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
290 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
291 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
292 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
293 basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
294 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
296 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
300 gst_v4l2src_init (GstV4l2Src * v4l2src, GstV4l2SrcClass * klass)
302 /* fixme: give an update_fps_function */
303 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
304 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
305 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
307 /* number of buffers requested */
308 v4l2src->num_buffers = PROP_DEF_QUEUE_SIZE;
310 v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
311 v4l2src->decimate = PROP_DEF_DECIMATE;
313 v4l2src->is_capturing = FALSE;
315 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
316 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
324 gst_v4l2src_dispose (GObject * object)
326 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
328 if (v4l2src->probed_caps) {
329 gst_caps_unref (v4l2src->probed_caps);
332 G_OBJECT_CLASS (parent_class)->dispose (object);
337 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
339 gst_v4l2_object_destroy (v4l2src->v4l2object);
341 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
346 gst_v4l2src_set_property (GObject * object,
347 guint prop_id, const GValue * value, GParamSpec * pspec)
349 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
351 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
352 prop_id, value, pspec)) {
354 case PROP_QUEUE_SIZE:
355 v4l2src->num_buffers = g_value_get_uint (value);
357 case PROP_ALWAYS_COPY:
358 v4l2src->always_copy = g_value_get_boolean (value);
361 v4l2src->decimate = g_value_get_int (value);
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372 gst_v4l2src_get_property (GObject * object,
373 guint prop_id, GValue * value, GParamSpec * pspec)
375 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
377 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
378 prop_id, value, pspec)) {
380 case PROP_QUEUE_SIZE:
381 g_value_set_uint (value, v4l2src->num_buffers);
383 case PROP_ALWAYS_COPY:
384 g_value_set_boolean (value, v4l2src->always_copy);
387 g_value_set_int (value, v4l2src->decimate);
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 /* this function is a bit of a last resort */
399 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
401 GstStructure *structure;
404 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
406 for (i = 0; i < gst_caps_get_size (caps); ++i) {
409 structure = gst_caps_get_structure (caps, i);
411 /* FIXME such sizes? we usually fixate to something in the 320x200
413 /* We are fixating to greater possble size (limited to GST_V4L2_MAX_SIZE)
414 and the maximum framerate resolution for that size */
415 gst_structure_fixate_field_nearest_int (structure, "width",
417 gst_structure_fixate_field_nearest_int (structure, "height",
419 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
422 v = gst_structure_get_value (structure, "format");
423 if (v && G_VALUE_TYPE (v) != GST_TYPE_FOURCC) {
426 g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
428 fourcc = gst_value_get_fourcc (gst_value_list_get_value (v, 0));
429 gst_structure_set (structure, "format", GST_TYPE_FOURCC, fourcc, NULL);
433 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
438 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
441 GstCaps *caps = NULL;
442 GstCaps *peercaps = NULL;
443 gboolean result = FALSE;
445 /* first see what is possible on our source pad */
446 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
447 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
448 LOG_CAPS (basesrc, thiscaps);
450 /* nothing or anything is allowed, we're done */
451 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
454 /* get the peer caps */
455 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
456 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
457 LOG_CAPS (basesrc, peercaps);
458 if (peercaps && !gst_caps_is_any (peercaps)) {
459 GstCaps *icaps = NULL;
462 /* Prefer the first caps we are compatible with that the peer proposed */
463 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
464 /* get intersection */
465 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
467 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
468 LOG_CAPS (basesrc, ipcaps);
470 icaps = gst_caps_intersect (thiscaps, ipcaps);
471 gst_caps_unref (ipcaps);
473 if (!gst_caps_is_empty (icaps))
476 gst_caps_unref (icaps);
480 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
481 LOG_CAPS (basesrc, icaps);
483 /* If there are multiple intersections pick the one with the smallest
484 * resolution strictly bigger then the first peer caps */
485 if (gst_caps_get_size (icaps) > 1) {
486 GstStructure *s = gst_caps_get_structure (peercaps, 0);
492 int width = G_MAXINT, height = G_MAXINT;
494 if (gst_structure_get_int (s, "width", &twidth)
495 && gst_structure_get_int (s, "height", &theight)) {
497 /* Walk the structure backwards to get the first entry of the
498 * smallest resolution bigger (or equal to) the preferred resolution)
500 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
501 GstStructure *is = gst_caps_get_structure (icaps, i);
505 if (gst_structure_get_int (is, "width", &w)
506 && gst_structure_get_int (is, "height", &h)) {
507 if (w >= twidth && w <= width && h >= theight && h <= height) {
516 caps = gst_caps_copy_nth (icaps, best);
517 gst_caps_unref (icaps);
522 gst_caps_unref (thiscaps);
523 gst_caps_unref (peercaps);
525 /* no peer or peer have ANY caps, work with our own caps then */
529 caps = gst_caps_make_writable (caps);
530 gst_caps_truncate (caps);
533 if (!gst_caps_is_empty (caps)) {
534 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
535 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
536 LOG_CAPS (basesrc, caps);
538 if (gst_caps_is_any (caps)) {
539 /* hmm, still anything, so element can do anything and
540 * nego is not needed */
542 } else if (gst_caps_is_fixed (caps)) {
543 /* yay, fixed caps, use those then */
544 if (gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps))
548 gst_caps_unref (caps);
554 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
556 gst_caps_unref (thiscaps);
562 gst_v4l2src_get_caps (GstBaseSrc * src)
564 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
569 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object)) {
572 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
576 if (v4l2src->probed_caps)
577 return gst_caps_ref (v4l2src->probed_caps);
579 formats = gst_v4l2_object_get_format_list (v4l2src->v4l2object);
581 ret = gst_caps_new_empty ();
583 for (walk = formats; walk; walk = walk->next) {
584 struct v4l2_fmtdesc *format;
586 GstStructure *template;
588 format = (struct v4l2_fmtdesc *) walk->data;
590 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
596 gst_v4l2_object_probe_caps_for_format (v4l2src->v4l2object,
597 format->pixelformat, template);
599 gst_caps_append (ret, tmp);
601 gst_structure_free (template);
603 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
607 v4l2src->probed_caps = gst_caps_ref (ret);
609 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
615 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
620 struct v4l2_fmtdesc *format;
624 v4l2src = GST_V4L2SRC (src);
626 /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
627 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object))
630 /* make sure we stop capturing and dealloc buffers */
631 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)) {
632 /* both will throw an element-error on failure */
633 if (!gst_v4l2src_capture_stop (v4l2src))
635 if (!gst_v4l2src_capture_deinit (v4l2src))
639 /* we want our own v4l2 type of fourcc codes */
640 if (!gst_v4l2_object_get_caps_info (v4l2src->v4l2object, caps, &format, &w,
641 &h, &interlaced, &fps_n, &fps_d, &size)) {
642 GST_INFO_OBJECT (v4l2src,
643 "can't get capture format from caps %" GST_PTR_FORMAT, caps);
647 GST_DEBUG_OBJECT (v4l2src, "trying to set_capture %dx%d at %d/%d fps, "
648 "format %s", w, h, fps_n, fps_d, format->description);
650 if (!gst_v4l2src_set_capture (v4l2src, format->pixelformat, w, h,
651 interlaced, fps_n, fps_d))
652 /* error already posted */
655 if (!gst_v4l2src_capture_init (v4l2src, caps))
658 if (v4l2src->use_mmap) {
659 v4l2src->get_frame = gst_v4l2src_get_mmap;
661 v4l2src->get_frame = gst_v4l2src_get_read;
664 if (!gst_v4l2src_capture_start (v4l2src))
667 /* now store the expected output size */
668 v4l2src->frame_byte_size = size;
674 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
678 gboolean res = FALSE;
680 src = GST_V4L2SRC (bsrc);
682 switch (GST_QUERY_TYPE (query)) {
683 case GST_QUERY_LATENCY:{
684 GstClockTime min_latency, max_latency;
686 /* device must be open */
687 if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
688 GST_WARNING_OBJECT (src,
689 "Can't give latency since device isn't open !");
693 /* we must have a framerate */
694 if (src->fps_n <= 0 || src->fps_d <= 0) {
695 GST_WARNING_OBJECT (src,
696 "Can't give latency since framerate isn't fixated !");
700 /* min latency is the time to capture one frame */
702 gst_util_uint64_scale_int (GST_SECOND, src->fps_d, src->fps_n);
704 /* max latency is total duration of the frame buffer */
705 max_latency = src->num_buffers * min_latency;
707 GST_DEBUG_OBJECT (bsrc,
708 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
709 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
711 /* we are always live, the min latency is 1 frame and the max latency is
712 * the complete buffer of frames. */
713 gst_query_set_latency (query, TRUE, min_latency, max_latency);
719 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
728 /* start and stop are not symmetric -- start will open the device, but not start
729 * capture. it's setcaps that will start capture, which is called via basesrc's
730 * negotiate method. stop will both stop capture and close the device.
733 gst_v4l2src_start (GstBaseSrc * src)
735 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
739 /* activate settings for first frame */
740 v4l2src->ctrl_time = 0;
741 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
747 gst_v4l2src_unlock (GstBaseSrc * src)
749 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
751 GST_LOG_OBJECT (src, "Flushing");
752 gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
758 gst_v4l2src_unlock_stop (GstBaseSrc * src)
760 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
762 GST_LOG_OBJECT (src, "No longer flushing");
763 gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
769 gst_v4l2src_stop (GstBaseSrc * src)
771 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
773 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)
774 && !gst_v4l2src_capture_stop (v4l2src))
777 if (v4l2src->v4l2object->buffer != NULL) {
778 if (!gst_v4l2src_capture_deinit (v4l2src))
788 static GstStateChangeReturn
789 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
791 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
792 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
794 switch (transition) {
795 case GST_STATE_CHANGE_NULL_TO_READY:
796 /* open the device */
797 if (!gst_v4l2_object_start (v4l2src->v4l2object))
798 return GST_STATE_CHANGE_FAILURE;
804 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
806 switch (transition) {
807 case GST_STATE_CHANGE_READY_TO_NULL:
808 /* close the device */
809 if (!gst_v4l2_object_stop (v4l2src->v4l2object))
810 return GST_STATE_CHANGE_FAILURE;
812 if (v4l2src->probed_caps) {
813 gst_caps_unref (v4l2src->probed_caps);
814 v4l2src->probed_caps = NULL;
825 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
832 buffersize = v4l2src->frame_byte_size;
833 /* In case the size per frame is unknown assume it's a streaming format (e.g.
834 * mpegts) and grab a reasonable default size instead */
836 buffersize = GST_BASE_SRC (v4l2src)->blocksize;
838 *buf = gst_buffer_new_and_alloc (buffersize);
841 ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
842 if (G_UNLIKELY (ret < 0)) {
845 if (errno == ENXIO) {
846 GST_DEBUG_OBJECT (v4l2src,
847 "v4l2 device doesn't support polling. Disabling");
848 v4l2src->v4l2object->can_poll_device = FALSE;
850 if (errno != EAGAIN && errno != EINTR)
855 v4l2_read (v4l2src->v4l2object->video_fd, GST_BUFFER_DATA (*buf),
857 if (amount == buffersize) {
859 } else if (amount == -1) {
860 if (errno == EAGAIN || errno == EINTR) {
865 /* short reads can happen if a signal interrupts the read */
870 /* we set the buffer metadata in gst_v4l2src_create() */
877 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
878 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
879 return GST_FLOW_ERROR;
883 GST_DEBUG ("stop called");
884 return GST_FLOW_WRONG_STATE;
888 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
889 (_("Error reading %d bytes from device '%s'."),
890 buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
891 gst_buffer_unref (*buf);
892 return GST_FLOW_ERROR;
897 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
905 ret = gst_v4l2src_grab_frame (v4l2src, &temp);
906 if (G_UNLIKELY (ret != GST_FLOW_OK))
909 if (v4l2src->frame_byte_size > 0) {
910 size = GST_BUFFER_SIZE (temp);
912 /* if size does not match what we expected, try again */
913 if (size != v4l2src->frame_byte_size) {
914 GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
915 (_("Got unexpected frame size of %u instead of %u."),
916 size, v4l2src->frame_byte_size), (NULL));
917 gst_buffer_unref (temp);
932 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
933 (_("Error reading %d bytes on device '%s'."),
934 v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
935 return GST_FLOW_ERROR;
940 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
942 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
946 for (i = 0; i < v4l2src->decimate - 1; i++) {
947 ret = v4l2src->get_frame (v4l2src, buf);
948 if (ret != GST_FLOW_OK) {
951 gst_buffer_unref (*buf);
954 ret = v4l2src->get_frame (v4l2src, buf);
956 /* set buffer metadata */
957 if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
959 GstClockTime timestamp;
961 GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
962 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
964 /* timestamps, LOCK to get clock and base time. */
965 /* FIXME: element clock and base_time is rarely changing */
966 GST_OBJECT_LOCK (v4l2src);
967 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
968 /* we have a clock, get base time and ref clock */
969 timestamp = GST_ELEMENT (v4l2src)->base_time;
970 gst_object_ref (clock);
972 /* no clock, can't set timestamps */
973 timestamp = GST_CLOCK_TIME_NONE;
975 GST_OBJECT_UNLOCK (v4l2src);
977 if (G_LIKELY (clock)) {
978 /* the time now is the time of the clock minus the base time */
979 timestamp = gst_clock_get_time (clock) - timestamp;
980 gst_object_unref (clock);
982 /* if we have a framerate adjust timestamp for frame latency */
983 if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
984 if (timestamp > v4l2src->duration)
985 timestamp -= v4l2src->duration;
991 /* activate settings for next frame */
992 if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
993 v4l2src->ctrl_time += v4l2src->duration;
995 /* this is not very good (as it should be the next timestamp),
996 * still good enough for linear fades (as long as it is not -1)
998 v4l2src->ctrl_time = timestamp;
1000 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
1001 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
1002 GST_TIME_ARGS (v4l2src->ctrl_time));
1004 /* FIXME: use the timestamp from the buffer itself! */
1005 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
1006 GST_BUFFER_DURATION (*buf) = v4l2src->duration;
1012 /* GstURIHandler interface */
1014 gst_v4l2src_uri_get_type (void)
1020 gst_v4l2src_uri_get_protocols (void)
1022 static gchar *protocols[] = { (char *) "v4l2", NULL };
1027 static const gchar *
1028 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
1030 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1032 if (v4l2src->v4l2object->videodev != NULL) {
1035 /* need to return a const string, but also don't want to leak the generated
1036 * string, so just intern it - there's a limited number of video devices
1038 g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
1039 return g_intern_string (uri);
1046 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1048 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1049 const gchar *device = DEFAULT_PROP_DEVICE;
1051 if (strcmp (uri, "v4l2://") != 0) {
1054 g_object_set (v4l2src, "device", device, NULL);
1061 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1063 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1065 iface->get_type = gst_v4l2src_uri_get_type;
1066 iface->get_protocols = gst_v4l2src_uri_get_protocols;
1067 iface->get_uri = gst_v4l2src_uri_get_uri;
1068 iface->set_uri = gst_v4l2src_uri_set_uri;