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"
35 #include <gstv4l2bufferpool.h>
36 #include "gstv4l2src.h"
37 #include "gstv4l2sink.h"
38 #include "v4l2_calls.h"
39 #include "gst/gst-i18n-plugin.h"
41 /* videodev2.h is not versioned and we can't easily check for the presence
42 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
43 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
44 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
45 #define V4L2_FIELD_INTERLACED_TB 8
46 #define V4L2_FIELD_INTERLACED_BT 9
50 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
51 #define GST_CAT_DEFAULT v4l2_debug
57 gst_meta_v4l2_get_info (void)
59 static const GstMetaInfo *meta_info = NULL;
61 if (meta_info == NULL) {
63 gst_meta_register ("GstMetaV4l2", "GstMetaV4l2",
64 sizeof (GstMetaV4l2), (GstMetaInitFunction) NULL,
65 (GstMetaFreeFunction) NULL, (GstMetaCopyFunction) NULL,
66 (GstMetaTransformFunction) NULL);
72 gst_v4l2_buffer_dispose (GstBuffer * buffer)
74 GstV4l2BufferPool *pool;
75 gboolean resuscitated = FALSE;
79 meta = GST_META_V4L2_GET (buffer);
80 g_assert (meta != NULL);
83 index = meta->vbuffer.index;
85 GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index);
87 GST_V4L2_BUFFER_POOL_LOCK (pool);
89 if (pool->requeuebuf) {
90 if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
91 GST_WARNING ("could not requeue buffer %p %d", buffer, index);
97 /* XXX double check this... I think it is ok to not synchronize this
98 * w.r.t. destruction of the pool, since the buffer is still live and
99 * the buffer holds a ref to the pool..
101 g_async_queue_push (pool->avail_buffers, buffer);
104 GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down");
108 GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index);
109 gst_buffer_ref (buffer);
110 pool->buffers[index] = buffer;
113 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
116 GST_LOG_OBJECT (pool->v4l2elem,
117 "buffer %p (data %p, len %u) not recovered, unmapping",
118 buffer, meta->mem, meta->vbuffer.length);
119 v4l2_munmap (meta->mem, meta->vbuffer.length);
121 g_object_unref (pool);
126 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index)
131 ret = gst_buffer_new ();
132 GST_MINI_OBJECT_CAST (ret)->dispose =
133 (GstMiniObjectDisposeFunction) gst_v4l2_buffer_dispose;
135 meta = GST_META_V4L2_ADD (ret);
137 GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
140 meta->pool = (GstV4l2BufferPool *) g_object_ref (pool);
142 meta->vbuffer.index = index;
143 meta->vbuffer.type = pool->type;
144 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
146 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
147 goto querybuf_failed;
149 GST_LOG_OBJECT (pool->v4l2elem, " index: %u", meta->vbuffer.index);
150 GST_LOG_OBJECT (pool->v4l2elem, " type: %d", meta->vbuffer.type);
151 GST_LOG_OBJECT (pool->v4l2elem, " bytesused: %u", meta->vbuffer.bytesused);
152 GST_LOG_OBJECT (pool->v4l2elem, " flags: %08x", meta->vbuffer.flags);
153 GST_LOG_OBJECT (pool->v4l2elem, " field: %d", meta->vbuffer.field);
154 GST_LOG_OBJECT (pool->v4l2elem, " memory: %d", meta->vbuffer.memory);
155 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
156 GST_LOG_OBJECT (pool->v4l2elem, " MMAP offset: %u",
157 meta->vbuffer.m.offset);
158 GST_LOG_OBJECT (pool->v4l2elem, " length: %u", meta->vbuffer.length);
159 GST_LOG_OBJECT (pool->v4l2elem, " input: %u", meta->vbuffer.input);
161 meta->mem = v4l2_mmap (0, meta->vbuffer.length,
162 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
163 meta->vbuffer.m.offset);
164 if (meta->mem == MAP_FAILED)
167 gst_buffer_take_memory (ret, -1,
168 gst_memory_new_wrapped (0,
169 meta->mem, NULL, meta->vbuffer.length, 0, meta->vbuffer.length));
176 gint errnosave = errno;
178 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
179 gst_buffer_unref (ret);
185 gint errnosave = errno;
187 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
188 gst_buffer_unref (ret);
197 #define gst_v4l2_buffer_pool_parent_class parent_class
198 G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, G_TYPE_OBJECT);
201 gst_v4l2_buffer_pool_finalize (GObject * object)
203 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
205 g_mutex_free (pool->lock);
208 g_async_queue_unref (pool->avail_buffers);
209 pool->avail_buffers = NULL;
211 if (pool->video_fd >= 0)
212 v4l2_close (pool->video_fd);
215 g_free (pool->buffers);
216 pool->buffers = NULL;
219 G_OBJECT_CLASS (parent_class)->finalize (object);
223 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
225 pool->lock = g_mutex_new ();
226 pool->running = FALSE;
227 pool->num_live_buffers = 0;
231 gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
233 GObjectClass *object_class = G_OBJECT_CLASS (klass);
235 object_class->finalize = gst_v4l2_buffer_pool_finalize;
238 /* this is somewhat of a hack.. but better to keep the hack in
239 * one place than copy/pasting it around..
241 static GstV4l2Object *
242 get_v4l2_object (GstElement * v4l2elem)
244 GstV4l2Object *v4l2object = NULL;
245 if (GST_IS_V4L2SRC (v4l2elem)) {
246 v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
247 } else if (GST_IS_V4L2SINK (v4l2elem)) {
248 v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
250 GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
256 * gst_v4l2_buffer_pool_new:
257 * @v4l2elem: the v4l2 element (src or sink) that owns this pool
258 * @fd: the video device file descriptor
259 * @num_buffers: the requested number of buffers in the pool
260 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
261 * buffer with no remaining references is immediately passed back to v4l2
262 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
263 * (which can be accessed via gst_v4l2_buffer_pool_get().
265 * Construct a new buffer pool.
267 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
270 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
271 gboolean requeuebuf, enum v4l2_buf_type type)
273 GstV4l2BufferPool *pool;
275 struct v4l2_requestbuffers breq;
277 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
279 pool->video_fd = v4l2_dup (fd);
280 if (pool->video_fd < 0)
283 /* first, lets request buffers, and see how many we can get: */
284 GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
287 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
289 breq.count = num_buffers;
290 breq.memory = V4L2_MEMORY_MMAP;
292 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
295 GST_LOG_OBJECT (v4l2elem, " count: %u", breq.count);
296 GST_LOG_OBJECT (v4l2elem, " type: %d", breq.type);
297 GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
299 if (breq.count < GST_V4L2_MIN_BUFFERS)
302 if (num_buffers != breq.count) {
303 GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
304 num_buffers = breq.count;
307 pool->v4l2elem = v4l2elem;
308 pool->requeuebuf = requeuebuf;
310 pool->buffer_count = num_buffers;
311 pool->buffers = g_new0 (GstBuffer *, num_buffers);
312 pool->avail_buffers = g_async_queue_new ();
314 /* now, map the buffers: */
315 for (n = 0; n < num_buffers; n++) {
316 pool->buffers[n] = gst_v4l2_buffer_new (pool, n);
317 if (!pool->buffers[n])
318 goto buffer_new_failed;
319 pool->num_live_buffers++;
320 g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
328 gint errnosave = errno;
330 g_object_unref (pool);
338 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
339 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
340 (_("Could not get buffers from device '%s'."),
341 v4l2object->videodev),
342 ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
347 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
348 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
349 (_("Could not get enough buffers from device '%s'."),
350 v4l2object->videodev),
351 ("we received %d from device '%s', we want at least %d",
352 breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
357 gint errnosave = errno;
359 gst_v4l2_buffer_pool_destroy (pool);
368 * gst_v4l2_buffer_pool_destroy:
371 * Free all resources in the pool and the pool itself.
374 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
378 GST_V4L2_BUFFER_POOL_LOCK (pool);
379 pool->running = FALSE;
380 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
382 GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
384 /* after this point, no more buffers will be queued or dequeued; no buffer
385 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
386 * is not NULL will be pushed out. */
388 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
389 * the pool, we need to unref the buffer to properly finalize te pool */
390 for (n = 0; n < pool->buffer_count; n++) {
393 GST_V4L2_BUFFER_POOL_LOCK (pool);
394 buf = pool->buffers[n];
395 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
398 /* we own the ref if the buffer is in pool->buffers; drop it. */
399 gst_buffer_unref (buf);
402 g_object_unref (pool);
406 * gst_v4l2_buffer_pool_get:
407 * @pool: the "this" object
408 * @blocking: should this call suspend until there is a buffer available
409 * in the buffer pool?
411 * Get an available buffer in the pool
414 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
419 buf = g_async_queue_pop (pool->avail_buffers);
421 buf = g_async_queue_try_pop (pool->avail_buffers);
425 GstMetaV4l2 *meta = GST_META_V4L2_GET (buf);
427 GST_V4L2_BUFFER_POOL_LOCK (pool);
428 gst_buffer_resize (buf, 0, meta->vbuffer.length);
429 GST_BUFFER_FLAG_UNSET (buf, 0xffffffff);
430 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
433 pool->running = TRUE;
440 * gst_v4l2_buffer_pool_qbuf:
442 * @buf: the buffer to queue
444 * Queue a buffer to the driver
446 * Returns: %TRUE for success
449 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
453 meta = GST_META_V4L2_GET (buf);
455 GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d",
456 meta->vbuffer.index);
458 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
461 pool->num_live_buffers--;
462 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
463 pool->num_live_buffers);
470 GST_WARNING_OBJECT (pool->v4l2elem, "could not queue a buffer");
476 * gst_v4l2_buffer_pool_dqbuf:
479 * Dequeue a buffer from the driver. Some generic error handling is done in
480 * this function, but any error handling specific to v4l2src (capture) or
481 * v4l2sink (output) can be done outside this function by checking 'errno'
486 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
488 GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
489 GstBuffer *pool_buffer;
490 struct v4l2_buffer buffer;
492 memset (&buffer, 0x00, sizeof (buffer));
493 buffer.type = pool->type;
494 buffer.memory = V4L2_MEMORY_MMAP;
496 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) < 0)
499 GST_V4L2_BUFFER_POOL_LOCK (pool);
501 /* get our GstBuffer with that index from the pool, if the buffer was
502 * outstanding we have a serious problem.
504 pool_buffer = pool->buffers[buffer.index];
506 if (pool_buffer == NULL)
509 GST_LOG_OBJECT (pool->v4l2elem,
510 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
511 buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
514 pool->num_live_buffers++;
515 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
516 pool->num_live_buffers);
518 /* set top/bottom field first if v4l2_buffer has the information */
519 if (buffer.field == V4L2_FIELD_INTERLACED_TB)
520 GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF);
521 if (buffer.field == V4L2_FIELD_INTERLACED_BT)
522 GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF);
524 /* this can change at every frame, esp. with jpeg */
525 gst_buffer_resize (pool_buffer, 0, buffer.bytesused);
527 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
534 GST_WARNING_OBJECT (pool->v4l2elem,
535 "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
536 buffer.sequence, buffer.index,
537 GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
541 GST_WARNING_OBJECT (pool->v4l2elem,
542 "Non-blocking I/O has been selected using O_NONBLOCK and"
543 " no buffer was in the outgoing queue. device %s",
544 v4l2object->videodev);
547 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
548 (_("Failed trying to get video frames from device '%s'."),
549 v4l2object->videodev),
550 (_("The buffer type is not supported, or the index is out of bounds," " or no buffers have been allocated yet, or the userptr" " or length are invalid. device %s"), v4l2object->videodev));
553 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
554 (_("Failed trying to get video frames from device '%s'. Not enough memory."), v4l2object->videodev), (_("insufficient memory to enqueue a user pointer buffer. device %s."), v4l2object->videodev));
557 GST_INFO_OBJECT (pool->v4l2elem,
558 "VIDIOC_DQBUF failed due to an internal error."
559 " Can also indicate temporary problems like signal loss."
560 " Note the driver might dequeue an (empty) buffer despite"
561 " returning an error, or even stop capturing."
562 " device %s", v4l2object->videodev);
563 /* have we de-queued a buffer ? */
564 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
565 GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
566 /* FIXME ... should we do something here? */
570 GST_WARNING_OBJECT (pool->v4l2elem,
571 "could not sync on a buffer on device %s", v4l2object->videodev);
574 GST_WARNING_OBJECT (pool->v4l2elem,
575 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
576 v4l2object->videodev, errno, g_strerror (errno));
583 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
584 (_("Failed trying to get video frames from device '%s'."),
585 v4l2object->videodev),
586 (_("No free buffers found in the pool at index %d."), buffer.index));
587 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
593 * gst_v4l2_buffer_pool_available_buffers:
596 * Check the number of buffers available to the driver, ie. buffers that
597 * have been QBUF'd but not yet DQBUF'd.
599 * Returns: the number of buffers available.
602 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
604 return pool->buffer_count - pool->num_live_buffers;