3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
5 * 2009 Texas Instruments, Inc - http://www.ti.com/
7 * gstv4l2bufferpool.c V4L2 buffer pool class
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
30 # define _GNU_SOURCE /* O_CLOEXEC */
38 #include "gst/video/video.h"
39 #include "gst/video/gstvideometa.h"
40 #include "gst/video/gstvideopool.h"
41 #include "gst/allocators/gstdmabuf.h"
43 #include <gstv4l2bufferpool.h>
45 #include "v4l2_calls.h"
46 #include "gst/gst-i18n-plugin.h"
47 #include <gst/glib-compat-private.h>
49 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
50 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2bufferpool_debug
53 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
59 #define gst_v4l2_buffer_pool_parent_class parent_class
60 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
62 enum _GstV4l2BufferPoolAcquireFlags
64 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
65 GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
66 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
69 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
73 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
75 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
76 gboolean valid = FALSE;
78 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
81 if (gst_is_dmabuf_memory (mem))
82 mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
83 GST_V4L2_MEMORY_QUARK);
85 if (mem && gst_is_v4l2_memory (mem)) {
86 GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
87 GstV4l2MemoryGroup *group = vmem->group;
90 if (group->n_mem != gst_buffer_n_memory (buffer))
93 for (i = 0; i < group->n_mem; i++) {
94 if (group->mem[i] != gst_buffer_peek_memory (buffer, i))
97 if (!gst_memory_is_writable (group->mem[i]))
111 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
114 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
116 GST_LOG_OBJECT (pool, "copying buffer");
118 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
119 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
120 GstVideoFrame src_frame, dest_frame;
122 GST_DEBUG_OBJECT (pool, "copy video frame");
124 /* we have raw video, use videoframe copy to get strides right */
125 if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
128 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
130 gst_video_frame_unmap (&src_frame);
134 gst_video_frame_copy (&dest_frame, &src_frame);
136 gst_video_frame_unmap (&src_frame);
137 gst_video_frame_unmap (&dest_frame);
141 GST_DEBUG_OBJECT (pool, "copy raw bytes");
143 if (!gst_buffer_map (src, &map, GST_MAP_READ))
146 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
148 gst_buffer_unmap (src, &map);
149 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
152 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
159 GST_ERROR_OBJECT (pool, "could not map buffer");
160 return GST_FLOW_ERROR;
173 gst_v4l2_buffer_pool_import_quark (void)
175 static GQuark quark = 0;
178 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
184 _unmap_userptr_frame (struct UserPtrData *data)
187 gst_video_frame_unmap (&data->frame);
189 gst_buffer_unmap (data->buffer, &data->map);
192 gst_buffer_unref (data->buffer);
194 g_slice_free (struct UserPtrData, data);
198 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
199 GstBuffer * dest, GstBuffer * src)
201 GstFlowReturn ret = GST_FLOW_OK;
202 GstV4l2MemoryGroup *group = NULL;
204 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
205 struct UserPtrData *data = NULL;
207 GST_LOG_OBJECT (pool, "importing userptr");
210 if (!gst_v4l2_is_buffer_valid (dest, &group))
213 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
214 flags = GST_MAP_READ;
216 flags = GST_MAP_WRITE;
218 data = g_slice_new0 (struct UserPtrData);
220 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
221 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
222 gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
225 data->is_frame = TRUE;
227 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
230 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
231 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
232 gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
236 pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
237 GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
239 pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
240 GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
242 size[i] = pstride * pheight;
244 size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
245 GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
249 /* In the single planar API, planes must be contiguous in memory and
250 * therefore they must have expected size. ie: no padding.
251 * To check these conditions, we check that plane 'i' start address
252 * + plane 'i' size equals to plane 'i+1' start address */
253 if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
254 for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
255 const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
257 gint estride = gst_v4l2_object_extrapolate_stride (finfo, i,
258 pix_fmt->bytesperline);
259 guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
262 tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
263 if (tmp != data->frame.data[i + 1])
264 goto non_contiguous_mem;
268 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
269 data->frame.info.size, finfo->n_planes, data->frame.data, size))
275 data->is_frame = FALSE;
277 if (!gst_buffer_map (src, &data->map, flags))
280 ptr[0] = data->map.data;
281 size[0] = data->map.size;
283 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
284 data->map.size, 1, ptr, size))
288 data->buffer = gst_buffer_ref (src);
290 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
291 data, (GDestroyNotify) _unmap_userptr_frame);
297 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
298 return GST_FLOW_ERROR;
302 GST_ERROR_OBJECT (pool, "could not map buffer");
303 g_slice_free (struct UserPtrData, data);
304 return GST_FLOW_ERROR;
308 GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
309 _unmap_userptr_frame (data);
310 return GST_FLOW_ERROR;
314 GST_ERROR_OBJECT (pool, "failed to import data");
315 _unmap_userptr_frame (data);
316 return GST_FLOW_ERROR;
321 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
322 GstBuffer * dest, GstBuffer * src)
324 GstV4l2MemoryGroup *group = NULL;
325 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
326 guint n_mem = gst_buffer_n_memory (src);
329 GST_LOG_OBJECT (pool, "importing dmabuf");
331 if (!gst_v4l2_is_buffer_valid (dest, &group))
334 if (n_mem > GST_VIDEO_MAX_PLANES)
337 for (i = 0; i < n_mem; i++)
338 dma_mem[i] = gst_buffer_peek_memory (src, i);
340 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
344 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
345 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
351 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
352 return GST_FLOW_ERROR;
356 GST_ERROR_OBJECT (pool, "could not map buffer");
357 return GST_FLOW_ERROR;
361 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
362 return GST_FLOW_ERROR;
367 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
368 GstBuffer * dest, GstBuffer * src)
370 GstFlowReturn ret = GST_FLOW_OK;
371 gboolean own_src = FALSE;
374 if (pool->other_pool == NULL) {
375 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
376 return GST_FLOW_ERROR;
379 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
380 if (ret != GST_FLOW_OK) {
381 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
388 switch (pool->obj->mode) {
389 case GST_V4L2_IO_MMAP:
390 case GST_V4L2_IO_DMABUF:
391 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
393 case GST_V4L2_IO_USERPTR:
394 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
396 case GST_V4L2_IO_DMABUF_IMPORT:
397 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
404 gst_buffer_unref (src);
411 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
412 GstBufferPoolAcquireParams * params)
414 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
415 GstV4l2MemoryGroup *group = NULL;
416 GstBuffer *newbuf = NULL;
426 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
428 case GST_V4L2_IO_MMAP:
429 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
431 case GST_V4L2_IO_DMABUF:
432 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
435 case GST_V4L2_IO_USERPTR:
436 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
438 case GST_V4L2_IO_DMABUF_IMPORT:
439 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
443 g_assert_not_reached ();
449 newbuf = gst_buffer_new ();
451 for (i = 0; i < group->n_mem; i++)
452 gst_buffer_append_memory (newbuf, group->mem[i]);
453 } else if (newbuf == NULL) {
454 goto allocation_failed;
457 /* add metadata to raw video buffers */
458 if (pool->add_videometa)
459 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
460 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
461 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
462 info->offset, info->stride);
471 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
472 return GST_FLOW_ERROR;
477 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
479 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
480 GstV4l2Object *obj = pool->obj;
482 guint size, min_buffers, max_buffers;
483 GstAllocator *allocator;
484 GstAllocationParams params;
485 gboolean can_allocate = FALSE;
486 gboolean updated = FALSE;
489 pool->add_videometa =
490 gst_buffer_pool_config_has_option (config,
491 GST_BUFFER_POOL_OPTION_VIDEO_META);
493 /* parse the config and keep around */
494 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
498 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
501 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
504 gst_object_unref (pool->allocator);
505 pool->allocator = NULL;
508 case GST_V4L2_IO_DMABUF:
509 pool->allocator = gst_dmabuf_allocator_new ();
510 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
512 case GST_V4L2_IO_MMAP:
513 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
515 case GST_V4L2_IO_USERPTR:
517 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
519 case GST_V4L2_IO_DMABUF_IMPORT:
520 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
524 pool->allocator = g_object_ref (allocator);
525 pool->params = params;
526 /* No need to change the configuration */
530 g_assert_not_reached ();
534 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
535 * instability and crash, disable it for now */
536 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
537 GST_WARNING_OBJECT (pool,
538 "libv4l2 converter detected, disabling CREATE_BUFS");
539 can_allocate = FALSE;
540 GST_OBJECT_FLAG_UNSET (pool->vallocator,
541 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
542 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
543 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
546 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
548 min_buffers = GST_V4L2_MIN_BUFFERS;
549 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
552 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
554 max_buffers = VIDEO_MAX_FRAME;
555 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
558 if (min_buffers > max_buffers) {
560 min_buffers = max_buffers;
561 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
562 } else if (min_buffers != max_buffers) {
565 max_buffers = min_buffers;
566 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
570 if (!pool->add_videometa && obj->need_video_meta) {
571 GST_INFO_OBJECT (pool, "adding needed video meta");
573 gst_buffer_pool_config_add_option (config,
574 GST_BUFFER_POOL_OPTION_VIDEO_META);
577 /* Always update the config to ensure the configured size matches */
578 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
581 /* keep a GstVideoInfo with defaults for the when we need to copy */
582 gst_video_info_from_caps (&pool->caps_info, caps);
585 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
587 /* If anything was changed documentation recommand to return FALSE */
588 return !updated && ret;
593 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
599 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
601 GstV4l2Object *obj = pool->obj;
604 case GST_V4L2_IO_MMAP:
605 case GST_V4L2_IO_USERPTR:
606 case GST_V4L2_IO_DMABUF:
607 case GST_V4L2_IO_DMABUF_IMPORT:
608 if (!pool->streaming) {
609 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
610 goto streamon_failed;
612 pool->streaming = TRUE;
614 GST_DEBUG_OBJECT (pool, "Started streaming");
625 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
632 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
634 GstV4l2Object *obj = pool->obj;
637 case GST_V4L2_IO_MMAP:
638 case GST_V4L2_IO_USERPTR:
639 case GST_V4L2_IO_DMABUF:
640 case GST_V4L2_IO_DMABUF_IMPORT:
641 if (pool->streaming) {
642 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
643 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
644 errno, g_strerror (errno));
646 pool->streaming = FALSE;
648 GST_DEBUG_OBJECT (pool, "Stopped streaming");
650 if (pool->vallocator)
651 gst_v4l2_allocator_flush (pool->vallocator);
660 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
662 GstBufferPoolAcquireParams params = { 0 };
663 GstBuffer *buffer = NULL;
666 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
669 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
671 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
673 if (ret == GST_FLOW_OK)
674 gst_buffer_unref (buffer);
680 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
682 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
683 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
684 GstV4l2Object *obj = pool->obj;
685 GstStructure *config;
687 guint size, min_buffers, max_buffers;
688 guint max_latency, min_latency, copy_threshold = 0;
689 gboolean can_allocate = FALSE;
691 GST_DEBUG_OBJECT (pool, "activating pool");
693 config = gst_buffer_pool_get_config (bpool);
694 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
698 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
704 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
705 * of MMAP. In this case, the first read initialize the queues, but the
706 * poll before that will always fail. Doing an empty read, forces the
707 * queue to be initialized now. We only do this if we have a streaming
709 if (obj->vcap.capabilities & V4L2_CAP_STREAMING)
710 v4l2_read (obj->video_fd, NULL, 0);
713 case GST_V4L2_IO_DMABUF:
714 case GST_V4L2_IO_MMAP:
718 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
720 /* first, lets request buffers, and see how many we can get: */
721 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
723 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
726 if (count < GST_V4L2_MIN_BUFFERS) {
731 /* V4L2 buffer pool are often very limited in the amount of buffers it
732 * can offer. The copy_threshold will workaround this limitation by
733 * falling back to copy if the pipeline needed more buffers. This also
734 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
736 if (count != min_buffers || pool->enable_copy_threshold) {
737 GST_WARNING_OBJECT (pool,
738 "Uncertain or not enough buffers, enabling copy threshold");
740 copy_threshold = min_latency;
745 case GST_V4L2_IO_USERPTR:
750 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
752 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
754 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
755 V4L2_MEMORY_USERPTR);
757 /* There is no rational to not get what we asked */
758 if (count < min_buffers) {
766 case GST_V4L2_IO_DMABUF_IMPORT:
770 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
772 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
774 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
777 /* There is no rational to not get what we asked */
778 if (count < min_buffers) {
789 g_assert_not_reached ();
794 max_latency = max_buffers;
796 max_latency = min_buffers;
799 pool->copy_threshold = copy_threshold;
800 pool->max_latency = max_latency;
801 pool->min_latency = min_latency;
802 pool->num_queued = 0;
804 if (max_buffers != 0 && max_buffers < min_buffers)
805 max_buffers = min_buffers;
807 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
809 pclass->set_config (bpool, config);
810 gst_structure_free (config);
812 if (pool->other_pool)
813 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
814 goto other_pool_failed;
816 /* now, allocate the buffers: */
817 if (!pclass->start (bpool))
820 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
821 pool->group_released_handler =
822 g_signal_connect_swapped (pool->vallocator, "group-released",
823 G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
830 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
831 gst_structure_free (config);
836 GST_ERROR_OBJECT (pool,
837 "we received %d buffer from device '%s', we want at least %d",
838 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
839 gst_structure_free (config);
844 GST_ERROR_OBJECT (pool, "failed to start streaming");
849 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
850 GST_PTR_FORMAT, pool->other_pool);
856 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
858 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
859 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
863 GST_DEBUG_OBJECT (pool, "stopping pool");
865 if (pool->group_released_handler > 0) {
866 g_signal_handler_disconnect (pool->vallocator,
867 pool->group_released_handler);
868 pool->group_released_handler = 0;
871 if (pool->other_pool) {
872 gst_buffer_pool_set_active (pool->other_pool, FALSE);
873 gst_object_unref (pool->other_pool);
874 pool->other_pool = NULL;
877 gst_v4l2_buffer_pool_streamoff (pool);
879 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
880 if (pool->buffers[i]) {
881 GstBuffer *buffer = pool->buffers[i];
883 pool->buffers[i] = NULL;
885 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
886 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
887 else /* Don't re-enqueue capture buffer on stop */
888 pclass->release_buffer (bpool, buffer);
890 g_atomic_int_add (&pool->num_queued, -1);
894 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
896 if (ret && pool->vallocator) {
899 vret = gst_v4l2_allocator_stop (pool->vallocator);
901 if (vret == GST_V4L2_BUSY)
902 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
904 ret = (vret == GST_V4L2_OK);
911 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
913 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
915 GST_DEBUG_OBJECT (pool, "start flushing");
917 gst_poll_set_flushing (pool->poll, TRUE);
919 GST_OBJECT_LOCK (pool);
921 g_cond_broadcast (&pool->empty_cond);
922 GST_OBJECT_UNLOCK (pool);
924 if (pool->other_pool)
925 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
929 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
931 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
932 GstV4l2Object *obj = pool->obj;
935 GST_DEBUG_OBJECT (pool, "stop flushing");
937 /* If we haven't started streaming yet, simply call streamon */
938 if (!pool->streaming)
941 if (pool->other_pool)
942 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
944 gst_v4l2_buffer_pool_streamoff (pool);
946 /* Reset our state */
950 case GST_V4L2_IO_MMAP:
951 case GST_V4L2_IO_USERPTR:
952 case GST_V4L2_IO_DMABUF:
953 case GST_V4L2_IO_DMABUF_IMPORT:
955 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
956 /* Re-enqueue buffers */
957 if (pool->buffers[i]) {
958 GstBufferPool *bpool = (GstBufferPool *) pool;
959 GstBuffer *buffer = pool->buffers[i];
961 pool->buffers[i] = NULL;
963 /* Remove qdata, this will unmap any map data in
964 * userptr/dmabuf-import */
965 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
966 GST_V4L2_IMPORT_QUARK, NULL, NULL);
968 if (buffer->pool == NULL)
969 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
971 g_atomic_int_add (&pool->num_queued, -1);
978 g_assert_not_reached ();
983 /* Start streaming on capture device only */
984 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
985 gst_v4l2_buffer_pool_streamon (pool);
987 gst_poll_set_flushing (pool->poll, FALSE);
991 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
995 /* In RW mode there is no queue, hence no need to wait while the queue is
997 if (pool->obj->mode != GST_V4L2_IO_RW) {
998 GST_OBJECT_LOCK (pool);
1000 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1001 GST_OBJECT_UNLOCK (pool);
1004 if (!pool->can_poll_device)
1007 GST_LOG_OBJECT (pool, "polling device");
1010 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
1011 if (G_UNLIKELY (ret < 0)) {
1019 GST_WARNING_OBJECT (pool,
1020 "v4l2 device doesn't support polling. Disabling"
1021 " using libv4l2 in this case may cause deadlocks");
1022 pool->can_poll_device = FALSE;
1029 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1038 GST_DEBUG_OBJECT (pool, "stop called");
1039 return GST_FLOW_FLUSHING;
1043 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1044 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1045 return GST_FLOW_ERROR;
1049 static GstFlowReturn
1050 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1052 GstV4l2MemoryGroup *group = NULL;
1053 const GstV4l2Object *obj = pool->obj;
1056 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1057 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1058 gst_buffer_unref (buf);
1062 index = group->buffer.index;
1064 if (pool->buffers[index] != NULL)
1065 goto already_queued;
1067 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1069 g_atomic_int_inc (&pool->num_queued);
1070 pool->buffers[index] = buf;
1072 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1073 enum v4l2_field field;
1075 /* Except when field is set to alternate, buffer field is the same as
1076 * the one defined in format */
1077 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1078 field = obj->format.fmt.pix_mp.field;
1080 field = obj->format.fmt.pix.field;
1082 /* NB: At this moment, we can't have alternate mode because it not handled
1084 if (field == V4L2_FIELD_ALTERNATE) {
1085 if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1086 field = V4L2_FIELD_TOP;
1088 field = V4L2_FIELD_BOTTOM;
1091 group->buffer.field = field;
1094 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1097 GST_OBJECT_LOCK (pool);
1098 pool->empty = FALSE;
1099 g_cond_signal (&pool->empty_cond);
1100 GST_OBJECT_UNLOCK (pool);
1106 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1107 return GST_FLOW_ERROR;
1111 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1112 /* Mark broken buffer to the allocator */
1113 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1114 g_atomic_int_add (&pool->num_queued, -1);
1115 pool->buffers[index] = NULL;
1116 return GST_FLOW_ERROR;
1120 static GstFlowReturn
1121 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1125 GstV4l2Object *obj = pool->obj;
1126 GstClockTime timestamp;
1127 GstV4l2MemoryGroup *group;
1130 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1133 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1135 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1139 /* get our GstBuffer with that index from the pool, if the buffer was
1140 * outstanding we have a serious problem.
1142 outbuf = pool->buffers[group->buffer.index];
1146 /* mark the buffer outstanding */
1147 pool->buffers[group->buffer.index] = NULL;
1148 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1149 GST_OBJECT_LOCK (pool);
1151 GST_OBJECT_UNLOCK (pool);
1154 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1156 #ifndef GST_DISABLE_GST_DEBUG
1157 for (i = 0; i < group->n_mem; i++) {
1158 GST_LOG_OBJECT (pool,
1159 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1160 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1161 group->buffer.sequence, group->buffer.index, group->mem[i],
1162 group->planes[i].bytesused, i, group->buffer.flags,
1163 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1167 /* set top/bottom field first if v4l2_buffer has the information */
1168 switch (group->buffer.field) {
1169 case V4L2_FIELD_NONE:
1170 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1171 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1173 case V4L2_FIELD_INTERLACED_TB:
1174 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1175 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1177 case V4L2_FIELD_INTERLACED_BT:
1178 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1179 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1181 case V4L2_FIELD_INTERLACED:
1182 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1183 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1184 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1185 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1186 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1188 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1192 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1193 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1194 GST_FIXME_OBJECT (pool,
1195 "Unhandled enum v4l2_field %d - treating as progressive",
1196 group->buffer.field);
1199 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1200 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1201 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1203 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1206 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1207 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1209 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1218 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1223 return GST_FLOW_ERROR;
1227 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1228 group->buffer.index);
1229 return GST_FLOW_ERROR;
1233 static GstFlowReturn
1234 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1235 GstBufferPoolAcquireParams * params)
1238 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1239 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1240 GstV4l2Object *obj = pool->obj;
1242 GST_DEBUG_OBJECT (pool, "acquire");
1244 /* If this is being called to resurect a lost buffer */
1245 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1246 ret = pclass->acquire_buffer (bpool, buffer, params);
1250 switch (obj->type) {
1251 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1252 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1253 /* capture, This function should return a buffer with new captured data */
1254 switch (obj->mode) {
1255 case GST_V4L2_IO_RW:
1257 /* take empty buffer from the pool */
1258 ret = pclass->acquire_buffer (bpool, buffer, params);
1261 case GST_V4L2_IO_DMABUF:
1262 case GST_V4L2_IO_MMAP:
1263 case GST_V4L2_IO_USERPTR:
1264 case GST_V4L2_IO_DMABUF_IMPORT:
1266 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1267 * storage for our buffers. This function does poll first so we can
1268 * interrupt it fine. */
1269 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1273 ret = GST_FLOW_ERROR;
1274 g_assert_not_reached ();
1280 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1281 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1282 /* playback, This function should return an empty buffer */
1283 switch (obj->mode) {
1284 case GST_V4L2_IO_RW:
1285 /* get an empty buffer */
1286 ret = pclass->acquire_buffer (bpool, buffer, params);
1289 case GST_V4L2_IO_MMAP:
1290 case GST_V4L2_IO_DMABUF:
1291 case GST_V4L2_IO_USERPTR:
1292 case GST_V4L2_IO_DMABUF_IMPORT:
1293 /* get a free unqueued buffer */
1294 ret = pclass->acquire_buffer (bpool, buffer, params);
1298 ret = GST_FLOW_ERROR;
1299 g_assert_not_reached ();
1305 ret = GST_FLOW_ERROR;
1306 g_assert_not_reached ();
1314 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1316 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1317 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1318 GstV4l2Object *obj = pool->obj;
1320 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1322 switch (obj->type) {
1323 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1324 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1325 /* capture, put the buffer back in the queue so that we can refill it
1327 switch (obj->mode) {
1328 case GST_V4L2_IO_RW:
1329 /* release back in the pool */
1330 pclass->release_buffer (bpool, buffer);
1333 case GST_V4L2_IO_DMABUF:
1334 case GST_V4L2_IO_MMAP:
1335 case GST_V4L2_IO_USERPTR:
1336 case GST_V4L2_IO_DMABUF_IMPORT:
1338 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1339 /* queue back in the device */
1340 if (pool->other_pool)
1341 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1342 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1343 pclass->release_buffer (bpool, buffer);
1345 /* Simply release invalide/modified buffer, the allocator will
1346 * give it back later */
1347 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1348 pclass->release_buffer (bpool, buffer);
1353 g_assert_not_reached ();
1358 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1359 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1360 switch (obj->mode) {
1361 case GST_V4L2_IO_RW:
1362 /* release back in the pool */
1363 pclass->release_buffer (bpool, buffer);
1366 case GST_V4L2_IO_MMAP:
1367 case GST_V4L2_IO_DMABUF:
1368 case GST_V4L2_IO_USERPTR:
1369 case GST_V4L2_IO_DMABUF_IMPORT:
1371 GstV4l2MemoryGroup *group;
1374 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1375 /* Simply release invalide/modified buffer, the allocator will
1376 * give it back later */
1377 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1378 pclass->release_buffer (bpool, buffer);
1382 index = group->buffer.index;
1384 if (pool->buffers[index] == NULL) {
1385 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1388 /* Remove qdata, this will unmap any map data in userptr */
1389 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1390 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1392 /* reset to default size */
1393 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1395 /* playback, put the buffer back in the queue to refill later. */
1396 pclass->release_buffer (bpool, buffer);
1398 /* the buffer is queued in the device but maybe not played yet. We just
1399 * leave it there and not make it available for future calls to acquire
1400 * for now. The buffer will be dequeued and reused later. */
1401 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1407 g_assert_not_reached ();
1413 g_assert_not_reached ();
1419 gst_v4l2_buffer_pool_dispose (GObject * object)
1421 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1423 if (pool->vallocator)
1424 gst_object_unref (pool->vallocator);
1425 pool->vallocator = NULL;
1427 if (pool->allocator)
1428 gst_object_unref (pool->allocator);
1429 pool->allocator = NULL;
1431 if (pool->other_pool)
1432 gst_object_unref (pool->other_pool);
1433 pool->other_pool = NULL;
1435 G_OBJECT_CLASS (parent_class)->dispose (object);
1439 gst_v4l2_buffer_pool_finalize (GObject * object)
1441 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1443 if (pool->video_fd >= 0)
1444 v4l2_close (pool->video_fd);
1446 gst_poll_free (pool->poll);
1448 /* FIXME Is this required to keep around ?
1449 * This can't be done in dispose method because we must not set pointer
1450 * to NULL as it is part of the v4l2object and dispose could be called
1452 gst_object_unref (pool->obj->element);
1454 g_cond_clear (&pool->empty_cond);
1456 /* FIXME have we done enough here ? */
1458 G_OBJECT_CLASS (parent_class)->finalize (object);
1462 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1464 pool->poll = gst_poll_new (TRUE);
1465 pool->can_poll_device = TRUE;
1466 g_cond_init (&pool->empty_cond);
1471 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1473 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1474 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1476 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1477 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1479 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1480 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1481 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1482 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1483 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1484 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1485 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1486 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1488 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1489 "V4L2 Buffer Pool");
1493 * gst_v4l2_buffer_pool_new:
1494 * @obj: the v4l2 object owning the pool
1496 * Construct a new buffer pool.
1498 * Returns: the new pool, use gst_object_unref() to free resources
1501 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1503 GstV4l2BufferPool *pool;
1504 GstStructure *config;
1505 gchar *name, *parent_name;
1508 fd = v4l2_dup (obj->video_fd);
1512 /* setting a significant unique name */
1513 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1514 name = g_strconcat (parent_name, ":", "pool:",
1515 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1516 g_free (parent_name);
1518 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1519 "name", name, NULL);
1522 gst_poll_fd_init (&pool->pollfd);
1523 pool->pollfd.fd = fd;
1524 gst_poll_add_fd (pool->poll, &pool->pollfd);
1525 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1526 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1528 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1530 pool->video_fd = fd;
1532 pool->can_poll_device = TRUE;
1535 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1536 if (pool->vallocator == NULL)
1537 goto allocator_failed;
1539 gst_object_ref (obj->element);
1541 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1542 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1543 /* This will simply set a default config, but will not configure the pool
1544 * because min and max are not valid */
1545 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1547 return GST_BUFFER_POOL (pool);
1552 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1557 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1558 gst_object_unref (pool);
1563 static GstFlowReturn
1564 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1567 GstV4l2Object *obj = pool->obj;
1572 toread = obj->info.size;
1574 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1576 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1579 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1582 amount = v4l2_read (obj->video_fd, map.data, toread);
1584 if (amount == toread) {
1586 } else if (amount == -1) {
1587 if (errno == EAGAIN || errno == EINTR) {
1592 /* short reads can happen if a signal interrupts the read */
1597 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1598 gst_buffer_unmap (buf, &map);
1599 gst_buffer_resize (buf, 0, amount);
1606 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1611 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1612 (_("Error reading %d bytes from device '%s'."),
1613 toread, obj->videodev), GST_ERROR_SYSTEM);
1614 res = GST_FLOW_ERROR;
1619 gst_buffer_unmap (buf, &map);
1620 gst_buffer_resize (buf, 0, 0);
1626 * gst_v4l2_buffer_pool_process:
1627 * @bpool: a #GstBufferPool
1628 * @buf: a #GstBuffer, maybe be replaced
1630 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1631 * data from the device. For output devices, this functions send the contents of
1632 * @buf to the device for playback.
1634 * Returns: %GST_FLOW_OK on success.
1637 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1639 GstFlowReturn ret = GST_FLOW_OK;
1640 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1641 GstV4l2Object *obj = pool->obj;
1643 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1645 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1647 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1648 return GST_FLOW_FLUSHING;
1650 switch (obj->type) {
1651 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1652 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1654 switch (obj->mode) {
1655 case GST_V4L2_IO_RW:
1656 /* capture into the buffer */
1657 ret = gst_v4l2_do_read (pool, *buf);
1660 case GST_V4L2_IO_MMAP:
1661 case GST_V4L2_IO_DMABUF:
1665 if ((*buf)->pool == bpool) {
1668 if (gst_buffer_get_size (*buf) == 0) {
1669 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1670 goto buffer_corrupted;
1675 num_queued = g_atomic_int_get (&pool->num_queued);
1676 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1679 /* If we have no more buffer, and can allocate it time to do so */
1680 if (num_queued == 0) {
1681 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1682 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1683 if (ret == GST_FLOW_OK)
1688 /* start copying buffers when we are running low on buffers */
1689 if (num_queued < pool->copy_threshold) {
1692 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1693 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1694 if (ret == GST_FLOW_OK)
1698 /* copy the buffer */
1699 copy = gst_buffer_copy_region (*buf,
1700 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1701 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1703 /* and requeue so that we can continue capturing */
1704 gst_buffer_unref (*buf);
1708 /* nothing, data was inside the buffer when we did _acquire() */
1712 /* buffer not from our pool, grab a frame and copy it into the target */
1713 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1716 /* An empty buffer on capture indicates the end of stream */
1717 if (gst_buffer_get_size (tmp) == 0) {
1718 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1720 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1721 goto buffer_corrupted;
1726 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1728 /* an queue the buffer again after the copy */
1729 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1731 if (ret != GST_FLOW_OK)
1736 case GST_V4L2_IO_USERPTR:
1738 struct UserPtrData *data;
1740 /* Replace our buffer with downstream allocated buffer */
1741 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1742 GST_V4L2_IMPORT_QUARK);
1743 gst_buffer_replace (buf, data->buffer);
1744 _unmap_userptr_frame (data);
1748 case GST_V4L2_IO_DMABUF_IMPORT:
1752 /* Replace our buffer with downstream allocated buffer */
1753 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1754 GST_V4L2_IMPORT_QUARK);
1755 gst_buffer_replace (buf, tmp);
1756 gst_buffer_unref (tmp);
1761 g_assert_not_reached ();
1766 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1767 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1769 switch (obj->mode) {
1770 case GST_V4L2_IO_RW:
1771 /* FIXME, do write() */
1772 GST_WARNING_OBJECT (pool, "implement write()");
1775 case GST_V4L2_IO_USERPTR:
1776 case GST_V4L2_IO_DMABUF_IMPORT:
1777 case GST_V4L2_IO_DMABUF:
1778 case GST_V4L2_IO_MMAP:
1780 GstBuffer *to_queue = NULL;
1781 GstV4l2MemoryGroup *group;
1784 if ((*buf)->pool != bpool)
1787 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1790 index = group->buffer.index;
1792 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1794 index = group->buffer.index;
1795 if (pool->buffers[index] != NULL) {
1796 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1800 /* we can queue directly */
1801 to_queue = gst_buffer_ref (*buf);
1804 if (to_queue == NULL) {
1805 GstBufferPoolAcquireParams params = { 0 };
1807 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1809 /* this can return EOS if all buffers are outstanding which would
1810 * be strange because we would expect the upstream element to have
1811 * allocated them and returned to us.. */
1812 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1813 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1814 if (ret != GST_FLOW_OK)
1815 goto acquire_failed;
1817 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1818 if (ret != GST_FLOW_OK) {
1819 gst_buffer_unref (to_queue);
1820 goto prepare_failed;
1824 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1827 /* if we are not streaming yet (this is the first buffer, start
1829 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1830 /* don't check return value because qbuf would have failed */
1831 gst_v4l2_is_buffer_valid (to_queue, &group);
1833 /* qbuf has stored to_queue buffer but we are not in
1834 * streaming state, so the flush logic won't be performed.
1835 * To avoid leaks, flush the allocator and restore the queued
1836 * buffer as non-queued */
1837 gst_v4l2_allocator_flush (pool->vallocator);
1839 pool->buffers[group->buffer.index] = NULL;
1841 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1842 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1843 gst_buffer_unref (to_queue);
1844 g_atomic_int_add (&pool->num_queued, -1);
1848 /* Remove our ref, we will still hold this buffer in acquire as needed,
1849 * otherwise the pool will think it is outstanding and will refuse to stop. */
1850 gst_buffer_unref (to_queue);
1852 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1854 /* all buffers are queued, try to dequeue one and release it back
1855 * into the pool so that _acquire can get to it again. */
1856 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1857 if (ret == GST_FLOW_OK && out->pool == NULL)
1858 /* release the rendered buffer back into the pool. This wakes up any
1859 * thread waiting for a buffer in _acquire(). */
1860 gst_v4l2_buffer_pool_release_buffer (bpool, out);
1865 g_assert_not_reached ();
1870 g_assert_not_reached ();
1879 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1884 GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1885 gst_buffer_unref (*buf);
1887 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1891 GST_DEBUG_OBJECT (pool, "end of stream reached");
1892 gst_buffer_unref (*buf);
1894 return GST_V4L2_FLOW_LAST_BUFFER;
1898 if (ret == GST_FLOW_FLUSHING)
1899 GST_DEBUG_OBJECT (pool, "flushing");
1901 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1902 gst_flow_get_name (ret));
1907 GST_ERROR_OBJECT (pool, "failed to prepare data");
1912 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1917 GST_ERROR_OBJECT (pool, "failed to start streaming");
1918 return GST_FLOW_ERROR;
1923 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1924 GstBufferPool * other_pool)
1926 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1928 if (pool->other_pool)
1929 gst_object_unref (pool->other_pool);
1930 pool->other_pool = gst_object_ref (other_pool);
1934 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
1936 GST_OBJECT_LOCK (pool);
1937 pool->enable_copy_threshold = copy;
1938 GST_OBJECT_UNLOCK (pool);