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;
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);
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))
368 GST_ERROR_OBJECT (pool,
369 "error requesting %d buffers: %s", max_buffers, g_strerror (errno));
374 GST_ERROR_OBJECT (pool,
375 "we received %d from device '%s', we want at least %d",
376 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
381 GST_ERROR_OBJECT (pool, "failed to create a buffer");
386 GST_ERROR_OBJECT (pool, "failed to start streaming");
392 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
395 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
396 GstV4l2Object *obj = pool->obj;
399 GST_DEBUG_OBJECT (pool, "stopping pool");
401 if (pool->streaming) {
405 case GST_V4L2_IO_MMAP:
406 case GST_V4L2_IO_USERPTR:
407 /* we actually need to sync on all queued buffers but not
408 * on the non-queued ones */
409 GST_DEBUG_OBJECT (pool, "STREAMOFF");
410 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
414 g_assert_not_reached ();
417 pool->streaming = FALSE;
420 /* first free the buffers in the queue */
421 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
423 /* then free the remaining buffers */
424 for (n = 0; n < pool->num_buffers; n++) {
425 if (pool->buffers[n])
426 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
433 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
440 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
444 if (v4l2object->can_poll_device) {
445 GST_LOG_OBJECT (v4l2object->element, "polling device");
446 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
447 if (G_UNLIKELY (ret < 0)) {
450 if (errno == ENXIO) {
451 GST_WARNING_OBJECT (v4l2object->element,
452 "v4l2 device doesn't support polling. Disabling");
453 v4l2object->can_poll_device = FALSE;
455 if (errno != EAGAIN && errno != EINTR)
465 GST_DEBUG ("stop called");
466 return GST_FLOW_WRONG_STATE;
470 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
471 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
472 return GST_FLOW_ERROR;
477 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
482 meta = GST_META_V4L2_GET (buf);
483 g_assert (meta != NULL);
485 index = meta->vbuffer.index;
487 GST_LOG_OBJECT (pool, "enqueue buffer %p, index:%d, queued:%d", buf,
488 index, pool->num_queued);
490 if (pool->buffers[index] != NULL)
493 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
496 pool->buffers[index] = buf;
504 GST_WARNING_OBJECT (pool, "the buffer was already queued");
505 return GST_FLOW_ERROR;
509 GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
511 return GST_FLOW_ERROR;
516 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
520 struct v4l2_buffer vbuffer;
521 GstV4l2Object *obj = pool->obj;
523 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
524 /* select works for input devices when data is available. According to the
525 * specs we can also poll to find out when a frame has been displayed but
526 * that just seems to lock up here */
527 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
531 memset (&vbuffer, 0x00, sizeof (vbuffer));
532 vbuffer.type = obj->type;
533 vbuffer.memory = V4L2_MEMORY_MMAP;
535 GST_LOG_OBJECT (pool, "doing DQBUF");
536 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
539 /* get our GstBuffer with that index from the pool, if the buffer was
540 * outstanding we have a serious problem.
542 outbuf = pool->buffers[vbuffer.index];
546 /* mark the buffer outstanding */
547 pool->buffers[vbuffer.index] = NULL;
550 GST_LOG_OBJECT (pool,
551 "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, pool-queued=%d, buffer=%p",
552 outbuf, vbuffer.sequence, vbuffer.index, vbuffer.bytesused, vbuffer.flags,
553 pool->num_queued, outbuf);
555 /* set top/bottom field first if v4l2_buffer has the information */
556 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB)
557 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
558 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT)
559 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_TFF);
561 /* this can change at every frame, esp. with jpeg */
562 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
563 gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
565 gst_buffer_resize (outbuf, 0, vbuffer.length);
574 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
579 GST_WARNING_OBJECT (pool,
580 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
581 vbuffer.sequence, vbuffer.index,
582 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
586 GST_WARNING_OBJECT (pool,
587 "Non-blocking I/O has been selected using O_NONBLOCK and"
588 " no buffer was in the outgoing queue. device %s", obj->videodev);
591 GST_ERROR_OBJECT (pool,
592 "The buffer type is not supported, or the index is out of bounds, "
593 "or no buffers have been allocated yet, or the userptr "
594 "or length are invalid. device %s", obj->videodev);
597 GST_ERROR_OBJECT (pool,
598 "insufficient memory to enqueue a user pointer buffer");
601 GST_INFO_OBJECT (pool,
602 "VIDIOC_DQBUF failed due to an internal error."
603 " Can also indicate temporary problems like signal loss."
604 " Note the driver might dequeue an (empty) buffer despite"
605 " returning an error, or even stop capturing."
606 " device %s", obj->videodev);
607 /* have we de-queued a buffer ? */
608 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
609 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
610 /* FIXME ... should we do something here? */
614 GST_WARNING_OBJECT (pool,
615 "could not sync on a buffer on device %s", obj->videodev);
618 GST_WARNING_OBJECT (pool,
619 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
620 obj->videodev, errno, g_strerror (errno));
623 return GST_FLOW_ERROR;
627 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
629 return GST_FLOW_ERROR;
634 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
635 GstBufferPoolParams * params)
638 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
639 GstV4l2Object *obj = pool->obj;
641 GST_DEBUG_OBJECT (pool, "acquire");
643 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
647 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
648 /* capture, This function should return a buffer with new captured data */
651 /* take empty buffer from the pool */
652 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
656 case GST_V4L2_IO_MMAP:
657 /* just dequeue a buffer, we basically use the queue of v4l2 as the
658 * storage for our buffers. */
659 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
662 case GST_V4L2_IO_USERPTR:
664 g_assert_not_reached ();
669 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
670 /* playback, This function should return an empty buffer */
673 /* get an empty buffer */
674 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
678 case GST_V4L2_IO_MMAP:
680 GstBufferPoolParams tparams = { 0, };
685 tparams.flags |= GST_BUFFER_POOL_FLAG_DONTWAIT;
687 /* first try to get a free unqueued buffer */
688 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
690 if (ret == GST_FLOW_UNEXPECTED) {
691 GST_DEBUG_OBJECT (pool, "pool empty, try to dequeue a buffer");
692 /* all buffers are queued, try to dequeue one */
693 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
698 case GST_V4L2_IO_USERPTR:
700 g_assert_not_reached ();
706 g_assert_not_reached ();
714 GST_DEBUG_OBJECT (pool, "We are flushing");
715 return GST_FLOW_WRONG_STATE;
720 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
722 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
723 GstV4l2Object *obj = pool->obj;
725 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
728 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
729 /* capture, put the buffer back in the queue so that we can refill it
733 /* release back in the pool */
734 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
737 case GST_V4L2_IO_MMAP:
738 /* queue back in the device */
739 gst_v4l2_buffer_pool_qbuf (pool, buffer);
742 case GST_V4L2_IO_USERPTR:
744 g_assert_not_reached ();
749 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
753 meta = GST_META_V4L2_GET (buffer);
754 g_assert (meta != NULL);
756 if (pool->buffers[meta->vbuffer.index] == NULL) {
757 GST_LOG_OBJECT (pool, "buffer not queued, putting on free list");
758 /* playback, put the buffer back in the queue to refill later. */
759 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
761 GST_LOG_OBJECT (pool, "buffer is queued");
767 g_assert_not_reached ();
773 gst_v4l2_buffer_pool_finalize (GObject * object)
775 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
777 if (pool->video_fd >= 0)
778 v4l2_close (pool->video_fd);
781 g_free (pool->buffers);
782 pool->buffers = NULL;
785 G_OBJECT_CLASS (parent_class)->finalize (object);
789 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
794 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
796 GObjectClass *object_class = G_OBJECT_CLASS (klass);
797 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
799 object_class->finalize = gst_v4l2_buffer_pool_finalize;
801 bufferpool_class->start = gst_v4l2_buffer_pool_start;
802 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
803 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
804 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
805 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
806 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
807 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
811 * gst_v4l2_buffer_pool_new:
812 * @obj: the v4l2 object owning the pool
813 * @num_buffers: the requested number of buffers in the pool
814 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
815 * buffer with no remaining references is immediately passed back to v4l2
816 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
817 * (which can be accessed via gst_v4l2_buffer_pool_get().
819 * Construct a new buffer pool.
821 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
824 gst_v4l2_buffer_pool_new (GstV4l2Object * obj)
826 GstV4l2BufferPool *pool;
829 fd = v4l2_dup (obj->video_fd);
833 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
837 return GST_BUFFER_POOL_CAST (pool);
842 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
848 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
851 GstV4l2Object *obj = pool->obj;
856 buffersize = gst_buffer_get_size (buf);
858 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", buffersize, buf);
860 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
863 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
866 amount = v4l2_read (obj->video_fd, data, buffersize);
868 if (amount == buffersize) {
870 } else if (amount == -1) {
871 if (errno == EAGAIN || errno == EINTR) {
876 /* short reads can happen if a signal interrupts the read */
881 GST_LOG_OBJECT (pool, "read %d bytes", amount);
882 gst_buffer_unmap (buf, data, amount);
889 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
894 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
895 (_("Error reading %d bytes from device '%s'."),
896 buffersize, obj->videodev), GST_ERROR_SYSTEM);
897 res = GST_FLOW_ERROR;
902 gst_buffer_unmap (buf, data, 0);
908 * gst_v4l2_buffer_pool_process:
909 * @bpool: a #GstBufferPool
912 * Process @buf in @bpool. For capture devices, this functions fills @buf with
913 * data from the device. For output devices, this functions send the contents of
914 * @buf to the device for playback.
916 * Returns: %GST_FLOW_OK on success.
919 gst_v4l2_buffer_pool_process (GstBufferPool * bpool, GstBuffer * buf)
921 GstFlowReturn ret = GST_FLOW_OK;
922 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
923 GstV4l2Object *obj = pool->obj;
925 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
928 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
932 /* capture into the buffer */
933 ret = gst_v4l2_do_read (pool, buf);
936 case GST_V4L2_IO_MMAP:
940 if (buf->pool == bpool)
941 /* nothing, data was inside the buffer when we did _acquire() */
944 /* buffer not from our pool, grab a frame and copy it into the target */
945 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
948 if (!gst_v4l2_object_copy (obj, buf, tmp))
951 /* an queue the buffer again after the copy */
952 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
957 case GST_V4L2_IO_USERPTR:
959 g_assert_not_reached ();
964 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
968 /* FIXME, do write() */
971 case GST_V4L2_IO_MMAP:
975 if (buf->pool == bpool) {
976 /* nothing, we can queue directly */
979 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
981 if (ret == GST_FLOW_UNEXPECTED) {
982 GST_DEBUG_OBJECT (pool, "pool empty, try to dequeue a buffer");
983 /* all buffers are queued, try to dequeue one */
984 ret = gst_v4l2_buffer_pool_dqbuf (pool, &to_queue);
986 if (ret != GST_FLOW_OK)
989 /* copy into it and queue */
990 if (!gst_v4l2_object_copy (obj, to_queue, buf))
994 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
997 /* if we are not streaming yet (this is the first buffer, start
999 if (!pool->streaming)
1000 if (!start_streaming (pool))
1005 case GST_V4L2_IO_USERPTR:
1007 g_assert_not_reached ();
1012 g_assert_not_reached ();
1021 GST_ERROR_OBJECT (obj->element, "failed to copy data");
1022 return GST_FLOW_ERROR;
1026 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1027 return GST_FLOW_ERROR;
1032 * gst_v4l2_buffer_pool_available_buffers:
1035 * Check the number of buffers available to the driver, ie. buffers that
1036 * have been QBUF'd but not yet DQBUF'd.
1038 * Returns: the number of buffers available.
1041 gst_v4l2_buffer_pool_available_buffers (GstBufferPool * bpool)
1043 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
1045 return pool->num_queued;