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 "v4l2_calls.h"
44 #include "gst/gst-i18n-plugin.h"
45 #include <gst/glib-compat-private.h>
47 /* videodev2.h is not versioned and we can't easily check for the presence
48 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
49 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
50 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
51 #define V4L2_FIELD_INTERLACED_TB 8
52 #define V4L2_FIELD_INTERLACED_BT 9
56 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
57 #define GST_CAT_DEFAULT v4l2_debug
63 gst_v4l2_meta_api_get_type (void)
65 static volatile GType type;
66 static const gchar *tags[] = { "memory", NULL };
68 if (g_once_init_enter (&type)) {
69 GType _type = gst_meta_api_type_register ("GstV4l2MetaAPI", tags);
70 g_once_init_leave (&type, _type);
76 gst_v4l2_meta_get_info (void)
78 static const GstMetaInfo *meta_info = NULL;
80 if (g_once_init_enter (&meta_info)) {
81 const GstMetaInfo *meta =
82 gst_meta_register (gst_v4l2_meta_api_get_type (), "GstV4l2Meta",
83 sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
84 (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
85 g_once_init_leave (&meta_info, meta);
93 #define gst_v4l2_buffer_pool_parent_class parent_class
94 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
96 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
100 gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
102 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
109 case GST_V4L2_IO_DMABUF:
111 case GST_V4L2_IO_MMAP:
116 meta = GST_V4L2_META_GET (buffer);
117 g_assert (meta != NULL);
119 index = meta->vbuffer.index;
120 GST_LOG_OBJECT (pool,
121 "unmap buffer %p idx %d (data %p, len %u)", buffer,
122 index, meta->mem, meta->vbuffer.length);
124 v4l2_munmap (meta->mem, meta->vbuffer.length);
125 pool->buffers[index] = NULL;
128 case GST_V4L2_IO_USERPTR:
130 g_assert_not_reached ();
133 gst_buffer_unref (buffer);
137 gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
138 GstBufferPoolAcquireParams * params)
140 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
154 gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
157 case GST_V4L2_IO_MMAP:
158 case GST_V4L2_IO_DMABUF:
160 #ifdef VIDIOC_CREATE_BUFS
161 if (pool->num_allocated == pool->num_buffers) {
162 struct v4l2_create_buffers create_bufs;
164 memset (&create_bufs, 0, sizeof (struct v4l2_create_buffers));
165 create_bufs.count = 1;
166 create_bufs.memory = V4L2_MEMORY_MMAP;
167 create_bufs.format.type = obj->type;
169 if (v4l2_ioctl (pool->video_fd, VIDIOC_G_FMT, &create_bufs.format) < 0)
172 if (v4l2_ioctl (pool->video_fd, VIDIOC_CREATE_BUFS, &create_bufs) < 0)
173 goto create_bufs_failed;
175 GST_LOG_OBJECT (pool, "created buffer with index: %u",
178 pool->buffers = g_renew (GstBuffer *, pool->buffers, pool->num_buffers);
179 pool->buffers[pool->num_buffers - 1] = NULL;
182 newbuf = gst_buffer_new ();
183 meta = GST_V4L2_META_ADD (newbuf);
185 index = pool->num_allocated;
187 GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf);
189 meta->vbuffer.index = index;
190 meta->vbuffer.type = obj->type;
191 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
193 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
194 goto querybuf_failed;
196 GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
197 GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
198 GST_LOG_OBJECT (pool, " bytesused: %u", meta->vbuffer.bytesused);
199 GST_LOG_OBJECT (pool, " flags: %08x", meta->vbuffer.flags);
200 GST_LOG_OBJECT (pool, " field: %d", meta->vbuffer.field);
201 GST_LOG_OBJECT (pool, " memory: %d", meta->vbuffer.memory);
202 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
203 GST_LOG_OBJECT (pool, " MMAP offset: %u", meta->vbuffer.m.offset);
205 if (obj->mode == GST_V4L2_IO_MMAP) {
206 meta->mem = v4l2_mmap (0, meta->vbuffer.length,
207 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
208 meta->vbuffer.m.offset);
209 if (meta->mem == MAP_FAILED)
212 gst_buffer_append_memory (newbuf,
213 gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
214 meta->mem, meta->vbuffer.length, 0, meta->vbuffer.length, NULL,
217 #if HAVE_DECL_V4L2_MEMORY_DMABUF
218 if (obj->mode == GST_V4L2_IO_DMABUF) {
219 struct v4l2_exportbuffer expbuf;
221 memset (&expbuf, 0, sizeof (struct v4l2_exportbuffer));
222 expbuf.type = meta->vbuffer.type;
223 expbuf.index = meta->vbuffer.index;
224 expbuf.flags = O_CLOEXEC;
225 if (v4l2_ioctl (pool->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
228 meta->vbuffer.memory = V4L2_MEMORY_DMABUF;
229 gst_buffer_append_memory (newbuf,
230 gst_dmabuf_allocator_alloc (pool->allocator, expbuf.fd,
231 meta->vbuffer.length));
234 /* add metadata to raw video buffers */
235 if (pool->add_videometa && info->finfo) {
236 const GstVideoFormatInfo *finfo = info->finfo;
237 gsize offset[GST_VIDEO_MAX_PLANES];
238 gint width, height, n_planes, offs, i, stride[GST_VIDEO_MAX_PLANES];
240 width = GST_VIDEO_INFO_WIDTH (info);
241 height = GST_VIDEO_INFO_HEIGHT (info);
242 n_planes = GST_VIDEO_INFO_N_PLANES (info);
244 GST_DEBUG_OBJECT (pool, "adding video meta, bytesperline %d",
248 for (i = 0; i < n_planes; i++) {
251 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, obj->bytesperline);
254 stride[i] * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, height);
256 gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
257 GST_VIDEO_INFO_FORMAT (info), width, height, n_planes,
262 case GST_V4L2_IO_USERPTR:
265 g_assert_not_reached ();
268 pool->num_allocated++;
275 #ifdef VIDIOC_CREATE_BUFS
278 gint errnosave = errno;
280 GST_WARNING ("Failed G_FMT: %s", g_strerror (errnosave));
282 return GST_FLOW_ERROR;
286 gint errnosave = errno;
288 GST_WARNING ("Failed CREATE_BUFS: %s", g_strerror (errnosave));
290 return GST_FLOW_ERROR;
295 gint errnosave = errno;
297 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
298 gst_buffer_unref (newbuf);
300 return GST_FLOW_ERROR;
304 gint errnosave = errno;
306 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
307 gst_buffer_unref (newbuf);
309 return GST_FLOW_ERROR;
311 #if HAVE_DECL_V4L2_MEMORY_DMABUF
314 gint errnosave = errno;
316 GST_WARNING ("Failed EXPBUF: %s", g_strerror (errnosave));
317 gst_buffer_unref (newbuf);
319 return GST_FLOW_ERROR;
325 gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
327 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
328 GstV4l2Object *obj = pool->obj;
330 guint size, min_buffers, max_buffers, num_buffers, copy_threshold;
331 GstAllocator *allocator;
332 GstAllocationParams params;
333 struct v4l2_requestbuffers breq;
335 GST_DEBUG_OBJECT (pool, "set config");
337 pool->add_videometa =
338 gst_buffer_pool_config_has_option (config,
339 GST_BUFFER_POOL_OPTION_VIDEO_META);
341 if (!pool->add_videometa &&
342 GST_VIDEO_INFO_FORMAT (&obj->info) != GST_VIDEO_FORMAT_ENCODED) {
345 /* we don't have video metadata, and we are not dealing with raw video,
346 * see if the strides are compatible */
347 stride = GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0);
349 GST_DEBUG_OBJECT (pool, "no videometadata, checking strides %d and %u",
350 stride, obj->bytesperline);
352 if (stride != obj->bytesperline)
353 goto missing_video_api;
356 /* parse the config and keep around */
357 if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
361 if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
364 GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
368 /* we preallocate 1 buffer, this value also instructs the latency
369 * calculation to have 1 frame latency max */
373 case GST_V4L2_IO_DMABUF:
374 case GST_V4L2_IO_MMAP:
376 /* request a reasonable number of buffers when no max specified. We will
377 * copy when we run out of buffers */
378 if (max_buffers == 0)
381 num_buffers = max_buffers;
383 /* first, lets request buffers, and see how many we can get: */
384 GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
387 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
388 breq.type = obj->type;
389 breq.count = num_buffers;
390 breq.memory = V4L2_MEMORY_MMAP;
392 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
395 GST_LOG_OBJECT (pool, " count: %u", breq.count);
396 GST_LOG_OBJECT (pool, " type: %d", breq.type);
397 GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
399 if (breq.count < GST_V4L2_MIN_BUFFERS)
402 if (num_buffers != breq.count) {
403 GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
404 num_buffers = breq.count;
406 /* update min buffers with the amount of buffers we just reserved. We need
407 * to configure this value in the bufferpool so that the default start
408 * implementation calls our allocate function */
409 min_buffers = breq.count;
411 if (max_buffers == 0 || num_buffers < max_buffers) {
412 /* if we are asked to provide more buffers than we have allocated, start
413 * copying buffers when we only have 2 buffers left in the pool */
416 /* we are certain that we have enough buffers so we don't need to
422 case GST_V4L2_IO_USERPTR:
426 g_assert_not_reached ();
431 pool->num_buffers = num_buffers;
432 pool->copy_threshold = copy_threshold;
434 if (obj->mode == GST_V4L2_IO_DMABUF)
435 allocator = gst_dmabuf_allocator_obtain ();
438 gst_object_unref (pool->allocator);
439 if ((pool->allocator = allocator))
440 gst_object_ref (allocator);
441 pool->params = params;
443 gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
446 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
451 GST_ERROR_OBJECT (pool, "missing GstMetaVideo API in config, "
452 "default stride: %d, wanted stride %u",
453 GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0), obj->bytesperline);
458 GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
463 GST_ERROR_OBJECT (pool,
464 "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
469 GST_ERROR_OBJECT (pool,
470 "we received %d from device '%s', we want at least %d",
471 breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
477 start_streaming (GstV4l2BufferPool * pool)
479 GstV4l2Object *obj = pool->obj;
484 case GST_V4L2_IO_MMAP:
485 case GST_V4L2_IO_USERPTR:
486 case GST_V4L2_IO_DMABUF:
487 GST_DEBUG_OBJECT (pool, "STREAMON");
488 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
492 g_assert_not_reached ();
496 pool->streaming = TRUE;
503 GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
510 gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
512 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
513 GstV4l2Object *obj = pool->obj;
516 pool->buffers = g_new0 (GstBuffer *, pool->num_buffers);
517 pool->num_allocated = 0;
519 /* now, allocate the buffers: */
520 if (!GST_BUFFER_POOL_CLASS (parent_class)->start (bpool))
523 /* we can start capturing now, we wait for the playback case until we queued
524 * the first buffer */
525 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
526 if (!start_streaming (pool))
529 gst_poll_set_flushing (obj->poll, FALSE);
536 GST_ERROR_OBJECT (pool, "failed to start streaming");
542 gst_v4l2_buffer_pool_free_buffers (GstV4l2BufferPool * pool)
544 if (pool->num_buffers > 0) {
545 struct v4l2_requestbuffers breq;
546 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
547 breq.type = pool->obj->type;
549 breq.memory = V4L2_MEMORY_MMAP;
550 if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0) {
551 GST_ERROR_OBJECT (pool, "error releasing buffers: %s",
554 pool->num_buffers = 0;
559 gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
562 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
563 GstV4l2Object *obj = pool->obj;
566 GST_DEBUG_OBJECT (pool, "stopping pool");
568 gst_poll_set_flushing (obj->poll, TRUE);
570 if (pool->streaming) {
574 case GST_V4L2_IO_MMAP:
575 case GST_V4L2_IO_USERPTR:
576 case GST_V4L2_IO_DMABUF:
577 /* we actually need to sync on all queued buffers but not
578 * on the non-queued ones */
579 GST_DEBUG_OBJECT (pool, "STREAMOFF");
580 if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
584 g_assert_not_reached ();
587 pool->streaming = FALSE;
590 /* first free the buffers in the queue */
591 ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
593 /* then free the remaining buffers */
594 for (n = 0; n < pool->num_buffers; n++) {
595 if (pool->buffers[n])
596 gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
598 pool->num_queued = 0;
599 g_free (pool->buffers);
600 pool->buffers = NULL;
602 gst_v4l2_buffer_pool_free_buffers (pool);
609 GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
616 gst_v4l2_object_poll (GstV4l2Object * v4l2object)
620 if (v4l2object->can_poll_device) {
621 GST_LOG_OBJECT (v4l2object->element, "polling device");
622 ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
623 if (G_UNLIKELY (ret < 0)) {
626 if (errno == ENXIO) {
627 GST_WARNING_OBJECT (v4l2object->element,
628 "v4l2 device doesn't support polling. Disabling");
629 v4l2object->can_poll_device = FALSE;
631 if (errno != EAGAIN && errno != EINTR)
641 GST_DEBUG ("stop called");
642 return GST_FLOW_FLUSHING;
646 GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
647 ("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
648 return GST_FLOW_ERROR;
653 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
658 meta = GST_V4L2_META_GET (buf);
660 GST_LOG_OBJECT (pool, "unref copied buffer %p", buf);
661 /* no meta, it was a copied buffer that we can unref */
662 gst_buffer_unref (buf);
666 index = meta->vbuffer.index;
667 meta->vbuffer.bytesused = gst_buffer_get_size (buf);
669 GST_LOG_OBJECT (pool,
670 "enqueue buffer %p, index:%d, queued:%d, flags:%08x used:%d",
671 buf, index, pool->num_queued, meta->vbuffer.flags,
672 meta->vbuffer.bytesused);
674 if (pool->buffers[index] != NULL)
677 GST_LOG_OBJECT (pool, "doing QBUF");
678 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
681 pool->buffers[index] = buf;
689 GST_WARNING_OBJECT (pool, "the buffer was already queued");
690 return GST_FLOW_ERROR;
694 GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
696 return GST_FLOW_ERROR;
701 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
705 struct v4l2_buffer vbuffer;
706 GstV4l2Object *obj = pool->obj;
707 GstClockTime timestamp;
709 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
712 memset (&vbuffer, 0x00, sizeof (vbuffer));
713 vbuffer.type = obj->type;
714 #if HAVE_DECL_V4L2_MEMORY_DMABUF
715 if (obj->mode == GST_V4L2_IO_DMABUF)
716 vbuffer.memory = V4L2_MEMORY_DMABUF;
719 vbuffer.memory = V4L2_MEMORY_MMAP;
721 GST_LOG_OBJECT (pool, "doing DQBUF");
722 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
725 /* get our GstBuffer with that index from the pool, if the buffer was
726 * outstanding we have a serious problem.
728 outbuf = pool->buffers[vbuffer.index];
732 /* mark the buffer outstanding */
733 pool->buffers[vbuffer.index] = NULL;
736 timestamp = GST_TIMEVAL_TO_TIME (vbuffer.timestamp);
738 GST_LOG_OBJECT (pool,
739 "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, ts %"
740 GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf, vbuffer.sequence,
741 vbuffer.index, vbuffer.bytesused, vbuffer.flags,
742 GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
744 /* set top/bottom field first if v4l2_buffer has the information */
745 if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
746 GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
748 if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
749 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
752 /* this can change at every frame, esp. with jpeg */
753 if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
754 gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
756 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
765 GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
770 GST_WARNING_OBJECT (pool,
771 "problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
772 vbuffer.sequence, vbuffer.index,
773 GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
777 GST_WARNING_OBJECT (pool,
778 "Non-blocking I/O has been selected using O_NONBLOCK and"
779 " no buffer was in the outgoing queue. device %s", obj->videodev);
782 GST_ERROR_OBJECT (pool,
783 "The buffer type is not supported, or the index is out of bounds, "
784 "or no buffers have been allocated yet, or the userptr "
785 "or length are invalid. device %s", obj->videodev);
788 GST_ERROR_OBJECT (pool,
789 "insufficient memory to enqueue a user pointer buffer");
792 GST_INFO_OBJECT (pool,
793 "VIDIOC_DQBUF failed due to an internal error."
794 " Can also indicate temporary problems like signal loss."
795 " Note the driver might dequeue an (empty) buffer despite"
796 " returning an error, or even stop capturing."
797 " device %s", obj->videodev);
798 /* have we de-queued a buffer ? */
799 if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
800 GST_DEBUG_OBJECT (pool, "reenqueing buffer");
801 /* FIXME ... should we do something here? */
805 GST_WARNING_OBJECT (pool,
806 "could not sync on a buffer on device %s", obj->videodev);
809 GST_WARNING_OBJECT (pool,
810 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
811 obj->videodev, errno, g_strerror (errno));
814 return GST_FLOW_ERROR;
818 GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
820 return GST_FLOW_ERROR;
825 gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
826 GstBufferPoolAcquireParams * params)
829 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
830 GstV4l2Object *obj = pool->obj;
832 GST_DEBUG_OBJECT (pool, "acquire");
834 if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
838 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
839 /* capture, This function should return a buffer with new captured data */
842 /* take empty buffer from the pool */
843 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
846 case GST_V4L2_IO_DMABUF:
847 case GST_V4L2_IO_MMAP:
848 /* just dequeue a buffer, we basically use the queue of v4l2 as the
849 * storage for our buffers. This function does poll first so we can
850 * interrupt it fine. */
851 ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
852 if (G_UNLIKELY (ret != GST_FLOW_OK))
855 /* start copying buffers when we are running low on buffers */
856 if (pool->num_queued < pool->copy_threshold) {
858 #ifdef VIDIOC_CREATE_BUFS
859 if (pool->can_alloc) {
860 if (GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
861 ©, params) == GST_FLOW_OK) {
862 gst_v4l2_buffer_pool_release_buffer (bpool, copy);
865 pool->can_alloc = FALSE;
870 /* copy the memory */
871 copy = gst_buffer_copy (*buffer);
872 GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
874 /* and requeue so that we can continue capturing */
875 ret = gst_v4l2_buffer_pool_qbuf (pool, *buffer);
880 case GST_V4L2_IO_USERPTR:
882 ret = GST_FLOW_ERROR;
883 g_assert_not_reached ();
888 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
889 /* playback, This function should return an empty buffer */
892 /* get an empty buffer */
893 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
897 case GST_V4L2_IO_MMAP:
898 /* get a free unqueued buffer */
899 ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
903 case GST_V4L2_IO_USERPTR:
905 ret = GST_FLOW_ERROR;
906 g_assert_not_reached ();
912 ret = GST_FLOW_ERROR;
913 g_assert_not_reached ();
922 GST_DEBUG_OBJECT (pool, "We are flushing");
923 return GST_FLOW_FLUSHING;
928 gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
930 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
931 GstV4l2Object *obj = pool->obj;
933 GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
936 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
937 /* capture, put the buffer back in the queue so that we can refill it
941 /* release back in the pool */
942 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
945 case GST_V4L2_IO_DMABUF:
946 case GST_V4L2_IO_MMAP:
947 /* queue back in the device */
948 gst_v4l2_buffer_pool_qbuf (pool, buffer);
951 case GST_V4L2_IO_USERPTR:
953 g_assert_not_reached ();
958 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
961 /* release back in the pool */
962 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
965 case GST_V4L2_IO_MMAP:
970 meta = GST_V4L2_META_GET (buffer);
971 g_assert (meta != NULL);
973 index = meta->vbuffer.index;
975 if (pool->buffers[index] == NULL) {
976 GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
979 /* reset to the full length, in case it was changed */
980 gst_buffer_resize (buffer, 0, meta->vbuffer.length);
982 /* playback, put the buffer back in the queue to refill later. */
983 GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
986 /* the buffer is queued in the device but maybe not played yet. We just
987 * leave it there and not make it available for future calls to acquire
988 * for now. The buffer will be dequeued and reused later. */
989 GST_LOG_OBJECT (pool, "buffer %u is queued", index);
994 case GST_V4L2_IO_USERPTR:
996 g_assert_not_reached ();
1002 g_assert_not_reached ();
1008 gst_v4l2_buffer_pool_finalize (GObject * object)
1010 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
1012 gst_v4l2_buffer_pool_free_buffers (pool);
1014 if (pool->video_fd >= 0)
1015 v4l2_close (pool->video_fd);
1016 if (pool->allocator)
1017 gst_object_unref (pool->allocator);
1018 g_free (pool->buffers);
1020 gst_object_unref (pool->obj->element);
1022 G_OBJECT_CLASS (parent_class)->finalize (object);
1026 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
1031 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
1033 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1034 GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
1036 object_class->finalize = gst_v4l2_buffer_pool_finalize;
1038 bufferpool_class->start = gst_v4l2_buffer_pool_start;
1039 bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
1040 bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
1041 bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
1042 bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
1043 bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
1044 bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
1048 * gst_v4l2_buffer_pool_new:
1049 * @obj: the v4l2 object owning the pool
1051 * Construct a new buffer pool.
1053 * Returns: the new pool, use gst_object_unref() to free resources
1056 gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
1058 GstV4l2BufferPool *pool;
1062 fd = v4l2_dup (obj->video_fd);
1066 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
1067 pool->video_fd = fd;
1069 pool->can_alloc = TRUE;
1071 s = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
1072 gst_buffer_pool_config_set_params (s, caps, obj->sizeimage, 2, 0);
1073 gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), s);
1075 gst_object_ref (obj->element);
1077 return GST_BUFFER_POOL (pool);
1082 GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
1087 static GstFlowReturn
1088 gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
1091 GstV4l2Object *obj = pool->obj;
1096 toread = obj->sizeimage;
1098 GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
1100 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1103 if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
1106 amount = v4l2_read (obj->video_fd, map.data, toread);
1108 if (amount == toread) {
1110 } else if (amount == -1) {
1111 if (errno == EAGAIN || errno == EINTR) {
1116 /* short reads can happen if a signal interrupts the read */
1121 GST_LOG_OBJECT (pool, "read %d bytes", amount);
1122 gst_buffer_unmap (buf, &map);
1123 gst_buffer_resize (buf, 0, amount);
1130 GST_DEBUG ("poll error %s", gst_flow_get_name (res));
1135 GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
1136 (_("Error reading %d bytes from device '%s'."),
1137 toread, obj->videodev), GST_ERROR_SYSTEM);
1138 res = GST_FLOW_ERROR;
1143 gst_buffer_unmap (buf, &map);
1144 gst_buffer_resize (buf, 0, 0);
1150 * gst_v4l2_buffer_pool_process:
1151 * @bpool: a #GstBufferPool
1152 * @buf: a #GstBuffer
1154 * Process @buf in @bpool. For capture devices, this functions fills @buf with
1155 * data from the device. For output devices, this functions send the contents of
1156 * @buf to the device for playback.
1158 * Returns: %GST_FLOW_OK on success.
1161 gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
1163 GstFlowReturn ret = GST_FLOW_OK;
1164 GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
1165 GstV4l2Object *obj = pool->obj;
1167 GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
1169 switch (obj->type) {
1170 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1172 switch (obj->mode) {
1173 case GST_V4L2_IO_RW:
1174 /* capture into the buffer */
1175 ret = gst_v4l2_do_read (pool, buf);
1178 case GST_V4L2_IO_MMAP:
1182 if (buf->pool == bpool)
1183 /* nothing, data was inside the buffer when we did _acquire() */
1186 /* buffer not from our pool, grab a frame and copy it into the target */
1187 if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
1190 if (!gst_v4l2_object_copy (obj, buf, tmp))
1193 /* an queue the buffer again after the copy */
1194 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
1199 case GST_V4L2_IO_USERPTR:
1201 g_assert_not_reached ();
1206 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1208 switch (obj->mode) {
1209 case GST_V4L2_IO_RW:
1210 /* FIXME, do write() */
1211 GST_WARNING_OBJECT (pool, "implement write()");
1213 case GST_V4L2_IO_DMABUF:
1214 case GST_V4L2_IO_MMAP:
1216 GstBuffer *to_queue;
1218 if (buf->pool == bpool) {
1219 /* nothing, we can queue directly */
1220 to_queue = gst_buffer_ref (buf);
1221 GST_LOG_OBJECT (pool, "processing buffer from our pool");
1223 GST_LOG_OBJECT (pool, "alloc buffer from our pool");
1224 if (!gst_buffer_pool_is_active (bpool)) {
1225 GstStructure *config;
1227 /* this pool was not activated, configure and activate */
1228 GST_DEBUG_OBJECT (pool, "activating pool");
1230 config = gst_buffer_pool_get_config (bpool);
1231 gst_buffer_pool_config_add_option (config,
1232 GST_BUFFER_POOL_OPTION_VIDEO_META);
1233 gst_buffer_pool_set_config (bpool, config);
1235 if (!gst_buffer_pool_set_active (bpool, TRUE))
1236 goto activate_failed;
1239 /* this can block if all buffers are outstanding which would be
1240 * strange because we would expect the upstream element to have
1241 * allocated them and returned to us.. */
1242 ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, NULL);
1243 if (ret != GST_FLOW_OK)
1244 goto acquire_failed;
1246 /* copy into it and queue */
1247 if (!gst_v4l2_object_copy (obj, to_queue, buf))
1251 if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
1254 /* if we are not streaming yet (this is the first buffer, start
1256 if (!pool->streaming)
1257 if (!start_streaming (pool))
1260 if (pool->num_queued == pool->num_allocated) {
1262 /* all buffers are queued, try to dequeue one and release it back
1263 * into the pool so that _acquire can get to it again. */
1264 ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
1265 if (ret != GST_FLOW_OK) {
1266 gst_buffer_unref (to_queue);
1270 /* release the rendered buffer back into the pool. This wakes up any
1271 * thread waiting for a buffer in _acquire(). If the buffer still has
1272 * a pool then this will happen when the refcount reaches 0 */
1274 gst_v4l2_buffer_pool_release_buffer (bpool, out);
1276 gst_buffer_unref (to_queue);
1280 case GST_V4L2_IO_USERPTR:
1282 g_assert_not_reached ();
1287 g_assert_not_reached ();
1296 GST_ERROR_OBJECT (obj->element, "failed to activate pool");
1297 return GST_FLOW_ERROR;
1301 GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
1302 gst_flow_get_name (ret));
1307 GST_ERROR_OBJECT (obj->element, "failed to copy data");
1308 return GST_FLOW_ERROR;
1312 GST_ERROR_OBJECT (obj->element, "failed to start streaming");
1313 return GST_FLOW_ERROR;