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/gstvideometa.h"
35 #include "gst/video/gstvideopool.h"
37 #include <gstv4l2bufferpool.h>
39 #include "gstv4l2src.h"
40 #include "gstv4l2sink.h"
41 #include "v4l2_calls.h"
42 #include "gst/gst-i18n-plugin.h"
43 #include <gst/glib-compat-private.h>
45 /* videodev2.h is not versioned and we can't easily check for the presence
46 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
47 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
48 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
49 #define V4L2_FIELD_INTERLACED_TB 8
50 #define V4L2_FIELD_INTERLACED_BT 9
54 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
55 #define GST_CAT_DEFAULT v4l2_debug
61 gst_v4l2_meta_get_info (void)
63 static const GstMetaInfo *meta_info = NULL;
65 if (meta_info == NULL) {
67 gst_meta_register ("GstV4l2Meta", "GstV4l2Meta",
68 sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
69 (GstMetaFreeFunction) NULL, (GstMetaCopyFunction) NULL,
70 (GstMetaTransformFunction) NULL);
78 #define gst_v4l2_buffer_pool_parent_class parent_class
79 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
81 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
85 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
87 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
95 case GST_V4L2_IO_MMAP:
100 meta = GST_V4L2_META_GET (buffer);
101 g_assert (meta != NULL);
103 index = meta->vbuffer.index;
104 GST_LOG_OBJECT (pool,
105 "mmap buffer %p idx %d (data %p, len %u) freed, unmapping", buffer,
106 index, meta->mem, meta->vbuffer.length);
108 v4l2_munmap (meta->mem, meta->vbuffer.length);
109 pool->buffers[index] = NULL;
112 case GST_V4L2_IO_USERPTR:
114 g_assert_not_reached ();
117 gst_buffer_unref (buffer);
121 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
122 GstBufferPoolParams * params)
124 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
138 gst_buffer_new_allocate (pool->allocator, pool->size, pool->align);
141 case GST_V4L2_IO_MMAP:
143 newbuf = gst_buffer_new ();
144 meta = GST_V4L2_META_ADD (newbuf);
146 index = pool->num_allocated;
148 GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf);
150 meta->vbuffer.index = index;
151 meta->vbuffer.type = obj->type;
152 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
154 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
155 goto querybuf_failed;
157 GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
158 GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
159 GST_LOG_OBJECT (pool, " bytesused: %u", meta->vbuffer.bytesused);
160 GST_LOG_OBJECT (pool, " flags: %08x", meta->vbuffer.flags);
161 GST_LOG_OBJECT (pool, " field: %d", meta->vbuffer.field);
162 GST_LOG_OBJECT (pool, " memory: %d", meta->vbuffer.memory);
163 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
164 GST_LOG_OBJECT (pool, " MMAP offset: %u", meta->vbuffer.m.offset);
165 GST_LOG_OBJECT (pool, " length: %u", meta->vbuffer.length);
166 GST_LOG_OBJECT (pool, " input: %u", meta->vbuffer.input);
168 meta->mem = v4l2_mmap (0, meta->vbuffer.length,
169 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
170 meta->vbuffer.m.offset);
171 if (meta->mem == MAP_FAILED)
174 gst_buffer_take_memory (newbuf, -1,
175 gst_memory_new_wrapped (0,
176 meta->mem, NULL, meta->vbuffer.length, 0, meta->vbuffer.length));
178 /* add metadata to raw video buffers */
179 if (pool->add_videometa && info->finfo) {
180 gsize offset[GST_VIDEO_MAX_PLANES];
181 gint stride[GST_VIDEO_MAX_PLANES];
184 stride[0] = obj->bytesperline;
186 GST_DEBUG_OBJECT (pool, "adding video meta, stride %d", stride[0]);
187 gst_buffer_add_video_meta_full (newbuf, info->flags,
188 GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
189 GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
194 case GST_V4L2_IO_USERPTR:
196 g_assert_not_reached ();
200 pool->num_allocated++;
209 gint errnosave = errno;
211 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
212 gst_buffer_unref (newbuf);
214 return GST_FLOW_ERROR;
218 gint errnosave = errno;
220 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
221 gst_buffer_unref (newbuf);
223 return GST_FLOW_ERROR;
228 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
230 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
231 GstV4l2Object *obj = pool->obj;
233 guint size, min_buffers, max_buffers;
236 GST_DEBUG_OBJECT (pool, "set config");
238 pool->add_videometa =
239 gst_buffer_pool_config_has_option (config,
240 GST_BUFFER_POOL_OPTION_VIDEO_META);
242 if (!pool->add_videometa) {
245 /* we don't have video metadata, see if the strides are compatible */
246 stride = GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0);
248 GST_DEBUG_OBJECT (pool, "no videometadata, checking strides %d and %u",
249 stride, obj->bytesperline);
251 if (stride != obj->bytesperline)
252 goto missing_video_api;
255 /* parse the config and keep around */
256 if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
257 &max_buffers, &prefix, &align))
260 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
263 pool->max_buffers = MAX (min_buffers, max_buffers);
264 pool->min_buffers = MIN (pool->max_buffers, min_buffers);
265 pool->prefix = prefix;
268 gst_buffer_pool_config_set (config, caps, size, min_buffers,
269 max_buffers, prefix, align);
271 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
276 GST_ERROR_OBJECT (pool, "missing GstMetaVideo API in config, "
277 "default stride: %d, wanted stride %u",
278 GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0), obj->bytesperline);
283 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
289 start_streaming (GstV4l2BufferPool * pool)
291 GstV4l2Object *obj = pool->obj;
296 case GST_V4L2_IO_MMAP:
297 case GST_V4L2_IO_USERPTR:
298 GST_DEBUG_OBJECT (pool, "STREAMON");
299 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
303 g_assert_not_reached ();
307 pool->streaming = TRUE;
314 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
321 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
323 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
324 GstV4l2Object *obj = pool->obj;
326 struct v4l2_requestbuffers breq;
327 gint min_buffers, max_buffers;
329 min_buffers = pool->min_buffers;
330 max_buffers = pool->max_buffers;
337 case GST_V4L2_IO_MMAP:
339 /* first, lets request buffers, and see how many we can get: */
340 GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
343 if (max_buffers == 0)
346 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
347 breq.type = obj->type;
348 breq.count = max_buffers;
349 breq.memory = V4L2_MEMORY_MMAP;
351 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
354 GST_LOG_OBJECT (pool, " count: %u", breq.count);
355 GST_LOG_OBJECT (pool, " type: %d", breq.type);
356 GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
358 if (breq.count < GST_V4L2_MIN_BUFFERS)
361 if (max_buffers != breq.count) {
362 GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
363 max_buffers = breq.count;
367 case GST_V4L2_IO_USERPTR:
369 g_assert_not_reached ();
374 pool->max_buffers = max_buffers;
375 pool->buffers = g_new0 (GstBuffer *, max_buffers);
376 pool->num_allocated = 0;
378 /* now, allocate the buffers: */
379 for (n = 0; n < min_buffers; n++) {
382 if (gst_v4l2_buffer_pool_alloc_buffer (bpool, &buffer, NULL) != GST_FLOW_OK)
383 goto buffer_new_failed;
385 gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
388 /* we can start capturing now, we wait for the playback case until we queued
389 * the first buffer */
390 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
391 if (!start_streaming (pool))
394 gst_poll_set_flushing (obj->poll, FALSE);
401 GST_ERROR_OBJECT (pool,
402 "error requesting %d buffers: %s", max_buffers, g_strerror (errno));
407 GST_ERROR_OBJECT (pool,
408 "we received %d from device '%s', we want at least %d",
409 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
414 GST_ERROR_OBJECT (pool, "failed to create a buffer");
419 GST_ERROR_OBJECT (pool, "failed to start streaming");
425 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
428 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
429 GstV4l2Object *obj = pool->obj;
432 GST_DEBUG_OBJECT (pool, "stopping pool");
434 gst_poll_set_flushing (obj->poll, TRUE);
436 if (pool->streaming) {
440 case GST_V4L2_IO_MMAP:
441 case GST_V4L2_IO_USERPTR:
442 /* we actually need to sync on all queued buffers but not
443 * on the non-queued ones */
444 GST_DEBUG_OBJECT (pool, "STREAMOFF");
445 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
449 g_assert_not_reached ();
452 pool->streaming = FALSE;
455 /* first free the buffers in the queue */
456 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
458 /* then free the remaining buffers */
459 for (n = 0; n < pool->num_allocated; n++) {
460 if (pool->buffers[n])
461 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
463 g_free (pool->buffers);
464 pool->buffers = NULL;
471 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
478 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
482 if (v4l2object->can_poll_device) {
483 GST_LOG_OBJECT (v4l2object->element, "polling device");
484 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
485 if (G_UNLIKELY (ret < 0)) {
488 if (errno == ENXIO) {
489 GST_WARNING_OBJECT (v4l2object->element,
490 "v4l2 device doesn't support polling. Disabling");
491 v4l2object->can_poll_device = FALSE;
493 if (errno != EAGAIN && errno != EINTR)
503 GST_DEBUG ("stop called");
504 return GST_FLOW_FLUSHING;
508 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
509 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
510 return GST_FLOW_ERROR;
515 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
520 meta = GST_V4L2_META_GET (buf);
521 g_assert (meta != NULL);
523 index = meta->vbuffer.index;
525 GST_LOG_OBJECT (pool, "enqueue buffer %p, index:%d, queued:%d", buf,
526 index, pool->num_queued);
528 if (pool->buffers[index] != NULL)
531 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
534 pool->buffers[index] = buf;
542 GST_WARNING_OBJECT (pool, "the buffer was already queued");
543 return GST_FLOW_ERROR;
547 GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
549 return GST_FLOW_ERROR;
554 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
558 struct v4l2_buffer vbuffer;
559 GstV4l2Object *obj = pool->obj;
561 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
562 /* select works for input devices when data is available. According to the
563 * specs we can also poll to find out when a frame has been displayed but
564 * that just seems to lock up here */
565 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
569 memset (&vbuffer, 0x00, sizeof (vbuffer));
570 vbuffer.type = obj->type;
571 vbuffer.memory = V4L2_MEMORY_MMAP;
573 GST_LOG_OBJECT (pool, "doing DQBUF");
574 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
577 /* get our GstBuffer with that index from the pool, if the buffer was
578 * outstanding we have a serious problem.
580 outbuf = pool->buffers[vbuffer.index];
584 /* mark the buffer outstanding */
585 pool->buffers[vbuffer.index] = NULL;
588 GST_LOG_OBJECT (pool,
589 "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, pool-queued=%d, buffer=%p",
590 outbuf, vbuffer.sequence, vbuffer.index, vbuffer.bytesused, vbuffer.flags,
591 pool->num_queued, outbuf);
593 /* set top/bottom field first if v4l2_buffer has the information */
594 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
595 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
596 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
598 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
599 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
600 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
603 /* this can change at every frame, esp. with jpeg */
604 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
605 gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
607 gst_buffer_resize (outbuf, 0, vbuffer.length);
616 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
621 GST_WARNING_OBJECT (pool,
622 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
623 vbuffer.sequence, vbuffer.index,
624 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
628 GST_WARNING_OBJECT (pool,
629 "Non-blocking I/O has been selected using O_NONBLOCK and"
630 " no buffer was in the outgoing queue. device %s", obj->videodev);
633 GST_ERROR_OBJECT (pool,
634 "The buffer type is not supported, or the index is out of bounds, "
635 "or no buffers have been allocated yet, or the userptr "
636 "or length are invalid. device %s", obj->videodev);
639 GST_ERROR_OBJECT (pool,
640 "insufficient memory to enqueue a user pointer buffer");
643 GST_INFO_OBJECT (pool,
644 "VIDIOC_DQBUF failed due to an internal error."
645 " Can also indicate temporary problems like signal loss."
646 " Note the driver might dequeue an (empty) buffer despite"
647 " returning an error, or even stop capturing."
648 " device %s", obj->videodev);
649 /* have we de-queued a buffer ? */
650 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
651 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
652 /* FIXME ... should we do something here? */
656 GST_WARNING_OBJECT (pool,
657 "could not sync on a buffer on device %s", obj->videodev);
660 GST_WARNING_OBJECT (pool,
661 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
662 obj->videodev, errno, g_strerror (errno));
665 return GST_FLOW_ERROR;
669 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
671 return GST_FLOW_ERROR;
676 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
677 GstBufferPoolParams * params)
680 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
681 GstV4l2Object *obj = pool->obj;
683 GST_DEBUG_OBJECT (pool, "acquire");
685 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
689 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
690 /* capture, This function should return a buffer with new captured data */
693 /* take empty buffer from the pool */
694 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
698 case GST_V4L2_IO_MMAP:
699 /* just dequeue a buffer, we basically use the queue of v4l2 as the
700 * storage for our buffers. This function does poll first so we can
701 * interrupt it fine. */
702 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
705 case GST_V4L2_IO_USERPTR:
707 g_assert_not_reached ();
712 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
713 /* playback, This function should return an empty buffer */
716 /* get an empty buffer */
717 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
721 case GST_V4L2_IO_MMAP:
722 /* get a free unqueued buffer */
723 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
727 case GST_V4L2_IO_USERPTR:
729 g_assert_not_reached ();
735 g_assert_not_reached ();
743 GST_DEBUG_OBJECT (pool, "We are flushing");
744 return GST_FLOW_FLUSHING;
749 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
751 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
752 GstV4l2Object *obj = pool->obj;
754 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
757 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
758 /* capture, put the buffer back in the queue so that we can refill it
762 /* release back in the pool */
763 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
766 case GST_V4L2_IO_MMAP:
767 /* queue back in the device */
768 gst_v4l2_buffer_pool_qbuf (pool, buffer);
771 case GST_V4L2_IO_USERPTR:
773 g_assert_not_reached ();
778 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
781 /* release back in the pool */
782 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
785 case GST_V4L2_IO_MMAP:
789 meta = GST_V4L2_META_GET (buffer);
790 g_assert (meta != NULL);
792 if (pool->buffers[meta->vbuffer.index] == NULL) {
793 GST_LOG_OBJECT (pool, "buffer not queued, putting on free list");
794 /* playback, put the buffer back in the queue to refill later. */
795 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
798 /* the buffer is queued in the device but maybe not played yet. We just
799 * leave it there and not make it available for future calls to acquire
800 * for now. The buffer will be dequeued and reused later. */
801 GST_LOG_OBJECT (pool, "buffer is queued");
806 case GST_V4L2_IO_USERPTR:
808 g_assert_not_reached ();
814 g_assert_not_reached ();
820 gst_v4l2_buffer_pool_finalize (GObject * object)
822 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
824 if (pool->video_fd >= 0)
825 v4l2_close (pool->video_fd);
827 g_free (pool->buffers);
829 G_OBJECT_CLASS (parent_class)->finalize (object);
833 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
838 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
840 GObjectClass *object_class = G_OBJECT_CLASS (klass);
841 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
843 object_class->finalize = gst_v4l2_buffer_pool_finalize;
845 bufferpool_class->start = gst_v4l2_buffer_pool_start;
846 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
847 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
848 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
849 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
850 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
851 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
855 * gst_v4l2_buffer_pool_new:
856 * @obj: the v4l2 object owning the pool
858 * Construct a new buffer pool.
860 * Returns: the new pool, use gst_object_unref() to free resources
863 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
865 GstV4l2BufferPool *pool;
868 fd = v4l2_dup (obj->video_fd);
872 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
876 gst_buffer_pool_config_set (GST_BUFFER_POOL_CAST (pool)->config, caps,
877 obj->sizeimage, 2, 0, 0, 0);
879 return GST_BUFFER_POOL (pool);
884 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
890 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
893 GstV4l2Object *obj = pool->obj;
898 toread = obj->sizeimage;
900 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
902 gst_buffer_map (buf, &map, GST_MAP_WRITE);
905 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
908 amount = v4l2_read (obj->video_fd, map.data, toread);
910 if (amount == toread) {
912 } else if (amount == -1) {
913 if (errno == EAGAIN || errno == EINTR) {
918 /* short reads can happen if a signal interrupts the read */
923 GST_LOG_OBJECT (pool, "read %d bytes", amount);
924 gst_buffer_unmap (buf, &map);
925 gst_buffer_resize (buf, 0, amount);
932 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
937 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
938 (_("Error reading %d bytes from device '%s'."),
939 toread, obj->videodev), GST_ERROR_SYSTEM);
940 res = GST_FLOW_ERROR;
945 gst_buffer_unmap (buf, &map);
946 gst_buffer_resize (buf, 0, 0);
952 * gst_v4l2_buffer_pool_process:
953 * @bpool: a #GstBufferPool
956 * Process @buf in @bpool. For capture devices, this functions fills @buf with
957 * data from the device. For output devices, this functions send the contents of
958 * @buf to the device for playback.
960 * Returns: %GST_FLOW_OK on success.
963 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
965 GstFlowReturn ret = GST_FLOW_OK;
966 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
967 GstV4l2Object *obj = pool->obj;
969 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
972 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
976 /* capture into the buffer */
977 ret = gst_v4l2_do_read (pool, buf);
980 case GST_V4L2_IO_MMAP:
984 if (buf->pool == bpool)
985 /* nothing, data was inside the buffer when we did _acquire() */
988 /* buffer not from our pool, grab a frame and copy it into the target */
989 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
992 if (!gst_v4l2_object_copy (obj, buf, tmp))
995 /* an queue the buffer again after the copy */
996 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
1001 case GST_V4L2_IO_USERPTR:
1003 g_assert_not_reached ();
1008 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1010 switch (obj->mode) {
1011 case GST_V4L2_IO_RW:
1012 /* FIXME, do write() */
1013 GST_WARNING_OBJECT (pool, "implement write()");
1016 case GST_V4L2_IO_MMAP:
1018 GstBuffer *to_queue;
1020 if (buf->pool == bpool) {
1021 /* nothing, we can queue directly */
1023 GST_LOG_OBJECT (pool, "processing buffer from our pool");
1025 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1026 if (!gst_buffer_pool_is_active (bpool)) {
1027 GstStructure *config;
1029 /* this pool was not activated, configure and activate */
1030 GST_DEBUG_OBJECT (pool, "activating pool");
1032 config = gst_buffer_pool_get_config (bpool);
1033 gst_buffer_pool_config_add_option (config,
1034 GST_BUFFER_POOL_OPTION_VIDEO_META);
1035 gst_buffer_pool_set_config (bpool, config);
1037 if (!gst_buffer_pool_set_active (bpool, TRUE))
1038 goto activate_failed;
1041 /* this can block if all buffers are outstanding which would be
1042 * strange because we would expect the upstream element to have
1043 * allocated them and returned to us.. */
1044 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
1046 if (ret != GST_FLOW_OK)
1047 goto acquire_failed;
1049 /* copy into it and queue */
1050 if (!gst_v4l2_object_copy (obj, to_queue, buf))
1054 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1057 /* if we are not streaming yet (this is the first buffer, start
1059 if (!pool->streaming)
1060 if (!start_streaming (pool))
1063 if (pool->num_queued == pool->num_allocated) {
1064 /* all buffers are queued, try to dequeue one and release it back
1065 * into the pool so that _acquire can get to it again. */
1066 ret = gst_v4l2_buffer_pool_dqbuf (pool, &to_queue);
1067 if (ret != GST_FLOW_OK)
1070 /* release the rendered buffer back into the pool. This wakes up any
1071 * thread waiting for a buffer in _acquire() */
1072 gst_v4l2_buffer_pool_release_buffer (bpool, to_queue);
1077 case GST_V4L2_IO_USERPTR:
1079 g_assert_not_reached ();
1084 g_assert_not_reached ();
1093 GST_ERROR_OBJECT (obj->element, "failed to activate pool");
1094 return GST_FLOW_ERROR;
1098 GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
1099 gst_flow_get_name (ret));
1104 GST_ERROR_OBJECT (obj->element, "failed to copy data");
1105 return GST_FLOW_ERROR;
1109 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1110 return GST_FLOW_ERROR;