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 * gstv4l2src.h: BT8x8/V4L2 source element
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 <gstv4l2bufferpool.h>
34 #include "gstv4l2src.h"
35 #include "gstv4l2sink.h"
36 #include "v4l2_calls.h"
37 #include "gst/gst-i18n-plugin.h"
40 GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
41 #define GST_CAT_DEFAULT v4l2_debug
48 static GstBufferClass *v4l2buffer_parent_class = NULL;
51 gst_v4l2_buffer_finalize (GstV4l2Buffer * buffer)
53 GstV4l2BufferPool *pool;
54 gboolean resuscitated = FALSE;
59 index = buffer->vbuffer.index;
61 GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index);
63 GST_V4L2_BUFFER_POOL_LOCK (pool);
64 if (GST_BUFFER_SIZE (buffer) != 0) {
65 /* BUFFER_SIZE is only set if the frame was dequeued */
66 pool->num_live_buffers--;
67 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
68 pool->num_live_buffers);
72 if (pool->requeuebuf) {
73 if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
74 GST_WARNING ("could not requeue buffer %p %d", buffer, index);
80 /* XXX double check this... I think it is ok to not synchronize this
81 * w.r.t. destruction of the pool, since the buffer is still live and
82 * the buffer holds a ref to the pool..
84 g_async_queue_push (pool->avail_buffers, buffer);
87 GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down");
91 /* FIXME: check that the caps didn't change */
92 GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index);
93 gst_buffer_ref (GST_BUFFER (buffer));
94 GST_BUFFER_SIZE (buffer) = 0;
95 pool->buffers[index] = buffer;
98 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
101 GST_LOG_OBJECT (pool->v4l2elem, "buffer %p not recovered, unmapping",
103 gst_mini_object_unref (GST_MINI_OBJECT (pool));
104 v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), buffer->vbuffer.length);
106 GST_MINI_OBJECT_CLASS (v4l2buffer_parent_class)->finalize (GST_MINI_OBJECT
112 gst_v4l2_buffer_class_init (gpointer g_class, gpointer class_data)
114 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
116 v4l2buffer_parent_class = g_type_class_peek_parent (g_class);
118 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
119 gst_v4l2_buffer_finalize;
123 gst_v4l2_buffer_get_type (void)
125 static GType _gst_v4l2_buffer_type;
127 if (G_UNLIKELY (_gst_v4l2_buffer_type == 0)) {
128 static const GTypeInfo v4l2_buffer_info = {
129 sizeof (GstBufferClass),
132 gst_v4l2_buffer_class_init,
135 sizeof (GstV4l2Buffer),
140 _gst_v4l2_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
141 "GstV4l2Buffer", &v4l2_buffer_info, 0);
143 return _gst_v4l2_buffer_type;
146 static GstV4l2Buffer *
147 gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index, GstCaps * caps)
152 ret = (GstV4l2Buffer *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER);
154 GST_LOG_OBJECT (pool->v4l2elem, "creating buffer %u, %p in pool %p", index,
158 (GstV4l2BufferPool *) gst_mini_object_ref (GST_MINI_OBJECT (pool));
160 ret->vbuffer.index = index;
161 ret->vbuffer.type = pool->type;
162 ret->vbuffer.memory = V4L2_MEMORY_MMAP;
164 if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &ret->vbuffer) < 0)
165 goto querybuf_failed;
167 GST_LOG_OBJECT (pool->v4l2elem, " index: %u", ret->vbuffer.index);
168 GST_LOG_OBJECT (pool->v4l2elem, " type: %d", ret->vbuffer.type);
169 GST_LOG_OBJECT (pool->v4l2elem, " bytesused: %u", ret->vbuffer.bytesused);
170 GST_LOG_OBJECT (pool->v4l2elem, " flags: %08x", ret->vbuffer.flags);
171 GST_LOG_OBJECT (pool->v4l2elem, " field: %d", ret->vbuffer.field);
172 GST_LOG_OBJECT (pool->v4l2elem, " memory: %d", ret->vbuffer.memory);
173 if (ret->vbuffer.memory == V4L2_MEMORY_MMAP)
174 GST_LOG_OBJECT (pool->v4l2elem, " MMAP offset: %u",
175 ret->vbuffer.m.offset);
176 GST_LOG_OBJECT (pool->v4l2elem, " length: %u", ret->vbuffer.length);
177 GST_LOG_OBJECT (pool->v4l2elem, " input: %u", ret->vbuffer.input);
179 data = (guint8 *) v4l2_mmap (0, ret->vbuffer.length,
180 PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
181 ret->vbuffer.m.offset);
183 if (data == MAP_FAILED)
186 GST_BUFFER_DATA (ret) = data;
187 GST_BUFFER_SIZE (ret) = ret->vbuffer.length;
189 GST_BUFFER_FLAG_SET (ret, GST_BUFFER_FLAG_READONLY);
191 gst_buffer_set_caps (GST_BUFFER (ret), caps);
198 gint errnosave = errno;
200 GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
201 gst_buffer_unref (GST_BUFFER (ret));
207 gint errnosave = errno;
209 GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
210 gst_buffer_unref (GST_BUFFER (ret));
221 static GstMiniObjectClass *buffer_pool_parent_class = NULL;
224 gst_v4l2_buffer_pool_finalize (GstV4l2BufferPool * pool)
226 g_mutex_free (pool->lock);
229 g_async_queue_unref (pool->avail_buffers);
230 pool->avail_buffers = NULL;
232 if (pool->video_fd >= 0)
233 v4l2_close (pool->video_fd);
236 g_free (pool->buffers);
237 pool->buffers = NULL;
240 GST_MINI_OBJECT_CLASS (buffer_pool_parent_class)->finalize (GST_MINI_OBJECT
245 gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool, gpointer g_class)
247 pool->lock = g_mutex_new ();
248 pool->running = FALSE;
249 pool->num_live_buffers = 0;
253 gst_v4l2_buffer_pool_class_init (gpointer g_class, gpointer class_data)
255 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
257 buffer_pool_parent_class = g_type_class_peek_parent (g_class);
259 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
260 gst_v4l2_buffer_pool_finalize;
264 gst_v4l2_buffer_pool_get_type (void)
266 static GType _gst_v4l2_buffer_pool_type;
268 if (G_UNLIKELY (_gst_v4l2_buffer_pool_type == 0)) {
269 static const GTypeInfo v4l2_buffer_pool_info = {
270 sizeof (GstBufferClass),
273 gst_v4l2_buffer_pool_class_init,
276 sizeof (GstV4l2BufferPool),
278 (GInstanceInitFunc) gst_v4l2_buffer_pool_init,
281 _gst_v4l2_buffer_pool_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
282 "GstV4l2BufferPool", &v4l2_buffer_pool_info, 0);
284 return _gst_v4l2_buffer_pool_type;
288 /* this is somewhat of a hack.. but better to keep the hack in
289 * one place than copy/pasting it around..
291 static GstV4l2Object *
292 get_v4l2_object (GstElement * v4l2elem)
294 GstV4l2Object *v4l2object = NULL;
295 if (GST_IS_V4L2SRC (v4l2elem)) {
296 v4l2object = (GST_V4L2SRC (v4l2elem))->v4l2object;
297 } else if (GST_IS_V4L2SINK (v4l2elem)) {
298 v4l2object = (GST_V4L2SINK (v4l2elem))->v4l2object;
300 GST_ERROR_OBJECT (v4l2elem, "unknown v4l2 element");
308 * Construct a new buffer pool
310 * @v4l2elem the v4l2 element (src or sink) that owns this pool
311 * @fd the video device file descriptor
312 * @num_buffers the requested number of buffers in the pool
313 * @caps the caps to set on the buffer
314 * @requeuebuf if <code>TRUE</code>, and if the pool is still in the
315 * <code>running</code> state, a buffer with no remaining references
316 * is immediately passed back to v4l2 (<code>VIDIOC_QBUF</code>),
317 * otherwise it is returned to the pool of available buffers
318 * (which can be accessed via <code>gst_v4l2_buffer_pool_get()</code>.
321 gst_v4l2_buffer_pool_new (GstElement * v4l2elem, gint fd, gint num_buffers,
322 GstCaps * caps, gboolean requeuebuf, enum v4l2_buf_type type)
324 GstV4l2BufferPool *pool;
326 struct v4l2_requestbuffers breq;
328 pool = (GstV4l2BufferPool *) gst_mini_object_new (GST_TYPE_V4L2_BUFFER_POOL);
330 pool->video_fd = v4l2_dup (fd);
331 if (pool->video_fd < 0)
335 /* first, lets request buffers, and see how many we can get: */
336 GST_DEBUG_OBJECT (v4l2elem, "STREAMING, requesting %d MMAP buffers",
339 memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
341 breq.count = num_buffers;
342 breq.memory = V4L2_MEMORY_MMAP;
344 if (v4l2_ioctl (fd, VIDIOC_REQBUFS, &breq) < 0)
347 GST_LOG_OBJECT (v4l2elem, " count: %u", breq.count);
348 GST_LOG_OBJECT (v4l2elem, " type: %d", breq.type);
349 GST_LOG_OBJECT (v4l2elem, " memory: %d", breq.memory);
351 if (breq.count < GST_V4L2_MIN_BUFFERS)
354 if (num_buffers != breq.count) {
355 GST_WARNING_OBJECT (v4l2elem, "using %u buffers instead", breq.count);
356 num_buffers = breq.count;
359 pool->v4l2elem = v4l2elem;
360 pool->requeuebuf = requeuebuf;
362 pool->buffer_count = num_buffers;
363 pool->buffers = g_new0 (GstV4l2Buffer *, num_buffers);
364 pool->avail_buffers = g_async_queue_new ();
366 /* now, map the buffers: */
367 for (n = 0; n < num_buffers; n++) {
368 pool->buffers[n] = gst_v4l2_buffer_new (pool, n, caps);
369 if (!pool->buffers[n])
370 goto buffer_new_failed;
371 g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
379 gint errnosave = errno;
381 gst_mini_object_unref (GST_MINI_OBJECT (pool));
389 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
390 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
391 (_("Could not get buffers from device '%s'."),
392 v4l2object->videodev),
393 ("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
398 GstV4l2Object *v4l2object = get_v4l2_object (v4l2elem);
399 GST_ELEMENT_ERROR (v4l2elem, RESOURCE, READ,
400 (_("Could not get enough buffers from device '%s'."),
401 v4l2object->videodev),
402 ("we received %d from device '%s', we want at least %d",
403 breq.count, v4l2object->videodev, GST_V4L2_MIN_BUFFERS));
408 gint errnosave = errno;
410 gst_v4l2_buffer_pool_destroy (pool);
420 gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
424 GST_V4L2_BUFFER_POOL_LOCK (pool);
425 pool->running = FALSE;
426 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
428 GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool");
430 /* after this point, no more buffers will be queued or dequeued; no buffer
431 * from pool->buffers that is NULL will be set to a buffer, and no buffer that
432 * is not NULL will be pushed out. */
434 /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
435 * the pool, we need to unref the buffer to properly finalize te pool */
436 for (n = 0; n < pool->buffer_count; n++) {
439 GST_V4L2_BUFFER_POOL_LOCK (pool);
440 buf = GST_BUFFER (pool->buffers[n]);
441 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
444 /* we own the ref if the buffer is in pool->buffers; drop it. */
445 gst_buffer_unref (buf);
448 gst_mini_object_unref (GST_MINI_OBJECT (pool));
452 * Get an available buffer in the pool
454 * @pool the "this" object
455 * @blocking if <code>TRUE</code>, then suspend until a buffer is available
458 gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
460 GstV4l2Buffer *buf = NULL;
463 buf = g_async_queue_try_pop (pool->avail_buffers);
465 /* if there isn't a buffer avail, let's try to dequeue one:
467 if (blocking && !buf) {
468 GST_DEBUG_OBJECT (pool->v4l2elem, "No buffers available.. need to dqbuf");
469 buf = gst_v4l2_buffer_pool_dqbuf (pool);
471 /* note: if we get a buf, we don't want to use it directly (because
472 * someone else could still hold a ref).. but instead we release our
473 * reference to it, and if no one else holds a ref it will be returned
474 * to the pool of available buffers.. and if not, we keep looping.
477 gst_buffer_unref (GST_BUFFER (buf));
486 pool->num_live_buffers++;
487 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
488 pool->num_live_buffers);
489 GST_BUFFER_SIZE (buf) = buf->vbuffer.length;
492 pool->running = TRUE;
500 * Queue a buffer to the driver
502 * @pool the "this" object
503 * @buf the buffer to queue
506 gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstV4l2Buffer * buf)
508 GST_LOG_OBJECT (pool->v4l2elem, "enqueue pool buffer %d", buf->vbuffer.index);
510 if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &buf->vbuffer) < 0)
513 pool->num_live_buffers--;
514 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers--: %d",
515 pool->num_live_buffers);
521 * Dequeue a buffer from the driver. Some generic error handling is done in
522 * this function, but any error handling specific to v4l2src (capture) or
523 * v4l2sink (output) can be done outside this function by checking 'errno'
525 * @pool the "this" object
528 gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
530 GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem);
531 GstV4l2Buffer *pool_buffer;
532 struct v4l2_buffer buffer;
534 memset (&buffer, 0x00, sizeof (buffer));
535 buffer.type = pool->type;
536 buffer.memory = V4L2_MEMORY_MMAP;
539 if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) {
541 GST_V4L2_BUFFER_POOL_LOCK (pool);
543 /* get our GstBuffer with that index from the pool, if the buffer was
544 * outstanding we have a serious problem.
546 pool_buffer = pool->buffers[buffer.index];
548 if (pool_buffer == NULL) {
549 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
550 (_("Failed trying to get video frames from device '%s'."),
551 v4l2object->videodev),
552 (_("No free buffers found in the pool at index %d."), buffer.index));
553 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
557 GST_LOG_OBJECT (pool->v4l2elem,
558 "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
559 buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
562 pool->num_live_buffers++;
563 GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d",
564 pool->num_live_buffers);
566 GST_V4L2_BUFFER_POOL_UNLOCK (pool);
568 /* this can change at every frame, esp. with jpeg */
569 GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused;
575 GST_WARNING_OBJECT (pool->v4l2elem,
576 "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
577 buffer.sequence, buffer.index,
578 GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
582 GST_WARNING_OBJECT (pool->v4l2elem,
583 "Non-blocking I/O has been selected using O_NONBLOCK and"
584 " no buffer was in the outgoing queue. device %s",
585 v4l2object->videodev);
588 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
589 (_("Failed trying to get video frames from device '%s'."),
590 v4l2object->videodev),
591 (_("The buffer type is not supported, or the index is out of bounds,"
592 " or no buffers have been allocated yet, or the userptr"
593 " or length are invalid. device %s"), v4l2object->videodev));
596 GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED,
597 (_("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));
600 GST_INFO_OBJECT (pool->v4l2elem,
601 "VIDIOC_DQBUF failed due to an internal error."
602 " Can also indicate temporary problems like signal loss."
603 " Note the driver might dequeue an (empty) buffer despite"
604 " returning an error, or even stop capturing."
605 " device %s", v4l2object->videodev);
606 /* have we de-queued a buffer ? */
607 if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
608 GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer");
609 /* FIXME ... should we do something here? */
613 GST_WARNING_OBJECT (pool->v4l2elem,
614 "could not sync on a buffer on device %s", v4l2object->videodev);
617 GST_WARNING_OBJECT (pool->v4l2elem,
618 "Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
619 v4l2object->videodev, errno, g_strerror (errno));
627 gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
629 return pool->buffer_count - pool->num_live_buffers;