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/
7 * gstv4l2bufferpool.c V4L2 buffer pool class
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.
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.
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.
30 # define _GNU_SOURCE /* O_CLOEXEC */
38 #include "gst/video/video.h"
39 #include "gst/video/gstvideometa.h"
40 #include "gst/video/gstvideopool.h"
41 #include "gst/allocators/gstdmabuf.h"
43 #include <gstv4l2bufferpool.h>
45 #include "v4l2_calls.h"
46 #include "gst/gst-i18n-plugin.h"
47 #include <gst/glib-compat-private.h>
49 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
50 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2_debug
53 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
59 #define gst_v4l2_buffer_pool_parent_class parent_class
60 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
62 enum _GstV4l2BufferPoolAcquireFlags
64 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
65 GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
66 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
69 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
73 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
75 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
76 gboolean valid = FALSE;
78 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
81 if (gst_is_dmabuf_memory (mem))
82 mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
83 GST_V4L2_MEMORY_QUARK);
85 if (mem && gst_is_v4l2_memory (mem)) {
86 GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
87 GstV4l2MemoryGroup *group = vmem->group;
90 if (group->n_mem != gst_buffer_n_memory (buffer))
93 for (i = 0; i < group->n_mem; i++) {
94 if (group->mem[i] != gst_buffer_peek_memory (buffer, i))
97 if (!gst_memory_is_writable (group->mem[i]))
111 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
114 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
116 GST_LOG_OBJECT (pool, "copying buffer");
118 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
119 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
120 GstVideoFrame src_frame, dest_frame;
122 GST_DEBUG_OBJECT (pool, "copy video frame");
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))
128 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
130 gst_video_frame_unmap (&src_frame);
134 gst_video_frame_copy (&dest_frame, &src_frame);
136 gst_video_frame_unmap (&src_frame);
137 gst_video_frame_unmap (&dest_frame);
141 GST_DEBUG_OBJECT (pool, "copy raw bytes");
143 if (!gst_buffer_map (src, &map, GST_MAP_READ))
146 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
148 gst_buffer_unmap (src, &map);
149 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
152 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
159 GST_ERROR_OBJECT (pool, "could not map buffer");
160 return GST_FLOW_ERROR;
173 gst_v4l2_buffer_pool_import_quark (void)
175 static GQuark quark = 0;
178 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
184 _unmap_userptr_frame (struct UserPtrData *data)
187 gst_video_frame_unmap (&data->frame);
189 gst_buffer_unmap (data->buffer, &data->map);
192 gst_buffer_unref (data->buffer);
194 g_slice_free (struct UserPtrData, data);
198 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
199 GstBuffer * dest, GstBuffer * src)
201 GstFlowReturn ret = GST_FLOW_OK;
202 GstV4l2MemoryGroup *group = NULL;
204 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
205 struct UserPtrData *data = NULL;
207 GST_LOG_OBJECT (pool, "importing userptr");
210 if (!gst_v4l2_is_buffer_valid (dest, &group))
213 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
214 flags = GST_MAP_READ;
216 flags = GST_MAP_WRITE;
218 data = g_slice_new0 (struct UserPtrData);
220 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
221 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
222 data->is_frame = TRUE;
224 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
227 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
228 data->frame.info.size, finfo->n_planes, data->frame.data,
229 data->frame.info.offset))
232 gsize offset[1] = { 0 };
235 data->is_frame = FALSE;
237 if (!gst_buffer_map (src, &data->map, flags))
240 ptr[0] = data->map.data;
242 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
243 data->map.size, 1, ptr, offset))
247 data->buffer = gst_buffer_ref (src);
249 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
250 data, (GDestroyNotify) _unmap_userptr_frame);
256 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
257 return GST_FLOW_ERROR;
261 GST_ERROR_OBJECT (pool, "could not map buffer");
262 g_slice_free (struct UserPtrData, data);
263 return GST_FLOW_ERROR;
267 GST_ERROR_OBJECT (pool, "failed to import data");
268 _unmap_userptr_frame (data);
269 return GST_FLOW_ERROR;
274 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
275 GstBuffer * dest, GstBuffer * src)
277 GstV4l2MemoryGroup *group = NULL;
278 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
279 guint n_mem = gst_buffer_n_memory (src);
282 GST_LOG_OBJECT (pool, "importing dmabuf");
284 if (!gst_v4l2_is_buffer_valid (dest, &group))
287 if (n_mem > GST_VIDEO_MAX_PLANES)
290 for (i = 0; i < n_mem; i++)
291 dma_mem[i] = gst_buffer_peek_memory (src, i);
293 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
297 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
298 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
304 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
305 return GST_FLOW_ERROR;
309 GST_ERROR_OBJECT (pool, "could not map buffer");
310 return GST_FLOW_ERROR;
314 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
315 return GST_FLOW_ERROR;
320 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
321 GstBuffer * dest, GstBuffer * src)
323 GstFlowReturn ret = GST_FLOW_OK;
324 gboolean own_src = FALSE;
327 if (pool->other_pool == NULL) {
328 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
329 return GST_FLOW_ERROR;
332 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
333 if (ret != GST_FLOW_OK) {
334 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
341 switch (pool->obj->mode) {
342 case GST_V4L2_IO_MMAP:
343 case GST_V4L2_IO_DMABUF:
344 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
346 case GST_V4L2_IO_USERPTR:
347 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
349 case GST_V4L2_IO_DMABUF_IMPORT:
350 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
357 gst_buffer_unref (src);
364 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
365 GstBufferPoolAcquireParams * params)
367 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
368 GstV4l2MemoryGroup *group = NULL;
369 GstBuffer *newbuf = NULL;
379 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
381 case GST_V4L2_IO_MMAP:
382 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
384 case GST_V4L2_IO_DMABUF:
385 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
388 case GST_V4L2_IO_USERPTR:
389 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
391 case GST_V4L2_IO_DMABUF_IMPORT:
392 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
396 g_assert_not_reached ();
402 newbuf = gst_buffer_new ();
404 for (i = 0; i < group->n_mem; i++)
405 gst_buffer_append_memory (newbuf, group->mem[i]);
406 } else if (newbuf == NULL) {
407 goto allocation_failed;
410 /* add metadata to raw video buffers */
411 if (pool->add_videometa)
412 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
413 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
414 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
415 info->offset, info->stride);
424 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
425 return GST_FLOW_ERROR;
430 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
432 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
433 GstV4l2Object *obj = pool->obj;
435 guint size, min_buffers, max_buffers;
436 GstAllocator *allocator;
437 GstAllocationParams params;
438 gboolean can_allocate = FALSE;
439 gboolean updated = FALSE;
442 pool->add_videometa =
443 gst_buffer_pool_config_has_option (config,
444 GST_BUFFER_POOL_OPTION_VIDEO_META);
446 /* parse the config and keep around */
447 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
451 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
454 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
457 gst_object_unref (pool->allocator);
458 pool->allocator = NULL;
461 case GST_V4L2_IO_DMABUF:
462 pool->allocator = gst_dmabuf_allocator_new ();
463 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, MMAP);
465 case GST_V4L2_IO_MMAP:
466 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, MMAP);
468 case GST_V4L2_IO_USERPTR:
469 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, USERPTR);
471 case GST_V4L2_IO_DMABUF_IMPORT:
472 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, DMABUF);
475 pool->allocator = g_object_ref (allocator);
476 pool->params = params;
477 /* No need to change the configuration */
481 g_assert_not_reached ();
485 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
487 min_buffers = GST_V4L2_MIN_BUFFERS;
488 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
491 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
493 max_buffers = VIDEO_MAX_FRAME;
494 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
497 if (min_buffers > max_buffers) {
499 min_buffers = max_buffers;
500 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
501 } else if (min_buffers != max_buffers) {
504 max_buffers = min_buffers;
505 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
509 if (!pool->add_videometa && obj->need_video_meta) {
510 GST_INFO_OBJECT (pool, "adding needed video meta");
512 gst_buffer_pool_config_add_option (config,
513 GST_BUFFER_POOL_OPTION_VIDEO_META);
517 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
520 gst_buffer_pool_config_set_allocator (config, pool->allocator, ¶ms);
522 /* keep a GstVideoInfo with defaults for the when we need to copy */
523 gst_video_info_from_caps (&pool->caps_info, caps);
526 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
528 /* If anything was changed documentation recommand to return FALSE */
529 return !updated && ret;
534 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
540 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
542 GstV4l2Object *obj = pool->obj;
545 case GST_V4L2_IO_MMAP:
546 case GST_V4L2_IO_USERPTR:
547 case GST_V4L2_IO_DMABUF:
548 case GST_V4L2_IO_DMABUF_IMPORT:
549 if (!pool->streaming) {
550 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
551 goto streamon_failed;
553 pool->streaming = TRUE;
555 GST_DEBUG_OBJECT (pool, "Started streaming");
566 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
573 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
575 GstV4l2Object *obj = pool->obj;
578 case GST_V4L2_IO_MMAP:
579 case GST_V4L2_IO_USERPTR:
580 case GST_V4L2_IO_DMABUF:
581 case GST_V4L2_IO_DMABUF_IMPORT:
582 if (pool->streaming) {
583 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
584 goto streamoff_failed;
586 pool->streaming = FALSE;
588 GST_DEBUG_OBJECT (pool, "Stopped streaming");
599 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
606 gst_v4l2_buffer_pool_group_released (GstV4l2BufferPool * pool)
608 GstBufferPoolAcquireParams params = { 0 };
609 GstBuffer *buffer = NULL;
612 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
615 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
617 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
619 if (ret == GST_FLOW_OK)
620 gst_buffer_unref (buffer);
624 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
626 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
627 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
628 GstV4l2Object *obj = pool->obj;
629 GstStructure *config;
631 guint size, min_buffers, max_buffers;
632 guint max_latency, min_latency, copy_threshold = 0;
633 gboolean can_allocate = FALSE;
635 GST_DEBUG_OBJECT (pool, "activating pool");
637 config = gst_buffer_pool_get_config (bpool);
638 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
642 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
648 case GST_V4L2_IO_DMABUF:
649 case GST_V4L2_IO_MMAP:
653 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, MMAP);
655 /* first, lets request buffers, and see how many we can get: */
656 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
658 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
661 if (count < GST_V4L2_MIN_BUFFERS) {
666 /* V4L2 buffer pool are often very limited in the amount of buffers it
667 * can offer. The copy_threshold will workaround this limitation by
668 * falling back to copy if the pipeline needed more buffers. This also
669 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
671 if (count != min_buffers) {
672 GST_WARNING_OBJECT (pool, "using %u buffers instead of %u",
675 copy_threshold = min_latency;
677 /* The initial minimum could be provide either by GstBufferPool or
684 case GST_V4L2_IO_USERPTR:
688 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, USERPTR);
690 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
692 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
693 V4L2_MEMORY_USERPTR);
695 /* There is no rational to not get what we asked */
696 if (count < min_buffers) {
704 case GST_V4L2_IO_DMABUF_IMPORT:
708 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, DMABUF);
710 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
712 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
715 /* There is no rational to not get what we asked */
716 if (count < min_buffers) {
727 g_assert_not_reached ();
732 max_latency = max_buffers;
734 max_latency = min_buffers;
737 pool->copy_threshold = copy_threshold;
738 pool->max_latency = max_latency;
739 pool->min_latency = min_latency;
740 pool->num_queued = 0;
742 if (max_buffers < min_buffers)
743 max_buffers = min_buffers;
745 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
747 pclass->set_config (bpool, config);
748 gst_structure_free (config);
750 if (pool->other_pool)
751 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
752 goto other_pool_failed;
754 /* now, allocate the buffers: */
755 if (!pclass->start (bpool))
758 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
759 pool->group_released_handler =
760 g_signal_connect_swapped (pool->vallocator, "group-released",
761 G_CALLBACK (gst_v4l2_buffer_pool_group_released), pool);
768 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
769 gst_structure_free (config);
774 GST_ERROR_OBJECT (pool,
775 "we received %d buffer from device '%s', we want at least %d",
776 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
777 gst_structure_free (config);
782 GST_ERROR_OBJECT (pool, "failed to start streaming");
787 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
788 GST_PTR_FORMAT, pool->other_pool);
794 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
796 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
797 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
801 GST_DEBUG_OBJECT (pool, "stopping pool");
803 if (pool->group_released_handler > 0) {
804 g_signal_handler_disconnect (pool->vallocator,
805 pool->group_released_handler);
806 pool->group_released_handler = 0;
809 if (pool->other_pool) {
810 gst_object_unref (pool->other_pool);
811 pool->other_pool = NULL;
814 if (!gst_v4l2_buffer_pool_streamoff (pool))
815 goto streamoff_failed;
817 if (pool->vallocator)
818 gst_v4l2_allocator_flush (pool->vallocator);
820 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
821 if (pool->buffers[i]) {
822 GstBuffer *buffer = pool->buffers[i];
824 pool->buffers[i] = NULL;
826 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
827 gst_buffer_unref (buffer);
829 pclass->release_buffer (bpool, buffer);
831 g_atomic_int_add (&pool->num_queued, -1);
835 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
837 if (ret && pool->vallocator) {
840 vret = gst_v4l2_allocator_stop (pool->vallocator);
842 if (vret == GST_V4L2_BUSY)
843 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
845 ret = (vret == GST_V4L2_OK);
852 GST_ERROR_OBJECT (pool, "device refused to stop streaming");
857 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
859 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
861 GST_DEBUG_OBJECT (pool, "start flushing");
863 gst_poll_set_flushing (pool->poll, TRUE);
865 GST_OBJECT_LOCK (pool);
867 g_cond_broadcast (&pool->empty_cond);
868 GST_OBJECT_UNLOCK (pool);
870 if (pool->other_pool)
871 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
875 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
877 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
878 GstV4l2Object *obj = pool->obj;
881 GST_DEBUG_OBJECT (pool, "stop flushing");
883 /* If we haven't started streaming yet, simply call streamon */
884 if (!pool->streaming)
887 if (pool->other_pool)
888 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
890 if (!gst_v4l2_buffer_pool_streamoff (pool))
893 gst_v4l2_allocator_flush (pool->vallocator);
895 /* Reset our state */
899 case GST_V4L2_IO_MMAP:
900 case GST_V4L2_IO_USERPTR:
901 case GST_V4L2_IO_DMABUF:
902 case GST_V4L2_IO_DMABUF_IMPORT:
906 num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
908 for (i = 0; i < num_allocated; i++) {
909 /* Re-enqueue buffers */
910 if (pool->buffers[i]) {
911 GstBufferPool *bpool = (GstBufferPool *) pool;
912 GstBuffer *buffer = pool->buffers[i];
914 pool->buffers[i] = NULL;
916 /* Remove qdata, this will unmap any map data in
917 * userptr/dmabuf-import */
918 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
919 GST_V4L2_IMPORT_QUARK, NULL, NULL);
921 if (V4L2_TYPE_IS_OUTPUT (obj->type))
922 gst_buffer_unref (buffer);
924 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
926 g_atomic_int_add (&pool->num_queued, -1);
933 g_assert_not_reached ();
938 /* Start streaming on capture device only */
939 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
940 gst_v4l2_buffer_pool_streamon (pool);
942 gst_poll_set_flushing (pool->poll, FALSE);
949 GST_ERROR_OBJECT (pool, "device refused to flush");
954 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
958 GST_OBJECT_LOCK (pool);
960 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
961 GST_OBJECT_UNLOCK (pool);
963 if (!pool->can_poll_device)
966 GST_LOG_OBJECT (pool, "polling device");
969 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
970 if (G_UNLIKELY (ret < 0)) {
978 GST_WARNING_OBJECT (pool,
979 "v4l2 device doesn't support polling. Disabling"
980 " using libv4l2 in this case may cause deadlocks");
981 pool->can_poll_device = FALSE;
988 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
997 GST_DEBUG_OBJECT (pool, "stop called");
998 return GST_FLOW_FLUSHING;
1002 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1003 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1004 return GST_FLOW_ERROR;
1008 static GstFlowReturn
1009 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1011 GstV4l2MemoryGroup *group = NULL;
1014 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1015 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1016 gst_buffer_unref (buf);
1020 index = group->buffer.index;
1022 if (pool->buffers[index] != NULL)
1023 goto already_queued;
1025 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1027 g_atomic_int_inc (&pool->num_queued);
1028 pool->buffers[index] = buf;
1030 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1033 GST_OBJECT_LOCK (pool);
1034 pool->empty = FALSE;
1035 g_cond_signal (&pool->empty_cond);
1036 GST_OBJECT_UNLOCK (pool);
1042 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1043 return GST_FLOW_ERROR;
1047 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1048 /* Mark broken buffer to the allocator */
1049 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1050 g_atomic_int_add (&pool->num_queued, -1);
1051 pool->buffers[index] = NULL;
1052 return GST_FLOW_ERROR;
1056 static GstFlowReturn
1057 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1061 GstV4l2Object *obj = pool->obj;
1062 GstClockTime timestamp;
1063 GstV4l2MemoryGroup *group;
1066 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1069 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1071 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1075 /* get our GstBuffer with that index from the pool, if the buffer was
1076 * outstanding we have a serious problem.
1078 outbuf = pool->buffers[group->buffer.index];
1082 /* mark the buffer outstanding */
1083 pool->buffers[group->buffer.index] = NULL;
1084 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1085 GST_OBJECT_LOCK (pool);
1087 GST_OBJECT_UNLOCK (pool);
1090 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1092 #ifndef GST_DISABLE_GST_DEBUG
1093 for (i = 0; i < group->n_mem; i++) {
1094 GST_LOG_OBJECT (pool,
1095 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1096 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1097 group->buffer.sequence, group->buffer.index, group->mem[i],
1098 group->planes[i].bytesused, i, group->buffer.flags,
1099 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1103 /* set top/bottom field first if v4l2_buffer has the information */
1104 switch (group->buffer.field) {
1105 case V4L2_FIELD_NONE:
1106 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1107 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1109 case V4L2_FIELD_INTERLACED_TB:
1110 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1111 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1113 case V4L2_FIELD_INTERLACED_BT:
1114 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1115 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1118 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1119 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1120 GST_FIXME_OBJECT (pool,
1121 "Unhandled enum v4l2_field %d - treating as progressive",
1122 group->buffer.field);
1125 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1126 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1127 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1129 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1132 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1133 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1135 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1144 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1149 return GST_FLOW_ERROR;
1153 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1154 group->buffer.index);
1155 return GST_FLOW_ERROR;
1159 static GstFlowReturn
1160 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1161 GstBufferPoolAcquireParams * params)
1164 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1165 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1166 GstV4l2Object *obj = pool->obj;
1168 GST_DEBUG_OBJECT (pool, "acquire");
1170 /* If this is being called to resurect a lost buffer */
1171 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1172 ret = pclass->acquire_buffer (bpool, buffer, params);
1176 switch (obj->type) {
1177 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1178 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1179 /* capture, This function should return a buffer with new captured data */
1180 switch (obj->mode) {
1181 case GST_V4L2_IO_RW:
1183 /* take empty buffer from the pool */
1184 ret = pclass->acquire_buffer (bpool, buffer, params);
1187 case GST_V4L2_IO_DMABUF:
1188 case GST_V4L2_IO_MMAP:
1190 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1191 * storage for our buffers. This function does poll first so we can
1192 * interrupt it fine. */
1193 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1194 if (G_UNLIKELY (ret != GST_FLOW_OK))
1198 case GST_V4L2_IO_USERPTR:
1199 case GST_V4L2_IO_DMABUF_IMPORT:
1201 /* dequeue filled buffer */
1202 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1206 ret = GST_FLOW_ERROR;
1207 g_assert_not_reached ();
1213 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1214 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1215 /* playback, This function should return an empty buffer */
1216 switch (obj->mode) {
1217 case GST_V4L2_IO_RW:
1218 /* get an empty buffer */
1219 ret = pclass->acquire_buffer (bpool, buffer, params);
1222 case GST_V4L2_IO_MMAP:
1223 case GST_V4L2_IO_DMABUF:
1224 case GST_V4L2_IO_USERPTR:
1225 case GST_V4L2_IO_DMABUF_IMPORT:
1226 /* get a free unqueued buffer */
1227 ret = pclass->acquire_buffer (bpool, buffer, params);
1231 ret = GST_FLOW_ERROR;
1232 g_assert_not_reached ();
1238 ret = GST_FLOW_ERROR;
1239 g_assert_not_reached ();
1247 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1249 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1250 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1251 GstV4l2Object *obj = pool->obj;
1253 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1255 switch (obj->type) {
1256 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1257 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1258 /* capture, put the buffer back in the queue so that we can refill it
1260 switch (obj->mode) {
1261 case GST_V4L2_IO_RW:
1262 /* release back in the pool */
1263 pclass->release_buffer (bpool, buffer);
1266 case GST_V4L2_IO_DMABUF:
1267 case GST_V4L2_IO_MMAP:
1268 case GST_V4L2_IO_USERPTR:
1269 case GST_V4L2_IO_DMABUF_IMPORT:
1271 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1272 /* queue back in the device */
1273 if (pool->other_pool)
1274 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1275 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1276 pclass->release_buffer (bpool, buffer);
1278 /* Simply release invalide/modified buffer, the allocator will
1279 * give it back later */
1280 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1281 pclass->release_buffer (bpool, buffer);
1286 g_assert_not_reached ();
1291 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1292 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1293 switch (obj->mode) {
1294 case GST_V4L2_IO_RW:
1295 /* release back in the pool */
1296 pclass->release_buffer (bpool, buffer);
1299 case GST_V4L2_IO_MMAP:
1300 case GST_V4L2_IO_DMABUF:
1301 case GST_V4L2_IO_USERPTR:
1302 case GST_V4L2_IO_DMABUF_IMPORT:
1304 GstV4l2MemoryGroup *group;
1307 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1308 /* Simply release invalide/modified buffer, the allocator will
1309 * give it back later */
1310 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1311 pclass->release_buffer (bpool, buffer);
1315 index = group->buffer.index;
1317 if (pool->buffers[index] == NULL) {
1318 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1321 /* Remove qdata, this will unmap any map data in userptr */
1322 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1323 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1325 /* reset to default size */
1326 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1328 /* playback, put the buffer back in the queue to refill later. */
1329 pclass->release_buffer (bpool, buffer);
1331 /* We keep a ref on queued buffer, so this should never happen */
1332 g_assert_not_reached ();
1338 g_assert_not_reached ();
1344 g_assert_not_reached ();
1350 gst_v4l2_buffer_pool_dispose (GObject * object)
1352 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1355 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1356 if (pool->buffers[i])
1357 gst_buffer_replace (&(pool->buffers[i]), NULL);
1360 if (pool->vallocator)
1361 gst_object_unref (pool->vallocator);
1362 pool->vallocator = NULL;
1364 if (pool->allocator)
1365 gst_object_unref (pool->allocator);
1366 pool->allocator = NULL;
1368 if (pool->other_pool)
1369 gst_object_unref (pool->other_pool);
1370 pool->other_pool = NULL;
1372 G_OBJECT_CLASS (parent_class)->dispose (object);
1376 gst_v4l2_buffer_pool_finalize (GObject * object)
1378 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1380 if (pool->video_fd >= 0)
1381 v4l2_close (pool->video_fd);
1383 gst_poll_free (pool->poll);
1385 /* FIXME Is this required to keep around ?
1386 * This can't be done in dispose method because we must not set pointer
1387 * to NULL as it is part of the v4l2object and dispose could be called
1389 gst_object_unref (pool->obj->element);
1391 g_cond_clear (&pool->empty_cond);
1393 /* FIXME have we done enough here ? */
1395 G_OBJECT_CLASS (parent_class)->finalize (object);
1399 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1401 pool->poll = gst_poll_new (TRUE);
1402 pool->can_poll_device = TRUE;
1403 g_cond_init (&pool->empty_cond);
1408 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1410 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1411 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1413 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1414 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1416 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1417 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1418 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1419 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1420 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1421 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1422 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1423 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1427 * gst_v4l2_buffer_pool_new:
1428 * @obj: the v4l2 object owning the pool
1430 * Construct a new buffer pool.
1432 * Returns: the new pool, use gst_object_unref() to free resources
1435 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1437 GstV4l2BufferPool *pool;
1438 GstStructure *config;
1439 gchar *name, *parent_name;
1442 fd = v4l2_dup (obj->video_fd);
1446 /* setting a significant unique name */
1447 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1448 name = g_strconcat (parent_name, ":", "pool:",
1449 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1450 g_free (parent_name);
1452 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1453 "name", name, NULL);
1456 gst_poll_fd_init (&pool->pollfd);
1457 pool->pollfd.fd = fd;
1458 gst_poll_add_fd (pool->poll, &pool->pollfd);
1459 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1460 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1462 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1464 pool->video_fd = fd;
1466 pool->can_poll_device = TRUE;
1468 GST_OBJECT_FLAG_SET (pool, GST_OBJECT_FLAGS (obj));
1471 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1472 if (pool->vallocator == NULL)
1473 goto allocator_failed;
1475 gst_object_ref (obj->element);
1477 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1478 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1479 /* This will simply set a default config, but will not configure the pool
1480 * because min and max are not valid */
1481 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1483 return GST_BUFFER_POOL (pool);
1488 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1493 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1498 static GstFlowReturn
1499 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1502 GstV4l2Object *obj = pool->obj;
1507 toread = obj->info.size;
1509 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1511 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1514 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1517 amount = v4l2_read (obj->video_fd, map.data, toread);
1519 if (amount == toread) {
1521 } else if (amount == -1) {
1522 if (errno == EAGAIN || errno == EINTR) {
1527 /* short reads can happen if a signal interrupts the read */
1532 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1533 gst_buffer_unmap (buf, &map);
1534 gst_buffer_resize (buf, 0, amount);
1541 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1546 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1547 (_("Error reading %d bytes from device '%s'."),
1548 toread, obj->videodev), GST_ERROR_SYSTEM);
1549 res = GST_FLOW_ERROR;
1554 gst_buffer_unmap (buf, &map);
1555 gst_buffer_resize (buf, 0, 0);
1561 * gst_v4l2_buffer_pool_process:
1562 * @bpool: a #GstBufferPool
1563 * @buf: a #GstBuffer, maybe be replaced
1565 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1566 * data from the device. For output devices, this functions send the contents of
1567 * @buf to the device for playback.
1569 * Returns: %GST_FLOW_OK on success.
1572 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1574 GstFlowReturn ret = GST_FLOW_OK;
1575 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1576 GstV4l2Object *obj = pool->obj;
1578 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1580 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1582 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1583 return GST_FLOW_FLUSHING;
1585 switch (obj->type) {
1586 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1587 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1589 switch (obj->mode) {
1590 case GST_V4L2_IO_RW:
1591 /* capture into the buffer */
1592 ret = gst_v4l2_do_read (pool, *buf);
1595 case GST_V4L2_IO_MMAP:
1596 case GST_V4L2_IO_DMABUF:
1600 if ((*buf)->pool == bpool) {
1601 if (gst_buffer_get_size (*buf) == 0) {
1602 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1603 goto buffer_corrupted;
1608 /* start copying buffers when we are running low on buffers */
1609 if (g_atomic_int_get (&pool->num_queued) < pool->copy_threshold) {
1612 if (GST_V4L2_OBJECT_CAN_ALLOCATE (pool, MMAP)) {
1614 if (gst_buffer_pool_acquire_buffer (bpool, ©,
1615 NULL) == GST_FLOW_OK) {
1616 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
1621 /* copy the buffer */
1622 copy = gst_buffer_copy_region (*buf,
1623 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1624 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1626 /* and requeue so that we can continue capturing */
1627 gst_buffer_unref (*buf);
1631 /* nothing, data was inside the buffer when we did _acquire() */
1635 /* buffer not from our pool, grab a frame and copy it into the target */
1636 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1639 /* An empty buffer on capture indicates the end of stream */
1640 if (gst_buffer_get_size (tmp) == 0) {
1641 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1643 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1644 goto buffer_corrupted;
1649 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1651 /* an queue the buffer again after the copy */
1652 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1654 if (ret != GST_FLOW_OK)
1659 case GST_V4L2_IO_USERPTR:
1661 struct UserPtrData *data;
1663 /* Replace our buffer with downstream allocated buffer */
1664 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1665 GST_V4L2_IMPORT_QUARK);
1666 gst_buffer_replace (buf, data->buffer);
1667 _unmap_userptr_frame (data);
1671 case GST_V4L2_IO_DMABUF_IMPORT:
1675 /* Replace our buffer with downstream allocated buffer */
1676 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1677 GST_V4L2_IMPORT_QUARK);
1678 gst_buffer_replace (buf, tmp);
1679 gst_buffer_unref (tmp);
1684 g_assert_not_reached ();
1689 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1690 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1692 switch (obj->mode) {
1693 case GST_V4L2_IO_RW:
1694 /* FIXME, do write() */
1695 GST_WARNING_OBJECT (pool, "implement write()");
1698 case GST_V4L2_IO_USERPTR:
1699 case GST_V4L2_IO_DMABUF_IMPORT:
1700 case GST_V4L2_IO_DMABUF:
1701 case GST_V4L2_IO_MMAP:
1703 GstBuffer *to_queue = NULL;
1704 GstV4l2MemoryGroup *group;
1707 if ((*buf)->pool != bpool)
1710 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1713 index = group->buffer.index;
1715 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1717 index = group->buffer.index;
1718 if (pool->buffers[index] != NULL) {
1719 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1723 /* we can queue directly */
1724 to_queue = gst_buffer_ref (*buf);
1727 if (to_queue == NULL) {
1728 GstBufferPoolAcquireParams params = { 0 };
1730 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1732 /* this can return EOS if all buffers are outstanding which would
1733 * be strange because we would expect the upstream element to have
1734 * allocated them and returned to us.. */
1735 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1736 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1737 if (ret != GST_FLOW_OK)
1738 goto acquire_failed;
1740 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1741 if (ret != GST_FLOW_OK) {
1742 gst_buffer_unref (to_queue);
1743 goto prepare_failed;
1747 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1750 /* if we are not streaming yet (this is the first buffer, start
1752 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1753 /* don't check return value because qbuf would have failed */
1754 gst_v4l2_is_buffer_valid (to_queue, &group);
1756 /* qbuf has taken the ref of the to_queue buffer but we are no in
1757 * streaming state, so the flush logic won't be performed.
1758 * To avoid leaks, flush the allocator and restore the queued
1759 * buffer as non-queued */
1760 gst_v4l2_allocator_flush (pool->vallocator);
1762 pool->buffers[group->buffer.index] = NULL;
1764 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1765 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1766 gst_buffer_unref (to_queue);
1767 g_atomic_int_add (&pool->num_queued, -1);
1771 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1773 /* all buffers are queued, try to dequeue one and release it back
1774 * into the pool so that _acquire can get to it again. */
1775 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1776 if (ret == GST_FLOW_OK)
1777 /* release the rendered buffer back into the pool. This wakes up any
1778 * thread waiting for a buffer in _acquire(). */
1779 gst_buffer_unref (out);
1784 g_assert_not_reached ();
1789 g_assert_not_reached ();
1798 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1803 GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1804 gst_buffer_unref (*buf);
1806 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1810 GST_DEBUG_OBJECT (pool, "end of stream reached");
1811 gst_buffer_unref (*buf);
1813 return GST_V4L2_FLOW_LAST_BUFFER;
1817 if (ret == GST_FLOW_FLUSHING)
1818 GST_DEBUG_OBJECT (pool, "flushing");
1820 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1821 gst_flow_get_name (ret));
1826 GST_ERROR_OBJECT (pool, "failed to prepare data");
1831 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1836 GST_ERROR_OBJECT (pool, "failed to start streaming");
1837 return GST_FLOW_ERROR;
1842 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1843 GstBufferPool * other_pool)
1845 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1847 if (pool->other_pool)
1848 gst_object_unref (pool->other_pool);
1849 pool->other_pool = gst_object_ref (other_pool);