3 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@indt.org.br>
5 * 2009 Texas Instruments, Inc - http://www.ti.com/
7 * gstv4l2bufferpool.c V4L2 buffer pool class
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
30 # define _GNU_SOURCE /* O_CLOEXEC */
38 #include "gst/video/video.h"
39 #include "gst/video/gstvideometa.h"
40 #include "gst/video/gstvideopool.h"
41 #include "gst/allocators/gstdmabuf.h"
43 #include <gstv4l2bufferpool.h>
45 #include "v4l2_calls.h"
46 #include "gst/gst-i18n-plugin.h"
47 #include <gst/glib-compat-private.h>
49 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
50 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2bufferpool_debug
53 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
59 #define gst_v4l2_buffer_pool_parent_class parent_class
60 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
62 enum _GstV4l2BufferPoolAcquireFlags
64 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
65 GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
66 GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
69 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
73 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
75 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
76 gboolean valid = FALSE;
78 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
81 if (gst_is_dmabuf_memory (mem))
82 mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
83 GST_V4L2_MEMORY_QUARK);
85 if (mem && gst_is_v4l2_memory (mem)) {
86 GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
87 GstV4l2MemoryGroup *group = vmem->group;
90 if (group->n_mem != gst_buffer_n_memory (buffer))
93 for (i = 0; i < group->n_mem; i++) {
94 if (group->mem[i] != gst_buffer_peek_memory (buffer, i))
97 if (!gst_memory_is_writable (group->mem[i]))
111 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
114 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
116 GST_LOG_OBJECT (pool, "copying buffer");
118 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
119 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
120 GstVideoFrame src_frame, dest_frame;
122 GST_DEBUG_OBJECT (pool, "copy video frame");
124 /* we have raw video, use videoframe copy to get strides right */
125 if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
128 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
130 gst_video_frame_unmap (&src_frame);
134 gst_video_frame_copy (&dest_frame, &src_frame);
136 gst_video_frame_unmap (&src_frame);
137 gst_video_frame_unmap (&dest_frame);
141 GST_DEBUG_OBJECT (pool, "copy raw bytes");
143 if (!gst_buffer_map (src, &map, GST_MAP_READ))
146 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
148 gst_buffer_unmap (src, &map);
149 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
152 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
159 GST_ERROR_OBJECT (pool, "could not map buffer");
160 return GST_FLOW_ERROR;
173 gst_v4l2_buffer_pool_import_quark (void)
175 static GQuark quark = 0;
178 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
184 _unmap_userptr_frame (struct UserPtrData *data)
187 gst_video_frame_unmap (&data->frame);
189 gst_buffer_unmap (data->buffer, &data->map);
192 gst_buffer_unref (data->buffer);
194 g_slice_free (struct UserPtrData, data);
198 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
199 GstBuffer * dest, GstBuffer * src)
201 GstFlowReturn ret = GST_FLOW_OK;
202 GstV4l2MemoryGroup *group = NULL;
204 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
205 struct UserPtrData *data = NULL;
207 GST_LOG_OBJECT (pool, "importing userptr");
210 if (!gst_v4l2_is_buffer_valid (dest, &group))
213 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
214 flags = GST_MAP_READ;
216 flags = GST_MAP_WRITE;
218 data = g_slice_new0 (struct UserPtrData);
220 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
221 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
222 gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
225 data->is_frame = TRUE;
227 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
230 for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
231 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
232 gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
236 pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
237 GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
239 pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
240 GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
242 size[i] = pstride * pheight;
244 size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
245 GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
249 /* In the single planar API, planes must be contiguous in memory and
250 * therefore they must have expected size. ie: no padding.
251 * To check these conditions, we check that plane 'i' start address
252 * + plane 'i' size equals to plane 'i+1' start address */
253 if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
254 for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
255 const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
257 gint estride = gst_v4l2_object_extrapolate_stride (finfo, i,
258 pix_fmt->bytesperline);
259 guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
262 tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
263 if (tmp != data->frame.data[i + 1])
264 goto non_contiguous_mem;
268 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
269 data->frame.info.size, finfo->n_planes, data->frame.data, size))
275 data->is_frame = FALSE;
277 if (!gst_buffer_map (src, &data->map, flags))
280 ptr[0] = data->map.data;
281 size[0] = data->map.size;
283 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
284 data->map.size, 1, ptr, size))
288 data->buffer = gst_buffer_ref (src);
290 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
291 data, (GDestroyNotify) _unmap_userptr_frame);
297 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
298 return GST_FLOW_ERROR;
302 GST_ERROR_OBJECT (pool, "could not map buffer");
303 g_slice_free (struct UserPtrData, data);
304 return GST_FLOW_ERROR;
308 GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
309 _unmap_userptr_frame (data);
310 return GST_FLOW_ERROR;
314 GST_ERROR_OBJECT (pool, "failed to import data");
315 _unmap_userptr_frame (data);
316 return GST_FLOW_ERROR;
321 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
322 GstBuffer * dest, GstBuffer * src)
324 GstV4l2MemoryGroup *group = NULL;
325 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
326 guint n_mem = gst_buffer_n_memory (src);
329 GST_LOG_OBJECT (pool, "importing dmabuf");
331 if (!gst_v4l2_is_buffer_valid (dest, &group))
334 if (n_mem > GST_VIDEO_MAX_PLANES)
337 for (i = 0; i < n_mem; i++)
338 dma_mem[i] = gst_buffer_peek_memory (src, i);
340 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
344 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
345 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
351 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
352 return GST_FLOW_ERROR;
356 GST_ERROR_OBJECT (pool, "could not map buffer");
357 return GST_FLOW_ERROR;
361 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
362 return GST_FLOW_ERROR;
367 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
368 GstBuffer * dest, GstBuffer * src)
370 GstFlowReturn ret = GST_FLOW_OK;
371 gboolean own_src = FALSE;
374 if (pool->other_pool == NULL) {
375 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
376 return GST_FLOW_ERROR;
379 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
380 if (ret != GST_FLOW_OK) {
381 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
388 switch (pool->obj->mode) {
389 case GST_V4L2_IO_MMAP:
390 case GST_V4L2_IO_DMABUF:
391 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
393 case GST_V4L2_IO_USERPTR:
394 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
396 case GST_V4L2_IO_DMABUF_IMPORT:
397 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
404 gst_buffer_unref (src);
411 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
412 GstBufferPoolAcquireParams * params)
414 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
415 GstV4l2MemoryGroup *group = NULL;
416 GstBuffer *newbuf = NULL;
426 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
428 case GST_V4L2_IO_MMAP:
429 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
431 case GST_V4L2_IO_DMABUF:
432 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
435 case GST_V4L2_IO_USERPTR:
436 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
438 case GST_V4L2_IO_DMABUF_IMPORT:
439 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
443 g_assert_not_reached ();
449 newbuf = gst_buffer_new ();
451 for (i = 0; i < group->n_mem; i++)
452 gst_buffer_append_memory (newbuf, group->mem[i]);
453 } else if (newbuf == NULL) {
454 goto allocation_failed;
457 /* add metadata to raw video buffers */
458 if (pool->add_videometa)
459 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
460 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
461 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
462 info->offset, info->stride);
471 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
472 return GST_FLOW_ERROR;
477 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
479 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
480 GstV4l2Object *obj = pool->obj;
482 guint size, min_buffers, max_buffers;
483 GstAllocator *allocator;
484 GstAllocationParams params;
485 gboolean can_allocate = FALSE;
486 gboolean updated = FALSE;
489 pool->add_videometa =
490 gst_buffer_pool_config_has_option (config,
491 GST_BUFFER_POOL_OPTION_VIDEO_META);
493 /* parse the config and keep around */
494 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
498 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
501 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
504 gst_object_unref (pool->allocator);
505 pool->allocator = NULL;
508 case GST_V4L2_IO_DMABUF:
509 pool->allocator = gst_dmabuf_allocator_new ();
510 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
512 case GST_V4L2_IO_MMAP:
513 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
515 case GST_V4L2_IO_USERPTR:
517 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
519 case GST_V4L2_IO_DMABUF_IMPORT:
520 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
524 pool->allocator = g_object_ref (allocator);
525 pool->params = params;
526 /* No need to change the configuration */
530 g_assert_not_reached ();
534 /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
535 * instability and crash, disable it for now */
536 if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
537 GST_WARNING_OBJECT (pool,
538 "libv4l2 converter detected, disabling CREATE_BUFS");
539 can_allocate = FALSE;
540 GST_OBJECT_FLAG_UNSET (pool->vallocator,
541 GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
542 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
543 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
546 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
548 min_buffers = GST_V4L2_MIN_BUFFERS;
549 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
552 /* respect driver requirements */
553 if (min_buffers < obj->min_buffers) {
555 min_buffers = obj->min_buffers;
556 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
559 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
561 max_buffers = VIDEO_MAX_FRAME;
562 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
565 if (min_buffers > max_buffers) {
567 min_buffers = max_buffers;
568 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
569 } else if (min_buffers != max_buffers) {
572 max_buffers = min_buffers;
573 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
577 if (!pool->add_videometa && obj->need_video_meta) {
578 GST_INFO_OBJECT (pool, "adding needed video meta");
580 gst_buffer_pool_config_add_option (config,
581 GST_BUFFER_POOL_OPTION_VIDEO_META);
584 /* Always update the config to ensure the configured size matches */
585 gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
588 /* keep a GstVideoInfo with defaults for the when we need to copy */
589 gst_video_info_from_caps (&pool->caps_info, caps);
592 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
594 /* If anything was changed documentation recommand to return FALSE */
595 return !updated && ret;
600 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
606 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
608 GstV4l2Object *obj = pool->obj;
611 case GST_V4L2_IO_MMAP:
612 case GST_V4L2_IO_USERPTR:
613 case GST_V4L2_IO_DMABUF:
614 case GST_V4L2_IO_DMABUF_IMPORT:
615 if (!pool->streaming) {
616 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
617 goto streamon_failed;
619 pool->streaming = TRUE;
621 GST_DEBUG_OBJECT (pool, "Started streaming");
632 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
639 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
641 GstV4l2Object *obj = pool->obj;
644 case GST_V4L2_IO_MMAP:
645 case GST_V4L2_IO_USERPTR:
646 case GST_V4L2_IO_DMABUF:
647 case GST_V4L2_IO_DMABUF_IMPORT:
648 if (pool->streaming) {
649 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
650 GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
651 errno, g_strerror (errno));
653 pool->streaming = FALSE;
655 GST_DEBUG_OBJECT (pool, "Stopped streaming");
657 if (pool->vallocator)
658 gst_v4l2_allocator_flush (pool->vallocator);
667 gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
669 GstBufferPoolAcquireParams params = { 0 };
670 GstBuffer *buffer = NULL;
673 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
676 (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT;
678 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
680 if (ret == GST_FLOW_OK)
681 gst_buffer_unref (buffer);
687 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
689 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
690 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
691 GstV4l2Object *obj = pool->obj;
692 GstStructure *config;
694 guint size, min_buffers, max_buffers;
695 guint max_latency, min_latency, copy_threshold = 0;
696 gboolean can_allocate = FALSE;
698 GST_DEBUG_OBJECT (pool, "activating pool");
700 config = gst_buffer_pool_get_config (bpool);
701 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
705 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
711 /* This workaround a unfixable bug in libv4l2 when RW is emulated on top
712 * of MMAP. In this case, the first read initialize the queues, but the
713 * poll before that will always fail. Doing an empty read, forces the
714 * queue to be initialized now. We only do this if we have a streaming
716 if (obj->vcap.capabilities & V4L2_CAP_STREAMING)
717 v4l2_read (obj->video_fd, NULL, 0);
720 case GST_V4L2_IO_DMABUF:
721 case GST_V4L2_IO_MMAP:
725 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
727 /* first, lets request buffers, and see how many we can get: */
728 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
730 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
733 if (count < GST_V4L2_MIN_BUFFERS) {
738 /* V4L2 buffer pool are often very limited in the amount of buffers it
739 * can offer. The copy_threshold will workaround this limitation by
740 * falling back to copy if the pipeline needed more buffers. This also
741 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
743 if (count != min_buffers || pool->enable_copy_threshold) {
744 GST_WARNING_OBJECT (pool,
745 "Uncertain or not enough buffers, enabling copy threshold");
747 copy_threshold = min_latency;
752 case GST_V4L2_IO_USERPTR:
757 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
759 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
761 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
762 V4L2_MEMORY_USERPTR);
764 /* There is no rational to not get what we asked */
765 if (count < min_buffers) {
773 case GST_V4L2_IO_DMABUF_IMPORT:
777 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
779 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
781 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
784 /* There is no rational to not get what we asked */
785 if (count < min_buffers) {
796 g_assert_not_reached ();
801 max_latency = max_buffers;
803 max_latency = min_buffers;
806 pool->copy_threshold = copy_threshold;
807 pool->max_latency = max_latency;
808 pool->min_latency = min_latency;
809 pool->num_queued = 0;
811 if (max_buffers != 0 && max_buffers < min_buffers)
812 max_buffers = min_buffers;
814 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
816 pclass->set_config (bpool, config);
817 gst_structure_free (config);
819 if (pool->other_pool)
820 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
821 goto other_pool_failed;
823 /* now, allocate the buffers: */
824 if (!pclass->start (bpool))
827 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
828 pool->group_released_handler =
829 g_signal_connect_swapped (pool->vallocator, "group-released",
830 G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
837 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
838 gst_structure_free (config);
843 GST_ERROR_OBJECT (pool,
844 "we received %d buffer from device '%s', we want at least %d",
845 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
846 gst_structure_free (config);
851 GST_ERROR_OBJECT (pool, "failed to start streaming");
856 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
857 GST_PTR_FORMAT, pool->other_pool);
863 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
865 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
866 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
870 GST_DEBUG_OBJECT (pool, "stopping pool");
872 if (pool->group_released_handler > 0) {
873 g_signal_handler_disconnect (pool->vallocator,
874 pool->group_released_handler);
875 pool->group_released_handler = 0;
878 if (pool->other_pool) {
879 gst_buffer_pool_set_active (pool->other_pool, FALSE);
880 gst_object_unref (pool->other_pool);
881 pool->other_pool = NULL;
884 gst_v4l2_buffer_pool_streamoff (pool);
886 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
887 if (pool->buffers[i]) {
888 GstBuffer *buffer = pool->buffers[i];
890 pool->buffers[i] = NULL;
892 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
893 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
894 else /* Don't re-enqueue capture buffer on stop */
895 pclass->release_buffer (bpool, buffer);
897 g_atomic_int_add (&pool->num_queued, -1);
901 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
903 if (ret && pool->vallocator) {
906 vret = gst_v4l2_allocator_stop (pool->vallocator);
908 if (vret == GST_V4L2_BUSY)
909 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
911 ret = (vret == GST_V4L2_OK);
918 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
920 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
922 GST_DEBUG_OBJECT (pool, "start flushing");
924 gst_poll_set_flushing (pool->poll, TRUE);
926 GST_OBJECT_LOCK (pool);
928 g_cond_broadcast (&pool->empty_cond);
929 GST_OBJECT_UNLOCK (pool);
931 if (pool->other_pool)
932 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
936 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
938 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
939 GstV4l2Object *obj = pool->obj;
942 GST_DEBUG_OBJECT (pool, "stop flushing");
944 /* If we haven't started streaming yet, simply call streamon */
945 if (!pool->streaming)
948 if (pool->other_pool)
949 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
951 gst_v4l2_buffer_pool_streamoff (pool);
953 /* Reset our state */
957 case GST_V4L2_IO_MMAP:
958 case GST_V4L2_IO_USERPTR:
959 case GST_V4L2_IO_DMABUF:
960 case GST_V4L2_IO_DMABUF_IMPORT:
962 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
963 /* Re-enqueue buffers */
964 if (pool->buffers[i]) {
965 GstBufferPool *bpool = (GstBufferPool *) pool;
966 GstBuffer *buffer = pool->buffers[i];
968 pool->buffers[i] = NULL;
970 /* Remove qdata, this will unmap any map data in
971 * userptr/dmabuf-import */
972 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
973 GST_V4L2_IMPORT_QUARK, NULL, NULL);
975 if (buffer->pool == NULL)
976 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
978 g_atomic_int_add (&pool->num_queued, -1);
985 g_assert_not_reached ();
990 /* Start streaming on capture device only */
991 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
992 gst_v4l2_buffer_pool_streamon (pool);
994 gst_poll_set_flushing (pool->poll, FALSE);
998 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
1002 /* In RW mode there is no queue, hence no need to wait while the queue is
1004 if (pool->obj->mode != GST_V4L2_IO_RW) {
1005 GST_OBJECT_LOCK (pool);
1007 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
1008 GST_OBJECT_UNLOCK (pool);
1011 if (!pool->can_poll_device)
1014 GST_LOG_OBJECT (pool, "polling device");
1017 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
1018 if (G_UNLIKELY (ret < 0)) {
1026 GST_WARNING_OBJECT (pool,
1027 "v4l2 device doesn't support polling. Disabling"
1028 " using libv4l2 in this case may cause deadlocks");
1029 pool->can_poll_device = FALSE;
1036 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
1045 GST_DEBUG_OBJECT (pool, "stop called");
1046 return GST_FLOW_FLUSHING;
1050 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
1051 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
1052 return GST_FLOW_ERROR;
1056 static GstFlowReturn
1057 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1059 GstV4l2MemoryGroup *group = NULL;
1060 const GstV4l2Object *obj = pool->obj;
1063 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1064 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1065 gst_buffer_unref (buf);
1069 index = group->buffer.index;
1071 if (pool->buffers[index] != NULL)
1072 goto already_queued;
1074 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1076 g_atomic_int_inc (&pool->num_queued);
1077 pool->buffers[index] = buf;
1079 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1080 enum v4l2_field field;
1082 /* Except when field is set to alternate, buffer field is the same as
1083 * the one defined in format */
1084 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
1085 field = obj->format.fmt.pix_mp.field;
1087 field = obj->format.fmt.pix.field;
1089 /* NB: At this moment, we can't have alternate mode because it not handled
1091 if (field == V4L2_FIELD_ALTERNATE) {
1092 if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
1093 field = V4L2_FIELD_TOP;
1095 field = V4L2_FIELD_BOTTOM;
1098 group->buffer.field = field;
1101 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1104 GST_OBJECT_LOCK (pool);
1105 pool->empty = FALSE;
1106 g_cond_signal (&pool->empty_cond);
1107 GST_OBJECT_UNLOCK (pool);
1113 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1114 return GST_FLOW_ERROR;
1118 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1119 /* Mark broken buffer to the allocator */
1120 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1121 g_atomic_int_add (&pool->num_queued, -1);
1122 pool->buffers[index] = NULL;
1123 return GST_FLOW_ERROR;
1127 static GstFlowReturn
1128 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1132 GstV4l2Object *obj = pool->obj;
1133 GstClockTime timestamp;
1134 GstV4l2MemoryGroup *group;
1137 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1140 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1142 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1146 /* get our GstBuffer with that index from the pool, if the buffer was
1147 * outstanding we have a serious problem.
1149 outbuf = pool->buffers[group->buffer.index];
1153 /* mark the buffer outstanding */
1154 pool->buffers[group->buffer.index] = NULL;
1155 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1156 GST_OBJECT_LOCK (pool);
1158 GST_OBJECT_UNLOCK (pool);
1161 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1163 #ifndef GST_DISABLE_GST_DEBUG
1164 for (i = 0; i < group->n_mem; i++) {
1165 GST_LOG_OBJECT (pool,
1166 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1167 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1168 group->buffer.sequence, group->buffer.index, group->mem[i],
1169 group->planes[i].bytesused, i, group->buffer.flags,
1170 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1174 /* Ignore timestamp and field for OUTPUT device */
1175 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1178 /* Check for driver bug in reporting feild */
1179 if (group->buffer.field == V4L2_FIELD_ANY) {
1180 /* Only warn once to avoid the spamming */
1181 #ifndef GST_DISABLE_GST_DEBUG
1182 if (!pool->has_warned_on_buggy_field) {
1183 pool->has_warned_on_buggy_field = TRUE;
1184 GST_WARNING_OBJECT (pool,
1185 "Driver should never set v4l2_buffer.field to ANY");
1189 /* Use the value from the format (works for UVC bug) */
1190 group->buffer.field = obj->format.fmt.pix.field;
1192 /* If driver also has buggy S_FMT, assume progressive */
1193 if (group->buffer.field == V4L2_FIELD_ANY) {
1194 #ifndef GST_DISABLE_GST_DEBUG
1195 if (!pool->has_warned_on_buggy_field) {
1196 pool->has_warned_on_buggy_field = TRUE;
1197 GST_WARNING_OBJECT (pool,
1198 "Driver should never set v4l2_format.pix.field to ANY");
1202 group->buffer.field = V4L2_FIELD_NONE;
1206 /* set top/bottom field first if v4l2_buffer has the information */
1207 switch (group->buffer.field) {
1208 case V4L2_FIELD_NONE:
1209 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1210 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1212 case V4L2_FIELD_INTERLACED_TB:
1213 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1214 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1216 case V4L2_FIELD_INTERLACED_BT:
1217 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1218 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1220 case V4L2_FIELD_INTERLACED:
1221 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1222 if (obj->tv_norm == V4L2_STD_NTSC_M ||
1223 obj->tv_norm == V4L2_STD_NTSC_M_JP ||
1224 obj->tv_norm == V4L2_STD_NTSC_M_KR) {
1225 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1227 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1231 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1232 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1233 GST_FIXME_OBJECT (pool,
1234 "Unhandled enum v4l2_field %d - treating as progressive",
1235 group->buffer.field);
1239 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1240 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1241 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1243 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1246 if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
1247 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
1249 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1259 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1264 return GST_FLOW_ERROR;
1268 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1269 group->buffer.index);
1270 return GST_FLOW_ERROR;
1274 static GstFlowReturn
1275 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1276 GstBufferPoolAcquireParams * params)
1279 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1280 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1281 GstV4l2Object *obj = pool->obj;
1283 GST_DEBUG_OBJECT (pool, "acquire");
1285 /* If this is being called to resurect a lost buffer */
1286 if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
1287 ret = pclass->acquire_buffer (bpool, buffer, params);
1291 switch (obj->type) {
1292 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1293 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1294 /* capture, This function should return a buffer with new captured data */
1295 switch (obj->mode) {
1296 case GST_V4L2_IO_RW:
1298 /* take empty buffer from the pool */
1299 ret = pclass->acquire_buffer (bpool, buffer, params);
1302 case GST_V4L2_IO_DMABUF:
1303 case GST_V4L2_IO_MMAP:
1304 case GST_V4L2_IO_USERPTR:
1305 case GST_V4L2_IO_DMABUF_IMPORT:
1307 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1308 * storage for our buffers. This function does poll first so we can
1309 * interrupt it fine. */
1310 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1314 ret = GST_FLOW_ERROR;
1315 g_assert_not_reached ();
1321 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1322 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1323 /* playback, This function should return an empty buffer */
1324 switch (obj->mode) {
1325 case GST_V4L2_IO_RW:
1326 /* get an empty buffer */
1327 ret = pclass->acquire_buffer (bpool, buffer, params);
1330 case GST_V4L2_IO_MMAP:
1331 case GST_V4L2_IO_DMABUF:
1332 case GST_V4L2_IO_USERPTR:
1333 case GST_V4L2_IO_DMABUF_IMPORT:
1334 /* get a free unqueued buffer */
1335 ret = pclass->acquire_buffer (bpool, buffer, params);
1339 ret = GST_FLOW_ERROR;
1340 g_assert_not_reached ();
1346 ret = GST_FLOW_ERROR;
1347 g_assert_not_reached ();
1355 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1357 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1358 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1359 GstV4l2Object *obj = pool->obj;
1361 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1363 switch (obj->type) {
1364 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1365 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1366 /* capture, put the buffer back in the queue so that we can refill it
1368 switch (obj->mode) {
1369 case GST_V4L2_IO_RW:
1370 /* release back in the pool */
1371 pclass->release_buffer (bpool, buffer);
1374 case GST_V4L2_IO_DMABUF:
1375 case GST_V4L2_IO_MMAP:
1376 case GST_V4L2_IO_USERPTR:
1377 case GST_V4L2_IO_DMABUF_IMPORT:
1379 GstV4l2MemoryGroup *group;
1380 if (gst_v4l2_is_buffer_valid (buffer, &group)) {
1381 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1382 /* queue back in the device */
1383 if (pool->other_pool)
1384 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1385 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1386 pclass->release_buffer (bpool, buffer);
1388 /* Simply release invalide/modified buffer, the allocator will
1389 * give it back later */
1390 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1391 pclass->release_buffer (bpool, buffer);
1396 g_assert_not_reached ();
1401 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1402 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1403 switch (obj->mode) {
1404 case GST_V4L2_IO_RW:
1405 /* release back in the pool */
1406 pclass->release_buffer (bpool, buffer);
1409 case GST_V4L2_IO_MMAP:
1410 case GST_V4L2_IO_DMABUF:
1411 case GST_V4L2_IO_USERPTR:
1412 case GST_V4L2_IO_DMABUF_IMPORT:
1414 GstV4l2MemoryGroup *group;
1417 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1418 /* Simply release invalide/modified buffer, the allocator will
1419 * give it back later */
1420 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1421 pclass->release_buffer (bpool, buffer);
1425 index = group->buffer.index;
1427 if (pool->buffers[index] == NULL) {
1428 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1431 /* Remove qdata, this will unmap any map data in userptr */
1432 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1433 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1435 /* reset to default size */
1436 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1438 /* playback, put the buffer back in the queue to refill later. */
1439 pclass->release_buffer (bpool, buffer);
1441 /* the buffer is queued in the device but maybe not played yet. We just
1442 * leave it there and not make it available for future calls to acquire
1443 * for now. The buffer will be dequeued and reused later. */
1444 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1450 g_assert_not_reached ();
1456 g_assert_not_reached ();
1462 gst_v4l2_buffer_pool_dispose (GObject * object)
1464 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1466 if (pool->vallocator)
1467 gst_object_unref (pool->vallocator);
1468 pool->vallocator = NULL;
1470 if (pool->allocator)
1471 gst_object_unref (pool->allocator);
1472 pool->allocator = NULL;
1474 if (pool->other_pool)
1475 gst_object_unref (pool->other_pool);
1476 pool->other_pool = NULL;
1478 G_OBJECT_CLASS (parent_class)->dispose (object);
1482 gst_v4l2_buffer_pool_finalize (GObject * object)
1484 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1486 if (pool->video_fd >= 0)
1487 v4l2_close (pool->video_fd);
1489 gst_poll_free (pool->poll);
1491 /* FIXME Is this required to keep around ?
1492 * This can't be done in dispose method because we must not set pointer
1493 * to NULL as it is part of the v4l2object and dispose could be called
1495 gst_object_unref (pool->obj->element);
1497 g_cond_clear (&pool->empty_cond);
1499 /* FIXME have we done enough here ? */
1501 G_OBJECT_CLASS (parent_class)->finalize (object);
1505 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1507 pool->poll = gst_poll_new (TRUE);
1508 pool->can_poll_device = TRUE;
1509 g_cond_init (&pool->empty_cond);
1514 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1516 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1517 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1519 object_class->dispose = gst_v4l2_buffer_pool_dispose;
1520 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1522 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1523 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1524 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1525 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1526 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1527 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1528 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1529 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1531 GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
1532 "V4L2 Buffer Pool");
1536 * gst_v4l2_buffer_pool_new:
1537 * @obj: the v4l2 object owning the pool
1539 * Construct a new buffer pool.
1541 * Returns: the new pool, use gst_object_unref() to free resources
1544 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1546 GstV4l2BufferPool *pool;
1547 GstStructure *config;
1548 gchar *name, *parent_name;
1551 fd = v4l2_dup (obj->video_fd);
1555 /* setting a significant unique name */
1556 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1557 name = g_strconcat (parent_name, ":", "pool:",
1558 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1559 g_free (parent_name);
1561 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1562 "name", name, NULL);
1565 gst_poll_fd_init (&pool->pollfd);
1566 pool->pollfd.fd = fd;
1567 gst_poll_add_fd (pool->poll, &pool->pollfd);
1568 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1569 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1571 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1573 pool->video_fd = fd;
1575 pool->can_poll_device = TRUE;
1578 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1579 if (pool->vallocator == NULL)
1580 goto allocator_failed;
1582 gst_object_ref (obj->element);
1584 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1585 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1586 /* This will simply set a default config, but will not configure the pool
1587 * because min and max are not valid */
1588 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1590 return GST_BUFFER_POOL (pool);
1595 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1600 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1601 gst_object_unref (pool);
1606 static GstFlowReturn
1607 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1610 GstV4l2Object *obj = pool->obj;
1615 toread = obj->info.size;
1617 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1619 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1622 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1625 amount = v4l2_read (obj->video_fd, map.data, toread);
1627 if (amount == toread) {
1629 } else if (amount == -1) {
1630 if (errno == EAGAIN || errno == EINTR) {
1635 /* short reads can happen if a signal interrupts the read */
1640 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1641 gst_buffer_unmap (buf, &map);
1642 gst_buffer_resize (buf, 0, amount);
1649 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1654 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1655 (_("Error reading %d bytes from device '%s'."),
1656 toread, obj->videodev), GST_ERROR_SYSTEM);
1657 res = GST_FLOW_ERROR;
1662 gst_buffer_unmap (buf, &map);
1663 gst_buffer_resize (buf, 0, 0);
1669 * gst_v4l2_buffer_pool_process:
1670 * @bpool: a #GstBufferPool
1671 * @buf: a #GstBuffer, maybe be replaced
1673 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1674 * data from the device. For output devices, this functions send the contents of
1675 * @buf to the device for playback.
1677 * Returns: %GST_FLOW_OK on success.
1680 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1682 GstFlowReturn ret = GST_FLOW_OK;
1683 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1684 GstV4l2Object *obj = pool->obj;
1686 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1688 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1690 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1691 return GST_FLOW_FLUSHING;
1693 switch (obj->type) {
1694 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1695 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1697 switch (obj->mode) {
1698 case GST_V4L2_IO_RW:
1699 /* capture into the buffer */
1700 ret = gst_v4l2_do_read (pool, *buf);
1703 case GST_V4L2_IO_MMAP:
1704 case GST_V4L2_IO_DMABUF:
1708 if ((*buf)->pool == bpool) {
1711 if (gst_buffer_get_size (*buf) == 0) {
1712 if (GST_BUFFER_FLAG_IS_SET (*buf, GST_BUFFER_FLAG_CORRUPTED))
1713 goto buffer_corrupted;
1718 num_queued = g_atomic_int_get (&pool->num_queued);
1719 GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
1722 /* If we have no more buffer, and can allocate it time to do so */
1723 if (num_queued == 0) {
1724 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1725 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1726 if (ret == GST_FLOW_OK)
1731 /* start copying buffers when we are running low on buffers */
1732 if (num_queued < pool->copy_threshold) {
1735 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1736 ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
1737 if (ret == GST_FLOW_OK)
1741 /* copy the buffer */
1742 copy = gst_buffer_copy_region (*buf,
1743 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1744 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
1746 /* and requeue so that we can continue capturing */
1747 gst_buffer_unref (*buf);
1751 /* nothing, data was inside the buffer when we did _acquire() */
1755 /* buffer not from our pool, grab a frame and copy it into the target */
1756 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1759 /* An empty buffer on capture indicates the end of stream */
1760 if (gst_buffer_get_size (tmp) == 0) {
1761 gboolean corrupted = GST_BUFFER_FLAG_IS_SET (tmp,
1762 GST_BUFFER_FLAG_CORRUPTED);
1764 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1767 goto buffer_corrupted;
1772 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1774 /* an queue the buffer again after the copy */
1775 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1777 if (ret != GST_FLOW_OK)
1782 case GST_V4L2_IO_USERPTR:
1784 struct UserPtrData *data;
1786 /* Replace our buffer with downstream allocated buffer */
1787 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1788 GST_V4L2_IMPORT_QUARK);
1789 gst_buffer_replace (buf, data->buffer);
1790 _unmap_userptr_frame (data);
1794 case GST_V4L2_IO_DMABUF_IMPORT:
1798 /* Replace our buffer with downstream allocated buffer */
1799 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1800 GST_V4L2_IMPORT_QUARK);
1801 gst_buffer_replace (buf, tmp);
1802 gst_buffer_unref (tmp);
1807 g_assert_not_reached ();
1812 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1813 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1815 switch (obj->mode) {
1816 case GST_V4L2_IO_RW:
1817 /* FIXME, do write() */
1818 GST_WARNING_OBJECT (pool, "implement write()");
1821 case GST_V4L2_IO_USERPTR:
1822 case GST_V4L2_IO_DMABUF_IMPORT:
1823 case GST_V4L2_IO_DMABUF:
1824 case GST_V4L2_IO_MMAP:
1826 GstBuffer *to_queue = NULL;
1827 GstV4l2MemoryGroup *group;
1830 if ((*buf)->pool != bpool)
1833 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1836 index = group->buffer.index;
1838 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1840 index = group->buffer.index;
1841 if (pool->buffers[index] != NULL) {
1842 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1846 /* we can queue directly */
1847 to_queue = gst_buffer_ref (*buf);
1850 if (to_queue == NULL) {
1851 GstBufferPoolAcquireParams params = { 0 };
1853 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1855 /* this can return EOS if all buffers are outstanding which would
1856 * be strange because we would expect the upstream element to have
1857 * allocated them and returned to us.. */
1858 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1859 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1860 if (ret != GST_FLOW_OK)
1861 goto acquire_failed;
1863 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1864 if (ret != GST_FLOW_OK) {
1865 gst_buffer_unref (to_queue);
1866 goto prepare_failed;
1870 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1873 /* if we are not streaming yet (this is the first buffer, start
1875 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1876 /* don't check return value because qbuf would have failed */
1877 gst_v4l2_is_buffer_valid (to_queue, &group);
1879 /* qbuf has stored to_queue buffer but we are not in
1880 * streaming state, so the flush logic won't be performed.
1881 * To avoid leaks, flush the allocator and restore the queued
1882 * buffer as non-queued */
1883 gst_v4l2_allocator_flush (pool->vallocator);
1885 pool->buffers[group->buffer.index] = NULL;
1887 gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
1888 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1889 gst_buffer_unref (to_queue);
1890 g_atomic_int_add (&pool->num_queued, -1);
1894 /* Remove our ref, we will still hold this buffer in acquire as needed,
1895 * otherwise the pool will think it is outstanding and will refuse to stop. */
1896 gst_buffer_unref (to_queue);
1898 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1900 /* all buffers are queued, try to dequeue one and release it back
1901 * into the pool so that _acquire can get to it again. */
1902 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1903 if (ret == GST_FLOW_OK && out->pool == NULL)
1904 /* release the rendered buffer back into the pool. This wakes up any
1905 * thread waiting for a buffer in _acquire(). */
1906 gst_v4l2_buffer_pool_release_buffer (bpool, out);
1911 g_assert_not_reached ();
1916 g_assert_not_reached ();
1925 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1930 GST_WARNING_OBJECT (pool, "Dropping corrupted buffer without payload");
1931 gst_buffer_unref (*buf);
1933 return GST_V4L2_FLOW_CORRUPTED_BUFFER;
1937 GST_DEBUG_OBJECT (pool, "end of stream reached");
1938 gst_buffer_unref (*buf);
1940 return GST_V4L2_FLOW_LAST_BUFFER;
1944 if (ret == GST_FLOW_FLUSHING)
1945 GST_DEBUG_OBJECT (pool, "flushing");
1947 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1948 gst_flow_get_name (ret));
1953 GST_ERROR_OBJECT (pool, "failed to prepare data");
1958 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1963 GST_ERROR_OBJECT (pool, "failed to start streaming");
1964 return GST_FLOW_ERROR;
1969 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1970 GstBufferPool * other_pool)
1972 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1974 if (pool->other_pool)
1975 gst_object_unref (pool->other_pool);
1976 pool->other_pool = gst_object_ref (other_pool);
1980 gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
1982 GST_OBJECT_LOCK (pool);
1983 pool->enable_copy_threshold = copy;
1984 GST_OBJECT_UNLOCK (pool);