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_streamon (GstV4l2BufferPool * pool)
616 GstV4l2Object *obj = pool->obj;
619 case GST_V4L2_IO_MMAP:
620 case GST_V4L2_IO_USERPTR:
621 case GST_V4L2_IO_DMABUF:
622 case GST_V4L2_IO_DMABUF_IMPORT:
623 if (!pool->streaming) {
624 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
625 goto streamon_failed;
627 pool->streaming = TRUE;
629 GST_DEBUG_OBJECT (pool, "Started streaming");
640 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
647 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
649 GstV4l2Object *obj = pool->obj;
652 case GST_V4L2_IO_MMAP:
653 case GST_V4L2_IO_USERPTR:
654 case GST_V4L2_IO_DMABUF:
655 case GST_V4L2_IO_DMABUF_IMPORT:
656 if (pool->streaming) {
657 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
658 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
659 errno, g_strerror (errno));
661 pool->streaming = FALSE;
663 GST_DEBUG_OBJECT (pool, "Stopped streaming");
665 if (pool->vallocator)
666 gst_v4l2_allocator_flush (pool->vallocator);
675 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
677 GstBufferPoolAcquireParams params = { 0 };
678 GstBuffer *buffer = NULL;
681 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
683 /* block recursive calls to this function */
684 g_signal_handler_block (pool->vallocator, pool->group_released_handler);
687 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT |
688 GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
690 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
692 if (ret == GST_FLOW_OK)
693 gst_buffer_unref (buffer);
695 g_signal_handler_unblock (pool->vallocator, pool->group_released_handler);
701 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
703 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
704 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
705 GstV4l2Object *obj = pool->obj;
706 GstStructure *config;
708 guint size, min_buffers, max_buffers;
709 guint max_latency, min_latency, copy_threshold = 0;
710 gboolean can_allocate = FALSE;
712 GST_DEBUG_OBJECT (pool, "activating pool");
714 config = gst_buffer_pool_get_config (bpool);
715 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
719 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
725 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
726 * of MMAP. In this case, the first read initialize the queues, but the
727 * poll before that will always fail. Doing an empty read, forces the
728 * queue to be initialized now. We only do this if we have a streaming
730 if (obj->device_caps & V4L2_CAP_STREAMING)
731 obj->read (obj->video_fd, NULL, 0);
734 case GST_V4L2_IO_DMABUF:
735 case GST_V4L2_IO_MMAP:
739 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
741 /* first, lets request buffers, and see how many we can get: */
742 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
744 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
747 if (count < GST_V4L2_MIN_BUFFERS) {
752 /* V4L2 buffer pool are often very limited in the amount of buffers it
753 * can offer. The copy_threshold will workaround this limitation by
754 * falling back to copy if the pipeline needed more buffers. This also
755 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
757 if (count != min_buffers || pool->enable_copy_threshold) {
758 GST_WARNING_OBJECT (pool,
759 "Uncertain or not enough buffers, enabling copy threshold");
761 copy_threshold = min_latency;
766 case GST_V4L2_IO_USERPTR:
771 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
773 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
775 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
776 V4L2_MEMORY_USERPTR);
778 /* There is no rational to not get what we asked */
779 if (count < min_buffers) {
787 case GST_V4L2_IO_DMABUF_IMPORT:
791 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
793 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
795 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
798 /* There is no rational to not get what we asked */
799 if (count < min_buffers) {
810 g_assert_not_reached ();
815 max_latency = max_buffers;
817 max_latency = min_buffers;
820 pool->copy_threshold = copy_threshold;
821 pool->max_latency = max_latency;
822 pool->min_latency = min_latency;
823 pool->num_queued = 0;
825 if (max_buffers != 0 && max_buffers < min_buffers)
826 max_buffers = min_buffers;
828 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
830 pclass->set_config (bpool, config);
831 gst_structure_free (config);
833 if (pool->other_pool)
834 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
835 goto other_pool_failed;
837 /* now, allocate the buffers: */
838 if (!pclass->start (bpool))
841 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
842 pool->group_released_handler =
843 g_signal_connect_swapped (pool->vallocator, "group-released",
844 G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
851 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
852 gst_structure_free (config);
857 GST_ERROR_OBJECT (pool,
858 "we received %d buffer from device '%s', we want at least %d",
859 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
860 gst_structure_free (config);
865 GST_ERROR_OBJECT (pool, "allocate failed");
870 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
871 GST_PTR_FORMAT, pool->other_pool);
877 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
879 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
880 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
884 GST_DEBUG_OBJECT (pool, "stopping pool");
886 if (pool->group_released_handler > 0) {
887 g_signal_handler_disconnect (pool->vallocator,
888 pool->group_released_handler);
889 pool->group_released_handler = 0;
892 if (pool->other_pool) {
893 gst_buffer_pool_set_active (pool->other_pool, FALSE);
894 gst_object_unref (pool->other_pool);
895 pool->other_pool = NULL;
898 gst_v4l2_buffer_pool_streamoff (pool);
900 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
901 if (pool->buffers[i]) {
902 GstBuffer *buffer = pool->buffers[i];
904 pool->buffers[i] = NULL;
906 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
907 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
908 else /* Don't re-enqueue capture buffer on stop */
909 pclass->release_buffer (bpool, buffer);
911 g_atomic_int_add (&pool->num_queued, -1);
915 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
917 if (ret && pool->vallocator) {
920 vret = gst_v4l2_allocator_stop (pool->vallocator);
922 if (vret == GST_V4L2_BUSY)
923 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
925 ret = (vret == GST_V4L2_OK);
932 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
934 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
936 GST_DEBUG_OBJECT (pool, "start flushing");
938 gst_poll_set_flushing (pool->poll, TRUE);
940 GST_OBJECT_LOCK (pool);
942 g_cond_broadcast (&pool->empty_cond);
943 GST_OBJECT_UNLOCK (pool);
945 if (pool->other_pool)
946 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
950 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
952 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
953 GstV4l2Object *obj = pool->obj;
954 GstBuffer *buffers[VIDEO_MAX_FRAME];
957 GST_DEBUG_OBJECT (pool, "stop flushing");
959 /* If we haven't started streaming yet, simply call streamon */
960 if (!pool->streaming)
963 if (pool->other_pool)
964 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
966 GST_OBJECT_LOCK (pool);
967 gst_v4l2_buffer_pool_streamoff (pool);
968 /* Remember buffers to re-enqueue */
969 memcpy (buffers, pool->buffers, sizeof (buffers));
970 memset (pool->buffers, 0, sizeof (pool->buffers));
971 GST_OBJECT_UNLOCK (pool);
973 /* Reset our state */
977 case GST_V4L2_IO_MMAP:
978 case GST_V4L2_IO_USERPTR:
979 case GST_V4L2_IO_DMABUF:
980 case GST_V4L2_IO_DMABUF_IMPORT:
982 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
983 /* Re-enqueue buffers */
985 GstBufferPool *bpool = (GstBufferPool *) pool;
986 GstBuffer *buffer = buffers[i];
988 /* Remove qdata, this will unmap any map data in
989 * userptr/dmabuf-import */
990 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
991 GST_V4L2_IMPORT_QUARK, NULL, NULL);
993 if (buffer->pool == NULL)
994 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
996 g_atomic_int_add (&pool->num_queued, -1);
1003 g_assert_not_reached ();
1008 /* Start streaming on capture device only */
1009 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
1010 gst_v4l2_buffer_pool_streamon (pool);
1012 gst_poll_set_flushing (pool->poll, FALSE);
1015 static GstFlowReturn
1016 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
1020 /* In RW mode there is no queue, hence no need to wait while the queue is
1022 if (pool->obj->mode != GST_V4L2_IO_RW) {
1023 GST_OBJECT_LOCK (pool);
1025 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1026 GST_OBJECT_UNLOCK (pool);
1029 if (!pool->can_poll_device)
1032 GST_LOG_OBJECT (pool, "polling device");
1035 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
1036 if (G_UNLIKELY (ret < 0)) {
1044 GST_WARNING_OBJECT (pool,
1045 "v4l2 device doesn't support polling. Disabling"
1046 " using libv4l2 in this case may cause deadlocks");
1047 pool->can_poll_device = FALSE;
1054 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1063 GST_DEBUG_OBJECT (pool, "stop called");
1064 return GST_FLOW_FLUSHING;
1068 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1069 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1070 return GST_FLOW_ERROR;
1074 static GstFlowReturn
1075 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1077 GstV4l2MemoryGroup *group = NULL;
1078 const GstV4l2Object *obj = pool->obj;
1079 GstClockTime timestamp;
1082 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1083 GST_ERROR_OBJECT (pool, "invalid buffer %p", buf);
1084 return GST_FLOW_ERROR;
1087 index = group->buffer.index;
1089 if (pool->buffers[index] != NULL)
1090 goto already_queued;
1092 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1094 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1095 enum v4l2_field field;
1097 /* Except when field is set to alternate, buffer field is the same as
1098 * the one defined in format */
1099 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1100 field = obj->format.fmt.pix_mp.field;
1102 field = obj->format.fmt.pix.field;
1104 /* NB: At this moment, we can't have alternate mode because it not handled
1106 if (field == V4L2_FIELD_ALTERNATE) {
1107 if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1108 field = V4L2_FIELD_TOP;
1110 field = V4L2_FIELD_BOTTOM;
1113 group->buffer.field = field;
1116 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1117 timestamp = GST_BUFFER_TIMESTAMP (buf);
1118 GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp);
1121 GST_OBJECT_LOCK (pool);
1122 g_atomic_int_inc (&pool->num_queued);
1123 pool->buffers[index] = buf;
1125 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1128 pool->empty = FALSE;
1129 g_cond_signal (&pool->empty_cond);
1130 GST_OBJECT_UNLOCK (pool);
1136 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1137 return GST_FLOW_ERROR;
1141 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1142 /* Mark broken buffer to the allocator */
1143 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1144 g_atomic_int_add (&pool->num_queued, -1);
1145 pool->buffers[index] = NULL;
1146 GST_OBJECT_UNLOCK (pool);
1147 return GST_FLOW_ERROR;
1151 static GstFlowReturn
1152 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1156 GstV4l2Object *obj = pool->obj;
1157 GstClockTime timestamp;
1158 GstV4l2MemoryGroup *group;
1161 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1164 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1166 res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
1167 if (res == GST_FLOW_EOS)
1169 if (res != GST_FLOW_OK)
1172 /* get our GstBuffer with that index from the pool, if the buffer was
1173 * outstanding we have a serious problem.
1175 outbuf = pool->buffers[group->buffer.index];
1179 /* mark the buffer outstanding */
1180 pool->buffers[group->buffer.index] = NULL;
1181 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1182 GST_OBJECT_LOCK (pool);
1184 GST_OBJECT_UNLOCK (pool);
1187 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1189 #ifndef GST_DISABLE_GST_DEBUG
1190 for (i = 0; i < group->n_mem; i++) {
1191 GST_LOG_OBJECT (pool,
1192 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1193 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1194 group->buffer.sequence, group->buffer.index, group->mem[i],
1195 group->planes[i].bytesused, i, group->buffer.flags,
1196 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1200 /* Ignore timestamp and field for OUTPUT device */
1201 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1204 /* Check for driver bug in reporting feild */
1205 if (group->buffer.field == V4L2_FIELD_ANY) {
1206 /* Only warn once to avoid the spamming */
1207 #ifndef GST_DISABLE_GST_DEBUG
1208 if (!pool->has_warned_on_buggy_field) {
1209 pool->has_warned_on_buggy_field = TRUE;
1210 GST_WARNING_OBJECT (pool,
1211 "Driver should never set v4l2_buffer.field to ANY");
1215 /* Use the value from the format (works for UVC bug) */
1216 group->buffer.field = obj->format.fmt.pix.field;
1218 /* If driver also has buggy S_FMT, assume progressive */
1219 if (group->buffer.field == V4L2_FIELD_ANY) {
1220 #ifndef GST_DISABLE_GST_DEBUG
1221 if (!pool->has_warned_on_buggy_field) {
1222 pool->has_warned_on_buggy_field = TRUE;
1223 GST_WARNING_OBJECT (pool,
1224 "Driver should never set v4l2_format.pix.field to ANY");
1228 group->buffer.field = V4L2_FIELD_NONE;
1232 /* set top/bottom field first if v4l2_buffer has the information */
1233 switch (group->buffer.field) {
1234 case V4L2_FIELD_NONE:
1235 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1236 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1238 case V4L2_FIELD_INTERLACED_TB:
1239 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1240 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1242 case V4L2_FIELD_INTERLACED_BT:
1243 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1244 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1246 case V4L2_FIELD_INTERLACED:
1247 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1248 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1249 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1250 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1251 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1253 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1257 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1258 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1259 GST_FIXME_OBJECT (pool,
1260 "Unhandled enum v4l2_field %d - treating as progressive",
1261 group->buffer.field);
1265 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1266 if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) ||
1267 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG ||
1268 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG ||
1269 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG)
1270 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1272 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1275 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1276 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1278 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1279 GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
1280 GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
1290 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1295 return GST_FLOW_EOS;
1299 return GST_FLOW_ERROR;
1303 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1304 group->buffer.index);
1305 return GST_FLOW_ERROR;
1309 static GstFlowReturn
1310 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1311 GstBufferPoolAcquireParams * params)
1314 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1315 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1316 GstV4l2Object *obj = pool->obj;
1318 GST_DEBUG_OBJECT (pool, "acquire");
1320 /* If this is being called to resurect a lost buffer */
1321 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1322 ret = pclass->acquire_buffer (bpool, buffer, params);
1326 switch (obj->type) {
1327 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1328 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1329 /* capture, This function should return a buffer with new captured data */
1330 switch (obj->mode) {
1331 case GST_V4L2_IO_RW:
1333 /* take empty buffer from the pool */
1334 ret = pclass->acquire_buffer (bpool, buffer, params);
1337 case GST_V4L2_IO_DMABUF:
1338 case GST_V4L2_IO_MMAP:
1339 case GST_V4L2_IO_USERPTR:
1340 case GST_V4L2_IO_DMABUF_IMPORT:
1342 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1343 * storage for our buffers. This function does poll first so we can
1344 * interrupt it fine. */
1345 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1349 ret = GST_FLOW_ERROR;
1350 g_assert_not_reached ();
1356 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1357 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1358 /* playback, This function should return an empty buffer */
1359 switch (obj->mode) {
1360 case GST_V4L2_IO_RW:
1361 /* get an empty buffer */
1362 ret = pclass->acquire_buffer (bpool, buffer, params);
1365 case GST_V4L2_IO_MMAP:
1366 case GST_V4L2_IO_DMABUF:
1367 case GST_V4L2_IO_USERPTR:
1368 case GST_V4L2_IO_DMABUF_IMPORT:
1369 /* get a free unqueued buffer */
1370 ret = pclass->acquire_buffer (bpool, buffer, params);
1374 ret = GST_FLOW_ERROR;
1375 g_assert_not_reached ();
1381 ret = GST_FLOW_ERROR;
1382 g_assert_not_reached ();
1390 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1392 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1393 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1394 GstV4l2Object *obj = pool->obj;
1396 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1398 switch (obj->type) {
1399 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1400 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1401 /* capture, put the buffer back in the queue so that we can refill it
1403 switch (obj->mode) {
1404 case GST_V4L2_IO_RW:
1405 /* release back in the pool */
1406 pclass->release_buffer (bpool, buffer);
1409 case GST_V4L2_IO_DMABUF:
1410 case GST_V4L2_IO_MMAP:
1411 case GST_V4L2_IO_USERPTR:
1412 case GST_V4L2_IO_DMABUF_IMPORT:
1414 GstV4l2MemoryGroup *group;
1415 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1416 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1417 /* queue back in the device */
1418 if (pool->other_pool)
1419 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1420 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1421 pclass->release_buffer (bpool, buffer);
1423 /* Simply release invalide/modified buffer, the allocator will
1424 * give it back later */
1425 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1426 pclass->release_buffer (bpool, buffer);
1431 g_assert_not_reached ();
1436 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1437 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1438 switch (obj->mode) {
1439 case GST_V4L2_IO_RW:
1440 /* release back in the pool */
1441 pclass->release_buffer (bpool, buffer);
1444 case GST_V4L2_IO_MMAP:
1445 case GST_V4L2_IO_DMABUF:
1446 case GST_V4L2_IO_USERPTR:
1447 case GST_V4L2_IO_DMABUF_IMPORT:
1449 GstV4l2MemoryGroup *group;
1452 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1453 /* Simply release invalide/modified buffer, the allocator will
1454 * give it back later */
1455 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1456 pclass->release_buffer (bpool, buffer);
1460 index = group->buffer.index;
1462 if (pool->buffers[index] == NULL) {
1463 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1466 /* Remove qdata, this will unmap any map data in userptr */
1467 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1468 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1470 /* reset to default size */
1471 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1473 /* playback, put the buffer back in the queue to refill later. */
1474 pclass->release_buffer (bpool, buffer);
1476 /* the buffer is queued in the device but maybe not played yet. We just
1477 * leave it there and not make it available for future calls to acquire
1478 * for now. The buffer will be dequeued and reused later. */
1479 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1485 g_assert_not_reached ();
1491 g_assert_not_reached ();
1497 gst_v4l2_buffer_pool_dispose (GObject * object)
1499 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1501 if (pool->vallocator)
1502 gst_object_unref (pool->vallocator);
1503 pool->vallocator = NULL;
1505 if (pool->allocator)
1506 gst_object_unref (pool->allocator);
1507 pool->allocator = NULL;
1509 if (pool->other_pool)
1510 gst_object_unref (pool->other_pool);
1511 pool->other_pool = NULL;
1513 G_OBJECT_CLASS (parent_class)->dispose (object);
1517 gst_v4l2_buffer_pool_finalize (GObject * object)
1519 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1521 if (pool->video_fd >= 0)
1522 pool->obj->close (pool->video_fd);
1524 gst_poll_free (pool->poll);
1526 /* This can't be done in dispose method because we must not set pointer
1527 * to NULL as it is part of the v4l2object and dispose could be called
1529 gst_object_unref (pool->obj->element);
1531 g_cond_clear (&pool->empty_cond);
1533 /* FIXME have we done enough here ? */
1535 G_OBJECT_CLASS (parent_class)->finalize (object);
1539 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1541 pool->poll = gst_poll_new (TRUE);
1542 pool->can_poll_device = TRUE;
1543 g_cond_init (&pool->empty_cond);
1548 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1550 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1551 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1553 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1554 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1556 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1557 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1558 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1559 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1560 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1561 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1562 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1563 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1565 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1566 "V4L2 Buffer Pool");
1567 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
1571 * gst_v4l2_buffer_pool_new:
1572 * @obj: the v4l2 object owning the pool
1574 * Construct a new buffer pool.
1576 * Returns: the new pool, use gst_object_unref() to free resources
1579 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1581 GstV4l2BufferPool *pool;
1582 GstStructure *config;
1583 gchar *name, *parent_name;
1586 fd = obj->dup (obj->video_fd);
1590 /* setting a significant unique name */
1591 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1592 name = g_strconcat (parent_name, ":", "pool:",
1593 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1594 g_free (parent_name);
1596 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1597 "name", name, NULL);
1598 g_object_ref_sink (pool);
1601 gst_poll_fd_init (&pool->pollfd);
1602 pool->pollfd.fd = fd;
1603 gst_poll_add_fd (pool->poll, &pool->pollfd);
1604 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1605 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1607 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1609 pool->video_fd = fd;
1611 pool->can_poll_device = TRUE;
1613 pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
1614 if (pool->vallocator == NULL)
1615 goto allocator_failed;
1617 gst_object_ref (obj->element);
1619 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1620 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1621 /* This will simply set a default config, but will not configure the pool
1622 * because min and max are not valid */
1623 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1625 return GST_BUFFER_POOL (pool);
1630 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1635 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1636 gst_object_unref (pool);
1641 static GstFlowReturn
1642 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1645 GstV4l2Object *obj = pool->obj;
1650 toread = obj->info.size;
1652 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1654 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1657 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1660 amount = obj->read (obj->video_fd, map.data, toread);
1662 if (amount == toread) {
1664 } else if (amount == -1) {
1665 if (errno == EAGAIN || errno == EINTR) {
1670 /* short reads can happen if a signal interrupts the read */
1675 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1676 gst_buffer_unmap (buf, &map);
1677 gst_buffer_resize (buf, 0, amount);
1684 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1689 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1690 (_("Error reading %d bytes from device '%s'."),
1691 toread, obj->videodev), GST_ERROR_SYSTEM);
1692 res = GST_FLOW_ERROR;
1697 gst_buffer_unmap (buf, &map);
1698 gst_buffer_resize (buf, 0, 0);
1704 * gst_v4l2_buffer_pool_process:
1705 * @bpool: a #GstBufferPool
1706 * @buf: a #GstBuffer, maybe be replaced
1708 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1709 * data from the device. For output devices, this functions send the contents of
1710 * @buf to the device for playback.
1712 * Returns: %GST_FLOW_OK on success.
1715 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1717 GstFlowReturn ret = GST_FLOW_OK;
1718 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1719 GstV4l2Object *obj = pool->obj;
1721 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1723 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1724 return GST_FLOW_FLUSHING;
1726 switch (obj->type) {
1727 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1728 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1730 switch (obj->mode) {
1731 case GST_V4L2_IO_RW:
1732 /* capture into the buffer */
1733 ret = gst_v4l2_do_read (pool, *buf);
1736 case GST_V4L2_IO_MMAP:
1737 case GST_V4L2_IO_DMABUF:
1741 if ((*buf)->pool == bpool) {
1743 gsize size = gst_buffer_get_size (*buf);
1746 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1747 goto buffer_corrupted;
1752 /* verify that buffer contains a full frame for raw video */
1753 if (GST_VIDEO_INFO_FORMAT (&obj->info) != GST_VIDEO_FORMAT_ENCODED
1754 && size < GST_VIDEO_INFO_SIZE (&obj->info)) {
1755 GST_WARNING_OBJECT (pool, "Invalid buffer size, this is likely "
1756 "due to a bug in your driver, dropping");
1757 goto buffer_corrupted;
1760 num_queued = g_atomic_int_get (&pool->num_queued);
1761 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1764 /* If we have no more buffer, and can allocate it time to do so */
1765 if (num_queued == 0) {
1766 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1767 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1768 if (ret == GST_FLOW_OK)
1773 /* start copying buffers when we are running low on buffers */
1774 if (num_queued < pool->copy_threshold) {
1777 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1778 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1779 if (ret == GST_FLOW_OK)
1783 /* copy the buffer */
1784 copy = gst_buffer_copy_region (*buf,
1785 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1786 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1788 /* and requeue so that we can continue capturing */
1789 gst_buffer_unref (*buf);
1794 /* nothing, data was inside the buffer when we did _acquire() */
1798 /* buffer not from our pool, grab a frame and copy it into the target */
1799 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1802 /* An empty buffer on capture indicates the end of stream */
1803 if (gst_buffer_get_size (tmp) == 0) {
1804 gboolean corrupted = GST_BUFFER_FLAG_IS_SET (tmp,
1805 GST_BUFFER_FLAG_CORRUPTED);
1807 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1810 goto buffer_corrupted;
1815 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1817 /* an queue the buffer again after the copy */
1818 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1820 if (ret != GST_FLOW_OK)
1825 case GST_V4L2_IO_USERPTR:
1827 struct UserPtrData *data;
1830 /* Replace our buffer with downstream allocated buffer */
1831 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1832 GST_V4L2_IMPORT_QUARK);
1833 tmp = gst_buffer_ref (data->buffer);
1834 _unmap_userptr_frame (data);
1836 /* Now tmp is writable, copy the flags and timestamp */
1837 gst_buffer_copy_into (tmp, *buf,
1838 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1840 gst_buffer_replace (buf, tmp);
1841 gst_buffer_unref (tmp);
1845 case GST_V4L2_IO_DMABUF_IMPORT:
1849 /* Replace our buffer with downstream allocated buffer */
1850 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1851 GST_V4L2_IMPORT_QUARK);
1853 gst_buffer_copy_into (tmp, *buf,
1854 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1856 gst_buffer_replace (buf, tmp);
1857 gst_buffer_unref (tmp);
1862 g_assert_not_reached ();
1867 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1868 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1870 switch (obj->mode) {
1871 case GST_V4L2_IO_RW:
1872 /* FIXME, do write() */
1873 GST_WARNING_OBJECT (pool, "implement write()");
1876 case GST_V4L2_IO_USERPTR:
1877 case GST_V4L2_IO_DMABUF_IMPORT:
1878 case GST_V4L2_IO_DMABUF:
1879 case GST_V4L2_IO_MMAP:
1881 GstBuffer *to_queue = NULL;
1882 GstV4l2MemoryGroup *group;
1885 if ((*buf)->pool != bpool)
1888 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1891 index = group->buffer.index;
1893 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1895 if (pool->buffers[index] != NULL) {
1896 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1900 /* we can queue directly */
1901 to_queue = gst_buffer_ref (*buf);
1904 if (to_queue == NULL) {
1905 GstBufferPoolAcquireParams params = { 0 };
1907 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1909 /* this can return EOS if all buffers are outstanding which would
1910 * be strange because we would expect the upstream element to have
1911 * allocated them and returned to us.. */
1912 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1913 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1914 if (ret != GST_FLOW_OK)
1915 goto acquire_failed;
1917 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1918 if (ret != GST_FLOW_OK) {
1919 gst_buffer_unref (to_queue);
1920 goto prepare_failed;
1924 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1927 /* if we are not streaming yet (this is the first buffer, start
1929 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1930 /* don't check return value because qbuf would have failed */
1931 gst_v4l2_is_buffer_valid (to_queue, &group);
1933 /* qbuf has stored to_queue buffer but we are not in
1934 * streaming state, so the flush logic won't be performed.
1935 * To avoid leaks, flush the allocator and restore the queued
1936 * buffer as non-queued */
1937 gst_v4l2_allocator_flush (pool->vallocator);
1939 pool->buffers[group->buffer.index] = NULL;
1941 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1942 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1943 gst_buffer_unref (to_queue);
1944 g_atomic_int_add (&pool->num_queued, -1);
1948 /* Remove our ref, we will still hold this buffer in acquire as needed,
1949 * otherwise the pool will think it is outstanding and will refuse to stop. */
1950 gst_buffer_unref (to_queue);
1952 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1954 /* all buffers are queued, try to dequeue one and release it back
1955 * into the pool so that _acquire can get to it again. */
1956 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1957 if (ret == GST_FLOW_OK && out->pool == NULL)
1958 /* release the rendered buffer back into the pool. This wakes up any
1959 * thread waiting for a buffer in _acquire(). */
1960 gst_v4l2_buffer_pool_release_buffer (bpool, out);
1965 g_assert_not_reached ();
1970 g_assert_not_reached ();
1979 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1984 GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1985 gst_buffer_unref (*buf);
1987 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1991 GST_DEBUG_OBJECT (pool, "end of stream reached");
1992 gst_buffer_unref (*buf);
1994 return GST_V4L2_FLOW_LAST_BUFFER;
1998 if (ret == GST_FLOW_FLUSHING)
1999 GST_DEBUG_OBJECT (pool, "flushing");
2001 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
2002 gst_flow_get_name (ret));
2007 GST_ERROR_OBJECT (pool, "failed to prepare data");
2012 GST_ERROR_OBJECT (pool, "failed to queue buffer");
2017 GST_ERROR_OBJECT (pool, "failed to start streaming");
2018 return GST_FLOW_ERROR;
2023 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
2024 GstBufferPool * other_pool)
2026 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
2028 if (pool->other_pool)
2029 gst_object_unref (pool->other_pool);
2030 pool->other_pool = gst_object_ref (other_pool);
2034 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
2036 GST_OBJECT_LOCK (pool);
2037 pool->enable_copy_threshold = copy;
2038 GST_OBJECT_UNLOCK (pool);