v4l2: remove more unused parameters
[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 "v4l2src_calls.h"
52 #include <unistd.h>
53
54 #include "gstv4l2colorbalance.h"
55 #include "gstv4l2tuner.h"
56 #ifdef HAVE_XVIDEO
57 #include "gstv4l2xoverlay.h"
58 #endif
59 #include "gstv4l2vidorient.h"
60
61 #include "gst/gst-i18n-plugin.h"
62
63 GST_DEBUG_CATEGORY (v4l2src_debug);
64 #define GST_CAT_DEFAULT v4l2src_debug
65
66 #define PROP_DEF_QUEUE_SIZE         2
67 #define PROP_DEF_ALWAYS_COPY        TRUE
68 #define PROP_DEF_DECIMATE           1
69
70 #define DEFAULT_PROP_DEVICE   "/dev/video0"
71
72 enum
73 {
74   PROP_0,
75   V4L2_STD_OBJECT_PROPS,
76   PROP_QUEUE_SIZE,
77   PROP_ALWAYS_COPY,
78   PROP_DECIMATE
79 };
80
81 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2SrcClass, gst_v4l2src);
82 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
83 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
84 #ifdef HAVE_XVIDEO
85 GST_IMPLEMENT_V4L2_XOVERLAY_METHODS (GstV4l2Src, gst_v4l2src);
86 #endif
87 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
88
89 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
90     gpointer iface_data);
91
92 #define gst_v4l2src_parent_class parent_class
93 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
94     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
95     G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
96 #ifdef HAVE_XVIDEO
97     /* FIXME: does GstXOverlay for v4l2src make sense in a GStreamer context? */
98     G_IMPLEMENT_INTERFACE (GST_TYPE_X_OVERLAY,
99         gst_v4l2src_xoverlay_interface_init);
100 #endif
101     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
102         gst_v4l2src_color_balance_interface_init);
103     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
104         gst_v4l2src_video_orientation_interface_init);
105     G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
106         gst_v4l2src_property_probe_interface_init));
107
108 static void gst_v4l2src_dispose (GObject * object);
109 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
110
111 /* element methods */
112 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
113     GstStateChange transition);
114
115 /* basesrc methods */
116 static gboolean gst_v4l2src_start (GstBaseSrc * src);
117 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
118 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
119 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
120 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
121 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
122 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
123 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
124 static void gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
125 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
126
127 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
128     const GValue * value, GParamSpec * pspec);
129 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
130     GValue * value, GParamSpec * pspec);
131
132 /* get_frame io methods */
133 static GstFlowReturn
134 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf);
135 static GstFlowReturn
136 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf);
137
138 static void
139 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
140 {
141   GObjectClass *gobject_class;
142   GstElementClass *element_class;
143   GstBaseSrcClass *basesrc_class;
144   GstPushSrcClass *pushsrc_class;
145
146   gobject_class = G_OBJECT_CLASS (klass);
147   element_class = GST_ELEMENT_CLASS (klass);
148   basesrc_class = GST_BASE_SRC_CLASS (klass);
149   pushsrc_class = GST_PUSH_SRC_CLASS (klass);
150
151   gobject_class->dispose = gst_v4l2src_dispose;
152   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
153   gobject_class->set_property = gst_v4l2src_set_property;
154   gobject_class->get_property = gst_v4l2src_get_property;
155
156   element_class->change_state = gst_v4l2src_change_state;
157
158   gst_v4l2_object_install_properties_helper (gobject_class,
159       DEFAULT_PROP_DEVICE);
160   g_object_class_install_property (gobject_class, PROP_QUEUE_SIZE,
161       g_param_spec_uint ("queue-size", "Queue size",
162           "Number of buffers to be enqueud in the driver in streaming mode",
163           GST_V4L2_MIN_BUFFERS, GST_V4L2_MAX_BUFFERS, PROP_DEF_QUEUE_SIZE,
164           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165   g_object_class_install_property (gobject_class, PROP_ALWAYS_COPY,
166       g_param_spec_boolean ("always-copy", "Always Copy",
167           "If the buffer will or not be used directly from mmap",
168           PROP_DEF_ALWAYS_COPY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169   /**
170    * GstV4l2Src:decimate
171    *
172    * Only use every nth frame
173    *
174    * Since: 0.10.26
175    */
176   g_object_class_install_property (gobject_class, PROP_DECIMATE,
177       g_param_spec_int ("decimate", "Decimate",
178           "Only use every nth frame", 1, G_MAXINT,
179           PROP_DEF_DECIMATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180
181   gst_element_class_set_details_simple (element_class,
182       "Video (video4linux2) Source", "Source/Video",
183       "Reads frames from a Video4Linux2 device",
184       "Edgard Lima <edgard.lima@indt.org.br>, "
185       "Stefan Kost <ensonic@users.sf.net>");
186
187   gst_element_class_add_pad_template
188       (element_class,
189       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
190           gst_v4l2_object_get_all_caps ()));
191
192   basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
193   basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
194   basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
195   basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
196   basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
197   basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
198   basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
199   basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
200   basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
201
202   pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
203
204   klass->v4l2_class_devices = NULL;
205
206   GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
207 }
208
209 static void
210 gst_v4l2src_init (GstV4l2Src * v4l2src)
211 {
212   /* fixme: give an update_fps_function */
213   v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
214       V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
215       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
216
217   /* number of buffers requested */
218   v4l2src->num_buffers = PROP_DEF_QUEUE_SIZE;
219
220   v4l2src->always_copy = PROP_DEF_ALWAYS_COPY;
221   v4l2src->decimate = PROP_DEF_DECIMATE;
222
223   v4l2src->is_capturing = FALSE;
224
225   gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
226   gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
227
228   v4l2src->fps_d = 0;
229   v4l2src->fps_n = 0;
230 }
231
232
233 static void
234 gst_v4l2src_dispose (GObject * object)
235 {
236   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
237
238   if (v4l2src->probed_caps) {
239     gst_caps_unref (v4l2src->probed_caps);
240   }
241
242   G_OBJECT_CLASS (parent_class)->dispose (object);
243 }
244
245
246 static void
247 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
248 {
249   gst_v4l2_object_destroy (v4l2src->v4l2object);
250
251   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
252 }
253
254
255 static void
256 gst_v4l2src_set_property (GObject * object,
257     guint prop_id, const GValue * value, GParamSpec * pspec)
258 {
259   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
260
261   if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
262           prop_id, value, pspec)) {
263     switch (prop_id) {
264       case PROP_QUEUE_SIZE:
265         v4l2src->num_buffers = g_value_get_uint (value);
266         break;
267       case PROP_ALWAYS_COPY:
268         v4l2src->always_copy = g_value_get_boolean (value);
269         break;
270       case PROP_DECIMATE:
271         v4l2src->decimate = g_value_get_int (value);
272         break;
273       default:
274         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275         break;
276     }
277   }
278 }
279
280 static void
281 gst_v4l2src_get_property (GObject * object,
282     guint prop_id, GValue * value, GParamSpec * pspec)
283 {
284   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
285
286   if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
287           prop_id, value, pspec)) {
288     switch (prop_id) {
289       case PROP_QUEUE_SIZE:
290         g_value_set_uint (value, v4l2src->num_buffers);
291         break;
292       case PROP_ALWAYS_COPY:
293         g_value_set_boolean (value, v4l2src->always_copy);
294         break;
295       case PROP_DECIMATE:
296         g_value_set_int (value, v4l2src->decimate);
297         break;
298       default:
299         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300         break;
301     }
302   }
303 }
304
305 /* this function is a bit of a last resort */
306 static void
307 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
308 {
309   GstStructure *structure;
310   gint i;
311
312   GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
313
314   for (i = 0; i < gst_caps_get_size (caps); ++i) {
315     const GValue *v;
316
317     structure = gst_caps_get_structure (caps, i);
318
319     /* We are fixating to a resonable 320x200 resolution
320        and the maximum framerate resolution for that size */
321     gst_structure_fixate_field_nearest_int (structure, "width", 320);
322     gst_structure_fixate_field_nearest_int (structure, "height", 200);
323     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
324         G_MAXINT, 1);
325
326     v = gst_structure_get_value (structure, "format");
327     if (v && G_VALUE_TYPE (v) != G_TYPE_STRING) {
328       const gchar *format;
329
330       g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);
331
332       format = g_value_get_string (gst_value_list_get_value (v, 0));
333       gst_structure_set (structure, "format", G_TYPE_STRING, format, NULL);
334     }
335   }
336
337   GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
338 }
339
340
341 static gboolean
342 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
343 {
344   GstCaps *thiscaps;
345   GstCaps *caps = NULL;
346   GstCaps *peercaps = NULL;
347   gboolean result = FALSE;
348
349   /* first see what is possible on our source pad */
350   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
351   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
352   LOG_CAPS (basesrc, thiscaps);
353
354   /* nothing or anything is allowed, we're done */
355   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
356     goto no_nego_needed;
357
358   /* get the peer caps */
359   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
360   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
361   LOG_CAPS (basesrc, peercaps);
362   if (peercaps && !gst_caps_is_any (peercaps)) {
363     GstCaps *icaps = NULL;
364     int i;
365
366     /* Prefer the first caps we are compatible with that the peer proposed */
367     for (i = 0; i < gst_caps_get_size (peercaps); i++) {
368       /* get intersection */
369       GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
370
371       GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
372       LOG_CAPS (basesrc, ipcaps);
373
374       icaps = gst_caps_intersect (thiscaps, ipcaps);
375       gst_caps_unref (ipcaps);
376
377       if (!gst_caps_is_empty (icaps))
378         break;
379
380       gst_caps_unref (icaps);
381       icaps = NULL;
382     }
383
384     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
385     LOG_CAPS (basesrc, icaps);
386     if (icaps) {
387       /* If there are multiple intersections pick the one with the smallest
388        * resolution strictly bigger then the first peer caps */
389       if (gst_caps_get_size (icaps) > 1) {
390         GstStructure *s = gst_caps_get_structure (peercaps, 0);
391         int best = 0;
392         int twidth, theight;
393         int width = G_MAXINT, height = G_MAXINT;
394
395         if (gst_structure_get_int (s, "width", &twidth)
396             && gst_structure_get_int (s, "height", &theight)) {
397
398           /* Walk the structure backwards to get the first entry of the
399            * smallest resolution bigger (or equal to) the preferred resolution)
400            */
401           for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
402             GstStructure *is = gst_caps_get_structure (icaps, i);
403             int w, h;
404
405             if (gst_structure_get_int (is, "width", &w)
406                 && gst_structure_get_int (is, "height", &h)) {
407               if (w >= twidth && w <= width && h >= theight && h <= height) {
408                 width = w;
409                 height = h;
410                 best = i;
411               }
412             }
413           }
414         }
415
416         caps = gst_caps_copy_nth (icaps, best);
417         gst_caps_unref (icaps);
418       } else {
419         caps = icaps;
420       }
421     }
422     gst_caps_unref (thiscaps);
423     gst_caps_unref (peercaps);
424   } else {
425     /* no peer or peer have ANY caps, work with our own caps then */
426     caps = thiscaps;
427   }
428   if (caps) {
429     caps = gst_caps_make_writable (caps);
430     gst_caps_truncate (caps);
431
432     /* now fixate */
433     if (!gst_caps_is_empty (caps)) {
434       gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
435       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
436       LOG_CAPS (basesrc, caps);
437
438       if (gst_caps_is_any (caps)) {
439         /* hmm, still anything, so element can do anything and
440          * nego is not needed */
441         result = TRUE;
442       } else if (gst_caps_is_fixed (caps)) {
443         /* yay, fixed caps, use those then */
444         gst_pad_push_event (GST_BASE_SRC_PAD (basesrc),
445             gst_event_new_caps (caps));
446         result = gst_v4l2src_set_caps (basesrc, caps);
447       }
448     }
449     gst_caps_unref (caps);
450   }
451   return result;
452
453 no_nego_needed:
454   {
455     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
456     if (thiscaps)
457       gst_caps_unref (thiscaps);
458     return TRUE;
459   }
460 }
461
462 static GstCaps *
463 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
464 {
465   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
466   GstCaps *ret;
467   GSList *walk;
468   GSList *formats;
469
470   if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object)) {
471     /* FIXME: copy? */
472     return
473         gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
474             (v4l2src)));
475   }
476
477   if (v4l2src->probed_caps)
478     return gst_caps_ref (v4l2src->probed_caps);
479
480   formats = gst_v4l2_object_get_format_list (v4l2src->v4l2object);
481
482   ret = gst_caps_new_empty ();
483
484   for (walk = formats; walk; walk = walk->next) {
485     struct v4l2_fmtdesc *format;
486     GstStructure *template;
487
488     format = (struct v4l2_fmtdesc *) walk->data;
489
490     template = gst_v4l2_object_v4l2fourcc_to_structure (format->pixelformat);
491
492     if (template) {
493       GstCaps *tmp;
494
495       tmp =
496           gst_v4l2_object_probe_caps_for_format (v4l2src->v4l2object,
497           format->pixelformat, template);
498       if (tmp)
499         gst_caps_append (ret, tmp);
500
501       gst_structure_free (template);
502     } else {
503       GST_DEBUG_OBJECT (v4l2src, "unknown format %u", format->pixelformat);
504     }
505   }
506
507   v4l2src->probed_caps = gst_caps_ref (ret);
508
509   GST_INFO_OBJECT (v4l2src, "probed caps: %" GST_PTR_FORMAT, ret);
510
511   return ret;
512 }
513
514 static gboolean
515 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
516 {
517   GstV4l2Src *v4l2src;
518   gint w = 0, h = 0;
519   gboolean interlaced;
520   struct v4l2_fmtdesc *format;
521   guint fps_n, fps_d;
522   guint size;
523
524   v4l2src = GST_V4L2SRC (src);
525
526   /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
527   if (!GST_V4L2_IS_OPEN (v4l2src->v4l2object))
528     return FALSE;
529
530   /* make sure we stop capturing and dealloc buffers */
531   if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)) {
532     /* both will throw an element-error on failure */
533     if (!gst_v4l2src_capture_stop (v4l2src))
534       return FALSE;
535     if (!gst_v4l2src_capture_deinit (v4l2src))
536       return FALSE;
537   }
538
539   /* we want our own v4l2 type of fourcc codes */
540   if (!gst_v4l2_object_get_caps_info (v4l2src->v4l2object, caps, &format, &w,
541           &h, &interlaced, &fps_n, &fps_d, &size)) {
542     GST_INFO_OBJECT (v4l2src,
543         "can't get capture format from caps %" GST_PTR_FORMAT, caps);
544     return FALSE;
545   }
546
547   GST_DEBUG_OBJECT (v4l2src, "trying to set_capture %dx%d at %d/%d fps, "
548       "format %s", w, h, fps_n, fps_d, format->description);
549
550   if (!gst_v4l2src_set_capture (v4l2src, format->pixelformat, w, h,
551           interlaced, fps_n, fps_d))
552     /* error already posted */
553     return FALSE;
554
555   if (!gst_v4l2src_capture_init (v4l2src))
556     return FALSE;
557
558   if (v4l2src->use_mmap) {
559     v4l2src->get_frame = gst_v4l2src_get_mmap;
560   } else {
561     v4l2src->get_frame = gst_v4l2src_get_read;
562   }
563
564   if (!gst_v4l2src_capture_start (v4l2src))
565     return FALSE;
566
567   /* now store the expected output size */
568   v4l2src->frame_byte_size = size;
569
570   return TRUE;
571 }
572
573 static gboolean
574 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
575 {
576   GstV4l2Src *src;
577
578   gboolean res = FALSE;
579
580   src = GST_V4L2SRC (bsrc);
581
582   switch (GST_QUERY_TYPE (query)) {
583     case GST_QUERY_LATENCY:{
584       GstClockTime min_latency, max_latency;
585
586       /* device must be open */
587       if (!GST_V4L2_IS_OPEN (src->v4l2object)) {
588         GST_WARNING_OBJECT (src,
589             "Can't give latency since device isn't open !");
590         goto done;
591       }
592
593       /* we must have a framerate */
594       if (src->fps_n <= 0 || src->fps_d <= 0) {
595         GST_WARNING_OBJECT (src,
596             "Can't give latency since framerate isn't fixated !");
597         goto done;
598       }
599
600       /* min latency is the time to capture one frame */
601       min_latency =
602           gst_util_uint64_scale_int (GST_SECOND, src->fps_d, src->fps_n);
603
604       /* max latency is total duration of the frame buffer */
605       max_latency = src->num_buffers * min_latency;
606
607       GST_DEBUG_OBJECT (bsrc,
608           "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
609           GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
610
611       /* we are always live, the min latency is 1 frame and the max latency is
612        * the complete buffer of frames. */
613       gst_query_set_latency (query, TRUE, min_latency, max_latency);
614
615       res = TRUE;
616       break;
617     }
618     default:
619       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
620       break;
621   }
622
623 done:
624
625   return res;
626 }
627
628 /* start and stop are not symmetric -- start will open the device, but not start
629  * capture. it's setcaps that will start capture, which is called via basesrc's
630  * negotiate method. stop will both stop capture and close the device.
631  */
632 static gboolean
633 gst_v4l2src_start (GstBaseSrc * src)
634 {
635   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
636
637   v4l2src->offset = 0;
638
639   /* activate settings for first frame */
640   v4l2src->ctrl_time = 0;
641   gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
642
643   return TRUE;
644 }
645
646 static gboolean
647 gst_v4l2src_unlock (GstBaseSrc * src)
648 {
649   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
650
651   GST_LOG_OBJECT (src, "Flushing");
652   gst_poll_set_flushing (v4l2src->v4l2object->poll, TRUE);
653
654   return TRUE;
655 }
656
657 static gboolean
658 gst_v4l2src_unlock_stop (GstBaseSrc * src)
659 {
660   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
661
662   GST_LOG_OBJECT (src, "No longer flushing");
663   gst_poll_set_flushing (v4l2src->v4l2object->poll, FALSE);
664
665   return TRUE;
666 }
667
668 static gboolean
669 gst_v4l2src_stop (GstBaseSrc * src)
670 {
671   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
672
673   if (GST_V4L2_IS_ACTIVE (v4l2src->v4l2object)
674       && !gst_v4l2src_capture_stop (v4l2src))
675     return FALSE;
676
677   if (v4l2src->v4l2object->buffer != NULL) {
678     if (!gst_v4l2src_capture_deinit (v4l2src))
679       return FALSE;
680   }
681
682   v4l2src->fps_d = 0;
683   v4l2src->fps_n = 0;
684
685   return TRUE;
686 }
687
688 static GstStateChangeReturn
689 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
690 {
691   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
692   GstV4l2Src *v4l2src = GST_V4L2SRC (element);
693
694   switch (transition) {
695     case GST_STATE_CHANGE_NULL_TO_READY:
696       /* open the device */
697       if (!gst_v4l2_object_start (v4l2src->v4l2object))
698         return GST_STATE_CHANGE_FAILURE;
699       break;
700     default:
701       break;
702   }
703
704   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
705
706   switch (transition) {
707     case GST_STATE_CHANGE_READY_TO_NULL:
708       /* close the device */
709       if (!gst_v4l2_object_stop (v4l2src->v4l2object))
710         return GST_STATE_CHANGE_FAILURE;
711
712       if (v4l2src->probed_caps) {
713         gst_caps_unref (v4l2src->probed_caps);
714         v4l2src->probed_caps = NULL;
715       }
716       break;
717     default:
718       break;
719   }
720
721   return ret;
722 }
723
724 static GstFlowReturn
725 gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
726 {
727   GstFlowReturn res;
728   gint amount;
729   gint ret;
730   gpointer data;
731   gint buffersize;
732
733   buffersize = v4l2src->frame_byte_size;
734   /* In case the size per frame is unknown assume it's a streaming format (e.g.
735    * mpegts) and grab a reasonable default size instead */
736   if (buffersize == 0)
737     buffersize = GST_BASE_SRC (v4l2src)->blocksize;
738
739   *buf = gst_buffer_new_and_alloc (buffersize);
740   data = gst_buffer_map (*buf, NULL, NULL, GST_MAP_WRITE);
741
742   do {
743     ret = gst_poll_wait (v4l2src->v4l2object->poll, GST_CLOCK_TIME_NONE);
744     if (G_UNLIKELY (ret < 0)) {
745       if (errno == EBUSY)
746         goto stopped;
747       if (errno == ENXIO) {
748         GST_DEBUG_OBJECT (v4l2src,
749             "v4l2 device doesn't support polling. Disabling");
750         v4l2src->v4l2object->can_poll_device = FALSE;
751       } else {
752         if (errno != EAGAIN && errno != EINTR)
753           goto select_error;
754       }
755     }
756     amount = v4l2_read (v4l2src->v4l2object->video_fd, data, buffersize);
757
758     if (amount == buffersize) {
759       break;
760     } else if (amount == -1) {
761       if (errno == EAGAIN || errno == EINTR) {
762         continue;
763       } else
764         goto read_error;
765     } else {
766       /* short reads can happen if a signal interrupts the read */
767       continue;
768     }
769   } while (TRUE);
770
771   gst_buffer_unmap (*buf, data, amount);
772
773   /* we set the buffer metadata in gst_v4l2src_create() */
774
775   return GST_FLOW_OK;
776
777   /* ERRORS */
778 select_error:
779   {
780     GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ, (NULL),
781         ("select error %d: %s (%d)", ret, g_strerror (errno), errno));
782     res = GST_FLOW_ERROR;
783     goto cleanup;
784   }
785 stopped:
786   {
787     GST_DEBUG ("stop called");
788     res = GST_FLOW_WRONG_STATE;
789     goto cleanup;
790   }
791 read_error:
792   {
793     GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
794         (_("Error reading %d bytes from device '%s'."),
795             buffersize, v4l2src->v4l2object->videodev), GST_ERROR_SYSTEM);
796     res = GST_FLOW_ERROR;
797     goto cleanup;
798   }
799 cleanup:
800   {
801     gst_buffer_unmap (*buf, data, 0);
802     gst_buffer_unref (*buf);
803     return res;
804   }
805 }
806
807 static GstFlowReturn
808 gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
809 {
810   GstBuffer *temp;
811   GstFlowReturn ret;
812   guint size;
813   guint count = 0;
814
815 again:
816   ret = gst_v4l2src_grab_frame (v4l2src, &temp);
817   if (G_UNLIKELY (ret != GST_FLOW_OK))
818     goto done;
819
820   if (v4l2src->frame_byte_size > 0) {
821     size = gst_buffer_get_size (temp);
822
823     /* if size does not match what we expected, try again */
824     if (size != v4l2src->frame_byte_size) {
825       GST_ELEMENT_WARNING (v4l2src, RESOURCE, READ,
826           (_("Got unexpected frame size of %u instead of %u."),
827               size, v4l2src->frame_byte_size), (NULL));
828       gst_buffer_unref (temp);
829       if (count++ > 50)
830         goto size_error;
831
832       goto again;
833     }
834   }
835
836   *buf = temp;
837 done:
838   return ret;
839
840   /* ERRORS */
841 size_error:
842   {
843     GST_ELEMENT_ERROR (v4l2src, RESOURCE, READ,
844         (_("Error reading %d bytes on device '%s'."),
845             v4l2src->frame_byte_size, v4l2src->v4l2object->videodev), (NULL));
846     return GST_FLOW_ERROR;
847   }
848 }
849
850 static GstFlowReturn
851 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
852 {
853   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
854   int i;
855   GstFlowReturn ret;
856
857   if (v4l2src->get_frame == NULL)
858     goto not_negotiated;
859
860   /* decimate, just capture and throw away frames */
861   for (i = 0; i < v4l2src->decimate - 1; i++) {
862     ret = v4l2src->get_frame (v4l2src, buf);
863     if (ret != GST_FLOW_OK) {
864       return ret;
865     }
866     gst_buffer_unref (*buf);
867   }
868
869   ret = v4l2src->get_frame (v4l2src, buf);
870
871   /* set buffer metadata */
872   if (G_LIKELY (ret == GST_FLOW_OK && *buf)) {
873     GstClock *clock;
874     GstClockTime timestamp;
875
876     GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
877     GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
878
879     /* timestamps, LOCK to get clock and base time. */
880     /* FIXME: element clock and base_time is rarely changing */
881     GST_OBJECT_LOCK (v4l2src);
882     if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
883       /* we have a clock, get base time and ref clock */
884       timestamp = GST_ELEMENT (v4l2src)->base_time;
885       gst_object_ref (clock);
886     } else {
887       /* no clock, can't set timestamps */
888       timestamp = GST_CLOCK_TIME_NONE;
889     }
890     GST_OBJECT_UNLOCK (v4l2src);
891
892     if (G_LIKELY (clock)) {
893       /* the time now is the time of the clock minus the base time */
894       timestamp = gst_clock_get_time (clock) - timestamp;
895       gst_object_unref (clock);
896
897       /* if we have a framerate adjust timestamp for frame latency */
898       if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
899         if (timestamp > v4l2src->duration)
900           timestamp -= v4l2src->duration;
901         else
902           timestamp = 0;
903       }
904     }
905
906     /* activate settings for next frame */
907     if (GST_CLOCK_TIME_IS_VALID (v4l2src->duration)) {
908       v4l2src->ctrl_time += v4l2src->duration;
909     } else {
910       /* this is not very good (as it should be the next timestamp),
911        * still good enough for linear fades (as long as it is not -1)
912        */
913       v4l2src->ctrl_time = timestamp;
914     }
915     gst_object_sync_values (G_OBJECT (src), v4l2src->ctrl_time);
916     GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT,
917         GST_TIME_ARGS (v4l2src->ctrl_time));
918
919     /* FIXME: use the timestamp from the buffer itself! */
920     GST_BUFFER_TIMESTAMP (*buf) = timestamp;
921     GST_BUFFER_DURATION (*buf) = v4l2src->duration;
922   }
923   return ret;
924
925   /* ERRORS */
926 not_negotiated:
927   {
928     GST_DEBUG_OBJECT (src, "we are not negotiated");
929     return GST_FLOW_NOT_NEGOTIATED;
930   }
931 }
932
933
934 /* GstURIHandler interface */
935 static GstURIType
936 gst_v4l2src_uri_get_type (GType type)
937 {
938   return GST_URI_SRC;
939 }
940
941 static gchar **
942 gst_v4l2src_uri_get_protocols (GType type)
943 {
944   static gchar *protocols[] = { (char *) "v4l2", NULL };
945
946   return protocols;
947 }
948
949 static const gchar *
950 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
951 {
952   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
953
954   if (v4l2src->v4l2object->videodev != NULL) {
955     gchar uri[256];
956
957     /* need to return a const string, but also don't want to leak the generated
958      * string, so just intern it - there's a limited number of video devices
959      * after all */
960     g_snprintf (uri, sizeof (uri), "v4l2://%s", v4l2src->v4l2object->videodev);
961     return g_intern_string (uri);
962   }
963
964   return "v4l2://";
965 }
966
967 static gboolean
968 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
969 {
970   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
971   const gchar *device = DEFAULT_PROP_DEVICE;
972
973   if (strcmp (uri, "v4l2://") != 0) {
974     device = uri + 7;
975   }
976   g_object_set (v4l2src, "device", device, NULL);
977
978   return TRUE;
979 }
980
981
982 static void
983 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
984 {
985   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
986
987   iface->get_type = gst_v4l2src_uri_get_type;
988   iface->get_protocols = gst_v4l2src_uri_get_protocols;
989   iface->get_uri = gst_v4l2src_uri_get_uri;
990   iface->set_uri = gst_v4l2src_uri_set_uri;
991 }