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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
33 #include "gst/video/video.h"
34 #include "gst/video/gstmetavideo.h"
36 #include <gstv4l2bufferpool.h>
38 #include "gstv4l2src.h"
39 #include "gstv4l2sink.h"
40 #include "v4l2_calls.h"
41 #include "gst/gst-i18n-plugin.h"
43 /* videodev2.h is not versioned and we can't easily check for the presence
44 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
45 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
46 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
47 #define V4L2_FIELD_INTERLACED_TB 8
48 #define V4L2_FIELD_INTERLACED_BT 9
52 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
53 #define GST_CAT_DEFAULT v4l2_debug
59 gst_meta_v4l2_get_info (void)
61 static const GstMetaInfo *meta_info = NULL;
63 if (meta_info == NULL) {
65 gst_meta_register ("GstMetaV4l2", "GstMetaV4l2",
66 sizeof (GstMetaV4l2), (GstMetaInitFunction) NULL,
67 (GstMetaFreeFunction) NULL, (GstMetaCopyFunction) NULL,
68 (GstMetaTransformFunction) NULL);
76 #define gst_v4l2_buffer_pool_parent_class parent_class
77 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
79 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
83 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
85 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
93 case GST_V4L2_IO_MMAP:
98 meta = GST_META_V4L2_GET (buffer);
99 g_assert (meta != NULL);
101 index = meta->vbuffer.index;
102 GST_LOG_OBJECT (pool,
103 "mmap buffer %p idx %d (data %p, len %u) freed, unmapping", buffer,
104 index, meta->mem, meta->vbuffer.length);
106 v4l2_munmap (meta->mem, meta->vbuffer.length);
107 pool->buffers[index] = NULL;
110 case GST_V4L2_IO_USERPTR:
112 g_assert_not_reached ();
115 gst_buffer_unref (buffer);
119 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
120 GstBufferPoolParams * params)
122 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
136 gst_buffer_new_allocate (pool->allocator, pool->size, pool->align);
139 case GST_V4L2_IO_MMAP:
141 newbuf = gst_buffer_new ();
142 meta = GST_META_V4L2_ADD (newbuf);
144 index = pool->num_allocated;
146 GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf, pool);
148 meta->vbuffer.index = index;
149 meta->vbuffer.type = obj->type;
150 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
152 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
153 goto querybuf_failed;
155 GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
156 GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
157 GST_LOG_OBJECT (pool, " bytesused: %u", meta->vbuffer.bytesused);
158 GST_LOG_OBJECT (pool, " flags: %08x", meta->vbuffer.flags);
159 GST_LOG_OBJECT (pool, " field: %d", meta->vbuffer.field);
160 GST_LOG_OBJECT (pool, " memory: %d", meta->vbuffer.memory);
161 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
162 GST_LOG_OBJECT (pool, " MMAP offset: %u", meta->vbuffer.m.offset);
163 GST_LOG_OBJECT (pool, " length: %u", meta->vbuffer.length);
164 GST_LOG_OBJECT (pool, " input: %u", meta->vbuffer.input);
166 meta->mem = v4l2_mmap (0, meta->vbuffer.length,
167 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
168 meta->vbuffer.m.offset);
169 if (meta->mem == MAP_FAILED)
172 gst_buffer_take_memory (newbuf, -1,
173 gst_memory_new_wrapped (0,
174 meta->mem, NULL, meta->vbuffer.length, 0, meta->vbuffer.length));
176 /* add metadata to raw video buffers */
178 gsize offset[GST_VIDEO_MAX_PLANES];
179 gint stride[GST_VIDEO_MAX_PLANES];
182 stride[0] = obj->bytesperline;
184 GST_DEBUG_OBJECT (pool, "adding video meta, stride %d", stride[0]);
185 gst_buffer_add_meta_video_full (newbuf, info->flags,
186 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
187 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
192 case GST_V4L2_IO_USERPTR:
194 g_assert_not_reached ();
198 pool->num_allocated++;
207 gint errnosave = errno;
209 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
210 gst_buffer_unref (newbuf);
212 return GST_FLOW_ERROR;
216 gint errnosave = errno;
218 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
219 gst_buffer_unref (newbuf);
221 return GST_FLOW_ERROR;
226 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
228 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
230 guint size, min_buffers, max_buffers;
233 GST_DEBUG_OBJECT (pool, "set config");
235 /* parse the config and keep around */
236 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
237 &max_buffers, &prefix, &align))
240 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
243 pool->max_buffers = MAX (min_buffers, max_buffers);
244 pool->min_buffers = MIN (pool->max_buffers, min_buffers);
245 pool->prefix = prefix;
248 gst_buffer_pool_config_set (config, caps, size, min_buffers,
249 max_buffers, prefix, align);
251 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
255 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
261 start_streaming (GstV4l2BufferPool * pool)
263 GstV4l2Object *obj = pool->obj;
268 case GST_V4L2_IO_MMAP:
269 case GST_V4L2_IO_USERPTR:
270 GST_DEBUG_OBJECT (pool, "STREAMON");
271 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
275 g_assert_not_reached ();
279 pool->streaming = TRUE;
286 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
293 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
295 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
296 GstV4l2Object *obj = pool->obj;
298 struct v4l2_requestbuffers breq;
299 gint min_buffers, max_buffers;
301 min_buffers = pool->min_buffers;
302 max_buffers = pool->max_buffers;
309 case GST_V4L2_IO_MMAP:
311 /* first, lets request buffers, and see how many we can get: */
312 GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
315 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
316 breq.type = obj->type;
317 breq.count = max_buffers;
318 breq.memory = V4L2_MEMORY_MMAP;
320 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
323 GST_LOG_OBJECT (pool, " count: %u", breq.count);
324 GST_LOG_OBJECT (pool, " type: %d", breq.type);
325 GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
327 if (breq.count < GST_V4L2_MIN_BUFFERS)
330 if (max_buffers != breq.count) {
331 GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
332 max_buffers = breq.count;
336 case GST_V4L2_IO_USERPTR:
338 g_assert_not_reached ();
343 pool->max_buffers = max_buffers;
344 pool->buffers = g_new0 (GstBuffer *, max_buffers);
345 pool->num_allocated = 0;
347 /* now, allocate the buffers: */
348 for (n = 0; n < min_buffers; n++) {
351 if (gst_v4l2_buffer_pool_alloc_buffer (bpool, &buffer, NULL) != GST_FLOW_OK)
352 goto buffer_new_failed;
354 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
357 /* we can start capturing now, we wait for the playback case until we queued
358 * the first buffer */
359 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
360 if (!start_streaming (pool))
363 gst_poll_set_flushing (obj->poll, FALSE);
370 GST_ERROR_OBJECT (pool,
371 "error requesting %d buffers: %s", max_buffers, g_strerror (errno));
376 GST_ERROR_OBJECT (pool,
377 "we received %d from device '%s', we want at least %d",
378 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
383 GST_ERROR_OBJECT (pool, "failed to create a buffer");
388 GST_ERROR_OBJECT (pool, "failed to start streaming");
394 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
397 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
398 GstV4l2Object *obj = pool->obj;
401 GST_DEBUG_OBJECT (pool, "stopping pool");
403 gst_poll_set_flushing (obj->poll, TRUE);
405 if (pool->streaming) {
409 case GST_V4L2_IO_MMAP:
410 case GST_V4L2_IO_USERPTR:
411 /* we actually need to sync on all queued buffers but not
412 * on the non-queued ones */
413 GST_DEBUG_OBJECT (pool, "STREAMOFF");
414 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
418 g_assert_not_reached ();
421 pool->streaming = FALSE;
424 /* first free the buffers in the queue */
425 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
427 /* then free the remaining buffers */
428 for (n = 0; n < pool->num_allocated; n++) {
429 if (pool->buffers[n])
430 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
437 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
444 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
448 if (v4l2object->can_poll_device) {
449 GST_LOG_OBJECT (v4l2object->element, "polling device");
450 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
451 if (G_UNLIKELY (ret < 0)) {
454 if (errno == ENXIO) {
455 GST_WARNING_OBJECT (v4l2object->element,
456 "v4l2 device doesn't support polling. Disabling");
457 v4l2object->can_poll_device = FALSE;
459 if (errno != EAGAIN && errno != EINTR)
469 GST_DEBUG ("stop called");
470 return GST_FLOW_WRONG_STATE;
474 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
475 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
476 return GST_FLOW_ERROR;
481 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
486 meta = GST_META_V4L2_GET (buf);
487 g_assert (meta != NULL);
489 index = meta->vbuffer.index;
491 GST_LOG_OBJECT (pool, "enqueue buffer %p, index:%d, queued:%d", buf,
492 index, pool->num_queued);
494 if (pool->buffers[index] != NULL)
497 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
500 pool->buffers[index] = buf;
508 GST_WARNING_OBJECT (pool, "the buffer was already queued");
509 return GST_FLOW_ERROR;
513 GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
515 return GST_FLOW_ERROR;
520 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
524 struct v4l2_buffer vbuffer;
525 GstV4l2Object *obj = pool->obj;
527 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
528 /* select works for input devices when data is available. According to the
529 * specs we can also poll to find out when a frame has been displayed but
530 * that just seems to lock up here */
531 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
535 memset (&vbuffer, 0x00, sizeof (vbuffer));
536 vbuffer.type = obj->type;
537 vbuffer.memory = V4L2_MEMORY_MMAP;
539 GST_LOG_OBJECT (pool, "doing DQBUF");
540 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
543 /* get our GstBuffer with that index from the pool, if the buffer was
544 * outstanding we have a serious problem.
546 outbuf = pool->buffers[vbuffer.index];
550 /* mark the buffer outstanding */
551 pool->buffers[vbuffer.index] = NULL;
554 GST_LOG_OBJECT (pool,
555 "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, pool-queued=%d, buffer=%p",
556 outbuf, vbuffer.sequence, vbuffer.index, vbuffer.bytesused, vbuffer.flags,
557 pool->num_queued, outbuf);
559 /* set top/bottom field first if v4l2_buffer has the information */
560 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB)
561 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
562 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT)
563 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_TFF);
565 /* this can change at every frame, esp. with jpeg */
566 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
567 gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
569 gst_buffer_resize (outbuf, 0, vbuffer.length);
578 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
583 GST_WARNING_OBJECT (pool,
584 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
585 vbuffer.sequence, vbuffer.index,
586 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
590 GST_WARNING_OBJECT (pool,
591 "Non-blocking I/O has been selected using O_NONBLOCK and"
592 " no buffer was in the outgoing queue. device %s", obj->videodev);
595 GST_ERROR_OBJECT (pool,
596 "The buffer type is not supported, or the index is out of bounds, "
597 "or no buffers have been allocated yet, or the userptr "
598 "or length are invalid. device %s", obj->videodev);
601 GST_ERROR_OBJECT (pool,
602 "insufficient memory to enqueue a user pointer buffer");
605 GST_INFO_OBJECT (pool,
606 "VIDIOC_DQBUF failed due to an internal error."
607 " Can also indicate temporary problems like signal loss."
608 " Note the driver might dequeue an (empty) buffer despite"
609 " returning an error, or even stop capturing."
610 " device %s", obj->videodev);
611 /* have we de-queued a buffer ? */
612 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
613 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
614 /* FIXME ... should we do something here? */
618 GST_WARNING_OBJECT (pool,
619 "could not sync on a buffer on device %s", obj->videodev);
622 GST_WARNING_OBJECT (pool,
623 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
624 obj->videodev, errno, g_strerror (errno));
627 return GST_FLOW_ERROR;
631 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
633 return GST_FLOW_ERROR;
638 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
639 GstBufferPoolParams * params)
642 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
643 GstV4l2Object *obj = pool->obj;
645 GST_DEBUG_OBJECT (pool, "acquire");
647 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
651 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
652 /* capture, This function should return a buffer with new captured data */
655 /* take empty buffer from the pool */
656 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
660 case GST_V4L2_IO_MMAP:
661 /* just dequeue a buffer, we basically use the queue of v4l2 as the
662 * storage for our buffers. This function does poll first so we can
663 * interrupt it fine. */
664 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
667 case GST_V4L2_IO_USERPTR:
669 g_assert_not_reached ();
674 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
675 /* playback, This function should return an empty buffer */
678 /* get an empty buffer */
679 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
683 case GST_V4L2_IO_MMAP:
684 /* get a free unqueued buffer */
685 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
689 case GST_V4L2_IO_USERPTR:
691 g_assert_not_reached ();
697 g_assert_not_reached ();
705 GST_DEBUG_OBJECT (pool, "We are flushing");
706 return GST_FLOW_WRONG_STATE;
711 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
713 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
714 GstV4l2Object *obj = pool->obj;
716 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
719 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
720 /* capture, put the buffer back in the queue so that we can refill it
724 /* release back in the pool */
725 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
728 case GST_V4L2_IO_MMAP:
729 /* queue back in the device */
730 gst_v4l2_buffer_pool_qbuf (pool, buffer);
733 case GST_V4L2_IO_USERPTR:
735 g_assert_not_reached ();
740 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
743 /* release back in the pool */
744 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
747 case GST_V4L2_IO_MMAP:
751 meta = GST_META_V4L2_GET (buffer);
752 g_assert (meta != NULL);
754 if (pool->buffers[meta->vbuffer.index] == NULL) {
755 GST_LOG_OBJECT (pool, "buffer not queued, putting on free list");
756 /* playback, put the buffer back in the queue to refill later. */
757 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
760 /* the buffer is queued in the device but maybe not played yet. We just
761 * leave it there and not make it available for future calls to acquire
762 * for now. The buffer will be dequeued and reused later. */
763 GST_LOG_OBJECT (pool, "buffer is queued");
768 case GST_V4L2_IO_USERPTR:
770 g_assert_not_reached ();
776 g_assert_not_reached ();
782 gst_v4l2_buffer_pool_finalize (GObject * object)
784 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
786 if (pool->video_fd >= 0)
787 v4l2_close (pool->video_fd);
790 g_free (pool->buffers);
791 pool->buffers = NULL;
794 G_OBJECT_CLASS (parent_class)->finalize (object);
798 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
803 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
805 GObjectClass *object_class = G_OBJECT_CLASS (klass);
806 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
808 object_class->finalize = gst_v4l2_buffer_pool_finalize;
810 bufferpool_class->start = gst_v4l2_buffer_pool_start;
811 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
812 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
813 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
814 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
815 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
816 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
820 * gst_v4l2_buffer_pool_new:
821 * @obj: the v4l2 object owning the pool
823 * Construct a new buffer pool.
825 * Returns: the new pool, use gst_object_unref() to free resources
828 gst_v4l2_buffer_pool_new (GstV4l2Object * obj)
830 GstV4l2BufferPool *pool;
833 fd = v4l2_dup (obj->video_fd);
837 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
841 return GST_BUFFER_POOL_CAST (pool);
846 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
852 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
855 GstV4l2Object *obj = pool->obj;
860 buffersize = gst_buffer_get_size (buf);
862 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", buffersize, buf);
864 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
867 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
870 amount = v4l2_read (obj->video_fd, data, buffersize);
872 if (amount == buffersize) {
874 } else if (amount == -1) {
875 if (errno == EAGAIN || errno == EINTR) {
880 /* short reads can happen if a signal interrupts the read */
885 GST_LOG_OBJECT (pool, "read %d bytes", amount);
886 gst_buffer_unmap (buf, data, amount);
893 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
898 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
899 (_("Error reading %d bytes from device '%s'."),
900 buffersize, obj->videodev), GST_ERROR_SYSTEM);
901 res = GST_FLOW_ERROR;
906 gst_buffer_unmap (buf, data, 0);
912 * gst_v4l2_buffer_pool_process:
913 * @bpool: a #GstBufferPool
916 * Process @buf in @bpool. For capture devices, this functions fills @buf with
917 * data from the device. For output devices, this functions send the contents of
918 * @buf to the device for playback.
920 * Returns: %GST_FLOW_OK on success.
923 gst_v4l2_buffer_pool_process (GstBufferPool * bpool, GstBuffer * buf)
925 GstFlowReturn ret = GST_FLOW_OK;
926 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
927 GstV4l2Object *obj = pool->obj;
929 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
932 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
936 /* capture into the buffer */
937 ret = gst_v4l2_do_read (pool, buf);
940 case GST_V4L2_IO_MMAP:
944 if (buf->pool == bpool)
945 /* nothing, data was inside the buffer when we did _acquire() */
948 /* buffer not from our pool, grab a frame and copy it into the target */
949 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
952 if (!gst_v4l2_object_copy (obj, buf, tmp))
955 /* an queue the buffer again after the copy */
956 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
961 case GST_V4L2_IO_USERPTR:
963 g_assert_not_reached ();
968 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
972 /* FIXME, do write() */
973 GST_WARNING_OBJECT (pool, "implement write()");
976 case GST_V4L2_IO_MMAP:
980 if (buf->pool == bpool) {
981 /* nothing, we can queue directly */
984 /* this can block if all buffers are outstanding which would be
985 * strange because we would expect the upstream element to have
986 * allocated them and returned to us.. */
987 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
989 if (ret != GST_FLOW_OK)
992 /* copy into it and queue */
993 if (!gst_v4l2_object_copy (obj, to_queue, buf))
997 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1000 /* if we are not streaming yet (this is the first buffer, start
1002 if (!pool->streaming)
1003 if (!start_streaming (pool))
1006 if (pool->num_queued == pool->num_allocated) {
1007 /* all buffers are queued, try to dequeue one and release it back
1008 * into the pool so that _acquire can get to it again. */
1009 ret = gst_v4l2_buffer_pool_dqbuf (pool, &to_queue);
1010 if (ret != GST_FLOW_OK)
1013 /* release the rendered buffer back into the pool. This wakes up any
1014 * thread waiting for a buffer in _acquire() */
1015 gst_v4l2_buffer_pool_release_buffer (bpool, to_queue);
1020 case GST_V4L2_IO_USERPTR:
1022 g_assert_not_reached ();
1027 g_assert_not_reached ();
1036 GST_ERROR_OBJECT (obj->element, "failed to copy data");
1037 return GST_FLOW_ERROR;
1041 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1042 return GST_FLOW_ERROR;