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.
32 #if HAVE_DECL_V4L2_MEMORY_DMABUF
36 #include "gst/video/video.h"
37 #include "gst/video/gstvideometa.h"
38 #include "gst/video/gstvideopool.h"
39 #include "gst/allocators/gstdmabuf.h"
41 #include <gstv4l2bufferpool.h>
43 #include "gstv4l2src.h"
44 #include "gstv4l2sink.h"
45 #include "v4l2_calls.h"
46 #include "gst/gst-i18n-plugin.h"
47 #include <gst/glib-compat-private.h>
49 /* videodev2.h is not versioned and we can't easily check for the presence
50 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
51 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
52 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
53 #define V4L2_FIELD_INTERLACED_TB 8
54 #define V4L2_FIELD_INTERLACED_BT 9
58 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
59 #define GST_CAT_DEFAULT v4l2_debug
65 gst_v4l2_meta_api_get_type (void)
67 static volatile GType type;
68 static const gchar *tags[] = { "memory", NULL };
70 if (g_once_init_enter (&type)) {
71 GType _type = gst_meta_api_type_register ("GstV4l2MetaAPI", tags);
72 g_once_init_leave (&type, _type);
78 gst_v4l2_meta_get_info (void)
80 static const GstMetaInfo *meta_info = NULL;
82 if (g_once_init_enter (&meta_info)) {
83 const GstMetaInfo *meta =
84 gst_meta_register (gst_v4l2_meta_api_get_type (), "GstV4l2Meta",
85 sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
86 (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
87 g_once_init_leave (&meta_info, meta);
95 #define gst_v4l2_buffer_pool_parent_class parent_class
96 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
98 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
102 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
104 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
111 case GST_V4L2_IO_DMABUF:
113 case GST_V4L2_IO_MMAP:
118 meta = GST_V4L2_META_GET (buffer);
119 g_assert (meta != NULL);
121 index = meta->vbuffer.index;
122 GST_LOG_OBJECT (pool,
123 "unmap buffer %p idx %d (data %p, len %u)", buffer,
124 index, meta->mem, meta->vbuffer.length);
126 v4l2_munmap (meta->mem, meta->vbuffer.length);
127 pool->buffers[index] = NULL;
130 case GST_V4L2_IO_USERPTR:
132 g_assert_not_reached ();
135 gst_buffer_unref (buffer);
139 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
140 GstBufferPoolAcquireParams * params)
142 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
156 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
159 case GST_V4L2_IO_MMAP:
160 case GST_V4L2_IO_DMABUF:
162 newbuf = gst_buffer_new ();
163 meta = GST_V4L2_META_ADD (newbuf);
165 index = pool->num_allocated;
167 GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf);
169 meta->vbuffer.index = index;
170 meta->vbuffer.type = obj->type;
171 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
173 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
174 goto querybuf_failed;
176 GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
177 GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
178 GST_LOG_OBJECT (pool, " bytesused: %u", meta->vbuffer.bytesused);
179 GST_LOG_OBJECT (pool, " flags: %08x", meta->vbuffer.flags);
180 GST_LOG_OBJECT (pool, " field: %d", meta->vbuffer.field);
181 GST_LOG_OBJECT (pool, " memory: %d", meta->vbuffer.memory);
182 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
183 GST_LOG_OBJECT (pool, " MMAP offset: %u", meta->vbuffer.m.offset);
185 if (obj->mode == GST_V4L2_IO_MMAP) {
186 meta->mem = v4l2_mmap (0, meta->vbuffer.length,
187 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
188 meta->vbuffer.m.offset);
189 if (meta->mem == MAP_FAILED)
192 gst_buffer_append_memory (newbuf,
193 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
194 meta->mem, meta->vbuffer.length, 0, meta->vbuffer.length, NULL,
197 #if HAVE_DECL_V4L2_MEMORY_DMABUF
198 if (obj->mode == GST_V4L2_IO_DMABUF) {
199 struct v4l2_exportbuffer expbuf;
201 expbuf.type = meta->vbuffer.type;
202 expbuf.index = meta->vbuffer.index;
203 expbuf.flags = O_CLOEXEC;
204 if (v4l2_ioctl (pool->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
207 meta->vbuffer.memory = V4L2_MEMORY_DMABUF;
208 gst_buffer_append_memory (newbuf,
209 gst_dmabuf_allocator_alloc (pool->allocator, expbuf.fd,
210 meta->vbuffer.length));
213 /* add metadata to raw video buffers */
214 if (pool->add_videometa && info->finfo) {
215 const GstVideoFormatInfo *finfo = info->finfo;
216 gsize offset[GST_VIDEO_MAX_PLANES];
217 gint width, height, n_planes, offs, i, stride[GST_VIDEO_MAX_PLANES];
219 width = GST_VIDEO_INFO_WIDTH (info);
220 height = GST_VIDEO_INFO_HEIGHT (info);
221 n_planes = GST_VIDEO_INFO_N_PLANES (info);
223 GST_DEBUG_OBJECT (pool, "adding video meta, bytesperline %d",
227 for (i = 0; i < n_planes; i++) {
230 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, obj->bytesperline);
233 stride[i] * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, height);
235 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
236 GST_VIDEO_INFO_FORMAT (info), width, height, n_planes,
241 case GST_V4L2_IO_USERPTR:
244 g_assert_not_reached ();
247 pool->num_allocated++;
256 gint errnosave = errno;
258 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
259 gst_buffer_unref (newbuf);
261 return GST_FLOW_ERROR;
265 gint errnosave = errno;
267 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
268 gst_buffer_unref (newbuf);
270 return GST_FLOW_ERROR;
275 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
277 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
278 GstV4l2Object *obj = pool->obj;
280 guint size, min_buffers, max_buffers, num_buffers, copy_threshold;
281 GstAllocator *allocator;
282 GstAllocationParams params;
283 struct v4l2_requestbuffers breq;
285 GST_DEBUG_OBJECT (pool, "set config");
287 pool->add_videometa =
288 gst_buffer_pool_config_has_option (config,
289 GST_BUFFER_POOL_OPTION_VIDEO_META);
291 if (!pool->add_videometa &&
292 GST_VIDEO_INFO_FORMAT (&obj->info) != GST_VIDEO_FORMAT_ENCODED) {
295 /* we don't have video metadata, and we are not dealing with raw video,
296 * see if the strides are compatible */
297 stride = GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0);
299 GST_DEBUG_OBJECT (pool, "no videometadata, checking strides %d and %u",
300 stride, obj->bytesperline);
302 if (stride != obj->bytesperline)
303 goto missing_video_api;
306 /* parse the config and keep around */
307 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
311 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
314 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
318 /* we preallocate 1 buffer, this value also instructs the latency
319 * calculation to have 1 frame latency max */
323 case GST_V4L2_IO_DMABUF:
324 case GST_V4L2_IO_MMAP:
326 /* request a reasonable number of buffers when no max specified. We will
327 * copy when we run out of buffers */
328 if (max_buffers == 0)
331 num_buffers = max_buffers;
333 /* first, lets request buffers, and see how many we can get: */
334 GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
337 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
338 breq.type = obj->type;
339 breq.count = num_buffers;
340 breq.memory = V4L2_MEMORY_MMAP;
342 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
345 GST_LOG_OBJECT (pool, " count: %u", breq.count);
346 GST_LOG_OBJECT (pool, " type: %d", breq.type);
347 GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
349 if (breq.count < GST_V4L2_MIN_BUFFERS)
352 if (num_buffers != breq.count) {
353 GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
354 num_buffers = breq.count;
356 /* update min buffers with the amount of buffers we just reserved. We need
357 * to configure this value in the bufferpool so that the default start
358 * implementation calls our allocate function */
359 min_buffers = breq.count;
361 if (max_buffers == 0 || num_buffers < max_buffers) {
362 /* if we are asked to provide more buffers than we have allocated, start
363 * copying buffers when we only have 2 buffers left in the pool */
366 /* we are certain that we have enough buffers so we don't need to
372 case GST_V4L2_IO_USERPTR:
376 g_assert_not_reached ();
381 pool->num_buffers = num_buffers;
382 pool->copy_threshold = copy_threshold;
384 if (obj->mode == GST_V4L2_IO_DMABUF)
385 allocator = gst_dmabuf_allocator_obtain ();
388 gst_object_unref (pool->allocator);
389 if ((pool->allocator = allocator))
390 gst_object_ref (allocator);
391 pool->params = params;
393 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
396 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
401 GST_ERROR_OBJECT (pool, "missing GstMetaVideo API in config, "
402 "default stride: %d, wanted stride %u",
403 GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0), obj->bytesperline);
408 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
413 GST_ERROR_OBJECT (pool,
414 "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
419 GST_ERROR_OBJECT (pool,
420 "we received %d from device '%s', we want at least %d",
421 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
427 start_streaming (GstV4l2BufferPool * pool)
429 GstV4l2Object *obj = pool->obj;
434 case GST_V4L2_IO_MMAP:
435 case GST_V4L2_IO_USERPTR:
436 case GST_V4L2_IO_DMABUF:
437 GST_DEBUG_OBJECT (pool, "STREAMON");
438 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
442 g_assert_not_reached ();
446 pool->streaming = TRUE;
453 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
460 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
462 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
463 GstV4l2Object *obj = pool->obj;
466 pool->buffers = g_new0 (GstBuffer *, pool->num_buffers);
467 pool->num_allocated = 0;
469 /* now, allocate the buffers: */
470 if (!GST_BUFFER_POOL_CLASS (parent_class)->start (bpool))
473 /* we can start capturing now, we wait for the playback case until we queued
474 * the first buffer */
475 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
476 if (!start_streaming (pool))
479 gst_poll_set_flushing (obj->poll, FALSE);
486 GST_ERROR_OBJECT (pool, "failed to start streaming");
492 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
495 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
496 GstV4l2Object *obj = pool->obj;
499 GST_DEBUG_OBJECT (pool, "stopping pool");
501 gst_poll_set_flushing (obj->poll, TRUE);
503 if (pool->streaming) {
507 case GST_V4L2_IO_MMAP:
508 case GST_V4L2_IO_USERPTR:
509 case GST_V4L2_IO_DMABUF:
510 /* we actually need to sync on all queued buffers but not
511 * on the non-queued ones */
512 GST_DEBUG_OBJECT (pool, "STREAMOFF");
513 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
517 g_assert_not_reached ();
520 pool->streaming = FALSE;
523 /* first free the buffers in the queue */
524 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
526 /* then free the remaining buffers */
527 for (n = 0; n < pool->num_buffers; n++) {
528 if (pool->buffers[n])
529 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
531 pool->num_queued = 0;
532 g_free (pool->buffers);
533 pool->buffers = NULL;
540 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
547 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
551 if (v4l2object->can_poll_device) {
552 GST_LOG_OBJECT (v4l2object->element, "polling device");
553 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
554 if (G_UNLIKELY (ret < 0)) {
557 if (errno == ENXIO) {
558 GST_WARNING_OBJECT (v4l2object->element,
559 "v4l2 device doesn't support polling. Disabling");
560 v4l2object->can_poll_device = FALSE;
562 if (errno != EAGAIN && errno != EINTR)
572 GST_DEBUG ("stop called");
573 return GST_FLOW_FLUSHING;
577 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
578 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
579 return GST_FLOW_ERROR;
584 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
589 meta = GST_V4L2_META_GET (buf);
591 GST_LOG_OBJECT (pool, "unref copied buffer %p", buf);
592 /* no meta, it was a copied buffer that we can unref */
593 gst_buffer_unref (buf);
597 index = meta->vbuffer.index;
599 GST_LOG_OBJECT (pool, "enqueue buffer %p, index:%d, queued:%d, flags:%08x",
600 buf, index, pool->num_queued, meta->vbuffer.flags);
602 if (pool->buffers[index] != NULL)
605 GST_LOG_OBJECT (pool, "doing QBUF");
606 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
609 pool->buffers[index] = buf;
617 GST_WARNING_OBJECT (pool, "the buffer was already queued");
618 return GST_FLOW_ERROR;
622 GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
624 return GST_FLOW_ERROR;
629 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
633 struct v4l2_buffer vbuffer;
634 GstV4l2Object *obj = pool->obj;
635 GstClockTime timestamp;
637 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
638 /* select works for input devices when data is available. According to the
639 * specs we can also poll to find out when a frame has been displayed but
640 * that just seems to lock up here */
641 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
645 memset (&vbuffer, 0x00, sizeof (vbuffer));
646 vbuffer.type = obj->type;
647 #if HAVE_DECL_V4L2_MEMORY_DMABUF
648 if (obj->mode == GST_V4L2_IO_DMABUF)
649 vbuffer.memory = V4L2_MEMORY_DMABUF;
652 vbuffer.memory = V4L2_MEMORY_MMAP;
654 GST_LOG_OBJECT (pool, "doing DQBUF");
655 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
658 /* get our GstBuffer with that index from the pool, if the buffer was
659 * outstanding we have a serious problem.
661 outbuf = pool->buffers[vbuffer.index];
665 /* mark the buffer outstanding */
666 pool->buffers[vbuffer.index] = NULL;
669 timestamp = GST_TIMEVAL_TO_TIME (vbuffer.timestamp);
671 GST_LOG_OBJECT (pool,
672 "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, ts %"
673 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf, vbuffer.sequence,
674 vbuffer.index, vbuffer.bytesused, vbuffer.flags,
675 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
677 /* set top/bottom field first if v4l2_buffer has the information */
678 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
679 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
681 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
682 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
685 /* this can change at every frame, esp. with jpeg */
686 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
687 gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
689 gst_buffer_resize (outbuf, 0, vbuffer.length);
691 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
700 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
705 GST_WARNING_OBJECT (pool,
706 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
707 vbuffer.sequence, vbuffer.index,
708 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
712 GST_WARNING_OBJECT (pool,
713 "Non-blocking I/O has been selected using O_NONBLOCK and"
714 " no buffer was in the outgoing queue. device %s", obj->videodev);
717 GST_ERROR_OBJECT (pool,
718 "The buffer type is not supported, or the index is out of bounds, "
719 "or no buffers have been allocated yet, or the userptr "
720 "or length are invalid. device %s", obj->videodev);
723 GST_ERROR_OBJECT (pool,
724 "insufficient memory to enqueue a user pointer buffer");
727 GST_INFO_OBJECT (pool,
728 "VIDIOC_DQBUF failed due to an internal error."
729 " Can also indicate temporary problems like signal loss."
730 " Note the driver might dequeue an (empty) buffer despite"
731 " returning an error, or even stop capturing."
732 " device %s", obj->videodev);
733 /* have we de-queued a buffer ? */
734 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
735 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
736 /* FIXME ... should we do something here? */
740 GST_WARNING_OBJECT (pool,
741 "could not sync on a buffer on device %s", obj->videodev);
744 GST_WARNING_OBJECT (pool,
745 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
746 obj->videodev, errno, g_strerror (errno));
749 return GST_FLOW_ERROR;
753 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
755 return GST_FLOW_ERROR;
760 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
761 GstBufferPoolAcquireParams * params)
764 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
765 GstV4l2Object *obj = pool->obj;
767 GST_DEBUG_OBJECT (pool, "acquire");
769 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
773 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
774 /* capture, This function should return a buffer with new captured data */
777 /* take empty buffer from the pool */
778 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
781 case GST_V4L2_IO_DMABUF:
782 case GST_V4L2_IO_MMAP:
783 /* just dequeue a buffer, we basically use the queue of v4l2 as the
784 * storage for our buffers. This function does poll first so we can
785 * interrupt it fine. */
786 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
787 if (G_UNLIKELY (ret != GST_FLOW_OK))
790 /* start copying buffers when we are running low on buffers */
791 if (pool->num_queued < pool->copy_threshold) {
794 /* copy the memory */
795 copy = gst_buffer_copy (*buffer);
796 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
798 /* and requeue so that we can continue capturing */
799 ret = gst_v4l2_buffer_pool_qbuf (pool, *buffer);
804 case GST_V4L2_IO_USERPTR:
806 ret = GST_FLOW_ERROR;
807 g_assert_not_reached ();
812 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
813 /* playback, This function should return an empty buffer */
816 /* get an empty buffer */
817 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
821 case GST_V4L2_IO_MMAP:
822 /* get a free unqueued buffer */
823 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
827 case GST_V4L2_IO_USERPTR:
829 ret = GST_FLOW_ERROR;
830 g_assert_not_reached ();
836 ret = GST_FLOW_ERROR;
837 g_assert_not_reached ();
846 GST_DEBUG_OBJECT (pool, "We are flushing");
847 return GST_FLOW_FLUSHING;
852 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
854 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
855 GstV4l2Object *obj = pool->obj;
857 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
860 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
861 /* capture, put the buffer back in the queue so that we can refill it
865 /* release back in the pool */
866 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
869 case GST_V4L2_IO_DMABUF:
870 case GST_V4L2_IO_MMAP:
871 /* queue back in the device */
872 gst_v4l2_buffer_pool_qbuf (pool, buffer);
875 case GST_V4L2_IO_USERPTR:
877 g_assert_not_reached ();
882 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
885 /* release back in the pool */
886 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
889 case GST_V4L2_IO_MMAP:
894 meta = GST_V4L2_META_GET (buffer);
895 g_assert (meta != NULL);
897 index = meta->vbuffer.index;
899 if (pool->buffers[index] == NULL) {
900 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
902 /* playback, put the buffer back in the queue to refill later. */
903 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
906 /* the buffer is queued in the device but maybe not played yet. We just
907 * leave it there and not make it available for future calls to acquire
908 * for now. The buffer will be dequeued and reused later. */
909 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
914 case GST_V4L2_IO_USERPTR:
916 g_assert_not_reached ();
922 g_assert_not_reached ();
928 gst_v4l2_buffer_pool_finalize (GObject * object)
930 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
932 if (pool->video_fd >= 0)
933 v4l2_close (pool->video_fd);
935 gst_object_unref (pool->allocator);
936 g_free (pool->buffers);
938 G_OBJECT_CLASS (parent_class)->finalize (object);
942 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
947 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
949 GObjectClass *object_class = G_OBJECT_CLASS (klass);
950 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
952 object_class->finalize = gst_v4l2_buffer_pool_finalize;
954 bufferpool_class->start = gst_v4l2_buffer_pool_start;
955 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
956 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
957 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
958 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
959 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
960 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
964 * gst_v4l2_buffer_pool_new:
965 * @obj: the v4l2 object owning the pool
967 * Construct a new buffer pool.
969 * Returns: the new pool, use gst_object_unref() to free resources
972 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
974 GstV4l2BufferPool *pool;
978 fd = v4l2_dup (obj->video_fd);
982 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
986 s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
987 gst_buffer_pool_config_set_params (s, caps, obj->sizeimage, 2, 0);
988 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), s);
990 return GST_BUFFER_POOL (pool);
995 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1000 static GstFlowReturn
1001 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1004 GstV4l2Object *obj = pool->obj;
1009 toread = obj->sizeimage;
1011 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1013 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1016 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
1019 amount = v4l2_read (obj->video_fd, map.data, toread);
1021 if (amount == toread) {
1023 } else if (amount == -1) {
1024 if (errno == EAGAIN || errno == EINTR) {
1029 /* short reads can happen if a signal interrupts the read */
1034 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1035 gst_buffer_unmap (buf, &map);
1036 gst_buffer_resize (buf, 0, amount);
1043 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1048 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1049 (_("Error reading %d bytes from device '%s'."),
1050 toread, obj->videodev), GST_ERROR_SYSTEM);
1051 res = GST_FLOW_ERROR;
1056 gst_buffer_unmap (buf, &map);
1057 gst_buffer_resize (buf, 0, 0);
1063 * gst_v4l2_buffer_pool_process:
1064 * @bpool: a #GstBufferPool
1065 * @buf: a #GstBuffer
1067 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1068 * data from the device. For output devices, this functions send the contents of
1069 * @buf to the device for playback.
1071 * Returns: %GST_FLOW_OK on success.
1074 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
1076 GstFlowReturn ret = GST_FLOW_OK;
1077 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1078 GstV4l2Object *obj = pool->obj;
1080 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1082 switch (obj->type) {
1083 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1085 switch (obj->mode) {
1086 case GST_V4L2_IO_RW:
1087 /* capture into the buffer */
1088 ret = gst_v4l2_do_read (pool, buf);
1091 case GST_V4L2_IO_MMAP:
1095 if (buf->pool == bpool)
1096 /* nothing, data was inside the buffer when we did _acquire() */
1099 /* buffer not from our pool, grab a frame and copy it into the target */
1100 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1103 if (!gst_v4l2_object_copy (obj, buf, tmp))
1106 /* an queue the buffer again after the copy */
1107 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
1112 case GST_V4L2_IO_USERPTR:
1114 g_assert_not_reached ();
1119 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1121 switch (obj->mode) {
1122 case GST_V4L2_IO_RW:
1123 /* FIXME, do write() */
1124 GST_WARNING_OBJECT (pool, "implement write()");
1126 case GST_V4L2_IO_DMABUF:
1127 case GST_V4L2_IO_MMAP:
1129 GstBuffer *to_queue;
1131 if (buf->pool == bpool) {
1132 /* nothing, we can queue directly */
1134 GST_LOG_OBJECT (pool, "processing buffer from our pool");
1136 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1137 if (!gst_buffer_pool_is_active (bpool)) {
1138 GstStructure *config;
1140 /* this pool was not activated, configure and activate */
1141 GST_DEBUG_OBJECT (pool, "activating pool");
1143 config = gst_buffer_pool_get_config (bpool);
1144 gst_buffer_pool_config_add_option (config,
1145 GST_BUFFER_POOL_OPTION_VIDEO_META);
1146 gst_buffer_pool_set_config (bpool, config);
1148 if (!gst_buffer_pool_set_active (bpool, TRUE))
1149 goto activate_failed;
1152 /* this can block if all buffers are outstanding which would be
1153 * strange because we would expect the upstream element to have
1154 * allocated them and returned to us.. */
1155 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
1157 if (ret != GST_FLOW_OK)
1158 goto acquire_failed;
1160 /* copy into it and queue */
1161 if (!gst_v4l2_object_copy (obj, to_queue, buf))
1165 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1168 /* if we are not streaming yet (this is the first buffer, start
1170 if (!pool->streaming)
1171 if (!start_streaming (pool))
1174 if (pool->num_queued == pool->num_allocated) {
1175 /* all buffers are queued, try to dequeue one and release it back
1176 * into the pool so that _acquire can get to it again. */
1177 ret = gst_v4l2_buffer_pool_dqbuf (pool, &to_queue);
1178 if (ret != GST_FLOW_OK)
1181 /* release the rendered buffer back into the pool. This wakes up any
1182 * thread waiting for a buffer in _acquire() */
1183 gst_v4l2_buffer_pool_release_buffer (bpool, to_queue);
1188 case GST_V4L2_IO_USERPTR:
1190 g_assert_not_reached ();
1195 g_assert_not_reached ();
1204 GST_ERROR_OBJECT (obj->element, "failed to activate pool");
1205 return GST_FLOW_ERROR;
1209 GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
1210 gst_flow_get_name (ret));
1215 GST_ERROR_OBJECT (obj->element, "failed to copy data");
1216 return GST_FLOW_ERROR;
1220 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1221 return GST_FLOW_ERROR;