3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
5 * 2009 Texas Instruments, Inc - http://www.ti.com/
7 * gstv4l2bufferpool.c V4L2 buffer pool class
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
30 # define _GNU_SOURCE /* O_CLOEXEC */
38 #include "gst/video/video.h"
39 #include "gst/video/gstvideometa.h"
40 #include "gst/video/gstvideopool.h"
41 #include "gst/allocators/gstdmabuf.h"
43 #include <gstv4l2bufferpool.h>
45 #include "gstv4l2object.h"
46 #include "gst/gst-i18n-plugin.h"
47 #include <gst/glib-compat-private.h>
49 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
50 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2bufferpool_debug
53 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
59 #define gst_v4l2_buffer_pool_parent_class parent_class
60 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
62 enum _GstV4l2BufferPoolAcquireFlags
64 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
65 GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
66 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
69 /* 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_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
128 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
130 GST_LOG_OBJECT (pool, "copying buffer");
132 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
133 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
134 GstVideoFrame src_frame, dest_frame;
136 GST_DEBUG_OBJECT (pool, "copy video frame");
138 /* we have raw video, use videoframe copy to get strides right */
139 if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
142 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
144 gst_video_frame_unmap (&src_frame);
148 gst_video_frame_copy (&dest_frame, &src_frame);
150 gst_video_frame_unmap (&src_frame);
151 gst_video_frame_unmap (&dest_frame);
155 GST_DEBUG_OBJECT (pool, "copy raw bytes");
157 if (!gst_buffer_map (src, &map, GST_MAP_READ))
160 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
162 gst_buffer_unmap (src, &map);
163 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
166 gst_buffer_copy_into (dest, src,
167 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
169 GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest);
175 GST_ERROR_OBJECT (pool, "could not map buffer");
176 return GST_FLOW_ERROR;
189 gst_v4l2_buffer_pool_import_quark (void)
191 static GQuark quark = 0;
194 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
200 _unmap_userptr_frame (struct UserPtrData *data)
203 gst_video_frame_unmap (&data->frame);
205 gst_buffer_unmap (data->buffer, &data->map);
208 gst_buffer_unref (data->buffer);
210 g_slice_free (struct UserPtrData, data);
214 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
215 GstBuffer * dest, GstBuffer * src)
217 GstFlowReturn ret = GST_FLOW_OK;
218 GstV4l2MemoryGroup *group = NULL;
220 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
221 struct UserPtrData *data = NULL;
223 GST_LOG_OBJECT (pool, "importing userptr");
226 if (!gst_v4l2_is_buffer_valid (dest, &group))
229 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
230 flags = GST_MAP_READ;
232 flags = GST_MAP_WRITE;
234 data = g_slice_new0 (struct UserPtrData);
236 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
237 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
238 gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
241 data->is_frame = TRUE;
243 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
246 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
247 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
248 gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
252 pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
253 GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
255 pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
256 GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
258 size[i] = pstride * pheight;
260 size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
261 GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
265 /* In the single planar API, planes must be contiguous in memory and
266 * therefore they must have expected size. ie: no padding.
267 * To check these conditions, we check that plane 'i' start address
268 * + plane 'i' size equals to plane 'i+1' start address */
269 if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
270 for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
271 const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
273 gint estride = gst_video_format_info_extrapolate_stride (finfo, i,
274 pix_fmt->bytesperline);
275 guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
278 tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
279 if (tmp != data->frame.data[i + 1])
280 goto non_contiguous_mem;
284 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
285 data->frame.info.size, finfo->n_planes, data->frame.data, size))
291 data->is_frame = FALSE;
293 if (!gst_buffer_map (src, &data->map, flags))
296 ptr[0] = data->map.data;
297 size[0] = data->map.size;
299 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
300 data->map.size, 1, ptr, size))
304 data->buffer = gst_buffer_ref (src);
306 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
307 data, (GDestroyNotify) _unmap_userptr_frame);
309 gst_buffer_copy_into (dest, src,
310 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
316 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
317 return GST_FLOW_ERROR;
321 GST_ERROR_OBJECT (pool, "could not map buffer");
322 g_slice_free (struct UserPtrData, data);
323 return GST_FLOW_ERROR;
327 GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
328 _unmap_userptr_frame (data);
329 return GST_FLOW_ERROR;
333 GST_ERROR_OBJECT (pool, "failed to import data");
334 _unmap_userptr_frame (data);
335 return GST_FLOW_ERROR;
340 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
341 GstBuffer * dest, GstBuffer * src)
343 GstV4l2MemoryGroup *group = NULL;
344 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
345 guint n_mem = gst_buffer_n_memory (src);
348 GST_LOG_OBJECT (pool, "importing dmabuf");
350 if (!gst_v4l2_is_buffer_valid (dest, &group))
353 if (n_mem > GST_VIDEO_MAX_PLANES)
356 for (i = 0; i < n_mem; i++)
357 dma_mem[i] = gst_buffer_peek_memory (src, i);
359 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
363 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
364 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
366 gst_buffer_copy_into (dest, src,
367 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
373 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
374 return GST_FLOW_ERROR;
378 GST_ERROR_OBJECT (pool, "could not map buffer");
379 return GST_FLOW_ERROR;
383 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
384 return GST_FLOW_ERROR;
389 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
390 GstBuffer * dest, GstBuffer * src)
392 GstFlowReturn ret = GST_FLOW_OK;
393 gboolean own_src = FALSE;
396 if (pool->other_pool == NULL) {
397 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
398 return GST_FLOW_ERROR;
401 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
402 if (ret != GST_FLOW_OK) {
403 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
410 switch (pool->obj->mode) {
411 case GST_V4L2_IO_MMAP:
412 case GST_V4L2_IO_DMABUF:
413 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
415 case GST_V4L2_IO_USERPTR:
416 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
418 case GST_V4L2_IO_DMABUF_IMPORT:
419 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
426 gst_buffer_unref (src);
433 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
434 GstBufferPoolAcquireParams * params)
436 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
437 GstV4l2MemoryGroup *group = NULL;
438 GstBuffer *newbuf = NULL;
448 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
450 case GST_V4L2_IO_MMAP:
451 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
453 case GST_V4L2_IO_DMABUF:
454 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
457 case GST_V4L2_IO_USERPTR:
458 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
460 case GST_V4L2_IO_DMABUF_IMPORT:
461 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
465 g_assert_not_reached ();
471 newbuf = gst_buffer_new ();
473 for (i = 0; i < group->n_mem; i++)
474 gst_buffer_append_memory (newbuf, group->mem[i]);
476 if (g_atomic_int_get (&pool->buffer_state[group->buffer.index])) {
477 GST_WARNING_OBJECT (pool, "newly allocated buffer %u is not free",
478 group->buffer.index);
480 } else if (newbuf == NULL) {
481 goto allocation_failed;
484 /* add metadata to raw video buffers */
485 if (pool->add_videometa) {
486 GstVideoMeta *videometa =
487 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
488 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
489 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
490 info->offset, info->stride);
492 gst_video_meta_set_alignment (videometa, obj->align);
502 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
503 return GST_FLOW_ERROR;
508 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
510 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
511 GstV4l2Object *obj = pool->obj;
513 guint size, min_buffers, max_buffers;
514 GstAllocator *allocator;
515 GstAllocationParams params;
516 gboolean can_allocate = FALSE;
517 gboolean updated = FALSE;
520 pool->add_videometa =
521 gst_buffer_pool_config_has_option (config,
522 GST_BUFFER_POOL_OPTION_VIDEO_META);
524 /* parse the config and keep around */
525 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
529 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
532 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
535 gst_object_unref (pool->allocator);
536 pool->allocator = NULL;
539 case GST_V4L2_IO_DMABUF:
540 pool->allocator = gst_dmabuf_allocator_new ();
541 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
543 case GST_V4L2_IO_MMAP:
544 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
546 case GST_V4L2_IO_USERPTR:
548 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
550 case GST_V4L2_IO_DMABUF_IMPORT:
551 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
555 pool->allocator = g_object_ref (allocator);
556 pool->params = params;
557 /* No need to change the configuration */
561 g_assert_not_reached ();
565 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
566 * instability and crash, disable it for now */
567 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
568 GST_WARNING_OBJECT (pool,
569 "libv4l2 converter detected, disabling CREATE_BUFS");
570 can_allocate = FALSE;
571 GST_OBJECT_FLAG_UNSET (pool->vallocator,
572 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
573 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
574 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
577 if (min_buffers < GST_V4L2_MIN_BUFFERS (obj)) {
579 min_buffers = GST_V4L2_MIN_BUFFERS (obj);
580 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
583 /* respect driver requirements */
584 if (min_buffers < obj->min_buffers) {
586 min_buffers = obj->min_buffers;
587 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
590 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
592 max_buffers = VIDEO_MAX_FRAME;
593 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
596 if (min_buffers > max_buffers) {
598 min_buffers = max_buffers;
599 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
600 } else if (min_buffers != max_buffers) {
603 max_buffers = min_buffers;
604 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
608 if (!pool->add_videometa && obj->need_video_meta) {
609 GST_INFO_OBJECT (pool, "adding needed video meta");
611 gst_buffer_pool_config_add_option (config,
612 GST_BUFFER_POOL_OPTION_VIDEO_META);
615 /* Always update the config to ensure the configured size matches */
616 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
619 /* keep a GstVideoInfo with defaults for the when we need to copy */
620 gst_video_info_from_caps (&pool->caps_info, caps);
623 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
625 /* If anything was changed documentation recommend to return FALSE */
626 return !updated && ret;
631 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
637 gst_v4l2_buffer_pool_resurrect_buffer (GstV4l2BufferPool * pool)
639 GstBufferPoolAcquireParams params = { 0 };
640 GstBuffer *buffer = NULL;
643 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
645 /* block recursive calls to this function */
646 g_signal_handler_block (pool->vallocator, pool->group_released_handler);
649 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT |
650 GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
652 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
654 if (ret == GST_FLOW_OK)
655 gst_buffer_unref (buffer);
657 g_signal_handler_unblock (pool->vallocator, pool->group_released_handler);
663 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
665 GstV4l2Object *obj = pool->obj;
671 case GST_V4L2_IO_MMAP:
672 case GST_V4L2_IO_USERPTR:
673 case GST_V4L2_IO_DMABUF:
674 case GST_V4L2_IO_DMABUF_IMPORT:
675 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
679 num_queued = g_atomic_int_get (&pool->num_queued);
680 if (num_queued < pool->num_allocated)
681 n = pool->num_allocated - num_queued;
683 /* For captures, we need to enqueue buffers before we start streaming,
684 * so the driver don't underflow immediately. As we have put then back
685 * into the base class queue, resurrect them, then releasing will queue
687 for (i = 0; i < n; i++)
688 gst_v4l2_buffer_pool_resurrect_buffer (pool);
691 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
692 goto streamon_failed;
694 pool->streaming = TRUE;
696 GST_DEBUG_OBJECT (pool, "Started streaming");
706 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
712 /* Call with streamlock held, or when streaming threads are down */
714 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
716 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
717 GstV4l2Object *obj = pool->obj;
720 if (!pool->streaming)
724 case GST_V4L2_IO_MMAP:
725 case GST_V4L2_IO_USERPTR:
726 case GST_V4L2_IO_DMABUF:
727 case GST_V4L2_IO_DMABUF_IMPORT:
729 if (obj->ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
730 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
731 errno, g_strerror (errno));
733 pool->streaming = FALSE;
735 GST_DEBUG_OBJECT (pool, "Stopped streaming");
737 if (pool->vallocator)
738 gst_v4l2_allocator_flush (pool->vallocator);
744 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
745 gint old_buffer_state =
746 g_atomic_int_and (&pool->buffer_state[i], ~BUFFER_STATE_QUEUED);
747 if ((old_buffer_state & BUFFER_STATE_QUEUED) && pool->buffers[i]) {
748 GstBuffer *buffer = pool->buffers[i];
749 GstBufferPool *bpool = GST_BUFFER_POOL (pool);
751 pool->buffers[i] = NULL;
753 if (!(old_buffer_state & BUFFER_STATE_OUTSTANDING)) {
754 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
755 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, FALSE);
757 else /* Don't re-enqueue capture buffer on stop */
758 pclass->release_buffer (bpool, buffer);
761 g_atomic_int_add (&pool->num_queued, -1);
767 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
769 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
770 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
771 GstV4l2Object *obj = pool->obj;
772 GstStructure *config;
774 guint size, min_buffers, max_buffers;
775 guint max_latency, min_latency, copy_threshold = 0;
776 gboolean can_allocate = FALSE, ret = TRUE;
778 GST_DEBUG_OBJECT (pool, "activating pool");
780 if (pool->other_pool) {
783 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
784 goto other_pool_failed;
786 if (gst_buffer_pool_acquire_buffer (pool->other_pool, &buffer, NULL) !=
788 goto other_pool_failed;
790 if (!gst_v4l2_object_try_import (obj, buffer)) {
791 gst_buffer_unref (buffer);
794 gst_buffer_unref (buffer);
797 config = gst_buffer_pool_get_config (bpool);
798 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
802 min_latency = MAX (GST_V4L2_MIN_BUFFERS (obj), obj->min_buffers);
808 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
809 * of MMAP. In this case, the first read initialize the queues, but the
810 * poll before that will always fail. Doing an empty read, forces the
811 * queue to be initialized now. We only do this if we have a streaming
813 if (obj->device_caps & V4L2_CAP_STREAMING)
814 obj->read (obj->video_fd, NULL, 0);
817 case GST_V4L2_IO_DMABUF:
818 case GST_V4L2_IO_MMAP:
822 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
824 /* first, lets request buffers, and see how many we can get: */
825 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
827 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
829 pool->num_allocated = count;
831 if (count < GST_V4L2_MIN_BUFFERS (obj)) {
836 /* V4L2 buffer pool are often very limited in the amount of buffers it
837 * can offer. The copy_threshold will workaround this limitation by
838 * falling back to copy if the pipeline needed more buffers. This also
839 * prevent having to do REQBUFS(N)/REQBUFS(0) every time configure is
841 if (count != min_buffers || pool->enable_copy_threshold) {
842 GST_WARNING_OBJECT (pool,
843 "Uncertain or not enough buffers, enabling copy threshold");
845 copy_threshold = min_latency;
850 case GST_V4L2_IO_USERPTR:
855 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
857 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
859 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
860 V4L2_MEMORY_USERPTR);
861 pool->num_allocated = count;
863 /* There is no rational to not get what we asked */
864 if (count < min_buffers) {
872 case GST_V4L2_IO_DMABUF_IMPORT:
876 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
878 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
880 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
882 pool->num_allocated = count;
884 /* There is no rational to not get what we asked */
885 if (count < min_buffers) {
896 g_assert_not_reached ();
901 max_latency = max_buffers;
903 max_latency = min_buffers;
906 pool->copy_threshold = copy_threshold;
907 pool->max_latency = max_latency;
908 pool->min_latency = min_latency;
909 pool->num_queued = 0;
911 if (max_buffers != 0 && max_buffers < min_buffers)
912 max_buffers = min_buffers;
914 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
916 pclass->set_config (bpool, config);
917 gst_structure_free (config);
919 /* now, allocate the buffers: */
920 if (!pclass->start (bpool))
923 if (!V4L2_TYPE_IS_OUTPUT (obj->type)) {
924 if (g_atomic_int_get (&pool->num_queued) < pool->num_allocated)
927 pool->group_released_handler =
928 g_signal_connect_swapped (pool->vallocator, "group-released",
929 G_CALLBACK (gst_v4l2_buffer_pool_resurrect_buffer), pool);
930 ret = gst_v4l2_buffer_pool_streamon (pool);
938 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
939 gst_structure_free (config);
944 GST_ERROR_OBJECT (pool,
945 "we received %d buffer from device '%s', we want at least %d",
946 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS (obj));
947 gst_structure_free (config);
952 GST_ERROR_OBJECT (pool, "allocate failed");
957 GST_ERROR_OBJECT (pool, "failed to activate the other pool %"
958 GST_PTR_FORMAT, pool->other_pool);
963 GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue");
968 GST_ERROR_OBJECT (pool, "cannot import buffers from downstream pool");
974 gst_v4l2_buffer_pool_vallocator_stop (GstV4l2BufferPool * pool)
978 if (!pool->vallocator)
981 vret = gst_v4l2_allocator_stop (pool->vallocator);
983 if (vret == GST_V4L2_BUSY)
984 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
986 return (vret == GST_V4L2_OK);
990 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
992 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
995 GST_DEBUG_OBJECT (pool, "stopping pool");
997 if (pool->group_released_handler > 0) {
998 g_signal_handler_disconnect (pool->vallocator,
999 pool->group_released_handler);
1000 pool->group_released_handler = 0;
1003 if (pool->other_pool) {
1004 gst_buffer_pool_set_active (pool->other_pool, FALSE);
1005 gst_object_unref (pool->other_pool);
1006 pool->other_pool = NULL;
1009 if (!pool->orphaned)
1010 gst_v4l2_buffer_pool_streamoff (pool);
1012 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
1015 ret = gst_v4l2_buffer_pool_vallocator_stop (pool);
1021 gst_v4l2_buffer_pool_orphan (GstBufferPool ** bpool)
1023 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (*bpool);
1026 g_return_val_if_fail (pool->orphaned == FALSE, FALSE);
1028 if (!GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS (pool->vallocator))
1031 if (g_getenv ("GST_V4L2_FORCE_DRAIN"))
1034 GST_DEBUG_OBJECT (pool, "orphaning pool");
1035 gst_buffer_pool_set_active (*bpool, FALSE);
1037 /* We lock to prevent racing with a return buffer in QBuf, and has a
1038 * workaround of not being able to use the pool hidden activation lock. */
1039 GST_OBJECT_LOCK (pool);
1041 gst_v4l2_buffer_pool_streamoff (pool);
1042 ret = gst_v4l2_allocator_orphan (pool->vallocator);
1044 pool->orphaned = TRUE;
1046 GST_OBJECT_UNLOCK (pool);
1049 gst_object_unref (*bpool);
1057 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
1059 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1061 GST_DEBUG_OBJECT (pool, "start flushing");
1063 gst_poll_set_flushing (pool->poll, TRUE);
1065 GST_OBJECT_LOCK (pool);
1066 pool->empty = FALSE;
1067 g_cond_broadcast (&pool->empty_cond);
1068 GST_OBJECT_UNLOCK (pool);
1070 if (pool->other_pool && gst_buffer_pool_is_active (pool->other_pool))
1071 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
1075 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
1077 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1079 GST_DEBUG_OBJECT (pool, "stop flushing");
1081 if (pool->other_pool && gst_buffer_pool_is_active (pool->other_pool))
1082 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
1084 gst_poll_set_flushing (pool->poll, FALSE);
1087 static GstFlowReturn
1088 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool, gboolean wait)
1091 GstClockTime timeout;
1094 timeout = GST_CLOCK_TIME_NONE;
1098 /* In RW mode there is no queue, hence no need to wait while the queue is
1100 if (pool->obj->mode != GST_V4L2_IO_RW) {
1101 GST_OBJECT_LOCK (pool);
1103 if (!wait && pool->empty) {
1104 GST_OBJECT_UNLOCK (pool);
1109 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1111 GST_OBJECT_UNLOCK (pool);
1114 if (!pool->can_poll_device) {
1121 GST_LOG_OBJECT (pool, "polling device");
1124 ret = gst_poll_wait (pool->poll, timeout);
1125 if (G_UNLIKELY (ret < 0)) {
1133 GST_WARNING_OBJECT (pool,
1134 "v4l2 device doesn't support polling. Disabling"
1135 " using libv4l2 in this case may cause deadlocks");
1136 pool->can_poll_device = FALSE;
1143 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1146 /* PRI is used to signal that events are available */
1147 if (gst_poll_fd_has_pri (pool->poll, &pool->pollfd)) {
1148 struct v4l2_event event = { 0, };
1150 if (!gst_v4l2_dequeue_event (pool->obj, &event))
1151 goto dqevent_failed;
1153 if (event.type != V4L2_EVENT_SOURCE_CHANGE) {
1154 GST_INFO_OBJECT (pool, "Received unhandled event, ignoring.");
1158 if ((event.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION) == 0) {
1159 GST_DEBUG_OBJECT (pool,
1160 "Received non-resolution source-change, ignoring.");
1164 return GST_V4L2_FLOW_RESOLUTION_CHANGE;
1176 GST_DEBUG_OBJECT (pool, "stop called");
1177 return GST_FLOW_FLUSHING;
1181 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1182 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1183 return GST_FLOW_ERROR;
1187 return GST_V4L2_FLOW_LAST_BUFFER;
1191 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1192 ("dqevent error: %s (%d)", g_strerror (errno), errno));
1193 return GST_FLOW_ERROR;
1197 static GstFlowReturn
1198 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf,
1199 GstV4l2MemoryGroup * group, guint32 * frame_number)
1201 const GstV4l2Object *obj = pool->obj;
1202 gint old_buffer_state;
1205 index = group->buffer.index;
1208 g_atomic_int_or (&pool->buffer_state[index], BUFFER_STATE_QUEUED);
1209 if (old_buffer_state & BUFFER_STATE_QUEUED)
1210 goto already_queued;
1212 GST_LOG_OBJECT (pool, "queuing buffer %i, previous-state = %i", index,
1215 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1216 enum v4l2_field field;
1218 /* Buffer field is the same as the one defined in format */
1219 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1220 field = obj->format.fmt.pix_mp.field;
1222 field = obj->format.fmt.pix.field;
1224 group->buffer.field = field;
1228 group->buffer.timestamp.tv_sec = *frame_number;
1229 group->buffer.timestamp.tv_usec = 0;
1231 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1232 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
1233 GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp);
1235 group->buffer.timestamp.tv_sec = -1;
1236 group->buffer.timestamp.tv_usec = -1;
1240 GST_OBJECT_LOCK (pool);
1242 /* If the pool was orphaned, don't try to queue any returned buffers.
1243 * This is done with the objet lock in order to synchronize with
1248 g_atomic_int_inc (&pool->num_queued);
1249 pool->buffers[index] = buf;
1251 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1254 pool->empty = FALSE;
1255 g_cond_signal (&pool->empty_cond);
1256 GST_OBJECT_UNLOCK (pool);
1262 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1263 return GST_FLOW_ERROR;
1267 GST_DEBUG_OBJECT (pool, "pool was orphaned, not queuing back buffer.");
1268 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1269 g_atomic_int_and (&pool->buffer_state[index], ~BUFFER_STATE_QUEUED);
1270 GST_OBJECT_UNLOCK (pool);
1271 return GST_FLOW_FLUSHING;
1275 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1276 /* Mark broken buffer to the allocator */
1277 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1278 g_atomic_int_add (&pool->num_queued, -1);
1279 pool->buffers[index] = NULL;
1280 g_atomic_int_and (&pool->buffer_state[index], ~BUFFER_STATE_QUEUED);
1281 GST_OBJECT_UNLOCK (pool);
1282 return GST_FLOW_ERROR;
1286 static GstFlowReturn
1287 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
1288 gboolean * outstanding, gboolean wait)
1291 GstBuffer *outbuf = NULL;
1292 GstV4l2Object *obj = pool->obj;
1293 GstClockTime timestamp;
1294 GstV4l2MemoryGroup *group;
1295 GstVideoMeta *vmeta;
1298 gint old_buffer_state;
1300 if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
1303 if (res == GST_V4L2_FLOW_LAST_BUFFER) {
1304 GST_LOG_OBJECT (pool, "nothing to dequeue");
1308 if (res == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
1309 GST_INFO_OBJECT (pool, "Resolution change detected.");
1313 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1315 res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
1316 if (res == GST_FLOW_EOS)
1318 if (res != GST_FLOW_OK)
1322 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
1323 ~BUFFER_STATE_QUEUED);
1324 if (!(old_buffer_state & BUFFER_STATE_QUEUED))
1328 *outstanding = (old_buffer_state & BUFFER_STATE_OUTSTANDING) != 0;
1329 } else if (old_buffer_state & BUFFER_STATE_OUTSTANDING) {
1330 GST_WARNING_OBJECT (pool, "unexpected outstanding buffer %u",
1331 group->buffer.index);
1334 outbuf = pool->buffers[group->buffer.index];
1338 pool->buffers[group->buffer.index] = NULL;
1339 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1340 GST_OBJECT_LOCK (pool);
1342 GST_OBJECT_UNLOCK (pool);
1345 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1348 vmeta = gst_buffer_get_video_meta (outbuf);
1349 for (i = 0; i < group->n_mem; i++) {
1350 GST_LOG_OBJECT (pool,
1351 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1352 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p, previous-state=%i",
1353 outbuf, group->buffer.sequence, group->buffer.index, group->mem[i],
1354 group->planes[i].bytesused, i, group->buffer.flags,
1355 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf, old_buffer_state);
1358 vmeta->offset[i] = size;
1359 size += gst_memory_get_sizes (group->mem[i], NULL, NULL);
1363 /* Ignore timestamp and field for OUTPUT device */
1364 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1367 /* Check for driver bug in reporting feild */
1368 if (group->buffer.field == V4L2_FIELD_ANY) {
1369 /* Only warn once to avoid the spamming */
1370 #ifndef GST_DISABLE_GST_DEBUG
1371 if (!pool->has_warned_on_buggy_field) {
1372 pool->has_warned_on_buggy_field = TRUE;
1373 GST_WARNING_OBJECT (pool,
1374 "Driver should never set v4l2_buffer.field to ANY");
1378 /* Use the value from the format (works for UVC bug) */
1379 group->buffer.field = obj->format.fmt.pix.field;
1381 /* If driver also has buggy S_FMT, assume progressive */
1382 if (group->buffer.field == V4L2_FIELD_ANY) {
1383 #ifndef GST_DISABLE_GST_DEBUG
1384 if (!pool->has_warned_on_buggy_field) {
1385 pool->has_warned_on_buggy_field = TRUE;
1386 GST_WARNING_OBJECT (pool,
1387 "Driver should never set v4l2_format.pix.field to ANY");
1391 group->buffer.field = V4L2_FIELD_NONE;
1395 /* set top/bottom field first if v4l2_buffer has the information */
1396 switch (group->buffer.field) {
1397 case V4L2_FIELD_NONE:
1398 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1399 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1401 case V4L2_FIELD_TOP:
1402 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1403 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1405 case V4L2_FIELD_BOTTOM:
1406 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1407 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1409 case V4L2_FIELD_INTERLACED_TB:
1410 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1411 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1413 case V4L2_FIELD_INTERLACED_BT:
1414 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1415 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1417 case V4L2_FIELD_INTERLACED:
1418 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1419 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1420 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1421 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1422 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1424 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1428 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1429 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1430 GST_FIXME_OBJECT (pool,
1431 "Unhandled enum v4l2_field %d - treating as progressive",
1432 group->buffer.field);
1436 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1437 if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) ||
1438 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG ||
1439 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG ||
1440 GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG)
1441 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1443 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1446 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1447 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1449 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1450 GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
1451 GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
1461 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1466 return GST_FLOW_EOS;
1470 return GST_FLOW_ERROR;
1474 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1475 group->buffer.index);
1476 return GST_FLOW_ERROR;
1480 static GstFlowReturn
1481 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1482 GstBufferPoolAcquireParams * params)
1485 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1486 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1487 GstV4l2Object *obj = pool->obj;
1489 GST_DEBUG_OBJECT (pool, "acquire");
1491 /* If this is being called to resurrect a lost buffer */
1492 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1493 ret = pclass->acquire_buffer (bpool, buffer, params);
1497 switch (obj->type) {
1498 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1499 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1500 /* capture, This function should return a buffer with new captured data */
1501 switch (obj->mode) {
1502 case GST_V4L2_IO_RW:
1504 /* take empty buffer from the pool */
1505 ret = pclass->acquire_buffer (bpool, buffer, params);
1508 case GST_V4L2_IO_DMABUF:
1509 case GST_V4L2_IO_MMAP:
1510 case GST_V4L2_IO_USERPTR:
1511 case GST_V4L2_IO_DMABUF_IMPORT:
1513 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1514 * storage for our buffers. This function does poll first so we can
1515 * interrupt it fine. */
1516 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer, NULL, TRUE);
1520 ret = GST_FLOW_ERROR;
1521 g_assert_not_reached ();
1527 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1528 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1529 /* playback, This function should return an empty buffer */
1530 switch (obj->mode) {
1531 case GST_V4L2_IO_RW:
1532 /* get an empty buffer */
1533 ret = pclass->acquire_buffer (bpool, buffer, params);
1536 case GST_V4L2_IO_MMAP:
1537 case GST_V4L2_IO_DMABUF:
1538 case GST_V4L2_IO_USERPTR:
1539 case GST_V4L2_IO_DMABUF_IMPORT:
1540 /* get a free unqueued buffer */
1541 ret = pclass->acquire_buffer (bpool, buffer, params);
1545 ret = GST_FLOW_ERROR;
1546 g_assert_not_reached ();
1552 ret = GST_FLOW_ERROR;
1553 g_assert_not_reached ();
1557 /* Mark buffer as outstanding */
1558 if (ret == GST_FLOW_OK) {
1559 GstV4l2MemoryGroup *group;
1560 if (gst_v4l2_is_buffer_valid (*buffer, &group)) {
1561 GST_LOG_OBJECT (pool, "mark buffer %u outstanding", group->buffer.index);
1562 g_atomic_int_or (&pool->buffer_state[group->buffer.index],
1563 BUFFER_STATE_OUTSTANDING);
1571 * Completes a release buffer operation
1573 * Before calling this function:
1574 * - The buffer state (if applicable) must have already been updated.
1575 * - The buffer must not be outstanding.
1576 * - The "queued" argument contains whether the buffer is currently queued.
1579 gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
1580 GstBuffer * buffer, gboolean queued)
1582 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1583 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1584 GstV4l2Object *obj = pool->obj;
1586 GST_DEBUG_OBJECT (pool, "complete release buffer %p (queued = %s)", buffer,
1587 queued ? "yes" : "no");
1589 switch (obj->type) {
1590 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1591 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1592 /* capture, put the buffer back in the queue so that we can refill it
1595 GST_WARNING_OBJECT (pool,
1596 "capture buffer %p was release while still queued", buffer);
1599 switch (obj->mode) {
1600 case GST_V4L2_IO_RW:
1601 /* release back in the pool */
1602 pclass->release_buffer (bpool, buffer);
1605 case GST_V4L2_IO_DMABUF:
1606 case GST_V4L2_IO_MMAP:
1607 case GST_V4L2_IO_USERPTR:
1608 case GST_V4L2_IO_DMABUF_IMPORT:
1610 GstV4l2MemoryGroup *group;
1611 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1612 GstFlowReturn ret = GST_FLOW_OK;
1614 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1615 /* queue back in the device */
1616 if (pool->other_pool)
1617 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1618 if (ret != GST_FLOW_OK ||
1619 gst_v4l2_buffer_pool_qbuf (pool, buffer, group,
1620 NULL) != GST_FLOW_OK)
1621 pclass->release_buffer (bpool, buffer);
1623 /* Simply release invalid/modified buffer, the allocator will
1624 * give it back later */
1625 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1626 pclass->release_buffer (bpool, buffer);
1631 g_assert_not_reached ();
1636 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1637 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1638 switch (obj->mode) {
1639 case GST_V4L2_IO_RW:
1640 /* release back in the pool */
1641 pclass->release_buffer (bpool, buffer);
1644 case GST_V4L2_IO_MMAP:
1645 case GST_V4L2_IO_DMABUF:
1646 case GST_V4L2_IO_USERPTR:
1647 case GST_V4L2_IO_DMABUF_IMPORT:
1649 GstV4l2MemoryGroup *group;
1652 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1653 /* Simply release invalid/modified buffer, the allocator will
1654 * give it back later */
1655 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1656 pclass->release_buffer (bpool, buffer);
1660 index = group->buffer.index;
1663 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1666 /* Remove qdata, this will unmap any map data in userptr */
1667 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1668 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1670 /* reset to default size */
1671 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1673 /* playback, put the buffer back in the queue to refill later. */
1674 pclass->release_buffer (bpool, buffer);
1676 /* the buffer is queued in the device but maybe not played yet. We just
1677 * leave it there and not make it available for future calls to acquire
1678 * for now. The buffer will be dequeued and reused later. */
1679 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1685 g_assert_not_reached ();
1691 g_assert_not_reached ();
1697 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1699 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1700 GstV4l2MemoryGroup *group;
1701 gboolean queued = FALSE;
1703 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1704 gint old_buffer_state =
1705 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
1706 ~BUFFER_STATE_OUTSTANDING);
1707 queued = (old_buffer_state & BUFFER_STATE_QUEUED) != 0;
1708 GST_LOG_OBJECT (pool, "mark buffer %u not outstanding",
1709 group->buffer.index);
1712 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, queued);
1716 gst_v4l2_buffer_pool_dispose (GObject * object)
1718 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1720 if (pool->vallocator)
1721 gst_object_unref (pool->vallocator);
1722 pool->vallocator = NULL;
1724 if (pool->allocator)
1725 gst_object_unref (pool->allocator);
1726 pool->allocator = NULL;
1728 if (pool->other_pool)
1729 gst_object_unref (pool->other_pool);
1730 pool->other_pool = NULL;
1732 G_OBJECT_CLASS (parent_class)->dispose (object);
1736 gst_v4l2_buffer_pool_finalize (GObject * object)
1738 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1740 if (pool->video_fd >= 0)
1741 pool->obj->close (pool->video_fd);
1743 gst_poll_free (pool->poll);
1745 /* This can't be done in dispose method because we must not set pointer
1746 * to NULL as it is part of the v4l2object and dispose could be called
1748 gst_object_unref (pool->obj->element);
1750 g_cond_clear (&pool->empty_cond);
1752 /* FIXME have we done enough here ? */
1754 G_OBJECT_CLASS (parent_class)->finalize (object);
1758 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1760 pool->poll = gst_poll_new (TRUE);
1761 pool->can_poll_device = TRUE;
1762 g_cond_init (&pool->empty_cond);
1764 pool->orphaned = FALSE;
1765 for (gint i = 0; i < VIDEO_MAX_FRAME; i++)
1766 g_atomic_int_set (&pool->buffer_state[i], BUFFER_STATE_FREE);
1770 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1772 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1773 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1775 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1776 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1778 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1779 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1780 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1781 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1782 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1783 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1784 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1785 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1787 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1788 "V4L2 Buffer Pool");
1789 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
1793 * gst_v4l2_buffer_pool_new:
1794 * @obj: the v4l2 object owning the pool
1796 * Construct a new buffer pool.
1798 * Returns: the new pool, use gst_object_unref() to free resources
1801 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1803 GstV4l2BufferPool *pool;
1804 GstStructure *config;
1805 gchar *name, *parent_name;
1808 fd = obj->dup (obj->video_fd);
1812 /* setting a significant unique name */
1813 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1814 name = g_strdup_printf ("%s:pool%u:%s",
1815 parent_name, obj->pool_seq++,
1816 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src");
1817 g_free (parent_name);
1819 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1820 "name", name, NULL);
1821 g_object_ref_sink (pool);
1824 gst_poll_fd_init (&pool->pollfd);
1825 pool->pollfd.fd = fd;
1826 gst_poll_add_fd (pool->poll, &pool->pollfd);
1827 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1828 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1830 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1832 pool->video_fd = fd;
1834 pool->can_poll_device = TRUE;
1836 pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
1837 if (pool->vallocator == NULL)
1838 goto allocator_failed;
1840 gst_object_ref (obj->element);
1842 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1843 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1844 /* This will simply set a default config, but will not configure the pool
1845 * because min and max are not valid */
1846 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1848 return GST_BUFFER_POOL (pool);
1853 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1858 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1859 gst_object_unref (pool);
1864 static GstFlowReturn
1865 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1868 GstV4l2Object *obj = pool->obj;
1873 toread = obj->info.size;
1875 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1877 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1880 if ((res = gst_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK)
1883 amount = obj->read (obj->video_fd, map.data, toread);
1885 if (amount == toread) {
1887 } else if (amount == -1) {
1888 if (errno == EAGAIN || errno == EINTR) {
1893 /* short reads can happen if a signal interrupts the read */
1898 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1899 gst_buffer_unmap (buf, &map);
1900 gst_buffer_resize (buf, 0, amount);
1907 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1912 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1913 (_("Error reading %d bytes from device '%s'."),
1914 toread, obj->videodev), GST_ERROR_SYSTEM);
1915 res = GST_FLOW_ERROR;
1920 gst_buffer_unmap (buf, &map);
1921 gst_buffer_resize (buf, 0, 0);
1927 * gst_v4l2_buffer_pool_process:
1928 * @bpool: a #GstBufferPool
1929 * @buf: a #GstBuffer, maybe be replaced
1930 * @frame_number: 32 bit frame number or %NULL
1932 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1933 * data from the device. For output devices, this functions send the contents of
1934 * @buf to the device for playback.
1936 * If non-%NULL and an output device, @frame_number is stored inside the timestamp for output devices and read
1937 * back from the timestamp for capture devices.
1939 * Returns: %GST_FLOW_OK on success.
1942 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf,
1943 guint32 * frame_number)
1945 GstFlowReturn ret = GST_FLOW_OK;
1946 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1947 GstV4l2Object *obj = pool->obj;
1949 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1951 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1952 return GST_FLOW_FLUSHING;
1954 switch (obj->type) {
1955 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1956 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1958 switch (obj->mode) {
1959 case GST_V4L2_IO_RW:
1960 /* capture into the buffer */
1961 ret = gst_v4l2_do_read (pool, *buf);
1964 case GST_V4L2_IO_MMAP:
1965 case GST_V4L2_IO_DMABUF:
1969 if ((*buf)->pool == bpool) {
1971 gsize size = gst_buffer_get_size (*buf);
1973 /* Legacy M2M devices return empty buffer when drained */
1974 if (size == 0 && GST_V4L2_IS_M2M (obj->device_caps))
1977 if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) !=
1978 GST_VIDEO_FORMAT_ENCODED && size < pool->size)
1979 goto buffer_truncated;
1981 num_queued = g_atomic_int_get (&pool->num_queued);
1982 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1985 /* If we have no more buffer, and can allocate it time to do so */
1986 if (num_queued == 0) {
1987 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1988 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
1989 if (ret == GST_FLOW_OK)
1994 /* start copying buffers when we are running low on buffers */
1995 if (num_queued < pool->copy_threshold) {
1998 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1999 ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
2000 if (ret == GST_FLOW_OK)
2004 /* copy the buffer */
2005 copy = gst_buffer_copy_region (*buf,
2006 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
2007 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
2009 /* and requeue so that we can continue capturing */
2010 gst_buffer_unref (*buf);
2015 /* nothing, data was inside the buffer when we did _acquire() */
2019 /* buffer not from our pool, grab a frame and copy it into the target */
2020 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp, NULL, TRUE))
2024 /* An empty buffer on capture indicates the end of stream */
2025 if (gst_buffer_get_size (tmp) == 0) {
2026 gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
2028 /* Legacy M2M devices return empty buffer when drained */
2029 if (GST_V4L2_IS_M2M (obj->device_caps))
2033 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
2035 /* an queue the buffer again after the copy */
2036 gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
2038 if (ret != GST_FLOW_OK)
2043 case GST_V4L2_IO_USERPTR:
2045 struct UserPtrData *data;
2048 /* Replace our buffer with downstream allocated buffer */
2049 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
2050 GST_V4L2_IMPORT_QUARK);
2051 tmp = gst_buffer_ref (data->buffer);
2052 _unmap_userptr_frame (data);
2054 /* Now tmp is writable, copy the flags and timestamp */
2055 gst_buffer_copy_into (tmp, *buf,
2056 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2058 gst_buffer_replace (buf, tmp);
2059 gst_buffer_unref (tmp);
2063 case GST_V4L2_IO_DMABUF_IMPORT:
2067 /* Replace our buffer with downstream allocated buffer */
2068 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
2069 GST_V4L2_IMPORT_QUARK);
2071 gst_buffer_copy_into (tmp, *buf,
2072 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2074 gst_buffer_replace (buf, tmp);
2075 gst_buffer_unref (tmp);
2080 g_assert_not_reached ();
2085 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
2086 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
2088 switch (obj->mode) {
2089 case GST_V4L2_IO_RW:
2090 /* FIXME, do write() */
2091 GST_WARNING_OBJECT (pool, "implement write()");
2094 case GST_V4L2_IO_USERPTR:
2095 case GST_V4L2_IO_DMABUF_IMPORT:
2096 case GST_V4L2_IO_DMABUF:
2097 case GST_V4L2_IO_MMAP:
2099 GstBuffer *to_queue = NULL;
2101 GstV4l2MemoryGroup *group;
2103 gboolean outstanding;
2105 if ((*buf)->pool != bpool)
2108 if (!gst_v4l2_is_buffer_valid (*buf, &group))
2111 index = group->buffer.index;
2113 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
2115 if (g_atomic_int_get (&pool->buffer_state[index]) &
2116 BUFFER_STATE_QUEUED) {
2117 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
2121 /* we can queue directly */
2122 to_queue = gst_buffer_ref (*buf);
2125 if (to_queue == NULL) {
2126 GstBufferPoolAcquireParams params = { 0 };
2128 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
2130 /* this can return EOS if all buffers are outstanding which would
2131 * be strange because we would expect the upstream element to have
2132 * allocated them and returned to us.. */
2133 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
2134 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
2135 if (ret != GST_FLOW_OK)
2136 goto acquire_failed;
2138 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
2139 if (ret != GST_FLOW_OK) {
2140 gst_buffer_unref (to_queue);
2141 goto prepare_failed;
2144 /* retrieve the group */
2145 gst_v4l2_is_buffer_valid (to_queue, &group);
2149 gst_v4l2_buffer_pool_qbuf (pool, to_queue, group,
2154 /* if we are not streaming yet (this is the first buffer, start
2156 if (!gst_v4l2_buffer_pool_streamon (pool)) {
2157 /* don't check return value because qbuf would have failed */
2158 gst_v4l2_is_buffer_valid (to_queue, &group);
2160 /* qbuf has stored to_queue buffer but we are not in
2161 * streaming state, so the flush logic won't be performed.
2162 * To avoid leaks, flush the allocator and restore the queued
2163 * buffer as non-queued */
2164 gst_v4l2_allocator_flush (pool->vallocator);
2166 pool->buffers[group->buffer.index] = NULL;
2167 g_atomic_int_and (&pool->buffer_state[group->buffer.index],
2168 ~BUFFER_STATE_QUEUED);
2170 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
2171 GST_V4L2_IMPORT_QUARK, NULL, NULL);
2172 gst_buffer_unref (to_queue);
2173 g_atomic_int_add (&pool->num_queued, -1);
2177 /* Remove our ref, we will still hold this buffer in acquire as needed,
2178 * otherwise the pool will think it is outstanding and will refuse to stop. */
2179 gst_buffer_unref (to_queue);
2181 /* release as many buffer as possible */
2182 while (gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding,
2183 FALSE) == GST_FLOW_OK) {
2185 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
2189 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
2190 /* all buffers are queued, try to dequeue one and release it back
2191 * into the pool so that _acquire can get to it again. */
2193 gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding, TRUE);
2194 if (ret == GST_FLOW_OK && !outstanding)
2195 /* release the rendered buffer back into the pool. This wakes up any
2196 * thread waiting for a buffer in _acquire(). */
2197 gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
2203 g_assert_not_reached ();
2208 g_assert_not_reached ();
2217 GST_ERROR_OBJECT (pool, "failed to copy buffer");
2222 GST_WARNING_OBJECT (pool,
2223 "Dropping truncated buffer, this is likely a driver bug.");
2224 gst_buffer_unref (*buf);
2226 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
2230 GST_DEBUG_OBJECT (pool, "end of stream reached");
2231 gst_buffer_unref (*buf);
2233 return GST_V4L2_FLOW_LAST_BUFFER;
2237 if (ret == GST_FLOW_FLUSHING)
2238 GST_DEBUG_OBJECT (pool, "flushing");
2240 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
2241 gst_flow_get_name (ret));
2246 GST_ERROR_OBJECT (pool, "failed to prepare data");
2251 GST_ERROR_OBJECT (pool, "failed to queue buffer");
2256 GST_ERROR_OBJECT (pool, "failed to start streaming");
2257 return GST_FLOW_ERROR;
2262 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
2263 GstBufferPool * other_pool)
2265 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
2267 if (pool->other_pool)
2268 gst_object_unref (pool->other_pool);
2269 pool->other_pool = gst_object_ref (other_pool);
2273 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
2275 GST_OBJECT_LOCK (pool);
2276 pool->enable_copy_threshold = copy;
2277 GST_OBJECT_UNLOCK (pool);
2281 gst_v4l2_buffer_pool_flush (GstBufferPool * bpool)
2283 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
2284 gboolean ret = TRUE;
2286 gst_v4l2_buffer_pool_streamoff (pool);
2288 if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type))
2289 ret = gst_v4l2_buffer_pool_streamon (pool);
2295 * gst_v4l2_buffer_pool_enable_resolution_change:
2296 * @pool: a #GstBufferPool
2298 * When this is called, the pool will subscribe to the
2299 * %V4L2_EVENT_SOURCE_CHANGE. Upon receiving this event, it will notify
2300 * the element acquiring buffer with the special flow return
2301 * %GST_V4L2_FLOW_RESOLUTION_CHANGE.
2304 gst_v4l2_buffer_pool_enable_resolution_change (GstV4l2BufferPool * pool)
2306 guint32 input_id = 0;
2308 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
2310 /* Make sure we subscribe for the current input */
2311 gst_v4l2_get_input (pool->obj, &input_id);
2313 if (gst_v4l2_subscribe_event (pool->obj, V4L2_EVENT_SOURCE_CHANGE, input_id))
2314 gst_poll_fd_ctl_pri (pool->poll, &pool->pollfd, TRUE);