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_STATIC (v4l2bufferpool_debug);
50 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2bufferpool_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 gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
225 data->is_frame = TRUE;
227 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
230 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
231 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
232 gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
236 pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
237 GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
239 pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
240 GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
242 size[i] = pstride * pheight;
244 size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
245 GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
249 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
250 data->frame.info.size, finfo->n_planes, data->frame.data, size))
256 data->is_frame = FALSE;
258 if (!gst_buffer_map (src, &data->map, flags))
261 ptr[0] = data->map.data;
262 size[0] = data->map.size;
264 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
265 data->map.size, 1, ptr, size))
269 data->buffer = gst_buffer_ref (src);
271 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
272 data, (GDestroyNotify) _unmap_userptr_frame);
278 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
279 return GST_FLOW_ERROR;
283 GST_ERROR_OBJECT (pool, "could not map buffer");
284 g_slice_free (struct UserPtrData, data);
285 return GST_FLOW_ERROR;
289 GST_ERROR_OBJECT (pool, "failed to import data");
290 _unmap_userptr_frame (data);
291 return GST_FLOW_ERROR;
296 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
297 GstBuffer * dest, GstBuffer * src)
299 GstV4l2MemoryGroup *group = NULL;
300 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
301 guint n_mem = gst_buffer_n_memory (src);
304 GST_LOG_OBJECT (pool, "importing dmabuf");
306 if (!gst_v4l2_is_buffer_valid (dest, &group))
309 if (n_mem > GST_VIDEO_MAX_PLANES)
312 for (i = 0; i < n_mem; i++)
313 dma_mem[i] = gst_buffer_peek_memory (src, i);
315 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
319 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
320 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
326 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
327 return GST_FLOW_ERROR;
331 GST_ERROR_OBJECT (pool, "could not map buffer");
332 return GST_FLOW_ERROR;
336 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
337 return GST_FLOW_ERROR;
342 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
343 GstBuffer * dest, GstBuffer * src)
345 GstFlowReturn ret = GST_FLOW_OK;
346 gboolean own_src = FALSE;
349 if (pool->other_pool == NULL) {
350 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
351 return GST_FLOW_ERROR;
354 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
355 if (ret != GST_FLOW_OK) {
356 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
363 switch (pool->obj->mode) {
364 case GST_V4L2_IO_MMAP:
365 case GST_V4L2_IO_DMABUF:
366 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
368 case GST_V4L2_IO_USERPTR:
369 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
371 case GST_V4L2_IO_DMABUF_IMPORT:
372 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
379 gst_buffer_unref (src);
386 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
387 GstBufferPoolAcquireParams * params)
389 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
390 GstV4l2MemoryGroup *group = NULL;
391 GstBuffer *newbuf = NULL;
401 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
403 case GST_V4L2_IO_MMAP:
404 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
406 case GST_V4L2_IO_DMABUF:
407 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
410 case GST_V4L2_IO_USERPTR:
411 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
413 case GST_V4L2_IO_DMABUF_IMPORT:
414 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
418 g_assert_not_reached ();
424 newbuf = gst_buffer_new ();
426 for (i = 0; i < group->n_mem; i++)
427 gst_buffer_append_memory (newbuf, group->mem[i]);
428 } else if (newbuf == NULL) {
429 goto allocation_failed;
432 /* add metadata to raw video buffers */
433 if (pool->add_videometa)
434 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
435 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
436 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
437 info->offset, info->stride);
446 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
447 return GST_FLOW_ERROR;
452 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
454 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
455 GstV4l2Object *obj = pool->obj;
457 guint size, min_buffers, max_buffers;
458 GstAllocator *allocator;
459 GstAllocationParams params;
460 gboolean can_allocate = FALSE;
461 gboolean updated = FALSE;
464 pool->add_videometa =
465 gst_buffer_pool_config_has_option (config,
466 GST_BUFFER_POOL_OPTION_VIDEO_META);
468 /* parse the config and keep around */
469 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
473 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
476 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
479 gst_object_unref (pool->allocator);
480 pool->allocator = NULL;
483 case GST_V4L2_IO_DMABUF:
484 pool->allocator = gst_dmabuf_allocator_new ();
485 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
487 case GST_V4L2_IO_MMAP:
488 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
490 case GST_V4L2_IO_USERPTR:
492 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
494 case GST_V4L2_IO_DMABUF_IMPORT:
495 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
499 pool->allocator = g_object_ref (allocator);
500 pool->params = params;
501 /* No need to change the configuration */
505 g_assert_not_reached ();
509 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
510 * instability and crash, disable it for now */
511 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
512 GST_WARNING_OBJECT (pool,
513 "libv4l2 converter detected, disabling CREATE_BUFS");
514 can_allocate = FALSE;
515 GST_OBJECT_FLAG_UNSET (pool->vallocator,
516 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
517 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
518 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
521 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
523 min_buffers = GST_V4L2_MIN_BUFFERS;
524 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
527 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
529 max_buffers = VIDEO_MAX_FRAME;
530 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
533 if (min_buffers > max_buffers) {
535 min_buffers = max_buffers;
536 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
537 } else if (min_buffers != max_buffers) {
540 max_buffers = min_buffers;
541 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
545 if (!pool->add_videometa && obj->need_video_meta) {
546 GST_INFO_OBJECT (pool, "adding needed video meta");
548 gst_buffer_pool_config_add_option (config,
549 GST_BUFFER_POOL_OPTION_VIDEO_META);
552 /* Always update the config to ensure the configured size matches */
553 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
556 /* keep a GstVideoInfo with defaults for the when we need to copy */
557 gst_video_info_from_caps (&pool->caps_info, caps);
560 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
562 /* If anything was changed documentation recommand to return FALSE */
563 return !updated && ret;
568 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
574 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
576 GstV4l2Object *obj = pool->obj;
579 case GST_V4L2_IO_MMAP:
580 case GST_V4L2_IO_USERPTR:
581 case GST_V4L2_IO_DMABUF:
582 case GST_V4L2_IO_DMABUF_IMPORT:
583 if (!pool->streaming) {
584 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
585 goto streamon_failed;
587 pool->streaming = TRUE;
589 GST_DEBUG_OBJECT (pool, "Started streaming");
600 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
607 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
609 GstV4l2Object *obj = pool->obj;
612 case GST_V4L2_IO_MMAP:
613 case GST_V4L2_IO_USERPTR:
614 case GST_V4L2_IO_DMABUF:
615 case GST_V4L2_IO_DMABUF_IMPORT:
616 if (pool->streaming) {
617 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
618 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
619 errno, g_strerror (errno));
621 pool->streaming = FALSE;
623 GST_DEBUG_OBJECT (pool, "Stopped streaming");
625 if (pool->vallocator)
626 gst_v4l2_allocator_flush (pool->vallocator);
635 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
637 GstBufferPoolAcquireParams params = { 0 };
638 GstBuffer *buffer = NULL;
641 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
644 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
646 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
648 if (ret == GST_FLOW_OK)
649 gst_buffer_unref (buffer);
655 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
657 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
658 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
659 GstV4l2Object *obj = pool->obj;
660 GstStructure *config;
662 guint size, min_buffers, max_buffers;
663 guint max_latency, min_latency, copy_threshold = 0;
664 gboolean can_allocate = FALSE;
666 GST_DEBUG_OBJECT (pool, "activating pool");
668 config = gst_buffer_pool_get_config (bpool);
669 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
673 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
679 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
680 * of MMAP. In this case, the first read initialize the queues, but the
681 * poll before that will always fail. Doing an empty read, forces the
682 * queue to be initialized now. We only do this if we have a streaming
684 if (obj->vcap.capabilities & V4L2_CAP_STREAMING)
685 v4l2_read (obj->video_fd, NULL, 0);
688 case GST_V4L2_IO_DMABUF:
689 case GST_V4L2_IO_MMAP:
693 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
695 /* first, lets request buffers, and see how many we can get: */
696 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
698 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
701 if (count < GST_V4L2_MIN_BUFFERS) {
706 /* V4L2 buffer pool are often very limited in the amount of buffers it
707 * can offer. The copy_threshold will workaround this limitation by
708 * falling back to copy if the pipeline needed more buffers. This also
709 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
711 if (count != min_buffers || pool->enable_copy_threshold) {
712 GST_WARNING_OBJECT (pool,
713 "Uncertain or not enough buffers, enabling copy threshold");
715 copy_threshold = min_latency;
720 case GST_V4L2_IO_USERPTR:
725 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
727 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
729 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
730 V4L2_MEMORY_USERPTR);
732 /* There is no rational to not get what we asked */
733 if (count < min_buffers) {
741 case GST_V4L2_IO_DMABUF_IMPORT:
745 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
747 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
749 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
752 /* There is no rational to not get what we asked */
753 if (count < min_buffers) {
764 g_assert_not_reached ();
769 max_latency = max_buffers;
771 max_latency = min_buffers;
774 pool->copy_threshold = copy_threshold;
775 pool->max_latency = max_latency;
776 pool->min_latency = min_latency;
777 pool->num_queued = 0;
779 if (max_buffers != 0 && max_buffers < min_buffers)
780 max_buffers = min_buffers;
782 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
784 pclass->set_config (bpool, config);
785 gst_structure_free (config);
787 if (pool->other_pool)
788 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
789 goto other_pool_failed;
791 /* now, allocate the buffers: */
792 if (!pclass->start (bpool))
795 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
796 pool->group_released_handler =
797 g_signal_connect_swapped (pool->vallocator, "group-released",
798 G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
805 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
806 gst_structure_free (config);
811 GST_ERROR_OBJECT (pool,
812 "we received %d buffer from device '%s', we want at least %d",
813 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
814 gst_structure_free (config);
819 GST_ERROR_OBJECT (pool, "failed to start streaming");
824 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
825 GST_PTR_FORMAT, pool->other_pool);
831 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
833 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
834 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
838 GST_DEBUG_OBJECT (pool, "stopping pool");
840 if (pool->group_released_handler > 0) {
841 g_signal_handler_disconnect (pool->vallocator,
842 pool->group_released_handler);
843 pool->group_released_handler = 0;
846 if (pool->other_pool) {
847 gst_buffer_pool_set_active (pool->other_pool, FALSE);
848 gst_object_unref (pool->other_pool);
849 pool->other_pool = NULL;
852 gst_v4l2_buffer_pool_streamoff (pool);
854 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
855 if (pool->buffers[i]) {
856 GstBuffer *buffer = pool->buffers[i];
858 pool->buffers[i] = NULL;
860 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
861 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
862 else /* Don't re-enqueue capture buffer on stop */
863 pclass->release_buffer (bpool, buffer);
865 g_atomic_int_add (&pool->num_queued, -1);
869 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
871 if (ret && pool->vallocator) {
874 vret = gst_v4l2_allocator_stop (pool->vallocator);
876 if (vret == GST_V4L2_BUSY)
877 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
879 ret = (vret == GST_V4L2_OK);
886 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
888 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
890 GST_DEBUG_OBJECT (pool, "start flushing");
892 gst_poll_set_flushing (pool->poll, TRUE);
894 GST_OBJECT_LOCK (pool);
896 g_cond_broadcast (&pool->empty_cond);
897 GST_OBJECT_UNLOCK (pool);
899 if (pool->other_pool)
900 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
904 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
906 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
907 GstV4l2Object *obj = pool->obj;
910 GST_DEBUG_OBJECT (pool, "stop flushing");
912 /* If we haven't started streaming yet, simply call streamon */
913 if (!pool->streaming)
916 if (pool->other_pool)
917 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
919 gst_v4l2_buffer_pool_streamoff (pool);
921 /* Reset our state */
925 case GST_V4L2_IO_MMAP:
926 case GST_V4L2_IO_USERPTR:
927 case GST_V4L2_IO_DMABUF:
928 case GST_V4L2_IO_DMABUF_IMPORT:
930 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
931 /* Re-enqueue buffers */
932 if (pool->buffers[i]) {
933 GstBufferPool *bpool = (GstBufferPool *) pool;
934 GstBuffer *buffer = pool->buffers[i];
936 pool->buffers[i] = NULL;
938 /* Remove qdata, this will unmap any map data in
939 * userptr/dmabuf-import */
940 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
941 GST_V4L2_IMPORT_QUARK, NULL, NULL);
943 if (buffer->pool == NULL)
944 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
946 g_atomic_int_add (&pool->num_queued, -1);
953 g_assert_not_reached ();
958 /* Start streaming on capture device only */
959 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
960 gst_v4l2_buffer_pool_streamon (pool);
962 gst_poll_set_flushing (pool->poll, FALSE);
966 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
970 /* In RW mode there is no queue, hence no need to wait while the queue is
972 if (pool->obj->mode != GST_V4L2_IO_RW) {
973 GST_OBJECT_LOCK (pool);
975 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
976 GST_OBJECT_UNLOCK (pool);
979 if (!pool->can_poll_device)
982 GST_LOG_OBJECT (pool, "polling device");
985 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
986 if (G_UNLIKELY (ret < 0)) {
994 GST_WARNING_OBJECT (pool,
995 "v4l2 device doesn't support polling. Disabling"
996 " using libv4l2 in this case may cause deadlocks");
997 pool->can_poll_device = FALSE;
1004 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1013 GST_DEBUG_OBJECT (pool, "stop called");
1014 return GST_FLOW_FLUSHING;
1018 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1019 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1020 return GST_FLOW_ERROR;
1024 static GstFlowReturn
1025 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1027 GstV4l2MemoryGroup *group = NULL;
1028 const GstV4l2Object *obj = pool->obj;
1031 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1032 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1033 gst_buffer_unref (buf);
1037 index = group->buffer.index;
1039 if (pool->buffers[index] != NULL)
1040 goto already_queued;
1042 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1044 g_atomic_int_inc (&pool->num_queued);
1045 pool->buffers[index] = buf;
1047 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1048 enum v4l2_field field;
1050 /* Except when field is set to alternate, buffer field is the same as
1051 * the one defined in format */
1052 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1053 field = obj->format.fmt.pix_mp.field;
1055 field = obj->format.fmt.pix.field;
1057 /* NB: At this moment, we can't have alternate mode because it not handled
1059 if (field == V4L2_FIELD_ALTERNATE) {
1060 if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1061 field = V4L2_FIELD_TOP;
1063 field = V4L2_FIELD_BOTTOM;
1066 group->buffer.field = field;
1069 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1072 GST_OBJECT_LOCK (pool);
1073 pool->empty = FALSE;
1074 g_cond_signal (&pool->empty_cond);
1075 GST_OBJECT_UNLOCK (pool);
1081 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1082 return GST_FLOW_ERROR;
1086 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1087 /* Mark broken buffer to the allocator */
1088 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1089 g_atomic_int_add (&pool->num_queued, -1);
1090 pool->buffers[index] = NULL;
1091 return GST_FLOW_ERROR;
1095 static GstFlowReturn
1096 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1100 GstV4l2Object *obj = pool->obj;
1101 GstClockTime timestamp;
1102 GstV4l2MemoryGroup *group;
1105 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1108 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1110 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1114 /* get our GstBuffer with that index from the pool, if the buffer was
1115 * outstanding we have a serious problem.
1117 outbuf = pool->buffers[group->buffer.index];
1121 /* mark the buffer outstanding */
1122 pool->buffers[group->buffer.index] = NULL;
1123 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1124 GST_OBJECT_LOCK (pool);
1126 GST_OBJECT_UNLOCK (pool);
1129 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1131 #ifndef GST_DISABLE_GST_DEBUG
1132 for (i = 0; i < group->n_mem; i++) {
1133 GST_LOG_OBJECT (pool,
1134 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1135 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1136 group->buffer.sequence, group->buffer.index, group->mem[i],
1137 group->planes[i].bytesused, i, group->buffer.flags,
1138 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1142 /* set top/bottom field first if v4l2_buffer has the information */
1143 switch (group->buffer.field) {
1144 case V4L2_FIELD_NONE:
1145 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1146 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1148 case V4L2_FIELD_INTERLACED_TB:
1149 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1150 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1152 case V4L2_FIELD_INTERLACED_BT:
1153 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1154 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1156 case V4L2_FIELD_INTERLACED:
1157 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1158 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1159 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1160 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1161 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1163 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1167 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1168 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1169 GST_FIXME_OBJECT (pool,
1170 "Unhandled enum v4l2_field %d - treating as progressive",
1171 group->buffer.field);
1174 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1175 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1176 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1178 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1181 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1182 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1184 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1193 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1198 return GST_FLOW_ERROR;
1202 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1203 group->buffer.index);
1204 return GST_FLOW_ERROR;
1208 static GstFlowReturn
1209 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1210 GstBufferPoolAcquireParams * params)
1213 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1214 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1215 GstV4l2Object *obj = pool->obj;
1217 GST_DEBUG_OBJECT (pool, "acquire");
1219 /* If this is being called to resurect a lost buffer */
1220 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1221 ret = pclass->acquire_buffer (bpool, buffer, params);
1225 switch (obj->type) {
1226 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1227 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1228 /* capture, This function should return a buffer with new captured data */
1229 switch (obj->mode) {
1230 case GST_V4L2_IO_RW:
1232 /* take empty buffer from the pool */
1233 ret = pclass->acquire_buffer (bpool, buffer, params);
1236 case GST_V4L2_IO_DMABUF:
1237 case GST_V4L2_IO_MMAP:
1238 case GST_V4L2_IO_USERPTR:
1239 case GST_V4L2_IO_DMABUF_IMPORT:
1241 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1242 * storage for our buffers. This function does poll first so we can
1243 * interrupt it fine. */
1244 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1248 ret = GST_FLOW_ERROR;
1249 g_assert_not_reached ();
1255 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1256 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1257 /* playback, This function should return an empty buffer */
1258 switch (obj->mode) {
1259 case GST_V4L2_IO_RW:
1260 /* get an empty buffer */
1261 ret = pclass->acquire_buffer (bpool, buffer, params);
1264 case GST_V4L2_IO_MMAP:
1265 case GST_V4L2_IO_DMABUF:
1266 case GST_V4L2_IO_USERPTR:
1267 case GST_V4L2_IO_DMABUF_IMPORT:
1268 /* get a free unqueued buffer */
1269 ret = pclass->acquire_buffer (bpool, buffer, params);
1273 ret = GST_FLOW_ERROR;
1274 g_assert_not_reached ();
1280 ret = GST_FLOW_ERROR;
1281 g_assert_not_reached ();
1289 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1291 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1292 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1293 GstV4l2Object *obj = pool->obj;
1295 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1297 switch (obj->type) {
1298 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1299 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1300 /* capture, put the buffer back in the queue so that we can refill it
1302 switch (obj->mode) {
1303 case GST_V4L2_IO_RW:
1304 /* release back in the pool */
1305 pclass->release_buffer (bpool, buffer);
1308 case GST_V4L2_IO_DMABUF:
1309 case GST_V4L2_IO_MMAP:
1310 case GST_V4L2_IO_USERPTR:
1311 case GST_V4L2_IO_DMABUF_IMPORT:
1313 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1314 /* queue back in the device */
1315 if (pool->other_pool)
1316 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1317 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1318 pclass->release_buffer (bpool, buffer);
1320 /* Simply release invalide/modified buffer, the allocator will
1321 * give it back later */
1322 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1323 pclass->release_buffer (bpool, buffer);
1328 g_assert_not_reached ();
1333 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1334 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1335 switch (obj->mode) {
1336 case GST_V4L2_IO_RW:
1337 /* release back in the pool */
1338 pclass->release_buffer (bpool, buffer);
1341 case GST_V4L2_IO_MMAP:
1342 case GST_V4L2_IO_DMABUF:
1343 case GST_V4L2_IO_USERPTR:
1344 case GST_V4L2_IO_DMABUF_IMPORT:
1346 GstV4l2MemoryGroup *group;
1349 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1350 /* Simply release invalide/modified buffer, the allocator will
1351 * give it back later */
1352 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1353 pclass->release_buffer (bpool, buffer);
1357 index = group->buffer.index;
1359 if (pool->buffers[index] == NULL) {
1360 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1363 /* Remove qdata, this will unmap any map data in userptr */
1364 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1365 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1367 /* reset to default size */
1368 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1370 /* playback, put the buffer back in the queue to refill later. */
1371 pclass->release_buffer (bpool, buffer);
1373 /* the buffer is queued in the device but maybe not played yet. We just
1374 * leave it there and not make it available for future calls to acquire
1375 * for now. The buffer will be dequeued and reused later. */
1376 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1382 g_assert_not_reached ();
1388 g_assert_not_reached ();
1394 gst_v4l2_buffer_pool_dispose (GObject * object)
1396 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1398 if (pool->vallocator)
1399 gst_object_unref (pool->vallocator);
1400 pool->vallocator = NULL;
1402 if (pool->allocator)
1403 gst_object_unref (pool->allocator);
1404 pool->allocator = NULL;
1406 if (pool->other_pool)
1407 gst_object_unref (pool->other_pool);
1408 pool->other_pool = NULL;
1410 G_OBJECT_CLASS (parent_class)->dispose (object);
1414 gst_v4l2_buffer_pool_finalize (GObject * object)
1416 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1418 if (pool->video_fd >= 0)
1419 v4l2_close (pool->video_fd);
1421 gst_poll_free (pool->poll);
1423 /* FIXME Is this required to keep around ?
1424 * This can't be done in dispose method because we must not set pointer
1425 * to NULL as it is part of the v4l2object and dispose could be called
1427 gst_object_unref (pool->obj->element);
1429 g_cond_clear (&pool->empty_cond);
1431 /* FIXME have we done enough here ? */
1433 G_OBJECT_CLASS (parent_class)->finalize (object);
1437 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1439 pool->poll = gst_poll_new (TRUE);
1440 pool->can_poll_device = TRUE;
1441 g_cond_init (&pool->empty_cond);
1446 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1448 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1449 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1451 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1452 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1454 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1455 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1456 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1457 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1458 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1459 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1460 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1461 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1463 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1464 "V4L2 Buffer Pool");
1468 * gst_v4l2_buffer_pool_new:
1469 * @obj: the v4l2 object owning the pool
1471 * Construct a new buffer pool.
1473 * Returns: the new pool, use gst_object_unref() to free resources
1476 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1478 GstV4l2BufferPool *pool;
1479 GstStructure *config;
1480 gchar *name, *parent_name;
1483 fd = v4l2_dup (obj->video_fd);
1487 /* setting a significant unique name */
1488 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1489 name = g_strconcat (parent_name, ":", "pool:",
1490 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1491 g_free (parent_name);
1493 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1494 "name", name, NULL);
1497 gst_poll_fd_init (&pool->pollfd);
1498 pool->pollfd.fd = fd;
1499 gst_poll_add_fd (pool->poll, &pool->pollfd);
1500 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1501 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1503 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1505 pool->video_fd = fd;
1507 pool->can_poll_device = TRUE;
1510 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1511 if (pool->vallocator == NULL)
1512 goto allocator_failed;
1514 gst_object_ref (obj->element);
1516 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1517 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1518 /* This will simply set a default config, but will not configure the pool
1519 * because min and max are not valid */
1520 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1522 return GST_BUFFER_POOL (pool);
1527 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1532 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1533 gst_object_unref (pool);
1538 static GstFlowReturn
1539 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1542 GstV4l2Object *obj = pool->obj;
1547 toread = obj->info.size;
1549 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1551 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1554 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1557 amount = v4l2_read (obj->video_fd, map.data, toread);
1559 if (amount == toread) {
1561 } else if (amount == -1) {
1562 if (errno == EAGAIN || errno == EINTR) {
1567 /* short reads can happen if a signal interrupts the read */
1572 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1573 gst_buffer_unmap (buf, &map);
1574 gst_buffer_resize (buf, 0, amount);
1581 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1586 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1587 (_("Error reading %d bytes from device '%s'."),
1588 toread, obj->videodev), GST_ERROR_SYSTEM);
1589 res = GST_FLOW_ERROR;
1594 gst_buffer_unmap (buf, &map);
1595 gst_buffer_resize (buf, 0, 0);
1601 * gst_v4l2_buffer_pool_process:
1602 * @bpool: a #GstBufferPool
1603 * @buf: a #GstBuffer, maybe be replaced
1605 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1606 * data from the device. For output devices, this functions send the contents of
1607 * @buf to the device for playback.
1609 * Returns: %GST_FLOW_OK on success.
1612 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1614 GstFlowReturn ret = GST_FLOW_OK;
1615 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1616 GstV4l2Object *obj = pool->obj;
1618 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1620 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1622 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1623 return GST_FLOW_FLUSHING;
1625 switch (obj->type) {
1626 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1627 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1629 switch (obj->mode) {
1630 case GST_V4L2_IO_RW:
1631 /* capture into the buffer */
1632 ret = gst_v4l2_do_read (pool, *buf);
1635 case GST_V4L2_IO_MMAP:
1636 case GST_V4L2_IO_DMABUF:
1640 if ((*buf)->pool == bpool) {
1643 if (gst_buffer_get_size (*buf) == 0) {
1644 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1645 goto buffer_corrupted;
1650 num_queued = g_atomic_int_get (&pool->num_queued);
1651 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1654 /* If we have no more buffer, and can allocate it time to do so */
1655 if (num_queued == 0) {
1656 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1657 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1658 if (ret == GST_FLOW_OK)
1663 /* start copying buffers when we are running low on buffers */
1664 if (num_queued < pool->copy_threshold) {
1667 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1668 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1669 if (ret == GST_FLOW_OK)
1673 /* copy the buffer */
1674 copy = gst_buffer_copy_region (*buf,
1675 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1676 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1678 /* and requeue so that we can continue capturing */
1679 gst_buffer_unref (*buf);
1683 /* nothing, data was inside the buffer when we did _acquire() */
1687 /* buffer not from our pool, grab a frame and copy it into the target */
1688 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1691 /* An empty buffer on capture indicates the end of stream */
1692 if (gst_buffer_get_size (tmp) == 0) {
1693 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1695 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1696 goto buffer_corrupted;
1701 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1703 /* an queue the buffer again after the copy */
1704 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1706 if (ret != GST_FLOW_OK)
1711 case GST_V4L2_IO_USERPTR:
1713 struct UserPtrData *data;
1715 /* Replace our buffer with downstream allocated buffer */
1716 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1717 GST_V4L2_IMPORT_QUARK);
1718 gst_buffer_replace (buf, data->buffer);
1719 _unmap_userptr_frame (data);
1723 case GST_V4L2_IO_DMABUF_IMPORT:
1727 /* Replace our buffer with downstream allocated buffer */
1728 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1729 GST_V4L2_IMPORT_QUARK);
1730 gst_buffer_replace (buf, tmp);
1731 gst_buffer_unref (tmp);
1736 g_assert_not_reached ();
1741 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1742 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1744 switch (obj->mode) {
1745 case GST_V4L2_IO_RW:
1746 /* FIXME, do write() */
1747 GST_WARNING_OBJECT (pool, "implement write()");
1750 case GST_V4L2_IO_USERPTR:
1751 case GST_V4L2_IO_DMABUF_IMPORT:
1752 case GST_V4L2_IO_DMABUF:
1753 case GST_V4L2_IO_MMAP:
1755 GstBuffer *to_queue = NULL;
1756 GstV4l2MemoryGroup *group;
1759 if ((*buf)->pool != bpool)
1762 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1765 index = group->buffer.index;
1767 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1769 index = group->buffer.index;
1770 if (pool->buffers[index] != NULL) {
1771 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1775 /* we can queue directly */
1776 to_queue = gst_buffer_ref (*buf);
1779 if (to_queue == NULL) {
1780 GstBufferPoolAcquireParams params = { 0 };
1782 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1784 /* this can return EOS if all buffers are outstanding which would
1785 * be strange because we would expect the upstream element to have
1786 * allocated them and returned to us.. */
1787 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1788 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1789 if (ret != GST_FLOW_OK)
1790 goto acquire_failed;
1792 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1793 if (ret != GST_FLOW_OK) {
1794 gst_buffer_unref (to_queue);
1795 goto prepare_failed;
1799 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1802 /* if we are not streaming yet (this is the first buffer, start
1804 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1805 /* don't check return value because qbuf would have failed */
1806 gst_v4l2_is_buffer_valid (to_queue, &group);
1808 /* qbuf has stored to_queue buffer but we are not in
1809 * streaming state, so the flush logic won't be performed.
1810 * To avoid leaks, flush the allocator and restore the queued
1811 * buffer as non-queued */
1812 gst_v4l2_allocator_flush (pool->vallocator);
1814 pool->buffers[group->buffer.index] = NULL;
1816 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1817 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1818 gst_buffer_unref (to_queue);
1819 g_atomic_int_add (&pool->num_queued, -1);
1823 /* Remove our ref, we will still hold this buffer in acquire as needed,
1824 * otherwise the pool will think it is outstanding and will refuse to stop. */
1825 gst_buffer_unref (to_queue);
1827 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1829 /* all buffers are queued, try to dequeue one and release it back
1830 * into the pool so that _acquire can get to it again. */
1831 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1832 if (ret == GST_FLOW_OK && out->pool == NULL)
1833 /* release the rendered buffer back into the pool. This wakes up any
1834 * thread waiting for a buffer in _acquire(). */
1835 gst_v4l2_buffer_pool_release_buffer (bpool, out);
1840 g_assert_not_reached ();
1845 g_assert_not_reached ();
1854 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1859 GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1860 gst_buffer_unref (*buf);
1862 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1866 GST_DEBUG_OBJECT (pool, "end of stream reached");
1867 gst_buffer_unref (*buf);
1869 return GST_V4L2_FLOW_LAST_BUFFER;
1873 if (ret == GST_FLOW_FLUSHING)
1874 GST_DEBUG_OBJECT (pool, "flushing");
1876 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1877 gst_flow_get_name (ret));
1882 GST_ERROR_OBJECT (pool, "failed to prepare data");
1887 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1892 GST_ERROR_OBJECT (pool, "failed to start streaming");
1893 return GST_FLOW_ERROR;
1898 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1899 GstBufferPool * other_pool)
1901 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1903 if (pool->other_pool)
1904 gst_object_unref (pool->other_pool);
1905 pool->other_pool = gst_object_ref (other_pool);
1909 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
1911 GST_OBJECT_LOCK (pool);
1912 pool->enable_copy_threshold = copy;
1913 GST_OBJECT_UNLOCK (pool);