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_EXTERN (v4l2_debug);
50 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
51 #define GST_CAT_DEFAULT v4l2_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_POOL_ACQUIRE_FLAG_RESURECT = GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
65 GST_V4L2_BUFFER_POOL_ACQUIRE_FAG_LAST
68 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
72 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** group)
74 GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
75 gboolean valid = FALSE;
77 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
80 if (gst_is_dmabuf_memory (mem))
81 mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
82 GST_V4L2_MEMORY_QUARK);
84 if (mem && gst_is_v4l2_memory (mem)) {
85 GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
96 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
99 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
101 GST_LOG_OBJECT (pool, "copying buffer");
103 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
104 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
105 GstVideoFrame src_frame, dest_frame;
107 GST_DEBUG_OBJECT (pool, "copy video frame");
109 /* we have raw video, use videoframe copy to get strides right */
110 if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
113 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
115 gst_video_frame_unmap (&src_frame);
119 gst_video_frame_copy (&dest_frame, &src_frame);
121 gst_video_frame_unmap (&src_frame);
122 gst_video_frame_unmap (&dest_frame);
126 GST_DEBUG_OBJECT (pool, "copy raw bytes");
128 if (!gst_buffer_map (src, &map, GST_MAP_READ))
131 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
133 gst_buffer_unmap (src, &map);
134 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
137 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
144 GST_ERROR_OBJECT (pool, "could not map buffer");
145 return GST_FLOW_ERROR;
158 gst_v4l2_buffer_pool_import_quark (void)
160 static GQuark quark = 0;
163 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
169 _unmap_userptr_frame (struct UserPtrData *data)
172 gst_video_frame_unmap (&data->frame);
174 gst_buffer_unmap (data->buffer, &data->map);
177 gst_buffer_unref (data->buffer);
179 g_slice_free (struct UserPtrData, data);
183 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
184 GstBuffer * dest, GstBuffer * src)
186 GstFlowReturn ret = GST_FLOW_OK;
187 GstV4l2MemoryGroup *group = NULL;
189 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
190 struct UserPtrData *data = NULL;
192 GST_LOG_OBJECT (pool, "importing userptr");
195 if (!gst_v4l2_is_buffer_valid (dest, &group))
198 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
199 flags = GST_MAP_READ;
201 flags = GST_MAP_WRITE;
203 data = g_slice_new0 (struct UserPtrData);
205 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
206 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
207 data->is_frame = TRUE;
209 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
212 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
213 data->frame.info.size, finfo->n_planes, data->frame.data,
214 data->frame.info.offset))
217 gsize offset[1] = { 0 };
220 data->is_frame = FALSE;
222 if (!gst_buffer_map (src, &data->map, flags))
225 ptr[0] = data->map.data;
227 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
228 data->map.size, 1, ptr, offset))
232 data->buffer = gst_buffer_ref (src);
234 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
235 data, (GDestroyNotify) _unmap_userptr_frame);
241 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
242 return GST_FLOW_ERROR;
246 GST_ERROR_OBJECT (pool, "could not map buffer");
247 g_slice_free (struct UserPtrData, data);
248 return GST_FLOW_ERROR;
252 GST_ERROR_OBJECT (pool, "failed to import data");
253 _unmap_userptr_frame (data);
254 return GST_FLOW_ERROR;
259 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
260 GstBuffer * dest, GstBuffer * src)
262 GstV4l2MemoryGroup *group = NULL;
263 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
264 guint n_mem = gst_buffer_n_memory (src);
267 GST_LOG_OBJECT (pool, "importing dmabuf");
269 if (!gst_v4l2_is_buffer_valid (dest, &group))
272 if (n_mem > GST_VIDEO_MAX_PLANES)
275 for (i = 0; i < n_mem; i++)
276 dma_mem[i] = gst_buffer_peek_memory (src, i);
278 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
282 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
283 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
289 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
290 return GST_FLOW_ERROR;
294 GST_ERROR_OBJECT (pool, "could not map buffer");
295 return GST_FLOW_ERROR;
299 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
300 return GST_FLOW_ERROR;
305 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
306 GstBuffer * dest, GstBuffer * src)
308 GstFlowReturn ret = GST_FLOW_OK;
309 gboolean own_src = FALSE;
312 if (pool->other_pool == NULL) {
313 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
314 return GST_FLOW_ERROR;
317 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
318 if (ret != GST_FLOW_OK) {
319 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
326 switch (pool->obj->mode) {
327 case GST_V4L2_IO_MMAP:
328 case GST_V4L2_IO_DMABUF:
329 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
331 case GST_V4L2_IO_USERPTR:
332 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
334 case GST_V4L2_IO_DMABUF_IMPORT:
335 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
342 gst_buffer_unref (src);
349 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
350 GstBufferPoolAcquireParams * params)
352 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
353 GstV4l2MemoryGroup *group = NULL;
354 GstBuffer *newbuf = NULL;
364 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
366 case GST_V4L2_IO_MMAP:
367 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
369 case GST_V4L2_IO_DMABUF:
370 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
373 case GST_V4L2_IO_USERPTR:
374 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
376 case GST_V4L2_IO_DMABUF_IMPORT:
377 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
381 g_assert_not_reached ();
387 newbuf = gst_buffer_new ();
389 for (i = 0; i < group->n_mem; i++)
390 gst_buffer_append_memory (newbuf, group->mem[i]);
391 } else if (newbuf == NULL) {
392 goto allocation_failed;
395 /* add metadata to raw video buffers */
396 if (pool->add_videometa)
397 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
398 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
399 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
400 info->offset, info->stride);
409 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
410 return GST_FLOW_ERROR;
415 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
417 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
418 GstV4l2Object *obj = pool->obj;
420 guint size, min_buffers, max_buffers;
421 GstAllocator *allocator;
422 GstAllocationParams params;
423 gboolean can_allocate = FALSE;
424 gboolean updated = FALSE;
427 pool->add_videometa =
428 gst_buffer_pool_config_has_option (config,
429 GST_BUFFER_POOL_OPTION_VIDEO_META);
431 /* parse the config and keep around */
432 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
436 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
439 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
442 gst_object_unref (pool->allocator);
443 pool->allocator = NULL;
446 case GST_V4L2_IO_DMABUF:
447 pool->allocator = gst_dmabuf_allocator_new ();
448 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
450 case GST_V4L2_IO_MMAP:
451 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
453 case GST_V4L2_IO_USERPTR:
455 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
457 case GST_V4L2_IO_DMABUF_IMPORT:
458 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
461 pool->allocator = g_object_ref (allocator);
462 pool->params = params;
463 /* No need to change the configuration */
467 g_assert_not_reached ();
471 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
473 min_buffers = GST_V4L2_MIN_BUFFERS;
474 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
477 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
479 max_buffers = VIDEO_MAX_FRAME;
480 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
483 if (min_buffers > max_buffers) {
485 min_buffers = max_buffers;
486 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
487 } else if (min_buffers != max_buffers) {
490 max_buffers = min_buffers;
491 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
495 if (!pool->add_videometa && obj->need_video_meta) {
496 GST_INFO_OBJECT (pool, "adding needed video meta");
498 gst_buffer_pool_config_add_option (config,
499 GST_BUFFER_POOL_OPTION_VIDEO_META);
503 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
506 /* keep a GstVideoInfo with defaults for the when we need to copy */
507 gst_video_info_from_caps (&pool->caps_info, caps);
510 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
512 /* If anything was changed documentation recommand to return FALSE */
513 return !updated && ret;
518 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
524 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
526 GstV4l2Object *obj = pool->obj;
529 case GST_V4L2_IO_MMAP:
530 case GST_V4L2_IO_USERPTR:
531 case GST_V4L2_IO_DMABUF:
532 case GST_V4L2_IO_DMABUF_IMPORT:
533 if (!pool->streaming) {
534 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
535 goto streamon_failed;
537 pool->streaming = TRUE;
539 GST_DEBUG_OBJECT (pool, "Started streaming");
550 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
557 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
559 GstV4l2Object *obj = pool->obj;
562 case GST_V4L2_IO_MMAP:
563 case GST_V4L2_IO_USERPTR:
564 case GST_V4L2_IO_DMABUF:
565 case GST_V4L2_IO_DMABUF_IMPORT:
566 if (pool->streaming) {
567 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
568 goto streamoff_failed;
570 pool->streaming = FALSE;
572 GST_DEBUG_OBJECT (pool, "Stopped streaming");
583 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
590 gst_v4l2_buffer_pool_group_released (GstV4l2BufferPool * pool)
592 GstBufferPoolAcquireParams params = { 0 };
593 GstBuffer *buffer = NULL;
596 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
599 (GstBufferPoolAcquireFlags) GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT;
601 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
603 if (ret == GST_FLOW_OK)
604 gst_buffer_unref (buffer);
608 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
610 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
611 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
612 GstV4l2Object *obj = pool->obj;
613 GstStructure *config;
615 guint size, min_buffers, max_buffers;
616 guint max_latency, min_latency, copy_threshold = 0;
617 gboolean can_allocate = FALSE;
619 GST_DEBUG_OBJECT (pool, "activating pool");
621 config = gst_buffer_pool_get_config (bpool);
622 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
626 /* TODO Also consider min_buffers_for_output when implemented */
627 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers_for_capture);
633 case GST_V4L2_IO_DMABUF:
634 case GST_V4L2_IO_MMAP:
638 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
640 /* first, lets request buffers, and see how many we can get: */
641 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
643 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
646 if (count < GST_V4L2_MIN_BUFFERS) {
651 /* V4L2 buffer pool are often very limited in the amount of buffers it
652 * can offer. The copy_threshold will workaround this limitation by
653 * falling back to copy if the pipeline needed more buffers. This also
654 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
656 if (count != min_buffers) {
657 GST_WARNING_OBJECT (pool, "using %u buffers instead of %u",
660 copy_threshold = min_latency;
662 /* The initial minimum could be provide either by GstBufferPool or
669 case GST_V4L2_IO_USERPTR:
674 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
676 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
678 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
679 V4L2_MEMORY_USERPTR);
681 /* There is no rational to not get what we asked */
682 if (count < min_buffers) {
690 case GST_V4L2_IO_DMABUF_IMPORT:
694 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
696 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
698 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
701 /* There is no rational to not get what we asked */
702 if (count < min_buffers) {
713 g_assert_not_reached ();
718 max_latency = max_buffers;
720 max_latency = min_buffers;
722 /* FIXME Encoder don't negotiate amount of buffers. If we can't grow the
723 * pool, or the minimum is at V4L2 maximum, enabled copy on threshold
724 * https://bugzilla.gnome.org/show_bug.cgi?id=732288 */
725 if (!can_allocate || min_buffers == VIDEO_MAX_FRAME)
726 copy_threshold = min_latency;
729 pool->copy_threshold = copy_threshold;
730 pool->max_latency = max_latency;
731 pool->min_latency = min_latency;
732 pool->num_queued = 0;
734 if (max_buffers < min_buffers)
735 max_buffers = min_buffers;
737 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
739 pclass->set_config (bpool, config);
740 gst_structure_free (config);
742 if (pool->other_pool)
743 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
744 goto other_pool_failed;
746 /* now, allocate the buffers: */
747 if (!pclass->start (bpool))
750 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
751 pool->group_released_handler =
752 g_signal_connect_swapped (pool->vallocator, "group-released",
753 G_CALLBACK (gst_v4l2_buffer_pool_group_released), pool);
760 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
761 gst_structure_free (config);
766 GST_ERROR_OBJECT (pool,
767 "we received %d buffer from device '%s', we want at least %d",
768 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
769 gst_structure_free (config);
774 GST_ERROR_OBJECT (pool, "failed to start streaming");
779 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
780 GST_PTR_FORMAT, pool->other_pool);
786 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
788 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
789 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
793 GST_DEBUG_OBJECT (pool, "stopping pool");
795 if (pool->group_released_handler > 0) {
796 g_signal_handler_disconnect (pool->vallocator,
797 pool->group_released_handler);
798 pool->group_released_handler = 0;
801 if (pool->other_pool) {
802 gst_object_unref (pool->other_pool);
803 pool->other_pool = NULL;
806 if (!gst_v4l2_buffer_pool_streamoff (pool))
807 goto streamoff_failed;
809 gst_v4l2_allocator_flush (pool->vallocator);
811 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
812 if (pool->buffers[i]) {
813 GstBuffer *buffer = pool->buffers[i];
815 pool->buffers[i] = NULL;
817 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
818 gst_buffer_unref (buffer);
820 pclass->release_buffer (bpool, buffer);
822 g_atomic_int_add (&pool->num_queued, -1);
826 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
831 vret = gst_v4l2_allocator_stop (pool->vallocator);
833 if (vret == GST_V4L2_BUSY)
834 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
836 ret = (vret == GST_V4L2_OK);
843 GST_ERROR_OBJECT (pool, "device refused to stop streaming");
848 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
850 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
852 GST_DEBUG_OBJECT (pool, "start flushing");
854 gst_poll_set_flushing (pool->poll, TRUE);
856 GST_OBJECT_LOCK (pool);
858 g_cond_broadcast (&pool->empty_cond);
859 GST_OBJECT_UNLOCK (pool);
861 if (pool->other_pool)
862 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
866 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
868 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
869 GstV4l2Object *obj = pool->obj;
872 GST_DEBUG_OBJECT (pool, "stop flushing");
874 /* If we haven't started streaming yet, simply call streamon */
875 if (!pool->streaming)
878 if (pool->other_pool)
879 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
881 if (!gst_v4l2_buffer_pool_streamoff (pool))
884 gst_v4l2_allocator_flush (pool->vallocator);
886 /* Reset our state */
890 case GST_V4L2_IO_MMAP:
891 case GST_V4L2_IO_USERPTR:
892 case GST_V4L2_IO_DMABUF:
893 case GST_V4L2_IO_DMABUF_IMPORT:
897 num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
899 for (i = 0; i < num_allocated; i++) {
900 /* Re-enqueue buffers */
901 if (pool->buffers[i]) {
902 GstBufferPool *bpool = (GstBufferPool *) pool;
903 GstBuffer *buffer = pool->buffers[i];
905 pool->buffers[i] = NULL;
907 /* Remove qdata, this will unmap any map data in
908 * userptr/dmabuf-import */
909 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
910 GST_V4L2_IMPORT_QUARK, NULL, NULL);
912 if (V4L2_TYPE_IS_OUTPUT (obj->type))
913 gst_buffer_unref (buffer);
915 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
917 g_atomic_int_add (&pool->num_queued, -1);
924 g_assert_not_reached ();
929 /* Start streaming on capture device only */
930 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
931 gst_v4l2_buffer_pool_streamon (pool);
933 gst_poll_set_flushing (pool->poll, FALSE);
940 GST_ERROR_OBJECT (pool, "device refused to flush");
945 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
949 GST_OBJECT_LOCK (pool);
951 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
952 GST_OBJECT_UNLOCK (pool);
954 if (!pool->can_poll_device)
957 GST_LOG_OBJECT (pool, "polling device");
960 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
961 if (G_UNLIKELY (ret < 0)) {
969 GST_WARNING_OBJECT (pool,
970 "v4l2 device doesn't support polling. Disabling"
971 " using libv4l2 in this case may cause deadlocks");
972 pool->can_poll_device = FALSE;
979 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
988 GST_DEBUG_OBJECT (pool, "stop called");
989 return GST_FLOW_FLUSHING;
993 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
994 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
995 return GST_FLOW_ERROR;
1000 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1002 GstV4l2MemoryGroup *group = NULL;
1005 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1006 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1007 gst_buffer_unref (buf);
1011 index = group->buffer.index;
1013 if (pool->buffers[index] != NULL)
1014 goto already_queued;
1016 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1018 g_atomic_int_inc (&pool->num_queued);
1019 pool->buffers[index] = buf;
1021 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1024 GST_OBJECT_LOCK (pool);
1025 pool->empty = FALSE;
1026 g_cond_signal (&pool->empty_cond);
1027 GST_OBJECT_UNLOCK (pool);
1033 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1034 return GST_FLOW_ERROR;
1038 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1039 /* Mark broken buffer to the allocator */
1040 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1041 g_atomic_int_add (&pool->num_queued, -1);
1042 pool->buffers[index] = NULL;
1043 return GST_FLOW_ERROR;
1047 static GstFlowReturn
1048 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1052 GstV4l2Object *obj = pool->obj;
1053 GstClockTime timestamp;
1054 GstV4l2MemoryGroup *group;
1057 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1060 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1062 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1066 /* get our GstBuffer with that index from the pool, if the buffer was
1067 * outstanding we have a serious problem.
1069 outbuf = pool->buffers[group->buffer.index];
1073 /* mark the buffer outstanding */
1074 pool->buffers[group->buffer.index] = NULL;
1075 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1076 GST_OBJECT_LOCK (pool);
1078 GST_OBJECT_UNLOCK (pool);
1081 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1083 #ifndef GST_DISABLE_GST_DEBUG
1084 for (i = 0; i < group->n_mem; i++) {
1085 GST_LOG_OBJECT (pool,
1086 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1087 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1088 group->buffer.sequence, group->buffer.index, group->mem[i],
1089 group->planes[i].bytesused, i, group->buffer.flags,
1090 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1094 /* set top/bottom field first if v4l2_buffer has the information */
1095 if (group->buffer.field == V4L2_FIELD_INTERLACED_TB) {
1096 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1097 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1098 } else if (group->buffer.field == V4L2_FIELD_INTERLACED_BT) {
1099 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1100 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1102 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1103 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1106 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1107 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1108 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1110 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1113 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1122 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1127 return GST_FLOW_ERROR;
1131 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1132 group->buffer.index);
1133 return GST_FLOW_ERROR;
1137 static GstFlowReturn
1138 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1139 GstBufferPoolAcquireParams * params)
1142 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1143 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1144 GstV4l2Object *obj = pool->obj;
1146 GST_DEBUG_OBJECT (pool, "acquire");
1148 /* If this is being called to resurect a lost buffer */
1149 if (params && params->flags & GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT) {
1150 ret = pclass->acquire_buffer (bpool, buffer, params);
1154 switch (obj->type) {
1155 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1156 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1157 /* capture, This function should return a buffer with new captured data */
1158 switch (obj->mode) {
1159 case GST_V4L2_IO_RW:
1161 /* take empty buffer from the pool */
1162 ret = pclass->acquire_buffer (bpool, buffer, params);
1165 case GST_V4L2_IO_DMABUF:
1166 case GST_V4L2_IO_MMAP:
1168 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1169 * storage for our buffers. This function does poll first so we can
1170 * interrupt it fine. */
1171 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1172 if (G_UNLIKELY (ret != GST_FLOW_OK))
1175 /* start copying buffers when we are running low on buffers */
1176 if (g_atomic_int_get (&pool->num_queued) < pool->copy_threshold) {
1179 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1180 if (pclass->acquire_buffer (bpool, ©, params) == GST_FLOW_OK) {
1181 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
1186 /* copy the buffer */
1187 copy = gst_buffer_copy_region (*buffer,
1188 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1189 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
1191 /* and requeue so that we can continue capturing */
1192 gst_v4l2_buffer_pool_release_buffer (bpool, *buffer);
1197 case GST_V4L2_IO_USERPTR:
1198 case GST_V4L2_IO_DMABUF_IMPORT:
1200 /* dequeue filled buffer */
1201 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1205 ret = GST_FLOW_ERROR;
1206 g_assert_not_reached ();
1212 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1213 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1214 /* playback, This function should return an empty buffer */
1215 switch (obj->mode) {
1216 case GST_V4L2_IO_RW:
1217 /* get an empty buffer */
1218 ret = pclass->acquire_buffer (bpool, buffer, params);
1221 case GST_V4L2_IO_MMAP:
1222 case GST_V4L2_IO_DMABUF:
1223 case GST_V4L2_IO_USERPTR:
1224 case GST_V4L2_IO_DMABUF_IMPORT:
1225 /* get a free unqueued buffer */
1226 ret = pclass->acquire_buffer (bpool, buffer, params);
1230 ret = GST_FLOW_ERROR;
1231 g_assert_not_reached ();
1237 ret = GST_FLOW_ERROR;
1238 g_assert_not_reached ();
1246 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1248 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1249 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1250 GstV4l2Object *obj = pool->obj;
1252 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1254 switch (obj->type) {
1255 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1256 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1257 /* capture, put the buffer back in the queue so that we can refill it
1259 switch (obj->mode) {
1260 case GST_V4L2_IO_RW:
1261 /* release back in the pool */
1262 pclass->release_buffer (bpool, buffer);
1265 case GST_V4L2_IO_DMABUF:
1266 case GST_V4L2_IO_MMAP:
1267 case GST_V4L2_IO_USERPTR:
1268 case GST_V4L2_IO_DMABUF_IMPORT:
1270 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1271 /* queue back in the device */
1272 if (pool->other_pool)
1273 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1274 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1275 pclass->release_buffer (bpool, buffer);
1277 /* Simply release invalide/modified buffer, the allocator will
1278 * give it back later */
1279 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1280 pclass->release_buffer (bpool, buffer);
1285 g_assert_not_reached ();
1290 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1291 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1292 switch (obj->mode) {
1293 case GST_V4L2_IO_RW:
1294 /* release back in the pool */
1295 pclass->release_buffer (bpool, buffer);
1298 case GST_V4L2_IO_MMAP:
1299 case GST_V4L2_IO_DMABUF:
1300 case GST_V4L2_IO_USERPTR:
1301 case GST_V4L2_IO_DMABUF_IMPORT:
1303 GstV4l2MemoryGroup *group;
1306 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1307 /* Simply release invalide/modified buffer, the allocator will
1308 * give it back later */
1309 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1310 pclass->release_buffer (bpool, buffer);
1314 index = group->buffer.index;
1316 if (pool->buffers[index] == NULL) {
1317 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1320 /* Remove qdata, this will unmap any map data in userptr */
1321 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1322 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1324 /* reset to default size */
1325 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1327 /* playback, put the buffer back in the queue to refill later. */
1328 pclass->release_buffer (bpool, buffer);
1330 /* We keep a ref on queued buffer, so this should never happen */
1331 g_assert_not_reached ();
1337 g_assert_not_reached ();
1343 g_assert_not_reached ();
1349 gst_v4l2_buffer_pool_finalize (GObject * object)
1351 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1354 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1355 if (pool->buffers[i])
1356 gst_buffer_replace (&(pool->buffers[i]), NULL);
1359 if (pool->video_fd >= 0)
1360 v4l2_close (pool->video_fd);
1362 gst_poll_free (pool->poll);
1364 if (pool->vallocator)
1365 gst_object_unref (pool->vallocator);
1367 if (pool->allocator)
1368 gst_object_unref (pool->allocator);
1370 if (pool->other_pool)
1371 gst_object_unref (pool->other_pool);
1373 /* FIXME Is this required to keep around ? */
1374 gst_object_unref (pool->obj->element);
1376 /* FIXME have we done enough here ? */
1378 G_OBJECT_CLASS (parent_class)->finalize (object);
1382 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1384 pool->poll = gst_poll_new (TRUE);
1385 pool->can_poll_device = TRUE;
1386 g_cond_init (&pool->empty_cond);
1391 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1393 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1394 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1396 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1398 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1399 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1400 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1401 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1402 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1403 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1404 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1405 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1409 * gst_v4l2_buffer_pool_new:
1410 * @obj: the v4l2 object owning the pool
1412 * Construct a new buffer pool.
1414 * Returns: the new pool, use gst_object_unref() to free resources
1417 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1419 GstV4l2BufferPool *pool;
1420 GstStructure *config;
1421 gchar *name, *parent_name;
1424 fd = v4l2_dup (obj->video_fd);
1428 /* setting a significant unique name */
1429 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1430 name = g_strconcat (parent_name, ":", "pool:",
1431 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1432 g_free (parent_name);
1434 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1435 "name", name, NULL);
1438 gst_poll_fd_init (&pool->pollfd);
1439 pool->pollfd.fd = fd;
1440 gst_poll_add_fd (pool->poll, &pool->pollfd);
1441 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1442 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1444 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1446 pool->video_fd = fd;
1448 pool->can_poll_device = TRUE;
1451 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1452 if (pool->vallocator == NULL)
1453 goto allocator_failed;
1455 gst_object_ref (obj->element);
1457 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1458 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1459 /* This will simply set a default config, but will not configure the pool
1460 * because min and max are not valid */
1461 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1463 return GST_BUFFER_POOL (pool);
1468 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1473 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1478 static GstFlowReturn
1479 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1482 GstV4l2Object *obj = pool->obj;
1487 toread = obj->info.size;
1489 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1491 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1494 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1497 amount = v4l2_read (obj->video_fd, map.data, toread);
1499 if (amount == toread) {
1501 } else if (amount == -1) {
1502 if (errno == EAGAIN || errno == EINTR) {
1507 /* short reads can happen if a signal interrupts the read */
1512 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1513 gst_buffer_unmap (buf, &map);
1514 gst_buffer_resize (buf, 0, amount);
1521 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1526 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1527 (_("Error reading %d bytes from device '%s'."),
1528 toread, obj->videodev), GST_ERROR_SYSTEM);
1529 res = GST_FLOW_ERROR;
1534 gst_buffer_unmap (buf, &map);
1535 gst_buffer_resize (buf, 0, 0);
1541 * gst_v4l2_buffer_pool_process:
1542 * @bpool: a #GstBufferPool
1543 * @buf: a #GstBuffer, maybe be replaced
1545 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1546 * data from the device. For output devices, this functions send the contents of
1547 * @buf to the device for playback.
1549 * Returns: %GST_FLOW_OK on success.
1552 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1554 GstFlowReturn ret = GST_FLOW_OK;
1555 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1556 GstV4l2Object *obj = pool->obj;
1558 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1560 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1562 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1563 return GST_FLOW_FLUSHING;
1565 switch (obj->type) {
1566 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1567 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1569 switch (obj->mode) {
1570 case GST_V4L2_IO_RW:
1571 /* capture into the buffer */
1572 ret = gst_v4l2_do_read (pool, *buf);
1575 case GST_V4L2_IO_MMAP:
1576 case GST_V4L2_IO_DMABUF:
1580 if ((*buf)->pool == bpool) {
1581 if (gst_buffer_get_size (*buf) == 0)
1584 /* nothing, data was inside the buffer when we did _acquire() */
1588 /* buffer not from our pool, grab a frame and copy it into the target */
1589 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1592 /* An empty buffer on capture indicates the end of stream */
1593 if (gst_buffer_get_size (tmp) == 0) {
1594 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1598 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1600 /* an queue the buffer again after the copy */
1601 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1603 if (ret != GST_FLOW_OK)
1608 case GST_V4L2_IO_USERPTR:
1610 struct UserPtrData *data;
1612 /* Replace our buffer with downstream allocated buffer */
1613 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1614 GST_V4L2_IMPORT_QUARK);
1615 gst_buffer_replace (buf, data->buffer);
1616 _unmap_userptr_frame (data);
1620 case GST_V4L2_IO_DMABUF_IMPORT:
1624 /* Replace our buffer with downstream allocated buffer */
1625 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1626 GST_V4L2_IMPORT_QUARK);
1627 gst_buffer_replace (buf, tmp);
1628 gst_buffer_unref (tmp);
1633 g_assert_not_reached ();
1638 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1639 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1641 switch (obj->mode) {
1642 case GST_V4L2_IO_RW:
1643 /* FIXME, do write() */
1644 GST_WARNING_OBJECT (pool, "implement write()");
1647 case GST_V4L2_IO_USERPTR:
1648 case GST_V4L2_IO_DMABUF_IMPORT:
1649 case GST_V4L2_IO_DMABUF:
1650 case GST_V4L2_IO_MMAP:
1652 GstBuffer *to_queue = NULL;
1653 GstV4l2MemoryGroup *group;
1656 if ((*buf)->pool != bpool)
1659 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1662 index = group->buffer.index;
1664 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1666 index = group->buffer.index;
1667 if (pool->buffers[index] != NULL) {
1668 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1672 /* we can queue directly */
1673 to_queue = gst_buffer_ref (*buf);
1676 if (to_queue == NULL) {
1677 GstBufferPoolAcquireParams params = { 0 };
1679 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1681 /* this can return EOS if all buffers are outstanding which would
1682 * be strange because we would expect the upstream element to have
1683 * allocated them and returned to us.. */
1684 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1685 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1686 if (ret != GST_FLOW_OK)
1687 goto acquire_failed;
1689 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1690 if (ret != GST_FLOW_OK) {
1691 gst_buffer_unref (to_queue);
1692 goto prepare_failed;
1696 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1699 /* if we are not streaming yet (this is the first buffer, start
1701 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1702 gst_buffer_unref (to_queue);
1706 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1708 /* all buffers are queued, try to dequeue one and release it back
1709 * into the pool so that _acquire can get to it again. */
1710 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1711 if (ret == GST_FLOW_OK)
1712 /* release the rendered buffer back into the pool. This wakes up any
1713 * thread waiting for a buffer in _acquire(). */
1714 gst_buffer_unref (out);
1719 g_assert_not_reached ();
1724 g_assert_not_reached ();
1733 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1738 GST_DEBUG_OBJECT (pool, "end of stream reached");
1739 return GST_FLOW_EOS;
1743 if (ret == GST_FLOW_FLUSHING)
1744 GST_DEBUG_OBJECT (pool, "flushing");
1746 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1747 gst_flow_get_name (ret));
1752 GST_ERROR_OBJECT (pool, "failed to prepare data");
1757 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1762 GST_ERROR_OBJECT (pool, "failed to start streaming");
1763 return GST_FLOW_ERROR;
1768 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1769 GstBufferPool * other_pool)
1771 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1773 if (pool->other_pool)
1774 gst_object_unref (pool->other_pool);
1775 pool->other_pool = gst_object_ref (other_pool);