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 /* keep a GstVideoInfo with defaults for the when we need to copy */
521 gst_video_info_from_caps (&pool->caps_info, caps);
524 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
526 /* If anything was changed documentation recommand to return FALSE */
527 return !updated && ret;
532 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
538 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
540 GstV4l2Object *obj = pool->obj;
543 case GST_V4L2_IO_MMAP:
544 case GST_V4L2_IO_USERPTR:
545 case GST_V4L2_IO_DMABUF:
546 case GST_V4L2_IO_DMABUF_IMPORT:
547 if (!pool->streaming) {
548 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
549 goto streamon_failed;
551 pool->streaming = TRUE;
553 GST_DEBUG_OBJECT (pool, "Started streaming");
564 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
571 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
573 GstV4l2Object *obj = pool->obj;
576 case GST_V4L2_IO_MMAP:
577 case GST_V4L2_IO_USERPTR:
578 case GST_V4L2_IO_DMABUF:
579 case GST_V4L2_IO_DMABUF_IMPORT:
580 if (pool->streaming) {
581 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
582 goto streamoff_failed;
584 pool->streaming = FALSE;
586 GST_DEBUG_OBJECT (pool, "Stopped streaming");
597 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
604 gst_v4l2_buffer_pool_group_released (GstV4l2BufferPool * pool)
606 GstBufferPoolAcquireParams params = { 0 };
607 GstBuffer *buffer = NULL;
610 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
613 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
615 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
617 if (ret == GST_FLOW_OK)
618 gst_buffer_unref (buffer);
622 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
624 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
625 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
626 GstV4l2Object *obj = pool->obj;
627 GstStructure *config;
629 guint size, min_buffers, max_buffers;
630 guint max_latency, min_latency, copy_threshold = 0;
631 gboolean can_allocate = FALSE;
633 GST_DEBUG_OBJECT (pool, "activating pool");
635 config = gst_buffer_pool_get_config (bpool);
636 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
640 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
646 case GST_V4L2_IO_DMABUF:
647 case GST_V4L2_IO_MMAP:
651 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, MMAP);
653 /* first, lets request buffers, and see how many we can get: */
654 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
656 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
659 if (count < GST_V4L2_MIN_BUFFERS) {
664 /* V4L2 buffer pool are often very limited in the amount of buffers it
665 * can offer. The copy_threshold will workaround this limitation by
666 * falling back to copy if the pipeline needed more buffers. This also
667 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
669 if (count != min_buffers) {
670 GST_WARNING_OBJECT (pool, "using %u buffers instead of %u",
673 copy_threshold = min_latency;
675 /* The initial minimum could be provide either by GstBufferPool or
682 case GST_V4L2_IO_USERPTR:
686 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, USERPTR);
688 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
690 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
691 V4L2_MEMORY_USERPTR);
693 /* There is no rational to not get what we asked */
694 if (count < min_buffers) {
702 case GST_V4L2_IO_DMABUF_IMPORT:
706 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (pool, DMABUF);
708 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
710 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
713 /* There is no rational to not get what we asked */
714 if (count < min_buffers) {
725 g_assert_not_reached ();
730 max_latency = max_buffers;
732 max_latency = min_buffers;
735 pool->copy_threshold = copy_threshold;
736 pool->max_latency = max_latency;
737 pool->min_latency = min_latency;
738 pool->num_queued = 0;
740 if (max_buffers < min_buffers)
741 max_buffers = min_buffers;
743 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
745 pclass->set_config (bpool, config);
746 gst_structure_free (config);
748 if (pool->other_pool)
749 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
750 goto other_pool_failed;
752 /* now, allocate the buffers: */
753 if (!pclass->start (bpool))
756 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
757 pool->group_released_handler =
758 g_signal_connect_swapped (pool->vallocator, "group-released",
759 G_CALLBACK (gst_v4l2_buffer_pool_group_released), pool);
766 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
767 gst_structure_free (config);
772 GST_ERROR_OBJECT (pool,
773 "we received %d buffer from device '%s', we want at least %d",
774 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
775 gst_structure_free (config);
780 GST_ERROR_OBJECT (pool, "failed to start streaming");
785 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
786 GST_PTR_FORMAT, pool->other_pool);
792 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
794 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
795 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
799 GST_DEBUG_OBJECT (pool, "stopping pool");
801 if (pool->group_released_handler > 0) {
802 g_signal_handler_disconnect (pool->vallocator,
803 pool->group_released_handler);
804 pool->group_released_handler = 0;
807 if (pool->other_pool) {
808 gst_object_unref (pool->other_pool);
809 pool->other_pool = NULL;
812 if (!gst_v4l2_buffer_pool_streamoff (pool))
813 goto streamoff_failed;
815 if (pool->vallocator)
816 gst_v4l2_allocator_flush (pool->vallocator);
818 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
819 if (pool->buffers[i]) {
820 GstBuffer *buffer = pool->buffers[i];
822 pool->buffers[i] = NULL;
824 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
825 gst_buffer_unref (buffer);
827 pclass->release_buffer (bpool, buffer);
829 g_atomic_int_add (&pool->num_queued, -1);
833 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
835 if (ret && pool->vallocator) {
838 vret = gst_v4l2_allocator_stop (pool->vallocator);
840 if (vret == GST_V4L2_BUSY)
841 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
843 ret = (vret == GST_V4L2_OK);
850 GST_ERROR_OBJECT (pool, "device refused to stop streaming");
855 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
857 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
859 GST_DEBUG_OBJECT (pool, "start flushing");
861 gst_poll_set_flushing (pool->poll, TRUE);
863 GST_OBJECT_LOCK (pool);
865 g_cond_broadcast (&pool->empty_cond);
866 GST_OBJECT_UNLOCK (pool);
868 if (pool->other_pool)
869 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
873 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
875 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
876 GstV4l2Object *obj = pool->obj;
879 GST_DEBUG_OBJECT (pool, "stop flushing");
881 /* If we haven't started streaming yet, simply call streamon */
882 if (!pool->streaming)
885 if (pool->other_pool)
886 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
888 if (!gst_v4l2_buffer_pool_streamoff (pool))
891 gst_v4l2_allocator_flush (pool->vallocator);
893 /* Reset our state */
897 case GST_V4L2_IO_MMAP:
898 case GST_V4L2_IO_USERPTR:
899 case GST_V4L2_IO_DMABUF:
900 case GST_V4L2_IO_DMABUF_IMPORT:
904 num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
906 for (i = 0; i < num_allocated; i++) {
907 /* Re-enqueue buffers */
908 if (pool->buffers[i]) {
909 GstBufferPool *bpool = (GstBufferPool *) pool;
910 GstBuffer *buffer = pool->buffers[i];
912 pool->buffers[i] = NULL;
914 /* Remove qdata, this will unmap any map data in
915 * userptr/dmabuf-import */
916 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
917 GST_V4L2_IMPORT_QUARK, NULL, NULL);
919 if (V4L2_TYPE_IS_OUTPUT (obj->type))
920 gst_buffer_unref (buffer);
922 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
924 g_atomic_int_add (&pool->num_queued, -1);
931 g_assert_not_reached ();
936 /* Start streaming on capture device only */
937 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
938 gst_v4l2_buffer_pool_streamon (pool);
940 gst_poll_set_flushing (pool->poll, FALSE);
947 GST_ERROR_OBJECT (pool, "device refused to flush");
952 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
956 GST_OBJECT_LOCK (pool);
958 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
959 GST_OBJECT_UNLOCK (pool);
961 if (!pool->can_poll_device)
964 GST_LOG_OBJECT (pool, "polling device");
967 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
968 if (G_UNLIKELY (ret < 0)) {
976 GST_WARNING_OBJECT (pool,
977 "v4l2 device doesn't support polling. Disabling"
978 " using libv4l2 in this case may cause deadlocks");
979 pool->can_poll_device = FALSE;
986 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
995 GST_DEBUG_OBJECT (pool, "stop called");
996 return GST_FLOW_FLUSHING;
1000 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1001 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1002 return GST_FLOW_ERROR;
1006 static GstFlowReturn
1007 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1009 GstV4l2MemoryGroup *group = NULL;
1012 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1013 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1014 gst_buffer_unref (buf);
1018 index = group->buffer.index;
1020 if (pool->buffers[index] != NULL)
1021 goto already_queued;
1023 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1025 g_atomic_int_inc (&pool->num_queued);
1026 pool->buffers[index] = buf;
1028 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1031 GST_OBJECT_LOCK (pool);
1032 pool->empty = FALSE;
1033 g_cond_signal (&pool->empty_cond);
1034 GST_OBJECT_UNLOCK (pool);
1040 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1041 return GST_FLOW_ERROR;
1045 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1046 /* Mark broken buffer to the allocator */
1047 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1048 g_atomic_int_add (&pool->num_queued, -1);
1049 pool->buffers[index] = NULL;
1050 return GST_FLOW_ERROR;
1054 static GstFlowReturn
1055 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1059 GstV4l2Object *obj = pool->obj;
1060 GstClockTime timestamp;
1061 GstV4l2MemoryGroup *group;
1064 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1067 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1069 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1073 /* get our GstBuffer with that index from the pool, if the buffer was
1074 * outstanding we have a serious problem.
1076 outbuf = pool->buffers[group->buffer.index];
1080 /* mark the buffer outstanding */
1081 pool->buffers[group->buffer.index] = NULL;
1082 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1083 GST_OBJECT_LOCK (pool);
1085 GST_OBJECT_UNLOCK (pool);
1088 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1090 #ifndef GST_DISABLE_GST_DEBUG
1091 for (i = 0; i < group->n_mem; i++) {
1092 GST_LOG_OBJECT (pool,
1093 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1094 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1095 group->buffer.sequence, group->buffer.index, group->mem[i],
1096 group->planes[i].bytesused, i, group->buffer.flags,
1097 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1101 /* set top/bottom field first if v4l2_buffer has the information */
1102 switch (group->buffer.field) {
1103 case V4L2_FIELD_NONE:
1104 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1105 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1107 case V4L2_FIELD_INTERLACED_TB:
1108 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1109 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1111 case V4L2_FIELD_INTERLACED_BT:
1112 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1113 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1116 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1117 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1118 GST_FIXME_OBJECT (pool,
1119 "Unhandled enum v4l2_field %d - treating as progressive",
1120 group->buffer.field);
1123 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1124 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1125 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1127 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1130 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1139 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1144 return GST_FLOW_ERROR;
1148 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1149 group->buffer.index);
1150 return GST_FLOW_ERROR;
1154 static GstFlowReturn
1155 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1156 GstBufferPoolAcquireParams * params)
1159 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1160 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1161 GstV4l2Object *obj = pool->obj;
1163 GST_DEBUG_OBJECT (pool, "acquire");
1165 /* If this is being called to resurect a lost buffer */
1166 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1167 ret = pclass->acquire_buffer (bpool, buffer, params);
1171 switch (obj->type) {
1172 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1173 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1174 /* capture, This function should return a buffer with new captured data */
1175 switch (obj->mode) {
1176 case GST_V4L2_IO_RW:
1178 /* take empty buffer from the pool */
1179 ret = pclass->acquire_buffer (bpool, buffer, params);
1182 case GST_V4L2_IO_DMABUF:
1183 case GST_V4L2_IO_MMAP:
1185 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1186 * storage for our buffers. This function does poll first so we can
1187 * interrupt it fine. */
1188 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1189 if (G_UNLIKELY (ret != GST_FLOW_OK))
1193 case GST_V4L2_IO_USERPTR:
1194 case GST_V4L2_IO_DMABUF_IMPORT:
1196 /* dequeue filled buffer */
1197 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1201 ret = GST_FLOW_ERROR;
1202 g_assert_not_reached ();
1208 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1209 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1210 /* playback, This function should return an empty buffer */
1211 switch (obj->mode) {
1212 case GST_V4L2_IO_RW:
1213 /* get an empty buffer */
1214 ret = pclass->acquire_buffer (bpool, buffer, params);
1217 case GST_V4L2_IO_MMAP:
1218 case GST_V4L2_IO_DMABUF:
1219 case GST_V4L2_IO_USERPTR:
1220 case GST_V4L2_IO_DMABUF_IMPORT:
1221 /* get a free unqueued buffer */
1222 ret = pclass->acquire_buffer (bpool, buffer, params);
1226 ret = GST_FLOW_ERROR;
1227 g_assert_not_reached ();
1233 ret = GST_FLOW_ERROR;
1234 g_assert_not_reached ();
1242 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1244 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1245 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1246 GstV4l2Object *obj = pool->obj;
1248 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1250 switch (obj->type) {
1251 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1252 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1253 /* capture, put the buffer back in the queue so that we can refill it
1255 switch (obj->mode) {
1256 case GST_V4L2_IO_RW:
1257 /* release back in the pool */
1258 pclass->release_buffer (bpool, buffer);
1261 case GST_V4L2_IO_DMABUF:
1262 case GST_V4L2_IO_MMAP:
1263 case GST_V4L2_IO_USERPTR:
1264 case GST_V4L2_IO_DMABUF_IMPORT:
1266 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1267 /* queue back in the device */
1268 if (pool->other_pool)
1269 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1270 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1271 pclass->release_buffer (bpool, buffer);
1273 /* Simply release invalide/modified buffer, the allocator will
1274 * give it back later */
1275 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1276 pclass->release_buffer (bpool, buffer);
1281 g_assert_not_reached ();
1286 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1287 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1288 switch (obj->mode) {
1289 case GST_V4L2_IO_RW:
1290 /* release back in the pool */
1291 pclass->release_buffer (bpool, buffer);
1294 case GST_V4L2_IO_MMAP:
1295 case GST_V4L2_IO_DMABUF:
1296 case GST_V4L2_IO_USERPTR:
1297 case GST_V4L2_IO_DMABUF_IMPORT:
1299 GstV4l2MemoryGroup *group;
1302 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1303 /* Simply release invalide/modified buffer, the allocator will
1304 * give it back later */
1305 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1306 pclass->release_buffer (bpool, buffer);
1310 index = group->buffer.index;
1312 if (pool->buffers[index] == NULL) {
1313 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1316 /* Remove qdata, this will unmap any map data in userptr */
1317 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1318 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1320 /* reset to default size */
1321 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1323 /* playback, put the buffer back in the queue to refill later. */
1324 pclass->release_buffer (bpool, buffer);
1326 /* We keep a ref on queued buffer, so this should never happen */
1327 g_assert_not_reached ();
1333 g_assert_not_reached ();
1339 g_assert_not_reached ();
1345 gst_v4l2_buffer_pool_dispose (GObject * object)
1347 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1350 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1351 if (pool->buffers[i])
1352 gst_buffer_replace (&(pool->buffers[i]), NULL);
1355 if (pool->vallocator)
1356 gst_object_unref (pool->vallocator);
1357 pool->vallocator = NULL;
1359 if (pool->allocator)
1360 gst_object_unref (pool->allocator);
1361 pool->allocator = NULL;
1363 if (pool->other_pool)
1364 gst_object_unref (pool->other_pool);
1365 pool->other_pool = NULL;
1367 G_OBJECT_CLASS (parent_class)->dispose (object);
1371 gst_v4l2_buffer_pool_finalize (GObject * object)
1373 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1375 if (pool->video_fd >= 0)
1376 v4l2_close (pool->video_fd);
1378 gst_poll_free (pool->poll);
1380 /* FIXME Is this required to keep around ?
1381 * This can't be done in dispose method because we must not set pointer
1382 * to NULL as it is part of the v4l2object and dispose could be called
1384 gst_object_unref (pool->obj->element);
1386 g_cond_clear (&pool->empty_cond);
1388 /* FIXME have we done enough here ? */
1390 G_OBJECT_CLASS (parent_class)->finalize (object);
1394 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1396 pool->poll = gst_poll_new (TRUE);
1397 pool->can_poll_device = TRUE;
1398 g_cond_init (&pool->empty_cond);
1403 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1405 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1406 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1408 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1409 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1411 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1412 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1413 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1414 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1415 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1416 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1417 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1418 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1422 * gst_v4l2_buffer_pool_new:
1423 * @obj: the v4l2 object owning the pool
1425 * Construct a new buffer pool.
1427 * Returns: the new pool, use gst_object_unref() to free resources
1430 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1432 GstV4l2BufferPool *pool;
1433 GstStructure *config;
1434 gchar *name, *parent_name;
1437 fd = v4l2_dup (obj->video_fd);
1441 /* setting a significant unique name */
1442 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1443 name = g_strconcat (parent_name, ":", "pool:",
1444 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1445 g_free (parent_name);
1447 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1448 "name", name, NULL);
1451 gst_poll_fd_init (&pool->pollfd);
1452 pool->pollfd.fd = fd;
1453 gst_poll_add_fd (pool->poll, &pool->pollfd);
1454 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1455 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1457 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1459 pool->video_fd = fd;
1461 pool->can_poll_device = TRUE;
1463 GST_OBJECT_FLAG_SET (pool, GST_OBJECT_FLAGS (obj));
1466 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1467 if (pool->vallocator == NULL)
1468 goto allocator_failed;
1470 gst_object_ref (obj->element);
1472 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1473 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1474 /* This will simply set a default config, but will not configure the pool
1475 * because min and max are not valid */
1476 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1478 return GST_BUFFER_POOL (pool);
1483 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1488 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1493 static GstFlowReturn
1494 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1497 GstV4l2Object *obj = pool->obj;
1502 toread = obj->info.size;
1504 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1506 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1509 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1512 amount = v4l2_read (obj->video_fd, map.data, toread);
1514 if (amount == toread) {
1516 } else if (amount == -1) {
1517 if (errno == EAGAIN || errno == EINTR) {
1522 /* short reads can happen if a signal interrupts the read */
1527 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1528 gst_buffer_unmap (buf, &map);
1529 gst_buffer_resize (buf, 0, amount);
1536 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1541 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1542 (_("Error reading %d bytes from device '%s'."),
1543 toread, obj->videodev), GST_ERROR_SYSTEM);
1544 res = GST_FLOW_ERROR;
1549 gst_buffer_unmap (buf, &map);
1550 gst_buffer_resize (buf, 0, 0);
1556 * gst_v4l2_buffer_pool_process:
1557 * @bpool: a #GstBufferPool
1558 * @buf: a #GstBuffer, maybe be replaced
1560 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1561 * data from the device. For output devices, this functions send the contents of
1562 * @buf to the device for playback.
1564 * Returns: %GST_FLOW_OK on success.
1567 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1569 GstFlowReturn ret = GST_FLOW_OK;
1570 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1571 GstV4l2Object *obj = pool->obj;
1573 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1575 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1577 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1578 return GST_FLOW_FLUSHING;
1580 switch (obj->type) {
1581 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1582 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1584 switch (obj->mode) {
1585 case GST_V4L2_IO_RW:
1586 /* capture into the buffer */
1587 ret = gst_v4l2_do_read (pool, *buf);
1590 case GST_V4L2_IO_MMAP:
1591 case GST_V4L2_IO_DMABUF:
1595 if ((*buf)->pool == bpool) {
1596 if (gst_buffer_get_size (*buf) == 0)
1599 /* start copying buffers when we are running low on buffers */
1600 if (g_atomic_int_get (&pool->num_queued) < pool->copy_threshold) {
1603 if (GST_V4L2_OBJECT_CAN_ALLOCATE (pool, MMAP)) {
1605 if (gst_buffer_pool_acquire_buffer (bpool, ©,
1606 NULL) == GST_FLOW_OK) {
1607 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
1612 /* copy the buffer */
1613 copy = gst_buffer_copy_region (*buf,
1614 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1615 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1617 /* and requeue so that we can continue capturing */
1618 gst_buffer_unref (*buf);
1622 /* nothing, data was inside the buffer when we did _acquire() */
1626 /* buffer not from our pool, grab a frame and copy it into the target */
1627 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1630 /* An empty buffer on capture indicates the end of stream */
1631 if (gst_buffer_get_size (tmp) == 0) {
1632 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1636 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1638 /* an queue the buffer again after the copy */
1639 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1641 if (ret != GST_FLOW_OK)
1646 case GST_V4L2_IO_USERPTR:
1648 struct UserPtrData *data;
1650 /* Replace our buffer with downstream allocated buffer */
1651 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1652 GST_V4L2_IMPORT_QUARK);
1653 gst_buffer_replace (buf, data->buffer);
1654 _unmap_userptr_frame (data);
1658 case GST_V4L2_IO_DMABUF_IMPORT:
1662 /* Replace our buffer with downstream allocated buffer */
1663 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1664 GST_V4L2_IMPORT_QUARK);
1665 gst_buffer_replace (buf, tmp);
1666 gst_buffer_unref (tmp);
1671 g_assert_not_reached ();
1676 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1677 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1679 switch (obj->mode) {
1680 case GST_V4L2_IO_RW:
1681 /* FIXME, do write() */
1682 GST_WARNING_OBJECT (pool, "implement write()");
1685 case GST_V4L2_IO_USERPTR:
1686 case GST_V4L2_IO_DMABUF_IMPORT:
1687 case GST_V4L2_IO_DMABUF:
1688 case GST_V4L2_IO_MMAP:
1690 GstBuffer *to_queue = NULL;
1691 GstV4l2MemoryGroup *group;
1694 if ((*buf)->pool != bpool)
1697 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1700 index = group->buffer.index;
1702 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1704 index = group->buffer.index;
1705 if (pool->buffers[index] != NULL) {
1706 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1710 /* we can queue directly */
1711 to_queue = gst_buffer_ref (*buf);
1714 if (to_queue == NULL) {
1715 GstBufferPoolAcquireParams params = { 0 };
1717 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1719 /* this can return EOS if all buffers are outstanding which would
1720 * be strange because we would expect the upstream element to have
1721 * allocated them and returned to us.. */
1722 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1723 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1724 if (ret != GST_FLOW_OK)
1725 goto acquire_failed;
1727 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1728 if (ret != GST_FLOW_OK) {
1729 gst_buffer_unref (to_queue);
1730 goto prepare_failed;
1734 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1737 /* if we are not streaming yet (this is the first buffer, start
1739 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1740 /* don't check return value because qbuf would have failed */
1741 gst_v4l2_is_buffer_valid (to_queue, &group);
1743 /* qbuf has taken the ref of the to_queue buffer but we are no in
1744 * streaming state, so the flush logic won't be performed.
1745 * To avoid leaks, flush the allocator and restore the queued
1746 * buffer as non-queued */
1747 gst_v4l2_allocator_flush (pool->vallocator);
1749 pool->buffers[group->buffer.index] = NULL;
1751 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1752 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1753 gst_buffer_unref (to_queue);
1754 g_atomic_int_add (&pool->num_queued, -1);
1758 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1760 /* all buffers are queued, try to dequeue one and release it back
1761 * into the pool so that _acquire can get to it again. */
1762 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1763 if (ret == GST_FLOW_OK)
1764 /* release the rendered buffer back into the pool. This wakes up any
1765 * thread waiting for a buffer in _acquire(). */
1766 gst_buffer_unref (out);
1771 g_assert_not_reached ();
1776 g_assert_not_reached ();
1785 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1790 GST_DEBUG_OBJECT (pool, "end of stream reached");
1791 return GST_FLOW_EOS;
1795 if (ret == GST_FLOW_FLUSHING)
1796 GST_DEBUG_OBJECT (pool, "flushing");
1798 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1799 gst_flow_get_name (ret));
1804 GST_ERROR_OBJECT (pool, "failed to prepare data");
1809 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1814 GST_ERROR_OBJECT (pool, "failed to start streaming");
1815 return GST_FLOW_ERROR;
1820 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1821 GstBufferPool * other_pool)
1823 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1825 if (pool->other_pool)
1826 gst_object_unref (pool->other_pool);
1827 pool->other_pool = gst_object_ref (other_pool);