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);
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 ();
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->min_buffers = min_buffers;
244 pool->max_buffers = max_buffers;
245 pool->prefix = prefix;
252 GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
258 start_streaming (GstBufferPool * bpool)
260 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
261 GstV4l2Object *obj = pool->obj;
266 case GST_V4L2_IO_MMAP:
267 case GST_V4L2_IO_USERPTR:
268 GST_DEBUG_OBJECT (pool, "STREAMON");
269 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
273 g_assert_not_reached ();
277 pool->streaming = TRUE;
284 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
291 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
293 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
294 GstV4l2Object *obj = pool->obj;
296 struct v4l2_requestbuffers breq;
299 num_buffers = pool->min_buffers;
306 case GST_V4L2_IO_MMAP:
308 /* first, lets request buffers, and see how many we can get: */
309 GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
312 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
313 breq.type = obj->type;
314 breq.count = num_buffers;
315 breq.memory = V4L2_MEMORY_MMAP;
317 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
320 GST_LOG_OBJECT (pool, " count: %u", breq.count);
321 GST_LOG_OBJECT (pool, " type: %d", breq.type);
322 GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
324 if (breq.count < GST_V4L2_MIN_BUFFERS)
327 if (num_buffers != breq.count) {
328 GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
329 num_buffers = breq.count;
333 case GST_V4L2_IO_USERPTR:
335 g_assert_not_reached ();
340 pool->num_buffers = num_buffers;
341 pool->buffers = g_new0 (GstBuffer *, num_buffers);
344 /* now, allocate the buffers: */
345 for (n = 0; n < num_buffers; n++) {
348 if (gst_v4l2_buffer_pool_alloc_buffer (bpool, &buffer, NULL) != GST_FLOW_OK)
349 goto buffer_new_failed;
351 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
354 /* we can start capturing now, we wait for the playback case until we queued
355 * the first buffer */
356 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
357 if (!start_streaming (bpool))
365 GST_ERROR_OBJECT (pool,
366 "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
371 GST_ERROR_OBJECT (pool,
372 "we received %d from device '%s', we want at least %d",
373 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
378 GST_ERROR_OBJECT (pool, "failed to create a buffer");
383 GST_ERROR_OBJECT (pool, "failed to start streaming");
389 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
392 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
393 GstV4l2Object *obj = pool->obj;
396 GST_DEBUG_OBJECT (pool, "stopping pool");
401 case GST_V4L2_IO_MMAP:
402 case GST_V4L2_IO_USERPTR:
403 /* we actually need to sync on all queued buffers but not
404 * on the non-queued ones */
405 GST_DEBUG_OBJECT (pool, "STREAMOFF");
406 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
410 g_assert_not_reached ();
414 pool->streaming = FALSE;
416 /* first free the buffers in the queue */
417 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
419 /* then free the remaining buffers */
420 for (n = 0; n < pool->num_buffers; n++) {
421 if (pool->buffers[n])
422 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
429 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
436 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
440 if (v4l2object->can_poll_device) {
441 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
442 if (G_UNLIKELY (ret < 0)) {
445 if (errno == ENXIO) {
446 GST_DEBUG_OBJECT (v4l2object->element,
447 "v4l2 device doesn't support polling. Disabling");
448 v4l2object->can_poll_device = FALSE;
450 if (errno != EAGAIN && errno != EINTR)
460 GST_DEBUG ("stop called");
461 return GST_FLOW_WRONG_STATE;
465 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
466 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
467 return GST_FLOW_ERROR;
472 gst_v4l2_buffer_pool_qbuf (GstBufferPool * bpool, GstBuffer * buf)
474 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
478 meta = GST_META_V4L2_GET (buf);
479 g_assert (meta != NULL);
481 index = meta->vbuffer.index;
483 GST_LOG_OBJECT (pool, "enqueue pool buffer %d, queued: %d", index,
486 if (pool->buffers[index] != NULL)
489 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
492 pool->buffers[index] = buf;
501 GST_WARNING_OBJECT (pool, "the buffer was already queued");
502 return GST_FLOW_ERROR;
506 GST_WARNING_OBJECT (pool, "could not queue a buffer");
507 return GST_FLOW_ERROR;
512 gst_v4l2_buffer_pool_dqbuf (GstBufferPool * bpool, GstBuffer ** buffer)
515 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
517 struct v4l2_buffer vbuffer;
518 GstV4l2Object *obj = pool->obj;
520 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
523 memset (&vbuffer, 0x00, sizeof (vbuffer));
524 vbuffer.type = obj->type;
525 vbuffer.memory = V4L2_MEMORY_MMAP;
527 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
530 /* get our GstBuffer with that index from the pool, if the buffer was
531 * outstanding we have a serious problem.
533 outbuf = pool->buffers[vbuffer.index];
537 /* mark the buffer outstanding */
538 pool->buffers[vbuffer.index] = NULL;
541 GST_LOG_OBJECT (pool,
542 "dequeued frame %d (ix=%d), used %d, flags %08x, pool-queued=%d, buffer=%p",
543 vbuffer.sequence, vbuffer.index, vbuffer.bytesused, vbuffer.flags,
544 pool->num_queued, outbuf);
546 /* set top/bottom field first if v4l2_buffer has the information */
547 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB)
548 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
549 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT)
550 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_TFF);
552 /* this can change at every frame, esp. with jpeg */
553 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
554 gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
556 gst_buffer_resize (outbuf, 0, vbuffer.length);
565 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
570 GST_WARNING_OBJECT (pool,
571 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
572 vbuffer.sequence, vbuffer.index,
573 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
577 GST_WARNING_OBJECT (pool,
578 "Non-blocking I/O has been selected using O_NONBLOCK and"
579 " no buffer was in the outgoing queue. device %s", obj->videodev);
582 GST_ERROR_OBJECT (pool,
583 "The buffer type is not supported, or the index is out of bounds, "
584 "or no buffers have been allocated yet, or the userptr "
585 "or length are invalid. device %s", obj->videodev);
588 GST_ERROR_OBJECT (pool,
589 "insufficient memory to enqueue a user pointer buffer");
592 GST_INFO_OBJECT (pool,
593 "VIDIOC_DQBUF failed due to an internal error."
594 " Can also indicate temporary problems like signal loss."
595 " Note the driver might dequeue an (empty) buffer despite"
596 " returning an error, or even stop capturing."
597 " device %s", obj->videodev);
598 /* have we de-queued a buffer ? */
599 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
600 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
601 /* FIXME ... should we do something here? */
605 GST_WARNING_OBJECT (pool,
606 "could not sync on a buffer on device %s", obj->videodev);
609 GST_WARNING_OBJECT (pool,
610 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
611 obj->videodev, errno, g_strerror (errno));
614 return GST_FLOW_ERROR;
618 GST_ERROR_OBJECT (pool, "No free buffers found in the pool at index %d.",
620 return GST_FLOW_ERROR;
625 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
626 GstBufferPoolParams * params)
629 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
630 GstV4l2Object *obj = pool->obj;
632 GST_DEBUG_OBJECT (pool, "acquire");
634 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
638 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
639 /* capture, This function should return a buffer with new captured data */
642 /* take empty buffer from the pool */
643 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
647 case GST_V4L2_IO_MMAP:
648 /* just dequeue a buffer, we basically use the queue of v4l2 as the
649 * storage for our buffers. */
650 ret = gst_v4l2_buffer_pool_dqbuf (bpool, buffer);
653 case GST_V4L2_IO_USERPTR:
655 g_assert_not_reached ();
660 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
661 /* playback, This function should return an empty buffer */
664 /* get an empty buffer */
665 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
669 case GST_V4L2_IO_MMAP:
670 /* first try to get a free unqueued buffer */
671 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
673 if (ret == GST_FLOW_UNEXPECTED) {
674 /* all buffers are queued, try to dequeue one */
675 ret = gst_v4l2_buffer_pool_dqbuf (bpool, buffer);
679 case GST_V4L2_IO_USERPTR:
681 g_assert_not_reached ();
687 g_assert_not_reached ();
695 GST_DEBUG_OBJECT (bpool, "We are flushing");
696 return GST_FLOW_WRONG_STATE;
701 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
703 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
704 GstV4l2Object *obj = pool->obj;
706 GST_DEBUG_OBJECT (pool, "release");
709 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
710 /* capture, put the buffer back in the queue so that we can refill it
714 /* release back in the pool */
715 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
718 case GST_V4L2_IO_MMAP:
719 /* queue back in the device */
720 gst_v4l2_buffer_pool_qbuf (bpool, buffer);
723 case GST_V4L2_IO_USERPTR:
725 g_assert_not_reached ();
730 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
731 /* playback, put the buffer back in the queue to refill later. */
732 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
736 g_assert_not_reached ();
742 gst_v4l2_buffer_pool_finalize (GObject * object)
744 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
746 if (pool->video_fd >= 0)
747 v4l2_close (pool->video_fd);
750 g_free (pool->buffers);
751 pool->buffers = NULL;
754 G_OBJECT_CLASS (parent_class)->finalize (object);
758 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
763 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
765 GObjectClass *object_class = G_OBJECT_CLASS (klass);
766 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
768 object_class->finalize = gst_v4l2_buffer_pool_finalize;
770 bufferpool_class->start = gst_v4l2_buffer_pool_start;
771 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
772 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
773 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
774 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
775 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
776 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
780 * gst_v4l2_buffer_pool_new:
781 * @obj: the v4l2 object owning the pool
782 * @num_buffers: the requested number of buffers in the pool
783 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
784 * buffer with no remaining references is immediately passed back to v4l2
785 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
786 * (which can be accessed via gst_v4l2_buffer_pool_get().
788 * Construct a new buffer pool.
790 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
793 gst_v4l2_buffer_pool_new (GstV4l2Object * obj)
795 GstV4l2BufferPool *pool;
798 fd = v4l2_dup (obj->video_fd);
802 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
806 return GST_BUFFER_POOL_CAST (pool);
811 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
817 gst_v4l2_do_read (GstBufferPool * bpool, GstBuffer * buf)
820 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
821 GstV4l2Object *obj = pool->obj;
826 buffersize = gst_buffer_get_size (buf);
828 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", buffersize, buf);
830 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
833 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
836 amount = v4l2_read (obj->video_fd, data, buffersize);
838 if (amount == buffersize) {
840 } else if (amount == -1) {
841 if (errno == EAGAIN || errno == EINTR) {
846 /* short reads can happen if a signal interrupts the read */
851 GST_LOG_OBJECT (pool, "read %d bytes", amount);
852 gst_buffer_unmap (buf, data, amount);
859 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
864 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
865 (_("Error reading %d bytes from device '%s'."),
866 buffersize, obj->videodev), GST_ERROR_SYSTEM);
867 res = GST_FLOW_ERROR;
872 gst_buffer_unmap (buf, data, 0);
878 * gst_v4l2_buffer_pool_process:
879 * @bpool: a #GstBufferPool
882 * Process @buf in @bpool. For capture devices, this functions fills @buf with
883 * data from the device. For output devices, this functions send the contents of
884 * @buf to the device for playback.
886 * Returns: %GST_FLOW_OK on success.
889 gst_v4l2_buffer_pool_process (GstBufferPool * bpool, GstBuffer * buf)
891 GstFlowReturn ret = GST_FLOW_OK;
892 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
893 GstV4l2Object *obj = pool->obj;
896 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
900 /* capture into the buffer */
901 ret = gst_v4l2_do_read (bpool, buf);
904 case GST_V4L2_IO_MMAP:
908 if (buf->pool == bpool)
909 /* nothing, data was inside the buffer when we did _acquire() */
912 /* buffer not from our pool, grab a frame and copy it into the target */
913 if ((ret = gst_v4l2_buffer_pool_dqbuf (bpool, &tmp)) != GST_FLOW_OK)
916 if (!gst_v4l2_object_copy (obj, buf, tmp))
919 if ((ret = gst_v4l2_buffer_pool_qbuf (bpool, tmp)) != GST_FLOW_OK)
924 case GST_V4L2_IO_USERPTR:
926 g_assert_not_reached ();
931 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
935 /* FIXME, do write() */
938 case GST_V4L2_IO_MMAP:
942 if (buf->pool == bpool) {
943 /* nothing, we can queue directly */
944 ret = gst_v4l2_buffer_pool_qbuf (bpool, buf);
946 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
948 if (ret == GST_FLOW_UNEXPECTED) {
949 /* all buffers are queued, try to dequeue one */
950 ret = gst_v4l2_buffer_pool_dqbuf (bpool, &tmp);
952 if (ret != GST_FLOW_OK)
955 /* copy into it and queue */
956 if (!gst_v4l2_object_copy (obj, tmp, buf))
959 if ((ret = gst_v4l2_buffer_pool_qbuf (bpool, tmp)) != GST_FLOW_OK)
962 /* if we are not streaming yet (this is the first buffer, start
964 if (!pool->streaming)
965 if (!start_streaming (bpool))
971 case GST_V4L2_IO_USERPTR:
973 g_assert_not_reached ();
978 g_assert_not_reached ();
987 GST_ERROR_OBJECT (obj->element, "failed to copy data");
988 return GST_FLOW_ERROR;
992 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
993 return GST_FLOW_ERROR;
998 * gst_v4l2_buffer_pool_available_buffers:
1001 * Check the number of buffers available to the driver, ie. buffers that
1002 * have been QBUF'd but not yet DQBUF'd.
1004 * Returns: the number of buffers available.
1007 gst_v4l2_buffer_pool_available_buffers (GstBufferPool * bpool)
1009 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1011 return pool->num_queued;