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 static GstBufferClass *v4l2buffer_parent_class = NULL;
63 gst_v4l2_buffer_finalize (GstV4l2Buffer * buffer)
65 GstV4l2BufferPool *pool;
66 gboolean resuscitated = FALSE;
71 index = buffer->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 (GST_BUFFER (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), buffer->vbuffer.length);
109 gst_mini_object_unref (GST_MINI_OBJECT (pool));
110 v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), buffer->vbuffer.length);
112 GST_MINI_OBJECT_CLASS (v4l2buffer_parent_class)->finalize (GST_MINI_OBJECT
118 gst_v4l2_buffer_class_init (gpointer g_class, gpointer class_data)
120 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
122 v4l2buffer_parent_class = g_type_class_peek_parent (g_class);
124 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
125 gst_v4l2_buffer_finalize;
129 gst_v4l2_buffer_get_type (void)
131 static GType _gst_v4l2_buffer_type;
133 if (G_UNLIKELY (_gst_v4l2_buffer_type == 0)) {
134 static const GTypeInfo v4l2_buffer_info = {
135 sizeof (GstBufferClass),
138 gst_v4l2_buffer_class_init,
141 sizeof (GstV4l2Buffer),
146 _gst_v4l2_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
147 "GstV4l2Buffer", &v4l2_buffer_info, 0);
149 return _gst_v4l2_buffer_type;
152 static GstV4l2Buffer *
153 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index, GstCaps * caps)
158 ret = (GstV4l2Buffer *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER);
160 GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
164 (GstV4l2BufferPool *) gst_mini_object_ref (GST_MINI_OBJECT (pool));
166 ret->vbuffer.index = index;
167 ret->vbuffer.type = pool->type;
168 ret->vbuffer.memory = V4L2_MEMORY_MMAP;
170 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &ret->vbuffer) < 0)
171 goto querybuf_failed;
173 GST_LOG_OBJECT (pool->v4l2elem, " index: %u", ret->vbuffer.index);
174 GST_LOG_OBJECT (pool->v4l2elem, " type: %d", ret->vbuffer.type);
175 GST_LOG_OBJECT (pool->v4l2elem, " bytesused: %u", ret->vbuffer.bytesused);
176 GST_LOG_OBJECT (pool->v4l2elem, " flags: %08x", ret->vbuffer.flags);
177 GST_LOG_OBJECT (pool->v4l2elem, " field: %d", ret->vbuffer.field);
178 GST_LOG_OBJECT (pool->v4l2elem, " memory: %d", ret->vbuffer.memory);
179 if (ret->vbuffer.memory == V4L2_MEMORY_MMAP)
180 GST_LOG_OBJECT (pool->v4l2elem, " MMAP offset: %u",
181 ret->vbuffer.m.offset);
182 GST_LOG_OBJECT (pool->v4l2elem, " length: %u", ret->vbuffer.length);
183 GST_LOG_OBJECT (pool->v4l2elem, " input: %u", ret->vbuffer.input);
185 data = (guint8 *) v4l2_mmap (0, ret->vbuffer.length,
186 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
187 ret->vbuffer.m.offset);
189 if (data == MAP_FAILED)
192 GST_BUFFER_DATA (ret) = data;
193 GST_BUFFER_SIZE (ret) = ret->vbuffer.length;
195 GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
197 gst_buffer_set_caps (GST_BUFFER (ret), caps);
204 gint errnosave = errno;
206 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
207 gst_buffer_unref (GST_BUFFER (ret));
213 gint errnosave = errno;
215 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
216 gst_buffer_unref (GST_BUFFER (ret));
227 static GstMiniObjectClass *buffer_pool_parent_class = NULL;
230 gst_v4l2_buffer_pool_finalize (GstV4l2BufferPool * pool)
232 g_mutex_free (pool->lock);
235 g_async_queue_unref (pool->avail_buffers);
236 pool->avail_buffers = NULL;
238 if (pool->video_fd >= 0)
239 v4l2_close (pool->video_fd);
242 g_free (pool->buffers);
243 pool->buffers = NULL;
246 GST_MINI_OBJECT_CLASS (buffer_pool_parent_class)->finalize (GST_MINI_OBJECT
251 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool, gpointer g_class)
253 pool->lock = g_mutex_new ();
254 pool->running = FALSE;
255 pool->num_live_buffers = 0;
259 gst_v4l2_buffer_pool_class_init (gpointer g_class, gpointer class_data)
261 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
263 buffer_pool_parent_class = g_type_class_peek_parent (g_class);
265 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
266 gst_v4l2_buffer_pool_finalize;
270 gst_v4l2_buffer_pool_get_type (void)
272 static GType _gst_v4l2_buffer_pool_type;
274 if (G_UNLIKELY (_gst_v4l2_buffer_pool_type == 0)) {
275 static const GTypeInfo v4l2_buffer_pool_info = {
276 sizeof (GstMiniObjectClass),
279 gst_v4l2_buffer_pool_class_init,
282 sizeof (GstV4l2BufferPool),
284 (GInstanceInitFunc) gst_v4l2_buffer_pool_init,
287 _gst_v4l2_buffer_pool_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
288 "GstV4l2BufferPool", &v4l2_buffer_pool_info, 0);
290 return _gst_v4l2_buffer_pool_type;
294 /* this is somewhat of a hack.. but better to keep the hack in
295 * one place than copy/pasting it around..
297 static GstV4l2Object *
298 get_v4l2_object (GstElement * v4l2elem)
300 GstV4l2Object *v4l2object = NULL;
301 if (GST_IS_V4L2SRC (v4l2elem)) {
302 v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
303 #ifdef HAVE_EXPERIMENTAL
304 } else if (GST_IS_V4L2SINK (v4l2elem)) {
305 v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
308 GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
316 * gst_v4l2_buffer_pool_new:
317 * @v4l2elem: the v4l2 element (src or sink) that owns this pool
318 * @fd: the video device file descriptor
319 * @num_buffers: the requested number of buffers in the pool
320 * @caps: the caps to set on the buffer
321 * @requeuebuf: if %TRUE, and if the pool is still in the running state, a
322 * buffer with no remaining references is immediately passed back to v4l2
323 * (VIDIOC_QBUF), otherwise it is returned to the pool of available buffers
324 * (which can be accessed via gst_v4l2_buffer_pool_get().
326 * Construct a new buffer pool.
328 * Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
331 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
332 GstCaps * caps, gboolean requeuebuf, enum v4l2_buf_type type)
334 GstV4l2BufferPool *pool;
336 struct v4l2_requestbuffers breq;
338 pool = (GstV4l2BufferPool *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER_POOL);
340 pool->video_fd = v4l2_dup (fd);
341 if (pool->video_fd < 0)
345 /* first, lets request buffers, and see how many we can get: */
346 GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
349 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
351 breq.count = num_buffers;
352 breq.memory = V4L2_MEMORY_MMAP;
354 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
357 GST_LOG_OBJECT (v4l2elem, " count: %u", breq.count);
358 GST_LOG_OBJECT (v4l2elem, " type: %d", breq.type);
359 GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
361 if (breq.count < GST_V4L2_MIN_BUFFERS)
364 if (num_buffers != breq.count) {
365 GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
366 num_buffers = breq.count;
369 pool->v4l2elem = v4l2elem;
370 pool->requeuebuf = requeuebuf;
372 pool->buffer_count = num_buffers;
373 pool->buffers = g_new0 (GstV4l2Buffer *, num_buffers);
374 pool->avail_buffers = g_async_queue_new ();
376 /* now, map the buffers: */
377 for (n = 0; n < num_buffers; n++) {
378 pool->buffers[n] = gst_v4l2_buffer_new (pool, n, caps);
379 if (!pool->buffers[n])
380 goto buffer_new_failed;
381 pool->num_live_buffers++;
382 g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
390 gint errnosave = errno;
392 gst_mini_object_unref (GST_MINI_OBJECT (pool));
400 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
401 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
402 (_("Could not get buffers from device '%s'."),
403 v4l2object->videodev),
404 ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
409 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
410 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
411 (_("Could not get enough buffers from device '%s'."),
412 v4l2object->videodev),
413 ("we received %d from device '%s', we want at least %d",
414 breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
419 gint errnosave = errno;
421 gst_v4l2_buffer_pool_destroy (pool);
430 * gst_v4l2_buffer_pool_destroy:
433 * Free all resources in the pool and the pool itself.
436 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
440 GST_V4L2_BUFFER_POOL_LOCK (pool);
441 pool->running = FALSE;
442 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
444 GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
446 /* after this point, no more buffers will be queued or dequeued; no buffer
447 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
448 * is not NULL will be pushed out. */
450 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
451 * the pool, we need to unref the buffer to properly finalize te pool */
452 for (n = 0; n < pool->buffer_count; n++) {
455 GST_V4L2_BUFFER_POOL_LOCK (pool);
456 buf = GST_BUFFER (pool->buffers[n]);
457 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
460 /* we own the ref if the buffer is in pool->buffers; drop it. */
461 gst_buffer_unref (buf);
464 gst_mini_object_unref (GST_MINI_OBJECT (pool));
468 * gst_v4l2_buffer_pool_get:
469 * @pool: the "this" object
470 * @blocking: should this call suspend until there is a buffer available
471 * in the buffer pool?
473 * Get an available buffer in the pool
476 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
481 buf = g_async_queue_pop (pool->avail_buffers);
483 buf = g_async_queue_try_pop (pool->avail_buffers);
487 GST_V4L2_BUFFER_POOL_LOCK (pool);
488 GST_BUFFER_SIZE (buf) = buf->vbuffer.length;
489 GST_BUFFER_FLAG_UNSET (buf, 0xffffffff);
490 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
493 pool->running = TRUE;
500 * gst_v4l2_buffer_pool_qbuf:
502 * @buf: the buffer to queue
504 * Queue a buffer to the driver
506 * Returns: %TRUE for success
509 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstV4l2Buffer * buf)
511 GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d", buf->vbuffer.index);
513 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &buf->vbuffer) < 0)
516 pool->num_live_buffers--;
517 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
518 pool->num_live_buffers);
524 * gst_v4l2_buffer_pool_dqbuf:
527 * Dequeue a buffer from the driver. Some generic error handling is done in
528 * this function, but any error handling specific to v4l2src (capture) or
529 * v4l2sink (output) can be done outside this function by checking 'errno'
534 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
536 GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
537 GstV4l2Buffer *pool_buffer;
538 struct v4l2_buffer buffer;
540 memset (&buffer, 0x00, sizeof (buffer));
541 buffer.type = pool->type;
542 buffer.memory = V4L2_MEMORY_MMAP;
545 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) {
547 GST_V4L2_BUFFER_POOL_LOCK (pool);
549 /* get our GstBuffer with that index from the pool, if the buffer was
550 * outstanding we have a serious problem.
552 pool_buffer = pool->buffers[buffer.index];
554 if (pool_buffer == NULL) {
555 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
556 (_("Failed trying to get video frames from device '%s'."),
557 v4l2object->videodev),
558 (_("No free buffers found in the pool at index %d."), buffer.index));
559 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
563 GST_LOG_OBJECT (pool->v4l2elem,
564 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
565 buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
568 pool->num_live_buffers++;
569 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
570 pool->num_live_buffers);
572 /* set top/bottom field first if v4l2_buffer has the information */
573 if (buffer.field == V4L2_FIELD_INTERLACED_TB)
574 GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF);
575 if (buffer.field == V4L2_FIELD_INTERLACED_BT)
576 GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF);
578 /* this can change at every frame, esp. with jpeg */
579 GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
581 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
587 GST_WARNING_OBJECT (pool->v4l2elem,
588 "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
589 buffer.sequence, buffer.index,
590 GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
594 GST_WARNING_OBJECT (pool->v4l2elem,
595 "Non-blocking I/O has been selected using O_NONBLOCK and"
596 " no buffer was in the outgoing queue. device %s",
597 v4l2object->videodev);
600 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
601 (_("Failed trying to get video frames from device '%s'."),
602 v4l2object->videodev),
603 (_("The buffer type is not supported, or the index is out of bounds,"
604 " or no buffers have been allocated yet, or the userptr"
605 " or length are invalid. device %s"), v4l2object->videodev));
608 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
609 (_("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));
612 GST_INFO_OBJECT (pool->v4l2elem,
613 "VIDIOC_DQBUF failed due to an internal error."
614 " Can also indicate temporary problems like signal loss."
615 " Note the driver might dequeue an (empty) buffer despite"
616 " returning an error, or even stop capturing."
617 " device %s", v4l2object->videodev);
618 /* have we de-queued a buffer ? */
619 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
620 GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
621 /* FIXME ... should we do something here? */
625 GST_WARNING_OBJECT (pool->v4l2elem,
626 "could not sync on a buffer on device %s", v4l2object->videodev);
629 GST_WARNING_OBJECT (pool->v4l2elem,
630 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
631 v4l2object->videodev, errno, g_strerror (errno));
639 * gst_v4l2_buffer_pool_available_buffers:
642 * Check the number of buffers available to the driver, ie. buffers that
643 * have been QBUF'd but not yet DQBUF'd.
645 * Returns: the number of buffers available.
648 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
650 return pool->buffer_count - pool->num_live_buffers;