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 #ifdef HAVE_EXPERIMENTAL
38 #include "gstv4l2sink.h"
40 #include "v4l2_calls.h"
41 #include "gst/gst-i18n-plugin.h"
43 /* videodev2.h is not versioned and we can't easily check for the presence
44 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
45 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
46 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
47 #define V4L2_FIELD_INTERLACED_TB 8
48 #define V4L2_FIELD_INTERLACED_BT 9
52 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
53 #define GST_CAT_DEFAULT v4l2_debug
59 gst_meta_v4l2_get_info (void)
61 static const GstMetaInfo *meta_info = NULL;
63 if (meta_info == NULL) {
65 gst_meta_register ("GstMetaV4l2", "GstMetaV4l2",
66 sizeof (GstMetaV4l2), (GstMetaInitFunction) NULL,
67 (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL,
68 (GstMetaSerializeFunction) NULL, (GstMetaDeserializeFunction) NULL);
74 gst_v4l2_buffer_dispose (GstBuffer * buffer)
76 GstV4l2BufferPool *pool;
77 gboolean resuscitated = FALSE;
81 meta = GST_META_V4L2_GET (buffer);
82 g_assert (meta != NULL);
85 index = meta->vbuffer.index;
87 GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index);
89 GST_V4L2_BUFFER_POOL_LOCK (pool);
91 if (pool->requeuebuf) {
92 if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
93 GST_WARNING ("could not requeue buffer %p %d", buffer, index);
99 /* XXX double check this... I think it is ok to not synchronize this
100 * w.r.t. destruction of the pool, since the buffer is still live and
101 * the buffer holds a ref to the pool..
103 g_async_queue_push (pool->avail_buffers, buffer);
106 GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down");
110 /* FIXME: check that the caps didn't change */
111 GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index);
112 gst_buffer_ref (buffer);
113 GST_BUFFER_SIZE (buffer) = 0;
114 pool->buffers[index] = buffer;
117 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
120 GST_LOG_OBJECT (pool->v4l2elem,
121 "buffer %p (data %p, len %u) not recovered, unmapping",
122 buffer, GST_BUFFER_DATA (buffer), meta->vbuffer.length);
123 v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), meta->vbuffer.length);
125 g_object_unref (pool);
130 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index, GstCaps * caps)
136 ret = gst_buffer_new ();
137 GST_MINI_OBJECT_CAST (ret)->dispose =
138 (GstMiniObjectDisposeFunction) gst_v4l2_buffer_dispose;
140 meta = GST_META_V4L2_ADD (ret);
142 GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
145 meta->pool = (GstV4l2BufferPool *) g_object_ref (pool);
147 meta->vbuffer.index = index;
148 meta->vbuffer.type = pool->type;
149 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
151 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
152 goto querybuf_failed;
154 GST_LOG_OBJECT (pool->v4l2elem, " index: %u", meta->vbuffer.index);
155 GST_LOG_OBJECT (pool->v4l2elem, " type: %d", meta->vbuffer.type);
156 GST_LOG_OBJECT (pool->v4l2elem, " bytesused: %u", meta->vbuffer.bytesused);
157 GST_LOG_OBJECT (pool->v4l2elem, " flags: %08x", meta->vbuffer.flags);
158 GST_LOG_OBJECT (pool->v4l2elem, " field: %d", meta->vbuffer.field);
159 GST_LOG_OBJECT (pool->v4l2elem, " memory: %d", meta->vbuffer.memory);
160 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
161 GST_LOG_OBJECT (pool->v4l2elem, " MMAP offset: %u",
162 meta->vbuffer.m.offset);
163 GST_LOG_OBJECT (pool->v4l2elem, " length: %u", meta->vbuffer.length);
164 GST_LOG_OBJECT (pool->v4l2elem, " input: %u", meta->vbuffer.input);
166 mem = (guint8 *) v4l2_mmap (0, meta->vbuffer.length,
167 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
168 meta->vbuffer.m.offset);
170 if (mem == MAP_FAILED)
173 GST_BUFFER_DATA (ret) = mem;
174 GST_BUFFER_SIZE (ret) = meta->vbuffer.length;
176 GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
178 gst_buffer_set_caps (ret, caps);
185 gint errnosave = errno;
187 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
188 gst_buffer_unref (ret);
194 gint errnosave = errno;
196 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
197 gst_buffer_unref (ret);
208 static GObjectClass *buffer_pool_parent_class = NULL;
211 gst_v4l2_buffer_pool_finalize (GObject * object)
213 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
215 g_mutex_free (pool->lock);
218 g_async_queue_unref (pool->avail_buffers);
219 pool->avail_buffers = NULL;
221 if (pool->video_fd >= 0)
222 v4l2_close (pool->video_fd);
225 g_free (pool->buffers);
226 pool->buffers = NULL;
229 buffer_pool_parent_class->finalize (object);
233 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool, gpointer g_class)
235 pool->lock = g_mutex_new ();
236 pool->running = FALSE;
237 pool->num_live_buffers = 0;
241 gst_v4l2_buffer_pool_class_init (gpointer g_class, gpointer class_data)
243 GObjectClass *object_class = G_OBJECT_CLASS (g_class);
245 buffer_pool_parent_class = g_type_class_peek_parent (g_class);
247 object_class->finalize = gst_v4l2_buffer_pool_finalize;
251 gst_v4l2_buffer_pool_get_type (void)
253 static GType _gst_v4l2_buffer_pool_type;
255 if (G_UNLIKELY (_gst_v4l2_buffer_pool_type == 0)) {
256 static const GTypeInfo v4l2_buffer_pool_info = {
257 sizeof (GObjectClass),
260 gst_v4l2_buffer_pool_class_init,
263 sizeof (GstV4l2BufferPool),
265 (GInstanceInitFunc) gst_v4l2_buffer_pool_init,
268 _gst_v4l2_buffer_pool_type = g_type_register_static (G_TYPE_OBJECT,
269 "GstV4l2BufferPool", &v4l2_buffer_pool_info, 0);
271 return _gst_v4l2_buffer_pool_type;
275 /* this is somewhat of a hack.. but better to keep the hack in
276 * one place than copy/pasting it around..
278 static GstV4l2Object *
279 get_v4l2_object (GstElement * v4l2elem)
281 GstV4l2Object *v4l2object = NULL;
282 if (GST_IS_V4L2SRC (v4l2elem)) {
283 v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
284 #ifdef HAVE_EXPERIMENTAL
285 } else if (GST_IS_V4L2SINK (v4l2elem)) {
286 v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
289 GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
297 * gst_v4l2_buffer_pool_new:
298 * @v4l2elem: the v4l2 element (src or sink) that owns this pool
299 * @fd: the video device file descriptor
300 * @num_buffers: the requested number of buffers in the pool
301 * @caps: the caps to set on the buffer
302 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
303 * buffer with no remaining references is immediately passed back to v4l2
304 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
305 * (which can be accessed via gst_v4l2_buffer_pool_get().
307 * Construct a new buffer pool.
309 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
312 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
313 GstCaps * caps, gboolean requeuebuf, enum v4l2_buf_type type)
315 GstV4l2BufferPool *pool;
317 struct v4l2_requestbuffers breq;
319 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
321 pool->video_fd = v4l2_dup (fd);
322 if (pool->video_fd < 0)
326 /* first, lets request buffers, and see how many we can get: */
327 GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
330 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
332 breq.count = num_buffers;
333 breq.memory = V4L2_MEMORY_MMAP;
335 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
338 GST_LOG_OBJECT (v4l2elem, " count: %u", breq.count);
339 GST_LOG_OBJECT (v4l2elem, " type: %d", breq.type);
340 GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
342 if (breq.count < GST_V4L2_MIN_BUFFERS)
345 if (num_buffers != breq.count) {
346 GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
347 num_buffers = breq.count;
350 pool->v4l2elem = v4l2elem;
351 pool->requeuebuf = requeuebuf;
353 pool->buffer_count = num_buffers;
354 pool->buffers = g_new0 (GstBuffer *, num_buffers);
355 pool->avail_buffers = g_async_queue_new ();
357 /* now, map the buffers: */
358 for (n = 0; n < num_buffers; n++) {
359 pool->buffers[n] = gst_v4l2_buffer_new (pool, n, caps);
360 if (!pool->buffers[n])
361 goto buffer_new_failed;
362 pool->num_live_buffers++;
363 g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
371 gint errnosave = errno;
373 g_object_unref (pool);
381 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
382 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
383 (_("Could not get buffers from device '%s'."),
384 v4l2object->videodev),
385 ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
390 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
391 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
392 (_("Could not get enough buffers from device '%s'."),
393 v4l2object->videodev),
394 ("we received %d from device '%s', we want at least %d",
395 breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
400 gint errnosave = errno;
402 gst_v4l2_buffer_pool_destroy (pool);
411 * gst_v4l2_buffer_pool_destroy:
414 * Free all resources in the pool and the pool itself.
417 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
421 GST_V4L2_BUFFER_POOL_LOCK (pool);
422 pool->running = FALSE;
423 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
425 GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
427 /* after this point, no more buffers will be queued or dequeued; no buffer
428 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
429 * is not NULL will be pushed out. */
431 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
432 * the pool, we need to unref the buffer to properly finalize te pool */
433 for (n = 0; n < pool->buffer_count; n++) {
436 GST_V4L2_BUFFER_POOL_LOCK (pool);
437 buf = GST_BUFFER (pool->buffers[n]);
438 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
441 /* we own the ref if the buffer is in pool->buffers; drop it. */
442 gst_buffer_unref (buf);
445 g_object_unref (pool);
449 * gst_v4l2_buffer_pool_get:
450 * @pool: the "this" object
451 * @blocking: should this call suspend until there is a buffer available
452 * in the buffer pool?
454 * Get an available buffer in the pool
457 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
462 buf = g_async_queue_pop (pool->avail_buffers);
464 buf = g_async_queue_try_pop (pool->avail_buffers);
468 GstMetaV4l2 *meta = GST_META_V4L2_GET (buf);
470 GST_V4L2_BUFFER_POOL_LOCK (pool);
471 GST_BUFFER_SIZE (buf) = meta->vbuffer.length;
472 GST_BUFFER_FLAG_UNSET (buf, 0xffffffff);
473 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
476 pool->running = TRUE;
483 * gst_v4l2_buffer_pool_qbuf:
485 * @buf: the buffer to queue
487 * Queue a buffer to the driver
489 * Returns: %TRUE for success
492 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
496 meta = GST_META_V4L2_GET (buf);
498 GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d",
499 meta->vbuffer.index);
501 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
504 pool->num_live_buffers--;
505 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
506 pool->num_live_buffers);
512 * gst_v4l2_buffer_pool_dqbuf:
515 * Dequeue a buffer from the driver. Some generic error handling is done in
516 * this function, but any error handling specific to v4l2src (capture) or
517 * v4l2sink (output) can be done outside this function by checking 'errno'
522 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
524 GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
525 GstBuffer *pool_buffer;
526 struct v4l2_buffer buffer;
528 memset (&buffer, 0x00, sizeof (buffer));
529 buffer.type = pool->type;
530 buffer.memory = V4L2_MEMORY_MMAP;
532 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) {
534 GST_V4L2_BUFFER_POOL_LOCK (pool);
536 /* get our GstBuffer with that index from the pool, if the buffer was
537 * outstanding we have a serious problem.
539 pool_buffer = pool->buffers[buffer.index];
541 if (pool_buffer == NULL) {
542 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
543 (_("Failed trying to get video frames from device '%s'."),
544 v4l2object->videodev),
545 (_("No free buffers found in the pool at index %d."), buffer.index));
546 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
550 GST_LOG_OBJECT (pool->v4l2elem,
551 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
552 buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
555 pool->num_live_buffers++;
556 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
557 pool->num_live_buffers);
559 /* set top/bottom field first if v4l2_buffer has the information */
560 if (buffer.field == V4L2_FIELD_INTERLACED_TB)
561 GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF);
562 if (buffer.field == V4L2_FIELD_INTERLACED_BT)
563 GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF);
565 /* this can change at every frame, esp. with jpeg */
566 GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
568 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
574 GST_WARNING_OBJECT (pool->v4l2elem,
575 "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
576 buffer.sequence, buffer.index,
577 GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
581 GST_WARNING_OBJECT (pool->v4l2elem,
582 "Non-blocking I/O has been selected using O_NONBLOCK and"
583 " no buffer was in the outgoing queue. device %s",
584 v4l2object->videodev);
587 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
588 (_("Failed trying to get video frames from device '%s'."),
589 v4l2object->videodev),
590 (_("The buffer type is not supported, or the index is out of bounds,"
591 " or no buffers have been allocated yet, or the userptr"
592 " or length are invalid. device %s"), v4l2object->videodev));
595 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
596 (_("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));
599 GST_INFO_OBJECT (pool->v4l2elem,
600 "VIDIOC_DQBUF failed due to an internal error."
601 " Can also indicate temporary problems like signal loss."
602 " Note the driver might dequeue an (empty) buffer despite"
603 " returning an error, or even stop capturing."
604 " device %s", v4l2object->videodev);
605 /* have we de-queued a buffer ? */
606 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
607 GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
608 /* FIXME ... should we do something here? */
612 GST_WARNING_OBJECT (pool->v4l2elem,
613 "could not sync on a buffer on device %s", v4l2object->videodev);
616 GST_WARNING_OBJECT (pool->v4l2elem,
617 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
618 v4l2object->videodev, errno, g_strerror (errno));
626 * gst_v4l2_buffer_pool_available_buffers:
629 * Check the number of buffers available to the driver, ie. buffers that
630 * have been QBUF'd but not yet DQBUF'd.
632 * Returns: the number of buffers available.
635 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
637 return pool->buffer_count - pool->num_live_buffers;