3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
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 "gstv4l2object.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_STATIC (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_buffer_copy_into (dest, src,
153 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
155 GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest);
161 GST_ERROR_OBJECT (pool, "could not map buffer");
162 return GST_FLOW_ERROR;
175 gst_v4l2_buffer_pool_import_quark (void)
177 static GQuark quark = 0;
180 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
186 _unmap_userptr_frame (struct UserPtrData *data)
189 gst_video_frame_unmap (&data->frame);
191 gst_buffer_unmap (data->buffer, &data->map);
194 gst_buffer_unref (data->buffer);
196 g_slice_free (struct UserPtrData, data);
200 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
201 GstBuffer * dest, GstBuffer * src)
203 GstFlowReturn ret = GST_FLOW_OK;
204 GstV4l2MemoryGroup *group = NULL;
206 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
207 struct UserPtrData *data = NULL;
209 GST_LOG_OBJECT (pool, "importing userptr");
212 if (!gst_v4l2_is_buffer_valid (dest, &group))
215 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
216 flags = GST_MAP_READ;
218 flags = GST_MAP_WRITE;
220 data = g_slice_new0 (struct UserPtrData);
222 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
223 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
224 gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
227 data->is_frame = TRUE;
229 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
232 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
233 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
234 gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
238 pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
239 GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
241 pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
242 GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
244 size[i] = pstride * pheight;
246 size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
247 GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
251 /* In the single planar API, planes must be contiguous in memory and
252 * therefore they must have expected size. ie: no padding.
253 * To check these conditions, we check that plane 'i' start address
254 * + plane 'i' size equals to plane 'i+1' start address */
255 if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
256 for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
257 const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
259 gint estride = gst_v4l2_object_extrapolate_stride (finfo, i,
260 pix_fmt->bytesperline);
261 guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
264 tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
265 if (tmp != data->frame.data[i + 1])
266 goto non_contiguous_mem;
270 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
271 data->frame.info.size, finfo->n_planes, data->frame.data, size))
277 data->is_frame = FALSE;
279 if (!gst_buffer_map (src, &data->map, flags))
282 ptr[0] = data->map.data;
283 size[0] = data->map.size;
285 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
286 data->map.size, 1, ptr, size))
290 data->buffer = gst_buffer_ref (src);
292 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
293 data, (GDestroyNotify) _unmap_userptr_frame);
295 gst_buffer_copy_into (dest, src,
296 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
302 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
303 return GST_FLOW_ERROR;
307 GST_ERROR_OBJECT (pool, "could not map buffer");
308 g_slice_free (struct UserPtrData, data);
309 return GST_FLOW_ERROR;
313 GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
314 _unmap_userptr_frame (data);
315 return GST_FLOW_ERROR;
319 GST_ERROR_OBJECT (pool, "failed to import data");
320 _unmap_userptr_frame (data);
321 return GST_FLOW_ERROR;
326 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
327 GstBuffer * dest, GstBuffer * src)
329 GstV4l2MemoryGroup *group = NULL;
330 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
331 guint n_mem = gst_buffer_n_memory (src);
334 GST_LOG_OBJECT (pool, "importing dmabuf");
336 if (!gst_v4l2_is_buffer_valid (dest, &group))
339 if (n_mem > GST_VIDEO_MAX_PLANES)
342 for (i = 0; i < n_mem; i++)
343 dma_mem[i] = gst_buffer_peek_memory (src, i);
345 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
349 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
350 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
352 gst_buffer_copy_into (dest, src,
353 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
359 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
360 return GST_FLOW_ERROR;
364 GST_ERROR_OBJECT (pool, "could not map buffer");
365 return GST_FLOW_ERROR;
369 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
370 return GST_FLOW_ERROR;
375 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
376 GstBuffer * dest, GstBuffer * src)
378 GstFlowReturn ret = GST_FLOW_OK;
379 gboolean own_src = FALSE;
382 if (pool->other_pool == NULL) {
383 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
384 return GST_FLOW_ERROR;
387 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
388 if (ret != GST_FLOW_OK) {
389 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
396 switch (pool->obj->mode) {
397 case GST_V4L2_IO_MMAP:
398 case GST_V4L2_IO_DMABUF:
399 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
401 case GST_V4L2_IO_USERPTR:
402 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
404 case GST_V4L2_IO_DMABUF_IMPORT:
405 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
412 gst_buffer_unref (src);
419 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
420 GstBufferPoolAcquireParams * params)
422 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
423 GstV4l2MemoryGroup *group = NULL;
424 GstBuffer *newbuf = NULL;
434 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
436 case GST_V4L2_IO_MMAP:
437 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
439 case GST_V4L2_IO_DMABUF:
440 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
443 case GST_V4L2_IO_USERPTR:
444 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
446 case GST_V4L2_IO_DMABUF_IMPORT:
447 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
451 g_assert_not_reached ();
457 newbuf = gst_buffer_new ();
459 for (i = 0; i < group->n_mem; i++)
460 gst_buffer_append_memory (newbuf, group->mem[i]);
461 } else if (newbuf == NULL) {
462 goto allocation_failed;
465 /* add metadata to raw video buffers */
466 if (pool->add_videometa)
467 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
468 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
469 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
470 info->offset, info->stride);
479 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
480 return GST_FLOW_ERROR;
485 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
487 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
488 GstV4l2Object *obj = pool->obj;
490 guint size, min_buffers, max_buffers;
491 GstAllocator *allocator;
492 GstAllocationParams params;
493 gboolean can_allocate = FALSE;
494 gboolean updated = FALSE;
497 pool->add_videometa =
498 gst_buffer_pool_config_has_option (config,
499 GST_BUFFER_POOL_OPTION_VIDEO_META);
501 /* parse the config and keep around */
502 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
506 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
509 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
512 gst_object_unref (pool->allocator);
513 pool->allocator = NULL;
516 case GST_V4L2_IO_DMABUF:
517 pool->allocator = gst_dmabuf_allocator_new ();
518 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
520 case GST_V4L2_IO_MMAP:
521 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
523 case GST_V4L2_IO_USERPTR:
525 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
527 case GST_V4L2_IO_DMABUF_IMPORT:
528 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
532 pool->allocator = g_object_ref (allocator);
533 pool->params = params;
534 /* No need to change the configuration */
538 g_assert_not_reached ();
542 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
543 * instability and crash, disable it for now */
544 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
545 GST_WARNING_OBJECT (pool,
546 "libv4l2 converter detected, disabling CREATE_BUFS");
547 can_allocate = FALSE;
548 GST_OBJECT_FLAG_UNSET (pool->vallocator,
549 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
550 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
551 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
554 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
556 min_buffers = GST_V4L2_MIN_BUFFERS;
557 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
560 /* respect driver requirements */
561 if (min_buffers < obj->min_buffers) {
563 min_buffers = obj->min_buffers;
564 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
567 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
569 max_buffers = VIDEO_MAX_FRAME;
570 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
573 if (min_buffers > max_buffers) {
575 min_buffers = max_buffers;
576 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
577 } else if (min_buffers != max_buffers) {
580 max_buffers = min_buffers;
581 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
585 if (!pool->add_videometa && obj->need_video_meta) {
586 GST_INFO_OBJECT (pool, "adding needed video meta");
588 gst_buffer_pool_config_add_option (config,
589 GST_BUFFER_POOL_OPTION_VIDEO_META);
592 /* Always update the config to ensure the configured size matches */
593 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
596 /* keep a GstVideoInfo with defaults for the when we need to copy */
597 gst_video_info_from_caps (&pool->caps_info, caps);
600 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
602 /* If anything was changed documentation recommand to return FALSE */
603 return !updated && ret;
608 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
614 gst_v4l2_buffer_pool_resurrect_buffer (GstV4l2BufferPool * pool)
616 GstBufferPoolAcquireParams params = { 0 };
617 GstBuffer *buffer = NULL;
620 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
622 /* block recursive calls to this function */
623 g_signal_handler_block (pool->vallocator, pool->group_released_handler);
626 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT |
627 GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
629 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
631 if (ret == GST_FLOW_OK)
632 gst_buffer_unref (buffer);
634 g_signal_handler_unblock (pool->vallocator, pool->group_released_handler);
640 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
642 GstV4l2Object *obj = pool->obj;
648 case GST_V4L2_IO_MMAP:
649 case GST_V4L2_IO_USERPTR:
650 case GST_V4L2_IO_DMABUF:
651 case GST_V4L2_IO_DMABUF_IMPORT:
652 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
655 /* For captures, we need to enqueue buffers before we start streaming,
656 * so the driver don't underflow immediatly. As we have put then back
657 * into the base class queue, resurrect them, then releasing will queue
659 for (i = 0; i < pool->num_allocated; i++)
660 gst_v4l2_buffer_pool_resurrect_buffer (pool);
663 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
664 goto streamon_failed;
666 pool->streaming = TRUE;
668 GST_DEBUG_OBJECT (pool, "Started streaming");
678 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
684 /* Call with streamlock held, or when streaming threads are down */
686 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
688 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
689 GstV4l2Object *obj = pool->obj;
692 if (!pool->streaming)
696 case GST_V4L2_IO_MMAP:
697 case GST_V4L2_IO_USERPTR:
698 case GST_V4L2_IO_DMABUF:
699 case GST_V4L2_IO_DMABUF_IMPORT:
701 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
702 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
703 errno, g_strerror (errno));
705 pool->streaming = FALSE;
707 GST_DEBUG_OBJECT (pool, "Stopped streaming");
709 if (pool->vallocator)
710 gst_v4l2_allocator_flush (pool->vallocator);
716 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
717 if (pool->buffers[i]) {
718 GstBuffer *buffer = pool->buffers[i];
719 GstBufferPool *bpool = GST_BUFFER_POOL (pool);
721 pool->buffers[i] = NULL;
723 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
724 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
725 else /* Don't re-enqueue capture buffer on stop */
726 pclass->release_buffer (bpool, buffer);
728 g_atomic_int_add (&pool->num_queued, -1);
734 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
736 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
737 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
738 GstV4l2Object *obj = pool->obj;
739 GstStructure *config;
741 guint size, min_buffers, max_buffers;
742 guint max_latency, min_latency, copy_threshold = 0;
743 gboolean can_allocate = FALSE, ret = TRUE;
745 GST_DEBUG_OBJECT (pool, "activating pool");
747 if (pool->other_pool) {
750 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
751 goto other_pool_failed;
753 if (gst_buffer_pool_acquire_buffer (pool->other_pool, &buffer, NULL) !=
755 goto other_pool_failed;
757 if (!gst_v4l2_object_try_import (obj, buffer)) {
758 gst_buffer_unref (buffer);
761 gst_buffer_unref (buffer);
764 config = gst_buffer_pool_get_config (bpool);
765 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
769 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
775 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
776 * of MMAP. In this case, the first read initialize the queues, but the
777 * poll before that will always fail. Doing an empty read, forces the
778 * queue to be initialized now. We only do this if we have a streaming
780 if (obj->device_caps & V4L2_CAP_STREAMING)
781 obj->read (obj->video_fd, NULL, 0);
784 case GST_V4L2_IO_DMABUF:
785 case GST_V4L2_IO_MMAP:
789 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
791 /* first, lets request buffers, and see how many we can get: */
792 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
794 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
796 pool->num_allocated = count;
798 if (count < GST_V4L2_MIN_BUFFERS) {
803 /* V4L2 buffer pool are often very limited in the amount of buffers it
804 * can offer. The copy_threshold will workaround this limitation by
805 * falling back to copy if the pipeline needed more buffers. This also
806 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
808 if (count != min_buffers || pool->enable_copy_threshold) {
809 GST_WARNING_OBJECT (pool,
810 "Uncertain or not enough buffers, enabling copy threshold");
812 copy_threshold = min_latency;
817 case GST_V4L2_IO_USERPTR:
822 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
824 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
826 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
827 V4L2_MEMORY_USERPTR);
829 /* There is no rational to not get what we asked */
830 if (count < min_buffers) {
838 case GST_V4L2_IO_DMABUF_IMPORT:
842 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
844 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
846 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
849 /* There is no rational to not get what we asked */
850 if (count < min_buffers) {
861 g_assert_not_reached ();
866 max_latency = max_buffers;
868 max_latency = min_buffers;
871 pool->copy_threshold = copy_threshold;
872 pool->max_latency = max_latency;
873 pool->min_latency = min_latency;
874 pool->num_queued = 0;
876 if (max_buffers != 0 && max_buffers < min_buffers)
877 max_buffers = min_buffers;
879 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
881 pclass->set_config (bpool, config);
882 gst_structure_free (config);
884 /* now, allocate the buffers: */
885 if (!pclass->start (bpool))
888 if (!V4L2_TYPE_IS_OUTPUT (obj->type)) {
889 if (g_atomic_int_get (&pool->num_queued) < min_buffers)
892 pool->group_released_handler =
893 g_signal_connect_swapped (pool->vallocator, "group-released",
894 G_CALLBACK (gst_v4l2_buffer_pool_resurrect_buffer), pool);
895 ret = gst_v4l2_buffer_pool_streamon (pool);
903 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
904 gst_structure_free (config);
909 GST_ERROR_OBJECT (pool,
910 "we received %d buffer from device '%s', we want at least %d",
911 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
912 gst_structure_free (config);
917 GST_ERROR_OBJECT (pool, "allocate failed");
922 GST_ERROR_OBJECT (pool, "failed to activate the other pool %"
923 GST_PTR_FORMAT, pool->other_pool);
928 GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue");
933 GST_ERROR_OBJECT (pool, "cannot import buffers from downstream pool");
939 gst_v4l2_buffer_pool_vallocator_stop (GstV4l2BufferPool * pool)
943 if (!pool->vallocator)
946 vret = gst_v4l2_allocator_stop (pool->vallocator);
948 if (vret == GST_V4L2_BUSY)
949 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
951 return (vret == GST_V4L2_OK);
955 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
957 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
961 return gst_v4l2_buffer_pool_vallocator_stop (pool);
963 GST_DEBUG_OBJECT (pool, "stopping pool");
965 if (pool->group_released_handler > 0) {
966 g_signal_handler_disconnect (pool->vallocator,
967 pool->group_released_handler);
968 pool->group_released_handler = 0;
971 if (pool->other_pool) {
972 gst_buffer_pool_set_active (pool->other_pool, FALSE);
973 gst_object_unref (pool->other_pool);
974 pool->other_pool = NULL;
977 gst_v4l2_buffer_pool_streamoff (pool);
979 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
982 ret = gst_v4l2_buffer_pool_vallocator_stop (pool);
988 gst_v4l2_buffer_pool_orphan (GstBufferPool ** bpool)
990 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (*bpool);
993 if (!GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS (pool->vallocator))
996 if (g_getenv ("GST_V4L2_FORCE_DRAIN"))
999 GST_DEBUG_OBJECT (pool, "orphaning pool");
1001 gst_buffer_pool_set_active (*bpool, FALSE);
1003 * If the buffer pool has outstanding buffers, it will not be stopped
1004 * by the base class when set inactive. Stop it manually and mark it
1007 ret = gst_v4l2_buffer_pool_stop (*bpool);
1009 ret = gst_v4l2_allocator_orphan (pool->vallocator);
1014 pool->orphaned = TRUE;
1015 gst_object_unref (*bpool);
1023 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
1025 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1027 GST_DEBUG_OBJECT (pool, "start flushing");
1029 gst_poll_set_flushing (pool->poll, TRUE);
1031 GST_OBJECT_LOCK (pool);
1032 pool->empty = FALSE;
1033 g_cond_broadcast (&pool->empty_cond);
1034 GST_OBJECT_UNLOCK (pool);
1036 if (pool->other_pool)
1037 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
1041 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
1043 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1045 GST_DEBUG_OBJECT (pool, "stop flushing");
1047 if (pool->other_pool)
1048 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
1050 gst_poll_set_flushing (pool->poll, FALSE);
1053 static GstFlowReturn
1054 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool, gboolean wait)
1057 GstClockTime timeout;
1060 timeout = GST_CLOCK_TIME_NONE;
1064 /* In RW mode there is no queue, hence no need to wait while the queue is
1066 if (pool->obj->mode != GST_V4L2_IO_RW) {
1067 GST_OBJECT_LOCK (pool);
1069 if (!wait && pool->empty) {
1070 GST_OBJECT_UNLOCK (pool);
1075 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1077 GST_OBJECT_UNLOCK (pool);
1080 if (!pool->can_poll_device) {
1087 GST_LOG_OBJECT (pool, "polling device");
1090 ret = gst_poll_wait (pool->poll, timeout);
1091 if (G_UNLIKELY (ret < 0)) {
1099 GST_WARNING_OBJECT (pool,
1100 "v4l2 device doesn't support polling. Disabling"
1101 " using libv4l2 in this case may cause deadlocks");
1102 pool->can_poll_device = FALSE;
1109 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1121 GST_DEBUG_OBJECT (pool, "stop called");
1122 return GST_FLOW_FLUSHING;
1126 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1127 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1128 return GST_FLOW_ERROR;
1131 return GST_FLOW_CUSTOM_SUCCESS;
1134 static GstFlowReturn
1135 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf,
1136 GstV4l2MemoryGroup * group)
1138 const GstV4l2Object *obj = pool->obj;
1139 GstClockTime timestamp;
1142 index = group->buffer.index;
1144 if (pool->buffers[index] != NULL)
1145 goto already_queued;
1147 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1149 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1150 enum v4l2_field field;
1152 /* Except when field is set to alternate, buffer field is the same as
1153 * the one defined in format */
1154 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1155 field = obj->format.fmt.pix_mp.field;
1157 field = obj->format.fmt.pix.field;
1159 /* NB: At this moment, we can't have alternate mode because it not handled
1161 if (field == V4L2_FIELD_ALTERNATE) {
1162 if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1163 field = V4L2_FIELD_TOP;
1165 field = V4L2_FIELD_BOTTOM;
1168 group->buffer.field = field;
1171 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1172 timestamp = GST_BUFFER_TIMESTAMP (buf);
1173 GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp);
1176 GST_OBJECT_LOCK (pool);
1177 g_atomic_int_inc (&pool->num_queued);
1178 pool->buffers[index] = buf;
1180 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1183 pool->empty = FALSE;
1184 g_cond_signal (&pool->empty_cond);
1185 GST_OBJECT_UNLOCK (pool);
1191 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1192 return GST_FLOW_ERROR;
1196 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1197 /* Mark broken buffer to the allocator */
1198 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1199 g_atomic_int_add (&pool->num_queued, -1);
1200 pool->buffers[index] = NULL;
1201 GST_OBJECT_UNLOCK (pool);
1202 return GST_FLOW_ERROR;
1206 static GstFlowReturn
1207 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
1211 GstBuffer *outbuf = NULL;
1212 GstV4l2Object *obj = pool->obj;
1213 GstClockTime timestamp;
1214 GstV4l2MemoryGroup *group;
1215 GstVideoMeta *vmeta;
1219 if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
1222 if (res == GST_FLOW_CUSTOM_SUCCESS) {
1223 GST_LOG_OBJECT (pool, "nothing to dequeue");
1227 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1229 res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
1230 if (res == GST_FLOW_EOS)
1232 if (res != GST_FLOW_OK)
1235 /* get our GstBuffer with that index from the pool, if the buffer was
1236 * outstanding we have a serious problem.
1238 outbuf = pool->buffers[group->buffer.index];
1242 /* mark the buffer outstanding */
1243 pool->buffers[group->buffer.index] = NULL;
1244 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1245 GST_OBJECT_LOCK (pool);
1247 GST_OBJECT_UNLOCK (pool);
1250 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1253 vmeta = gst_buffer_get_video_meta (outbuf);
1254 for (i = 0; i < group->n_mem; i++) {
1255 GST_LOG_OBJECT (pool,
1256 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1257 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1258 group->buffer.sequence, group->buffer.index, group->mem[i],
1259 group->planes[i].bytesused, i, group->buffer.flags,
1260 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1263 vmeta->offset[i] = size;
1264 size += gst_memory_get_sizes (group->mem[i], NULL, NULL);
1268 /* Ignore timestamp and field for OUTPUT device */
1269 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1272 /* Check for driver bug in reporting feild */
1273 if (group->buffer.field == V4L2_FIELD_ANY) {
1274 /* Only warn once to avoid the spamming */
1275 #ifndef GST_DISABLE_GST_DEBUG
1276 if (!pool->has_warned_on_buggy_field) {
1277 pool->has_warned_on_buggy_field = TRUE;
1278 GST_WARNING_OBJECT (pool,
1279 "Driver should never set v4l2_buffer.field to ANY");
1283 /* Use the value from the format (works for UVC bug) */
1284 group->buffer.field = obj->format.fmt.pix.field;
1286 /* If driver also has buggy S_FMT, assume progressive */
1287 if (group->buffer.field == V4L2_FIELD_ANY) {
1288 #ifndef GST_DISABLE_GST_DEBUG
1289 if (!pool->has_warned_on_buggy_field) {
1290 pool->has_warned_on_buggy_field = TRUE;
1291 GST_WARNING_OBJECT (pool,
1292 "Driver should never set v4l2_format.pix.field to ANY");
1296 group->buffer.field = V4L2_FIELD_NONE;
1300 /* set top/bottom field first if v4l2_buffer has the information */
1301 switch (group->buffer.field) {
1302 case V4L2_FIELD_NONE:
1303 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1304 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1306 case V4L2_FIELD_INTERLACED_TB:
1307 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1308 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1310 case V4L2_FIELD_INTERLACED_BT:
1311 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1312 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1314 case V4L2_FIELD_INTERLACED:
1315 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1316 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1317 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1318 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1319 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1321 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1325 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1326 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1327 GST_FIXME_OBJECT (pool,
1328 "Unhandled enum v4l2_field %d - treating as progressive",
1329 group->buffer.field);
1333 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1334 if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) ||
1335 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG ||
1336 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG ||
1337 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG)
1338 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1340 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1343 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1344 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1346 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1347 GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
1348 GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
1358 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1363 return GST_FLOW_EOS;
1367 return GST_FLOW_ERROR;
1371 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1372 group->buffer.index);
1373 return GST_FLOW_ERROR;
1377 static GstFlowReturn
1378 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1379 GstBufferPoolAcquireParams * params)
1382 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1383 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1384 GstV4l2Object *obj = pool->obj;
1386 GST_DEBUG_OBJECT (pool, "acquire");
1388 /* If this is being called to resurrect a lost buffer */
1389 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1390 ret = pclass->acquire_buffer (bpool, buffer, params);
1394 switch (obj->type) {
1395 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1396 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1397 /* capture, This function should return a buffer with new captured data */
1398 switch (obj->mode) {
1399 case GST_V4L2_IO_RW:
1401 /* take empty buffer from the pool */
1402 ret = pclass->acquire_buffer (bpool, buffer, params);
1405 case GST_V4L2_IO_DMABUF:
1406 case GST_V4L2_IO_MMAP:
1407 case GST_V4L2_IO_USERPTR:
1408 case GST_V4L2_IO_DMABUF_IMPORT:
1410 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1411 * storage for our buffers. This function does poll first so we can
1412 * interrupt it fine. */
1413 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer, TRUE);
1417 ret = GST_FLOW_ERROR;
1418 g_assert_not_reached ();
1424 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1425 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1426 /* playback, This function should return an empty buffer */
1427 switch (obj->mode) {
1428 case GST_V4L2_IO_RW:
1429 /* get an empty buffer */
1430 ret = pclass->acquire_buffer (bpool, buffer, params);
1433 case GST_V4L2_IO_MMAP:
1434 case GST_V4L2_IO_DMABUF:
1435 case GST_V4L2_IO_USERPTR:
1436 case GST_V4L2_IO_DMABUF_IMPORT:
1437 /* get a free unqueued buffer */
1438 ret = pclass->acquire_buffer (bpool, buffer, params);
1442 ret = GST_FLOW_ERROR;
1443 g_assert_not_reached ();
1449 ret = GST_FLOW_ERROR;
1450 g_assert_not_reached ();
1458 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1460 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1461 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1462 GstV4l2Object *obj = pool->obj;
1464 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1466 /* If the buffer's pool has been orphaned, dispose of it so that
1467 * the pool resources can be freed */
1468 if (pool->orphaned) {
1469 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1470 pclass->release_buffer (bpool, buffer);
1474 switch (obj->type) {
1475 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1476 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1477 /* capture, put the buffer back in the queue so that we can refill it
1479 switch (obj->mode) {
1480 case GST_V4L2_IO_RW:
1481 /* release back in the pool */
1482 pclass->release_buffer (bpool, buffer);
1485 case GST_V4L2_IO_DMABUF:
1486 case GST_V4L2_IO_MMAP:
1487 case GST_V4L2_IO_USERPTR:
1488 case GST_V4L2_IO_DMABUF_IMPORT:
1490 GstV4l2MemoryGroup *group;
1491 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1492 GstFlowReturn ret = GST_FLOW_OK;
1494 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1495 /* queue back in the device */
1496 if (pool->other_pool)
1497 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1498 if (ret != GST_FLOW_OK ||
1499 gst_v4l2_buffer_pool_qbuf (pool, buffer, group) != GST_FLOW_OK)
1500 pclass->release_buffer (bpool, buffer);
1502 /* Simply release invalide/modified buffer, the allocator will
1503 * give it back later */
1504 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1505 pclass->release_buffer (bpool, buffer);
1510 g_assert_not_reached ();
1515 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1516 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1517 switch (obj->mode) {
1518 case GST_V4L2_IO_RW:
1519 /* release back in the pool */
1520 pclass->release_buffer (bpool, buffer);
1523 case GST_V4L2_IO_MMAP:
1524 case GST_V4L2_IO_DMABUF:
1525 case GST_V4L2_IO_USERPTR:
1526 case GST_V4L2_IO_DMABUF_IMPORT:
1528 GstV4l2MemoryGroup *group;
1531 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1532 /* Simply release invalide/modified buffer, the allocator will
1533 * give it back later */
1534 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1535 pclass->release_buffer (bpool, buffer);
1539 index = group->buffer.index;
1541 if (pool->buffers[index] == NULL) {
1542 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1545 /* Remove qdata, this will unmap any map data in userptr */
1546 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1547 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1549 /* reset to default size */
1550 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1552 /* playback, put the buffer back in the queue to refill later. */
1553 pclass->release_buffer (bpool, buffer);
1555 /* the buffer is queued in the device but maybe not played yet. We just
1556 * leave it there and not make it available for future calls to acquire
1557 * for now. The buffer will be dequeued and reused later. */
1558 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1564 g_assert_not_reached ();
1570 g_assert_not_reached ();
1576 gst_v4l2_buffer_pool_dispose (GObject * object)
1578 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1580 if (pool->vallocator)
1581 gst_object_unref (pool->vallocator);
1582 pool->vallocator = NULL;
1584 if (pool->allocator)
1585 gst_object_unref (pool->allocator);
1586 pool->allocator = NULL;
1588 if (pool->other_pool)
1589 gst_object_unref (pool->other_pool);
1590 pool->other_pool = NULL;
1592 G_OBJECT_CLASS (parent_class)->dispose (object);
1596 gst_v4l2_buffer_pool_finalize (GObject * object)
1598 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1600 if (pool->video_fd >= 0)
1601 pool->obj->close (pool->video_fd);
1603 gst_poll_free (pool->poll);
1605 /* This can't be done in dispose method because we must not set pointer
1606 * to NULL as it is part of the v4l2object and dispose could be called
1608 gst_object_unref (pool->obj->element);
1610 g_cond_clear (&pool->empty_cond);
1612 /* FIXME have we done enough here ? */
1614 G_OBJECT_CLASS (parent_class)->finalize (object);
1618 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1620 pool->poll = gst_poll_new (TRUE);
1621 pool->can_poll_device = TRUE;
1622 g_cond_init (&pool->empty_cond);
1624 pool->orphaned = FALSE;
1628 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1630 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1631 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1633 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1634 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1636 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1637 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1638 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1639 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1640 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1641 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1642 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1643 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1645 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1646 "V4L2 Buffer Pool");
1647 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
1651 * gst_v4l2_buffer_pool_new:
1652 * @obj: the v4l2 object owning the pool
1654 * Construct a new buffer pool.
1656 * Returns: the new pool, use gst_object_unref() to free resources
1659 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1661 GstV4l2BufferPool *pool;
1662 GstStructure *config;
1663 gchar *name, *parent_name;
1666 fd = obj->dup (obj->video_fd);
1670 /* setting a significant unique name */
1671 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1672 name = g_strconcat (parent_name, ":", "pool:",
1673 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1674 g_free (parent_name);
1676 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1677 "name", name, NULL);
1678 g_object_ref_sink (pool);
1681 gst_poll_fd_init (&pool->pollfd);
1682 pool->pollfd.fd = fd;
1683 gst_poll_add_fd (pool->poll, &pool->pollfd);
1684 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1685 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1687 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1689 pool->video_fd = fd;
1691 pool->can_poll_device = TRUE;
1693 pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
1694 if (pool->vallocator == NULL)
1695 goto allocator_failed;
1697 gst_object_ref (obj->element);
1699 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1700 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1701 /* This will simply set a default config, but will not configure the pool
1702 * because min and max are not valid */
1703 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1705 return GST_BUFFER_POOL (pool);
1710 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1715 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1716 gst_object_unref (pool);
1721 static GstFlowReturn
1722 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1725 GstV4l2Object *obj = pool->obj;
1730 toread = obj->info.size;
1732 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1734 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1737 if ((res = gst_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK)
1740 amount = obj->read (obj->video_fd, map.data, toread);
1742 if (amount == toread) {
1744 } else if (amount == -1) {
1745 if (errno == EAGAIN || errno == EINTR) {
1750 /* short reads can happen if a signal interrupts the read */
1755 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1756 gst_buffer_unmap (buf, &map);
1757 gst_buffer_resize (buf, 0, amount);
1764 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1769 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1770 (_("Error reading %d bytes from device '%s'."),
1771 toread, obj->videodev), GST_ERROR_SYSTEM);
1772 res = GST_FLOW_ERROR;
1777 gst_buffer_unmap (buf, &map);
1778 gst_buffer_resize (buf, 0, 0);
1784 * gst_v4l2_buffer_pool_process:
1785 * @bpool: a #GstBufferPool
1786 * @buf: a #GstBuffer, maybe be replaced
1788 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1789 * data from the device. For output devices, this functions send the contents of
1790 * @buf to the device for playback.
1792 * Returns: %GST_FLOW_OK on success.
1795 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1797 GstFlowReturn ret = GST_FLOW_OK;
1798 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1799 GstV4l2Object *obj = pool->obj;
1801 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1803 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1804 return GST_FLOW_FLUSHING;
1806 switch (obj->type) {
1807 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1808 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1810 switch (obj->mode) {
1811 case GST_V4L2_IO_RW:
1812 /* capture into the buffer */
1813 ret = gst_v4l2_do_read (pool, *buf);
1816 case GST_V4L2_IO_MMAP:
1817 case GST_V4L2_IO_DMABUF:
1821 if ((*buf)->pool == bpool) {
1823 gsize size = gst_buffer_get_size (*buf);
1825 /* Legacy M2M devices return empty buffer when drained */
1826 if (size == 0 && GST_V4L2_IS_M2M (obj->device_caps))
1829 if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) !=
1830 GST_VIDEO_FORMAT_ENCODED && size < pool->size)
1831 goto buffer_truncated;
1833 num_queued = g_atomic_int_get (&pool->num_queued);
1834 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1837 /* If we have no more buffer, and can allocate it time to do so */
1838 if (num_queued == 0) {
1839 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1840 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1841 if (ret == GST_FLOW_OK)
1846 /* start copying buffers when we are running low on buffers */
1847 if (num_queued < pool->copy_threshold) {
1850 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1851 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1852 if (ret == GST_FLOW_OK)
1856 /* copy the buffer */
1857 copy = gst_buffer_copy_region (*buf,
1858 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1859 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1861 /* and requeue so that we can continue capturing */
1862 gst_buffer_unref (*buf);
1867 /* nothing, data was inside the buffer when we did _acquire() */
1871 /* buffer not from our pool, grab a frame and copy it into the target */
1872 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp, TRUE))
1876 /* An empty buffer on capture indicates the end of stream */
1877 if (gst_buffer_get_size (tmp) == 0) {
1878 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1880 /* Legacy M2M devices return empty buffer when drained */
1881 if (GST_V4L2_IS_M2M (obj->device_caps))
1885 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1887 /* an queue the buffer again after the copy */
1888 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1890 if (ret != GST_FLOW_OK)
1895 case GST_V4L2_IO_USERPTR:
1897 struct UserPtrData *data;
1900 /* Replace our buffer with downstream allocated buffer */
1901 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1902 GST_V4L2_IMPORT_QUARK);
1903 tmp = gst_buffer_ref (data->buffer);
1904 _unmap_userptr_frame (data);
1906 /* Now tmp is writable, copy the flags and timestamp */
1907 gst_buffer_copy_into (tmp, *buf,
1908 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1910 gst_buffer_replace (buf, tmp);
1911 gst_buffer_unref (tmp);
1915 case GST_V4L2_IO_DMABUF_IMPORT:
1919 /* Replace our buffer with downstream allocated buffer */
1920 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1921 GST_V4L2_IMPORT_QUARK);
1923 gst_buffer_copy_into (tmp, *buf,
1924 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1926 gst_buffer_replace (buf, tmp);
1927 gst_buffer_unref (tmp);
1932 g_assert_not_reached ();
1937 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1938 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1940 switch (obj->mode) {
1941 case GST_V4L2_IO_RW:
1942 /* FIXME, do write() */
1943 GST_WARNING_OBJECT (pool, "implement write()");
1946 case GST_V4L2_IO_USERPTR:
1947 case GST_V4L2_IO_DMABUF_IMPORT:
1948 case GST_V4L2_IO_DMABUF:
1949 case GST_V4L2_IO_MMAP:
1951 GstBuffer *to_queue = NULL;
1953 GstV4l2MemoryGroup *group;
1956 if ((*buf)->pool != bpool)
1959 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1962 index = group->buffer.index;
1964 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1966 if (pool->buffers[index] != NULL) {
1967 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1971 /* we can queue directly */
1972 to_queue = gst_buffer_ref (*buf);
1975 if (to_queue == NULL) {
1976 GstBufferPoolAcquireParams params = { 0 };
1978 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1980 /* this can return EOS if all buffers are outstanding which would
1981 * be strange because we would expect the upstream element to have
1982 * allocated them and returned to us.. */
1983 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1984 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1985 if (ret != GST_FLOW_OK)
1986 goto acquire_failed;
1988 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1989 if (ret != GST_FLOW_OK) {
1990 gst_buffer_unref (to_queue);
1991 goto prepare_failed;
1994 /* retreive the group */
1995 gst_v4l2_is_buffer_valid (to_queue, &group);
1998 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue, group))
2002 /* if we are not streaming yet (this is the first buffer, start
2004 if (!gst_v4l2_buffer_pool_streamon (pool)) {
2005 /* don't check return value because qbuf would have failed */
2006 gst_v4l2_is_buffer_valid (to_queue, &group);
2008 /* qbuf has stored to_queue buffer but we are not in
2009 * streaming state, so the flush logic won't be performed.
2010 * To avoid leaks, flush the allocator and restore the queued
2011 * buffer as non-queued */
2012 gst_v4l2_allocator_flush (pool->vallocator);
2014 pool->buffers[group->buffer.index] = NULL;
2016 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
2017 GST_V4L2_IMPORT_QUARK, NULL, NULL);
2018 gst_buffer_unref (to_queue);
2019 g_atomic_int_add (&pool->num_queued, -1);
2023 /* Remove our ref, we will still hold this buffer in acquire as needed,
2024 * otherwise the pool will think it is outstanding and will refuse to stop. */
2025 gst_buffer_unref (to_queue);
2027 /* release as many buffer as possible */
2028 while (gst_v4l2_buffer_pool_dqbuf (pool, &buffer, FALSE) ==
2030 if (buffer->pool == NULL)
2031 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
2034 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
2035 /* all buffers are queued, try to dequeue one and release it back
2036 * into the pool so that _acquire can get to it again. */
2037 ret = gst_v4l2_buffer_pool_dqbuf (pool, &buffer, TRUE);
2038 if (ret == GST_FLOW_OK && buffer->pool == NULL)
2039 /* release the rendered buffer back into the pool. This wakes up any
2040 * thread waiting for a buffer in _acquire(). */
2041 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
2046 g_assert_not_reached ();
2051 g_assert_not_reached ();
2060 GST_ERROR_OBJECT (pool, "failed to copy buffer");
2065 GST_WARNING_OBJECT (pool,
2066 "Dropping truncated buffer, this is likely a driver bug.");
2067 gst_buffer_unref (*buf);
2069 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
2073 GST_DEBUG_OBJECT (pool, "end of stream reached");
2074 gst_buffer_unref (*buf);
2076 return GST_V4L2_FLOW_LAST_BUFFER;
2080 if (ret == GST_FLOW_FLUSHING)
2081 GST_DEBUG_OBJECT (pool, "flushing");
2083 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
2084 gst_flow_get_name (ret));
2089 GST_ERROR_OBJECT (pool, "failed to prepare data");
2094 GST_ERROR_OBJECT (pool, "failed to queue buffer");
2099 GST_ERROR_OBJECT (pool, "failed to start streaming");
2100 return GST_FLOW_ERROR;
2105 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
2106 GstBufferPool * other_pool)
2108 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
2110 if (pool->other_pool)
2111 gst_object_unref (pool->other_pool);
2112 pool->other_pool = gst_object_ref (other_pool);
2116 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
2118 GST_OBJECT_LOCK (pool);
2119 pool->enable_copy_threshold = copy;
2120 GST_OBJECT_UNLOCK (pool);
2124 gst_v4l2_buffer_pool_flush (GstBufferPool * bpool)
2126 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
2127 gboolean ret = TRUE;
2129 gst_v4l2_buffer_pool_streamoff (pool);
2131 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type))
2132 ret = gst_v4l2_buffer_pool_streamon (pool);