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.
29 #if HAVE_DECL_V4L2_MEMORY_DMABUF
31 # define _GNU_SOURCE /* O_CLOEXEC */
40 #include "gst/video/video.h"
41 #include "gst/video/gstvideometa.h"
42 #include "gst/video/gstvideopool.h"
43 #include "gst/allocators/gstdmabuf.h"
45 #include <gstv4l2bufferpool.h>
47 #include "v4l2_calls.h"
48 #include "gst/gst-i18n-plugin.h"
49 #include <gst/glib-compat-private.h>
51 /* videodev2.h is not versioned and we can't easily check for the presence
52 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
53 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
54 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
55 #define V4L2_FIELD_INTERLACED_TB 8
56 #define V4L2_FIELD_INTERLACED_BT 9
59 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
60 #define GST_CAT_DEFAULT v4l2_debug
66 gst_v4l2_meta_api_get_type (void)
68 static volatile GType type;
69 static const gchar *tags[] =
70 { GST_META_TAG_VIDEO_STR, GST_META_TAG_MEMORY_STR, NULL };
72 if (g_once_init_enter (&type)) {
73 GType _type = gst_meta_api_type_register ("GstV4l2MetaAPI", tags);
74 g_once_init_leave (&type, _type);
80 gst_v4l2_meta_get_info (void)
82 static const GstMetaInfo *meta_info = NULL;
84 if (g_once_init_enter (&meta_info)) {
85 const GstMetaInfo *meta =
86 gst_meta_register (gst_v4l2_meta_api_get_type (), "GstV4l2Meta",
87 sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
88 (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
89 g_once_init_leave (&meta_info, meta);
97 #define gst_v4l2_buffer_pool_parent_class parent_class
98 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
100 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
104 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
106 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
113 case GST_V4L2_IO_DMABUF:
115 case GST_V4L2_IO_MMAP:
121 meta = GST_V4L2_META_GET (buffer);
122 g_assert (meta != NULL);
124 index = meta->vbuffer.index;
126 for (i = 0; i < meta->n_planes; i++) {
127 GST_LOG_OBJECT (pool,
128 "unmap multiplanar buffer %p idx %d (data %p, len %u, plane %u)",
129 buffer, index, meta->mem[i], meta->vplanes[i].length, i);
131 v4l2_munmap (meta->mem[i], meta->vplanes[i].length);
134 pool->buffers[index] = NULL;
137 case GST_V4L2_IO_USERPTR:
139 g_assert_not_reached ();
142 gst_buffer_unref (buffer);
146 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
147 GstBufferPoolAcquireParams * params)
149 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
164 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
167 case GST_V4L2_IO_MMAP:
168 case GST_V4L2_IO_DMABUF:
170 #ifdef VIDIOC_CREATE_BUFS
171 if (pool->num_allocated == pool->num_buffers) {
172 struct v4l2_create_buffers create_bufs;
174 memset (&create_bufs, 0, sizeof (struct v4l2_create_buffers));
175 create_bufs.count = 1;
176 create_bufs.memory = V4L2_MEMORY_MMAP;
177 create_bufs.format.type = obj->type;
179 if (v4l2_ioctl (pool->video_fd, VIDIOC_G_FMT, &create_bufs.format) < 0)
182 if (v4l2_ioctl (pool->video_fd, VIDIOC_CREATE_BUFS, &create_bufs) < 0)
183 goto create_bufs_failed;
185 GST_LOG_OBJECT (pool, "created buffer with index: %u",
188 pool->buffers = g_renew (GstBuffer *, pool->buffers, pool->num_buffers);
189 pool->buffers[pool->num_buffers - 1] = NULL;
192 newbuf = gst_buffer_new ();
193 meta = GST_V4L2_META_ADD (newbuf);
195 index = pool->num_allocated;
197 GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf);
199 /* prepare the buffer */
200 memset (&meta->vbuffer, 0x0, sizeof (struct v4l2_buffer));
201 meta->vbuffer.index = index;
202 meta->vbuffer.type = obj->type;
203 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
205 /* main information */
206 meta->n_planes = obj->n_v4l2_planes;
208 /* prepare the planes of the buffer */
209 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
210 /* length is the number of elements in the
212 meta->vbuffer.length = obj->n_v4l2_planes;
213 meta->vbuffer.m.planes = meta->vplanes;
216 /* the buffer is prepared, now fill in it with meaningful values */
217 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
218 goto querybuf_failed;
220 /* in non MPLANE mode we emulate one plane in order to
221 * factorize the code */
222 if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
223 /* here meta->n_planes == 1 */
224 meta->vplanes[0].length = meta->vbuffer.length;
225 meta->vplanes[0].bytesused = meta->vbuffer.bytesused;
226 meta->vplanes[0].m.mem_offset = meta->vbuffer.m.offset;
227 meta->vplanes[0].data_offset = 0;
230 GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
231 GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
232 GST_LOG_OBJECT (pool, " flags: %08x", meta->vbuffer.flags);
233 GST_LOG_OBJECT (pool, " field: %d", meta->vbuffer.field);
234 GST_LOG_OBJECT (pool, " memory: %d", meta->vbuffer.memory);
235 GST_LOG_OBJECT (pool, " planes: %d", meta->n_planes);
237 #ifndef GST_DISABLE_GST_DEBUG
238 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP) {
239 for (i = 0; i < meta->n_planes; i++) {
240 GST_LOG_OBJECT (pool, " bytesused: %u, plane: %u",
241 meta->vplanes[i].bytesused, i);
242 GST_LOG_OBJECT (pool, " MMAP offset: %u, plane: %u",
243 meta->vplanes[i].m.mem_offset, i);
248 if (obj->mode == GST_V4L2_IO_MMAP) {
249 /* append one gstmemory for each plane */
250 for (i = 0; i < meta->n_planes; i++) {
251 meta->mem[i] = v4l2_mmap (0, meta->vplanes[i].length,
252 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
253 meta->vplanes[i].m.mem_offset);
254 if (meta->mem[i] == MAP_FAILED)
257 GST_LOG_OBJECT (pool, " buffer length %d, data offset %d, plane %d",
258 meta->vplanes[i].length, meta->vplanes[i].data_offset, i);
260 gst_buffer_append_memory (newbuf,
261 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
262 meta->mem[i], meta->vplanes[i].length,
263 meta->vplanes[i].data_offset,
264 meta->vplanes[i].length, NULL, NULL));
267 #if HAVE_DECL_V4L2_MEMORY_DMABUF
268 if (obj->mode == GST_V4L2_IO_DMABUF) {
269 struct v4l2_exportbuffer expbuf;
271 memset (&expbuf, 0, sizeof (struct v4l2_exportbuffer));
272 expbuf.type = meta->vbuffer.type;
273 expbuf.index = meta->vbuffer.index;
274 expbuf.flags = O_CLOEXEC;
276 for (i = 0; i < meta->n_planes; i++) {
279 if (v4l2_ioctl (pool->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
282 gst_buffer_append_memory (newbuf,
283 gst_dmabuf_allocator_alloc (pool->allocator, expbuf.fd,
284 meta->vplanes[i].length));
287 meta->vbuffer.memory = V4L2_MEMORY_DMABUF;
289 /* in non-MPLANE mode our meta is not automatically updated
290 * because the plane is emulated (not referenced by
292 if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type))
293 meta->vplanes[0].m.fd = meta->vbuffer.m.fd;
296 /* add metadata to raw video buffers */
297 if (pool->add_videometa && info->finfo) {
298 const GstVideoFormatInfo *finfo = info->finfo;
299 gsize offset[GST_VIDEO_MAX_PLANES];
300 gint width, height, n_gst_planes, offs, i, stride[GST_VIDEO_MAX_PLANES];
302 width = GST_VIDEO_INFO_WIDTH (info);
303 height = GST_VIDEO_INFO_HEIGHT (info);
305 /* n_gst_planes is the number of planes
306 * (RGB: 1, YUY2: 1, NV12: 2, I420: 3)
307 * It's greater or equal than the number of v4l2 planes. */
308 n_gst_planes = GST_VIDEO_INFO_N_PLANES (info);
310 /* the basic are common between MPLANE mode and non MPLANE mode
311 * except a special case inside the loop at the end
314 for (i = 0; i < n_gst_planes; i++) {
315 GST_DEBUG_OBJECT (pool, "adding video meta, bytesperline %d",
316 obj->bytesperline[i]);
320 if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
321 guint x_tiles, y_tiles, ws, hs, tile_height;
323 ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
324 hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
325 tile_height = 1 << hs;
327 x_tiles = obj->bytesperline[i] >> ws;
328 y_tiles = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
329 GST_ROUND_UP_N (height, tile_height) >> hs);
330 stride[i] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
332 stride[i] = obj->bytesperline[i];
335 /* when using multiplanar mode and if there is more then one v4l
336 * plane for each gst plane
338 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type) && meta->n_planes > 1)
339 /* non_contiguous case here so we have to make sure that gst goes to the
340 * next plane (using default gstvideometa.c::default_map).
341 * And the next plane is after length bytes of the previous one from
342 * the gst buffer point of view. */
343 offs += meta->vplanes[i].length;
345 offs += obj->bytesperline[i] *
346 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, height);
349 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
350 GST_VIDEO_INFO_FORMAT (info), width, height, n_gst_planes,
355 case GST_V4L2_IO_USERPTR:
358 g_assert_not_reached ();
361 pool->num_allocated++;
368 #ifdef VIDIOC_CREATE_BUFS
371 gint errnosave = errno;
373 GST_WARNING ("Failed G_FMT: %s", g_strerror (errnosave));
375 return GST_FLOW_ERROR;
379 gint errnosave = errno;
381 GST_WARNING ("Failed CREATE_BUFS: %s", g_strerror (errnosave));
383 return GST_FLOW_ERROR;
388 gint errnosave = errno;
390 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
391 gst_buffer_unref (newbuf);
393 return GST_FLOW_ERROR;
397 gint errnosave = errno;
399 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
400 gst_buffer_unref (newbuf);
402 return GST_FLOW_ERROR;
404 #if HAVE_DECL_V4L2_MEMORY_DMABUF
407 gint errnosave = errno;
409 GST_WARNING ("Failed EXPBUF: %s", g_strerror (errnosave));
410 gst_buffer_unref (newbuf);
412 return GST_FLOW_ERROR;
418 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
420 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
421 GstV4l2Object *obj = pool->obj;
423 guint size, min_buffers, max_buffers;
424 GstAllocator *allocator;
425 GstAllocationParams params;
427 GST_DEBUG_OBJECT (pool, "set config");
429 pool->add_videometa =
430 gst_buffer_pool_config_has_option (config,
431 GST_BUFFER_POOL_OPTION_VIDEO_META);
433 if (!pool->add_videometa &&
434 GST_VIDEO_INFO_FORMAT (&obj->info) != GST_VIDEO_FORMAT_ENCODED) {
435 /* in non MPLANE mode, there is only one bytesperline field */
436 gint nb_checked_planes =
437 V4L2_TYPE_IS_MULTIPLANAR (obj->type) ? GST_VIDEO_INFO_N_PLANES (&obj->
441 for (i = 0; i < nb_checked_planes; i++) {
442 /* we don't have video metadata, and we are dealing with raw video,
443 * see if the strides are compatible */
444 stride = GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, i);
446 if (GST_VIDEO_FORMAT_INFO_IS_TILED (obj->info.finfo)) {
447 stride = GST_VIDEO_TILE_X_TILES (stride) <<
448 GST_VIDEO_FORMAT_INFO_TILE_WS ((obj->info.finfo));
451 GST_DEBUG_OBJECT (pool, "no videometadata, checking strides %d and %u",
452 stride, obj->bytesperline[i]);
454 if (stride != obj->bytesperline[i])
455 goto missing_video_api;
459 /* parse the config and keep around */
460 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
464 /* FIXME Check alignement, and S_FMT with new size if different */
466 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
469 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
471 if (obj->mode == GST_V4L2_IO_DMABUF)
472 allocator = gst_dmabuf_allocator_new ();
474 gst_object_ref (allocator);
477 gst_object_unref (pool->allocator);
478 pool->allocator = allocator;
480 pool->params = params;
482 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
485 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
490 GST_ERROR_OBJECT (pool, "missing GstMetaVideo API in config, "
491 "default stride: %d, wanted stride %u",
492 GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0), obj->bytesperline[0]);
497 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
503 start_streaming (GstV4l2BufferPool * pool)
505 GstV4l2Object *obj = pool->obj;
510 case GST_V4L2_IO_MMAP:
511 case GST_V4L2_IO_USERPTR:
512 case GST_V4L2_IO_DMABUF:
513 GST_DEBUG_OBJECT (pool, "STREAMON");
514 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
518 g_assert_not_reached ();
522 pool->streaming = TRUE;
529 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
536 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
538 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
539 GstV4l2Object *obj = pool->obj;
540 GstStructure *config;
542 guint size, num_buffers, min_buffers, max_buffers, copy_threshold;
543 struct v4l2_requestbuffers breq;
545 config = gst_buffer_pool_get_config (bpool);
546 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
552 /* we preallocate 1 buffer, this value also instructs the latency
553 * calculation to have 1 frame latency max */
557 case GST_V4L2_IO_DMABUF:
558 case GST_V4L2_IO_MMAP:
560 /* request a reasonable number of buffers when no max specified. We will
561 * copy when we run out of buffers */
562 if (max_buffers == 0)
563 num_buffers = MAX (4, min_buffers);
565 num_buffers = max_buffers;
567 /* first, lets request buffers, and see how many we can get: */
568 GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
571 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
572 breq.type = obj->type;
573 breq.count = num_buffers;
574 breq.memory = V4L2_MEMORY_MMAP;
576 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
579 GST_LOG_OBJECT (pool, " count: %u", breq.count);
580 GST_LOG_OBJECT (pool, " type: %d", breq.type);
581 GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
583 if (breq.count < GST_V4L2_MIN_BUFFERS)
586 if (num_buffers != breq.count) {
587 GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
588 num_buffers = breq.count;
590 /* update min buffers with the amount of buffers we just reserved. We need
591 * to configure this value in the bufferpool so that the default start
592 * implementation calls our allocate function */
593 min_buffers = breq.count;
595 if (max_buffers == 0 || num_buffers < max_buffers) {
596 /* if we are asked to provide more buffers than we have allocated, start
597 * copying buffers when we only have 2 buffers left in the pool */
600 /* we are certain that we have enough buffers so we don't need to
605 /* FIXME try to call CREATEBUFS with count 0 to check if max shall
609 case GST_V4L2_IO_USERPTR:
613 g_assert_not_reached ();
618 pool->num_buffers = num_buffers;
619 pool->copy_threshold = copy_threshold;
621 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
623 GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
626 pool->buffers = g_new0 (GstBuffer *, pool->num_buffers);
627 pool->num_allocated = 0;
629 /* now, allocate the buffers: */
630 if (!GST_BUFFER_POOL_CLASS (parent_class)->start (bpool))
633 /* we can start capturing now, we wait for the playback case until we queued
634 * the first buffer */
635 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
636 if (!start_streaming (pool))
639 gst_poll_set_flushing (obj->poll, FALSE);
646 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
651 GST_ERROR_OBJECT (pool,
652 "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
657 GST_ERROR_OBJECT (pool,
658 "we received %d from device '%s', we want at least %d",
659 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
664 GST_ERROR_OBJECT (pool, "failed to start streaming");
670 gst_v4l2_buffer_pool_free_buffers (GstV4l2BufferPool * pool)
672 if (pool->num_buffers > 0) {
673 struct v4l2_requestbuffers breq;
674 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
675 breq.type = pool->obj->type;
677 breq.memory = V4L2_MEMORY_MMAP;
678 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0) {
679 GST_ERROR_OBJECT (pool, "error releasing buffers: %s",
682 pool->num_buffers = 0;
687 stop_streaming (GstV4l2BufferPool * pool)
689 GstV4l2Object *obj = pool->obj;
691 GST_DEBUG_OBJECT (pool, "stopping stream");
693 gst_poll_set_flushing (obj->poll, TRUE);
695 if (!pool->streaming) {
696 /* it avoid error: STREAMOFF 22 (Invalid argument) when
697 * attempting to stop a stream not previously started */
698 GST_DEBUG_OBJECT (pool, "no need to stop, was not previously started");
705 case GST_V4L2_IO_MMAP:
706 case GST_V4L2_IO_USERPTR:
707 case GST_V4L2_IO_DMABUF:
708 GST_DEBUG_OBJECT (pool, "STREAMOFF");
709 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
713 g_assert_not_reached ();
717 pool->streaming = FALSE;
724 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
731 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
734 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
735 GstV4l2Object *obj = pool->obj;
738 GST_DEBUG_OBJECT (pool, "stopping pool");
740 gst_poll_set_flushing (obj->poll, TRUE);
742 if (pool->streaming) {
746 case GST_V4L2_IO_MMAP:
747 case GST_V4L2_IO_USERPTR:
748 case GST_V4L2_IO_DMABUF:
749 /* we actually need to sync on all queued buffers but not
750 * on the non-queued ones */
751 GST_DEBUG_OBJECT (pool, "STREAMOFF");
752 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
756 g_assert_not_reached ();
759 pool->streaming = FALSE;
762 /* first free the buffers in the queue */
763 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
765 /* then free the remaining buffers */
766 for (n = 0; n < pool->num_buffers; n++) {
767 if (pool->buffers[n])
768 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
770 pool->num_queued = 0;
771 g_free (pool->buffers);
772 pool->buffers = NULL;
774 gst_v4l2_buffer_pool_free_buffers (pool);
781 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
788 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
792 if (v4l2object->can_poll_device) {
793 GST_LOG_OBJECT (v4l2object->element, "polling device");
794 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
795 if (G_UNLIKELY (ret < 0)) {
798 if (errno == ENXIO) {
799 GST_WARNING_OBJECT (v4l2object->element,
800 "v4l2 device doesn't support polling. Disabling");
801 v4l2object->can_poll_device = FALSE;
803 if (errno != EAGAIN && errno != EINTR)
813 GST_DEBUG ("stop called");
814 return GST_FLOW_FLUSHING;
818 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
819 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
820 return GST_FLOW_ERROR;
825 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
831 meta = GST_V4L2_META_GET (buf);
833 GST_LOG_OBJECT (pool, "unref copied buffer %p", buf);
834 /* no meta, it was a copied buffer that we can unref */
835 gst_buffer_unref (buf);
839 index = meta->vbuffer.index;
841 /* this field is common to MPLANE and not MPLANE */
842 meta->vbuffer.bytesused = gst_buffer_get_size (buf);
844 for (i = 0; i < meta->n_planes; i++) {
845 meta->vplanes[i].bytesused =
846 gst_buffer_get_sizes_range (buf, i, 1, NULL, NULL);
848 GST_LOG_OBJECT (pool,
849 "enqueue buffer %p, index:%d, queued:%d, flags:%08x mem:%p used:%d, plane:%d",
850 buf, index, pool->num_queued, meta->vbuffer.flags,
851 meta->mem[i], meta->vplanes[i].bytesused, i);
854 if (pool->buffers[index] != NULL)
857 GST_LOG_OBJECT (pool, "doing QBUF");
858 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
861 pool->buffers[index] = buf;
869 GST_WARNING_OBJECT (pool, "the buffer was already queued");
870 return GST_FLOW_ERROR;
874 GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
876 return GST_FLOW_ERROR;
881 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
885 struct v4l2_buffer vbuffer;
886 struct v4l2_plane vplanes[GST_VIDEO_MAX_PLANES];
887 GstV4l2Object *obj = pool->obj;
888 GstClockTime timestamp;
892 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
895 /* prepare the buffer */
896 memset (&vbuffer, 0x00, sizeof (vbuffer));
897 vbuffer.type = obj->type;
898 #if HAVE_DECL_V4L2_MEMORY_DMABUF
899 if (obj->mode == GST_V4L2_IO_DMABUF)
900 vbuffer.memory = V4L2_MEMORY_DMABUF;
903 vbuffer.memory = V4L2_MEMORY_MMAP;
905 /* prepare the planes of the buffer */
906 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
907 /* length is the number of elements in the
909 vbuffer.length = obj->n_v4l2_planes;
910 vbuffer.m.planes = vplanes;
913 /* the buffer is prepared, now fill in it with meaningful values */
914 GST_LOG_OBJECT (pool, "doing DQBUF");
915 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
918 /* get our GstBuffer with that index from the pool, if the buffer was
919 * outstanding we have a serious problem.
921 outbuf = pool->buffers[vbuffer.index];
925 /* mark the buffer outstanding */
926 pool->buffers[vbuffer.index] = NULL;
929 timestamp = GST_TIMEVAL_TO_TIME (vbuffer.timestamp);
931 meta = GST_V4L2_META_GET (outbuf);
932 g_assert (meta != NULL);
934 /* The size can change at every frame, esp. with jpeg. The GstMemory
935 * inside the GstBuffer could have been changed by some other
936 * element. So update our meta */
937 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
938 || obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
940 /* this field is common to MPLANE and not MPLANE */
941 meta->vbuffer.length = vbuffer.length;
942 meta->vbuffer.bytesused = vbuffer.bytesused;
944 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
945 for (i = 0; i < meta->n_planes; i++) {
946 /* the following also update meta->vbuffer.m.planes[i].length */
947 meta->vplanes[i].length = vbuffer.m.planes[i].length;
948 /* the following also update meta->vbuffer.m.planes[i].bytesused */
949 meta->vplanes[i].bytesused = vbuffer.m.planes[i].bytesused;
950 /* the following also update meta->vbuffer.m.planes[i].data_offset */
951 meta->vplanes[i].data_offset = vbuffer.m.planes[i].data_offset;
954 meta->vplanes[0].length = vbuffer.length;
955 meta->vplanes[0].bytesused = vbuffer.bytesused;
956 meta->vplanes[0].data_offset = 0;
959 #ifndef GST_DISABLE_GST_DEBUG
960 for (i = 0; i < meta->n_planes; i++) {
961 GST_LOG_OBJECT (pool,
962 "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
963 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
964 vbuffer.sequence, vbuffer.index, meta->mem[i],
965 meta->vplanes[i].bytesused, i, vbuffer.flags,
966 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
970 /* set top/bottom field first if v4l2_buffer has the information */
971 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
972 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
974 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
975 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
978 /* The size can change at every frame, esp. with jpeg. The GstMemory
979 * inside the GstBuffer could have been changed by some other
980 * element, so just put back the original one. We always set it as
981 * no share, so if it's not there, it's not used at all.
983 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
984 || obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
985 gst_buffer_remove_all_memory (outbuf);
986 for (i = 0; i < meta->n_planes; i++) {
987 gst_buffer_append_memory (outbuf,
988 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
989 meta->mem[i], meta->vplanes[i].length,
990 meta->vplanes[i].data_offset,
991 meta->vplanes[i].bytesused, NULL, NULL));
995 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1004 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
1009 GST_WARNING_OBJECT (pool,
1010 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
1011 vbuffer.sequence, vbuffer.index,
1012 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
1016 GST_WARNING_OBJECT (pool,
1017 "Non-blocking I/O has been selected using O_NONBLOCK and"
1018 " no buffer was in the outgoing queue. device %s", obj->videodev);
1021 GST_ERROR_OBJECT (pool,
1022 "The buffer type is not supported, or the index is out of bounds, "
1023 "or no buffers have been allocated yet, or the userptr "
1024 "or length are invalid. device %s", obj->videodev);
1027 GST_ERROR_OBJECT (pool,
1028 "insufficient memory to enqueue a user pointer buffer");
1031 GST_INFO_OBJECT (pool,
1032 "VIDIOC_DQBUF failed due to an internal error."
1033 " Can also indicate temporary problems like signal loss."
1034 " Note the driver might dequeue an (empty) buffer despite"
1035 " returning an error, or even stop capturing."
1036 " device %s", obj->videodev);
1037 /* have we de-queued a buffer ? */
1038 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
1039 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
1040 /* FIXME ... should we do something here? */
1044 GST_WARNING_OBJECT (pool,
1045 "could not sync on a buffer on device %s", obj->videodev);
1048 GST_WARNING_OBJECT (pool,
1049 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
1050 obj->videodev, errno, g_strerror (errno));
1053 return GST_FLOW_ERROR;
1057 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
1059 return GST_FLOW_ERROR;
1063 static GstFlowReturn
1064 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
1065 GstBufferPoolAcquireParams * params)
1068 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1069 GstV4l2Object *obj = pool->obj;
1071 GST_DEBUG_OBJECT (pool, "acquire");
1073 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
1076 switch (obj->type) {
1077 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1078 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1079 /* capture, This function should return a buffer with new captured data */
1080 switch (obj->mode) {
1081 case GST_V4L2_IO_RW:
1082 /* take empty buffer from the pool */
1083 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
1086 case GST_V4L2_IO_DMABUF:
1087 case GST_V4L2_IO_MMAP:
1088 /* just dequeue a buffer, we basically use the queue of v4l2 as the
1089 * storage for our buffers. This function does poll first so we can
1090 * interrupt it fine. */
1091 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
1092 if (G_UNLIKELY (ret != GST_FLOW_OK))
1095 /* start copying buffers when we are running low on buffers */
1096 if (pool->num_queued < pool->copy_threshold) {
1098 #ifdef VIDIOC_CREATE_BUFS
1099 if (pool->can_alloc) {
1100 if (GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
1101 ©, params) == GST_FLOW_OK) {
1102 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
1105 pool->can_alloc = FALSE;
1110 /* copy the buffer */
1111 copy = gst_buffer_copy_region (*buffer,
1112 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
1113 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
1115 /* and requeue so that we can continue capturing */
1116 ret = gst_v4l2_buffer_pool_qbuf (pool, *buffer);
1121 case GST_V4L2_IO_USERPTR:
1123 ret = GST_FLOW_ERROR;
1124 g_assert_not_reached ();
1129 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1130 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1131 /* playback, This function should return an empty buffer */
1132 switch (obj->mode) {
1133 case GST_V4L2_IO_RW:
1134 /* get an empty buffer */
1135 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
1139 case GST_V4L2_IO_MMAP:
1140 /* get a free unqueued buffer */
1141 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
1145 case GST_V4L2_IO_USERPTR:
1147 ret = GST_FLOW_ERROR;
1148 g_assert_not_reached ();
1154 ret = GST_FLOW_ERROR;
1155 g_assert_not_reached ();
1164 GST_DEBUG_OBJECT (pool, "We are flushing");
1165 return GST_FLOW_FLUSHING;
1170 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
1172 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1173 GstV4l2Object *obj = pool->obj;
1175 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
1177 switch (obj->type) {
1178 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1179 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1180 /* capture, put the buffer back in the queue so that we can refill it
1182 switch (obj->mode) {
1183 case GST_V4L2_IO_RW:
1184 /* release back in the pool */
1185 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
1188 case GST_V4L2_IO_DMABUF:
1189 case GST_V4L2_IO_MMAP:
1190 /* queue back in the device */
1191 gst_v4l2_buffer_pool_qbuf (pool, buffer);
1194 case GST_V4L2_IO_USERPTR:
1196 g_assert_not_reached ();
1201 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1202 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1203 switch (obj->mode) {
1204 case GST_V4L2_IO_RW:
1205 /* release back in the pool */
1206 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
1209 case GST_V4L2_IO_MMAP:
1214 meta = GST_V4L2_META_GET (buffer);
1215 g_assert (meta != NULL);
1217 index = meta->vbuffer.index;
1219 if (pool->buffers[index] == NULL) {
1220 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
1223 /* reset to the full length, in case it was changed */
1224 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1226 gint total_length = 0;
1227 for (i = 0; i < meta->n_planes; i++)
1228 total_length += meta->vplanes[i].length;
1230 if (total_length != gst_buffer_get_size (buffer) &&
1231 obj->info.finfo->n_planes > 1) {
1232 /* FIXME if the lengths has actually changed it may require
1233 * to restore the sizes of the individual memories and
1235 GST_WARNING_OBJECT (pool,
1236 "lengths changed, more work required");
1239 gst_buffer_resize (buffer, 0, total_length);
1241 gst_buffer_resize (buffer, 0, meta->vbuffer.length);
1244 /* playback, put the buffer back in the queue to refill later. */
1245 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
1248 /* the buffer is queued in the device but maybe not played yet. We just
1249 * leave it there and not make it available for future calls to acquire
1250 * for now. The buffer will be dequeued and reused later. */
1251 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
1256 case GST_V4L2_IO_USERPTR:
1258 g_assert_not_reached ();
1264 g_assert_not_reached ();
1270 gst_v4l2_buffer_pool_finalize (GObject * object)
1272 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1274 gst_v4l2_buffer_pool_free_buffers (pool);
1276 if (pool->video_fd >= 0)
1277 v4l2_close (pool->video_fd);
1278 if (pool->allocator)
1279 gst_object_unref (pool->allocator);
1280 g_free (pool->buffers);
1282 gst_object_unref (pool->obj->element);
1284 G_OBJECT_CLASS (parent_class)->finalize (object);
1288 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1293 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1295 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1296 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1298 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1300 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1301 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1302 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1303 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1304 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1305 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1306 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
1310 * gst_v4l2_buffer_pool_new:
1311 * @obj: the v4l2 object owning the pool
1313 * Construct a new buffer pool.
1315 * Returns: the new pool, use gst_object_unref() to free resources
1318 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1320 GstV4l2BufferPool *pool;
1321 GstStructure *config;
1322 gboolean res = FALSE;
1325 fd = v4l2_dup (obj->video_fd);
1329 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
1330 pool->video_fd = fd;
1332 pool->can_alloc = TRUE;
1334 config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1335 gst_buffer_pool_config_set_params (config, caps, obj->sizeimage, 2, 0);
1337 res = gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
1341 gst_object_ref (obj->element);
1343 return GST_BUFFER_POOL (pool);
1348 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1353 GST_WARNING ("failed to set pool config");
1358 static GstFlowReturn
1359 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1362 GstV4l2Object *obj = pool->obj;
1367 toread = obj->sizeimage;
1369 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1371 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1374 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
1377 amount = v4l2_read (obj->video_fd, map.data, toread);
1379 if (amount == toread) {
1381 } else if (amount == -1) {
1382 if (errno == EAGAIN || errno == EINTR) {
1387 /* short reads can happen if a signal interrupts the read */
1392 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1393 gst_buffer_unmap (buf, &map);
1394 gst_buffer_resize (buf, 0, amount);
1401 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1406 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1407 (_("Error reading %d bytes from device '%s'."),
1408 toread, obj->videodev), GST_ERROR_SYSTEM);
1409 res = GST_FLOW_ERROR;
1414 gst_buffer_unmap (buf, &map);
1415 gst_buffer_resize (buf, 0, 0);
1421 * gst_v4l2_buffer_pool_process:
1422 * @bpool: a #GstBufferPool
1423 * @buf: a #GstBuffer
1425 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1426 * data from the device. For output devices, this functions send the contents of
1427 * @buf to the device for playback.
1429 * Returns: %GST_FLOW_OK on success.
1432 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
1434 GstFlowReturn ret = GST_FLOW_OK;
1435 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1436 GstV4l2Object *obj = pool->obj;
1438 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1440 switch (obj->type) {
1441 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1442 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1444 switch (obj->mode) {
1445 case GST_V4L2_IO_RW:
1446 /* capture into the buffer */
1447 ret = gst_v4l2_do_read (pool, buf);
1450 case GST_V4L2_IO_MMAP:
1454 if (buf->pool == bpool)
1455 /* nothing, data was inside the buffer when we did _acquire() */
1458 /* buffer not from our pool, grab a frame and copy it into the target */
1459 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1462 if (!gst_v4l2_object_copy (obj, buf, tmp))
1465 /* an queue the buffer again after the copy */
1466 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
1471 case GST_V4L2_IO_USERPTR:
1473 g_assert_not_reached ();
1478 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1479 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1481 switch (obj->mode) {
1482 case GST_V4L2_IO_RW:
1483 /* FIXME, do write() */
1484 GST_WARNING_OBJECT (pool, "implement write()");
1486 case GST_V4L2_IO_DMABUF:
1487 case GST_V4L2_IO_MMAP:
1489 GstBuffer *to_queue;
1491 if (buf->pool == bpool) {
1492 /* nothing, we can queue directly */
1493 to_queue = gst_buffer_ref (buf);
1494 GST_LOG_OBJECT (pool, "processing buffer from our pool");
1496 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1497 if (!gst_buffer_pool_is_active (bpool)) {
1498 GstStructure *config;
1500 /* this pool was not activated, configure and activate */
1501 GST_DEBUG_OBJECT (pool, "activating pool");
1503 config = gst_buffer_pool_get_config (bpool);
1504 gst_buffer_pool_config_add_option (config,
1505 GST_BUFFER_POOL_OPTION_VIDEO_META);
1506 gst_buffer_pool_set_config (bpool, config);
1508 if (!gst_buffer_pool_set_active (bpool, TRUE))
1509 goto activate_failed;
1512 /* this can block if all buffers are outstanding which would be
1513 * strange because we would expect the upstream element to have
1514 * allocated them and returned to us.. */
1515 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, NULL);
1516 if (ret != GST_FLOW_OK)
1517 goto acquire_failed;
1519 /* copy into it and queue */
1520 if (!gst_v4l2_object_copy (obj, to_queue, buf))
1524 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1527 /* if we are not streaming yet (this is the first buffer, start
1529 if (!pool->streaming)
1530 if (!start_streaming (pool))
1533 if (pool->num_queued == pool->num_allocated) {
1535 /* all buffers are queued, try to dequeue one and release it back
1536 * into the pool so that _acquire can get to it again. */
1537 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1538 if (ret != GST_FLOW_OK) {
1539 gst_buffer_unref (to_queue);
1543 /* release the rendered buffer back into the pool. This wakes up any
1544 * thread waiting for a buffer in _acquire(). If the buffer still has
1545 * a pool then this will happen when the refcount reaches 0 */
1547 gst_v4l2_buffer_pool_release_buffer (bpool, out);
1549 gst_buffer_unref (to_queue);
1553 case GST_V4L2_IO_USERPTR:
1555 g_assert_not_reached ();
1560 g_assert_not_reached ();
1569 GST_ERROR_OBJECT (obj->element, "failed to activate pool");
1570 return GST_FLOW_ERROR;
1574 GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
1575 gst_flow_get_name (ret));
1580 GST_ERROR_OBJECT (obj->element, "failed to copy data");
1581 return GST_FLOW_ERROR;
1585 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1586 return GST_FLOW_ERROR;
1592 * gst_v4l2_buffer_pool_flush:
1593 * @bpool: a #GstBufferPool
1595 * First, set obj->poll to be flushing
1596 * Call STREAMOFF to clear QUEUED flag on every driver buffers.
1597 * Then release all buffers that are in pool->buffers array.
1598 * Finally call STREAMON if CAPTURE type
1599 * The caller is responsible to unset flushing on obj->pool
1601 * Returns: TRUE on success.
1604 gst_v4l2_buffer_pool_flush (GstV4l2BufferPool * pool)
1606 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1607 GstV4l2Object *obj = pool->obj;
1610 GST_DEBUG_OBJECT (pool, "flush");
1612 stop_streaming (pool);
1614 switch (obj->type) {
1615 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1616 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1617 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1618 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1619 switch (obj->mode) {
1620 case GST_V4L2_IO_RW:
1622 case GST_V4L2_IO_MMAP:
1623 case GST_V4L2_IO_USERPTR:
1624 case GST_V4L2_IO_DMABUF:
1626 for (i = 0; i < pool->num_buffers; i++) {
1627 GstBuffer *buf = pool->buffers[i];
1629 /* it's necessary to set to NULL before to call
1630 * gst_v4l2_buffer_pool_release_buffer
1631 * otherwise it won't go back to the pool */
1632 pool->buffers[i] = NULL;
1634 /* dicrease counter */
1637 /* in CAPTURE mode the pool->num_queued will be re-incremented
1638 * because the buffers are queued when released */
1640 gst_buffer_unref (buf);
1642 gst_v4l2_buffer_pool_release_buffer (bpool, buf);
1646 /* do not set pool->num_queued to 0 because
1647 * the buffers are queued when released */
1652 g_assert_not_reached ();
1657 g_assert_not_reached ();
1660 /* we can start capturing now, we wait for the playback
1661 * case until we queued the first buffer */
1662 if (!V4L2_TYPE_IS_OUTPUT (obj->type))
1663 if (!start_streaming (pool))
1671 GST_ERROR_OBJECT (pool, "failed to start streaming");