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"
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
66 #define PROP_DEF_DECIMATE 1
68 #define DEFAULT_PROP_DEVICE "/dev/video0"
73 V4L2_STD_OBJECT_PROPS,
79 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SrcClass, gst_v4l2src);
80 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
81 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
83 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
85 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
87 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
91 gst_v4l2src_iface_supported (GstImplementsInterface * iface, GType iface_type)
93 GstV4l2Object *v4l2object = GST_V4L2SRC (iface)->v4l2object;
96 g_assert (iface_type == GST_TYPE_TUNER ||
97 iface_type == GST_TYPE_X_OVERLAY ||
98 iface_type == GST_TYPE_COLOR_BALANCE ||
99 iface_type == GST_TYPE_VIDEO_ORIENTATION);
101 g_assert (iface_type == GST_TYPE_TUNER ||
102 iface_type == GST_TYPE_COLOR_BALANCE ||
103 iface_type == GST_TYPE_VIDEO_ORIENTATION);
106 if (v4l2object->video_fd == -1)
110 if (iface_type == GST_TYPE_X_OVERLAY && !GST_V4L2_IS_OVERLAY (v4l2object))
118 gst_v4l2src_interface_init (GstImplementsInterfaceClass * klass)
121 * default virtual functions
123 klass->supported = gst_v4l2src_iface_supported;
127 gst_v4l2src_init_interfaces (GType type)
129 static const GInterfaceInfo urihandler_info = {
130 gst_v4l2src_uri_handler_init,
135 static const GInterfaceInfo v4l2iface_info = {
136 (GInterfaceInitFunc) gst_v4l2src_interface_init,
140 static const GInterfaceInfo v4l2_tuner_info = {
141 (GInterfaceInitFunc) gst_v4l2src_tuner_interface_init,
146 static const GInterfaceInfo v4l2_xoverlay_info = {
147 (GInterfaceInitFunc) gst_v4l2src_xoverlay_interface_init,
152 static const GInterfaceInfo v4l2_colorbalance_info = {
153 (GInterfaceInitFunc) gst_v4l2src_color_balance_interface_init,
157 static const GInterfaceInfo v4l2_videoorientation_info = {
158 (GInterfaceInitFunc) gst_v4l2src_video_orientation_interface_init,
162 static const GInterfaceInfo v4l2_propertyprobe_info = {
163 (GInterfaceInitFunc) gst_v4l2src_property_probe_interface_init,
168 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
169 g_type_add_interface_static (type,
170 GST_TYPE_IMPLEMENTS_INTERFACE, &v4l2iface_info);
171 g_type_add_interface_static (type, GST_TYPE_TUNER, &v4l2_tuner_info);
173 g_type_add_interface_static (type, GST_TYPE_X_OVERLAY, &v4l2_xoverlay_info);
175 g_type_add_interface_static (type,
176 GST_TYPE_COLOR_BALANCE, &v4l2_colorbalance_info);
177 g_type_add_interface_static (type,
178 GST_TYPE_VIDEO_ORIENTATION, &v4l2_videoorientation_info);
179 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
180 &v4l2_propertyprobe_info);
183 GST_BOILERPLATE_FULL (GstV4l2Src, gst_v4l2src, GstPushSrc, GST_TYPE_PUSH_SRC,
184 gst_v4l2src_init_interfaces);
186 static void gst_v4l2src_dispose (GObject * object);
187 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
189 /* element methods */
190 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
191 GstStateChange transition);
193 /* basesrc methods */
194 static gboolean gst_v4l2src_start (GstBaseSrc * src);
195 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
196 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
197 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
198 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
199 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src);
200 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
201 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
202 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
203 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
205 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec);
207 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
208 GValue * value, GParamSpec * pspec);
210 /* get_frame io methods */
212 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf);
214 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf);
217 gst_v4l2src_base_init (gpointer g_class)
219 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
220 GstV4l2SrcClass *gstv4l2src_class = GST_V4L2SRC_CLASS (g_class);
222 gstv4l2src_class->v4l2_class_devices = NULL;
224 GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
226 gst_element_class_set_details_simple (gstelement_class,
227 "Video (video4linux2) Source", "Source/Video",
228 "Reads frames from a Video4Linux2 device",
229 "Edgard Lima <edgard.lima@indt.org.br>,"
230 " Stefan Kost <ensonic@users.sf.net>");
232 gst_element_class_add_pad_template
234 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
235 gst_v4l2_object_get_all_caps ()));
239 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
241 GObjectClass *gobject_class;
242 GstElementClass *element_class;
243 GstBaseSrcClass *basesrc_class;
244 GstPushSrcClass *pushsrc_class;
246 gobject_class = G_OBJECT_CLASS (klass);
247 element_class = GST_ELEMENT_CLASS (klass);
248 basesrc_class = GST_BASE_SRC_CLASS (klass);
249 pushsrc_class = GST_PUSH_SRC_CLASS (klass);
251 gobject_class->dispose = gst_v4l2src_dispose;
252 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
253 gobject_class->set_property = gst_v4l2src_set_property;
254 gobject_class->get_property = gst_v4l2src_get_property;
256 element_class->change_state = gst_v4l2src_change_state;
258 gst_v4l2_object_install_properties_helper (gobject_class,
259 DEFAULT_PROP_DEVICE);
260 g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
261 g_param_spec_uint ("queue-size", "Queue size",
262 "Number of buffers to be enqueud in the driver in streaming mode",
263 GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
264 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
266 g_param_spec_boolean ("always-copy", "Always Copy",
267 "If the buffer will or not be used directly from mmap",
268 PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 * GstV4l2Src:decimate
272 * Only use every nth frame
276 g_object_class_install_property (gobject_class, PROP_DECIMATE,
277 g_param_spec_int ("decimate", "Decimate",
278 "Only use every nth frame", 1, G_MAXINT,
279 PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
282 basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
283 basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
284 basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
285 basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
286 basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
287 basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
288 basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
289 basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
291 pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
295 gst_v4l2src_init (GstV4l2Src * v4l2src, GstV4l2SrcClass * klass)
297 /* fixme: give an update_fps_function */
298 v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
299 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
300 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
302 /* number of buffers requested */
303 v4l2src->num_buffers = PROP_DEF_QUEUE_SIZE;
305 v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
306 v4l2src->decimate = PROP_DEF_DECIMATE;
308 v4l2src->is_capturing = FALSE;
310 gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
311 gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
319 gst_v4l2src_dispose (GObject * object)
321 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
323 if (v4l2src->probed_caps) {
324 gst_caps_unref (v4l2src->probed_caps);
327 G_OBJECT_CLASS (parent_class)->dispose (object);
332 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
334 gst_v4l2_object_destroy (v4l2src->v4l2object);
336 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
341 gst_v4l2src_set_property (GObject * object,
342 guint prop_id, const GValue * value, GParamSpec * pspec)
344 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
346 if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
347 prop_id, value, pspec)) {
349 case PROP_QUEUE_SIZE:
350 v4l2src->num_buffers = g_value_get_uint (value);
352 case PROP_ALWAYS_COPY:
353 v4l2src->always_copy = g_value_get_boolean (value);
356 v4l2src->decimate = g_value_get_int (value);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 gst_v4l2src_get_property (GObject * object,
368 guint prop_id, GValue * value, GParamSpec * pspec)
370 GstV4l2Src *v4l2src = GST_V4L2SRC (object);
372 if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
373 prop_id, value, pspec)) {
375 case PROP_QUEUE_SIZE:
376 g_value_set_uint (value, v4l2src->num_buffers);
378 case PROP_ALWAYS_COPY:
379 g_value_set_boolean (value, v4l2src->always_copy);
382 g_value_set_int (value, v4l2src->decimate);
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392 /* this function is a bit of a last resort */
394 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
396 GstStructure *structure;
399 GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
401 for (i = 0; i < gst_caps_get_size (caps); ++i) {
404 structure = gst_caps_get_structure (caps, i);
406 /* FIXME such sizes? we usually fixate to something in the 320x200
408 /* We are fixating to greater possble size (limited to GST_V4L2_MAX_SIZE)
409 and the maximum framerate resolution for that size */
410 gst_structure_fixate_field_nearest_int (structure, "width",
412 gst_structure_fixate_field_nearest_int (structure, "height",
414 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
417 v = gst_structure_get_value (structure, "format");
418 if (v && G_VALUE_TYPE (v) != GST_TYPE_FOURCC) {
421 g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
423 fourcc = gst_value_get_fourcc (gst_value_list_get_value (v, 0));
424 gst_structure_set (structure, "format", GST_TYPE_FOURCC, fourcc, NULL);
428 GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
433 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
436 GstCaps *caps = NULL;
437 GstCaps *peercaps = NULL;
438 gboolean result = FALSE;
440 /* first see what is possible on our source pad */
441 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
442 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
443 LOG_CAPS (basesrc, thiscaps);
445 /* nothing or anything is allowed, we're done */
446 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
449 /* get the peer caps */
450 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
451 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
452 LOG_CAPS (basesrc, peercaps);
453 if (peercaps && !gst_caps_is_any (peercaps)) {
454 GstCaps *icaps = NULL;
457 /* Prefer the first caps we are compatible with that the peer proposed */
458 for (i = 0; i < gst_caps_get_size (peercaps); i++) {
459 /* get intersection */
460 GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
462 GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
463 LOG_CAPS (basesrc, ipcaps);
465 icaps = gst_caps_intersect (thiscaps, ipcaps);
466 gst_caps_unref (ipcaps);
468 if (!gst_caps_is_empty (icaps))
471 gst_caps_unref (icaps);
475 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
476 LOG_CAPS (basesrc, icaps);
478 /* If there are multiple intersections pick the one with the smallest
479 * resolution strictly bigger then the first peer caps */
480 if (gst_caps_get_size (icaps) > 1) {
481 GstStructure *s = gst_caps_get_structure (peercaps, 0);
487 int width = G_MAXINT, height = G_MAXINT;
489 if (gst_structure_get_int (s, "width", &twidth)
490 && gst_structure_get_int (s, "height", &theight)) {
492 /* Walk the structure backwards to get the first entry of the
493 * smallest resolution bigger (or equal to) the preferred resolution)
495 for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
496 GstStructure *is = gst_caps_get_structure (icaps, i);
500 if (gst_structure_get_int (is, "width", &w)
501 && gst_structure_get_int (is, "height", &h)) {
502 if (w >= twidth && w <= width && h >= theight && h <= height) {
511 caps = gst_caps_copy_nth (icaps, best);
512 gst_caps_unref (icaps);
517 gst_caps_unref (thiscaps);
518 gst_caps_unref (peercaps);
520 /* no peer or peer have ANY caps, work with our own caps then */
524 caps = gst_caps_make_writable (caps);
525 gst_caps_truncate (caps);
528 if (!gst_caps_is_empty (caps)) {
529 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
530 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
531 LOG_CAPS (basesrc, caps);
533 if (gst_caps_is_any (caps)) {
534 /* hmm, still anything, so element can do anything and
535 * nego is not needed */
537 } else if (gst_caps_is_fixed (caps)) {
538 /* yay, fixed caps, use those then */
539 if (gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps))
543 gst_caps_unref (caps);
549 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
551 gst_caps_unref (thiscaps);
557 gst_v4l2src_get_caps (GstBaseSrc * src)
559 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
564 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object)) {
567 gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
571 if (v4l2src->probed_caps)
572 return gst_caps_ref (v4l2src->probed_caps);
574 formats = gst_v4l2_object_get_format_list (v4l2src->v4l2object);
576 ret = gst_caps_new_empty ();
578 for (walk = v4l2src->v4l2object->formats; walk; walk = walk->next) {
579 struct v4l2_fmtdesc *format;
581 GstStructure *template;
583 format = (struct v4l2_fmtdesc *) walk->data;
585 template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
591 gst_v4l2_object_probe_caps_for_format (v4l2src->v4l2object,
592 format->pixelformat, template);
594 gst_caps_append (ret, tmp);
596 gst_structure_free (template);
598 GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
602 v4l2src->probed_caps = gst_caps_ref (ret);
604 GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
610 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
615 struct v4l2_fmtdesc *format;
619 v4l2src = GST_V4L2SRC (src);
621 /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
622 if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object))
625 /* make sure we stop capturing and dealloc buffers */
626 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)) {
627 /* both will throw an element-error on failure */
628 if (!gst_v4l2src_capture_stop (v4l2src))
630 if (!gst_v4l2src_capture_deinit (v4l2src))
634 /* we want our own v4l2 type of fourcc codes */
635 if (!gst_v4l2_object_get_caps_info (v4l2src->v4l2object, caps, &format, &w,
636 &h, &interlaced, &fps_n, &fps_d, &size)) {
637 GST_INFO_OBJECT (v4l2src,
638 "can't get capture format from caps %" GST_PTR_FORMAT, caps);
642 GST_DEBUG_OBJECT (v4l2src, "trying to set_capture %dx%d at %d/%d fps, "
643 "format %s", w, h, fps_n, fps_d, format->description);
645 if (!gst_v4l2src_set_capture (v4l2src, format->pixelformat, w, h,
646 interlaced, fps_n, fps_d))
647 /* error already posted */
650 if (!gst_v4l2src_capture_init (v4l2src, caps))
653 if (v4l2src->use_mmap) {
654 v4l2src->get_frame = gst_v4l2src_get_mmap;
656 v4l2src->get_frame = gst_v4l2src_get_read;
659 if (!gst_v4l2src_capture_start (v4l2src))
662 /* now store the expected output size */
663 v4l2src->frame_byte_size = size;
669 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
673 gboolean res = FALSE;
675 src = GST_V4L2SRC (bsrc);
677 switch (GST_QUERY_TYPE (query)) {
678 case GST_QUERY_LATENCY:{
679 GstClockTime min_latency, max_latency;
681 /* device must be open */
682 if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
683 GST_WARNING_OBJECT (src,
684 "Can't give latency since device isn't open !");
688 /* we must have a framerate */
689 if (src->fps_n <= 0 || src->fps_d <= 0) {
690 GST_WARNING_OBJECT (src,
691 "Can't give latency since framerate isn't fixated !");
695 /* min latency is the time to capture one frame */
697 gst_util_uint64_scale_int (GST_SECOND, src->fps_d, src->fps_n);
699 /* max latency is total duration of the frame buffer */
700 max_latency = src->num_buffers * min_latency;
702 GST_DEBUG_OBJECT (bsrc,
703 "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
704 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
706 /* we are always live, the min latency is 1 frame and the max latency is
707 * the complete buffer of frames. */
708 gst_query_set_latency (query, TRUE, min_latency, max_latency);
714 res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
723 /* start and stop are not symmetric -- start will open the device, but not start
724 * capture. it's setcaps that will start capture, which is called via basesrc's
725 * negotiate method. stop will both stop capture and close the device.
728 gst_v4l2src_start (GstBaseSrc * src)
730 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
734 /* activate settings for first frame */
735 v4l2src->ctrl_time = 0;
736 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
742 gst_v4l2src_unlock (GstBaseSrc * src)
744 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
746 GST_LOG_OBJECT (src, "Flushing");
747 gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
753 gst_v4l2src_unlock_stop (GstBaseSrc * src)
755 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
757 GST_LOG_OBJECT (src, "No longer flushing");
758 gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
764 gst_v4l2src_stop (GstBaseSrc * src)
766 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
768 if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)
769 && !gst_v4l2src_capture_stop (v4l2src))
772 if (v4l2src->v4l2object->buffer != NULL) {
773 if (!gst_v4l2src_capture_deinit (v4l2src))
783 static GstStateChangeReturn
784 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
786 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
787 GstV4l2Src *v4l2src = GST_V4L2SRC (element);
789 switch (transition) {
790 case GST_STATE_CHANGE_NULL_TO_READY:
791 /* open the device */
792 if (!gst_v4l2_object_start (v4l2src->v4l2object))
793 return GST_STATE_CHANGE_FAILURE;
799 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
801 switch (transition) {
802 case GST_STATE_CHANGE_READY_TO_NULL:
803 /* close the device */
804 if (!gst_v4l2_object_stop (v4l2src->v4l2object))
805 return GST_STATE_CHANGE_FAILURE;
807 if (v4l2src->probed_caps) {
808 gst_caps_unref (v4l2src->probed_caps);
809 v4l2src->probed_caps = NULL;
820 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
827 buffersize = v4l2src->frame_byte_size;
828 /* In case the size per frame is unknown assume it's a streaming format (e.g.
829 * mpegts) and grab a reasonable default size instead */
831 buffersize = GST_BASE_SRC (v4l2src)->blocksize;
833 *buf = gst_buffer_new_and_alloc (buffersize);
836 ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
837 if (G_UNLIKELY (ret < 0)) {
840 if (errno == ENXIO) {
841 GST_DEBUG_OBJECT (v4l2src,
842 "v4l2 device doesn't support polling. Disabling");
843 v4l2src->v4l2object->can_poll_device = FALSE;
845 if (errno != EAGAIN && errno != EINTR)
850 v4l2_read (v4l2src->v4l2object->video_fd, GST_BUFFER_DATA (*buf),
852 if (amount == buffersize) {
854 } else if (amount == -1) {
855 if (errno == EAGAIN || errno == EINTR) {
860 /* short reads can happen if a signal interrupts the read */
865 /* we set the buffer metadata in gst_v4l2src_create() */
872 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
873 ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
874 return GST_FLOW_ERROR;
878 GST_DEBUG ("stop called");
879 return GST_FLOW_WRONG_STATE;
883 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
884 (_("Error reading %d bytes from device '%s'."),
885 buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
886 gst_buffer_unref (*buf);
887 return GST_FLOW_ERROR;
892 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
900 ret = gst_v4l2src_grab_frame (v4l2src, &temp);
901 if (G_UNLIKELY (ret != GST_FLOW_OK))
904 if (v4l2src->frame_byte_size > 0) {
905 size = GST_BUFFER_SIZE (temp);
907 /* if size does not match what we expected, try again */
908 if (size != v4l2src->frame_byte_size) {
909 GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
910 (_("Got unexpected frame size of %u instead of %u."),
911 size, v4l2src->frame_byte_size), (NULL));
912 gst_buffer_unref (temp);
927 GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
928 (_("Error reading %d bytes on device '%s'."),
929 v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
930 return GST_FLOW_ERROR;
935 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
937 GstV4l2Src *v4l2src = GST_V4L2SRC (src);
941 for (i = 0; i < v4l2src->decimate - 1; i++) {
942 ret = v4l2src->get_frame (v4l2src, buf);
943 if (ret != GST_FLOW_OK) {
946 gst_buffer_unref (*buf);
949 ret = v4l2src->get_frame (v4l2src, buf);
951 /* set buffer metadata */
952 if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
954 GstClockTime timestamp;
956 GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
957 GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
959 /* timestamps, LOCK to get clock and base time. */
960 /* FIXME: element clock and base_time is rarely changing */
961 GST_OBJECT_LOCK (v4l2src);
962 if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
963 /* we have a clock, get base time and ref clock */
964 timestamp = GST_ELEMENT (v4l2src)->base_time;
965 gst_object_ref (clock);
967 /* no clock, can't set timestamps */
968 timestamp = GST_CLOCK_TIME_NONE;
970 GST_OBJECT_UNLOCK (v4l2src);
972 if (G_LIKELY (clock)) {
973 /* the time now is the time of the clock minus the base time */
974 timestamp = gst_clock_get_time (clock) - timestamp;
975 gst_object_unref (clock);
977 /* if we have a framerate adjust timestamp for frame latency */
978 if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
979 if (timestamp > v4l2src->duration)
980 timestamp -= v4l2src->duration;
986 /* activate settings for next frame */
987 if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
988 v4l2src->ctrl_time += v4l2src->duration;
990 /* this is not very good (as it should be the next timestamp),
991 * still good enough for linear fades (as long as it is not -1)
993 v4l2src->ctrl_time = timestamp;
995 gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
996 GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
997 GST_TIME_ARGS (v4l2src->ctrl_time));
999 /* FIXME: use the timestamp from the buffer itself! */
1000 GST_BUFFER_TIMESTAMP (*buf) = timestamp;
1001 GST_BUFFER_DURATION (*buf) = v4l2src->duration;
1007 /* GstURIHandler interface */
1009 gst_v4l2src_uri_get_type (void)
1015 gst_v4l2src_uri_get_protocols (void)
1017 static gchar *protocols[] = { (char *) "v4l2", NULL };
1022 static const gchar *
1023 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
1025 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1027 if (v4l2src->v4l2object->videodev != NULL) {
1030 /* need to return a const string, but also don't want to leak the generated
1031 * string, so just intern it - there's a limited number of video devices
1033 g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
1034 return g_intern_string (uri);
1041 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1043 GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
1044 const gchar *device = DEFAULT_PROP_DEVICE;
1046 if (strcmp (uri, "v4l2://") != 0) {
1049 g_object_set (v4l2src, "device", device, NULL);
1056 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1058 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1060 iface->get_type = gst_v4l2src_uri_get_type;
1061 iface->get_protocols = gst_v4l2src_uri_get_protocols;
1062 iface->get_uri = gst_v4l2src_uri_get_uri;
1063 iface->set_uri = gst_v4l2src_uri_set_uri;