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 GstVideoMeta *vmeta;
1243 gint old_buffer_state;
1245 if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
1248 if (res == GST_V4L2_FLOW_LAST_BUFFER) {
1249 GST_LOG_OBJECT (pool, "nothing to dequeue");
1253 if (res == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
1254 GST_INFO_OBJECT (pool, "Resolution change detected.");
1258 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1260 res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
1261 if (res == GST_V4L2_FLOW_LAST_BUFFER)
1263 if (res != GST_FLOW_OK)
1267 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
1268 ~BUFFER_STATE_QUEUED);
1269 if (!(old_buffer_state & BUFFER_STATE_QUEUED))
1273 *outstanding = (old_buffer_state & BUFFER_STATE_OUTSTANDING) != 0;
1274 } else if (old_buffer_state & BUFFER_STATE_OUTSTANDING) {
1275 GST_WARNING_OBJECT (pool, "unexpected outstanding buffer %u",
1276 group->buffer.index);
1279 outbuf = pool->buffers[group->buffer.index];
1283 pool->buffers[group->buffer.index] = NULL;
1284 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1285 GST_OBJECT_LOCK (pool);
1287 GST_OBJECT_UNLOCK (pool);
1290 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1293 vmeta = gst_buffer_get_video_meta (outbuf);
1294 for (i = 0; i < group->n_mem; i++) {
1295 GST_LOG_OBJECT (pool,
1296 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1297 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p, previous-state=%i",
1298 outbuf, group->buffer.sequence, group->buffer.index, group->mem[i],
1299 group->planes[i].bytesused, i, group->buffer.flags,
1300 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf, old_buffer_state);
1303 vmeta->offset[i] = size;
1304 size += gst_memory_get_sizes (group->mem[i], NULL, NULL);
1308 /* Ignore timestamp and field for OUTPUT device */
1309 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1312 /* Check for driver bug in reporting feild */
1313 if (group->buffer.field == V4L2_FIELD_ANY) {
1314 /* Only warn once to avoid the spamming */
1315 #ifndef GST_DISABLE_GST_DEBUG
1316 if (!pool->has_warned_on_buggy_field) {
1317 pool->has_warned_on_buggy_field = TRUE;
1318 GST_WARNING_OBJECT (pool,
1319 "Driver should never set v4l2_buffer.field to ANY");
1323 /* Use the value from the format (works for UVC bug) */
1324 group->buffer.field = obj->format.fmt.pix.field;
1326 /* If driver also has buggy S_FMT, assume progressive */
1327 if (group->buffer.field == V4L2_FIELD_ANY) {
1328 #ifndef GST_DISABLE_GST_DEBUG
1329 if (!pool->has_warned_on_buggy_field) {
1330 pool->has_warned_on_buggy_field = TRUE;
1331 GST_WARNING_OBJECT (pool,
1332 "Driver should never set v4l2_format.pix.field to ANY");
1336 group->buffer.field = V4L2_FIELD_NONE;
1340 /* set top/bottom field first if v4l2_buffer has the information */
1341 switch (group->buffer.field) {
1342 case V4L2_FIELD_NONE:
1343 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1344 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1346 case V4L2_FIELD_TOP:
1347 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1348 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1350 case V4L2_FIELD_BOTTOM:
1351 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1352 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1354 case V4L2_FIELD_INTERLACED_TB:
1355 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1356 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1358 case V4L2_FIELD_INTERLACED_BT:
1359 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1360 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1362 case V4L2_FIELD_INTERLACED:
1363 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1364 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1365 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1366 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1367 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1369 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1373 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1374 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1375 GST_FIXME_OBJECT (pool,
1376 "Unhandled enum v4l2_field %d - treating as progressive",
1377 group->buffer.field);
1381 if (!gst_v4l2_object_is_raw (obj)) {
1382 if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) ||
1383 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG ||
1384 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG ||
1385 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG)
1386 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1388 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1391 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1392 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1394 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1395 GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
1396 GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
1406 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1411 return GST_V4L2_FLOW_LAST_BUFFER;
1415 return GST_FLOW_ERROR;
1419 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1420 group->buffer.index);
1421 return GST_FLOW_ERROR;
1425 static GstFlowReturn
1426 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1427 GstBufferPoolAcquireParams * params)
1430 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1431 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1432 GstV4l2Object *obj = pool->obj;
1434 GST_DEBUG_OBJECT (pool, "acquire");
1436 /* If this is being called to resurrect a lost buffer */
1437 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1438 ret = pclass->acquire_buffer (bpool, buffer, params);
1442 switch (obj->type) {
1443 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1444 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1445 /* capture, This function should return a buffer with new captured data */
1446 switch (obj->mode) {
1447 case GST_V4L2_IO_RW:
1449 /* take empty buffer from the pool */
1450 ret = pclass->acquire_buffer (bpool, buffer, params);
1453 case GST_V4L2_IO_DMABUF:
1454 case GST_V4L2_IO_MMAP:
1455 case GST_V4L2_IO_USERPTR:
1456 case GST_V4L2_IO_DMABUF_IMPORT:
1458 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1459 * storage for our buffers. This function does poll first so we can
1460 * interrupt it fine. */
1461 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer, NULL, TRUE);
1465 ret = GST_FLOW_ERROR;
1466 g_assert_not_reached ();
1472 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1473 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1474 /* playback, This function should return an empty buffer */
1475 switch (obj->mode) {
1476 case GST_V4L2_IO_RW:
1477 /* get an empty buffer */
1478 ret = pclass->acquire_buffer (bpool, buffer, params);
1481 case GST_V4L2_IO_MMAP:
1482 case GST_V4L2_IO_DMABUF:
1483 case GST_V4L2_IO_USERPTR:
1484 case GST_V4L2_IO_DMABUF_IMPORT:
1485 /* get a free unqueued buffer */
1486 ret = pclass->acquire_buffer (bpool, buffer, params);
1490 ret = GST_FLOW_ERROR;
1491 g_assert_not_reached ();
1497 ret = GST_FLOW_ERROR;
1498 g_assert_not_reached ();
1502 /* Mark buffer as outstanding */
1503 if (ret == GST_FLOW_OK) {
1504 GstV4l2MemoryGroup *group;
1505 if (gst_v4l2_is_buffer_valid (*buffer, &group)) {
1506 GST_LOG_OBJECT (pool, "mark buffer %u outstanding", group->buffer.index);
1507 g_atomic_int_or (&pool->buffer_state[group->buffer.index],
1508 BUFFER_STATE_OUTSTANDING);
1516 * Completes a release buffer operation
1518 * Before calling this function:
1519 * - The buffer state (if applicable) must have already been updated.
1520 * - The buffer must not be outstanding.
1521 * - The "queued" argument contains whether the buffer is currently queued.
1524 gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
1525 GstBuffer * buffer, gboolean queued)
1527 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1528 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1529 GstV4l2Object *obj = pool->obj;
1531 GST_DEBUG_OBJECT (pool, "complete release buffer %p (queued = %s)", buffer,
1532 queued ? "yes" : "no");
1534 switch (obj->type) {
1535 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1536 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1537 /* capture, put the buffer back in the queue so that we can refill it
1540 GST_WARNING_OBJECT (pool,
1541 "capture buffer %p was release while still queued", buffer);
1544 switch (obj->mode) {
1545 case GST_V4L2_IO_RW:
1546 /* release back in the pool */
1547 pclass->release_buffer (bpool, buffer);
1550 case GST_V4L2_IO_DMABUF:
1551 case GST_V4L2_IO_MMAP:
1552 case GST_V4L2_IO_USERPTR:
1553 case GST_V4L2_IO_DMABUF_IMPORT:
1555 GstV4l2MemoryGroup *group;
1556 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1557 GstFlowReturn ret = GST_FLOW_OK;
1559 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1560 /* queue back in the device */
1561 if (pool->other_pool)
1562 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1563 if (ret != GST_FLOW_OK ||
1564 gst_v4l2_buffer_pool_qbuf (pool, buffer, group,
1565 NULL) != GST_FLOW_OK)
1566 pclass->release_buffer (bpool, buffer);
1568 /* Simply release invalid/modified buffer, the allocator will
1569 * give it back later */
1570 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1571 pclass->release_buffer (bpool, buffer);
1576 g_assert_not_reached ();
1581 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1582 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1583 switch (obj->mode) {
1584 case GST_V4L2_IO_RW:
1585 /* release back in the pool */
1586 pclass->release_buffer (bpool, buffer);
1589 case GST_V4L2_IO_MMAP:
1590 case GST_V4L2_IO_DMABUF:
1591 case GST_V4L2_IO_USERPTR:
1592 case GST_V4L2_IO_DMABUF_IMPORT:
1594 GstV4l2MemoryGroup *group;
1597 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1598 /* Simply release invalid/modified buffer, the allocator will
1599 * give it back later */
1600 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1601 pclass->release_buffer (bpool, buffer);
1605 index = group->buffer.index;
1608 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1611 /* Remove qdata, this will unmap any map data in userptr */
1612 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1613 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1615 /* reset to default size */
1616 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1618 /* playback, put the buffer back in the queue to refill later. */
1619 pclass->release_buffer (bpool, buffer);
1621 /* the buffer is queued in the device but maybe not played yet. We just
1622 * leave it there and not make it available for future calls to acquire
1623 * for now. The buffer will be dequeued and reused later. */
1624 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1630 g_assert_not_reached ();
1636 g_assert_not_reached ();
1642 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1644 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1645 GstV4l2MemoryGroup *group;
1646 gboolean queued = FALSE;
1648 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1649 gint old_buffer_state =
1650 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
1651 ~BUFFER_STATE_OUTSTANDING);
1652 queued = (old_buffer_state & BUFFER_STATE_QUEUED) != 0;
1653 GST_LOG_OBJECT (pool, "mark buffer %u not outstanding",
1654 group->buffer.index);
1657 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, queued);
1661 gst_v4l2_buffer_pool_dispose (GObject * object)
1663 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1665 if (pool->vallocator)
1666 gst_object_unref (pool->vallocator);
1667 pool->vallocator = NULL;
1669 if (pool->allocator)
1670 gst_object_unref (pool->allocator);
1671 pool->allocator = NULL;
1673 if (pool->other_pool)
1674 gst_object_unref (pool->other_pool);
1675 pool->other_pool = NULL;
1677 G_OBJECT_CLASS (parent_class)->dispose (object);
1681 gst_v4l2_buffer_pool_finalize (GObject * object)
1683 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1685 if (pool->video_fd >= 0)
1686 pool->obj->close (pool->video_fd);
1688 /* This can't be done in dispose method because we must not set pointer
1689 * to NULL as it is part of the v4l2object and dispose could be called
1691 gst_object_unref (pool->obj->element);
1693 g_cond_clear (&pool->empty_cond);
1695 /* FIXME have we done enough here ? */
1697 G_OBJECT_CLASS (parent_class)->finalize (object);
1701 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1703 g_cond_init (&pool->empty_cond);
1705 pool->orphaned = FALSE;
1706 for (gint i = 0; i < VIDEO_MAX_FRAME; i++)
1707 g_atomic_int_set (&pool->buffer_state[i], BUFFER_STATE_FREE);
1711 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1713 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1714 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1716 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1717 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1719 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1720 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1721 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1722 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1723 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1724 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1725 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1726 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1728 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1729 "V4L2 Buffer Pool");
1730 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
1734 * gst_v4l2_buffer_pool_new:
1735 * @obj: the v4l2 object owning the pool
1737 * Construct a new buffer pool.
1739 * Returns: the new pool, use gst_object_unref() to free resources
1742 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1744 GstV4l2BufferPool *pool;
1745 GstStructure *config;
1746 gchar *name, *parent_name;
1749 fd = obj->dup (obj->video_fd);
1753 /* setting a significant unique name */
1754 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1755 name = g_strdup_printf ("%s:pool%u:%s",
1756 parent_name, obj->pool_seq++,
1757 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src");
1758 g_free (parent_name);
1760 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1761 "name", name, NULL);
1762 g_object_ref_sink (pool);
1765 pool->video_fd = fd;
1768 pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
1769 if (pool->vallocator == NULL)
1770 goto allocator_failed;
1772 gst_object_ref (obj->element);
1774 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1775 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1776 /* This will simply set a default config, but will not configure the pool
1777 * because min and max are not valid */
1778 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1780 return GST_BUFFER_POOL (pool);
1785 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1790 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1791 gst_object_unref (pool);
1796 static GstFlowReturn
1797 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1800 GstV4l2Object *obj = pool->obj;
1805 toread = obj->info.size;
1807 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1809 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1812 if ((res = gst_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK)
1815 amount = obj->read (obj->video_fd, map.data, toread);
1817 if (amount == toread) {
1819 } else if (amount == -1) {
1820 if (errno == EAGAIN || errno == EINTR) {
1825 /* short reads can happen if a signal interrupts the read */
1830 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1831 gst_buffer_unmap (buf, &map);
1832 gst_buffer_resize (buf, 0, amount);
1839 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1844 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1845 (_("Error reading %d bytes from device '%s'."),
1846 toread, obj->videodev), GST_ERROR_SYSTEM);
1847 res = GST_FLOW_ERROR;
1852 gst_buffer_unmap (buf, &map);
1853 gst_buffer_resize (buf, 0, 0);
1859 * gst_v4l2_buffer_pool_process:
1860 * @bpool: a #GstBufferPool
1861 * @buf: a #GstBuffer, maybe be replaced
1862 * @frame_number: 32 bit frame number or %NULL
1864 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1865 * data from the device. For output devices, this functions send the contents of
1866 * @buf to the device for playback.
1868 * If non-%NULL and an output device, @frame_number is stored inside the timestamp for output devices and read
1869 * back from the timestamp for capture devices.
1871 * Returns: %GST_FLOW_OK on success.
1874 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf,
1875 guint32 * frame_number)
1877 GstFlowReturn ret = GST_FLOW_OK;
1878 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1879 GstV4l2Object *obj = pool->obj;
1881 GST_DEBUG_OBJECT (pool, "process buffer %p", *buf);
1883 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1884 return GST_FLOW_FLUSHING;
1886 switch (obj->type) {
1887 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1888 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1890 switch (obj->mode) {
1891 case GST_V4L2_IO_RW:
1892 /* capture into the buffer */
1893 ret = gst_v4l2_do_read (pool, *buf);
1896 case GST_V4L2_IO_MMAP:
1897 case GST_V4L2_IO_DMABUF:
1901 if ((*buf)->pool == bpool) {
1903 gsize size = gst_buffer_get_size (*buf);
1905 /* Legacy M2M devices return empty buffer when drained */
1906 if (size == 0 && GST_V4L2_IS_M2M (obj->device_caps)) {
1907 gst_v4l2_buffer_pool_resize_buffer (bpool, *buf);
1911 if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) !=
1912 GST_VIDEO_FORMAT_ENCODED && size < pool->size)
1913 goto buffer_truncated;
1915 num_queued = g_atomic_int_get (&pool->num_queued);
1916 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1919 /* If we have no more buffer, and can allocate it time to do so */
1920 if (num_queued == 0) {
1921 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1922 GST_DEBUG_OBJECT (pool, "Resurrect for empty queue");
1923 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1924 if (ret == GST_FLOW_OK || ret == GST_FLOW_FLUSHING)
1929 /* start copying buffers when we are running low on buffers */
1930 if (num_queued < pool->copy_threshold) {
1933 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1934 GST_DEBUG_OBJECT (pool, "Resurrect for threshold");
1935 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1936 if (ret == GST_FLOW_OK || ret == GST_FLOW_FLUSHING)
1940 /* copy the buffer */
1941 copy = gst_buffer_copy_region (*buf,
1942 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1943 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1945 /* and requeue so that we can continue capturing */
1946 gst_buffer_unref (*buf);
1951 /* nothing, data was inside the buffer when we did _acquire() */
1955 /* buffer not from our pool, grab a frame and copy it into the target */
1956 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp, NULL, TRUE))
1960 /* An empty buffer on capture indicates the end of stream */
1961 if (gst_buffer_get_size (tmp) == 0) {
1962 gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
1964 /* Legacy M2M devices return empty buffer when drained */
1965 if (GST_V4L2_IS_M2M (obj->device_caps)) {
1966 gst_v4l2_buffer_pool_resize_buffer (bpool, *buf);
1971 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1973 /* an queue the buffer again after the copy */
1974 gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
1976 if (ret != GST_FLOW_OK)
1981 case GST_V4L2_IO_USERPTR:
1983 struct UserPtrData *data;
1986 /* Replace our buffer with downstream allocated buffer */
1987 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1988 GST_V4L2_IMPORT_QUARK);
1989 tmp = gst_buffer_ref (data->buffer);
1990 _unmap_userptr_frame (data);
1992 /* Now tmp is writable, copy the flags and timestamp */
1993 gst_buffer_copy_into (tmp, *buf,
1994 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1996 gst_buffer_replace (buf, tmp);
1997 gst_buffer_unref (tmp);
2001 case GST_V4L2_IO_DMABUF_IMPORT:
2005 /* Replace our buffer with downstream allocated buffer */
2006 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
2007 GST_V4L2_IMPORT_QUARK);
2009 gst_buffer_copy_into (tmp, *buf,
2010 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2012 gst_buffer_replace (buf, tmp);
2013 gst_buffer_unref (tmp);
2018 g_assert_not_reached ();
2023 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2024 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
2026 switch (obj->mode) {
2027 case GST_V4L2_IO_RW:
2028 /* FIXME, do write() */
2029 GST_WARNING_OBJECT (pool, "implement write()");
2032 case GST_V4L2_IO_USERPTR:
2033 case GST_V4L2_IO_DMABUF_IMPORT:
2034 case GST_V4L2_IO_DMABUF:
2035 case GST_V4L2_IO_MMAP:
2037 GstBuffer *to_queue = NULL;
2039 GstV4l2MemoryGroup *group;
2041 gboolean outstanding;
2043 if ((*buf)->pool != bpool)
2046 if (!gst_v4l2_is_buffer_valid (*buf, &group))
2049 index = group->buffer.index;
2051 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
2053 if (g_atomic_int_get (&pool->buffer_state[index]) &
2054 BUFFER_STATE_QUEUED) {
2055 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
2059 /* we can queue directly */
2060 to_queue = gst_buffer_ref (*buf);
2063 if (to_queue == NULL) {
2064 GstBufferPoolAcquireParams params = { 0 };
2066 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
2068 /* this can return EOS if all buffers are outstanding which would
2069 * be strange because we would expect the upstream element to have
2070 * allocated them and returned to us.. */
2071 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
2072 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
2073 if (ret != GST_FLOW_OK)
2074 goto acquire_failed;
2076 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
2077 if (ret != GST_FLOW_OK) {
2078 gst_buffer_unref (to_queue);
2079 goto prepare_failed;
2082 /* retrieve the group */
2083 gst_v4l2_is_buffer_valid (to_queue, &group);
2087 gst_v4l2_buffer_pool_qbuf (pool, to_queue, group,
2092 /* if we are not streaming yet (this is the first buffer, start
2094 if (!gst_v4l2_buffer_pool_streamon (pool)) {
2095 /* don't check return value because qbuf would have failed */
2096 gst_v4l2_is_buffer_valid (to_queue, &group);
2098 /* qbuf has stored to_queue buffer but we are not in
2099 * streaming state, so the flush logic won't be performed.
2100 * To avoid leaks, flush the allocator and restore the queued
2101 * buffer as non-queued */
2102 gst_v4l2_allocator_flush (pool->vallocator);
2104 pool->buffers[group->buffer.index] = NULL;
2105 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
2106 ~BUFFER_STATE_QUEUED);
2108 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
2109 GST_V4L2_IMPORT_QUARK, NULL, NULL);
2110 gst_buffer_unref (to_queue);
2111 g_atomic_int_add (&pool->num_queued, -1);
2115 /* Remove our ref, we will still hold this buffer in acquire as needed,
2116 * otherwise the pool will think it is outstanding and will refuse to stop. */
2117 gst_buffer_unref (to_queue);
2119 /* release as many buffer as possible */
2120 while (gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding,
2121 FALSE) == GST_FLOW_OK) {
2123 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
2127 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
2128 /* all buffers are queued, try to dequeue one and release it back
2129 * into the pool so that _acquire can get to it again. */
2131 gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding, TRUE);
2132 if (ret == GST_FLOW_OK && !outstanding)
2133 /* release the rendered buffer back into the pool. This wakes up any
2134 * thread waiting for a buffer in _acquire(). */
2135 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
2141 g_assert_not_reached ();
2146 g_assert_not_reached ();
2155 GST_ERROR_OBJECT (pool, "failed to copy buffer");
2160 GST_WARNING_OBJECT (pool,
2161 "Dropping truncated buffer, this is likely a driver bug.");
2162 gst_buffer_unref (*buf);
2164 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
2168 GST_DEBUG_OBJECT (pool, "end of sequence reached");
2169 gst_buffer_unref (*buf);
2171 return GST_V4L2_FLOW_LAST_BUFFER;
2175 if (ret == GST_FLOW_FLUSHING)
2176 GST_DEBUG_OBJECT (pool, "flushing");
2178 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
2179 gst_flow_get_name (ret));
2184 GST_ERROR_OBJECT (pool, "failed to prepare data");
2189 GST_ERROR_OBJECT (pool, "failed to queue buffer");
2194 GST_ERROR_OBJECT (pool, "failed to start streaming");
2195 return GST_FLOW_ERROR;
2200 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
2201 GstBufferPool * other_pool)
2203 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
2205 if (pool->other_pool)
2206 gst_object_unref (pool->other_pool);
2207 pool->other_pool = gst_object_ref (other_pool);
2211 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
2213 GST_OBJECT_LOCK (pool);
2214 pool->enable_copy_threshold = copy;
2215 GST_OBJECT_UNLOCK (pool);
2218 static GstFlowReturn
2219 gst_v4l2_buffer_pool_flush_events (GstV4l2Object * v4l2object)
2221 GstFlowReturn ret = GST_FLOW_OK;
2222 gboolean event_found;
2224 /* FIXME simplify this when we drop legacy support for driver without poll()
2225 * support. When we do, we can switch the video_fd to non blocking, and just
2226 * pop the events directly. */
2229 struct v4l2_event event = { 0, };
2232 event_found = FALSE;
2234 gst_poll_set_flushing (v4l2object->poll, FALSE);
2237 /* GstPoll don't have 0ns timeout, but 1 will do */
2238 poll_ret = gst_poll_wait (v4l2object->poll, 1);
2239 } while (poll_ret == EAGAIN || poll_ret == EINTR);
2241 if (gst_poll_fd_has_pri (v4l2object->poll, &v4l2object->pollfd)) {
2242 if (!gst_v4l2_dequeue_event (v4l2object, &event))
2243 return GST_FLOW_ERROR;
2247 if (event.type == V4L2_EVENT_SOURCE_CHANGE &&
2248 (event.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION)) {
2249 GST_DEBUG_OBJECT (v4l2object->dbg_obj,
2250 "Can't streamon capture as the resolution have changed.");
2251 ret = GST_V4L2_FLOW_RESOLUTION_CHANGE;
2254 } while (event_found);
2260 gst_v4l2_buffer_pool_flush (GstV4l2Object * v4l2object)
2262 GstBufferPool *bpool = gst_v4l2_object_get_buffer_pool (v4l2object);
2263 GstV4l2BufferPool *pool;
2264 GstFlowReturn ret = GST_FLOW_OK;
2267 return GST_FLOW_ERROR;
2269 pool = GST_V4L2_BUFFER_POOL (bpool);
2271 gst_v4l2_buffer_pool_streamoff (pool);
2273 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
2274 ret = gst_v4l2_buffer_pool_flush_events (v4l2object);
2276 /* If the format haven't change, avoid reallocation to go back to
2278 if (ret == GST_FLOW_OK)
2279 ret = gst_v4l2_buffer_pool_streamon (pool);
2282 gst_object_unref (bpool);
2287 * gst_v4l2_buffer_pool_enable_resolution_change:
2288 * @pool: a #GstBufferPool
2290 * When this is called, the pool will subscribe to the
2291 * %V4L2_EVENT_SOURCE_CHANGE. Upon receiving this event, it will notify
2292 * the element acquiring buffer with the special flow return
2293 * %GST_V4L2_FLOW_RESOLUTION_CHANGE.
2296 gst_v4l2_buffer_pool_enable_resolution_change (GstV4l2BufferPool * pool)
2298 gst_v4l2_object_subscribe_event (pool->obj, V4L2_EVENT_SOURCE_CHANGE);