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))
83 if (gst_is_v4l2_memory (mem)) {
84 GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
95 gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
98 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
100 GST_LOG_OBJECT (pool, "copying buffer");
102 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
103 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
104 GstVideoFrame src_frame, dest_frame;
106 GST_DEBUG_OBJECT (pool, "copy video frame");
108 /* we have raw video, use videoframe copy to get strides right */
109 if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
112 if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
114 gst_video_frame_unmap (&src_frame);
118 gst_video_frame_copy (&dest_frame, &src_frame);
120 gst_video_frame_unmap (&src_frame);
121 gst_video_frame_unmap (&dest_frame);
125 GST_DEBUG_OBJECT (pool, "copy raw bytes");
127 if (!gst_buffer_map (src, &map, GST_MAP_READ))
130 gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
132 gst_buffer_unmap (src, &map);
133 gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
136 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
143 GST_ERROR_OBJECT (pool, "could not map buffer");
144 return GST_FLOW_ERROR;
157 gst_v4l2_buffer_pool_import_quark (void)
159 static GQuark quark = 0;
162 quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
168 _unmap_userptr_frame (struct UserPtrData *data)
171 gst_video_frame_unmap (&data->frame);
173 gst_buffer_unmap (data->buffer, &data->map);
176 gst_buffer_unref (data->buffer);
178 g_slice_free (struct UserPtrData, data);
182 gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
183 GstBuffer * dest, GstBuffer * src)
185 GstFlowReturn ret = GST_FLOW_OK;
186 GstV4l2MemoryGroup *group = NULL;
188 const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
189 struct UserPtrData *data = NULL;
191 GST_LOG_OBJECT (pool, "importing userptr");
194 if (!gst_v4l2_is_buffer_valid (dest, &group))
197 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
198 flags = GST_MAP_READ;
200 flags = GST_MAP_WRITE;
202 data = g_slice_new0 (struct UserPtrData);
204 if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
205 finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
206 data->is_frame = TRUE;
208 if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
211 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
212 data->frame.info.size, finfo->n_planes, data->frame.data,
213 data->frame.info.offset))
216 gsize offset[1] = { 0 };
219 data->is_frame = FALSE;
221 if (!gst_buffer_map (src, &data->map, flags))
224 ptr[0] = data->map.data;
226 if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
227 data->map.size, 1, ptr, offset))
231 data->buffer = gst_buffer_ref (src);
233 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
234 data, (GDestroyNotify) _unmap_userptr_frame);
240 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
241 return GST_FLOW_ERROR;
245 GST_ERROR_OBJECT (pool, "could not map buffer");
246 g_slice_free (struct UserPtrData, data);
247 return GST_FLOW_ERROR;
251 GST_ERROR_OBJECT (pool, "failed to import data");
252 _unmap_userptr_frame (data);
253 return GST_FLOW_ERROR;
258 gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
259 GstBuffer * dest, GstBuffer * src)
261 GstV4l2MemoryGroup *group = NULL;
262 GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
263 guint n_mem = gst_buffer_n_memory (src);
266 GST_LOG_OBJECT (pool, "importing dmabuf");
268 if (!gst_v4l2_is_buffer_valid (dest, &group))
271 if (n_mem > GST_VIDEO_MAX_PLANES)
274 for (i = 0; i < n_mem; i++)
275 dma_mem[i] = gst_buffer_peek_memory (src, i);
277 if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
281 gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
282 gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
288 GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
289 return GST_FLOW_ERROR;
293 GST_ERROR_OBJECT (pool, "could not map buffer");
294 return GST_FLOW_ERROR;
298 GST_ERROR_OBJECT (pool, "failed to import dmabuf");
299 return GST_FLOW_ERROR;
304 gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
305 GstBuffer * dest, GstBuffer * src)
307 GstFlowReturn ret = GST_FLOW_OK;
308 gboolean own_src = FALSE;
311 if (pool->other_pool == NULL) {
312 GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
313 return GST_FLOW_ERROR;
316 ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
317 if (ret != GST_FLOW_OK) {
318 GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
325 switch (pool->obj->mode) {
326 case GST_V4L2_IO_MMAP:
327 case GST_V4L2_IO_DMABUF:
328 ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
330 case GST_V4L2_IO_USERPTR:
331 ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
333 case GST_V4L2_IO_DMABUF_IMPORT:
334 ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
341 gst_buffer_unref (src);
348 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
349 GstBufferPoolAcquireParams * params)
351 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
352 GstV4l2MemoryGroup *group = NULL;
353 GstBuffer *newbuf = NULL;
363 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
365 case GST_V4L2_IO_MMAP:
366 group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
368 case GST_V4L2_IO_DMABUF:
369 group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
372 case GST_V4L2_IO_USERPTR:
373 group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
375 case GST_V4L2_IO_DMABUF_IMPORT:
376 group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
380 g_assert_not_reached ();
386 newbuf = gst_buffer_new ();
388 for (i = 0; i < group->n_mem; i++)
389 gst_buffer_append_memory (newbuf, group->mem[i]);
390 } else if (newbuf == NULL) {
391 goto allocation_failed;
394 /* add metadata to raw video buffers */
395 if (pool->add_videometa)
396 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
397 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
398 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
399 info->offset, info->stride);
408 GST_ERROR_OBJECT (pool, "failed to allocate buffer");
409 return GST_FLOW_ERROR;
414 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
416 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
417 GstV4l2Object *obj = pool->obj;
419 guint size, min_buffers, max_buffers;
420 GstAllocator *allocator;
421 GstAllocationParams params;
422 gboolean can_allocate = FALSE;
423 gboolean updated = FALSE;
426 pool->add_videometa =
427 gst_buffer_pool_config_has_option (config,
428 GST_BUFFER_POOL_OPTION_VIDEO_META);
430 /* parse the config and keep around */
431 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
435 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
438 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
441 gst_object_unref (pool->allocator);
442 pool->allocator = NULL;
445 case GST_V4L2_IO_DMABUF:
446 pool->allocator = gst_dmabuf_allocator_new ();
447 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
449 case GST_V4L2_IO_MMAP:
450 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
452 case GST_V4L2_IO_USERPTR:
454 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
456 case GST_V4L2_IO_DMABUF_IMPORT:
457 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
460 pool->allocator = g_object_ref (allocator);
461 pool->params = params;
462 /* No need to change the configuration */
466 g_assert_not_reached ();
470 if (min_buffers < GST_V4L2_MIN_BUFFERS) {
472 min_buffers = GST_V4L2_MIN_BUFFERS;
473 GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
476 if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
478 max_buffers = VIDEO_MAX_FRAME;
479 GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
482 if (min_buffers > max_buffers) {
484 min_buffers = max_buffers;
485 GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
486 } else if (min_buffers != max_buffers) {
489 max_buffers = min_buffers;
490 GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
494 if (!pool->add_videometa && obj->need_video_meta) {
495 GST_INFO_OBJECT (pool, "adding needed video meta");
497 gst_buffer_pool_config_add_option (config,
498 GST_BUFFER_POOL_OPTION_VIDEO_META);
502 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
505 /* keep a GstVideoInfo with defaults for the when we need to copy */
506 gst_video_info_from_caps (&pool->caps_info, caps);
509 ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
511 /* If anything was changed documentation recommand to return FALSE */
512 return !updated && ret;
517 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
523 gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
525 GstV4l2Object *obj = pool->obj;
528 case GST_V4L2_IO_MMAP:
529 case GST_V4L2_IO_USERPTR:
530 case GST_V4L2_IO_DMABUF:
531 case GST_V4L2_IO_DMABUF_IMPORT:
532 if (!pool->streaming) {
533 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
534 goto streamon_failed;
536 pool->streaming = TRUE;
538 GST_DEBUG_OBJECT (pool, "Started streaming");
549 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
556 gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
558 GstV4l2Object *obj = pool->obj;
561 case GST_V4L2_IO_MMAP:
562 case GST_V4L2_IO_USERPTR:
563 case GST_V4L2_IO_DMABUF:
564 case GST_V4L2_IO_DMABUF_IMPORT:
565 if (pool->streaming) {
566 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
567 goto streamoff_failed;
569 pool->streaming = FALSE;
571 GST_DEBUG_OBJECT (pool, "Stopped streaming");
582 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
589 gst_v4l2_buffer_pool_group_released (GstV4l2BufferPool * pool)
591 GstBufferPoolAcquireParams params = { 0 };
592 GstBuffer *buffer = NULL;
595 GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
598 (GstBufferPoolAcquireFlags) GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT;
600 gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
602 if (ret == GST_FLOW_OK)
603 gst_buffer_unref (buffer);
607 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
609 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
610 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
611 GstV4l2Object *obj = pool->obj;
612 GstStructure *config;
614 guint size, min_buffers, max_buffers;
615 guint max_latency, min_latency, copy_threshold = 0;
616 gboolean can_allocate = FALSE;
618 GST_DEBUG_OBJECT (pool, "activating pool");
620 config = gst_buffer_pool_get_config (bpool);
621 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
625 /* TODO Also consider min_buffers_for_output when implemented */
626 min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers_for_capture);
632 case GST_V4L2_IO_DMABUF:
633 case GST_V4L2_IO_MMAP:
637 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
639 /* first, lets request buffers, and see how many we can get: */
640 GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
642 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
645 if (count < GST_V4L2_MIN_BUFFERS) {
650 /* V4L2 buffer pool are often very limited in the amount of buffers it
651 * can offer. The copy_threshold will workaround this limitation by
652 * falling back to copy if the pipeline needed more buffers. This also
653 * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
655 if (count != min_buffers) {
656 GST_WARNING_OBJECT (pool, "using %u buffers instead of %u",
659 copy_threshold = min_latency;
661 /* The initial minimum could be provide either by GstBufferPool or
668 case GST_V4L2_IO_USERPTR:
673 GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
675 GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
677 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
678 V4L2_MEMORY_USERPTR);
680 /* There is no rational to not get what we asked */
681 if (count < min_buffers) {
689 case GST_V4L2_IO_DMABUF_IMPORT:
693 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
695 GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
697 count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
700 /* There is no rational to not get what we asked */
701 if (count < min_buffers) {
712 g_assert_not_reached ();
717 max_latency = max_buffers;
719 max_latency = min_buffers;
721 /* FIXME Encoder don't negotiate amount of buffers. If we can't grow the
722 * pool, or the minimum is at V4L2 maximum, enabled copy on threshold
723 * https://bugzilla.gnome.org/show_bug.cgi?id=732288 */
724 if (!can_allocate || min_buffers == VIDEO_MAX_FRAME)
725 copy_threshold = min_latency;
728 pool->copy_threshold = copy_threshold;
729 pool->max_latency = max_latency;
730 pool->min_latency = min_latency;
731 pool->num_queued = 0;
733 if (max_buffers < min_buffers)
734 max_buffers = min_buffers;
736 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
738 pclass->set_config (bpool, config);
739 gst_structure_free (config);
741 if (pool->other_pool)
742 if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
743 goto other_pool_failed;
745 /* now, allocate the buffers: */
746 if (!pclass->start (bpool))
749 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
750 pool->group_released_handler =
751 g_signal_connect_swapped (pool->vallocator, "group-released",
752 G_CALLBACK (gst_v4l2_buffer_pool_group_released), pool);
759 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
760 gst_structure_free (config);
765 GST_ERROR_OBJECT (pool,
766 "we received %d buffer from device '%s', we want at least %d",
767 min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
768 gst_structure_free (config);
773 GST_ERROR_OBJECT (pool, "failed to start streaming");
778 GST_ERROR_OBJECT (pool, "failed to active the other pool %"
779 GST_PTR_FORMAT, pool->other_pool);
785 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
787 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
788 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
792 GST_DEBUG_OBJECT (pool, "stopping pool");
794 if (pool->group_released_handler > 0) {
795 g_signal_handler_disconnect (pool->vallocator,
796 pool->group_released_handler);
797 pool->group_released_handler = 0;
800 if (pool->other_pool) {
801 gst_object_unref (pool->other_pool);
802 pool->other_pool = NULL;
805 if (!gst_v4l2_buffer_pool_streamoff (pool))
806 goto streamoff_failed;
808 gst_v4l2_allocator_flush (pool->vallocator);
810 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
811 if (pool->buffers[i]) {
812 GstBuffer *buffer = pool->buffers[i];
814 pool->buffers[i] = NULL;
816 if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
817 gst_buffer_unref (buffer);
819 pclass->release_buffer (bpool, buffer);
821 g_atomic_int_add (&pool->num_queued, -1);
825 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
830 vret = gst_v4l2_allocator_stop (pool->vallocator);
832 if (vret == GST_V4L2_BUSY)
833 GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
835 ret = (vret == GST_V4L2_OK);
842 GST_ERROR_OBJECT (pool, "device refused to stop streaming");
847 gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
849 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
851 GST_DEBUG_OBJECT (pool, "start flushing");
853 gst_poll_set_flushing (pool->poll, TRUE);
855 GST_OBJECT_LOCK (pool);
857 g_cond_broadcast (&pool->empty_cond);
858 GST_OBJECT_UNLOCK (pool);
860 if (pool->other_pool)
861 gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
865 gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
867 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
868 GstV4l2Object *obj = pool->obj;
871 GST_DEBUG_OBJECT (pool, "stop flushing");
873 /* If we haven't started streaming yet, simply call streamon */
874 if (!pool->streaming)
877 if (pool->other_pool)
878 gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
880 if (!gst_v4l2_buffer_pool_streamoff (pool))
883 gst_v4l2_allocator_flush (pool->vallocator);
885 /* Reset our state */
889 case GST_V4L2_IO_MMAP:
890 case GST_V4L2_IO_USERPTR:
891 case GST_V4L2_IO_DMABUF:
892 case GST_V4L2_IO_DMABUF_IMPORT:
896 num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
898 for (i = 0; i < num_allocated; i++) {
899 /* Re-enqueue buffers */
900 if (pool->buffers[i]) {
901 GstBufferPool *bpool = (GstBufferPool *) pool;
902 GstBuffer *buffer = pool->buffers[i];
904 pool->buffers[i] = NULL;
906 /* Remove qdata, this will unmap any map data in
907 * userptr/dmabuf-import */
908 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
909 GST_V4L2_IMPORT_QUARK, NULL, NULL);
911 if (V4L2_TYPE_IS_OUTPUT (obj->type))
912 gst_buffer_unref (buffer);
914 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
916 g_atomic_int_add (&pool->num_queued, -1);
923 g_assert_not_reached ();
928 /* Start streaming on capture device only */
929 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
930 gst_v4l2_buffer_pool_streamon (pool);
932 gst_poll_set_flushing (pool->poll, FALSE);
939 GST_ERROR_OBJECT (pool, "device refused to flush");
944 gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool)
948 GST_OBJECT_LOCK (pool);
950 g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
951 GST_OBJECT_UNLOCK (pool);
953 if (!pool->can_poll_device)
956 GST_LOG_OBJECT (pool, "polling device");
959 ret = gst_poll_wait (pool->poll, GST_CLOCK_TIME_NONE);
960 if (G_UNLIKELY (ret < 0)) {
968 GST_WARNING_OBJECT (pool,
969 "v4l2 device doesn't support polling. Disabling"
970 " using libv4l2 in this case may cause deadlocks");
971 pool->can_poll_device = FALSE;
978 if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
987 GST_DEBUG_OBJECT (pool, "stop called");
988 return GST_FLOW_FLUSHING;
992 GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
993 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
994 return GST_FLOW_ERROR;
999 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
1001 GstV4l2MemoryGroup *group = NULL;
1004 if (!gst_v4l2_is_buffer_valid (buf, &group)) {
1005 GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
1006 gst_buffer_unref (buf);
1010 index = group->buffer.index;
1012 if (pool->buffers[index] != NULL)
1013 goto already_queued;
1015 GST_LOG_OBJECT (pool, "queuing buffer %i", index);
1017 g_atomic_int_inc (&pool->num_queued);
1018 pool->buffers[index] = buf;
1020 if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
1023 GST_OBJECT_LOCK (pool);
1024 pool->empty = FALSE;
1025 g_cond_signal (&pool->empty_cond);
1026 GST_OBJECT_UNLOCK (pool);
1032 GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
1033 return GST_FLOW_ERROR;
1037 GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
1038 /* Mark broken buffer to the allocator */
1039 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
1040 g_atomic_int_add (&pool->num_queued, -1);
1041 pool->buffers[index] = NULL;
1042 return GST_FLOW_ERROR;
1046 static GstFlowReturn
1047 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
1051 GstV4l2Object *obj = pool->obj;
1052 GstClockTime timestamp;
1053 GstV4l2MemoryGroup *group;
1056 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1059 GST_LOG_OBJECT (pool, "dequeueing a buffer");
1061 group = gst_v4l2_allocator_dqbuf (pool->vallocator);
1065 /* get our GstBuffer with that index from the pool, if the buffer was
1066 * outstanding we have a serious problem.
1068 outbuf = pool->buffers[group->buffer.index];
1072 /* mark the buffer outstanding */
1073 pool->buffers[group->buffer.index] = NULL;
1074 if (g_atomic_int_dec_and_test (&pool->num_queued)) {
1075 GST_OBJECT_LOCK (pool);
1077 GST_OBJECT_UNLOCK (pool);
1080 timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
1082 #ifndef GST_DISABLE_GST_DEBUG
1083 for (i = 0; i < group->n_mem; i++) {
1084 GST_LOG_OBJECT (pool,
1085 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
1086 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
1087 group->buffer.sequence, group->buffer.index, group->mem[i],
1088 group->planes[i].bytesused, i, group->buffer.flags,
1089 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
1093 /* set top/bottom field first if v4l2_buffer has the information */
1094 if (group->buffer.field == V4L2_FIELD_INTERLACED_TB) {
1095 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1096 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1097 } else if (group->buffer.field == V4L2_FIELD_INTERLACED_BT) {
1098 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1099 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1101 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1102 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
1105 if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
1106 if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
1107 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1109 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
1112 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1121 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1126 return GST_FLOW_ERROR;
1130 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1131 group->buffer.index);
1132 return GST_FLOW_ERROR;
1136 static GstFlowReturn
1137 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1138 GstBufferPoolAcquireParams * params)
1141 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1142 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1143 GstV4l2Object *obj = pool->obj;
1145 GST_DEBUG_OBJECT (pool, "acquire");
1147 /* If this is being called to resurect a lost buffer */
1148 if (params && params->flags & GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT) {
1149 ret = pclass->acquire_buffer (bpool, buffer, params);
1153 switch (obj->type) {
1154 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1155 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1156 /* capture, This function should return a buffer with new captured data */
1157 switch (obj->mode) {
1158 case GST_V4L2_IO_RW:
1160 /* take empty buffer from the pool */
1161 ret = pclass->acquire_buffer (bpool, buffer, params);
1164 case GST_V4L2_IO_DMABUF:
1165 case GST_V4L2_IO_MMAP:
1167 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1168 * storage for our buffers. This function does poll first so we can
1169 * interrupt it fine. */
1170 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1171 if (G_UNLIKELY (ret != GST_FLOW_OK))
1174 /* start copying buffers when we are running low on buffers */
1175 if (g_atomic_int_get (&pool->num_queued) < pool->copy_threshold) {
1178 if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
1179 if (pclass->acquire_buffer (bpool, ©, params) == GST_FLOW_OK) {
1180 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
1185 /* copy the buffer */
1186 copy = gst_buffer_copy_region (*buffer,
1187 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1188 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
1190 /* and requeue so that we can continue capturing */
1191 gst_v4l2_buffer_pool_release_buffer (bpool, *buffer);
1196 case GST_V4L2_IO_USERPTR:
1197 case GST_V4L2_IO_DMABUF_IMPORT:
1199 /* dequeue filled buffer */
1200 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1204 ret = GST_FLOW_ERROR;
1205 g_assert_not_reached ();
1211 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1212 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1213 /* playback, This function should return an empty buffer */
1214 switch (obj->mode) {
1215 case GST_V4L2_IO_RW:
1216 /* get an empty buffer */
1217 ret = pclass->acquire_buffer (bpool, buffer, params);
1220 case GST_V4L2_IO_MMAP:
1221 case GST_V4L2_IO_DMABUF:
1222 case GST_V4L2_IO_USERPTR:
1223 case GST_V4L2_IO_DMABUF_IMPORT:
1224 /* get a free unqueued buffer */
1225 ret = pclass->acquire_buffer (bpool, buffer, params);
1229 ret = GST_FLOW_ERROR;
1230 g_assert_not_reached ();
1236 ret = GST_FLOW_ERROR;
1237 g_assert_not_reached ();
1245 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1247 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1248 GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
1249 GstV4l2Object *obj = pool->obj;
1251 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1253 switch (obj->type) {
1254 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1255 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1256 /* capture, put the buffer back in the queue so that we can refill it
1258 switch (obj->mode) {
1259 case GST_V4L2_IO_RW:
1260 /* release back in the pool */
1261 pclass->release_buffer (bpool, buffer);
1264 case GST_V4L2_IO_DMABUF:
1265 case GST_V4L2_IO_MMAP:
1266 case GST_V4L2_IO_USERPTR:
1267 case GST_V4L2_IO_DMABUF_IMPORT:
1269 if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
1270 /* queue back in the device */
1271 if (pool->other_pool)
1272 gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
1273 if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
1274 pclass->release_buffer (bpool, buffer);
1276 /* Simply release invalide/modified buffer, the allocator will
1277 * give it back later */
1278 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1279 pclass->release_buffer (bpool, buffer);
1284 g_assert_not_reached ();
1289 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1290 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1291 switch (obj->mode) {
1292 case GST_V4L2_IO_RW:
1293 /* release back in the pool */
1294 pclass->release_buffer (bpool, buffer);
1297 case GST_V4L2_IO_MMAP:
1298 case GST_V4L2_IO_DMABUF:
1299 case GST_V4L2_IO_USERPTR:
1300 case GST_V4L2_IO_DMABUF_IMPORT:
1302 GstV4l2MemoryGroup *group;
1305 if (!gst_v4l2_is_buffer_valid (buffer, &group)) {
1306 /* Simply release invalide/modified buffer, the allocator will
1307 * give it back later */
1308 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1309 pclass->release_buffer (bpool, buffer);
1313 index = group->buffer.index;
1315 if (pool->buffers[index] == NULL) {
1316 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1319 /* Remove qdata, this will unmap any map data in userptr */
1320 gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
1321 GST_V4L2_IMPORT_QUARK, NULL, NULL);
1323 /* reset to default size */
1324 gst_v4l2_allocator_reset_group (pool->vallocator, group);
1326 /* playback, put the buffer back in the queue to refill later. */
1327 pclass->release_buffer (bpool, buffer);
1329 /* We keep a ref on queued buffer, so this should never happen */
1330 g_assert_not_reached ();
1336 g_assert_not_reached ();
1342 g_assert_not_reached ();
1348 gst_v4l2_buffer_pool_finalize (GObject * object)
1350 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1353 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1354 if (pool->buffers[i])
1355 gst_buffer_replace (&(pool->buffers[i]), NULL);
1358 if (pool->video_fd >= 0)
1359 v4l2_close (pool->video_fd);
1361 gst_poll_free (pool->poll);
1363 if (pool->vallocator)
1364 gst_object_unref (pool->vallocator);
1366 if (pool->allocator)
1367 gst_object_unref (pool->allocator);
1369 if (pool->other_pool)
1370 gst_object_unref (pool->other_pool);
1372 /* FIXME Is this required to keep around ? */
1373 gst_object_unref (pool->obj->element);
1375 /* FIXME have we done enough here ? */
1377 G_OBJECT_CLASS (parent_class)->finalize (object);
1381 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1383 pool->poll = gst_poll_new (TRUE);
1384 pool->can_poll_device = TRUE;
1385 g_cond_init (&pool->empty_cond);
1390 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1392 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1393 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1395 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1397 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1398 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1399 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1400 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1401 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1402 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1403 bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
1404 bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
1408 * gst_v4l2_buffer_pool_new:
1409 * @obj: the v4l2 object owning the pool
1411 * Construct a new buffer pool.
1413 * Returns: the new pool, use gst_object_unref() to free resources
1416 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1418 GstV4l2BufferPool *pool;
1419 GstStructure *config;
1420 gchar *name, *parent_name;
1423 fd = v4l2_dup (obj->video_fd);
1427 /* setting a significant unique name */
1428 parent_name = gst_object_get_name (GST_OBJECT (obj->element));
1429 name = g_strconcat (parent_name, ":", "pool:",
1430 V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src", NULL);
1431 g_free (parent_name);
1433 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
1434 "name", name, NULL);
1437 gst_poll_fd_init (&pool->pollfd);
1438 pool->pollfd.fd = fd;
1439 gst_poll_add_fd (pool->poll, &pool->pollfd);
1440 if (V4L2_TYPE_IS_OUTPUT (obj->type))
1441 gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
1443 gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
1445 pool->video_fd = fd;
1447 pool->can_poll_device = TRUE;
1450 gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
1451 if (pool->vallocator == NULL)
1452 goto allocator_failed;
1454 gst_object_ref (obj->element);
1456 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1457 gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
1458 /* This will simply set a default config, but will not configure the pool
1459 * because min and max are not valid */
1460 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1462 return GST_BUFFER_POOL (pool);
1467 GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1472 GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
1477 static GstFlowReturn
1478 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1481 GstV4l2Object *obj = pool->obj;
1486 toread = obj->info.size;
1488 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1490 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1493 if ((res = gst_v4l2_buffer_pool_poll (pool)) != GST_FLOW_OK)
1496 amount = v4l2_read (obj->video_fd, map.data, toread);
1498 if (amount == toread) {
1500 } else if (amount == -1) {
1501 if (errno == EAGAIN || errno == EINTR) {
1506 /* short reads can happen if a signal interrupts the read */
1511 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1512 gst_buffer_unmap (buf, &map);
1513 gst_buffer_resize (buf, 0, amount);
1520 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1525 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1526 (_("Error reading %d bytes from device '%s'."),
1527 toread, obj->videodev), GST_ERROR_SYSTEM);
1528 res = GST_FLOW_ERROR;
1533 gst_buffer_unmap (buf, &map);
1534 gst_buffer_resize (buf, 0, 0);
1540 * gst_v4l2_buffer_pool_process:
1541 * @bpool: a #GstBufferPool
1542 * @buf: a #GstBuffer, maybe be replaced
1544 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1545 * data from the device. For output devices, this functions send the contents of
1546 * @buf to the device for playback.
1548 * Returns: %GST_FLOW_OK on success.
1551 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
1553 GstFlowReturn ret = GST_FLOW_OK;
1554 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1555 GstV4l2Object *obj = pool->obj;
1557 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1559 g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
1561 if (GST_BUFFER_POOL_IS_FLUSHING (pool))
1562 return GST_FLOW_FLUSHING;
1564 switch (obj->type) {
1565 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1566 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1568 switch (obj->mode) {
1569 case GST_V4L2_IO_RW:
1570 /* capture into the buffer */
1571 ret = gst_v4l2_do_read (pool, *buf);
1574 case GST_V4L2_IO_MMAP:
1575 case GST_V4L2_IO_DMABUF:
1579 if ((*buf)->pool == bpool) {
1580 if (gst_buffer_get_size (*buf) == 0)
1583 /* nothing, data was inside the buffer when we did _acquire() */
1587 /* buffer not from our pool, grab a frame and copy it into the target */
1588 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1591 /* An empty buffer on capture indicates the end of stream */
1592 if (gst_buffer_get_size (tmp) == 0) {
1593 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1597 ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
1599 /* an queue the buffer again after the copy */
1600 gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
1602 if (ret != GST_FLOW_OK)
1607 case GST_V4L2_IO_USERPTR:
1609 struct UserPtrData *data;
1611 /* Replace our buffer with downstream allocated buffer */
1612 data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1613 GST_V4L2_IMPORT_QUARK);
1614 gst_buffer_replace (buf, data->buffer);
1615 _unmap_userptr_frame (data);
1619 case GST_V4L2_IO_DMABUF_IMPORT:
1623 /* Replace our buffer with downstream allocated buffer */
1624 tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
1625 GST_V4L2_IMPORT_QUARK);
1626 gst_buffer_replace (buf, tmp);
1627 gst_buffer_unref (tmp);
1632 g_assert_not_reached ();
1637 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1638 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1640 switch (obj->mode) {
1641 case GST_V4L2_IO_RW:
1642 /* FIXME, do write() */
1643 GST_WARNING_OBJECT (pool, "implement write()");
1646 case GST_V4L2_IO_USERPTR:
1647 case GST_V4L2_IO_DMABUF_IMPORT:
1648 case GST_V4L2_IO_DMABUF:
1649 case GST_V4L2_IO_MMAP:
1651 GstBuffer *to_queue = NULL;
1652 GstV4l2MemoryGroup *group;
1655 if ((*buf)->pool != bpool)
1658 if (!gst_v4l2_is_buffer_valid (*buf, &group))
1661 index = group->buffer.index;
1663 GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
1665 index = group->buffer.index;
1666 if (pool->buffers[index] != NULL) {
1667 GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
1671 /* we can queue directly */
1672 to_queue = gst_buffer_ref (*buf);
1675 if (to_queue == NULL) {
1676 GstBufferPoolAcquireParams params = { 0 };
1678 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1680 /* this can return EOS if all buffers are outstanding which would
1681 * be strange because we would expect the upstream element to have
1682 * allocated them and returned to us.. */
1683 params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
1684 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
1685 if (ret != GST_FLOW_OK)
1686 goto acquire_failed;
1688 ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
1689 if (ret != GST_FLOW_OK) {
1690 gst_buffer_unref (to_queue);
1691 goto prepare_failed;
1695 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1698 /* if we are not streaming yet (this is the first buffer, start
1700 if (!gst_v4l2_buffer_pool_streamon (pool)) {
1701 gst_buffer_unref (to_queue);
1705 if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
1707 /* all buffers are queued, try to dequeue one and release it back
1708 * into the pool so that _acquire can get to it again. */
1709 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1710 if (ret == GST_FLOW_OK)
1711 /* release the rendered buffer back into the pool. This wakes up any
1712 * thread waiting for a buffer in _acquire(). */
1713 gst_buffer_unref (out);
1718 g_assert_not_reached ();
1723 g_assert_not_reached ();
1732 GST_ERROR_OBJECT (pool, "failed to copy buffer");
1737 GST_DEBUG_OBJECT (pool, "end of stream reached");
1738 return GST_FLOW_EOS;
1742 if (ret == GST_FLOW_FLUSHING)
1743 GST_DEBUG_OBJECT (pool, "flushing");
1745 GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
1746 gst_flow_get_name (ret));
1751 GST_ERROR_OBJECT (pool, "failed to prepare data");
1756 GST_ERROR_OBJECT (pool, "failed to queue buffer");
1761 GST_ERROR_OBJECT (pool, "failed to start streaming");
1762 return GST_FLOW_ERROR;
1767 gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
1768 GstBufferPool * other_pool)
1770 g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
1772 if (pool->other_pool)
1773 gst_object_unref (pool->other_pool);
1774 pool->other_pool = gst_object_ref (other_pool);