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
60 gst_v4l2_buffer_dispose (GstBuffer * buffer)
62 GstV4l2BufferPool *pool;
63 gboolean resuscitated = FALSE;
67 meta = GST_META_V4L2_GET (buffer, FALSE);
68 g_assert (meta != NULL);
71 index = meta->vbuffer.index;
73 GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index);
75 GST_V4L2_BUFFER_POOL_LOCK (pool);
77 if (pool->requeuebuf) {
78 if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
79 GST_WARNING ("could not requeue buffer %p %d", buffer, index);
85 /* XXX double check this... I think it is ok to not synchronize this
86 * w.r.t. destruction of the pool, since the buffer is still live and
87 * the buffer holds a ref to the pool..
89 g_async_queue_push (pool->avail_buffers, buffer);
92 GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down");
96 /* FIXME: check that the caps didn't change */
97 GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index);
98 gst_buffer_ref (buffer);
99 GST_BUFFER_SIZE (buffer) = 0;
100 pool->buffers[index] = buffer;
103 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
106 GST_LOG_OBJECT (pool->v4l2elem,
107 "buffer %p (data %p, len %u) not recovered, unmapping",
108 buffer, GST_BUFFER_DATA (buffer), meta->vbuffer.length);
109 v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), meta->vbuffer.length);
111 g_object_unref (pool);
116 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index, GstCaps * caps)
122 ret = gst_buffer_new ();
123 GST_MINI_OBJECT_CAST (ret)->dispose =
124 (GstMiniObjectDisposeFunction) gst_v4l2_buffer_dispose;
126 meta = GST_META_V4L2_GET (ret, TRUE);
128 GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
131 meta->pool = (GstV4l2BufferPool *) g_object_ref (pool);
133 meta->vbuffer.index = index;
134 meta->vbuffer.type = pool->type;
135 meta->vbuffer.memory = V4L2_MEMORY_MMAP;
137 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
138 goto querybuf_failed;
140 GST_LOG_OBJECT (pool->v4l2elem, " index: %u", meta->vbuffer.index);
141 GST_LOG_OBJECT (pool->v4l2elem, " type: %d", meta->vbuffer.type);
142 GST_LOG_OBJECT (pool->v4l2elem, " bytesused: %u", meta->vbuffer.bytesused);
143 GST_LOG_OBJECT (pool->v4l2elem, " flags: %08x", meta->vbuffer.flags);
144 GST_LOG_OBJECT (pool->v4l2elem, " field: %d", meta->vbuffer.field);
145 GST_LOG_OBJECT (pool->v4l2elem, " memory: %d", meta->vbuffer.memory);
146 if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
147 GST_LOG_OBJECT (pool->v4l2elem, " MMAP offset: %u",
148 meta->vbuffer.m.offset);
149 GST_LOG_OBJECT (pool->v4l2elem, " length: %u", meta->vbuffer.length);
150 GST_LOG_OBJECT (pool->v4l2elem, " input: %u", meta->vbuffer.input);
152 mem = (guint8 *) v4l2_mmap (0, meta->vbuffer.length,
153 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
154 meta->vbuffer.m.offset);
156 if (mem == MAP_FAILED)
159 GST_BUFFER_DATA (ret) = mem;
160 GST_BUFFER_SIZE (ret) = meta->vbuffer.length;
162 GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
164 gst_buffer_set_caps (ret, caps);
171 gint errnosave = errno;
173 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
174 gst_buffer_unref (ret);
180 gint errnosave = errno;
182 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
183 gst_buffer_unref (ret);
194 static GObjectClass *buffer_pool_parent_class = NULL;
197 gst_v4l2_buffer_pool_finalize (GObject * object)
199 GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
201 g_mutex_free (pool->lock);
204 g_async_queue_unref (pool->avail_buffers);
205 pool->avail_buffers = NULL;
207 if (pool->video_fd >= 0)
208 v4l2_close (pool->video_fd);
211 g_free (pool->buffers);
212 pool->buffers = NULL;
215 buffer_pool_parent_class->finalize (object);
219 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool, gpointer g_class)
221 pool->lock = g_mutex_new ();
222 pool->running = FALSE;
223 pool->num_live_buffers = 0;
227 gst_v4l2_buffer_pool_class_init (gpointer g_class, gpointer class_data)
229 GObjectClass *object_class = G_OBJECT_CLASS (g_class);
231 buffer_pool_parent_class = g_type_class_peek_parent (g_class);
233 object_class->finalize = gst_v4l2_buffer_pool_finalize;
237 gst_v4l2_buffer_pool_get_type (void)
239 static GType _gst_v4l2_buffer_pool_type;
241 if (G_UNLIKELY (_gst_v4l2_buffer_pool_type == 0)) {
242 static const GTypeInfo v4l2_buffer_pool_info = {
243 sizeof (GObjectClass),
246 gst_v4l2_buffer_pool_class_init,
249 sizeof (GstV4l2BufferPool),
251 (GInstanceInitFunc) gst_v4l2_buffer_pool_init,
254 _gst_v4l2_buffer_pool_type = g_type_register_static (G_TYPE_OBJECT,
255 "GstV4l2BufferPool", &v4l2_buffer_pool_info, 0);
257 return _gst_v4l2_buffer_pool_type;
261 /* this is somewhat of a hack.. but better to keep the hack in
262 * one place than copy/pasting it around..
264 static GstV4l2Object *
265 get_v4l2_object (GstElement * v4l2elem)
267 GstV4l2Object *v4l2object = NULL;
268 if (GST_IS_V4L2SRC (v4l2elem)) {
269 v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
270 #ifdef HAVE_EXPERIMENTAL
271 } else if (GST_IS_V4L2SINK (v4l2elem)) {
272 v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
275 GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
283 * gst_v4l2_buffer_pool_new:
284 * @v4l2elem: the v4l2 element (src or sink) that owns this pool
285 * @fd: the video device file descriptor
286 * @num_buffers: the requested number of buffers in the pool
287 * @caps: the caps to set on the buffer
288 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
289 * buffer with no remaining references is immediately passed back to v4l2
290 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
291 * (which can be accessed via gst_v4l2_buffer_pool_get().
293 * Construct a new buffer pool.
295 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
298 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
299 GstCaps * caps, gboolean requeuebuf, enum v4l2_buf_type type)
301 GstV4l2BufferPool *pool;
303 struct v4l2_requestbuffers breq;
305 pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
307 pool->video_fd = v4l2_dup (fd);
308 if (pool->video_fd < 0)
312 /* first, lets request buffers, and see how many we can get: */
313 GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
316 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
318 breq.count = num_buffers;
319 breq.memory = V4L2_MEMORY_MMAP;
321 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
324 GST_LOG_OBJECT (v4l2elem, " count: %u", breq.count);
325 GST_LOG_OBJECT (v4l2elem, " type: %d", breq.type);
326 GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
328 if (breq.count < GST_V4L2_MIN_BUFFERS)
331 if (num_buffers != breq.count) {
332 GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
333 num_buffers = breq.count;
336 pool->v4l2elem = v4l2elem;
337 pool->requeuebuf = requeuebuf;
339 pool->buffer_count = num_buffers;
340 pool->buffers = g_new0 (GstBuffer *, num_buffers);
341 pool->avail_buffers = g_async_queue_new ();
343 /* now, map the buffers: */
344 for (n = 0; n < num_buffers; n++) {
345 pool->buffers[n] = gst_v4l2_buffer_new (pool, n, caps);
346 if (!pool->buffers[n])
347 goto buffer_new_failed;
348 pool->num_live_buffers++;
349 g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
357 gint errnosave = errno;
359 g_object_unref (pool);
367 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
368 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
369 (_("Could not get buffers from device '%s'."),
370 v4l2object->videodev),
371 ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
376 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
377 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
378 (_("Could not get enough buffers from device '%s'."),
379 v4l2object->videodev),
380 ("we received %d from device '%s', we want at least %d",
381 breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
386 gint errnosave = errno;
388 gst_v4l2_buffer_pool_destroy (pool);
397 * gst_v4l2_buffer_pool_destroy:
400 * Free all resources in the pool and the pool itself.
403 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
407 GST_V4L2_BUFFER_POOL_LOCK (pool);
408 pool->running = FALSE;
409 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
411 GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
413 /* after this point, no more buffers will be queued or dequeued; no buffer
414 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
415 * is not NULL will be pushed out. */
417 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
418 * the pool, we need to unref the buffer to properly finalize te pool */
419 for (n = 0; n < pool->buffer_count; n++) {
422 GST_V4L2_BUFFER_POOL_LOCK (pool);
423 buf = GST_BUFFER (pool->buffers[n]);
424 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
427 /* we own the ref if the buffer is in pool->buffers; drop it. */
428 gst_buffer_unref (buf);
431 g_object_unref (pool);
435 * gst_v4l2_buffer_pool_get:
436 * @pool: the "this" object
437 * @blocking: should this call suspend until there is a buffer available
438 * in the buffer pool?
440 * Get an available buffer in the pool
443 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
448 buf = g_async_queue_pop (pool->avail_buffers);
450 buf = g_async_queue_try_pop (pool->avail_buffers);
454 GstMetaV4l2 *meta = GST_META_V4L2_GET (buf, FALSE);
456 GST_V4L2_BUFFER_POOL_LOCK (pool);
457 GST_BUFFER_SIZE (buf) = meta->vbuffer.length;
458 GST_BUFFER_FLAG_UNSET (buf, 0xffffffff);
459 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
462 pool->running = TRUE;
469 * gst_v4l2_buffer_pool_qbuf:
471 * @buf: the buffer to queue
473 * Queue a buffer to the driver
475 * Returns: %TRUE for success
478 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
482 meta = GST_META_V4L2_GET (buf, FALSE);
484 GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d",
485 meta->vbuffer.index);
487 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
490 pool->num_live_buffers--;
491 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
492 pool->num_live_buffers);
498 * gst_v4l2_buffer_pool_dqbuf:
501 * Dequeue a buffer from the driver. Some generic error handling is done in
502 * this function, but any error handling specific to v4l2src (capture) or
503 * v4l2sink (output) can be done outside this function by checking 'errno'
508 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
510 GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
511 GstBuffer *pool_buffer;
512 struct v4l2_buffer buffer;
514 memset (&buffer, 0x00, sizeof (buffer));
515 buffer.type = pool->type;
516 buffer.memory = V4L2_MEMORY_MMAP;
518 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) {
520 GST_V4L2_BUFFER_POOL_LOCK (pool);
522 /* get our GstBuffer with that index from the pool, if the buffer was
523 * outstanding we have a serious problem.
525 pool_buffer = pool->buffers[buffer.index];
527 if (pool_buffer == NULL) {
528 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
529 (_("Failed trying to get video frames from device '%s'."),
530 v4l2object->videodev),
531 (_("No free buffers found in the pool at index %d."), buffer.index));
532 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
536 GST_LOG_OBJECT (pool->v4l2elem,
537 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
538 buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
541 pool->num_live_buffers++;
542 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
543 pool->num_live_buffers);
545 /* set top/bottom field first if v4l2_buffer has the information */
546 if (buffer.field == V4L2_FIELD_INTERLACED_TB)
547 GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF);
548 if (buffer.field == V4L2_FIELD_INTERLACED_BT)
549 GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF);
551 /* this can change at every frame, esp. with jpeg */
552 GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
554 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
560 GST_WARNING_OBJECT (pool->v4l2elem,
561 "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
562 buffer.sequence, buffer.index,
563 GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
567 GST_WARNING_OBJECT (pool->v4l2elem,
568 "Non-blocking I/O has been selected using O_NONBLOCK and"
569 " no buffer was in the outgoing queue. device %s",
570 v4l2object->videodev);
573 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
574 (_("Failed trying to get video frames from device '%s'."),
575 v4l2object->videodev),
576 (_("The buffer type is not supported, or the index is out of bounds,"
577 " or no buffers have been allocated yet, or the userptr"
578 " or length are invalid. device %s"), v4l2object->videodev));
581 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
582 (_("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));
585 GST_INFO_OBJECT (pool->v4l2elem,
586 "VIDIOC_DQBUF failed due to an internal error."
587 " Can also indicate temporary problems like signal loss."
588 " Note the driver might dequeue an (empty) buffer despite"
589 " returning an error, or even stop capturing."
590 " device %s", v4l2object->videodev);
591 /* have we de-queued a buffer ? */
592 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
593 GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
594 /* FIXME ... should we do something here? */
598 GST_WARNING_OBJECT (pool->v4l2elem,
599 "could not sync on a buffer on device %s", v4l2object->videodev);
602 GST_WARNING_OBJECT (pool->v4l2elem,
603 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
604 v4l2object->videodev, errno, g_strerror (errno));
612 * gst_v4l2_buffer_pool_available_buffers:
615 * Check the number of buffers available to the driver, ie. buffers that
616 * have been QBUF'd but not yet DQBUF'd.
618 * Returns: the number of buffers available.
621 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
623 return pool->buffer_count - pool->num_live_buffers;