Merge commit 'f9207722ca8fd8dcc1e7215d8af85efe4debfdf4' into 0.11
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2src.c
1 /* GStreamer
2  *
3  * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4  *               2006 Edgard Lima <edgard.lima@indt.org.br>
5  *
6  * gstv4l2src.c: Video4Linux2 source element
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:element-v4l2src
26  *
27  * v4l2src can be used to capture video from v4l2 devices, like webcams and tv
28  * cards.
29  *
30  * <refsect2>
31  * <title>Example launch lines</title>
32  * |[
33  * gst-launch v4l2src ! xvimagesink
34  * ]| This pipeline shows the video captured from /dev/video0 tv card and for
35  * webcams.
36  * |[
37  * gst-launch v4l2src ! jpegdec ! xvimagesink
38  * ]| This pipeline shows the video captured from a webcam that delivers jpeg
39  * images.
40  * </refsect2>
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #undef HAVE_XVIDEO
48
49 #include <string.h>
50 #include <sys/time.h>
51 #include <unistd.h>
52
53 #include "gst/video/gstvideometa.h"
54 #include "gst/video/gstvideopool.h"
55
56 #include "gstv4l2src.h"
57
58 #include "gstv4l2colorbalance.h"
59 #include "gstv4l2tuner.h"
60 #ifdef HAVE_XVIDEO
61 #include "gstv4l2xoverlay.h"
62 #endif
63 #include "gstv4l2vidorient.h"
64
65 #include "gst/gst-i18n-plugin.h"
66
67 GST_DEBUG_CATEGORY (v4l2src_debug);
68 #define GST_CAT_DEFAULT v4l2src_debug
69
70 #define PROP_DEF_ALWAYS_COPY        TRUE
71 #define PROP_DEF_DECIMATE           1
72
73 #define DEFAULT_PROP_DEVICE   "/dev/video0"
74
75 enum
76 {
77   PROP_0,
78   V4L2_STD_OBJECT_PROPS,
79   PROP_ALWAYS_COPY,
80   PROP_DECIMATE
81 };
82
83 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
84 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
85 #ifdef HAVE_XVIDEO
86 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
87 #endif
88 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
89
90 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
91     gpointer iface_data);
92
93 #define gst_v4l2src_parent_class parent_class
94 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
95     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
96     G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
97 #ifdef HAVE_XVIDEO
98     /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
99     G_IMPLEMENT_INTERFACE (GST_TYPE_X_OVERLAY,
100         gst_v4l2src_xoverlay_interface_init);
101 #endif
102     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
103         gst_v4l2src_color_balance_interface_init);
104     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
105         gst_v4l2src_video_orientation_interface_init));
106
107 static void gst_v4l2src_dispose (GObject * object);
108 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
109
110 /* element methods */
111 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
112     GstStateChange transition);
113
114 /* basesrc methods */
115 static gboolean gst_v4l2src_start (GstBaseSrc * src);
116 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
117 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
118 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
119 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
120 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
121 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
122 static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
123     GstQuery * query);
124 static GstFlowReturn gst_v4l2src_fill (GstPushSrc * src, GstBuffer * out);
125 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
126 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
127
128 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
129     const GValue * value, GParamSpec * pspec);
130 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
131     GValue * value, GParamSpec * pspec);
132
133 static void
134 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
135 {
136   GObjectClass *gobject_class;
137   GstElementClass *element_class;
138   GstBaseSrcClass *basesrc_class;
139   GstPushSrcClass *pushsrc_class;
140
141   gobject_class = G_OBJECT_CLASS (klass);
142   element_class = GST_ELEMENT_CLASS (klass);
143   basesrc_class = GST_BASE_SRC_CLASS (klass);
144   pushsrc_class = GST_PUSH_SRC_CLASS (klass);
145
146   gobject_class->dispose = gst_v4l2src_dispose;
147   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
148   gobject_class->set_property = gst_v4l2src_set_property;
149   gobject_class->get_property = gst_v4l2src_get_property;
150
151   element_class->change_state = gst_v4l2src_change_state;
152
153   gst_v4l2_object_install_properties_helper (gobject_class,
154       DEFAULT_PROP_DEVICE);
155   g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
156       g_param_spec_boolean ("always-copy", "Always Copy",
157           "If the buffer will or not be used directly from mmap",
158           PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159   /**
160    * GstV4l2Src:decimate
161    *
162    * Only use every nth frame
163    *
164    * Since: 0.10.26
165    */
166   g_object_class_install_property (gobject_class, PROP_DECIMATE,
167       g_param_spec_int ("decimate", "Decimate",
168           "Only use every nth frame", 1, G_MAXINT,
169           PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170
171   gst_element_class_set_details_simple (element_class,
172       "Video (video4linux2) Source", "Source/Video",
173       "Reads frames from a Video4Linux2 device",
174       "Edgard Lima <edgard.lima@indt.org.br>, "
175       "Stefan Kost <ensonic@users.sf.net>");
176
177   gst_element_class_add_pad_template
178       (element_class,
179       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
180           gst_v4l2_object_get_all_caps ()));
181
182   basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
183   basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
184   basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
185   basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
186   basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
187   basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
188   basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
189   basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
190   basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
191   basesrc_class->decide_allocation =
192       GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
193
194   pushsrc_class->fill = GST_DEBUG_FUNCPTR (gst_v4l2src_fill);
195
196   klass->v4l2_class_devices = NULL;
197
198   GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
199 }
200
201 static void
202 gst_v4l2src_init (GstV4l2Src * v4l2src)
203 {
204   /* fixme: give an update_fps_function */
205   v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
206       V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
207       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
208
209   v4l2src->v4l2object->always_copy = PROP_DEF_ALWAYS_COPY;
210   v4l2src->decimate = PROP_DEF_DECIMATE;
211
212   gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
213   gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
214 }
215
216 static void
217 gst_v4l2src_dispose (GObject * object)
218 {
219   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
220
221   if (v4l2src->probed_caps) {
222     gst_caps_unref (v4l2src->probed_caps);
223   }
224
225   G_OBJECT_CLASS (parent_class)->dispose (object);
226 }
227
228
229 static void
230 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
231 {
232   gst_v4l2_object_destroy (v4l2src->v4l2object);
233
234   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
235 }
236
237
238 static void
239 gst_v4l2src_set_property (GObject * object,
240     guint prop_id, const GValue * value, GParamSpec * pspec)
241 {
242   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
243
244   if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
245           prop_id, value, pspec)) {
246     switch (prop_id) {
247       case PROP_ALWAYS_COPY:
248         v4l2src->v4l2object->always_copy = g_value_get_boolean (value);
249         break;
250       case PROP_DECIMATE:
251         v4l2src->decimate = g_value_get_int (value);
252         break;
253       default:
254         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255         break;
256     }
257   }
258 }
259
260 static void
261 gst_v4l2src_get_property (GObject * object,
262     guint prop_id, GValue * value, GParamSpec * pspec)
263 {
264   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
265
266   if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
267           prop_id, value, pspec)) {
268     switch (prop_id) {
269       case PROP_ALWAYS_COPY:
270         g_value_set_boolean (value, v4l2src->v4l2object->always_copy);
271         break;
272       case PROP_DECIMATE:
273         g_value_set_int (value, v4l2src->decimate);
274         break;
275       default:
276         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277         break;
278     }
279   }
280 }
281
282 /* this function is a bit of a last resort */
283 static void
284 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
285 {
286   GstStructure *structure;
287   gint i;
288
289   GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
290
291   for (i = 0; i < gst_caps_get_size (caps); ++i) {
292     structure = gst_caps_get_structure (caps, i);
293
294     /* We are fixating to a resonable 320x200 resolution
295        and the maximum framerate resolution for that size */
296     gst_structure_fixate_field_nearest_int (structure, "width", 320);
297     gst_structure_fixate_field_nearest_int (structure, "height", 200);
298     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
299         G_MAXINT, 1);
300     gst_structure_fixate_field (structure, "format");
301   }
302
303   GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
304
305   GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
306 }
307
308
309 static gboolean
310 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
311 {
312   GstCaps *thiscaps;
313   GstCaps *caps = NULL;
314   GstCaps *peercaps = NULL;
315   gboolean result = FALSE;
316
317   /* first see what is possible on our source pad */
318   thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
319   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
320   LOG_CAPS (basesrc, thiscaps);
321
322   /* nothing or anything is allowed, we're done */
323   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
324     goto no_nego_needed;
325
326   /* get the peer caps */
327   peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
328   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
329   LOG_CAPS (basesrc, peercaps);
330   if (peercaps && !gst_caps_is_any (peercaps)) {
331     GstCaps *icaps = NULL;
332     int i;
333
334     /* Prefer the first caps we are compatible with that the peer proposed */
335     for (i = 0; i < gst_caps_get_size (peercaps); i++) {
336       /* get intersection */
337       GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
338
339       GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
340       LOG_CAPS (basesrc, ipcaps);
341
342       icaps = gst_caps_intersect (thiscaps, ipcaps);
343       gst_caps_unref (ipcaps);
344
345       if (!gst_caps_is_empty (icaps))
346         break;
347
348       gst_caps_unref (icaps);
349       icaps = NULL;
350     }
351
352     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
353     LOG_CAPS (basesrc, icaps);
354     if (icaps) {
355       /* If there are multiple intersections pick the one with the smallest
356        * resolution strictly bigger then the first peer caps */
357       if (gst_caps_get_size (icaps) > 1) {
358         GstStructure *s = gst_caps_get_structure (peercaps, 0);
359         int best = 0;
360         int twidth, theight;
361         int width = G_MAXINT, height = G_MAXINT;
362
363         if (gst_structure_get_int (s, "width", &twidth)
364             && gst_structure_get_int (s, "height", &theight)) {
365
366           /* Walk the structure backwards to get the first entry of the
367            * smallest resolution bigger (or equal to) the preferred resolution)
368            */
369           for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
370             GstStructure *is = gst_caps_get_structure (icaps, i);
371             int w, h;
372
373             if (gst_structure_get_int (is, "width", &w)
374                 && gst_structure_get_int (is, "height", &h)) {
375               if (w >= twidth && w <= width && h >= theight && h <= height) {
376                 width = w;
377                 height = h;
378                 best = i;
379               }
380             }
381           }
382         }
383
384         caps = gst_caps_copy_nth (icaps, best);
385         gst_caps_unref (icaps);
386       } else {
387         caps = icaps;
388       }
389     }
390     gst_caps_unref (thiscaps);
391   } else {
392     /* no peer or peer have ANY caps, work with our own caps then */
393     caps = thiscaps;
394   }
395   if (peercaps)
396     gst_caps_unref (peercaps);
397   if (caps) {
398     caps = gst_caps_make_writable (caps);
399     gst_caps_truncate (caps);
400
401     /* now fixate */
402     if (!gst_caps_is_empty (caps)) {
403       gst_v4l2src_fixate (basesrc, caps);
404       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
405       LOG_CAPS (basesrc, caps);
406
407       if (gst_caps_is_any (caps)) {
408         /* hmm, still anything, so element can do anything and
409          * nego is not needed */
410         result = TRUE;
411       } else if (gst_caps_is_fixed (caps)) {
412         /* yay, fixed caps, use those then */
413         result = gst_base_src_set_caps (basesrc, caps);
414       }
415     }
416     gst_caps_unref (caps);
417   }
418   return result;
419
420 no_nego_needed:
421   {
422     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
423     if (thiscaps)
424       gst_caps_unref (thiscaps);
425     return TRUE;
426   }
427 }
428
429 static GstCaps *
430 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
431 {
432   GstV4l2Src *v4l2src;
433   GstV4l2Object *obj;
434   GstCaps *ret;
435   GSList *walk;
436   GSList *formats;
437
438   v4l2src = GST_V4L2SRC (src);
439   obj = v4l2src->v4l2object;
440
441   if (!GST_V4L2_IS_OPEN (obj)) {
442     /* FIXME: copy? */
443     return
444         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
445             (v4l2src)));
446   }
447
448   if (v4l2src->probed_caps)
449     return gst_caps_ref (v4l2src->probed_caps);
450
451   formats = gst_v4l2_object_get_format_list (obj);
452
453   ret = gst_caps_new_empty ();
454
455   for (walk = formats; walk; walk = walk->next) {
456     struct v4l2_fmtdesc *format;
457     GstStructure *template;
458
459     format = (struct v4l2_fmtdesc *) walk->data;
460
461     template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
462
463     if (template) {
464       GstCaps *tmp;
465
466       tmp =
467           gst_v4l2_object_probe_caps_for_format (obj,
468           format->pixelformat, template);
469       if (tmp)
470         gst_caps_append (ret, tmp);
471
472       gst_structure_free (template);
473     } else {
474       GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
475     }
476   }
477
478   v4l2src->probed_caps = gst_caps_ref (ret);
479
480   GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
481
482   return ret;
483 }
484
485 static gboolean
486 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
487 {
488   GstV4l2Src *v4l2src;
489   GstV4l2Object *obj;
490
491   v4l2src = GST_V4L2SRC (src);
492   obj = v4l2src->v4l2object;
493
494   /* make sure we stop capturing and dealloc buffers */
495   if (!gst_v4l2_object_stop (obj))
496     return FALSE;
497
498   if (!gst_v4l2_object_set_format (obj, caps))
499     /* error already posted */
500     return FALSE;
501
502   return TRUE;
503 }
504
505 static gboolean
506 gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
507 {
508   GstV4l2Src *src;
509   GstV4l2Object *obj;
510   GstBufferPool *pool;
511   guint size, min, max, prefix, alignment;
512
513   src = GST_V4L2SRC (bsrc);
514   obj = src->v4l2object;
515
516   gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
517       &alignment, &pool);
518
519   GST_DEBUG_OBJECT (src, "allocation: size:%u min:%u max:%u prefix:%u "
520       "align:%u pool:%" GST_PTR_FORMAT, size, min, max, prefix, alignment,
521       pool);
522
523   if (min != 0) {
524     /* if there is a min-buffers suggestion, use it. We add 1 because we need 1
525      * buffer extra to capture while the other two buffers are downstream */
526     min += 1;
527   } else {
528     min = 2;
529   }
530
531   /* select a pool */
532   switch (obj->mode) {
533     case GST_V4L2_IO_RW:
534       if (pool == NULL) {
535         /* no downstream pool, use our own then */
536         GST_DEBUG_OBJECT (src,
537             "read/write mode: no downstream pool, using our own");
538         pool = GST_BUFFER_POOL_CAST (obj->pool);
539         size = obj->sizeimage;
540       } else {
541         /* in READ/WRITE mode, prefer a downstream pool because our own pool
542          * doesn't help much, we have to write to it as well */
543         GST_DEBUG_OBJECT (src, "read/write mode: using downstream pool");
544         /* use the bigest size, when we use our own pool we can't really do any
545          * other size than what the hardware gives us but for downstream pools
546          * we can try */
547         size = MAX (size, obj->sizeimage);
548       }
549       break;
550     case GST_V4L2_IO_MMAP:
551     case GST_V4L2_IO_USERPTR:
552       /* in streaming mode, prefer our own pool */
553       pool = GST_BUFFER_POOL_CAST (obj->pool);
554       size = obj->sizeimage;
555       GST_DEBUG_OBJECT (src,
556           "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
557       break;
558     case GST_V4L2_IO_AUTO:
559     default:
560       GST_WARNING_OBJECT (src, "unhandled mode");
561       break;
562   }
563
564   if (pool) {
565     GstStructure *config;
566     const GstCaps *caps;
567
568     config = gst_buffer_pool_get_config (pool);
569     gst_buffer_pool_config_get (config, &caps, NULL, NULL, NULL, NULL, NULL);
570     gst_buffer_pool_config_set (config, caps, size, min, max, prefix,
571         alignment);
572
573     /* if downstream supports video metadata, add this to the pool config */
574     if (gst_query_has_allocation_meta (query, GST_VIDEO_META_API))
575       gst_buffer_pool_config_add_option (config,
576           GST_BUFFER_POOL_OPTION_VIDEO_META);
577
578     gst_buffer_pool_set_config (pool, config);
579   }
580
581   gst_query_set_allocation_params (query, size, min, max, prefix,
582       alignment, pool);
583
584   return TRUE;
585 }
586
587 static gboolean
588 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
589 {
590   GstV4l2Src *src;
591   GstV4l2Object *obj;
592   gboolean res = FALSE;
593
594   src = GST_V4L2SRC (bsrc);
595   obj = src->v4l2object;
596
597   switch (GST_QUERY_TYPE (query)) {
598     case GST_QUERY_LATENCY:{
599       GstClockTime min_latency, max_latency;
600       guint32 fps_n, fps_d;
601
602       /* device must be open */
603       if (!GST_V4L2_IS_OPEN (obj)) {
604         GST_WARNING_OBJECT (src,
605             "Can't give latency since device isn't open !");
606         goto done;
607       }
608
609       fps_n = GST_V4L2_FPS_N (obj);
610       fps_d = GST_V4L2_FPS_D (obj);
611
612       /* we must have a framerate */
613       if (fps_n <= 0 || fps_d <= 0) {
614         GST_WARNING_OBJECT (src,
615             "Can't give latency since framerate isn't fixated !");
616         goto done;
617       }
618
619       /* min latency is the time to capture one frame */
620       min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
621
622       /* max latency is total duration of the frame buffer */
623       max_latency =
624           GST_V4L2_BUFFER_POOL_CAST (obj->pool)->max_buffers * min_latency;
625
626       GST_DEBUG_OBJECT (bsrc,
627           "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
628           GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
629
630       /* we are always live, the min latency is 1 frame and the max latency is
631        * the complete buffer of frames. */
632       gst_query_set_latency (query, TRUE, min_latency, max_latency);
633
634       res = TRUE;
635       break;
636     }
637     default:
638       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
639       break;
640   }
641
642 done:
643
644   return res;
645 }
646
647 /* start and stop are not symmetric -- start will open the device, but not start
648  * capture. it's setcaps that will start capture, which is called via basesrc's
649  * negotiate method. stop will both stop capture and close the device.
650  */
651 static gboolean
652 gst_v4l2src_start (GstBaseSrc * src)
653 {
654   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
655
656   v4l2src->offset = 0;
657
658   /* activate settings for first frame */
659   v4l2src->ctrl_time = 0;
660   gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
661
662   return TRUE;
663 }
664
665 static gboolean
666 gst_v4l2src_unlock (GstBaseSrc * src)
667 {
668   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
669   return gst_v4l2_object_unlock (v4l2src->v4l2object);
670 }
671
672 static gboolean
673 gst_v4l2src_unlock_stop (GstBaseSrc * src)
674 {
675   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
676   return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
677 }
678
679 static gboolean
680 gst_v4l2src_stop (GstBaseSrc * src)
681 {
682   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
683   GstV4l2Object *obj = v4l2src->v4l2object;
684
685   if (GST_V4L2_IS_ACTIVE (obj)) {
686     if (!gst_v4l2_object_stop (obj))
687       return FALSE;
688   }
689   return TRUE;
690 }
691
692 static GstStateChangeReturn
693 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
694 {
695   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
696   GstV4l2Src *v4l2src = GST_V4L2SRC (element);
697   GstV4l2Object *obj = v4l2src->v4l2object;
698
699   switch (transition) {
700     case GST_STATE_CHANGE_NULL_TO_READY:
701       /* open the device */
702       if (!gst_v4l2_object_open (obj))
703         return GST_STATE_CHANGE_FAILURE;
704       break;
705     default:
706       break;
707   }
708
709   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
710
711   switch (transition) {
712     case GST_STATE_CHANGE_READY_TO_NULL:
713       /* close the device */
714       if (!gst_v4l2_object_close (obj))
715         return GST_STATE_CHANGE_FAILURE;
716
717       if (v4l2src->probed_caps) {
718         gst_caps_unref (v4l2src->probed_caps);
719         v4l2src->probed_caps = NULL;
720       }
721       break;
722     default:
723       break;
724   }
725
726   return ret;
727 }
728
729 static GstFlowReturn
730 gst_v4l2src_fill (GstPushSrc * src, GstBuffer * buf)
731 {
732   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
733   GstV4l2Object *obj = v4l2src->v4l2object;
734   GstFlowReturn ret;
735   GstClock *clock;
736   GstClockTime timestamp, duration;
737
738 #if 0
739   int i;
740   /* decimate, just capture and throw away frames */
741   for (i = 0; i < v4l2src->decimate - 1; i++) {
742     ret = gst_v4l2_buffer_pool_process (obj, buf);
743     if (ret != GST_FLOW_OK) {
744       return ret;
745     }
746     gst_buffer_unref (*buf);
747   }
748 #endif
749
750   ret =
751       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL_CAST (obj->pool), buf);
752
753   if (G_UNLIKELY (ret != GST_FLOW_OK))
754     goto error;
755
756   /* set buffer metadata */
757   GST_BUFFER_OFFSET (buf) = v4l2src->offset++;
758   GST_BUFFER_OFFSET_END (buf) = v4l2src->offset;
759
760   /* timestamps, LOCK to get clock and base time. */
761   /* FIXME: element clock and base_time is rarely changing */
762   GST_OBJECT_LOCK (v4l2src);
763   if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
764     /* we have a clock, get base time and ref clock */
765     timestamp = GST_ELEMENT (v4l2src)->base_time;
766     gst_object_ref (clock);
767   } else {
768     /* no clock, can't set timestamps */
769     timestamp = GST_CLOCK_TIME_NONE;
770   }
771   GST_OBJECT_UNLOCK (v4l2src);
772
773   duration = obj->duration;
774
775   if (G_LIKELY (clock)) {
776     /* the time now is the time of the clock minus the base time */
777     timestamp = gst_clock_get_time (clock) - timestamp;
778     gst_object_unref (clock);
779
780     /* if we have a framerate adjust timestamp for frame latency */
781     if (GST_CLOCK_TIME_IS_VALID (duration)) {
782       if (timestamp > duration)
783         timestamp -= duration;
784       else
785         timestamp = 0;
786     }
787   }
788
789   /* activate settings for next frame */
790   if (GST_CLOCK_TIME_IS_VALID (duration)) {
791     v4l2src->ctrl_time += duration;
792   } else {
793     /* this is not very good (as it should be the next timestamp),
794      * still good enough for linear fades (as long as it is not -1)
795      */
796     v4l2src->ctrl_time = timestamp;
797   }
798   gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
799   GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
800       GST_TIME_ARGS (v4l2src->ctrl_time));
801
802   /* FIXME: use the timestamp from the buffer itself! */
803   GST_BUFFER_TIMESTAMP (buf) = timestamp;
804   GST_BUFFER_DURATION (buf) = duration;
805
806   return ret;
807
808   /* ERROR */
809 error:
810   {
811     GST_ERROR_OBJECT (src, "error processing buffer");
812     return ret;
813   }
814 }
815
816
817 /* GstURIHandler interface */
818 static GstURIType
819 gst_v4l2src_uri_get_type (GType type)
820 {
821   return GST_URI_SRC;
822 }
823
824 static const gchar *const *
825 gst_v4l2src_uri_get_protocols (GType type)
826 {
827   static const gchar *protocols[] = { "v4l2", NULL };
828
829   return protocols;
830 }
831
832 static gchar *
833 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
834 {
835   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
836
837   if (v4l2src->v4l2object->videodev != NULL) {
838     return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
839   }
840
841   return g_strdup ("v4l2://");
842 }
843
844 static gboolean
845 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
846     GError ** error)
847 {
848   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
849   const gchar *device = DEFAULT_PROP_DEVICE;
850
851   if (strcmp (uri, "v4l2://") != 0) {
852     device = uri + 7;
853   }
854   g_object_set (v4l2src, "device", device, NULL);
855
856   return TRUE;
857 }
858
859
860 static void
861 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
862 {
863   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
864
865   iface->get_type = gst_v4l2src_uri_get_type;
866   iface->get_protocols = gst_v4l2src_uri_get_protocols;
867   iface->get_uri = gst_v4l2src_uri_get_uri;
868   iface->set_uri = gst_v4l2src_uri_set_uri;
869 }