v4l2bufferpool: Don't update buffer for OUTPUT
[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   if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
553     updated = TRUE;
554     max_buffers = VIDEO_MAX_FRAME;
555     GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
556   }
557
558   if (min_buffers > max_buffers) {
559     updated = TRUE;
560     min_buffers = max_buffers;
561     GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
562   } else if (min_buffers != max_buffers) {
563     if (!can_allocate) {
564       updated = TRUE;
565       max_buffers = min_buffers;
566       GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
567     }
568   }
569
570   if (!pool->add_videometa && obj->need_video_meta) {
571     GST_INFO_OBJECT (pool, "adding needed video meta");
572     updated = TRUE;
573     gst_buffer_pool_config_add_option (config,
574         GST_BUFFER_POOL_OPTION_VIDEO_META);
575   }
576
577   /* Always update the config to ensure the configured size matches */
578   gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
579       max_buffers);
580
581   /* keep a GstVideoInfo with defaults for the when we need to copy */
582   gst_video_info_from_caps (&pool->caps_info, caps);
583
584 done:
585   ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
586
587   /* If anything was changed documentation recommand to return FALSE */
588   return !updated && ret;
589
590   /* ERRORS */
591 wrong_config:
592   {
593     GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
594     return FALSE;
595   }
596 }
597
598 static gboolean
599 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
600 {
601   GstV4l2Object *obj = pool->obj;
602
603   switch (obj->mode) {
604     case GST_V4L2_IO_MMAP:
605     case GST_V4L2_IO_USERPTR:
606     case GST_V4L2_IO_DMABUF:
607     case GST_V4L2_IO_DMABUF_IMPORT:
608       if (!pool->streaming) {
609         if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
610           goto streamon_failed;
611
612         pool->streaming = TRUE;
613
614         GST_DEBUG_OBJECT (pool, "Started streaming");
615       }
616       break;
617     default:
618       break;
619   }
620
621   return TRUE;
622
623 streamon_failed:
624   {
625     GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
626         g_strerror (errno));
627     return FALSE;
628   }
629 }
630
631 static void
632 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
633 {
634   GstV4l2Object *obj = pool->obj;
635
636   switch (obj->mode) {
637     case GST_V4L2_IO_MMAP:
638     case GST_V4L2_IO_USERPTR:
639     case GST_V4L2_IO_DMABUF:
640     case GST_V4L2_IO_DMABUF_IMPORT:
641       if (pool->streaming) {
642         if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
643           GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
644               errno, g_strerror (errno));
645
646         pool->streaming = FALSE;
647
648         GST_DEBUG_OBJECT (pool, "Stopped streaming");
649
650         if (pool->vallocator)
651           gst_v4l2_allocator_flush (pool->vallocator);
652       }
653       break;
654     default:
655       break;
656   }
657 }
658
659 static GstFlowReturn
660 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
661 {
662   GstBufferPoolAcquireParams params = { 0 };
663   GstBuffer *buffer = NULL;
664   GstFlowReturn ret;
665
666   GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
667
668   params.flags =
669       (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
670   ret =
671       gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, &params);
672
673   if (ret == GST_FLOW_OK)
674     gst_buffer_unref (buffer);
675
676   return ret;
677 }
678
679 static gboolean
680 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
681 {
682   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
683   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
684   GstV4l2Object *obj = pool->obj;
685   GstStructure *config;
686   GstCaps *caps;
687   guint size, min_buffers, max_buffers;
688   guint max_latency, min_latency, copy_threshold = 0;
689   gboolean can_allocate = FALSE;
690
691   GST_DEBUG_OBJECT (pool, "activating pool");
692
693   config = gst_buffer_pool_get_config (bpool);
694   if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
695           &max_buffers))
696     goto wrong_config;
697
698   min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
699
700   switch (obj->mode) {
701     case GST_V4L2_IO_RW:
702       can_allocate = TRUE;
703 #ifdef HAVE_LIBV4L2
704       /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
705        * of MMAP. In this case, the first read initialize the queues, but the
706        * poll before that will always fail. Doing an empty read, forces the
707        * queue to be initialized now. We only do this if we have a streaming
708        * driver. */
709       if (obj->vcap.capabilities & V4L2_CAP_STREAMING)
710         v4l2_read (obj->video_fd, NULL, 0);
711 #endif
712       break;
713     case GST_V4L2_IO_DMABUF:
714     case GST_V4L2_IO_MMAP:
715     {
716       guint count;
717
718       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
719
720       /* first, lets request buffers, and see how many we can get: */
721       GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
722
723       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
724           V4L2_MEMORY_MMAP);
725
726       if (count < GST_V4L2_MIN_BUFFERS) {
727         min_buffers = count;
728         goto no_buffers;
729       }
730
731       /* V4L2 buffer pool are often very limited in the amount of buffers it
732        * can offer. The copy_threshold will workaround this limitation by
733        * falling back to copy if the pipeline needed more buffers. This also
734        * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
735        * called. */
736       if (count != min_buffers || pool->enable_copy_threshold) {
737         GST_WARNING_OBJECT (pool,
738             "Uncertain or not enough buffers, enabling copy threshold");
739         min_buffers = count;
740         copy_threshold = min_latency;
741       }
742
743       break;
744     }
745     case GST_V4L2_IO_USERPTR:
746     {
747       guint count;
748
749       can_allocate =
750           GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
751
752       GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
753
754       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
755           V4L2_MEMORY_USERPTR);
756
757       /* There is no rational to not get what we asked */
758       if (count < min_buffers) {
759         min_buffers = count;
760         goto no_buffers;
761       }
762
763       min_buffers = count;
764       break;
765     }
766     case GST_V4L2_IO_DMABUF_IMPORT:
767     {
768       guint count;
769
770       can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
771
772       GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
773
774       count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
775           V4L2_MEMORY_DMABUF);
776
777       /* There is no rational to not get what we asked */
778       if (count < min_buffers) {
779         min_buffers = count;
780         goto no_buffers;
781       }
782
783       min_buffers = count;
784       break;
785     }
786     default:
787       min_buffers = 0;
788       copy_threshold = 0;
789       g_assert_not_reached ();
790       break;
791   }
792
793   if (can_allocate)
794     max_latency = max_buffers;
795   else
796     max_latency = min_buffers;
797
798   pool->size = size;
799   pool->copy_threshold = copy_threshold;
800   pool->max_latency = max_latency;
801   pool->min_latency = min_latency;
802   pool->num_queued = 0;
803
804   if (max_buffers != 0 && max_buffers < min_buffers)
805     max_buffers = min_buffers;
806
807   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
808       max_buffers);
809   pclass->set_config (bpool, config);
810   gst_structure_free (config);
811
812   if (pool->other_pool)
813     if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
814       goto other_pool_failed;
815
816   /* now, allocate the buffers: */
817   if (!pclass->start (bpool))
818     goto start_failed;
819
820   if (!V4L2_TYPE_IS_OUTPUT (obj->type))
821     pool->group_released_handler =
822         g_signal_connect_swapped (pool->vallocator, "group-released",
823         G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
824
825   return TRUE;
826
827   /* ERRORS */
828 wrong_config:
829   {
830     GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
831     gst_structure_free (config);
832     return FALSE;
833   }
834 no_buffers:
835   {
836     GST_ERROR_OBJECT (pool,
837         "we received %d buffer from device '%s', we want at least %d",
838         min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
839     gst_structure_free (config);
840     return FALSE;
841   }
842 start_failed:
843   {
844     GST_ERROR_OBJECT (pool, "failed to start streaming");
845     return FALSE;
846   }
847 other_pool_failed:
848   {
849     GST_ERROR_OBJECT (pool, "failed to active the other pool %"
850         GST_PTR_FORMAT, pool->other_pool);
851     return FALSE;
852   }
853 }
854
855 static gboolean
856 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
857 {
858   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
859   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
860   gboolean ret;
861   gint i;
862
863   GST_DEBUG_OBJECT (pool, "stopping pool");
864
865   if (pool->group_released_handler > 0) {
866     g_signal_handler_disconnect (pool->vallocator,
867         pool->group_released_handler);
868     pool->group_released_handler = 0;
869   }
870
871   if (pool->other_pool) {
872     gst_buffer_pool_set_active (pool->other_pool, FALSE);
873     gst_object_unref (pool->other_pool);
874     pool->other_pool = NULL;
875   }
876
877   gst_v4l2_buffer_pool_streamoff (pool);
878
879   for (i = 0; i < VIDEO_MAX_FRAME; i++) {
880     if (pool->buffers[i]) {
881       GstBuffer *buffer = pool->buffers[i];
882
883       pool->buffers[i] = NULL;
884
885       if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
886         gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
887       else                      /* Don't re-enqueue capture buffer on stop */
888         pclass->release_buffer (bpool, buffer);
889
890       g_atomic_int_add (&pool->num_queued, -1);
891     }
892   }
893
894   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
895
896   if (ret && pool->vallocator) {
897     GstV4l2Return vret;
898
899     vret = gst_v4l2_allocator_stop (pool->vallocator);
900
901     if (vret == GST_V4L2_BUSY)
902       GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
903
904     ret = (vret == GST_V4L2_OK);
905   }
906
907   return ret;
908 }
909
910 static void
911 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
912 {
913   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
914
915   GST_DEBUG_OBJECT (pool, "start flushing");
916
917   gst_poll_set_flushing (pool->poll, TRUE);
918
919   GST_OBJECT_LOCK (pool);
920   pool->empty = FALSE;
921   g_cond_broadcast (&pool->empty_cond);
922   GST_OBJECT_UNLOCK (pool);
923
924   if (pool->other_pool)
925     gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
926 }
927
928 static void
929 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
930 {
931   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
932   GstV4l2Object *obj = pool->obj;
933   gint i;
934
935   GST_DEBUG_OBJECT (pool, "stop flushing");
936
937   /* If we haven't started streaming yet, simply call streamon */
938   if (!pool->streaming)
939     goto streamon;
940
941   if (pool->other_pool)
942     gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
943
944   gst_v4l2_buffer_pool_streamoff (pool);
945
946   /* Reset our state */
947   switch (obj->mode) {
948     case GST_V4L2_IO_RW:
949       break;
950     case GST_V4L2_IO_MMAP:
951     case GST_V4L2_IO_USERPTR:
952     case GST_V4L2_IO_DMABUF:
953     case GST_V4L2_IO_DMABUF_IMPORT:
954     {
955       for (i = 0; i < VIDEO_MAX_FRAME; i++) {
956         /* Re-enqueue buffers */
957         if (pool->buffers[i]) {
958           GstBufferPool *bpool = (GstBufferPool *) pool;
959           GstBuffer *buffer = pool->buffers[i];
960
961           pool->buffers[i] = NULL;
962
963           /* Remove qdata, this will unmap any map data in
964            * userptr/dmabuf-import */
965           gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
966               GST_V4L2_IMPORT_QUARK, NULL, NULL);
967
968           if (buffer->pool == NULL)
969             gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
970
971           g_atomic_int_add (&pool->num_queued, -1);
972         }
973       }
974
975       break;
976     }
977     default:
978       g_assert_not_reached ();
979       break;
980   }
981
982 streamon:
983   /* Start streaming on capture device only */
984   if (!V4L2_TYPE_IS_OUTPUT (obj->type))
985     gst_v4l2_buffer_pool_streamon (pool);
986
987   gst_poll_set_flushing (pool->poll, FALSE);
988 }
989
990 static GstFlowReturn
991 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
992 {
993   gint ret;
994
995   /* In RW mode there is no queue, hence no need to wait while the queue is
996    * empty */
997   if (pool->obj->mode != GST_V4L2_IO_RW) {
998     GST_OBJECT_LOCK (pool);
999     while (pool->empty)
1000       g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1001     GST_OBJECT_UNLOCK (pool);
1002   }
1003
1004   if (!pool->can_poll_device)
1005     goto done;
1006
1007   GST_LOG_OBJECT (pool, "polling device");
1008
1009 again:
1010   ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
1011   if (G_UNLIKELY (ret < 0)) {
1012     switch (errno) {
1013       case EBUSY:
1014         goto stopped;
1015       case EAGAIN:
1016       case EINTR:
1017         goto again;
1018       case ENXIO:
1019         GST_WARNING_OBJECT (pool,
1020             "v4l2 device doesn't support polling. Disabling"
1021             " using libv4l2 in this case may cause deadlocks");
1022         pool->can_poll_device = FALSE;
1023         goto done;
1024       default:
1025         goto select_error;
1026     }
1027   }
1028
1029   if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1030     goto select_error;
1031
1032 done:
1033   return GST_FLOW_OK;
1034
1035   /* ERRORS */
1036 stopped:
1037   {
1038     GST_DEBUG_OBJECT (pool, "stop called");
1039     return GST_FLOW_FLUSHING;
1040   }
1041 select_error:
1042   {
1043     GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1044         ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1045     return GST_FLOW_ERROR;
1046   }
1047 }
1048
1049 static GstFlowReturn
1050 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1051 {
1052   GstV4l2MemoryGroup *group = NULL;
1053   const GstV4l2Object *obj = pool->obj;
1054   gint index;
1055
1056   if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1057     GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1058     gst_buffer_unref (buf);
1059     return GST_FLOW_OK;
1060   }
1061
1062   index = group->buffer.index;
1063
1064   if (pool->buffers[index] != NULL)
1065     goto already_queued;
1066
1067   GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1068
1069   g_atomic_int_inc (&pool->num_queued);
1070   pool->buffers[index] = buf;
1071
1072   if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1073     enum v4l2_field field;
1074
1075     /* Except when field is set to alternate, buffer field is the same as
1076      * the one defined in format */
1077     if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1078       field = obj->format.fmt.pix_mp.field;
1079     else
1080       field = obj->format.fmt.pix.field;
1081
1082     /* NB: At this moment, we can't have alternate mode because it not handled
1083      * yet */
1084     if (field == V4L2_FIELD_ALTERNATE) {
1085       if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1086         field = V4L2_FIELD_TOP;
1087       else
1088         field = V4L2_FIELD_BOTTOM;
1089     }
1090
1091     group->buffer.field = field;
1092   }
1093
1094   if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1095     goto queue_failed;
1096
1097   GST_OBJECT_LOCK (pool);
1098   pool->empty = FALSE;
1099   g_cond_signal (&pool->empty_cond);
1100   GST_OBJECT_UNLOCK (pool);
1101
1102   return GST_FLOW_OK;
1103
1104 already_queued:
1105   {
1106     GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1107     return GST_FLOW_ERROR;
1108   }
1109 queue_failed:
1110   {
1111     GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1112     /* Mark broken buffer to the allocator */
1113     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1114     g_atomic_int_add (&pool->num_queued, -1);
1115     pool->buffers[index] = NULL;
1116     return GST_FLOW_ERROR;
1117   }
1118 }
1119
1120 static GstFlowReturn
1121 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1122 {
1123   GstFlowReturn res;
1124   GstBuffer *outbuf;
1125   GstV4l2Object *obj = pool->obj;
1126   GstClockTime timestamp;
1127   GstV4l2MemoryGroup *group;
1128   gint i;
1129
1130   if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1131     goto poll_failed;
1132
1133   GST_LOG_OBJECT (pool, "dequeueing a buffer");
1134
1135   group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1136   if (group == NULL)
1137     goto dqbuf_failed;
1138
1139   /* get our GstBuffer with that index from the pool, if the buffer was
1140    * outstanding we have a serious problem.
1141    */
1142   outbuf = pool->buffers[group->buffer.index];
1143   if (outbuf == NULL)
1144     goto no_buffer;
1145
1146   /* mark the buffer outstanding */
1147   pool->buffers[group->buffer.index] = NULL;
1148   if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1149     GST_OBJECT_LOCK (pool);
1150     pool->empty = TRUE;
1151     GST_OBJECT_UNLOCK (pool);
1152   }
1153
1154   timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1155
1156 #ifndef GST_DISABLE_GST_DEBUG
1157   for (i = 0; i < group->n_mem; i++) {
1158     GST_LOG_OBJECT (pool,
1159         "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1160         GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1161         group->buffer.sequence, group->buffer.index, group->mem[i],
1162         group->planes[i].bytesused, i, group->buffer.flags,
1163         GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1164   }
1165 #endif
1166
1167   /* Ignore timestamp and field for OUTPUT device */
1168   if (V4L2_TYPE_IS_OUTPUT (obj->type))
1169     goto done;
1170
1171   /* Check for driver bug in reporting feild */
1172   if (group->buffer.field == V4L2_FIELD_ANY) {
1173     /* Only warn once to avoid the spamming */
1174 #ifndef GST_DISABLE_GST_DEBUG
1175     if (!pool->has_warned_on_buggy_field) {
1176       pool->has_warned_on_buggy_field = TRUE;
1177       GST_WARNING_OBJECT (pool,
1178           "Driver should never set v4l2_buffer.field to ANY");
1179     }
1180 #endif
1181
1182     /* Use the value from the format (works for UVC bug) */
1183     group->buffer.field = obj->format.fmt.pix.field;
1184
1185     /* If driver also has buggy S_FMT, assume progressive */
1186     if (group->buffer.field == V4L2_FIELD_ANY) {
1187 #ifndef GST_DISABLE_GST_DEBUG
1188       if (!pool->has_warned_on_buggy_field) {
1189         pool->has_warned_on_buggy_field = TRUE;
1190         GST_WARNING_OBJECT (pool,
1191             "Driver should never set v4l2_format.pix.field to ANY");
1192       }
1193 #endif
1194
1195       group->buffer.field = V4L2_FIELD_NONE;
1196     }
1197   }
1198
1199   /* set top/bottom field first if v4l2_buffer has the information */
1200   switch (group->buffer.field) {
1201     case V4L2_FIELD_NONE:
1202       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1203       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1204       break;
1205     case V4L2_FIELD_INTERLACED_TB:
1206       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1207       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1208       break;
1209     case V4L2_FIELD_INTERLACED_BT:
1210       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1211       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1212       break;
1213     case V4L2_FIELD_INTERLACED:
1214       GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1215       if (obj->tv_norm == V4L2_STD_NTSC_M ||
1216           obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1217           obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1218         GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1219       } else {
1220         GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1221       }
1222       break;
1223     default:
1224       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1225       GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1226       GST_FIXME_OBJECT (pool,
1227           "Unhandled enum v4l2_field %d - treating as progressive",
1228           group->buffer.field);
1229       break;
1230   }
1231
1232   if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1233     if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1234       GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1235     else
1236       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1237   }
1238
1239   if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1240     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1241
1242   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1243
1244 done:
1245   *buffer = outbuf;
1246
1247   return GST_FLOW_OK;
1248
1249   /* ERRORS */
1250 poll_failed:
1251   {
1252     GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1253     return res;
1254   }
1255 dqbuf_failed:
1256   {
1257     return GST_FLOW_ERROR;
1258   }
1259 no_buffer:
1260   {
1261     GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1262         group->buffer.index);
1263     return GST_FLOW_ERROR;
1264   }
1265 }
1266
1267 static GstFlowReturn
1268 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1269     GstBufferPoolAcquireParams * params)
1270 {
1271   GstFlowReturn ret;
1272   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1273   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1274   GstV4l2Object *obj = pool->obj;
1275
1276   GST_DEBUG_OBJECT (pool, "acquire");
1277
1278   /* If this is being called to resurect a lost buffer */
1279   if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1280     ret = pclass->acquire_buffer (bpool, buffer, params);
1281     goto done;
1282   }
1283
1284   switch (obj->type) {
1285     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1286     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1287       /* capture, This function should return a buffer with new captured data */
1288       switch (obj->mode) {
1289         case GST_V4L2_IO_RW:
1290         {
1291           /* take empty buffer from the pool */
1292           ret = pclass->acquire_buffer (bpool, buffer, params);
1293           break;
1294         }
1295         case GST_V4L2_IO_DMABUF:
1296         case GST_V4L2_IO_MMAP:
1297         case GST_V4L2_IO_USERPTR:
1298         case GST_V4L2_IO_DMABUF_IMPORT:
1299         {
1300           /* just dequeue a buffer, we basically use the queue of v4l2 as the
1301            * storage for our buffers. This function does poll first so we can
1302            * interrupt it fine. */
1303           ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1304           break;
1305         }
1306         default:
1307           ret = GST_FLOW_ERROR;
1308           g_assert_not_reached ();
1309           break;
1310       }
1311       break;
1312
1313
1314     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1315     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1316       /* playback, This function should return an empty buffer */
1317       switch (obj->mode) {
1318         case GST_V4L2_IO_RW:
1319           /* get an empty buffer */
1320           ret = pclass->acquire_buffer (bpool, buffer, params);
1321           break;
1322
1323         case GST_V4L2_IO_MMAP:
1324         case GST_V4L2_IO_DMABUF:
1325         case GST_V4L2_IO_USERPTR:
1326         case GST_V4L2_IO_DMABUF_IMPORT:
1327           /* get a free unqueued buffer */
1328           ret = pclass->acquire_buffer (bpool, buffer, params);
1329           break;
1330
1331         default:
1332           ret = GST_FLOW_ERROR;
1333           g_assert_not_reached ();
1334           break;
1335       }
1336       break;
1337
1338     default:
1339       ret = GST_FLOW_ERROR;
1340       g_assert_not_reached ();
1341       break;
1342   }
1343 done:
1344   return ret;
1345 }
1346
1347 static void
1348 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1349 {
1350   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1351   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1352   GstV4l2Object *obj = pool->obj;
1353
1354   GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1355
1356   switch (obj->type) {
1357     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1358     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1359       /* capture, put the buffer back in the queue so that we can refill it
1360        * later. */
1361       switch (obj->mode) {
1362         case GST_V4L2_IO_RW:
1363           /* release back in the pool */
1364           pclass->release_buffer (bpool, buffer);
1365           break;
1366
1367         case GST_V4L2_IO_DMABUF:
1368         case GST_V4L2_IO_MMAP:
1369         case GST_V4L2_IO_USERPTR:
1370         case GST_V4L2_IO_DMABUF_IMPORT:
1371         {
1372           GstV4l2MemoryGroup *group;
1373           if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1374             gst_v4l2_allocator_reset_group (pool->vallocator, group);
1375             /* queue back in the device */
1376             if (pool->other_pool)
1377               gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1378             if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1379               pclass->release_buffer (bpool, buffer);
1380           } else {
1381             /* Simply release invalide/modified buffer, the allocator will
1382              * give it back later */
1383             GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1384             pclass->release_buffer (bpool, buffer);
1385           }
1386           break;
1387         }
1388         default:
1389           g_assert_not_reached ();
1390           break;
1391       }
1392       break;
1393
1394     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1395     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1396       switch (obj->mode) {
1397         case GST_V4L2_IO_RW:
1398           /* release back in the pool */
1399           pclass->release_buffer (bpool, buffer);
1400           break;
1401
1402         case GST_V4L2_IO_MMAP:
1403         case GST_V4L2_IO_DMABUF:
1404         case GST_V4L2_IO_USERPTR:
1405         case GST_V4L2_IO_DMABUF_IMPORT:
1406         {
1407           GstV4l2MemoryGroup *group;
1408           guint index;
1409
1410           if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1411             /* Simply release invalide/modified buffer, the allocator will
1412              * give it back later */
1413             GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1414             pclass->release_buffer (bpool, buffer);
1415             break;
1416           }
1417
1418           index = group->buffer.index;
1419
1420           if (pool->buffers[index] == NULL) {
1421             GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1422                 index);
1423
1424             /* Remove qdata, this will unmap any map data in userptr */
1425             gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1426                 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1427
1428             /* reset to default size */
1429             gst_v4l2_allocator_reset_group (pool->vallocator, group);
1430
1431             /* playback, put the buffer back in the queue to refill later. */
1432             pclass->release_buffer (bpool, buffer);
1433           } else {
1434             /* the buffer is queued in the device but maybe not played yet. We just
1435              * leave it there and not make it available for future calls to acquire
1436              * for now. The buffer will be dequeued and reused later. */
1437             GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1438           }
1439           break;
1440         }
1441
1442         default:
1443           g_assert_not_reached ();
1444           break;
1445       }
1446       break;
1447
1448     default:
1449       g_assert_not_reached ();
1450       break;
1451   }
1452 }
1453
1454 static void
1455 gst_v4l2_buffer_pool_dispose (GObject * object)
1456 {
1457   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1458
1459   if (pool->vallocator)
1460     gst_object_unref (pool->vallocator);
1461   pool->vallocator = NULL;
1462
1463   if (pool->allocator)
1464     gst_object_unref (pool->allocator);
1465   pool->allocator = NULL;
1466
1467   if (pool->other_pool)
1468     gst_object_unref (pool->other_pool);
1469   pool->other_pool = NULL;
1470
1471   G_OBJECT_CLASS (parent_class)->dispose (object);
1472 }
1473
1474 static void
1475 gst_v4l2_buffer_pool_finalize (GObject * object)
1476 {
1477   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1478
1479   if (pool->video_fd >= 0)
1480     v4l2_close (pool->video_fd);
1481
1482   gst_poll_free (pool->poll);
1483
1484   /* FIXME Is this required to keep around ?
1485    * This can't be done in dispose method because we must not set pointer
1486    * to NULL as it is part of the v4l2object and dispose could be called
1487    * multiple times */
1488   gst_object_unref (pool->obj->element);
1489
1490   g_cond_clear (&pool->empty_cond);
1491
1492   /* FIXME have we done enough here ? */
1493
1494   G_OBJECT_CLASS (parent_class)->finalize (object);
1495 }
1496
1497 static void
1498 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1499 {
1500   pool->poll = gst_poll_new (TRUE);
1501   pool->can_poll_device = TRUE;
1502   g_cond_init (&pool->empty_cond);
1503   pool->empty = TRUE;
1504 }
1505
1506 static void
1507 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1508 {
1509   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1510   GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1511
1512   object_class->dispose = gst_v4l2_buffer_pool_dispose;
1513   object_class->finalize = gst_v4l2_buffer_pool_finalize;
1514
1515   bufferpool_class->start = gst_v4l2_buffer_pool_start;
1516   bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1517   bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1518   bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1519   bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1520   bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1521   bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1522   bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1523
1524   GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1525       "V4L2 Buffer Pool");
1526 }
1527
1528 /**
1529  * gst_v4l2_buffer_pool_new:
1530  * @obj:  the v4l2 object owning the pool
1531  *
1532  * Construct a new buffer pool.
1533  *
1534  * Returns: the new pool, use gst_object_unref() to free resources
1535  */
1536 GstBufferPool *
1537 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1538 {
1539   GstV4l2BufferPool *pool;
1540   GstStructure *config;
1541   gchar *name, *parent_name;
1542   gint fd;
1543
1544   fd = v4l2_dup (obj->video_fd);
1545   if (fd < 0)
1546     goto dup_failed;
1547
1548   /* setting a significant unique name */
1549   parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1550   name = g_strconcat (parent_name, ":", "pool:",
1551       V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1552   g_free (parent_name);
1553
1554   pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1555       "name", name, NULL);
1556   g_free (name);
1557
1558   gst_poll_fd_init (&pool->pollfd);
1559   pool->pollfd.fd = fd;
1560   gst_poll_add_fd (pool->poll, &pool->pollfd);
1561   if (V4L2_TYPE_IS_OUTPUT (obj->type))
1562     gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1563   else
1564     gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1565
1566   pool->video_fd = fd;
1567   pool->obj = obj;
1568   pool->can_poll_device = TRUE;
1569
1570   pool->vallocator =
1571       gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1572   if (pool->vallocator == NULL)
1573     goto allocator_failed;
1574
1575   gst_object_ref (obj->element);
1576
1577   config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1578   gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1579   /* This will simply set a default config, but will not configure the pool
1580    * because min and max are not valid */
1581   gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1582
1583   return GST_BUFFER_POOL (pool);
1584
1585   /* ERRORS */
1586 dup_failed:
1587   {
1588     GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1589     return NULL;
1590   }
1591 allocator_failed:
1592   {
1593     GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1594     gst_object_unref (pool);
1595     return NULL;
1596   }
1597 }
1598
1599 static GstFlowReturn
1600 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1601 {
1602   GstFlowReturn res;
1603   GstV4l2Object *obj = pool->obj;
1604   gint amount;
1605   GstMapInfo map;
1606   gint toread;
1607
1608   toread = obj->info.size;
1609
1610   GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1611
1612   gst_buffer_map (buf, &map, GST_MAP_WRITE);
1613
1614   do {
1615     if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1616       goto poll_error;
1617
1618     amount = v4l2_read (obj->video_fd, map.data, toread);
1619
1620     if (amount == toread) {
1621       break;
1622     } else if (amount == -1) {
1623       if (errno == EAGAIN || errno == EINTR) {
1624         continue;
1625       } else
1626         goto read_error;
1627     } else {
1628       /* short reads can happen if a signal interrupts the read */
1629       continue;
1630     }
1631   } while (TRUE);
1632
1633   GST_LOG_OBJECT (pool, "read %d bytes", amount);
1634   gst_buffer_unmap (buf, &map);
1635   gst_buffer_resize (buf, 0, amount);
1636
1637   return GST_FLOW_OK;
1638
1639   /* ERRORS */
1640 poll_error:
1641   {
1642     GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1643     goto cleanup;
1644   }
1645 read_error:
1646   {
1647     GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1648         (_("Error reading %d bytes from device '%s'."),
1649             toread, obj->videodev), GST_ERROR_SYSTEM);
1650     res = GST_FLOW_ERROR;
1651     goto cleanup;
1652   }
1653 cleanup:
1654   {
1655     gst_buffer_unmap (buf, &map);
1656     gst_buffer_resize (buf, 0, 0);
1657     return res;
1658   }
1659 }
1660
1661 /**
1662  * gst_v4l2_buffer_pool_process:
1663  * @bpool: a #GstBufferPool
1664  * @buf: a #GstBuffer, maybe be replaced
1665  *
1666  * Process @buf in @bpool. For capture devices, this functions fills @buf with
1667  * data from the device. For output devices, this functions send the contents of
1668  * @buf to the device for playback.
1669  *
1670  * Returns: %GST_FLOW_OK on success.
1671  */
1672 GstFlowReturn
1673 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1674 {
1675   GstFlowReturn ret = GST_FLOW_OK;
1676   GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1677   GstV4l2Object *obj = pool->obj;
1678
1679   GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1680
1681   g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1682
1683   if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1684     return GST_FLOW_FLUSHING;
1685
1686   switch (obj->type) {
1687     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1688     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1689       /* capture */
1690       switch (obj->mode) {
1691         case GST_V4L2_IO_RW:
1692           /* capture into the buffer */
1693           ret = gst_v4l2_do_read (pool, *buf);
1694           break;
1695
1696         case GST_V4L2_IO_MMAP:
1697         case GST_V4L2_IO_DMABUF:
1698         {
1699           GstBuffer *tmp;
1700
1701           if ((*buf)->pool == bpool) {
1702             guint num_queued;
1703
1704             if (gst_buffer_get_size (*buf) == 0) {
1705               if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1706                 goto buffer_corrupted;
1707               else
1708                 goto eos;
1709             }
1710
1711             num_queued = g_atomic_int_get (&pool->num_queued);
1712             GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1713                 num_queued);
1714
1715             /* If we have no more buffer, and can allocate it time to do so */
1716             if (num_queued == 0) {
1717               if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1718                 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1719                 if (ret == GST_FLOW_OK)
1720                   goto done;
1721               }
1722             }
1723
1724             /* start copying buffers when we are running low on buffers */
1725             if (num_queued < pool->copy_threshold) {
1726               GstBuffer *copy;
1727
1728               if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1729                 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1730                 if (ret == GST_FLOW_OK)
1731                   goto done;
1732               }
1733
1734               /* copy the buffer */
1735               copy = gst_buffer_copy_region (*buf,
1736                   GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1737               GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1738
1739               /* and requeue so that we can continue capturing */
1740               gst_buffer_unref (*buf);
1741               *buf = copy;
1742             }
1743
1744             /* nothing, data was inside the buffer when we did _acquire() */
1745             goto done;
1746           }
1747
1748           /* buffer not from our pool, grab a frame and copy it into the target */
1749           if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1750             goto done;
1751
1752           /* An empty buffer on capture indicates the end of stream */
1753           if (gst_buffer_get_size (tmp) == 0) {
1754             gboolean corrupted = GST_BUFFER_FLAG_IS_SET (tmp,
1755                 GST_BUFFER_FLAG_CORRUPTED);
1756
1757             gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1758
1759             if (corrupted)
1760               goto buffer_corrupted;
1761             else
1762               goto eos;
1763           }
1764
1765           ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1766
1767           /* an queue the buffer again after the copy */
1768           gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1769
1770           if (ret != GST_FLOW_OK)
1771             goto copy_failed;
1772           break;
1773         }
1774
1775         case GST_V4L2_IO_USERPTR:
1776         {
1777           struct UserPtrData *data;
1778
1779           /* Replace our buffer with downstream allocated buffer */
1780           data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1781               GST_V4L2_IMPORT_QUARK);
1782           gst_buffer_replace (buf, data->buffer);
1783           _unmap_userptr_frame (data);
1784           break;
1785         }
1786
1787         case GST_V4L2_IO_DMABUF_IMPORT:
1788         {
1789           GstBuffer *tmp;
1790
1791           /* Replace our buffer with downstream allocated buffer */
1792           tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1793               GST_V4L2_IMPORT_QUARK);
1794           gst_buffer_replace (buf, tmp);
1795           gst_buffer_unref (tmp);
1796           break;
1797         }
1798
1799         default:
1800           g_assert_not_reached ();
1801           break;
1802       }
1803       break;
1804
1805     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1806     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1807       /* playback */
1808       switch (obj->mode) {
1809         case GST_V4L2_IO_RW:
1810           /* FIXME, do write() */
1811           GST_WARNING_OBJECT (pool, "implement write()");
1812           break;
1813
1814         case GST_V4L2_IO_USERPTR:
1815         case GST_V4L2_IO_DMABUF_IMPORT:
1816         case GST_V4L2_IO_DMABUF:
1817         case GST_V4L2_IO_MMAP:
1818         {
1819           GstBuffer *to_queue = NULL;
1820           GstV4l2MemoryGroup *group;
1821           gint index;
1822
1823           if ((*buf)->pool != bpool)
1824             goto copying;
1825
1826           if (!gst_v4l2_is_buffer_valid (*buf, &group))
1827             goto copying;
1828
1829           index = group->buffer.index;
1830
1831           GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1832
1833           index = group->buffer.index;
1834           if (pool->buffers[index] != NULL) {
1835             GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1836             goto copying;
1837           }
1838
1839           /* we can queue directly */
1840           to_queue = gst_buffer_ref (*buf);
1841
1842         copying:
1843           if (to_queue == NULL) {
1844             GstBufferPoolAcquireParams params = { 0 };
1845
1846             GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1847
1848             /* this can return EOS if all buffers are outstanding which would
1849              * be strange because we would expect the upstream element to have
1850              * allocated them and returned to us.. */
1851             params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1852             ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, &params);
1853             if (ret != GST_FLOW_OK)
1854               goto acquire_failed;
1855
1856             ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1857             if (ret != GST_FLOW_OK) {
1858               gst_buffer_unref (to_queue);
1859               goto prepare_failed;
1860             }
1861           }
1862
1863           if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1864             goto queue_failed;
1865
1866           /* if we are not streaming yet (this is the first buffer, start
1867            * streaming now */
1868           if (!gst_v4l2_buffer_pool_streamon (pool)) {
1869             /* don't check return value because qbuf would have failed */
1870             gst_v4l2_is_buffer_valid (to_queue, &group);
1871
1872             /* qbuf has stored to_queue buffer but we are not in
1873              * streaming state, so the flush logic won't be performed.
1874              * To avoid leaks, flush the allocator and restore the queued
1875              * buffer as non-queued */
1876             gst_v4l2_allocator_flush (pool->vallocator);
1877
1878             pool->buffers[group->buffer.index] = NULL;
1879
1880             gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1881                 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1882             gst_buffer_unref (to_queue);
1883             g_atomic_int_add (&pool->num_queued, -1);
1884             goto start_failed;
1885           }
1886
1887           /* Remove our ref, we will still hold this buffer in acquire as needed,
1888            * otherwise the pool will think it is outstanding and will refuse to stop. */
1889           gst_buffer_unref (to_queue);
1890
1891           if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1892             GstBuffer *out;
1893             /* all buffers are queued, try to dequeue one and release it back
1894              * into the pool so that _acquire can get to it again. */
1895             ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1896             if (ret == GST_FLOW_OK && out->pool == NULL)
1897               /* release the rendered buffer back into the pool. This wakes up any
1898                * thread waiting for a buffer in _acquire(). */
1899               gst_v4l2_buffer_pool_release_buffer (bpool, out);
1900           }
1901           break;
1902         }
1903         default:
1904           g_assert_not_reached ();
1905           break;
1906       }
1907       break;
1908     default:
1909       g_assert_not_reached ();
1910       break;
1911   }
1912 done:
1913   return ret;
1914
1915   /* ERRORS */
1916 copy_failed:
1917   {
1918     GST_ERROR_OBJECT (pool, "failed to copy buffer");
1919     return ret;
1920   }
1921 buffer_corrupted:
1922   {
1923     GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1924     gst_buffer_unref (*buf);
1925     *buf = NULL;
1926     return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1927   }
1928 eos:
1929   {
1930     GST_DEBUG_OBJECT (pool, "end of stream reached");
1931     gst_buffer_unref (*buf);
1932     *buf = NULL;
1933     return GST_V4L2_FLOW_LAST_BUFFER;
1934   }
1935 acquire_failed:
1936   {
1937     if (ret == GST_FLOW_FLUSHING)
1938       GST_DEBUG_OBJECT (pool, "flushing");
1939     else
1940       GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1941           gst_flow_get_name (ret));
1942     return ret;
1943   }
1944 prepare_failed:
1945   {
1946     GST_ERROR_OBJECT (pool, "failed to prepare data");
1947     return ret;
1948   }
1949 queue_failed:
1950   {
1951     GST_ERROR_OBJECT (pool, "failed to queue buffer");
1952     return ret;
1953   }
1954 start_failed:
1955   {
1956     GST_ERROR_OBJECT (pool, "failed to start streaming");
1957     return GST_FLOW_ERROR;
1958   }
1959 }
1960
1961 void
1962 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1963     GstBufferPool * other_pool)
1964 {
1965   g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1966
1967   if (pool->other_pool)
1968     gst_object_unref (pool->other_pool);
1969   pool->other_pool = gst_object_ref (other_pool);
1970 }
1971
1972 void
1973 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
1974 {
1975   GST_OBJECT_LOCK (pool);
1976   pool->enable_copy_threshold = copy;
1977   GST_OBJECT_UNLOCK (pool);
1978 }