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_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
465 case GST_V4L2_IO_MMAP:
466 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
468 case GST_V4L2_IO_USERPTR:
470 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
472 case GST_V4L2_IO_DMABUF_IMPORT:
473 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
477 pool->allocator = g_object_ref (allocator);
478 pool->params = params;
479 /* No need to change the configuration */
483 g_assert_not_reached ();
487 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
488 * instability and crash, disable it for now */
489 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
490 GST_WARNING_OBJECT (pool,
491 "libv4l2 converter detected, disabling CREATE_BUFS");
492 can_allocate = FALSE;
495 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
497 min_buffers = GST_V4L2_MIN_BUFFERS;
498 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
501 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
503 max_buffers = VIDEO_MAX_FRAME;
504 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
507 if (min_buffers > max_buffers) {
509 min_buffers = max_buffers;
510 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
511 } else if (min_buffers != max_buffers) {
514 max_buffers = min_buffers;
515 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
519 if (!pool->add_videometa && obj->need_video_meta) {
520 GST_INFO_OBJECT (pool, "adding needed video meta");
522 gst_buffer_pool_config_add_option (config,
523 GST_BUFFER_POOL_OPTION_VIDEO_META);
526 /* Always update the config to ensure the configured size matches */
527 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
530 /* keep a GstVideoInfo with defaults for the when we need to copy */
531 gst_video_info_from_caps (&pool->caps_info, caps);
534 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
536 /* If anything was changed documentation recommand to return FALSE */
537 return !updated && ret;
542 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
548 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
550 GstV4l2Object *obj = pool->obj;
553 case GST_V4L2_IO_MMAP:
554 case GST_V4L2_IO_USERPTR:
555 case GST_V4L2_IO_DMABUF:
556 case GST_V4L2_IO_DMABUF_IMPORT:
557 if (!pool->streaming) {
558 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
559 goto streamon_failed;
561 pool->streaming = TRUE;
563 GST_DEBUG_OBJECT (pool, "Started streaming");
574 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
581 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
583 GstV4l2Object *obj = pool->obj;
586 case GST_V4L2_IO_MMAP:
587 case GST_V4L2_IO_USERPTR:
588 case GST_V4L2_IO_DMABUF:
589 case GST_V4L2_IO_DMABUF_IMPORT:
590 if (pool->streaming) {
591 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
592 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
593 errno, g_strerror (errno));
595 pool->streaming = FALSE;
597 GST_DEBUG_OBJECT (pool, "Stopped streaming");
599 if (pool->vallocator)
600 gst_v4l2_allocator_flush (pool->vallocator);
609 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
611 GstBufferPoolAcquireParams params = { 0 };
612 GstBuffer *buffer = NULL;
615 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
618 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
620 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
622 if (ret == GST_FLOW_OK)
623 gst_buffer_unref (buffer);
629 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
631 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
632 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
633 GstV4l2Object *obj = pool->obj;
634 GstStructure *config;
636 guint size, min_buffers, max_buffers;
637 guint max_latency, min_latency, copy_threshold = 0;
638 gboolean can_allocate = FALSE;
640 GST_DEBUG_OBJECT (pool, "activating pool");
642 config = gst_buffer_pool_get_config (bpool);
643 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
647 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
653 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
654 * of MMAP. In this case, the first read initialize the queues, but the
655 * poll before that will always fail. Doing an empty read, forces the
656 * queue to be initialized now. We only do this if we have a streaming
658 if (obj->vcap.capabilities & V4L2_CAP_STREAMING)
659 v4l2_read (obj->video_fd, NULL, 0);
662 case GST_V4L2_IO_DMABUF:
663 case GST_V4L2_IO_MMAP:
667 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
669 /* first, lets request buffers, and see how many we can get: */
670 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
672 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
675 if (count < GST_V4L2_MIN_BUFFERS) {
680 /* V4L2 buffer pool are often very limited in the amount of buffers it
681 * can offer. The copy_threshold will workaround this limitation by
682 * falling back to copy if the pipeline needed more buffers. This also
683 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
685 if (count != min_buffers) {
686 GST_WARNING_OBJECT (pool, "using %u buffers instead of %u",
689 copy_threshold = min_latency;
691 /* The initial minimum could be provide either by GstBufferPool or
698 case GST_V4L2_IO_USERPTR:
703 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
705 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
707 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
708 V4L2_MEMORY_USERPTR);
710 /* There is no rational to not get what we asked */
711 if (count < min_buffers) {
719 case GST_V4L2_IO_DMABUF_IMPORT:
723 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
725 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
727 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
730 /* There is no rational to not get what we asked */
731 if (count < min_buffers) {
742 g_assert_not_reached ();
747 max_latency = max_buffers;
749 max_latency = min_buffers;
752 pool->copy_threshold = copy_threshold;
753 pool->max_latency = max_latency;
754 pool->min_latency = min_latency;
755 pool->num_queued = 0;
757 if (max_buffers != 0 && max_buffers < min_buffers)
758 max_buffers = min_buffers;
760 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
762 pclass->set_config (bpool, config);
763 gst_structure_free (config);
765 if (pool->other_pool)
766 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
767 goto other_pool_failed;
769 /* now, allocate the buffers: */
770 if (!pclass->start (bpool))
773 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
774 pool->group_released_handler =
775 g_signal_connect_swapped (pool->vallocator, "group-released",
776 G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
783 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
784 gst_structure_free (config);
789 GST_ERROR_OBJECT (pool,
790 "we received %d buffer from device '%s', we want at least %d",
791 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
792 gst_structure_free (config);
797 GST_ERROR_OBJECT (pool, "failed to start streaming");
802 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
803 GST_PTR_FORMAT, pool->other_pool);
809 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
811 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
812 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
816 GST_DEBUG_OBJECT (pool, "stopping pool");
818 if (pool->group_released_handler > 0) {
819 g_signal_handler_disconnect (pool->vallocator,
820 pool->group_released_handler);
821 pool->group_released_handler = 0;
824 if (pool->other_pool) {
825 gst_object_unref (pool->other_pool);
826 pool->other_pool = NULL;
829 gst_v4l2_buffer_pool_streamoff (pool);
831 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
832 if (pool->buffers[i]) {
833 GstBuffer *buffer = pool->buffers[i];
835 pool->buffers[i] = NULL;
837 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
838 gst_buffer_unref (buffer);
840 pclass->release_buffer (bpool, buffer);
842 g_atomic_int_add (&pool->num_queued, -1);
846 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
848 if (ret && pool->vallocator) {
851 vret = gst_v4l2_allocator_stop (pool->vallocator);
853 if (vret == GST_V4L2_BUSY)
854 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
856 ret = (vret == GST_V4L2_OK);
863 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
865 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
867 GST_DEBUG_OBJECT (pool, "start flushing");
869 gst_poll_set_flushing (pool->poll, TRUE);
871 GST_OBJECT_LOCK (pool);
873 g_cond_broadcast (&pool->empty_cond);
874 GST_OBJECT_UNLOCK (pool);
876 if (pool->other_pool)
877 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
881 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
883 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
884 GstV4l2Object *obj = pool->obj;
887 GST_DEBUG_OBJECT (pool, "stop flushing");
889 /* If we haven't started streaming yet, simply call streamon */
890 if (!pool->streaming)
893 if (pool->other_pool)
894 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
896 gst_v4l2_buffer_pool_streamoff (pool);
898 /* Reset our state */
902 case GST_V4L2_IO_MMAP:
903 case GST_V4L2_IO_USERPTR:
904 case GST_V4L2_IO_DMABUF:
905 case GST_V4L2_IO_DMABUF_IMPORT:
909 num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
911 for (i = 0; i < num_allocated; i++) {
912 /* Re-enqueue buffers */
913 if (pool->buffers[i]) {
914 GstBufferPool *bpool = (GstBufferPool *) pool;
915 GstBuffer *buffer = pool->buffers[i];
917 pool->buffers[i] = NULL;
919 /* Remove qdata, this will unmap any map data in
920 * userptr/dmabuf-import */
921 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
922 GST_V4L2_IMPORT_QUARK, NULL, NULL);
924 if (V4L2_TYPE_IS_OUTPUT (obj->type))
925 gst_buffer_unref (buffer);
927 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
929 g_atomic_int_add (&pool->num_queued, -1);
936 g_assert_not_reached ();
941 /* Start streaming on capture device only */
942 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
943 gst_v4l2_buffer_pool_streamon (pool);
945 gst_poll_set_flushing (pool->poll, FALSE);
949 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
953 /* In RW mode there is no queue, hence no need to wait while the queue is
955 if (pool->obj->mode != GST_V4L2_IO_RW) {
956 GST_OBJECT_LOCK (pool);
958 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
959 GST_OBJECT_UNLOCK (pool);
962 if (!pool->can_poll_device)
965 GST_LOG_OBJECT (pool, "polling device");
968 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
969 if (G_UNLIKELY (ret < 0)) {
977 GST_WARNING_OBJECT (pool,
978 "v4l2 device doesn't support polling. Disabling"
979 " using libv4l2 in this case may cause deadlocks");
980 pool->can_poll_device = FALSE;
987 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
996 GST_DEBUG_OBJECT (pool, "stop called");
997 return GST_FLOW_FLUSHING;
1001 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1002 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1003 return GST_FLOW_ERROR;
1007 static GstFlowReturn
1008 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1010 GstV4l2MemoryGroup *group = NULL;
1013 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1014 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1015 gst_buffer_unref (buf);
1019 index = group->buffer.index;
1021 if (pool->buffers[index] != NULL)
1022 goto already_queued;
1024 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1026 g_atomic_int_inc (&pool->num_queued);
1027 pool->buffers[index] = buf;
1029 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1032 GST_OBJECT_LOCK (pool);
1033 pool->empty = FALSE;
1034 g_cond_signal (&pool->empty_cond);
1035 GST_OBJECT_UNLOCK (pool);
1041 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1042 return GST_FLOW_ERROR;
1046 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1047 /* Mark broken buffer to the allocator */
1048 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1049 g_atomic_int_add (&pool->num_queued, -1);
1050 pool->buffers[index] = NULL;
1051 return GST_FLOW_ERROR;
1055 static GstFlowReturn
1056 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1060 GstV4l2Object *obj = pool->obj;
1061 GstClockTime timestamp;
1062 GstV4l2MemoryGroup *group;
1065 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1068 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1070 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1074 /* get our GstBuffer with that index from the pool, if the buffer was
1075 * outstanding we have a serious problem.
1077 outbuf = pool->buffers[group->buffer.index];
1081 /* mark the buffer outstanding */
1082 pool->buffers[group->buffer.index] = NULL;
1083 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1084 GST_OBJECT_LOCK (pool);
1086 GST_OBJECT_UNLOCK (pool);
1089 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1091 #ifndef GST_DISABLE_GST_DEBUG
1092 for (i = 0; i < group->n_mem; i++) {
1093 GST_LOG_OBJECT (pool,
1094 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1095 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1096 group->buffer.sequence, group->buffer.index, group->mem[i],
1097 group->planes[i].bytesused, i, group->buffer.flags,
1098 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1102 /* set top/bottom field first if v4l2_buffer has the information */
1103 switch (group->buffer.field) {
1104 case V4L2_FIELD_NONE:
1105 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1106 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1108 case V4L2_FIELD_INTERLACED_TB:
1109 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1110 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1112 case V4L2_FIELD_INTERLACED_BT:
1113 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1114 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1116 case V4L2_FIELD_INTERLACED:
1117 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1118 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1119 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1120 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1121 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1123 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1127 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1128 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1129 GST_FIXME_OBJECT (pool,
1130 "Unhandled enum v4l2_field %d - treating as progressive",
1131 group->buffer.field);
1134 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1135 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1136 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1138 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1141 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1142 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1144 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1153 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1158 return GST_FLOW_ERROR;
1162 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1163 group->buffer.index);
1164 return GST_FLOW_ERROR;
1168 static GstFlowReturn
1169 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1170 GstBufferPoolAcquireParams * params)
1173 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1174 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1175 GstV4l2Object *obj = pool->obj;
1177 GST_DEBUG_OBJECT (pool, "acquire");
1179 /* If this is being called to resurect a lost buffer */
1180 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1181 ret = pclass->acquire_buffer (bpool, buffer, params);
1185 switch (obj->type) {
1186 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1187 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1188 /* capture, This function should return a buffer with new captured data */
1189 switch (obj->mode) {
1190 case GST_V4L2_IO_RW:
1192 /* take empty buffer from the pool */
1193 ret = pclass->acquire_buffer (bpool, buffer, params);
1196 case GST_V4L2_IO_DMABUF:
1197 case GST_V4L2_IO_MMAP:
1198 case GST_V4L2_IO_USERPTR:
1199 case GST_V4L2_IO_DMABUF_IMPORT:
1201 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1202 * storage for our buffers. This function does poll first so we can
1203 * interrupt it fine. */
1204 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1208 ret = GST_FLOW_ERROR;
1209 g_assert_not_reached ();
1215 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1216 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1217 /* playback, This function should return an empty buffer */
1218 switch (obj->mode) {
1219 case GST_V4L2_IO_RW:
1220 /* get an empty buffer */
1221 ret = pclass->acquire_buffer (bpool, buffer, params);
1224 case GST_V4L2_IO_MMAP:
1225 case GST_V4L2_IO_DMABUF:
1226 case GST_V4L2_IO_USERPTR:
1227 case GST_V4L2_IO_DMABUF_IMPORT:
1228 /* get a free unqueued buffer */
1229 ret = pclass->acquire_buffer (bpool, buffer, params);
1233 ret = GST_FLOW_ERROR;
1234 g_assert_not_reached ();
1240 ret = GST_FLOW_ERROR;
1241 g_assert_not_reached ();
1249 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1251 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1252 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1253 GstV4l2Object *obj = pool->obj;
1255 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1257 switch (obj->type) {
1258 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1259 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1260 /* capture, put the buffer back in the queue so that we can refill it
1262 switch (obj->mode) {
1263 case GST_V4L2_IO_RW:
1264 /* release back in the pool */
1265 pclass->release_buffer (bpool, buffer);
1268 case GST_V4L2_IO_DMABUF:
1269 case GST_V4L2_IO_MMAP:
1270 case GST_V4L2_IO_USERPTR:
1271 case GST_V4L2_IO_DMABUF_IMPORT:
1273 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1274 /* queue back in the device */
1275 if (pool->other_pool)
1276 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1277 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1278 pclass->release_buffer (bpool, buffer);
1280 /* Simply release invalide/modified buffer, the allocator will
1281 * give it back later */
1282 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1283 pclass->release_buffer (bpool, buffer);
1288 g_assert_not_reached ();
1293 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1294 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1295 switch (obj->mode) {
1296 case GST_V4L2_IO_RW:
1297 /* release back in the pool */
1298 pclass->release_buffer (bpool, buffer);
1301 case GST_V4L2_IO_MMAP:
1302 case GST_V4L2_IO_DMABUF:
1303 case GST_V4L2_IO_USERPTR:
1304 case GST_V4L2_IO_DMABUF_IMPORT:
1306 GstV4l2MemoryGroup *group;
1309 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1310 /* Simply release invalide/modified buffer, the allocator will
1311 * give it back later */
1312 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1313 pclass->release_buffer (bpool, buffer);
1317 index = group->buffer.index;
1319 if (pool->buffers[index] == NULL) {
1320 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1323 /* Remove qdata, this will unmap any map data in userptr */
1324 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1325 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1327 /* reset to default size */
1328 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1330 /* playback, put the buffer back in the queue to refill later. */
1331 pclass->release_buffer (bpool, buffer);
1333 /* We keep a ref on queued buffer, so this should never happen */
1334 g_assert_not_reached ();
1340 g_assert_not_reached ();
1346 g_assert_not_reached ();
1352 gst_v4l2_buffer_pool_dispose (GObject * object)
1354 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1357 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1358 if (pool->buffers[i])
1359 gst_buffer_replace (&(pool->buffers[i]), NULL);
1362 if (pool->vallocator)
1363 gst_object_unref (pool->vallocator);
1364 pool->vallocator = NULL;
1366 if (pool->allocator)
1367 gst_object_unref (pool->allocator);
1368 pool->allocator = NULL;
1370 if (pool->other_pool)
1371 gst_object_unref (pool->other_pool);
1372 pool->other_pool = NULL;
1374 G_OBJECT_CLASS (parent_class)->dispose (object);
1378 gst_v4l2_buffer_pool_finalize (GObject * object)
1380 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1382 if (pool->video_fd >= 0)
1383 v4l2_close (pool->video_fd);
1385 gst_poll_free (pool->poll);
1387 /* FIXME Is this required to keep around ?
1388 * This can't be done in dispose method because we must not set pointer
1389 * to NULL as it is part of the v4l2object and dispose could be called
1391 gst_object_unref (pool->obj->element);
1393 g_cond_clear (&pool->empty_cond);
1395 /* FIXME have we done enough here ? */
1397 G_OBJECT_CLASS (parent_class)->finalize (object);
1401 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1403 pool->poll = gst_poll_new (TRUE);
1404 pool->can_poll_device = TRUE;
1405 g_cond_init (&pool->empty_cond);
1410 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1412 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1413 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1415 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1416 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1418 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1419 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1420 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1421 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1422 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1423 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1424 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1425 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1429 * gst_v4l2_buffer_pool_new:
1430 * @obj: the v4l2 object owning the pool
1432 * Construct a new buffer pool.
1434 * Returns: the new pool, use gst_object_unref() to free resources
1437 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1439 GstV4l2BufferPool *pool;
1440 GstStructure *config;
1441 gchar *name, *parent_name;
1444 fd = v4l2_dup (obj->video_fd);
1448 /* setting a significant unique name */
1449 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1450 name = g_strconcat (parent_name, ":", "pool:",
1451 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1452 g_free (parent_name);
1454 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1455 "name", name, NULL);
1458 gst_poll_fd_init (&pool->pollfd);
1459 pool->pollfd.fd = fd;
1460 gst_poll_add_fd (pool->poll, &pool->pollfd);
1461 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1462 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1464 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1466 pool->video_fd = fd;
1468 pool->can_poll_device = TRUE;
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");
1494 gst_object_unref (pool);
1499 static GstFlowReturn
1500 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1503 GstV4l2Object *obj = pool->obj;
1508 toread = obj->info.size;
1510 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1512 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1515 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1518 amount = v4l2_read (obj->video_fd, map.data, toread);
1520 if (amount == toread) {
1522 } else if (amount == -1) {
1523 if (errno == EAGAIN || errno == EINTR) {
1528 /* short reads can happen if a signal interrupts the read */
1533 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1534 gst_buffer_unmap (buf, &map);
1535 gst_buffer_resize (buf, 0, amount);
1542 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1547 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1548 (_("Error reading %d bytes from device '%s'."),
1549 toread, obj->videodev), GST_ERROR_SYSTEM);
1550 res = GST_FLOW_ERROR;
1555 gst_buffer_unmap (buf, &map);
1556 gst_buffer_resize (buf, 0, 0);
1562 * gst_v4l2_buffer_pool_process:
1563 * @bpool: a #GstBufferPool
1564 * @buf: a #GstBuffer, maybe be replaced
1566 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1567 * data from the device. For output devices, this functions send the contents of
1568 * @buf to the device for playback.
1570 * Returns: %GST_FLOW_OK on success.
1573 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1575 GstFlowReturn ret = GST_FLOW_OK;
1576 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1577 GstV4l2Object *obj = pool->obj;
1579 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1581 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1583 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1584 return GST_FLOW_FLUSHING;
1586 switch (obj->type) {
1587 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1588 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1590 switch (obj->mode) {
1591 case GST_V4L2_IO_RW:
1592 /* capture into the buffer */
1593 ret = gst_v4l2_do_read (pool, *buf);
1596 case GST_V4L2_IO_MMAP:
1597 case GST_V4L2_IO_DMABUF:
1601 if ((*buf)->pool == bpool) {
1604 if (gst_buffer_get_size (*buf) == 0) {
1605 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1606 goto buffer_corrupted;
1611 num_queued = g_atomic_int_get (&pool->num_queued);
1612 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1615 /* If we have no more buffer, and can allocate it time to do so */
1616 if (num_queued == 0) {
1617 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1618 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1619 if (ret == GST_FLOW_OK)
1624 /* start copying buffers when we are running low on buffers */
1625 if (num_queued < pool->copy_threshold) {
1628 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1629 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1630 if (ret == GST_FLOW_OK)
1634 /* copy the buffer */
1635 copy = gst_buffer_copy_region (*buf,
1636 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1637 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1639 /* and requeue so that we can continue capturing */
1640 gst_buffer_unref (*buf);
1644 /* nothing, data was inside the buffer when we did _acquire() */
1648 /* buffer not from our pool, grab a frame and copy it into the target */
1649 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1652 /* An empty buffer on capture indicates the end of stream */
1653 if (gst_buffer_get_size (tmp) == 0) {
1654 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1656 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1657 goto buffer_corrupted;
1662 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1664 /* an queue the buffer again after the copy */
1665 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1667 if (ret != GST_FLOW_OK)
1672 case GST_V4L2_IO_USERPTR:
1674 struct UserPtrData *data;
1676 /* Replace our buffer with downstream allocated buffer */
1677 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1678 GST_V4L2_IMPORT_QUARK);
1679 gst_buffer_replace (buf, data->buffer);
1680 _unmap_userptr_frame (data);
1684 case GST_V4L2_IO_DMABUF_IMPORT:
1688 /* Replace our buffer with downstream allocated buffer */
1689 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1690 GST_V4L2_IMPORT_QUARK);
1691 gst_buffer_replace (buf, tmp);
1692 gst_buffer_unref (tmp);
1697 g_assert_not_reached ();
1702 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1703 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1705 switch (obj->mode) {
1706 case GST_V4L2_IO_RW:
1707 /* FIXME, do write() */
1708 GST_WARNING_OBJECT (pool, "implement write()");
1711 case GST_V4L2_IO_USERPTR:
1712 case GST_V4L2_IO_DMABUF_IMPORT:
1713 case GST_V4L2_IO_DMABUF:
1714 case GST_V4L2_IO_MMAP:
1716 GstBuffer *to_queue = NULL;
1717 GstV4l2MemoryGroup *group;
1720 if ((*buf)->pool != bpool)
1723 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1726 index = group->buffer.index;
1728 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1730 index = group->buffer.index;
1731 if (pool->buffers[index] != NULL) {
1732 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1736 /* we can queue directly */
1737 to_queue = gst_buffer_ref (*buf);
1740 if (to_queue == NULL) {
1741 GstBufferPoolAcquireParams params = { 0 };
1743 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1745 /* this can return EOS if all buffers are outstanding which would
1746 * be strange because we would expect the upstream element to have
1747 * allocated them and returned to us.. */
1748 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1749 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1750 if (ret != GST_FLOW_OK)
1751 goto acquire_failed;
1753 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1754 if (ret != GST_FLOW_OK) {
1755 gst_buffer_unref (to_queue);
1756 goto prepare_failed;
1760 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1763 /* if we are not streaming yet (this is the first buffer, start
1765 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1766 /* don't check return value because qbuf would have failed */
1767 gst_v4l2_is_buffer_valid (to_queue, &group);
1769 /* qbuf has taken the ref of the to_queue buffer but we are no in
1770 * streaming state, so the flush logic won't be performed.
1771 * To avoid leaks, flush the allocator and restore the queued
1772 * buffer as non-queued */
1773 gst_v4l2_allocator_flush (pool->vallocator);
1775 pool->buffers[group->buffer.index] = NULL;
1777 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1778 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1779 gst_buffer_unref (to_queue);
1780 g_atomic_int_add (&pool->num_queued, -1);
1784 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1786 /* all buffers are queued, try to dequeue one and release it back
1787 * into the pool so that _acquire can get to it again. */
1788 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1789 if (ret == GST_FLOW_OK)
1790 /* release the rendered buffer back into the pool. This wakes up any
1791 * thread waiting for a buffer in _acquire(). */
1792 gst_buffer_unref (out);
1797 g_assert_not_reached ();
1802 g_assert_not_reached ();
1811 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1816 GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1817 gst_buffer_unref (*buf);
1819 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1823 GST_DEBUG_OBJECT (pool, "end of stream reached");
1824 gst_buffer_unref (*buf);
1826 return GST_V4L2_FLOW_LAST_BUFFER;
1830 if (ret == GST_FLOW_FLUSHING)
1831 GST_DEBUG_OBJECT (pool, "flushing");
1833 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1834 gst_flow_get_name (ret));
1839 GST_ERROR_OBJECT (pool, "failed to prepare data");
1844 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1849 GST_ERROR_OBJECT (pool, "failed to start streaming");
1850 return GST_FLOW_ERROR;
1855 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1856 GstBufferPool * other_pool)
1858 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1860 if (pool->other_pool)
1861 gst_object_unref (pool->other_pool);
1862 pool->other_pool = gst_object_ref (other_pool);