v4l2bufferpool: Acquire cannot return a buffer from another pool
[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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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-1.0 v4l2src ! xvimagesink
34  * ]| This pipeline shows the video captured from /dev/video0 tv card and for
35  * webcams.
36  * |[
37  * gst-launch-1.0 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 #include <string.h>
48 #include <sys/time.h>
49 #include <unistd.h>
50
51 #include <gst/video/gstvideometa.h>
52 #include <gst/video/gstvideopool.h>
53
54 #include "gstv4l2src.h"
55
56 #include "gstv4l2colorbalance.h"
57 #include "gstv4l2tuner.h"
58 #include "gstv4l2vidorient.h"
59
60 #include "gst/gst-i18n-plugin.h"
61
62 GST_DEBUG_CATEGORY (v4l2src_debug);
63 #define GST_CAT_DEFAULT v4l2src_debug
64
65 #define DEFAULT_PROP_DEVICE   "/dev/video0"
66
67 enum
68 {
69   PROP_0,
70   V4L2_STD_OBJECT_PROPS,
71   PROP_LAST
72 };
73
74 /* signals and args */
75 enum
76 {
77   SIGNAL_PRE_SET_FORMAT,
78   LAST_SIGNAL
79 };
80
81 static guint gst_v4l2_signals[LAST_SIGNAL] = { 0 };
82
83 GST_IMPLEMENT_V4L2_COLOR_BALANCE_METHODS (GstV4l2Src, gst_v4l2src);
84 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Src, gst_v4l2src);
85 GST_IMPLEMENT_V4L2_VIDORIENT_METHODS (GstV4l2Src, gst_v4l2src);
86
87 static void gst_v4l2src_uri_handler_init (gpointer g_iface,
88     gpointer iface_data);
89
90 #define gst_v4l2src_parent_class parent_class
91 G_DEFINE_TYPE_WITH_CODE (GstV4l2Src, gst_v4l2src, GST_TYPE_PUSH_SRC,
92     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_v4l2src_uri_handler_init);
93     G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER, gst_v4l2src_tuner_interface_init);
94     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
95         gst_v4l2src_color_balance_interface_init);
96     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_ORIENTATION,
97         gst_v4l2src_video_orientation_interface_init));
98
99 static void gst_v4l2src_finalize (GstV4l2Src * v4l2src);
100
101 /* element methods */
102 static GstStateChangeReturn gst_v4l2src_change_state (GstElement * element,
103     GstStateChange transition);
104
105 /* basesrc methods */
106 static gboolean gst_v4l2src_start (GstBaseSrc * src);
107 static gboolean gst_v4l2src_unlock (GstBaseSrc * src);
108 static gboolean gst_v4l2src_unlock_stop (GstBaseSrc * src);
109 static gboolean gst_v4l2src_stop (GstBaseSrc * src);
110 static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
111 static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter);
112 static gboolean gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query);
113 static gboolean gst_v4l2src_decide_allocation (GstBaseSrc * src,
114     GstQuery * query);
115 static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
116 static GstCaps *gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps);
117 static gboolean gst_v4l2src_negotiate (GstBaseSrc * basesrc);
118
119 static void gst_v4l2src_set_property (GObject * object, guint prop_id,
120     const GValue * value, GParamSpec * pspec);
121 static void gst_v4l2src_get_property (GObject * object, guint prop_id,
122     GValue * value, GParamSpec * pspec);
123
124 static void
125 gst_v4l2src_class_init (GstV4l2SrcClass * klass)
126 {
127   GObjectClass *gobject_class;
128   GstElementClass *element_class;
129   GstBaseSrcClass *basesrc_class;
130   GstPushSrcClass *pushsrc_class;
131
132   gobject_class = G_OBJECT_CLASS (klass);
133   element_class = GST_ELEMENT_CLASS (klass);
134   basesrc_class = GST_BASE_SRC_CLASS (klass);
135   pushsrc_class = GST_PUSH_SRC_CLASS (klass);
136
137   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2src_finalize;
138   gobject_class->set_property = gst_v4l2src_set_property;
139   gobject_class->get_property = gst_v4l2src_get_property;
140
141   element_class->change_state = gst_v4l2src_change_state;
142
143   gst_v4l2_object_install_properties_helper (gobject_class,
144       DEFAULT_PROP_DEVICE);
145
146   /**
147    * GstV4l2Src::prepare-format:
148    * @v4l2src: the v4l2src instance
149    * @fd: the file descriptor of the current device
150    * @caps: the caps of the format being set
151    *
152    * This signal gets emitted before calling the v4l2 VIDIOC_S_FMT ioctl
153    * (set format). This allows for any custom configuration of the device to
154    * happen prior to the format being set.
155    * This is mostly useful for UVC H264 encoding cameras which need the H264
156    * Probe & Commit to happen prior to the normal Probe & Commit.
157    *
158    * Since: 0.10.32
159    */
160   gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT] = g_signal_new ("prepare-format",
161       G_TYPE_FROM_CLASS (klass),
162       G_SIGNAL_RUN_LAST,
163       0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CAPS);
164
165   gst_element_class_set_static_metadata (element_class,
166       "Video (video4linux2) Source", "Source/Video",
167       "Reads frames from a Video4Linux2 device",
168       "Edgard Lima <edgard.lima@indt.org.br>, "
169       "Stefan Kost <ensonic@users.sf.net>");
170
171   gst_element_class_add_pad_template
172       (element_class,
173       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
174           gst_v4l2_object_get_all_caps ()));
175
176   basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_get_caps);
177   basesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_v4l2src_set_caps);
178   basesrc_class->start = GST_DEBUG_FUNCPTR (gst_v4l2src_start);
179   basesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock);
180   basesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_v4l2src_unlock_stop);
181   basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2src_stop);
182   basesrc_class->query = GST_DEBUG_FUNCPTR (gst_v4l2src_query);
183   basesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_v4l2src_fixate);
184   basesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_v4l2src_negotiate);
185   basesrc_class->decide_allocation =
186       GST_DEBUG_FUNCPTR (gst_v4l2src_decide_allocation);
187
188   pushsrc_class->create = GST_DEBUG_FUNCPTR (gst_v4l2src_create);
189
190   klass->v4l2_class_devices = NULL;
191
192   GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");
193 }
194
195 static void
196 gst_v4l2src_init (GstV4l2Src * v4l2src)
197 {
198   /* fixme: give an update_fps_function */
199   v4l2src->v4l2object = gst_v4l2_object_new (GST_ELEMENT (v4l2src),
200       V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
201       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
202
203   gst_base_src_set_format (GST_BASE_SRC (v4l2src), GST_FORMAT_TIME);
204   gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
205 }
206
207
208 static void
209 gst_v4l2src_finalize (GstV4l2Src * v4l2src)
210 {
211   gst_v4l2_object_destroy (v4l2src->v4l2object);
212
213   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (v4l2src));
214 }
215
216
217 static void
218 gst_v4l2src_set_property (GObject * object,
219     guint prop_id, const GValue * value, GParamSpec * pspec)
220 {
221   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
222
223   if (!gst_v4l2_object_set_property_helper (v4l2src->v4l2object,
224           prop_id, value, pspec)) {
225     switch (prop_id) {
226       default:
227         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228         break;
229     }
230   }
231 }
232
233 static void
234 gst_v4l2src_get_property (GObject * object,
235     guint prop_id, GValue * value, GParamSpec * pspec)
236 {
237   GstV4l2Src *v4l2src = GST_V4L2SRC (object);
238
239   if (!gst_v4l2_object_get_property_helper (v4l2src->v4l2object,
240           prop_id, value, pspec)) {
241     switch (prop_id) {
242       default:
243         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244         break;
245     }
246   }
247 }
248
249 /* this function is a bit of a last resort */
250 static GstCaps *
251 gst_v4l2src_fixate (GstBaseSrc * basesrc, GstCaps * caps)
252 {
253   GstStructure *structure;
254   gint i;
255
256   GST_DEBUG_OBJECT (basesrc, "fixating caps %" GST_PTR_FORMAT, caps);
257
258   caps = gst_caps_make_writable (caps);
259
260   for (i = 0; i < gst_caps_get_size (caps); ++i) {
261     structure = gst_caps_get_structure (caps, i);
262
263     /* We are fixating to a reasonable 320x200 resolution
264        and the maximum framerate resolution for that size */
265     if (gst_structure_has_field (structure, "width"))
266       gst_structure_fixate_field_nearest_int (structure, "width", 320);
267
268     if (gst_structure_has_field (structure, "height"))
269       gst_structure_fixate_field_nearest_int (structure, "height", 200);
270
271     if (gst_structure_has_field (structure, "framerate"))
272       gst_structure_fixate_field_nearest_fraction (structure, "framerate",
273           G_MAXINT, 1);
274
275     if (gst_structure_has_field (structure, "format"))
276       gst_structure_fixate_field (structure, "format");
277   }
278
279   GST_DEBUG_OBJECT (basesrc, "fixated caps %" GST_PTR_FORMAT, caps);
280
281   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
282
283   return caps;
284 }
285
286
287 static gboolean
288 gst_v4l2src_negotiate (GstBaseSrc * basesrc)
289 {
290   GstV4l2Src *v4l2src;
291   GstV4l2Object *obj;
292   GstCaps *thiscaps;
293   GstCaps *caps = NULL;
294   GstCaps *peercaps = NULL;
295   gboolean result = FALSE;
296
297   v4l2src = GST_V4L2SRC (basesrc);
298   obj = v4l2src->v4l2object;
299
300   /* We don't allow renegotiation, just return TRUE in that case */
301   if (GST_V4L2_IS_ACTIVE (obj))
302     return TRUE;
303
304   /* first see what is possible on our source pad */
305   thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
306   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
307   LOG_CAPS (basesrc, thiscaps);
308
309   /* nothing or anything is allowed, we're done */
310   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
311     goto no_nego_needed;
312
313   /* get the peer caps without a filter as we'll filter ourselves later on */
314   peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
315   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
316   LOG_CAPS (basesrc, peercaps);
317   if (peercaps && !gst_caps_is_any (peercaps)) {
318     GstCaps *icaps = NULL;
319     int i;
320
321     /* Prefer the first caps we are compatible with that the peer proposed */
322     for (i = 0; i < gst_caps_get_size (peercaps); i++) {
323       /* get intersection */
324       GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i);
325
326       GST_DEBUG_OBJECT (basesrc, "peer: %" GST_PTR_FORMAT, ipcaps);
327       LOG_CAPS (basesrc, ipcaps);
328
329       icaps = gst_caps_intersect (thiscaps, ipcaps);
330       gst_caps_unref (ipcaps);
331
332       if (!gst_caps_is_empty (icaps))
333         break;
334
335       gst_caps_unref (icaps);
336       icaps = NULL;
337     }
338
339     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
340     LOG_CAPS (basesrc, icaps);
341     if (icaps) {
342       /* If there are multiple intersections pick the one with the smallest
343        * resolution strictly bigger then the first peer caps */
344       if (gst_caps_get_size (icaps) > 1) {
345         GstStructure *s = gst_caps_get_structure (peercaps, 0);
346         int best = 0;
347         int twidth, theight;
348         int width = G_MAXINT, height = G_MAXINT;
349
350         if (gst_structure_get_int (s, "width", &twidth)
351             && gst_structure_get_int (s, "height", &theight)) {
352
353           /* Walk the structure backwards to get the first entry of the
354            * smallest resolution bigger (or equal to) the preferred resolution)
355            */
356           for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) {
357             GstStructure *is = gst_caps_get_structure (icaps, i);
358             int w, h;
359
360             if (gst_structure_get_int (is, "width", &w)
361                 && gst_structure_get_int (is, "height", &h)) {
362               if (w >= twidth && w <= width && h >= theight && h <= height) {
363                 width = w;
364                 height = h;
365                 best = i;
366               }
367             }
368           }
369         }
370
371         caps = gst_caps_copy_nth (icaps, best);
372         gst_caps_unref (icaps);
373       } else {
374         caps = icaps;
375       }
376     }
377     gst_caps_unref (thiscaps);
378   } else {
379     /* no peer or peer have ANY caps, work with our own caps then */
380     caps = thiscaps;
381   }
382   if (peercaps)
383     gst_caps_unref (peercaps);
384   if (caps) {
385     caps = gst_caps_truncate (caps);
386
387     /* now fixate */
388     if (!gst_caps_is_empty (caps)) {
389       caps = gst_v4l2src_fixate (basesrc, caps);
390       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
391       LOG_CAPS (basesrc, caps);
392
393       if (gst_caps_is_any (caps)) {
394         /* hmm, still anything, so element can do anything and
395          * nego is not needed */
396         result = TRUE;
397       } else if (gst_caps_is_fixed (caps)) {
398         /* yay, fixed caps, use those then */
399         result = gst_base_src_set_caps (basesrc, caps);
400       }
401     }
402     gst_caps_unref (caps);
403   }
404   return result;
405
406 no_nego_needed:
407   {
408     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
409     if (thiscaps)
410       gst_caps_unref (thiscaps);
411     return TRUE;
412   }
413 }
414
415 static GstCaps *
416 gst_v4l2src_get_caps (GstBaseSrc * src, GstCaps * filter)
417 {
418   GstV4l2Src *v4l2src;
419   GstV4l2Object *obj;
420
421   v4l2src = GST_V4L2SRC (src);
422   obj = v4l2src->v4l2object;
423
424   if (!GST_V4L2_IS_OPEN (obj)) {
425     return gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (v4l2src));
426   }
427
428   return gst_v4l2_object_get_caps (obj, filter);
429 }
430
431 static gboolean
432 gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
433 {
434   GstV4l2Src *v4l2src;
435   GstV4l2Object *obj;
436
437   v4l2src = GST_V4L2SRC (src);
438   obj = v4l2src->v4l2object;
439
440   /* make sure the caps changed before doing anything */
441   if (gst_v4l2_object_caps_equal (obj, caps))
442     return TRUE;
443
444   /* make sure we stop capturing and dealloc buffers */
445   if (!gst_v4l2_object_stop (obj))
446     return FALSE;
447
448   g_signal_emit (v4l2src, gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT], 0,
449       v4l2src->v4l2object->video_fd, caps);
450
451   if (!gst_v4l2_object_set_format (obj, caps))
452     /* error already posted */
453     return FALSE;
454
455   return TRUE;
456 }
457
458 static gboolean
459 gst_v4l2src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
460 {
461   GstV4l2Src *src = GST_V4L2SRC (bsrc);
462   gboolean ret = FALSE;
463
464   if (gst_v4l2_object_decide_allocation (src->v4l2object, query))
465     ret = GST_BASE_SRC_CLASS (parent_class)->decide_allocation (bsrc, query);
466
467   return ret;
468 }
469
470 static gboolean
471 gst_v4l2src_query (GstBaseSrc * bsrc, GstQuery * query)
472 {
473   GstV4l2Src *src;
474   GstV4l2Object *obj;
475   gboolean res = FALSE;
476
477   src = GST_V4L2SRC (bsrc);
478   obj = src->v4l2object;
479
480   switch (GST_QUERY_TYPE (query)) {
481     case GST_QUERY_LATENCY:{
482       GstClockTime min_latency, max_latency;
483       guint32 fps_n, fps_d;
484       guint num_buffers = 0;
485
486       /* device must be open */
487       if (!GST_V4L2_IS_OPEN (obj)) {
488         GST_WARNING_OBJECT (src,
489             "Can't give latency since device isn't open !");
490         goto done;
491       }
492
493       fps_n = GST_V4L2_FPS_N (obj);
494       fps_d = GST_V4L2_FPS_D (obj);
495
496       /* we must have a framerate */
497       if (fps_n <= 0 || fps_d <= 0) {
498         GST_WARNING_OBJECT (src,
499             "Can't give latency since framerate isn't fixated !");
500         goto done;
501       }
502
503       /* min latency is the time to capture one frame */
504       min_latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
505
506       /* max latency is total duration of the frame buffer */
507       if (obj->pool != NULL)
508         num_buffers = GST_V4L2_BUFFER_POOL_CAST (obj->pool)->num_buffers;
509
510       if (num_buffers == 0)
511         max_latency = -1;
512       else
513         max_latency = num_buffers * min_latency;
514
515       GST_DEBUG_OBJECT (bsrc,
516           "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
517           GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
518
519       /* we are always live, the min latency is 1 frame and the max latency is
520        * the complete buffer of frames. */
521       gst_query_set_latency (query, TRUE, min_latency, max_latency);
522
523       res = TRUE;
524       break;
525     }
526     default:
527       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
528       break;
529   }
530
531 done:
532
533   return res;
534 }
535
536 /* start and stop are not symmetric -- start will open the device, but not start
537  * capture. it's setcaps that will start capture, which is called via basesrc's
538  * negotiate method. stop will both stop capture and close the device.
539  */
540 static gboolean
541 gst_v4l2src_start (GstBaseSrc * src)
542 {
543   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
544
545   v4l2src->offset = 0;
546
547   /* activate settings for first frame */
548   v4l2src->ctrl_time = 0;
549   gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
550
551   return TRUE;
552 }
553
554 static gboolean
555 gst_v4l2src_unlock (GstBaseSrc * src)
556 {
557   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
558   return gst_v4l2_object_unlock (v4l2src->v4l2object);
559 }
560
561 static gboolean
562 gst_v4l2src_unlock_stop (GstBaseSrc * src)
563 {
564   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
565   return gst_v4l2_object_unlock_stop (v4l2src->v4l2object);
566 }
567
568 static gboolean
569 gst_v4l2src_stop (GstBaseSrc * src)
570 {
571   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
572   GstV4l2Object *obj = v4l2src->v4l2object;
573
574   if (GST_V4L2_IS_ACTIVE (obj)) {
575     if (!gst_v4l2_object_stop (obj))
576       return FALSE;
577   }
578   return TRUE;
579 }
580
581 static GstStateChangeReturn
582 gst_v4l2src_change_state (GstElement * element, GstStateChange transition)
583 {
584   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
585   GstV4l2Src *v4l2src = GST_V4L2SRC (element);
586   GstV4l2Object *obj = v4l2src->v4l2object;
587
588   switch (transition) {
589     case GST_STATE_CHANGE_NULL_TO_READY:
590       /* open the device */
591       if (!gst_v4l2_object_open (obj))
592         return GST_STATE_CHANGE_FAILURE;
593       break;
594     default:
595       break;
596   }
597
598   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
599
600   switch (transition) {
601     case GST_STATE_CHANGE_READY_TO_NULL:
602       /* close the device */
603       if (!gst_v4l2_object_close (obj))
604         return GST_STATE_CHANGE_FAILURE;
605
606       break;
607     default:
608       break;
609   }
610
611   return ret;
612 }
613
614 static GstFlowReturn
615 gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
616 {
617   GstV4l2Src *v4l2src = GST_V4L2SRC (src);
618   GstV4l2Object *obj = v4l2src->v4l2object;
619   GstFlowReturn ret;
620   GstClock *clock;
621   GstClockTime abs_time, base_time, timestamp, duration;
622   GstClockTime delay;
623
624   ret = GST_BASE_SRC_CLASS (parent_class)->alloc (GST_BASE_SRC (src), 0,
625       obj->sizeimage, buf);
626
627   if (G_UNLIKELY (ret != GST_FLOW_OK))
628     goto alloc_failed;
629
630   ret =
631       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL_CAST (obj->pool), buf);
632
633   if (G_UNLIKELY (ret != GST_FLOW_OK))
634     goto error;
635
636   timestamp = GST_BUFFER_TIMESTAMP (*buf);
637   duration = obj->duration;
638
639   /* timestamps, LOCK to get clock and base time. */
640   /* FIXME: element clock and base_time is rarely changing */
641   GST_OBJECT_LOCK (v4l2src);
642   if ((clock = GST_ELEMENT_CLOCK (v4l2src))) {
643     /* we have a clock, get base time and ref clock */
644     base_time = GST_ELEMENT (v4l2src)->base_time;
645     gst_object_ref (clock);
646   } else {
647     /* no clock, can't set timestamps */
648     base_time = GST_CLOCK_TIME_NONE;
649   }
650   GST_OBJECT_UNLOCK (v4l2src);
651
652   /* sample pipeline clock */
653   if (clock) {
654     abs_time = gst_clock_get_time (clock);
655     gst_object_unref (clock);
656   } else {
657     abs_time = GST_CLOCK_TIME_NONE;
658   }
659
660   if (timestamp != GST_CLOCK_TIME_NONE) {
661     struct timespec now;
662     GstClockTime gstnow;
663
664     /* v4l2 specs say to use the system time although many drivers switched to
665      * the more desirable monotonic time. We first try to use the monotonic time
666      * and see how that goes */
667     clock_gettime (CLOCK_MONOTONIC, &now);
668     gstnow = GST_TIMESPEC_TO_TIME (now);
669
670     if (gstnow < timestamp && (timestamp - gstnow) > (10 * GST_SECOND)) {
671       GTimeVal now;
672
673       /* very large diff, fall back to system time */
674       g_get_current_time (&now);
675       gstnow = GST_TIMEVAL_TO_TIME (now);
676     }
677
678     if (gstnow > timestamp) {
679       delay = gstnow - timestamp;
680     } else {
681       delay = 0;
682     }
683
684     GST_DEBUG_OBJECT (v4l2src, "ts: %" GST_TIME_FORMAT " now %" GST_TIME_FORMAT
685         " delay %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
686         GST_TIME_ARGS (gstnow), GST_TIME_ARGS (delay));
687   } else {
688     /* we assume 1 frame latency otherwise */
689     if (GST_CLOCK_TIME_IS_VALID (duration))
690       delay = duration;
691     else
692       delay = 0;
693   }
694
695   /* set buffer metadata */
696   GST_BUFFER_OFFSET (*buf) = v4l2src->offset++;
697   GST_BUFFER_OFFSET_END (*buf) = v4l2src->offset;
698
699   if (G_LIKELY (abs_time != GST_CLOCK_TIME_NONE)) {
700     /* the time now is the time of the clock minus the base time */
701     timestamp = abs_time - base_time;
702
703     /* adjust for delay in the device */
704     if (timestamp > delay)
705       timestamp -= delay;
706     else
707       timestamp = 0;
708   } else {
709     timestamp = GST_CLOCK_TIME_NONE;
710   }
711
712   /* activate settings for next frame */
713   if (GST_CLOCK_TIME_IS_VALID (duration)) {
714     v4l2src->ctrl_time += duration;
715   } else {
716     /* this is not very good (as it should be the next timestamp),
717      * still good enough for linear fades (as long as it is not -1)
718      */
719     v4l2src->ctrl_time = timestamp;
720   }
721   gst_object_sync_values (GST_OBJECT (src), v4l2src->ctrl_time);
722
723   GST_INFO_OBJECT (src, "sync to %" GST_TIME_FORMAT " out ts %" GST_TIME_FORMAT,
724       GST_TIME_ARGS (v4l2src->ctrl_time), GST_TIME_ARGS (timestamp));
725
726   GST_BUFFER_TIMESTAMP (*buf) = timestamp;
727   GST_BUFFER_DURATION (*buf) = duration;
728
729   return ret;
730
731   /* ERROR */
732 alloc_failed:
733   {
734     if (ret != GST_FLOW_FLUSHING)
735       GST_ELEMENT_ERROR (src, RESOURCE, NO_SPACE_LEFT,
736           ("Failed to allocate a buffer"), (NULL));
737     return ret;
738   }
739 error:
740   {
741     GST_DEBUG_OBJECT (src, "error processing buffer %d (%s)", ret,
742         gst_flow_get_name (ret));
743     return ret;
744   }
745 }
746
747
748 /* GstURIHandler interface */
749 static GstURIType
750 gst_v4l2src_uri_get_type (GType type)
751 {
752   return GST_URI_SRC;
753 }
754
755 static const gchar *const *
756 gst_v4l2src_uri_get_protocols (GType type)
757 {
758   static const gchar *protocols[] = { "v4l2", NULL };
759
760   return protocols;
761 }
762
763 static gchar *
764 gst_v4l2src_uri_get_uri (GstURIHandler * handler)
765 {
766   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
767
768   if (v4l2src->v4l2object->videodev != NULL) {
769     return g_strdup_printf ("v4l2://%s", v4l2src->v4l2object->videodev);
770   }
771
772   return g_strdup ("v4l2://");
773 }
774
775 static gboolean
776 gst_v4l2src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
777     GError ** error)
778 {
779   GstV4l2Src *v4l2src = GST_V4L2SRC (handler);
780   const gchar *device = DEFAULT_PROP_DEVICE;
781
782   if (strcmp (uri, "v4l2://") != 0) {
783     device = uri + 7;
784   }
785   g_object_set (v4l2src, "device", device, NULL);
786
787   return TRUE;
788 }
789
790
791 static void
792 gst_v4l2src_uri_handler_init (gpointer g_iface, gpointer iface_data)
793 {
794   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
795
796   iface->get_type = gst_v4l2src_uri_get_type;
797   iface->get_protocols = gst_v4l2src_uri_get_protocols;
798   iface->get_uri = gst_v4l2src_uri_get_uri;
799   iface->set_uri = gst_v4l2src_uri_set_uri;
800 }