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"
42 #include <gst/glib-compat-private.h>
44 /* videodev2.h is not versioned and we can't easily check for the presence
45 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
46 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
47 #ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
48 #define V4L2_FIELD_INTERLACED_TB 8
49 #define V4L2_FIELD_INTERLACED_BT 9
53 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
54 #define GST_CAT_DEFAULT v4l2_debug
61 static GstBufferClass *v4l2buffer_parent_class = NULL;
64 gst_v4l2_buffer_finalize (GstV4l2Buffer * buffer)
66 GstV4l2BufferPool *pool;
67 gboolean resuscitated = FALSE;
72 index = buffer->vbuffer.index;
74 GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index);
76 GST_V4L2_BUFFER_POOL_LOCK (pool);
78 if (pool->requeuebuf) {
79 if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
80 GST_WARNING ("could not requeue buffer %p %d", buffer, index);
86 /* XXX double check this... I think it is ok to not synchronize this
87 * w.r.t. destruction of the pool, since the buffer is still live and
88 * the buffer holds a ref to the pool..
90 g_async_queue_push (pool->avail_buffers, buffer);
93 GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down");
97 /* FIXME: check that the caps didn't change */
98 GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index);
99 gst_buffer_ref (GST_BUFFER (buffer));
100 GST_BUFFER_SIZE (buffer) = 0;
101 pool->buffers[index] = buffer;
104 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
107 GST_LOG_OBJECT (pool->v4l2elem,
108 "buffer %p (data %p, len %u) not recovered, unmapping",
109 buffer, GST_BUFFER_DATA (buffer), buffer->mmap_length);
110 gst_mini_object_unref (GST_MINI_OBJECT (pool));
111 v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), buffer->mmap_length);
113 GST_MINI_OBJECT_CLASS (v4l2buffer_parent_class)->finalize (GST_MINI_OBJECT
119 gst_v4l2_buffer_class_init (gpointer g_class, gpointer class_data)
121 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
123 v4l2buffer_parent_class = g_type_class_peek_parent (g_class);
125 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
126 gst_v4l2_buffer_finalize;
130 gst_v4l2_buffer_get_type (void)
132 static GType _gst_v4l2_buffer_type;
134 if (G_UNLIKELY (_gst_v4l2_buffer_type == 0)) {
135 static const GTypeInfo v4l2_buffer_info = {
136 sizeof (GstBufferClass),
139 gst_v4l2_buffer_class_init,
142 sizeof (GstV4l2Buffer),
147 _gst_v4l2_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
148 "GstV4l2Buffer", &v4l2_buffer_info, 0);
150 return _gst_v4l2_buffer_type;
153 static GstV4l2Buffer *
154 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index, GstCaps * caps)
159 ret = (GstV4l2Buffer *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER);
161 GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
165 (GstV4l2BufferPool *) gst_mini_object_ref (GST_MINI_OBJECT (pool));
167 ret->vbuffer.index = index;
168 ret->vbuffer.type = pool->type;
169 ret->vbuffer.memory = V4L2_MEMORY_MMAP;
171 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &ret->vbuffer) < 0)
172 goto querybuf_failed;
174 GST_LOG_OBJECT (pool->v4l2elem, " index: %u", ret->vbuffer.index);
175 GST_LOG_OBJECT (pool->v4l2elem, " type: %d", ret->vbuffer.type);
176 GST_LOG_OBJECT (pool->v4l2elem, " bytesused: %u", ret->vbuffer.bytesused);
177 GST_LOG_OBJECT (pool->v4l2elem, " flags: %08x", ret->vbuffer.flags);
178 GST_LOG_OBJECT (pool->v4l2elem, " field: %d", ret->vbuffer.field);
179 GST_LOG_OBJECT (pool->v4l2elem, " memory: %d", ret->vbuffer.memory);
180 if (ret->vbuffer.memory == V4L2_MEMORY_MMAP)
181 GST_LOG_OBJECT (pool->v4l2elem, " MMAP offset: %u",
182 ret->vbuffer.m.offset);
183 GST_LOG_OBJECT (pool->v4l2elem, " length: %u", ret->vbuffer.length);
184 GST_LOG_OBJECT (pool->v4l2elem, " input: %u", ret->vbuffer.input);
186 ret->mmap_length = ret->vbuffer.length;
187 data = (guint8 *) v4l2_mmap (0, ret->vbuffer.length,
188 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
189 ret->vbuffer.m.offset);
191 if (data == MAP_FAILED)
194 GST_BUFFER_DATA (ret) = data;
195 GST_BUFFER_SIZE (ret) = ret->vbuffer.length;
197 GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
199 gst_buffer_set_caps (GST_BUFFER (ret), caps);
206 gint errnosave = errno;
208 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
209 gst_buffer_unref (GST_BUFFER (ret));
215 gint errnosave = errno;
217 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
218 gst_buffer_unref (GST_BUFFER (ret));
229 static GstMiniObjectClass *buffer_pool_parent_class = NULL;
232 gst_v4l2_buffer_pool_finalize (GstV4l2BufferPool * pool)
234 g_mutex_free (pool->lock);
237 g_async_queue_unref (pool->avail_buffers);
238 pool->avail_buffers = NULL;
240 if (pool->video_fd >= 0)
241 v4l2_close (pool->video_fd);
244 g_free (pool->buffers);
245 pool->buffers = NULL;
248 GST_MINI_OBJECT_CLASS (buffer_pool_parent_class)->finalize (GST_MINI_OBJECT
253 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool, gpointer g_class)
255 pool->lock = g_mutex_new ();
256 pool->running = FALSE;
257 pool->num_live_buffers = 0;
261 gst_v4l2_buffer_pool_class_init (gpointer g_class, gpointer class_data)
263 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
265 buffer_pool_parent_class = g_type_class_peek_parent (g_class);
267 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
268 gst_v4l2_buffer_pool_finalize;
272 gst_v4l2_buffer_pool_get_type (void)
274 static GType _gst_v4l2_buffer_pool_type;
276 if (G_UNLIKELY (_gst_v4l2_buffer_pool_type == 0)) {
277 static const GTypeInfo v4l2_buffer_pool_info = {
278 sizeof (GstMiniObjectClass),
281 gst_v4l2_buffer_pool_class_init,
284 sizeof (GstV4l2BufferPool),
286 (GInstanceInitFunc) gst_v4l2_buffer_pool_init,
289 _gst_v4l2_buffer_pool_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
290 "GstV4l2BufferPool", &v4l2_buffer_pool_info, 0);
292 return _gst_v4l2_buffer_pool_type;
296 /* this is somewhat of a hack.. but better to keep the hack in
297 * one place than copy/pasting it around..
299 static GstV4l2Object *
300 get_v4l2_object (GstElement * v4l2elem)
302 GstV4l2Object *v4l2object = NULL;
303 if (GST_IS_V4L2SRC (v4l2elem)) {
304 v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
305 #ifdef HAVE_EXPERIMENTAL
306 } else if (GST_IS_V4L2SINK (v4l2elem)) {
307 v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
310 GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
318 * gst_v4l2_buffer_pool_new:
319 * @v4l2elem: the v4l2 element (src or sink) that owns this pool
320 * @fd: the video device file descriptor
321 * @num_buffers: the requested number of buffers in the pool
322 * @caps: the caps to set on the buffer
323 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
324 * buffer with no remaining references is immediately passed back to v4l2
325 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
326 * (which can be accessed via gst_v4l2_buffer_pool_get().
328 * Construct a new buffer pool.
330 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
333 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
334 GstCaps * caps, gboolean requeuebuf, enum v4l2_buf_type type)
336 GstV4l2BufferPool *pool;
338 struct v4l2_requestbuffers breq;
340 pool = (GstV4l2BufferPool *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER_POOL);
342 pool->video_fd = v4l2_dup (fd);
343 if (pool->video_fd < 0)
347 /* first, lets request buffers, and see how many we can get: */
348 GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
351 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
353 breq.count = num_buffers;
354 breq.memory = V4L2_MEMORY_MMAP;
356 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
359 GST_LOG_OBJECT (v4l2elem, " count: %u", breq.count);
360 GST_LOG_OBJECT (v4l2elem, " type: %d", breq.type);
361 GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
363 if (breq.count < GST_V4L2_MIN_BUFFERS)
366 if (num_buffers != breq.count) {
367 GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
368 num_buffers = breq.count;
371 pool->v4l2elem = v4l2elem;
372 pool->requeuebuf = requeuebuf;
374 pool->buffer_count = num_buffers;
375 pool->buffers = g_new0 (GstV4l2Buffer *, num_buffers);
376 pool->avail_buffers = g_async_queue_new ();
378 /* now, map the buffers: */
379 for (n = 0; n < num_buffers; n++) {
380 pool->buffers[n] = gst_v4l2_buffer_new (pool, n, caps);
381 if (!pool->buffers[n])
382 goto buffer_new_failed;
383 pool->num_live_buffers++;
384 g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
392 gint errnosave = errno;
394 gst_mini_object_unref (GST_MINI_OBJECT (pool));
402 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
403 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
404 (_("Could not get buffers from device '%s'."),
405 v4l2object->videodev),
406 ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
411 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
412 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
413 (_("Could not get enough buffers from device '%s'."),
414 v4l2object->videodev),
415 ("we received %d from device '%s', we want at least %d",
416 breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
421 gint errnosave = errno;
423 gst_v4l2_buffer_pool_destroy (pool);
432 * gst_v4l2_buffer_pool_destroy:
435 * Free all resources in the pool and the pool itself.
438 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
442 GST_V4L2_BUFFER_POOL_LOCK (pool);
443 pool->running = FALSE;
444 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
446 GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
448 /* after this point, no more buffers will be queued or dequeued; no buffer
449 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
450 * is not NULL will be pushed out. */
452 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
453 * the pool, we need to unref the buffer to properly finalize te pool */
454 for (n = 0; n < pool->buffer_count; n++) {
457 GST_V4L2_BUFFER_POOL_LOCK (pool);
458 buf = GST_BUFFER (pool->buffers[n]);
459 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
462 /* we own the ref if the buffer is in pool->buffers; drop it. */
463 gst_buffer_unref (buf);
466 gst_mini_object_unref (GST_MINI_OBJECT (pool));
470 * gst_v4l2_buffer_pool_get:
471 * @pool: the "this" object
472 * @blocking: should this call suspend until there is a buffer available
473 * in the buffer pool?
475 * Get an available buffer in the pool
478 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
483 buf = g_async_queue_pop (pool->avail_buffers);
485 buf = g_async_queue_try_pop (pool->avail_buffers);
489 GST_V4L2_BUFFER_POOL_LOCK (pool);
490 GST_BUFFER_SIZE (buf) = buf->vbuffer.length;
491 GST_BUFFER_FLAG_UNSET (buf, 0xffffffff);
492 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
495 pool->running = TRUE;
502 * gst_v4l2_buffer_pool_qbuf:
504 * @buf: the buffer to queue
506 * Queue a buffer to the driver
508 * Returns: %TRUE for success
511 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstV4l2Buffer * buf)
513 GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d", buf->vbuffer.index);
515 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &buf->vbuffer) < 0)
518 pool->num_live_buffers--;
519 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
520 pool->num_live_buffers);
526 * gst_v4l2_buffer_pool_dqbuf:
529 * Dequeue a buffer from the driver. Some generic error handling is done in
530 * this function, but any error handling specific to v4l2src (capture) or
531 * v4l2sink (output) can be done outside this function by checking 'errno'
536 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
538 GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
539 GstV4l2Buffer *pool_buffer;
540 struct v4l2_buffer buffer;
542 memset (&buffer, 0x00, sizeof (buffer));
543 buffer.type = pool->type;
544 buffer.memory = V4L2_MEMORY_MMAP;
547 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) {
549 GST_V4L2_BUFFER_POOL_LOCK (pool);
551 /* get our GstBuffer with that index from the pool, if the buffer was
552 * outstanding we have a serious problem.
554 pool_buffer = pool->buffers[buffer.index];
556 if (pool_buffer == NULL) {
557 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
558 (_("Failed trying to get video frames from device '%s'."),
559 v4l2object->videodev),
560 (_("No free buffers found in the pool at index %d."), buffer.index));
561 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
565 GST_LOG_OBJECT (pool->v4l2elem,
566 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
567 buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
570 pool->num_live_buffers++;
571 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
572 pool->num_live_buffers);
574 /* set top/bottom field first if v4l2_buffer has the information */
575 if (buffer.field == V4L2_FIELD_INTERLACED_TB)
576 GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF);
577 if (buffer.field == V4L2_FIELD_INTERLACED_BT)
578 GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF);
580 /* this can change at every frame, esp. with jpeg */
581 GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
583 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
589 GST_WARNING_OBJECT (pool->v4l2elem,
590 "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
591 buffer.sequence, buffer.index,
592 GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
596 GST_WARNING_OBJECT (pool->v4l2elem,
597 "Non-blocking I/O has been selected using O_NONBLOCK and"
598 " no buffer was in the outgoing queue. device %s",
599 v4l2object->videodev);
602 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
603 (_("Failed trying to get video frames from device '%s'."),
604 v4l2object->videodev),
605 (_("The buffer type is not supported, or the index is out of bounds,"
606 " or no buffers have been allocated yet, or the userptr"
607 " or length are invalid. device %s"), v4l2object->videodev));
610 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
611 (_("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));
614 GST_INFO_OBJECT (pool->v4l2elem,
615 "VIDIOC_DQBUF failed due to an internal error."
616 " Can also indicate temporary problems like signal loss."
617 " Note the driver might dequeue an (empty) buffer despite"
618 " returning an error, or even stop capturing."
619 " device %s", v4l2object->videodev);
620 /* have we de-queued a buffer ? */
621 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
622 GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
623 /* FIXME ... should we do something here? */
627 GST_WARNING_OBJECT (pool->v4l2elem,
628 "could not sync on a buffer on device %s", v4l2object->videodev);
631 GST_WARNING_OBJECT (pool->v4l2elem,
632 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
633 v4l2object->videodev, errno, g_strerror (errno));
641 * gst_v4l2_buffer_pool_available_buffers:
644 * Check the number of buffers available to the driver, ie. buffers that
645 * have been QBUF'd but not yet DQBUF'd.
647 * Returns: the number of buffers available.
650 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
652 return pool->buffer_count - pool->num_live_buffers;