gstv4l2bufferpool: handle -EPIPE from DQBUF to signal EOS
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.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  *               2009 Texas Instruments, Inc - http://www.ti.com/
6  *
7  * gstv4l2bufferpool.c V4L2 buffer pool class
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifndef _GNU_SOURCE
30 # define _GNU_SOURCE            /* O_CLOEXEC */
31 #endif
32 #include <fcntl.h>
33
34 #include <sys/mman.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "gst/video/video.h"
39 #include "gst/video/gstvideometa.h"
40 #include "gst/video/gstvideopool.h"
41 #include "gst/allocators/gstdmabuf.h"
42
43 #include <gstv4l2bufferpool.h>
44
45 #include "v4l2_calls.h"
46 #include "gst/gst-i18n-plugin.h"
47 #include <gst/glib-compat-private.h>
48
49 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
50 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2bufferpool_debug
52
53 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
54
55
56 /*
57  * GstV4l2BufferPool:
58  */
59 #define gst_v4l2_buffer_pool_parent_class parent_class
60 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
61
62 enum _GstV4l2BufferPoolAcquireFlags
63 {
64   GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
65       GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
66   GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
67 };
68
69 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
70     GstBuffer * buffer);
71
72 static gboolean
73 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
74 {
75   GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
76   gboolean valid = FALSE;
77
78   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
79     goto done;
80
81   if (gst_is_dmabuf_memory (mem))
82     mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
83         GST_V4L2_MEMORY_QUARK);
84
85   if (mem && gst_is_v4l2_memory (mem)) {
86     GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
87     GstV4l2MemoryGroup *group = vmem->group;
88     gint i;
89
90     if (group->n_mem != gst_buffer_n_memory (buffer))
91       goto done;
92
93     for (i = 0; i < group->n_mem; i++) {
94       if (group->mem[i] != gst_buffer_peek_memory (buffer, i))
95         goto done;
96
97       if (!gst_memory_is_writable (group->mem[i]))
98         goto done;
99     }
100
101     valid = TRUE;
102     if (out_group)
103       *out_group = group;
104   }
105
106 done:
107   return valid;
108 }
109
110 static GstFlowReturn
111 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
112     GstBuffer * src)
113 {
114   const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
115
116   GST_LOG_OBJECT (pool, "copying buffer");
117
118   if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
119           finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
120     GstVideoFrame src_frame, dest_frame;
121
122     GST_DEBUG_OBJECT (pool, "copy video frame");
123
124     /* we have raw video, use videoframe copy to get strides right */
125     if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
126       goto invalid_buffer;
127
128     if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
129             GST_MAP_WRITE)) {
130       gst_video_frame_unmap (&src_frame);
131       goto invalid_buffer;
132     }
133
134     gst_video_frame_copy (&dest_frame, &src_frame);
135
136     gst_video_frame_unmap (&src_frame);
137     gst_video_frame_unmap (&dest_frame);
138   } else {
139     GstMapInfo map;
140
141     GST_DEBUG_OBJECT (pool, "copy raw bytes");
142
143     if (!gst_buffer_map (src, &map, GST_MAP_READ))
144       goto invalid_buffer;
145
146     gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
147
148     gst_buffer_unmap (src, &map);
149     gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
150   }
151
152   GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
153       dest);
154
155   return GST_FLOW_OK;
156
157 invalid_buffer:
158   {
159     GST_ERROR_OBJECT (pool, "could not map buffer");
160     return GST_FLOW_ERROR;
161   }
162 }
163
164 struct UserPtrData
165 {
166   GstBuffer *buffer;
167   gboolean is_frame;
168   GstVideoFrame frame;
169   GstMapInfo map;
170 };
171
172 static GQuark
173 gst_v4l2_buffer_pool_import_quark (void)
174 {
175   static GQuark quark = 0;
176
177   if (quark == 0)
178     quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
179
180   return quark;
181 }
182
183 static void
184 _unmap_userptr_frame (struct UserPtrData *data)
185 {
186   if (data->is_frame)
187     gst_video_frame_unmap (&data->frame);
188   else
189     gst_buffer_unmap (data->buffer, &data->map);
190
191   if (data->buffer)
192     gst_buffer_unref (data->buffer);
193
194   g_slice_free (struct UserPtrData, data);
195 }
196
197 static GstFlowReturn
198 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
199     GstBuffer * dest, GstBuffer * src)
200 {
201   GstFlowReturn ret = GST_FLOW_OK;
202   GstV4l2MemoryGroup *group = NULL;
203   GstMapFlags flags;
204   const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
205   struct UserPtrData *data = NULL;
206
207   GST_LOG_OBJECT (pool, "importing userptr");
208
209   /* get the group */
210   if (!gst_v4l2_is_buffer_valid (dest, &group))
211     goto not_our_buffer;
212
213   if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
214     flags = GST_MAP_READ;
215   else
216     flags = GST_MAP_WRITE;
217
218   data = g_slice_new0 (struct UserPtrData);
219
220   if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
221           finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
222     gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
223     gint i;
224
225     data->is_frame = TRUE;
226
227     if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
228       goto invalid_buffer;
229
230     for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
231       if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
232         gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
233         gint pstride;
234         guint pheight;
235
236         pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
237             GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
238
239         pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
240             GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
241
242         size[i] = pstride * pheight;
243       } else {
244         size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
245             GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
246       }
247     }
248
249     /* In the single planar API, planes must be contiguous in memory and
250      * therefore they must have expected size. ie: no padding.
251      * To check these conditions, we check that plane 'i' start address
252      * + plane 'i' size equals to plane 'i+1' start address */
253     if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
254       for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
255         const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
256         gpointer tmp;
257         gint estride = gst_v4l2_object_extrapolate_stride (finfo, i,
258             pix_fmt->bytesperline);
259         guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
260             pix_fmt->height);
261
262         tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
263         if (tmp != data->frame.data[i + 1])
264           goto non_contiguous_mem;
265       }
266     }
267
268     if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
269             data->frame.info.size, finfo->n_planes, data->frame.data, size))
270       goto import_failed;
271   } else {
272     gpointer ptr[1];
273     gsize size[1];
274
275     data->is_frame = FALSE;
276
277     if (!gst_buffer_map (src, &data->map, flags))
278       goto invalid_buffer;
279
280     ptr[0] = data->map.data;
281     size[0] = data->map.size;
282
283     if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
284             data->map.size, 1, ptr, size))
285       goto import_failed;
286   }
287
288   data->buffer = gst_buffer_ref (src);
289
290   gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
291       data, (GDestroyNotify) _unmap_userptr_frame);
292
293   return ret;
294
295 not_our_buffer:
296   {
297     GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
298     return GST_FLOW_ERROR;
299   }
300 invalid_buffer:
301   {
302     GST_ERROR_OBJECT (pool, "could not map buffer");
303     g_slice_free (struct UserPtrData, data);
304     return GST_FLOW_ERROR;
305   }
306 non_contiguous_mem:
307   {
308     GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
309     _unmap_userptr_frame (data);
310     return GST_FLOW_ERROR;
311   }
312 import_failed:
313   {
314     GST_ERROR_OBJECT (pool, "failed to import data");
315     _unmap_userptr_frame (data);
316     return GST_FLOW_ERROR;
317   }
318 }
319
320 static GstFlowReturn
321 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
322     GstBuffer * dest, GstBuffer * src)
323 {
324   GstV4l2MemoryGroup *group = NULL;
325   GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
326   guint n_mem = gst_buffer_n_memory (src);
327   gint i;
328
329   GST_LOG_OBJECT (pool, "importing dmabuf");
330
331   if (!gst_v4l2_is_buffer_valid (dest, &group))
332     goto not_our_buffer;
333
334   if (n_mem > GST_VIDEO_MAX_PLANES)
335     goto too_many_mems;
336
337   for (i = 0; i < n_mem; i++)
338     dma_mem[i] = gst_buffer_peek_memory (src, i);
339
340   if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
341           dma_mem))
342     goto import_failed;
343
344   gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
345       gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
346
347   return GST_FLOW_OK;
348
349 not_our_buffer:
350   {
351     GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
352     return GST_FLOW_ERROR;
353   }
354 too_many_mems:
355   {
356     GST_ERROR_OBJECT (pool, "could not map buffer");
357     return GST_FLOW_ERROR;
358   }
359 import_failed:
360   {
361     GST_ERROR_OBJECT (pool, "failed to import dmabuf");
362     return GST_FLOW_ERROR;
363   }
364 }
365
366 static GstFlowReturn
367 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
368     GstBuffer * dest, GstBuffer * src)
369 {
370   GstFlowReturn ret = GST_FLOW_OK;
371   gboolean own_src = FALSE;
372
373   if (src == NULL) {
374     if (pool->other_pool == NULL) {
375       GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
376       return GST_FLOW_ERROR;
377     }
378
379     ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
380     if (ret != GST_FLOW_OK) {
381       GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
382       goto done;
383     }
384
385     own_src = TRUE;
386   }
387
388   switch (pool->obj->mode) {
389     case GST_V4L2_IO_MMAP:
390     case GST_V4L2_IO_DMABUF:
391       ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
392       break;
393     case GST_V4L2_IO_USERPTR:
394       ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
395       break;
396     case GST_V4L2_IO_DMABUF_IMPORT:
397       ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
398       break;
399     default:
400       break;
401   }
402
403   if (own_src)
404     gst_buffer_unref (src);
405
406 done:
407   return ret;
408 }
409
410 static GstFlowReturn
411 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
412     GstBufferPoolAcquireParams * params)
413 {
414   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
415   GstV4l2MemoryGroup *group = NULL;
416   GstBuffer *newbuf = NULL;
417   GstV4l2Object *obj;
418   GstVideoInfo *info;
419
420   obj = pool->obj;
421   info = &obj->info;
422
423   switch (obj->mode) {
424     case GST_V4L2_IO_RW:
425       newbuf =
426           gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
427       break;
428     case GST_V4L2_IO_MMAP:
429       group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
430       break;
431     case GST_V4L2_IO_DMABUF:
432       group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
433           pool->allocator);
434       break;
435     case GST_V4L2_IO_USERPTR:
436       group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
437       break;
438     case GST_V4L2_IO_DMABUF_IMPORT:
439       group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
440       break;
441     default:
442       newbuf = NULL;
443       g_assert_not_reached ();
444       break;
445   }
446
447   if (group != NULL) {
448     gint i;
449     newbuf = gst_buffer_new ();
450
451     for (i = 0; i < group->n_mem; i++)
452       gst_buffer_append_memory (newbuf, group->mem[i]);
453   } else if (newbuf == NULL) {
454     goto allocation_failed;
455   }
456
457   /* add metadata to raw video buffers */
458   if (pool->add_videometa)
459     gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
460         GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
461         GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
462         info->offset, info->stride);
463
464   *buffer = newbuf;
465
466   return GST_FLOW_OK;
467
468   /* ERRORS */
469 allocation_failed:
470   {
471     GST_ERROR_OBJECT (pool, "failed to allocate buffer");
472     return GST_FLOW_ERROR;
473   }
474 }
475
476 static gboolean
477 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
478 {
479   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
480   GstV4l2Object *obj = pool->obj;
481   GstCaps *caps;
482   guint size, min_buffers, max_buffers;
483   GstAllocator *allocator;
484   GstAllocationParams params;
485   gboolean can_allocate = FALSE;
486   gboolean updated = FALSE;
487   gboolean ret;
488
489   pool->add_videometa =
490       gst_buffer_pool_config_has_option (config,
491       GST_BUFFER_POOL_OPTION_VIDEO_META);
492
493   /* parse the config and keep around */
494   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
495           &max_buffers))
496     goto wrong_config;
497
498   if (!gst_buffer_pool_config_get_allocator (config, &allocator, &params))
499     goto wrong_config;
500
501   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
502
503   if (pool->allocator)
504     gst_object_unref (pool->allocator);
505   pool->allocator = NULL;
506
507   switch (obj->mode) {
508     case GST_V4L2_IO_DMABUF:
509       pool->allocator = gst_dmabuf_allocator_new ();
510       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
511       break;
512     case GST_V4L2_IO_MMAP:
513       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
514       break;
515     case GST_V4L2_IO_USERPTR:
516       can_allocate =
517           GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
518       break;
519     case GST_V4L2_IO_DMABUF_IMPORT:
520       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
521       break;
522     case GST_V4L2_IO_RW:
523       if (allocator)
524         pool->allocator = g_object_ref (allocator);
525       pool->params = params;
526       /* No need to change the configuration */
527       goto done;
528       break;
529     default:
530       g_assert_not_reached ();
531       break;
532   }
533
534   /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
535    * instability and crash, disable it for now */
536   if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
537     GST_WARNING_OBJECT (pool,
538         "libv4l2 converter detected, disabling CREATE_BUFS");
539     can_allocate = FALSE;
540     GST_OBJECT_FLAG_UNSET (pool->vallocator,
541         GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
542         | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
543         | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
544   }
545
546   if (min_buffers < GST_V4L2_MIN_BUFFERS) {
547     updated = TRUE;
548     min_buffers = GST_V4L2_MIN_BUFFERS;
549     GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
550   }
551
552   /* respect driver requirements */
553   if (min_buffers < obj->min_buffers) {
554     updated = TRUE;
555     min_buffers = obj->min_buffers;
556     GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
557   }
558
559   if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
560     updated = TRUE;
561     max_buffers = VIDEO_MAX_FRAME;
562     GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
563   }
564
565   if (min_buffers > max_buffers) {
566     updated = TRUE;
567     min_buffers = max_buffers;
568     GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
569   } else if (min_buffers != max_buffers) {
570     if (!can_allocate) {
571       updated = TRUE;
572       max_buffers = min_buffers;
573       GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
574     }
575   }
576
577   if (!pool->add_videometa && obj->need_video_meta) {
578     GST_INFO_OBJECT (pool, "adding needed video meta");
579     updated = TRUE;
580     gst_buffer_pool_config_add_option (config,
581         GST_BUFFER_POOL_OPTION_VIDEO_META);
582   }
583
584   /* Always update the config to ensure the configured size matches */
585   gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
586       max_buffers);
587
588   /* keep a GstVideoInfo with defaults for the when we need to copy */
589   gst_video_info_from_caps (&pool->caps_info, caps);
590
591 done:
592   ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
593
594   /* If anything was changed documentation recommand to return FALSE */
595   return !updated && ret;
596
597   /* ERRORS */
598 wrong_config:
599   {
600     GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
601     return FALSE;
602   }
603 }
604
605 static gboolean
606 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
607 {
608   GstV4l2Object *obj = pool->obj;
609
610   switch (obj->mode) {
611     case GST_V4L2_IO_MMAP:
612     case GST_V4L2_IO_USERPTR:
613     case GST_V4L2_IO_DMABUF:
614     case GST_V4L2_IO_DMABUF_IMPORT:
615       if (!pool->streaming) {
616         if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
617           goto streamon_failed;
618
619         pool->streaming = TRUE;
620
621         GST_DEBUG_OBJECT (pool, "Started streaming");
622       }
623       break;
624     default:
625       break;
626   }
627
628   return TRUE;
629
630 streamon_failed:
631   {
632     GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
633         g_strerror (errno));
634     return FALSE;
635   }
636 }
637
638 static void
639 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
640 {
641   GstV4l2Object *obj = pool->obj;
642
643   switch (obj->mode) {
644     case GST_V4L2_IO_MMAP:
645     case GST_V4L2_IO_USERPTR:
646     case GST_V4L2_IO_DMABUF:
647     case GST_V4L2_IO_DMABUF_IMPORT:
648       if (pool->streaming) {
649         if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
650           GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
651               errno, g_strerror (errno));
652
653         pool->streaming = FALSE;
654
655         GST_DEBUG_OBJECT (pool, "Stopped streaming");
656
657         if (pool->vallocator)
658           gst_v4l2_allocator_flush (pool->vallocator);
659       }
660       break;
661     default:
662       break;
663   }
664 }
665
666 static GstFlowReturn
667 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
668 {
669   GstBufferPoolAcquireParams params = { 0 };
670   GstBuffer *buffer = NULL;
671   GstFlowReturn ret;
672
673   GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
674
675   params.flags =
676       (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
677   ret =
678       gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, &params);
679
680   if (ret == GST_FLOW_OK)
681     gst_buffer_unref (buffer);
682
683   return ret;
684 }
685
686 static gboolean
687 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
688 {
689   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
690   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
691   GstV4l2Object *obj = pool->obj;
692   GstStructure *config;
693   GstCaps *caps;
694   guint size, min_buffers, max_buffers;
695   guint max_latency, min_latency, copy_threshold = 0;
696   gboolean can_allocate = FALSE;
697
698   GST_DEBUG_OBJECT (pool, "activating pool");
699
700   config = gst_buffer_pool_get_config (bpool);
701   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
702           &max_buffers))
703     goto wrong_config;
704
705   min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
706
707   switch (obj->mode) {
708     case GST_V4L2_IO_RW:
709       can_allocate = TRUE;
710 #ifdef HAVE_LIBV4L2
711       /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
712        * of MMAP. In this case, the first read initialize the queues, but the
713        * poll before that will always fail. Doing an empty read, forces the
714        * queue to be initialized now. We only do this if we have a streaming
715        * driver. */
716       if (obj->vcap.capabilities & V4L2_CAP_STREAMING)
717         v4l2_read (obj->video_fd, NULL, 0);
718 #endif
719       break;
720     case GST_V4L2_IO_DMABUF:
721     case GST_V4L2_IO_MMAP:
722     {
723       guint count;
724
725       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
726
727       /* first, lets request buffers, and see how many we can get: */
728       GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
729
730       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
731           V4L2_MEMORY_MMAP);
732
733       if (count < GST_V4L2_MIN_BUFFERS) {
734         min_buffers = count;
735         goto no_buffers;
736       }
737
738       /* V4L2 buffer pool are often very limited in the amount of buffers it
739        * can offer. The copy_threshold will workaround this limitation by
740        * falling back to copy if the pipeline needed more buffers. This also
741        * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
742        * called. */
743       if (count != min_buffers || pool->enable_copy_threshold) {
744         GST_WARNING_OBJECT (pool,
745             "Uncertain or not enough buffers, enabling copy threshold");
746         min_buffers = count;
747         copy_threshold = min_latency;
748       }
749
750       break;
751     }
752     case GST_V4L2_IO_USERPTR:
753     {
754       guint count;
755
756       can_allocate =
757           GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
758
759       GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
760
761       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
762           V4L2_MEMORY_USERPTR);
763
764       /* There is no rational to not get what we asked */
765       if (count < min_buffers) {
766         min_buffers = count;
767         goto no_buffers;
768       }
769
770       min_buffers = count;
771       break;
772     }
773     case GST_V4L2_IO_DMABUF_IMPORT:
774     {
775       guint count;
776
777       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
778
779       GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
780
781       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
782           V4L2_MEMORY_DMABUF);
783
784       /* There is no rational to not get what we asked */
785       if (count < min_buffers) {
786         min_buffers = count;
787         goto no_buffers;
788       }
789
790       min_buffers = count;
791       break;
792     }
793     default:
794       min_buffers = 0;
795       copy_threshold = 0;
796       g_assert_not_reached ();
797       break;
798   }
799
800   if (can_allocate)
801     max_latency = max_buffers;
802   else
803     max_latency = min_buffers;
804
805   pool->size = size;
806   pool->copy_threshold = copy_threshold;
807   pool->max_latency = max_latency;
808   pool->min_latency = min_latency;
809   pool->num_queued = 0;
810
811   if (max_buffers != 0 && max_buffers < min_buffers)
812     max_buffers = min_buffers;
813
814   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
815       max_buffers);
816   pclass->set_config (bpool, config);
817   gst_structure_free (config);
818
819   if (pool->other_pool)
820     if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
821       goto other_pool_failed;
822
823   /* now, allocate the buffers: */
824   if (!pclass->start (bpool))
825     goto start_failed;
826
827   if (!V4L2_TYPE_IS_OUTPUT (obj->type))
828     pool->group_released_handler =
829         g_signal_connect_swapped (pool->vallocator, "group-released",
830         G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
831
832   return TRUE;
833
834   /* ERRORS */
835 wrong_config:
836   {
837     GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
838     gst_structure_free (config);
839     return FALSE;
840   }
841 no_buffers:
842   {
843     GST_ERROR_OBJECT (pool,
844         "we received %d buffer from device '%s', we want at least %d",
845         min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
846     gst_structure_free (config);
847     return FALSE;
848   }
849 start_failed:
850   {
851     GST_ERROR_OBJECT (pool, "failed to start streaming");
852     return FALSE;
853   }
854 other_pool_failed:
855   {
856     GST_ERROR_OBJECT (pool, "failed to active the other pool %"
857         GST_PTR_FORMAT, pool->other_pool);
858     return FALSE;
859   }
860 }
861
862 static gboolean
863 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
864 {
865   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
866   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
867   gboolean ret;
868   gint i;
869
870   GST_DEBUG_OBJECT (pool, "stopping pool");
871
872   if (pool->group_released_handler > 0) {
873     g_signal_handler_disconnect (pool->vallocator,
874         pool->group_released_handler);
875     pool->group_released_handler = 0;
876   }
877
878   if (pool->other_pool) {
879     gst_buffer_pool_set_active (pool->other_pool, FALSE);
880     gst_object_unref (pool->other_pool);
881     pool->other_pool = NULL;
882   }
883
884   gst_v4l2_buffer_pool_streamoff (pool);
885
886   for (i = 0; i < VIDEO_MAX_FRAME; i++) {
887     if (pool->buffers[i]) {
888       GstBuffer *buffer = pool->buffers[i];
889
890       pool->buffers[i] = NULL;
891
892       if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
893         gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
894       else                      /* Don't re-enqueue capture buffer on stop */
895         pclass->release_buffer (bpool, buffer);
896
897       g_atomic_int_add (&pool->num_queued, -1);
898     }
899   }
900
901   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
902
903   if (ret && pool->vallocator) {
904     GstV4l2Return vret;
905
906     vret = gst_v4l2_allocator_stop (pool->vallocator);
907
908     if (vret == GST_V4L2_BUSY)
909       GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
910
911     ret = (vret == GST_V4L2_OK);
912   }
913
914   return ret;
915 }
916
917 static void
918 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
919 {
920   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
921
922   GST_DEBUG_OBJECT (pool, "start flushing");
923
924   gst_poll_set_flushing (pool->poll, TRUE);
925
926   GST_OBJECT_LOCK (pool);
927   pool->empty = FALSE;
928   g_cond_broadcast (&pool->empty_cond);
929   GST_OBJECT_UNLOCK (pool);
930
931   if (pool->other_pool)
932     gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
933 }
934
935 static void
936 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
937 {
938   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
939   GstV4l2Object *obj = pool->obj;
940   gint i;
941
942   GST_DEBUG_OBJECT (pool, "stop flushing");
943
944   /* If we haven't started streaming yet, simply call streamon */
945   if (!pool->streaming)
946     goto streamon;
947
948   if (pool->other_pool)
949     gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
950
951   gst_v4l2_buffer_pool_streamoff (pool);
952
953   /* Reset our state */
954   switch (obj->mode) {
955     case GST_V4L2_IO_RW:
956       break;
957     case GST_V4L2_IO_MMAP:
958     case GST_V4L2_IO_USERPTR:
959     case GST_V4L2_IO_DMABUF:
960     case GST_V4L2_IO_DMABUF_IMPORT:
961     {
962       for (i = 0; i < VIDEO_MAX_FRAME; i++) {
963         /* Re-enqueue buffers */
964         if (pool->buffers[i]) {
965           GstBufferPool *bpool = (GstBufferPool *) pool;
966           GstBuffer *buffer = pool->buffers[i];
967
968           pool->buffers[i] = NULL;
969
970           /* Remove qdata, this will unmap any map data in
971            * userptr/dmabuf-import */
972           gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
973               GST_V4L2_IMPORT_QUARK, NULL, NULL);
974
975           if (buffer->pool == NULL)
976             gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
977
978           g_atomic_int_add (&pool->num_queued, -1);
979         }
980       }
981
982       break;
983     }
984     default:
985       g_assert_not_reached ();
986       break;
987   }
988
989 streamon:
990   /* Start streaming on capture device only */
991   if (!V4L2_TYPE_IS_OUTPUT (obj->type))
992     gst_v4l2_buffer_pool_streamon (pool);
993
994   gst_poll_set_flushing (pool->poll, FALSE);
995 }
996
997 static GstFlowReturn
998 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
999 {
1000   gint ret;
1001
1002   /* In RW mode there is no queue, hence no need to wait while the queue is
1003    * empty */
1004   if (pool->obj->mode != GST_V4L2_IO_RW) {
1005     GST_OBJECT_LOCK (pool);
1006     while (pool->empty)
1007       g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1008     GST_OBJECT_UNLOCK (pool);
1009   }
1010
1011   if (!pool->can_poll_device)
1012     goto done;
1013
1014   GST_LOG_OBJECT (pool, "polling device");
1015
1016 again:
1017   ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
1018   if (G_UNLIKELY (ret < 0)) {
1019     switch (errno) {
1020       case EBUSY:
1021         goto stopped;
1022       case EAGAIN:
1023       case EINTR:
1024         goto again;
1025       case ENXIO:
1026         GST_WARNING_OBJECT (pool,
1027             "v4l2 device doesn't support polling. Disabling"
1028             " using libv4l2 in this case may cause deadlocks");
1029         pool->can_poll_device = FALSE;
1030         goto done;
1031       default:
1032         goto select_error;
1033     }
1034   }
1035
1036   if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1037     goto select_error;
1038
1039 done:
1040   return GST_FLOW_OK;
1041
1042   /* ERRORS */
1043 stopped:
1044   {
1045     GST_DEBUG_OBJECT (pool, "stop called");
1046     return GST_FLOW_FLUSHING;
1047   }
1048 select_error:
1049   {
1050     GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1051         ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1052     return GST_FLOW_ERROR;
1053   }
1054 }
1055
1056 static GstFlowReturn
1057 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1058 {
1059   GstV4l2MemoryGroup *group = NULL;
1060   const GstV4l2Object *obj = pool->obj;
1061   gint index;
1062
1063   if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1064     GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1065     gst_buffer_unref (buf);
1066     return GST_FLOW_OK;
1067   }
1068
1069   index = group->buffer.index;
1070
1071   if (pool->buffers[index] != NULL)
1072     goto already_queued;
1073
1074   GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1075
1076   g_atomic_int_inc (&pool->num_queued);
1077   pool->buffers[index] = buf;
1078
1079   if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1080     enum v4l2_field field;
1081
1082     /* Except when field is set to alternate, buffer field is the same as
1083      * the one defined in format */
1084     if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1085       field = obj->format.fmt.pix_mp.field;
1086     else
1087       field = obj->format.fmt.pix.field;
1088
1089     /* NB: At this moment, we can't have alternate mode because it not handled
1090      * yet */
1091     if (field == V4L2_FIELD_ALTERNATE) {
1092       if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1093         field = V4L2_FIELD_TOP;
1094       else
1095         field = V4L2_FIELD_BOTTOM;
1096     }
1097
1098     group->buffer.field = field;
1099   }
1100
1101   if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1102     goto queue_failed;
1103
1104   GST_OBJECT_LOCK (pool);
1105   pool->empty = FALSE;
1106   g_cond_signal (&pool->empty_cond);
1107   GST_OBJECT_UNLOCK (pool);
1108
1109   return GST_FLOW_OK;
1110
1111 already_queued:
1112   {
1113     GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1114     return GST_FLOW_ERROR;
1115   }
1116 queue_failed:
1117   {
1118     GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1119     /* Mark broken buffer to the allocator */
1120     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1121     g_atomic_int_add (&pool->num_queued, -1);
1122     pool->buffers[index] = NULL;
1123     return GST_FLOW_ERROR;
1124   }
1125 }
1126
1127 static GstFlowReturn
1128 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1129 {
1130   GstFlowReturn res;
1131   GstBuffer *outbuf;
1132   GstV4l2Object *obj = pool->obj;
1133   GstClockTime timestamp;
1134   GstV4l2MemoryGroup *group;
1135   gint i;
1136
1137   if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1138     goto poll_failed;
1139
1140   GST_LOG_OBJECT (pool, "dequeueing a buffer");
1141
1142   res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
1143   if (res == GST_FLOW_EOS)
1144     goto eos;
1145   if (res != GST_FLOW_OK)
1146     goto dqbuf_failed;
1147
1148   /* get our GstBuffer with that index from the pool, if the buffer was
1149    * outstanding we have a serious problem.
1150    */
1151   outbuf = pool->buffers[group->buffer.index];
1152   if (outbuf == NULL)
1153     goto no_buffer;
1154
1155   /* mark the buffer outstanding */
1156   pool->buffers[group->buffer.index] = NULL;
1157   if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1158     GST_OBJECT_LOCK (pool);
1159     pool->empty = TRUE;
1160     GST_OBJECT_UNLOCK (pool);
1161   }
1162
1163   timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1164
1165 #ifndef GST_DISABLE_GST_DEBUG
1166   for (i = 0; i < group->n_mem; i++) {
1167     GST_LOG_OBJECT (pool,
1168         "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1169         GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1170         group->buffer.sequence, group->buffer.index, group->mem[i],
1171         group->planes[i].bytesused, i, group->buffer.flags,
1172         GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1173   }
1174 #endif
1175
1176   /* Ignore timestamp and field for OUTPUT device */
1177   if (V4L2_TYPE_IS_OUTPUT (obj->type))
1178     goto done;
1179
1180   /* Check for driver bug in reporting feild */
1181   if (group->buffer.field == V4L2_FIELD_ANY) {
1182     /* Only warn once to avoid the spamming */
1183 #ifndef GST_DISABLE_GST_DEBUG
1184     if (!pool->has_warned_on_buggy_field) {
1185       pool->has_warned_on_buggy_field = TRUE;
1186       GST_WARNING_OBJECT (pool,
1187           "Driver should never set v4l2_buffer.field to ANY");
1188     }
1189 #endif
1190
1191     /* Use the value from the format (works for UVC bug) */
1192     group->buffer.field = obj->format.fmt.pix.field;
1193
1194     /* If driver also has buggy S_FMT, assume progressive */
1195     if (group->buffer.field == V4L2_FIELD_ANY) {
1196 #ifndef GST_DISABLE_GST_DEBUG
1197       if (!pool->has_warned_on_buggy_field) {
1198         pool->has_warned_on_buggy_field = TRUE;
1199         GST_WARNING_OBJECT (pool,
1200             "Driver should never set v4l2_format.pix.field to ANY");
1201       }
1202 #endif
1203
1204       group->buffer.field = V4L2_FIELD_NONE;
1205     }
1206   }
1207
1208   /* set top/bottom field first if v4l2_buffer has the information */
1209   switch (group->buffer.field) {
1210     case V4L2_FIELD_NONE:
1211       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1212       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1213       break;
1214     case V4L2_FIELD_INTERLACED_TB:
1215       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1216       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1217       break;
1218     case V4L2_FIELD_INTERLACED_BT:
1219       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1220       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1221       break;
1222     case V4L2_FIELD_INTERLACED:
1223       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1224       if (obj->tv_norm == V4L2_STD_NTSC_M ||
1225           obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1226           obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1227         GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1228       } else {
1229         GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1230       }
1231       break;
1232     default:
1233       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1234       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1235       GST_FIXME_OBJECT (pool,
1236           "Unhandled enum v4l2_field %d - treating as progressive",
1237           group->buffer.field);
1238       break;
1239   }
1240
1241   if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1242     if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1243       GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1244     else
1245       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1246   }
1247
1248   if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1249     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1250
1251   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1252   GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
1253   GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
1254
1255 done:
1256   *buffer = outbuf;
1257
1258   return GST_FLOW_OK;
1259
1260   /* ERRORS */
1261 poll_failed:
1262   {
1263     GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1264     return res;
1265   }
1266 eos:
1267   {
1268     return GST_FLOW_EOS;
1269   }
1270 dqbuf_failed:
1271   {
1272     return GST_FLOW_ERROR;
1273   }
1274 no_buffer:
1275   {
1276     GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1277         group->buffer.index);
1278     return GST_FLOW_ERROR;
1279   }
1280 }
1281
1282 static GstFlowReturn
1283 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1284     GstBufferPoolAcquireParams * params)
1285 {
1286   GstFlowReturn ret;
1287   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1288   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1289   GstV4l2Object *obj = pool->obj;
1290
1291   GST_DEBUG_OBJECT (pool, "acquire");
1292
1293   /* If this is being called to resurect a lost buffer */
1294   if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1295     ret = pclass->acquire_buffer (bpool, buffer, params);
1296     goto done;
1297   }
1298
1299   switch (obj->type) {
1300     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1301     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1302       /* capture, This function should return a buffer with new captured data */
1303       switch (obj->mode) {
1304         case GST_V4L2_IO_RW:
1305         {
1306           /* take empty buffer from the pool */
1307           ret = pclass->acquire_buffer (bpool, buffer, params);
1308           break;
1309         }
1310         case GST_V4L2_IO_DMABUF:
1311         case GST_V4L2_IO_MMAP:
1312         case GST_V4L2_IO_USERPTR:
1313         case GST_V4L2_IO_DMABUF_IMPORT:
1314         {
1315           /* just dequeue a buffer, we basically use the queue of v4l2 as the
1316            * storage for our buffers. This function does poll first so we can
1317            * interrupt it fine. */
1318           ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1319           break;
1320         }
1321         default:
1322           ret = GST_FLOW_ERROR;
1323           g_assert_not_reached ();
1324           break;
1325       }
1326       break;
1327
1328
1329     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1330     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1331       /* playback, This function should return an empty buffer */
1332       switch (obj->mode) {
1333         case GST_V4L2_IO_RW:
1334           /* get an empty buffer */
1335           ret = pclass->acquire_buffer (bpool, buffer, params);
1336           break;
1337
1338         case GST_V4L2_IO_MMAP:
1339         case GST_V4L2_IO_DMABUF:
1340         case GST_V4L2_IO_USERPTR:
1341         case GST_V4L2_IO_DMABUF_IMPORT:
1342           /* get a free unqueued buffer */
1343           ret = pclass->acquire_buffer (bpool, buffer, params);
1344           break;
1345
1346         default:
1347           ret = GST_FLOW_ERROR;
1348           g_assert_not_reached ();
1349           break;
1350       }
1351       break;
1352
1353     default:
1354       ret = GST_FLOW_ERROR;
1355       g_assert_not_reached ();
1356       break;
1357   }
1358 done:
1359   return ret;
1360 }
1361
1362 static void
1363 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1364 {
1365   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1366   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1367   GstV4l2Object *obj = pool->obj;
1368
1369   GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1370
1371   switch (obj->type) {
1372     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1373     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1374       /* capture, put the buffer back in the queue so that we can refill it
1375        * later. */
1376       switch (obj->mode) {
1377         case GST_V4L2_IO_RW:
1378           /* release back in the pool */
1379           pclass->release_buffer (bpool, buffer);
1380           break;
1381
1382         case GST_V4L2_IO_DMABUF:
1383         case GST_V4L2_IO_MMAP:
1384         case GST_V4L2_IO_USERPTR:
1385         case GST_V4L2_IO_DMABUF_IMPORT:
1386         {
1387           GstV4l2MemoryGroup *group;
1388           if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1389             gst_v4l2_allocator_reset_group (pool->vallocator, group);
1390             /* queue back in the device */
1391             if (pool->other_pool)
1392               gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1393             if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1394               pclass->release_buffer (bpool, buffer);
1395           } else {
1396             /* Simply release invalide/modified buffer, the allocator will
1397              * give it back later */
1398             GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1399             pclass->release_buffer (bpool, buffer);
1400           }
1401           break;
1402         }
1403         default:
1404           g_assert_not_reached ();
1405           break;
1406       }
1407       break;
1408
1409     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1410     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1411       switch (obj->mode) {
1412         case GST_V4L2_IO_RW:
1413           /* release back in the pool */
1414           pclass->release_buffer (bpool, buffer);
1415           break;
1416
1417         case GST_V4L2_IO_MMAP:
1418         case GST_V4L2_IO_DMABUF:
1419         case GST_V4L2_IO_USERPTR:
1420         case GST_V4L2_IO_DMABUF_IMPORT:
1421         {
1422           GstV4l2MemoryGroup *group;
1423           guint index;
1424
1425           if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1426             /* Simply release invalide/modified buffer, the allocator will
1427              * give it back later */
1428             GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1429             pclass->release_buffer (bpool, buffer);
1430             break;
1431           }
1432
1433           index = group->buffer.index;
1434
1435           if (pool->buffers[index] == NULL) {
1436             GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1437                 index);
1438
1439             /* Remove qdata, this will unmap any map data in userptr */
1440             gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1441                 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1442
1443             /* reset to default size */
1444             gst_v4l2_allocator_reset_group (pool->vallocator, group);
1445
1446             /* playback, put the buffer back in the queue to refill later. */
1447             pclass->release_buffer (bpool, buffer);
1448           } else {
1449             /* the buffer is queued in the device but maybe not played yet. We just
1450              * leave it there and not make it available for future calls to acquire
1451              * for now. The buffer will be dequeued and reused later. */
1452             GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1453           }
1454           break;
1455         }
1456
1457         default:
1458           g_assert_not_reached ();
1459           break;
1460       }
1461       break;
1462
1463     default:
1464       g_assert_not_reached ();
1465       break;
1466   }
1467 }
1468
1469 static void
1470 gst_v4l2_buffer_pool_dispose (GObject * object)
1471 {
1472   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1473
1474   if (pool->vallocator)
1475     gst_object_unref (pool->vallocator);
1476   pool->vallocator = NULL;
1477
1478   if (pool->allocator)
1479     gst_object_unref (pool->allocator);
1480   pool->allocator = NULL;
1481
1482   if (pool->other_pool)
1483     gst_object_unref (pool->other_pool);
1484   pool->other_pool = NULL;
1485
1486   G_OBJECT_CLASS (parent_class)->dispose (object);
1487 }
1488
1489 static void
1490 gst_v4l2_buffer_pool_finalize (GObject * object)
1491 {
1492   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1493
1494   if (pool->video_fd >= 0)
1495     v4l2_close (pool->video_fd);
1496
1497   gst_poll_free (pool->poll);
1498
1499   /* FIXME Is this required to keep around ?
1500    * This can't be done in dispose method because we must not set pointer
1501    * to NULL as it is part of the v4l2object and dispose could be called
1502    * multiple times */
1503   gst_object_unref (pool->obj->element);
1504
1505   g_cond_clear (&pool->empty_cond);
1506
1507   /* FIXME have we done enough here ? */
1508
1509   G_OBJECT_CLASS (parent_class)->finalize (object);
1510 }
1511
1512 static void
1513 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1514 {
1515   pool->poll = gst_poll_new (TRUE);
1516   pool->can_poll_device = TRUE;
1517   g_cond_init (&pool->empty_cond);
1518   pool->empty = TRUE;
1519 }
1520
1521 static void
1522 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1523 {
1524   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1525   GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1526
1527   object_class->dispose = gst_v4l2_buffer_pool_dispose;
1528   object_class->finalize = gst_v4l2_buffer_pool_finalize;
1529
1530   bufferpool_class->start = gst_v4l2_buffer_pool_start;
1531   bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1532   bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1533   bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1534   bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1535   bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1536   bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1537   bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1538
1539   GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1540       "V4L2 Buffer Pool");
1541 }
1542
1543 /**
1544  * gst_v4l2_buffer_pool_new:
1545  * @obj:  the v4l2 object owning the pool
1546  *
1547  * Construct a new buffer pool.
1548  *
1549  * Returns: the new pool, use gst_object_unref() to free resources
1550  */
1551 GstBufferPool *
1552 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1553 {
1554   GstV4l2BufferPool *pool;
1555   GstStructure *config;
1556   gchar *name, *parent_name;
1557   gint fd;
1558
1559   fd = v4l2_dup (obj->video_fd);
1560   if (fd < 0)
1561     goto dup_failed;
1562
1563   /* setting a significant unique name */
1564   parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1565   name = g_strconcat (parent_name, ":", "pool:",
1566       V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1567   g_free (parent_name);
1568
1569   pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1570       "name", name, NULL);
1571   g_free (name);
1572
1573   gst_poll_fd_init (&pool->pollfd);
1574   pool->pollfd.fd = fd;
1575   gst_poll_add_fd (pool->poll, &pool->pollfd);
1576   if (V4L2_TYPE_IS_OUTPUT (obj->type))
1577     gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1578   else
1579     gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1580
1581   pool->video_fd = fd;
1582   pool->obj = obj;
1583   pool->can_poll_device = TRUE;
1584
1585   pool->vallocator =
1586       gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1587   if (pool->vallocator == NULL)
1588     goto allocator_failed;
1589
1590   gst_object_ref (obj->element);
1591
1592   config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1593   gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1594   /* This will simply set a default config, but will not configure the pool
1595    * because min and max are not valid */
1596   gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1597
1598   return GST_BUFFER_POOL (pool);
1599
1600   /* ERRORS */
1601 dup_failed:
1602   {
1603     GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1604     return NULL;
1605   }
1606 allocator_failed:
1607   {
1608     GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1609     gst_object_unref (pool);
1610     return NULL;
1611   }
1612 }
1613
1614 static GstFlowReturn
1615 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1616 {
1617   GstFlowReturn res;
1618   GstV4l2Object *obj = pool->obj;
1619   gint amount;
1620   GstMapInfo map;
1621   gint toread;
1622
1623   toread = obj->info.size;
1624
1625   GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1626
1627   gst_buffer_map (buf, &map, GST_MAP_WRITE);
1628
1629   do {
1630     if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1631       goto poll_error;
1632
1633     amount = v4l2_read (obj->video_fd, map.data, toread);
1634
1635     if (amount == toread) {
1636       break;
1637     } else if (amount == -1) {
1638       if (errno == EAGAIN || errno == EINTR) {
1639         continue;
1640       } else
1641         goto read_error;
1642     } else {
1643       /* short reads can happen if a signal interrupts the read */
1644       continue;
1645     }
1646   } while (TRUE);
1647
1648   GST_LOG_OBJECT (pool, "read %d bytes", amount);
1649   gst_buffer_unmap (buf, &map);
1650   gst_buffer_resize (buf, 0, amount);
1651
1652   return GST_FLOW_OK;
1653
1654   /* ERRORS */
1655 poll_error:
1656   {
1657     GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1658     goto cleanup;
1659   }
1660 read_error:
1661   {
1662     GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1663         (_("Error reading %d bytes from device '%s'."),
1664             toread, obj->videodev), GST_ERROR_SYSTEM);
1665     res = GST_FLOW_ERROR;
1666     goto cleanup;
1667   }
1668 cleanup:
1669   {
1670     gst_buffer_unmap (buf, &map);
1671     gst_buffer_resize (buf, 0, 0);
1672     return res;
1673   }
1674 }
1675
1676 /**
1677  * gst_v4l2_buffer_pool_process:
1678  * @bpool: a #GstBufferPool
1679  * @buf: a #GstBuffer, maybe be replaced
1680  *
1681  * Process @buf in @bpool. For capture devices, this functions fills @buf with
1682  * data from the device. For output devices, this functions send the contents of
1683  * @buf to the device for playback.
1684  *
1685  * Returns: %GST_FLOW_OK on success.
1686  */
1687 GstFlowReturn
1688 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1689 {
1690   GstFlowReturn ret = GST_FLOW_OK;
1691   GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1692   GstV4l2Object *obj = pool->obj;
1693
1694   GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1695
1696   g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1697
1698   if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1699     return GST_FLOW_FLUSHING;
1700
1701   switch (obj->type) {
1702     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1703     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1704       /* capture */
1705       switch (obj->mode) {
1706         case GST_V4L2_IO_RW:
1707           /* capture into the buffer */
1708           ret = gst_v4l2_do_read (pool, *buf);
1709           break;
1710
1711         case GST_V4L2_IO_MMAP:
1712         case GST_V4L2_IO_DMABUF:
1713         {
1714           GstBuffer *tmp;
1715
1716           if ((*buf)->pool == bpool) {
1717             guint num_queued;
1718
1719             if (gst_buffer_get_size (*buf) == 0) {
1720               if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1721                 goto buffer_corrupted;
1722               else
1723                 goto eos;
1724             }
1725
1726             num_queued = g_atomic_int_get (&pool->num_queued);
1727             GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1728                 num_queued);
1729
1730             /* If we have no more buffer, and can allocate it time to do so */
1731             if (num_queued == 0) {
1732               if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1733                 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1734                 if (ret == GST_FLOW_OK)
1735                   goto done;
1736               }
1737             }
1738
1739             /* start copying buffers when we are running low on buffers */
1740             if (num_queued < pool->copy_threshold) {
1741               GstBuffer *copy;
1742
1743               if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1744                 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1745                 if (ret == GST_FLOW_OK)
1746                   goto done;
1747               }
1748
1749               /* copy the buffer */
1750               copy = gst_buffer_copy_region (*buf,
1751                   GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1752               GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1753
1754               /* and requeue so that we can continue capturing */
1755               gst_buffer_unref (*buf);
1756               *buf = copy;
1757             }
1758
1759             /* nothing, data was inside the buffer when we did _acquire() */
1760             goto done;
1761           }
1762
1763           /* buffer not from our pool, grab a frame and copy it into the target */
1764           if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1765             goto done;
1766
1767           /* An empty buffer on capture indicates the end of stream */
1768           if (gst_buffer_get_size (tmp) == 0) {
1769             gboolean corrupted = GST_BUFFER_FLAG_IS_SET (tmp,
1770                 GST_BUFFER_FLAG_CORRUPTED);
1771
1772             gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1773
1774             if (corrupted)
1775               goto buffer_corrupted;
1776             else
1777               goto eos;
1778           }
1779
1780           ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1781
1782           /* an queue the buffer again after the copy */
1783           gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1784
1785           if (ret != GST_FLOW_OK)
1786             goto copy_failed;
1787           break;
1788         }
1789
1790         case GST_V4L2_IO_USERPTR:
1791         {
1792           struct UserPtrData *data;
1793
1794           /* Replace our buffer with downstream allocated buffer */
1795           data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1796               GST_V4L2_IMPORT_QUARK);
1797           gst_buffer_replace (buf, data->buffer);
1798           _unmap_userptr_frame (data);
1799           break;
1800         }
1801
1802         case GST_V4L2_IO_DMABUF_IMPORT:
1803         {
1804           GstBuffer *tmp;
1805
1806           /* Replace our buffer with downstream allocated buffer */
1807           tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1808               GST_V4L2_IMPORT_QUARK);
1809           gst_buffer_replace (buf, tmp);
1810           gst_buffer_unref (tmp);
1811           break;
1812         }
1813
1814         default:
1815           g_assert_not_reached ();
1816           break;
1817       }
1818       break;
1819
1820     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1821     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1822       /* playback */
1823       switch (obj->mode) {
1824         case GST_V4L2_IO_RW:
1825           /* FIXME, do write() */
1826           GST_WARNING_OBJECT (pool, "implement write()");
1827           break;
1828
1829         case GST_V4L2_IO_USERPTR:
1830         case GST_V4L2_IO_DMABUF_IMPORT:
1831         case GST_V4L2_IO_DMABUF:
1832         case GST_V4L2_IO_MMAP:
1833         {
1834           GstBuffer *to_queue = NULL;
1835           GstV4l2MemoryGroup *group;
1836           gint index;
1837
1838           if ((*buf)->pool != bpool)
1839             goto copying;
1840
1841           if (!gst_v4l2_is_buffer_valid (*buf, &group))
1842             goto copying;
1843
1844           index = group->buffer.index;
1845
1846           GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1847
1848           index = group->buffer.index;
1849           if (pool->buffers[index] != NULL) {
1850             GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1851             goto copying;
1852           }
1853
1854           /* we can queue directly */
1855           to_queue = gst_buffer_ref (*buf);
1856
1857         copying:
1858           if (to_queue == NULL) {
1859             GstBufferPoolAcquireParams params = { 0 };
1860
1861             GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1862
1863             /* this can return EOS if all buffers are outstanding which would
1864              * be strange because we would expect the upstream element to have
1865              * allocated them and returned to us.. */
1866             params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1867             ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, &params);
1868             if (ret != GST_FLOW_OK)
1869               goto acquire_failed;
1870
1871             ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1872             if (ret != GST_FLOW_OK) {
1873               gst_buffer_unref (to_queue);
1874               goto prepare_failed;
1875             }
1876           }
1877
1878           if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1879             goto queue_failed;
1880
1881           /* if we are not streaming yet (this is the first buffer, start
1882            * streaming now */
1883           if (!gst_v4l2_buffer_pool_streamon (pool)) {
1884             /* don't check return value because qbuf would have failed */
1885             gst_v4l2_is_buffer_valid (to_queue, &group);
1886
1887             /* qbuf has stored to_queue buffer but we are not in
1888              * streaming state, so the flush logic won't be performed.
1889              * To avoid leaks, flush the allocator and restore the queued
1890              * buffer as non-queued */
1891             gst_v4l2_allocator_flush (pool->vallocator);
1892
1893             pool->buffers[group->buffer.index] = NULL;
1894
1895             gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1896                 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1897             gst_buffer_unref (to_queue);
1898             g_atomic_int_add (&pool->num_queued, -1);
1899             goto start_failed;
1900           }
1901
1902           /* Remove our ref, we will still hold this buffer in acquire as needed,
1903            * otherwise the pool will think it is outstanding and will refuse to stop. */
1904           gst_buffer_unref (to_queue);
1905
1906           if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1907             GstBuffer *out;
1908             /* all buffers are queued, try to dequeue one and release it back
1909              * into the pool so that _acquire can get to it again. */
1910             ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1911             if (ret == GST_FLOW_OK && out->pool == NULL)
1912               /* release the rendered buffer back into the pool. This wakes up any
1913                * thread waiting for a buffer in _acquire(). */
1914               gst_v4l2_buffer_pool_release_buffer (bpool, out);
1915           }
1916           break;
1917         }
1918         default:
1919           g_assert_not_reached ();
1920           break;
1921       }
1922       break;
1923     default:
1924       g_assert_not_reached ();
1925       break;
1926   }
1927 done:
1928   return ret;
1929
1930   /* ERRORS */
1931 copy_failed:
1932   {
1933     GST_ERROR_OBJECT (pool, "failed to copy buffer");
1934     return ret;
1935   }
1936 buffer_corrupted:
1937   {
1938     GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1939     gst_buffer_unref (*buf);
1940     *buf = NULL;
1941     return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1942   }
1943 eos:
1944   {
1945     GST_DEBUG_OBJECT (pool, "end of stream reached");
1946     gst_buffer_unref (*buf);
1947     *buf = NULL;
1948     return GST_V4L2_FLOW_LAST_BUFFER;
1949   }
1950 acquire_failed:
1951   {
1952     if (ret == GST_FLOW_FLUSHING)
1953       GST_DEBUG_OBJECT (pool, "flushing");
1954     else
1955       GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1956           gst_flow_get_name (ret));
1957     return ret;
1958   }
1959 prepare_failed:
1960   {
1961     GST_ERROR_OBJECT (pool, "failed to prepare data");
1962     return ret;
1963   }
1964 queue_failed:
1965   {
1966     GST_ERROR_OBJECT (pool, "failed to queue buffer");
1967     return ret;
1968   }
1969 start_failed:
1970   {
1971     GST_ERROR_OBJECT (pool, "failed to start streaming");
1972     return GST_FLOW_ERROR;
1973   }
1974 }
1975
1976 void
1977 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1978     GstBufferPool * other_pool)
1979 {
1980   g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1981
1982   if (pool->other_pool)
1983     gst_object_unref (pool->other_pool);
1984   pool->other_pool = gst_object_ref (other_pool);
1985 }
1986
1987 void
1988 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
1989 {
1990   GST_OBJECT_LOCK (pool);
1991   pool->enable_copy_threshold = copy;
1992   GST_OBJECT_UNLOCK (pool);
1993 }