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