v4l2: move capture code to device object
[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 "gstv4l2src.h"
54
55 #include "gstv4l2colorbalance.h"
56 #include "gstv4l2tuner.h"
57 #ifdef HAVE_XVIDEO
58 #include "gstv4l2xoverlay.h"
59 #endif
60 #include "gstv4l2vidorient.h"
61
62 #include "gst/gst-i18n-plugin.h"
63
64 GST_DEBUG_CATEGORY (v4l2src_debug);
65 #define GST_CAT_DEFAULT v4l2src_debug
66
67 #define PROP_DEF_QUEUE_SIZE         2
68 #define PROP_DEF_ALWAYS_COPY        TRUE
69 #define PROP_DEF_DECIMATE           1
70
71 #define DEFAULT_PROP_DEVICE   "/dev/video0"
72
73 enum
74 {
75   PROP_0,
76   V4L2_STD_OBJECT_PROPS,
77   PROP_QUEUE_SIZE,
78   PROP_ALWAYS_COPY,
79   PROP_DECIMATE
80 };
81
82 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SrcClass, gst_v4l2src);
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     G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
107         gst_v4l2src_property_probe_interface_init));
108
109 static void gst_v4l2src_dispose (GObject * object);
110 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
111
112 /* element methods */
113 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
114     GstStateChange transition);
115
116 /* basesrc methods */
117 static gboolean gst_v4l2src_start (GstBaseSrc * src);
118 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
119 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
120 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
121 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
122 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
123 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
124 static GstFlowReturn gst_v4l2src_create (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_QUEUE_SIZE,
156       g_param_spec_uint ("queue-size", "Queue size",
157           "Number of buffers to be enqueud in the driver in streaming mode",
158           GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
159           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160   g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
161       g_param_spec_boolean ("always-copy", "Always Copy",
162           "If the buffer will or not be used directly from mmap",
163           PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164   /**
165    * GstV4l2Src:decimate
166    *
167    * Only use every nth frame
168    *
169    * Since: 0.10.26
170    */
171   g_object_class_install_property (gobject_class, PROP_DECIMATE,
172       g_param_spec_int ("decimate", "Decimate",
173           "Only use every nth frame", 1, G_MAXINT,
174           PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175
176   gst_element_class_set_details_simple (element_class,
177       "Video (video4linux2) Source", "Source/Video",
178       "Reads frames from a Video4Linux2 device",
179       "Edgard Lima <edgard.lima@indt.org.br>, "
180       "Stefan Kost <ensonic@users.sf.net>");
181
182   gst_element_class_add_pad_template
183       (element_class,
184       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
185           gst_v4l2_object_get_all_caps ()));
186
187   basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
188   basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
189   basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
190   basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
191   basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
192   basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
193   basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
194   basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
195   basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
196
197   pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
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   /* number of buffers requested */
213   v4l2src->v4l2object->num_buffers = PROP_DEF_QUEUE_SIZE;
214
215   v4l2src->v4l2object->always_copy = PROP_DEF_ALWAYS_COPY;
216   v4l2src->decimate = PROP_DEF_DECIMATE;
217
218   gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
219   gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
220 }
221
222 static void
223 gst_v4l2src_dispose (GObject * object)
224 {
225   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
226
227   if (v4l2src->probed_caps) {
228     gst_caps_unref (v4l2src->probed_caps);
229   }
230
231   G_OBJECT_CLASS (parent_class)->dispose (object);
232 }
233
234
235 static void
236 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
237 {
238   gst_v4l2_object_destroy (v4l2src->v4l2object);
239
240   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
241 }
242
243
244 static void
245 gst_v4l2src_set_property (GObject * object,
246     guint prop_id, const GValue * value, GParamSpec * pspec)
247 {
248   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
249
250   if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
251           prop_id, value, pspec)) {
252     switch (prop_id) {
253       case PROP_QUEUE_SIZE:
254         v4l2src->v4l2object->num_buffers = g_value_get_uint (value);
255         break;
256       case PROP_ALWAYS_COPY:
257         v4l2src->v4l2object->always_copy = g_value_get_boolean (value);
258         break;
259       case PROP_DECIMATE:
260         v4l2src->decimate = g_value_get_int (value);
261         break;
262       default:
263         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264         break;
265     }
266   }
267 }
268
269 static void
270 gst_v4l2src_get_property (GObject * object,
271     guint prop_id, GValue * value, GParamSpec * pspec)
272 {
273   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
274
275   if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
276           prop_id, value, pspec)) {
277     switch (prop_id) {
278       case PROP_QUEUE_SIZE:
279         g_value_set_uint (value, v4l2src->v4l2object->num_buffers);
280         break;
281       case PROP_ALWAYS_COPY:
282         g_value_set_boolean (value, v4l2src->v4l2object->always_copy);
283         break;
284       case PROP_DECIMATE:
285         g_value_set_int (value, v4l2src->decimate);
286         break;
287       default:
288         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289         break;
290     }
291   }
292 }
293
294 /* this function is a bit of a last resort */
295 static void
296 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
297 {
298   GstStructure *structure;
299   gint i;
300
301   GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
302
303   for (i = 0; i < gst_caps_get_size (caps); ++i) {
304     const GValue *v;
305
306     structure = gst_caps_get_structure (caps, i);
307
308     /* We are fixating to a resonable 320x200 resolution
309        and the maximum framerate resolution for that size */
310     gst_structure_fixate_field_nearest_int (structure, "width", 320);
311     gst_structure_fixate_field_nearest_int (structure, "height", 200);
312     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
313         G_MAXINT, 1);
314
315     v = gst_structure_get_value (structure, "format");
316     if (v && G_VALUE_TYPE (v) != G_TYPE_STRING) {
317       const gchar *format;
318
319       g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
320
321       format = g_value_get_string (gst_value_list_get_value (v, 0));
322       gst_structure_set (structure, "format", G_TYPE_STRING, format, NULL);
323     }
324   }
325
326   GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
327 }
328
329
330 static gboolean
331 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
332 {
333   GstCaps *thiscaps;
334   GstCaps *caps = NULL;
335   GstCaps *peercaps = NULL;
336   gboolean result = FALSE;
337
338   /* first see what is possible on our source pad */
339   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
340   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
341   LOG_CAPS (basesrc, thiscaps);
342
343   /* nothing or anything is allowed, we're done */
344   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
345     goto no_nego_needed;
346
347   /* get the peer caps */
348   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
349   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
350   LOG_CAPS (basesrc, peercaps);
351   if (peercaps && !gst_caps_is_any (peercaps)) {
352     GstCaps *icaps = NULL;
353     int i;
354
355     /* Prefer the first caps we are compatible with that the peer proposed */
356     for (i = 0; i < gst_caps_get_size (peercaps); i++) {
357       /* get intersection */
358       GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
359
360       GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
361       LOG_CAPS (basesrc, ipcaps);
362
363       icaps = gst_caps_intersect (thiscaps, ipcaps);
364       gst_caps_unref (ipcaps);
365
366       if (!gst_caps_is_empty (icaps))
367         break;
368
369       gst_caps_unref (icaps);
370       icaps = NULL;
371     }
372
373     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
374     LOG_CAPS (basesrc, icaps);
375     if (icaps) {
376       /* If there are multiple intersections pick the one with the smallest
377        * resolution strictly bigger then the first peer caps */
378       if (gst_caps_get_size (icaps) > 1) {
379         GstStructure *s = gst_caps_get_structure (peercaps, 0);
380         int best = 0;
381         int twidth, theight;
382         int width = G_MAXINT, height = G_MAXINT;
383
384         if (gst_structure_get_int (s, "width", &twidth)
385             && gst_structure_get_int (s, "height", &theight)) {
386
387           /* Walk the structure backwards to get the first entry of the
388            * smallest resolution bigger (or equal to) the preferred resolution)
389            */
390           for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
391             GstStructure *is = gst_caps_get_structure (icaps, i);
392             int w, h;
393
394             if (gst_structure_get_int (is, "width", &w)
395                 && gst_structure_get_int (is, "height", &h)) {
396               if (w >= twidth && w <= width && h >= theight && h <= height) {
397                 width = w;
398                 height = h;
399                 best = i;
400               }
401             }
402           }
403         }
404
405         caps = gst_caps_copy_nth (icaps, best);
406         gst_caps_unref (icaps);
407       } else {
408         caps = icaps;
409       }
410     }
411     gst_caps_unref (thiscaps);
412     gst_caps_unref (peercaps);
413   } else {
414     /* no peer or peer have ANY caps, work with our own caps then */
415     caps = thiscaps;
416   }
417   if (caps) {
418     caps = gst_caps_make_writable (caps);
419     gst_caps_truncate (caps);
420
421     /* now fixate */
422     if (!gst_caps_is_empty (caps)) {
423       gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
424       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
425       LOG_CAPS (basesrc, caps);
426
427       if (gst_caps_is_any (caps)) {
428         /* hmm, still anything, so element can do anything and
429          * nego is not needed */
430         result = TRUE;
431       } else if (gst_caps_is_fixed (caps)) {
432         /* yay, fixed caps, use those then */
433         gst_pad_push_event (GST_BASE_SRC_PAD (basesrc),
434             gst_event_new_caps (caps));
435         result = gst_v4l2src_set_caps (basesrc, caps);
436       }
437     }
438     gst_caps_unref (caps);
439   }
440   return result;
441
442 no_nego_needed:
443   {
444     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
445     if (thiscaps)
446       gst_caps_unref (thiscaps);
447     return TRUE;
448   }
449 }
450
451 static GstCaps *
452 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
453 {
454   GstV4l2Src *v4l2src;
455   GstV4l2Object *obj;
456   GstCaps *ret;
457   GSList *walk;
458   GSList *formats;
459
460   v4l2src = GST_V4L2SRC (src);
461   obj = v4l2src->v4l2object;
462
463   if (!GST_V4L2_IS_OPEN (obj)) {
464     /* FIXME: copy? */
465     return
466         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
467             (v4l2src)));
468   }
469
470   if (v4l2src->probed_caps)
471     return gst_caps_ref (v4l2src->probed_caps);
472
473   formats = gst_v4l2_object_get_format_list (obj);
474
475   ret = gst_caps_new_empty ();
476
477   for (walk = formats; walk; walk = walk->next) {
478     struct v4l2_fmtdesc *format;
479     GstStructure *template;
480
481     format = (struct v4l2_fmtdesc *) walk->data;
482
483     template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
484
485     if (template) {
486       GstCaps *tmp;
487
488       tmp =
489           gst_v4l2_object_probe_caps_for_format (obj,
490           format->pixelformat, template);
491       if (tmp)
492         gst_caps_append (ret, tmp);
493
494       gst_structure_free (template);
495     } else {
496       GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
497     }
498   }
499
500   v4l2src->probed_caps = gst_caps_ref (ret);
501
502   GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
503
504   return ret;
505 }
506
507 static gboolean
508 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
509 {
510   GstV4l2Src *v4l2src;
511   GstV4l2Object *obj;
512
513   v4l2src = GST_V4L2SRC (src);
514   obj = v4l2src->v4l2object;
515
516   /* make sure we stop capturing and dealloc buffers */
517   if (!gst_v4l2_object_stop (obj))
518     return FALSE;
519
520   if (!gst_v4l2_object_set_format (obj, caps))
521     /* error already posted */
522     return FALSE;
523
524   if (!gst_v4l2_object_start (obj))
525     return FALSE;
526
527   /* now store the expected output size */
528   v4l2src->frame_byte_size = obj->size;
529
530   return TRUE;
531 }
532
533 static gboolean
534 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
535 {
536   GstV4l2Src *src;
537
538   gboolean res = FALSE;
539
540   src = GST_V4L2SRC (bsrc);
541
542   switch (GST_QUERY_TYPE (query)) {
543     case GST_QUERY_LATENCY:{
544       GstClockTime min_latency, max_latency;
545       guint32 fps_n, fps_d;
546
547       /* device must be open */
548       if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
549         GST_WARNING_OBJECT (src,
550             "Can't give latency since device isn't open !");
551         goto done;
552       }
553
554       fps_n = GST_V4L2_FPS_N (src->v4l2object);
555       fps_d = GST_V4L2_FPS_D (src->v4l2object);
556
557       /* we must have a framerate */
558       if (fps_n <= 0 || fps_d <= 0) {
559         GST_WARNING_OBJECT (src,
560             "Can't give latency since framerate isn't fixated !");
561         goto done;
562       }
563
564       /* min latency is the time to capture one frame */
565       min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
566
567       /* max latency is total duration of the frame buffer */
568       max_latency = src->v4l2object->num_buffers * min_latency;
569
570       GST_DEBUG_OBJECT (bsrc,
571           "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
572           GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
573
574       /* we are always live, the min latency is 1 frame and the max latency is
575        * the complete buffer of frames. */
576       gst_query_set_latency (query, TRUE, min_latency, max_latency);
577
578       res = TRUE;
579       break;
580     }
581     default:
582       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
583       break;
584   }
585
586 done:
587
588   return res;
589 }
590
591 /* start and stop are not symmetric -- start will open the device, but not start
592  * capture. it's setcaps that will start capture, which is called via basesrc's
593  * negotiate method. stop will both stop capture and close the device.
594  */
595 static gboolean
596 gst_v4l2src_start (GstBaseSrc * src)
597 {
598   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
599
600   v4l2src->offset = 0;
601
602   /* activate settings for first frame */
603   v4l2src->ctrl_time = 0;
604   gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
605
606   return TRUE;
607 }
608
609 static gboolean
610 gst_v4l2src_unlock (GstBaseSrc * src)
611 {
612   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
613
614   GST_LOG_OBJECT (src, "Flushing");
615   gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
616
617   return TRUE;
618 }
619
620 static gboolean
621 gst_v4l2src_unlock_stop (GstBaseSrc * src)
622 {
623   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
624
625   GST_LOG_OBJECT (src, "No longer flushing");
626   gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
627
628   return TRUE;
629 }
630
631 static gboolean
632 gst_v4l2src_stop (GstBaseSrc * src)
633 {
634   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
635   GstV4l2Object *obj = v4l2src->v4l2object;
636
637   if (GST_V4L2_IS_ACTIVE (obj)) {
638     if (!gst_v4l2_object_stop (obj))
639       return FALSE;
640   }
641   return TRUE;
642 }
643
644 static GstStateChangeReturn
645 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
646 {
647   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
648   GstV4l2Src *v4l2src = GST_V4L2SRC (element);
649   GstV4l2Object *obj = v4l2src->v4l2object;
650
651   switch (transition) {
652     case GST_STATE_CHANGE_NULL_TO_READY:
653       /* open the device */
654       if (!gst_v4l2_object_open (obj))
655         return GST_STATE_CHANGE_FAILURE;
656       break;
657     default:
658       break;
659   }
660
661   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
662
663   switch (transition) {
664     case GST_STATE_CHANGE_READY_TO_NULL:
665       /* close the device */
666       if (!gst_v4l2_object_close (obj))
667         return GST_STATE_CHANGE_FAILURE;
668
669       if (v4l2src->probed_caps) {
670         gst_caps_unref (v4l2src->probed_caps);
671         v4l2src->probed_caps = NULL;
672       }
673       break;
674     default:
675       break;
676   }
677
678   return ret;
679 }
680
681 static GstFlowReturn
682 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
683 {
684   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
685   GstV4l2Object *obj = v4l2src->v4l2object;
686   int i;
687   GstFlowReturn ret;
688
689   /* decimate, just capture and throw away frames */
690   for (i = 0; i < v4l2src->decimate - 1; i++) {
691     ret = gst_v4l2_object_get_buffer (obj, buf);
692     if (ret != GST_FLOW_OK) {
693       return ret;
694     }
695     gst_buffer_unref (*buf);
696   }
697
698   ret = gst_v4l2_object_get_buffer (obj, buf);
699
700   /* set buffer metadata */
701   if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
702     GstClock *clock;
703     GstClockTime timestamp, duration;
704
705     GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
706     GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
707
708     /* timestamps, LOCK to get clock and base time. */
709     /* FIXME: element clock and base_time is rarely changing */
710     GST_OBJECT_LOCK (v4l2src);
711     if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
712       /* we have a clock, get base time and ref clock */
713       timestamp = GST_ELEMENT (v4l2src)->base_time;
714       gst_object_ref (clock);
715     } else {
716       /* no clock, can't set timestamps */
717       timestamp = GST_CLOCK_TIME_NONE;
718     }
719     GST_OBJECT_UNLOCK (v4l2src);
720
721     duration = obj->duration;
722
723     if (G_LIKELY (clock)) {
724       /* the time now is the time of the clock minus the base time */
725       timestamp = gst_clock_get_time (clock) - timestamp;
726       gst_object_unref (clock);
727
728       /* if we have a framerate adjust timestamp for frame latency */
729       if (GST_CLOCK_TIME_IS_VALID (duration)) {
730         if (timestamp > duration)
731           timestamp -= duration;
732         else
733           timestamp = 0;
734       }
735     }
736
737     /* activate settings for next frame */
738     if (GST_CLOCK_TIME_IS_VALID (duration)) {
739       v4l2src->ctrl_time += duration;
740     } else {
741       /* this is not very good (as it should be the next timestamp),
742        * still good enough for linear fades (as long as it is not -1)
743        */
744       v4l2src->ctrl_time = timestamp;
745     }
746     gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
747     GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
748         GST_TIME_ARGS (v4l2src->ctrl_time));
749
750     /* FIXME: use the timestamp from the buffer itself! */
751     GST_BUFFER_TIMESTAMP (*buf) = timestamp;
752     GST_BUFFER_DURATION (*buf) = duration;
753   }
754   return ret;
755 }
756
757
758 /* GstURIHandler interface */
759 static GstURIType
760 gst_v4l2src_uri_get_type (GType type)
761 {
762   return GST_URI_SRC;
763 }
764
765 static gchar **
766 gst_v4l2src_uri_get_protocols (GType type)
767 {
768   static gchar *protocols[] = { (char *) "v4l2", NULL };
769
770   return protocols;
771 }
772
773 static const gchar *
774 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
775 {
776   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
777
778   if (v4l2src->v4l2object->videodev != NULL) {
779     gchar uri[256];
780
781     /* need to return a const string, but also don't want to leak the generated
782      * string, so just intern it - there's a limited number of video devices
783      * after all */
784     g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
785     return g_intern_string (uri);
786   }
787
788   return "v4l2://";
789 }
790
791 static gboolean
792 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
793 {
794   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
795   const gchar *device = DEFAULT_PROP_DEVICE;
796
797   if (strcmp (uri, "v4l2://") != 0) {
798     device = uri + 7;
799   }
800   g_object_set (v4l2src, "device", device, NULL);
801
802   return TRUE;
803 }
804
805
806 static void
807 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
808 {
809   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
810
811   iface->get_type = gst_v4l2src_uri_get_type;
812   iface->get_protocols = gst_v4l2src_uri_get_protocols;
813   iface->get_uri = gst_v4l2src_uri_get_uri;
814   iface->set_uri = gst_v4l2src_uri_set_uri;
815 }