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 <glib/gi18n-lib.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 /* Buffer state flags */
70 enum _GstV4l2BufferState
72 /* Buffer is free (either on the GstBufferPool free queue, or no GstBuffer has
73 * been allocated yet) */
74 BUFFER_STATE_FREE = 0,
76 /* Buffer had outstanding external users */
77 BUFFER_STATE_OUTSTANDING = 1,
79 /* Buffer is on one of the kernel queues */
80 BUFFER_STATE_QUEUED = 2,
83 static void gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
84 GstBuffer * buffer, gboolean queued);
87 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
89 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
90 gboolean valid = FALSE;
92 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
95 if (gst_is_dmabuf_memory (mem))
96 mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
97 GST_V4L2_MEMORY_QUARK);
99 if (mem && gst_is_v4l2_memory (mem)) {
100 GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
101 GstV4l2MemoryGroup *group = vmem->group;
104 if (group->n_mem != gst_buffer_n_memory (buffer))
107 for (i = 0; i < group->n_mem; i++) {
108 if (group->mem[i] != gst_buffer_peek_memory (buffer, i))
111 if (!gst_memory_is_writable (group->mem[i]))
125 gst_v4l2_buffer_pool_resize_buffer (GstBufferPool * bpool, GstBuffer * buffer)
127 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
128 GstV4l2MemoryGroup *group;
130 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
131 gst_v4l2_allocator_reset_group (pool->vallocator, group);
133 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
138 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
141 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
143 GST_LOG_OBJECT (pool, "copying buffer");
145 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
146 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
147 GstVideoFrame src_frame, dest_frame;
149 GST_DEBUG_OBJECT (pool, "copy video frame");
151 /* we have raw video, use videoframe copy to get strides right */
152 if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
155 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
157 gst_video_frame_unmap (&src_frame);
161 gst_video_frame_copy (&dest_frame, &src_frame);
163 gst_video_frame_unmap (&src_frame);
164 gst_video_frame_unmap (&dest_frame);
168 GST_DEBUG_OBJECT (pool, "copy raw bytes");
170 if (!gst_buffer_map (src, &map, GST_MAP_READ))
173 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
175 gst_buffer_unmap (src, &map);
176 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
179 gst_buffer_copy_into (dest, src,
180 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
182 GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest);
188 GST_ERROR_OBJECT (pool, "could not map buffer");
189 return GST_FLOW_ERROR;
202 gst_v4l2_buffer_pool_import_quark (void)
204 static GQuark quark = 0;
207 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
213 _unmap_userptr_frame (struct UserPtrData *data)
216 gst_video_frame_unmap (&data->frame);
218 gst_buffer_unmap (data->buffer, &data->map);
221 gst_buffer_unref (data->buffer);
223 g_slice_free (struct UserPtrData, data);
227 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
228 GstBuffer * dest, GstBuffer * src)
230 GstFlowReturn ret = GST_FLOW_OK;
231 GstV4l2MemoryGroup *group = NULL;
233 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
234 struct UserPtrData *data = NULL;
236 GST_LOG_OBJECT (pool, "importing userptr");
239 if (!gst_v4l2_is_buffer_valid (dest, &group))
242 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
243 flags = GST_MAP_READ;
245 flags = GST_MAP_WRITE;
247 data = g_slice_new0 (struct UserPtrData);
249 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
250 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
251 gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
254 data->is_frame = TRUE;
256 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
259 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
260 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
261 gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
262 size[i] = GST_VIDEO_TILE_X_TILES (tinfo) *
263 GST_VIDEO_TILE_Y_TILES (tinfo) *
264 GST_VIDEO_FORMAT_INFO_TILE_SIZE (finfo, i);
266 size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
267 GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
271 /* In the single planar API, planes must be contiguous in memory and
272 * therefore they must have expected size. ie: no padding.
273 * To check these conditions, we check that plane 'i' start address
274 * + plane 'i' size equals to plane 'i+1' start address */
275 if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
276 for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
277 const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
279 gint estride = gst_video_format_info_extrapolate_stride (finfo, i,
280 pix_fmt->bytesperline);
281 guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
284 tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
285 if (tmp != data->frame.data[i + 1])
286 goto non_contiguous_mem;
290 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
291 data->frame.info.size, finfo->n_planes, data->frame.data, size))
297 data->is_frame = FALSE;
299 if (!gst_buffer_map (src, &data->map, flags))
302 ptr[0] = data->map.data;
303 size[0] = data->map.size;
305 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
306 data->map.size, 1, ptr, size))
310 data->buffer = gst_buffer_ref (src);
312 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
313 data, (GDestroyNotify) _unmap_userptr_frame);
315 gst_buffer_copy_into (dest, src,
316 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
322 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
323 return GST_FLOW_ERROR;
327 GST_ERROR_OBJECT (pool, "could not map buffer");
328 g_slice_free (struct UserPtrData, data);
329 return GST_FLOW_ERROR;
333 GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
334 _unmap_userptr_frame (data);
335 return GST_FLOW_ERROR;
339 GST_ERROR_OBJECT (pool, "failed to import data");
340 _unmap_userptr_frame (data);
341 return GST_FLOW_ERROR;
346 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
347 GstBuffer * dest, GstBuffer * src)
349 GstV4l2MemoryGroup *group = NULL;
350 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
351 guint n_mem = gst_buffer_n_memory (src);
354 GST_LOG_OBJECT (pool, "importing dmabuf");
356 if (!gst_v4l2_is_buffer_valid (dest, &group))
359 if (n_mem > GST_VIDEO_MAX_PLANES)
362 for (i = 0; i < n_mem; i++)
363 dma_mem[i] = gst_buffer_peek_memory (src, i);
365 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
369 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
370 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
372 gst_buffer_copy_into (dest, src,
373 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
379 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
380 return GST_FLOW_ERROR;
384 GST_ERROR_OBJECT (pool, "could not map buffer");
385 return GST_FLOW_ERROR;
389 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
390 return GST_FLOW_ERROR;
395 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
396 GstBuffer * dest, GstBuffer * src)
398 GstFlowReturn ret = GST_FLOW_OK;
399 gboolean own_src = FALSE;
402 if (pool->other_pool == NULL) {
403 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
404 return GST_FLOW_ERROR;
407 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
408 if (ret != GST_FLOW_OK) {
409 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
416 switch (pool->obj->mode) {
417 case GST_V4L2_IO_MMAP:
418 case GST_V4L2_IO_DMABUF:
419 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
421 case GST_V4L2_IO_USERPTR:
422 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
424 case GST_V4L2_IO_DMABUF_IMPORT:
425 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
432 gst_buffer_unref (src);
439 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
440 GstBufferPoolAcquireParams * params)
442 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
443 GstV4l2MemoryGroup *group = NULL;
444 GstBuffer *newbuf = NULL;
454 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
456 case GST_V4L2_IO_MMAP:
457 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
459 case GST_V4L2_IO_DMABUF:
460 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
463 case GST_V4L2_IO_USERPTR:
464 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
466 case GST_V4L2_IO_DMABUF_IMPORT:
467 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
471 g_assert_not_reached ();
477 newbuf = gst_buffer_new ();
479 for (i = 0; i < group->n_mem; i++)
480 gst_buffer_append_memory (newbuf, group->mem[i]);
482 if (g_atomic_int_get (&pool->buffer_state[group->buffer.index])) {
483 GST_WARNING_OBJECT (pool, "newly allocated buffer %u is not free",
484 group->buffer.index);
486 } else if (newbuf == NULL) {
487 goto allocation_failed;
490 /* add metadata to raw video buffers */
491 if (pool->add_videometa) {
492 GstVideoMeta *videometa =
493 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
494 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
495 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
496 info->offset, info->stride);
498 gst_video_meta_set_alignment (videometa, obj->align);
508 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
509 return GST_FLOW_ERROR;
514 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
516 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
517 GstV4l2Object *obj = pool->obj;
519 guint size, min_buffers, max_buffers;
520 GstAllocator *allocator;
521 GstAllocationParams params;
522 gboolean can_allocate = FALSE;
523 gboolean updated = FALSE;
526 pool->add_videometa =
527 gst_buffer_pool_config_has_option (config,
528 GST_BUFFER_POOL_OPTION_VIDEO_META);
530 /* parse the config and keep around */
531 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
535 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
538 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
541 gst_object_unref (pool->allocator);
542 pool->allocator = NULL;
545 case GST_V4L2_IO_DMABUF:
546 pool->allocator = gst_dmabuf_allocator_new ();
547 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
549 case GST_V4L2_IO_MMAP:
550 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
552 case GST_V4L2_IO_USERPTR:
554 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
556 case GST_V4L2_IO_DMABUF_IMPORT:
557 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
561 pool->allocator = g_object_ref (allocator);
562 pool->params = params;
563 /* No need to change the configuration */
567 g_assert_not_reached ();
571 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
572 * instability and crash, disable it for now */
573 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
574 GST_WARNING_OBJECT (pool,
575 "libv4l2 converter detected, disabling CREATE_BUFS");
576 can_allocate = FALSE;
577 GST_OBJECT_FLAG_UNSET (pool->vallocator,
578 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
579 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
580 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
583 if (min_buffers < GST_V4L2_MIN_BUFFERS (obj)) {
585 min_buffers = GST_V4L2_MIN_BUFFERS (obj);
586 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
589 /* respect driver requirements */
590 if (min_buffers < obj->min_buffers) {
592 min_buffers = obj->min_buffers;
593 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
596 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
598 max_buffers = VIDEO_MAX_FRAME;
599 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
602 if (min_buffers > max_buffers) {
604 min_buffers = max_buffers;
605 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
606 } else if (min_buffers != max_buffers) {
609 max_buffers = min_buffers;
610 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
614 if (!pool->add_videometa && obj->need_video_meta) {
615 GST_INFO_OBJECT (pool, "adding needed video meta");
617 gst_buffer_pool_config_add_option (config,
618 GST_BUFFER_POOL_OPTION_VIDEO_META);
621 /* Always update the config to ensure the configured size matches */
622 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
625 /* keep a GstVideoInfo with defaults for the when we need to copy */
626 gst_video_info_from_caps (&pool->caps_info, caps);
629 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
631 /* If anything was changed documentation recommend to return FALSE */
632 return !updated && ret;
637 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
643 gst_v4l2_buffer_pool_resurrect_buffer (GstV4l2BufferPool * pool)
645 GstBufferPoolAcquireParams params = { 0 };
646 GstBuffer *buffer = NULL;
649 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
651 /* block recursive calls to this function */
652 g_signal_handler_block (pool->vallocator, pool->group_released_handler);
655 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT |
656 GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
658 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
660 if (ret == GST_FLOW_OK)
661 gst_buffer_unref (buffer);
663 g_signal_handler_unblock (pool->vallocator, pool->group_released_handler);
669 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
671 GstV4l2Object *obj = pool->obj;
677 case GST_V4L2_IO_MMAP:
678 case GST_V4L2_IO_USERPTR:
679 case GST_V4L2_IO_DMABUF:
680 case GST_V4L2_IO_DMABUF_IMPORT:
681 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
685 num_queued = g_atomic_int_get (&pool->num_queued);
686 if (num_queued < pool->num_allocated)
687 n = pool->num_allocated - num_queued;
689 /* For captures, we need to enqueue buffers before we start streaming,
690 * so the driver don't underflow immediately. As we have put then back
691 * into the base class queue, resurrect them, then releasing will queue
693 for (i = 0; i < n; i++)
694 gst_v4l2_buffer_pool_resurrect_buffer (pool);
697 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
698 goto streamon_failed;
700 pool->streaming = TRUE;
702 GST_DEBUG_OBJECT (pool, "Started streaming");
712 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
718 /* Call with streamlock held, or when streaming threads are down */
720 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
722 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
723 GstV4l2Object *obj = pool->obj;
726 if (!pool->streaming)
730 case GST_V4L2_IO_MMAP:
731 case GST_V4L2_IO_USERPTR:
732 case GST_V4L2_IO_DMABUF:
733 case GST_V4L2_IO_DMABUF_IMPORT:
735 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
736 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
737 errno, g_strerror (errno));
739 pool->streaming = FALSE;
741 GST_DEBUG_OBJECT (pool, "Stopped streaming");
743 if (pool->vallocator)
744 gst_v4l2_allocator_flush (pool->vallocator);
750 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
751 gint old_buffer_state =
752 g_atomic_int_and (&pool->buffer_state[i], ~BUFFER_STATE_QUEUED);
753 if ((old_buffer_state & BUFFER_STATE_QUEUED) && pool->buffers[i]) {
754 GstBuffer *buffer = pool->buffers[i];
755 GstBufferPool *bpool = GST_BUFFER_POOL (pool);
757 pool->buffers[i] = NULL;
759 if (!(old_buffer_state & BUFFER_STATE_OUTSTANDING)) {
760 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
761 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, FALSE);
763 else /* Don't re-enqueue capture buffer on stop */
764 pclass->release_buffer (bpool, buffer);
767 g_atomic_int_add (&pool->num_queued, -1);
773 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
775 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
776 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
777 GstV4l2Object *obj = pool->obj;
778 GstStructure *config;
780 guint size, min_buffers, max_buffers;
781 guint max_latency, min_latency, copy_threshold = 0;
782 gboolean can_allocate = FALSE, ret = TRUE;
784 GST_DEBUG_OBJECT (pool, "activating pool");
786 if (pool->other_pool) {
789 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
790 goto other_pool_failed;
792 if (gst_buffer_pool_acquire_buffer (pool->other_pool, &buffer, NULL) !=
794 goto other_pool_failed;
796 if (!gst_v4l2_object_try_import (obj, buffer)) {
797 gst_buffer_unref (buffer);
800 gst_buffer_unref (buffer);
803 config = gst_buffer_pool_get_config (bpool);
804 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
808 min_latency = MAX (GST_V4L2_MIN_BUFFERS (obj), obj->min_buffers);
814 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
815 * of MMAP. In this case, the first read initialize the queues, but the
816 * poll before that will always fail. Doing an empty read, forces the
817 * queue to be initialized now. We only do this if we have a streaming
819 if (obj->device_caps & V4L2_CAP_STREAMING)
820 obj->read (obj->video_fd, NULL, 0);
823 case GST_V4L2_IO_DMABUF:
824 case GST_V4L2_IO_MMAP:
828 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
830 /* first, lets request buffers, and see how many we can get: */
831 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
833 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
835 pool->num_allocated = count;
837 if (count < GST_V4L2_MIN_BUFFERS (obj)) {
842 /* V4L2 buffer pool are often very limited in the amount of buffers it
843 * can offer. The copy_threshold will workaround this limitation by
844 * falling back to copy if the pipeline needed more buffers. This also
845 * prevent having to do REQBUFS(N)/REQBUFS(0) every time configure is
847 if (count != min_buffers || pool->enable_copy_threshold) {
848 GST_WARNING_OBJECT (pool,
849 "Uncertain or not enough buffers, enabling copy threshold");
851 copy_threshold = min_latency;
856 case GST_V4L2_IO_USERPTR:
861 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
863 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
865 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
866 V4L2_MEMORY_USERPTR);
867 pool->num_allocated = count;
869 /* There is no rational to not get what we asked */
870 if (count < min_buffers) {
878 case GST_V4L2_IO_DMABUF_IMPORT:
882 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
884 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
886 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
888 pool->num_allocated = count;
890 /* There is no rational to not get what we asked */
891 if (count < min_buffers) {
902 g_assert_not_reached ();
907 max_latency = max_buffers;
909 max_latency = min_buffers;
912 pool->copy_threshold = copy_threshold;
913 pool->max_latency = max_latency;
914 pool->min_latency = min_latency;
915 pool->num_queued = 0;
917 if (max_buffers != 0 && max_buffers < min_buffers)
918 max_buffers = min_buffers;
920 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
922 pclass->set_config (bpool, config);
923 gst_structure_free (config);
925 /* now, allocate the buffers: */
926 if (!pclass->start (bpool))
929 if (!V4L2_TYPE_IS_OUTPUT (obj->type)) {
930 if (g_atomic_int_get (&pool->num_queued) < pool->num_allocated)
933 pool->group_released_handler =
934 g_signal_connect_swapped (pool->vallocator, "group-released",
935 G_CALLBACK (gst_v4l2_buffer_pool_resurrect_buffer), pool);
936 ret = gst_v4l2_buffer_pool_streamon (pool);
944 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
945 gst_structure_free (config);
950 GST_ERROR_OBJECT (pool,
951 "we received %d buffer from device '%s', we want at least %d",
952 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS (obj));
953 gst_structure_free (config);
958 GST_ERROR_OBJECT (pool, "allocate failed");
963 GST_ERROR_OBJECT (pool, "failed to activate the other pool %"
964 GST_PTR_FORMAT, pool->other_pool);
969 GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue");
974 GST_ERROR_OBJECT (pool, "cannot import buffers from downstream pool");
980 gst_v4l2_buffer_pool_vallocator_stop (GstV4l2BufferPool * pool)
984 if (!pool->vallocator)
987 vret = gst_v4l2_allocator_stop (pool->vallocator);
989 if (vret == GST_V4L2_BUSY)
990 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
992 return (vret == GST_V4L2_OK);
996 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
998 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1001 GST_DEBUG_OBJECT (pool, "stopping pool");
1003 if (pool->group_released_handler > 0) {
1004 g_signal_handler_disconnect (pool->vallocator,
1005 pool->group_released_handler);
1006 pool->group_released_handler = 0;
1009 if (pool->other_pool) {
1010 gst_buffer_pool_set_active (pool->other_pool, FALSE);
1011 gst_object_unref (pool->other_pool);
1012 pool->other_pool = NULL;
1015 if (!pool->orphaned)
1016 gst_v4l2_buffer_pool_streamoff (pool);
1018 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
1021 ret = gst_v4l2_buffer_pool_vallocator_stop (pool);
1027 gst_v4l2_buffer_pool_orphan (GstV4l2Object * v4l2object)
1029 GstBufferPool *bpool = gst_v4l2_object_get_buffer_pool (v4l2object);
1030 GstV4l2BufferPool *pool;
1033 /* Nothing to do if there is no pool */
1037 pool = GST_V4L2_BUFFER_POOL (bpool);
1039 if (pool->orphaned != FALSE
1040 || !GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS (pool->vallocator)
1041 || g_getenv ("GST_V4L2_FORCE_DRAIN")) {
1042 gst_object_unref (bpool);
1046 GST_DEBUG_OBJECT (pool, "orphaning pool");
1047 gst_buffer_pool_set_active (bpool, FALSE);
1049 /* We lock to prevent racing with a return buffer in QBuf, and has a
1050 * workaround of not being able to use the pool hidden activation lock. */
1051 GST_OBJECT_LOCK (pool);
1053 gst_v4l2_buffer_pool_streamoff (pool);
1054 ret = gst_v4l2_allocator_orphan (pool->vallocator);
1056 pool->orphaned = TRUE;
1058 GST_OBJECT_UNLOCK (pool);
1061 GstBufferPool *old_pool;
1062 GST_OBJECT_LOCK (v4l2object->element);
1063 old_pool = v4l2object->pool;
1064 v4l2object->pool = NULL;
1065 GST_OBJECT_UNLOCK (v4l2object->element);
1067 gst_object_unref (old_pool);
1070 gst_object_unref (bpool);
1076 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
1078 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1080 GST_DEBUG_OBJECT (pool, "start flushing");
1082 gst_poll_set_flushing (pool->obj->poll, TRUE);
1084 GST_OBJECT_LOCK (pool);
1085 pool->empty = FALSE;
1086 g_cond_broadcast (&pool->empty_cond);
1087 GST_OBJECT_UNLOCK (pool);
1089 if (pool->other_pool && gst_buffer_pool_is_active (pool->other_pool))
1090 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
1094 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
1096 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1098 GST_DEBUG_OBJECT (pool, "stop flushing");
1100 if (pool->other_pool && gst_buffer_pool_is_active (pool->other_pool))
1101 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
1103 gst_poll_set_flushing (pool->obj->poll, FALSE);
1106 static GstFlowReturn
1107 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool, gboolean wait)
1109 GstClockTime timeout;
1112 timeout = GST_CLOCK_TIME_NONE;
1116 /* In RW mode there is no queue, hence no need to wait while the queue is
1118 if (pool->obj->mode != GST_V4L2_IO_RW) {
1119 GST_OBJECT_LOCK (pool);
1121 if (!wait && pool->empty) {
1122 GST_OBJECT_UNLOCK (pool);
1123 return GST_V4L2_FLOW_LAST_BUFFER;
1127 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1129 GST_OBJECT_UNLOCK (pool);
1132 if (!pool->obj->can_poll_device) {
1136 return GST_V4L2_FLOW_LAST_BUFFER;
1139 return gst_v4l2_object_poll (pool->obj, timeout);
1142 static GstFlowReturn
1143 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf,
1144 GstV4l2MemoryGroup * group, guint32 * frame_number)
1146 const GstV4l2Object *obj = pool->obj;
1147 gint old_buffer_state;
1150 index = group->buffer.index;
1153 g_atomic_int_or (&pool->buffer_state[index], BUFFER_STATE_QUEUED);
1154 if (old_buffer_state & BUFFER_STATE_QUEUED)
1155 goto already_queued;
1157 GST_LOG_OBJECT (pool, "queuing buffer %i, previous-state = %i", index,
1160 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1161 enum v4l2_field field;
1163 /* Buffer field is the same as the one defined in format */
1164 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1165 field = obj->format.fmt.pix_mp.field;
1167 field = obj->format.fmt.pix.field;
1169 group->buffer.field = field;
1173 group->buffer.timestamp.tv_sec = *frame_number;
1174 group->buffer.timestamp.tv_usec = 0;
1176 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1177 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
1178 GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp);
1180 group->buffer.timestamp.tv_sec = -1;
1181 group->buffer.timestamp.tv_usec = -1;
1185 GST_OBJECT_LOCK (pool);
1187 /* If the pool was orphaned, don't try to queue any returned buffers.
1188 * This is done with the objet lock in order to synchronize with
1193 g_atomic_int_inc (&pool->num_queued);
1194 pool->buffers[index] = buf;
1196 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1199 pool->empty = FALSE;
1200 g_cond_signal (&pool->empty_cond);
1201 GST_OBJECT_UNLOCK (pool);
1207 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1208 return GST_FLOW_ERROR;
1212 GST_DEBUG_OBJECT (pool, "pool was orphaned, not queuing back buffer.");
1213 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1214 g_atomic_int_and (&pool->buffer_state[index], ~BUFFER_STATE_QUEUED);
1215 GST_OBJECT_UNLOCK (pool);
1216 return GST_FLOW_FLUSHING;
1220 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1221 /* Mark broken buffer to the allocator */
1222 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1223 g_atomic_int_add (&pool->num_queued, -1);
1224 pool->buffers[index] = NULL;
1225 g_atomic_int_and (&pool->buffer_state[index], ~BUFFER_STATE_QUEUED);
1226 GST_OBJECT_UNLOCK (pool);
1227 return GST_FLOW_ERROR;
1231 static GstFlowReturn
1232 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
1233 gboolean * outstanding, gboolean wait)
1236 GstBuffer *outbuf = NULL;
1237 GstV4l2Object *obj = pool->obj;
1238 GstClockTime timestamp;
1239 GstV4l2MemoryGroup *group;
1240 const GstVideoInfo *info = &obj->info;
1242 gint old_buffer_state;
1244 if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
1247 if (res == GST_V4L2_FLOW_LAST_BUFFER) {
1248 GST_LOG_OBJECT (pool, "nothing to dequeue");
1252 if (res == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
1253 GST_INFO_OBJECT (pool, "Resolution change detected.");
1257 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1259 res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
1260 if (res == GST_V4L2_FLOW_LAST_BUFFER)
1262 if (res != GST_FLOW_OK)
1266 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
1267 ~BUFFER_STATE_QUEUED);
1268 if (!(old_buffer_state & BUFFER_STATE_QUEUED))
1272 *outstanding = (old_buffer_state & BUFFER_STATE_OUTSTANDING) != 0;
1273 } else if (old_buffer_state & BUFFER_STATE_OUTSTANDING) {
1274 GST_WARNING_OBJECT (pool, "unexpected outstanding buffer %u",
1275 group->buffer.index);
1278 outbuf = pool->buffers[group->buffer.index];
1282 pool->buffers[group->buffer.index] = NULL;
1283 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1284 GST_OBJECT_LOCK (pool);
1286 GST_OBJECT_UNLOCK (pool);
1289 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1291 for (i = 0; i < group->n_mem; i++) {
1292 const GstVideoFormatInfo *finfo = info->finfo;
1294 GST_LOG_OBJECT (pool,
1295 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1296 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p, previous-state=%i",
1297 outbuf, group->buffer.sequence, group->buffer.index, group->mem[i],
1298 group->planes[i].bytesused, i, group->buffer.flags,
1299 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf, old_buffer_state);
1301 if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) == GST_VIDEO_FORMAT_ENCODED)
1304 /* Ensure our offset matches the expected plane size, or image size if
1305 * there is only one memory */
1306 if (group->n_mem == 1) {
1307 gst_memory_resize (group->mem[0], 0, info->size + info->offset[0]);
1311 if (!GST_VIDEO_FORMAT_INFO_IS_TILED (finfo))
1312 gst_memory_resize (group->mem[i], 0, obj->plane_size[i]);
1315 /* Ignore timestamp and field for OUTPUT device */
1316 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1319 /* Check for driver bug in reporting feild */
1320 if (group->buffer.field == V4L2_FIELD_ANY) {
1321 /* Only warn once to avoid the spamming */
1322 #ifndef GST_DISABLE_GST_DEBUG
1323 if (!pool->has_warned_on_buggy_field) {
1324 pool->has_warned_on_buggy_field = TRUE;
1325 GST_WARNING_OBJECT (pool,
1326 "Driver should never set v4l2_buffer.field to ANY");
1330 /* Use the value from the format (works for UVC bug) */
1331 group->buffer.field = obj->format.fmt.pix.field;
1333 /* If driver also has buggy S_FMT, assume progressive */
1334 if (group->buffer.field == V4L2_FIELD_ANY) {
1335 #ifndef GST_DISABLE_GST_DEBUG
1336 if (!pool->has_warned_on_buggy_field) {
1337 pool->has_warned_on_buggy_field = TRUE;
1338 GST_WARNING_OBJECT (pool,
1339 "Driver should never set v4l2_format.pix.field to ANY");
1343 group->buffer.field = V4L2_FIELD_NONE;
1347 /* set top/bottom field first if v4l2_buffer has the information */
1348 switch (group->buffer.field) {
1349 case V4L2_FIELD_NONE:
1350 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1351 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1353 case V4L2_FIELD_TOP:
1354 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1355 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1357 case V4L2_FIELD_BOTTOM:
1358 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1359 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1361 case V4L2_FIELD_INTERLACED_TB:
1362 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1363 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1365 case V4L2_FIELD_INTERLACED_BT:
1366 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1367 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1369 case V4L2_FIELD_INTERLACED:
1370 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1371 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1372 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1373 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1374 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1376 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1380 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1381 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1382 GST_FIXME_OBJECT (pool,
1383 "Unhandled enum v4l2_field %d - treating as progressive",
1384 group->buffer.field);
1388 if (!gst_v4l2_object_is_raw (obj)) {
1389 if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) ||
1390 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG ||
1391 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG ||
1392 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG)
1393 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1395 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1398 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1399 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1401 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1402 GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
1403 GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
1413 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1418 return GST_V4L2_FLOW_LAST_BUFFER;
1422 return GST_FLOW_ERROR;
1426 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1427 group->buffer.index);
1428 return GST_FLOW_ERROR;
1432 static GstFlowReturn
1433 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1434 GstBufferPoolAcquireParams * params)
1437 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1438 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1439 GstV4l2Object *obj = pool->obj;
1441 GST_DEBUG_OBJECT (pool, "acquire");
1443 /* If this is being called to resurrect a lost buffer */
1444 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1445 ret = pclass->acquire_buffer (bpool, buffer, params);
1449 switch (obj->type) {
1450 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1451 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1452 /* capture, This function should return a buffer with new captured data */
1453 switch (obj->mode) {
1454 case GST_V4L2_IO_RW:
1456 /* take empty buffer from the pool */
1457 ret = pclass->acquire_buffer (bpool, buffer, params);
1460 case GST_V4L2_IO_DMABUF:
1461 case GST_V4L2_IO_MMAP:
1462 case GST_V4L2_IO_USERPTR:
1463 case GST_V4L2_IO_DMABUF_IMPORT:
1465 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1466 * storage for our buffers. This function does poll first so we can
1467 * interrupt it fine. */
1468 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer, NULL, TRUE);
1472 ret = GST_FLOW_ERROR;
1473 g_assert_not_reached ();
1479 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1480 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1481 /* playback, This function should return an empty buffer */
1482 switch (obj->mode) {
1483 case GST_V4L2_IO_RW:
1484 /* get an empty buffer */
1485 ret = pclass->acquire_buffer (bpool, buffer, params);
1488 case GST_V4L2_IO_MMAP:
1489 case GST_V4L2_IO_DMABUF:
1490 case GST_V4L2_IO_USERPTR:
1491 case GST_V4L2_IO_DMABUF_IMPORT:
1492 /* get a free unqueued buffer */
1493 ret = pclass->acquire_buffer (bpool, buffer, params);
1497 ret = GST_FLOW_ERROR;
1498 g_assert_not_reached ();
1504 ret = GST_FLOW_ERROR;
1505 g_assert_not_reached ();
1509 /* Mark buffer as outstanding */
1510 if (ret == GST_FLOW_OK) {
1511 GstV4l2MemoryGroup *group;
1512 if (gst_v4l2_is_buffer_valid (*buffer, &group)) {
1513 GST_LOG_OBJECT (pool, "mark buffer %u outstanding", group->buffer.index);
1514 g_atomic_int_or (&pool->buffer_state[group->buffer.index],
1515 BUFFER_STATE_OUTSTANDING);
1523 * Completes a release buffer operation
1525 * Before calling this function:
1526 * - The buffer state (if applicable) must have already been updated.
1527 * - The buffer must not be outstanding.
1528 * - The "queued" argument contains whether the buffer is currently queued.
1531 gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
1532 GstBuffer * buffer, gboolean queued)
1534 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1535 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1536 GstV4l2Object *obj = pool->obj;
1538 GST_DEBUG_OBJECT (pool, "complete release buffer %p (queued = %s)", buffer,
1539 queued ? "yes" : "no");
1541 switch (obj->type) {
1542 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1543 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1544 /* capture, put the buffer back in the queue so that we can refill it
1547 GST_WARNING_OBJECT (pool,
1548 "capture buffer %p was release while still queued", buffer);
1551 switch (obj->mode) {
1552 case GST_V4L2_IO_RW:
1553 /* release back in the pool */
1554 pclass->release_buffer (bpool, buffer);
1557 case GST_V4L2_IO_DMABUF:
1558 case GST_V4L2_IO_MMAP:
1559 case GST_V4L2_IO_USERPTR:
1560 case GST_V4L2_IO_DMABUF_IMPORT:
1562 GstV4l2MemoryGroup *group;
1563 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1564 GstFlowReturn ret = GST_FLOW_OK;
1566 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1567 /* queue back in the device */
1568 if (pool->other_pool)
1569 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1570 if (ret != GST_FLOW_OK ||
1571 gst_v4l2_buffer_pool_qbuf (pool, buffer, group,
1572 NULL) != GST_FLOW_OK)
1573 pclass->release_buffer (bpool, buffer);
1575 /* Simply release invalid/modified buffer, the allocator will
1576 * give it back later */
1577 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1578 pclass->release_buffer (bpool, buffer);
1583 g_assert_not_reached ();
1588 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1589 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1590 switch (obj->mode) {
1591 case GST_V4L2_IO_RW:
1592 /* release back in the pool */
1593 pclass->release_buffer (bpool, buffer);
1596 case GST_V4L2_IO_MMAP:
1597 case GST_V4L2_IO_DMABUF:
1598 case GST_V4L2_IO_USERPTR:
1599 case GST_V4L2_IO_DMABUF_IMPORT:
1601 GstV4l2MemoryGroup *group;
1604 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1605 /* Simply release invalid/modified buffer, the allocator will
1606 * give it back later */
1607 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1608 pclass->release_buffer (bpool, buffer);
1612 index = group->buffer.index;
1615 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1618 /* Remove qdata, this will unmap any map data in userptr */
1619 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1620 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1622 /* reset to default size */
1623 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1625 /* playback, put the buffer back in the queue to refill later. */
1626 pclass->release_buffer (bpool, buffer);
1628 /* the buffer is queued in the device but maybe not played yet. We just
1629 * leave it there and not make it available for future calls to acquire
1630 * for now. The buffer will be dequeued and reused later. */
1631 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1637 g_assert_not_reached ();
1643 g_assert_not_reached ();
1649 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1651 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1652 GstV4l2MemoryGroup *group;
1653 gboolean queued = FALSE;
1655 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1656 gint old_buffer_state =
1657 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
1658 ~BUFFER_STATE_OUTSTANDING);
1659 queued = (old_buffer_state & BUFFER_STATE_QUEUED) != 0;
1660 GST_LOG_OBJECT (pool, "mark buffer %u not outstanding",
1661 group->buffer.index);
1664 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, queued);
1668 gst_v4l2_buffer_pool_dispose (GObject * object)
1670 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1672 if (pool->vallocator)
1673 gst_object_unref (pool->vallocator);
1674 pool->vallocator = NULL;
1676 if (pool->allocator)
1677 gst_object_unref (pool->allocator);
1678 pool->allocator = NULL;
1680 if (pool->other_pool)
1681 gst_object_unref (pool->other_pool);
1682 pool->other_pool = NULL;
1684 G_OBJECT_CLASS (parent_class)->dispose (object);
1688 gst_v4l2_buffer_pool_finalize (GObject * object)
1690 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1692 if (pool->video_fd >= 0)
1693 pool->obj->close (pool->video_fd);
1695 /* This can't be done in dispose method because we must not set pointer
1696 * to NULL as it is part of the v4l2object and dispose could be called
1698 gst_object_unref (pool->obj->element);
1700 g_cond_clear (&pool->empty_cond);
1702 /* FIXME have we done enough here ? */
1704 G_OBJECT_CLASS (parent_class)->finalize (object);
1708 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1710 g_cond_init (&pool->empty_cond);
1712 pool->orphaned = FALSE;
1713 for (gint i = 0; i < VIDEO_MAX_FRAME; i++)
1714 g_atomic_int_set (&pool->buffer_state[i], BUFFER_STATE_FREE);
1718 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1720 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1721 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1723 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1724 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1726 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1727 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1728 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1729 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1730 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1731 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1732 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1733 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1735 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1736 "V4L2 Buffer Pool");
1737 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
1741 * gst_v4l2_buffer_pool_new:
1742 * @obj: the v4l2 object owning the pool
1744 * Construct a new buffer pool.
1746 * Returns: the new pool, use gst_object_unref() to free resources
1749 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1751 GstV4l2BufferPool *pool;
1752 GstStructure *config;
1753 gchar *name, *parent_name;
1756 fd = obj->dup (obj->video_fd);
1760 /* setting a significant unique name */
1761 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1762 name = g_strdup_printf ("%s:pool%u:%s",
1763 parent_name, obj->pool_seq++,
1764 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src");
1765 g_free (parent_name);
1767 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1768 "name", name, NULL);
1769 g_object_ref_sink (pool);
1772 pool->video_fd = fd;
1775 pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
1776 if (pool->vallocator == NULL)
1777 goto allocator_failed;
1779 gst_object_ref (obj->element);
1781 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1782 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1783 /* This will simply set a default config, but will not configure the pool
1784 * because min and max are not valid */
1785 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1787 return GST_BUFFER_POOL (pool);
1792 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1797 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1798 gst_object_unref (pool);
1803 static GstFlowReturn
1804 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1807 GstV4l2Object *obj = pool->obj;
1812 toread = obj->info.size;
1814 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1816 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1819 if ((res = gst_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK)
1822 amount = obj->read (obj->video_fd, map.data, toread);
1824 if (amount == toread) {
1826 } else if (amount == -1) {
1827 if (errno == EAGAIN || errno == EINTR) {
1832 /* short reads can happen if a signal interrupts the read */
1837 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1838 gst_buffer_unmap (buf, &map);
1839 gst_buffer_resize (buf, 0, amount);
1846 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1851 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1852 (_("Error reading %d bytes from device '%s'."),
1853 toread, obj->videodev), GST_ERROR_SYSTEM);
1854 res = GST_FLOW_ERROR;
1859 gst_buffer_unmap (buf, &map);
1860 gst_buffer_resize (buf, 0, 0);
1866 * gst_v4l2_buffer_pool_process:
1867 * @bpool: a #GstBufferPool
1868 * @buf: a #GstBuffer, maybe be replaced
1869 * @frame_number: 32 bit frame number or %NULL
1871 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1872 * data from the device. For output devices, this functions send the contents of
1873 * @buf to the device for playback.
1875 * If non-%NULL and an output device, @frame_number is stored inside the timestamp for output devices and read
1876 * back from the timestamp for capture devices.
1878 * Returns: %GST_FLOW_OK on success.
1881 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf,
1882 guint32 * frame_number)
1884 GstFlowReturn ret = GST_FLOW_OK;
1885 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1886 GstV4l2Object *obj = pool->obj;
1888 GST_DEBUG_OBJECT (pool, "process buffer %p", *buf);
1890 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1891 return GST_FLOW_FLUSHING;
1893 switch (obj->type) {
1894 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1895 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1897 switch (obj->mode) {
1898 case GST_V4L2_IO_RW:
1899 /* capture into the buffer */
1900 ret = gst_v4l2_do_read (pool, *buf);
1903 case GST_V4L2_IO_MMAP:
1904 case GST_V4L2_IO_DMABUF:
1908 if ((*buf)->pool == bpool) {
1910 gsize size = gst_buffer_get_size (*buf);
1912 /* Legacy M2M devices return empty buffer when drained */
1913 if (size == 0 && GST_V4L2_IS_M2M (obj->device_caps)) {
1914 gst_v4l2_buffer_pool_resize_buffer (bpool, *buf);
1918 if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) !=
1919 GST_VIDEO_FORMAT_ENCODED && size < pool->size)
1920 goto buffer_truncated;
1922 num_queued = g_atomic_int_get (&pool->num_queued);
1923 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1926 /* If we have no more buffer, and can allocate it time to do so */
1927 if (num_queued == 0) {
1928 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1929 GST_DEBUG_OBJECT (pool, "Resurrect for empty queue");
1930 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1931 if (ret == GST_FLOW_OK || ret == GST_FLOW_FLUSHING)
1936 /* start copying buffers when we are running low on buffers */
1937 if (num_queued < pool->copy_threshold) {
1940 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1941 GST_DEBUG_OBJECT (pool, "Resurrect for threshold");
1942 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1943 if (ret == GST_FLOW_OK || ret == GST_FLOW_FLUSHING)
1947 /* copy the buffer */
1948 copy = gst_buffer_copy_region (*buf,
1949 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1950 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1952 /* and requeue so that we can continue capturing */
1953 gst_buffer_unref (*buf);
1958 /* nothing, data was inside the buffer when we did _acquire() */
1962 /* buffer not from our pool, grab a frame and copy it into the target */
1963 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp, NULL, TRUE))
1967 /* An empty buffer on capture indicates the end of stream */
1968 if (gst_buffer_get_size (tmp) == 0) {
1969 gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
1971 /* Legacy M2M devices return empty buffer when drained */
1972 if (GST_V4L2_IS_M2M (obj->device_caps)) {
1973 gst_v4l2_buffer_pool_resize_buffer (bpool, *buf);
1978 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1980 /* an queue the buffer again after the copy */
1981 gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
1983 if (ret != GST_FLOW_OK)
1988 case GST_V4L2_IO_USERPTR:
1990 struct UserPtrData *data;
1993 /* Replace our buffer with downstream allocated buffer */
1994 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1995 GST_V4L2_IMPORT_QUARK);
1996 tmp = gst_buffer_ref (data->buffer);
1997 _unmap_userptr_frame (data);
1999 /* Now tmp is writable, copy the flags and timestamp */
2000 gst_buffer_copy_into (tmp, *buf,
2001 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2003 gst_buffer_replace (buf, tmp);
2004 gst_buffer_unref (tmp);
2008 case GST_V4L2_IO_DMABUF_IMPORT:
2012 /* Replace our buffer with downstream allocated buffer */
2013 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
2014 GST_V4L2_IMPORT_QUARK);
2016 gst_buffer_copy_into (tmp, *buf,
2017 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2019 gst_buffer_replace (buf, tmp);
2020 gst_buffer_unref (tmp);
2025 g_assert_not_reached ();
2030 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2031 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
2033 switch (obj->mode) {
2034 case GST_V4L2_IO_RW:
2035 /* FIXME, do write() */
2036 GST_WARNING_OBJECT (pool, "implement write()");
2039 case GST_V4L2_IO_USERPTR:
2040 case GST_V4L2_IO_DMABUF_IMPORT:
2041 case GST_V4L2_IO_DMABUF:
2042 case GST_V4L2_IO_MMAP:
2044 GstBuffer *to_queue = NULL;
2046 GstV4l2MemoryGroup *group;
2048 gboolean outstanding;
2050 if ((*buf)->pool != bpool)
2053 if (!gst_v4l2_is_buffer_valid (*buf, &group))
2056 index = group->buffer.index;
2058 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
2060 if (g_atomic_int_get (&pool->buffer_state[index]) &
2061 BUFFER_STATE_QUEUED) {
2062 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
2066 /* we can queue directly */
2067 to_queue = gst_buffer_ref (*buf);
2070 if (to_queue == NULL) {
2071 GstBufferPoolAcquireParams params = { 0 };
2073 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
2075 /* this can return EOS if all buffers are outstanding which would
2076 * be strange because we would expect the upstream element to have
2077 * allocated them and returned to us.. */
2078 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
2079 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
2080 if (ret != GST_FLOW_OK)
2081 goto acquire_failed;
2083 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
2084 if (ret != GST_FLOW_OK) {
2085 gst_buffer_unref (to_queue);
2086 goto prepare_failed;
2089 /* retrieve the group */
2090 gst_v4l2_is_buffer_valid (to_queue, &group);
2094 gst_v4l2_buffer_pool_qbuf (pool, to_queue, group,
2099 /* if we are not streaming yet (this is the first buffer, start
2101 if (!gst_v4l2_buffer_pool_streamon (pool)) {
2102 /* don't check return value because qbuf would have failed */
2103 gst_v4l2_is_buffer_valid (to_queue, &group);
2105 /* qbuf has stored to_queue buffer but we are not in
2106 * streaming state, so the flush logic won't be performed.
2107 * To avoid leaks, flush the allocator and restore the queued
2108 * buffer as non-queued */
2109 gst_v4l2_allocator_flush (pool->vallocator);
2111 pool->buffers[group->buffer.index] = NULL;
2112 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
2113 ~BUFFER_STATE_QUEUED);
2115 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
2116 GST_V4L2_IMPORT_QUARK, NULL, NULL);
2117 gst_buffer_unref (to_queue);
2118 g_atomic_int_add (&pool->num_queued, -1);
2122 /* Remove our ref, we will still hold this buffer in acquire as needed,
2123 * otherwise the pool will think it is outstanding and will refuse to stop. */
2124 gst_buffer_unref (to_queue);
2126 /* release as many buffer as possible */
2127 while (gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding,
2128 FALSE) == GST_FLOW_OK) {
2130 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
2134 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
2135 /* all buffers are queued, try to dequeue one and release it back
2136 * into the pool so that _acquire can get to it again. */
2138 gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding, TRUE);
2139 if (ret == GST_FLOW_OK && !outstanding)
2140 /* release the rendered buffer back into the pool. This wakes up any
2141 * thread waiting for a buffer in _acquire(). */
2142 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
2148 g_assert_not_reached ();
2153 g_assert_not_reached ();
2162 GST_ERROR_OBJECT (pool, "failed to copy buffer");
2167 GST_WARNING_OBJECT (pool,
2168 "Dropping truncated buffer, this is likely a driver bug.");
2169 gst_buffer_unref (*buf);
2171 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
2175 GST_DEBUG_OBJECT (pool, "end of sequence reached");
2176 gst_buffer_unref (*buf);
2178 return GST_V4L2_FLOW_LAST_BUFFER;
2182 if (ret == GST_FLOW_FLUSHING)
2183 GST_DEBUG_OBJECT (pool, "flushing");
2185 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
2186 gst_flow_get_name (ret));
2191 GST_ERROR_OBJECT (pool, "failed to prepare data");
2196 GST_ERROR_OBJECT (pool, "failed to queue buffer");
2201 GST_ERROR_OBJECT (pool, "failed to start streaming");
2202 return GST_FLOW_ERROR;
2207 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
2208 GstBufferPool * other_pool)
2210 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
2212 if (pool->other_pool)
2213 gst_object_unref (pool->other_pool);
2214 pool->other_pool = gst_object_ref (other_pool);
2218 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
2220 GST_OBJECT_LOCK (pool);
2221 pool->enable_copy_threshold = copy;
2222 GST_OBJECT_UNLOCK (pool);
2225 static GstFlowReturn
2226 gst_v4l2_buffer_pool_flush_events (GstV4l2Object * v4l2object)
2228 GstFlowReturn ret = GST_FLOW_OK;
2229 gboolean event_found;
2231 /* FIXME simplify this when we drop legacy support for driver without poll()
2232 * support. When we do, we can switch the video_fd to non blocking, and just
2233 * pop the events directly. */
2236 struct v4l2_event event = { 0, };
2239 event_found = FALSE;
2241 gst_poll_set_flushing (v4l2object->poll, FALSE);
2244 /* GstPoll don't have 0ns timeout, but 1 will do */
2245 poll_ret = gst_poll_wait (v4l2object->poll, 1);
2246 } while (poll_ret == EAGAIN || poll_ret == EINTR);
2248 if (gst_poll_fd_has_pri (v4l2object->poll, &v4l2object->pollfd)) {
2249 if (!gst_v4l2_dequeue_event (v4l2object, &event))
2250 return GST_FLOW_ERROR;
2254 if (event.type == V4L2_EVENT_SOURCE_CHANGE &&
2255 (event.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION)) {
2256 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2257 "Can't streamon capture as the resolution have changed.");
2258 ret = GST_V4L2_FLOW_RESOLUTION_CHANGE;
2261 } while (event_found);
2267 gst_v4l2_buffer_pool_flush (GstV4l2Object * v4l2object)
2269 GstBufferPool *bpool = gst_v4l2_object_get_buffer_pool (v4l2object);
2270 GstV4l2BufferPool *pool;
2271 GstFlowReturn ret = GST_FLOW_OK;
2274 return GST_FLOW_ERROR;
2276 pool = GST_V4L2_BUFFER_POOL (bpool);
2278 gst_v4l2_buffer_pool_streamoff (pool);
2280 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
2281 ret = gst_v4l2_buffer_pool_flush_events (v4l2object);
2283 /* If the format haven't change, avoid reallocation to go back to
2285 if (ret == GST_FLOW_OK)
2286 ret = gst_v4l2_buffer_pool_streamon (pool);
2289 gst_object_unref (bpool);
2294 * gst_v4l2_buffer_pool_enable_resolution_change:
2295 * @pool: a #GstBufferPool
2297 * When this is called, the pool will subscribe to the
2298 * %V4L2_EVENT_SOURCE_CHANGE. Upon receiving this event, it will notify
2299 * the element acquiring buffer with the special flow return
2300 * %GST_V4L2_FLOW_RESOLUTION_CHANGE.
2303 gst_v4l2_buffer_pool_enable_resolution_change (GstV4l2BufferPool * pool)
2305 gst_v4l2_object_subscribe_event (pool->obj, V4L2_EVENT_SOURCE_CHANGE);