v4l2: convert to GstBufferPool

Extend from GstBufferPool.
Handle the lifetime of the pool buffers correctly with the start/stop vmethods.
Map acquire and release directly to QBUF and DQBUF. We still expose an explicit
qbuf for the v4l2sink for now.
This commit is contained in:
Wim Taymans 2011-07-15 11:27:18 +02:00
parent 0f16daed15
commit 21f532ae10
4 changed files with 380 additions and 408 deletions

View file

@ -34,6 +34,7 @@
#include "gst/video/gstmetavideo.h"
#include <gstv4l2bufferpool.h>
#include "gstv4l2src.h"
#include "gstv4l2sink.h"
#include "v4l2_calls.h"
@ -69,11 +70,16 @@ gst_meta_v4l2_get_info (void)
return meta_info;
}
/*
* GstV4l2BufferPool:
*/
#define gst_v4l2_buffer_pool_parent_class parent_class
G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
static void
gst_v4l2_buffer_dispose (GstBuffer * buffer)
gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
{
GstV4l2BufferPool *pool;
gboolean resuscitated = FALSE;
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
gint index;
GstMetaV4l2 *meta;
GstV4l2Object *obj;
@ -81,90 +87,58 @@ gst_v4l2_buffer_dispose (GstBuffer * buffer)
meta = GST_META_V4L2_GET (buffer);
g_assert (meta != NULL);
pool = meta->pool;
index = meta->vbuffer.index;
obj = pool->obj;
GST_LOG_OBJECT (obj->element, "finalizing buffer %p %d", buffer, index);
index = meta->vbuffer.index;
GST_LOG_OBJECT (pool, "finalizing buffer %p %d", buffer, index);
pool->buffers[index] = NULL;
GST_V4L2_BUFFER_POOL_LOCK (pool);
if (pool->running) {
if (pool->requeuebuf) {
if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) {
GST_WARNING_OBJECT (obj->element, "could not requeue buffer %p %d",
buffer, index);
} else {
resuscitated = TRUE;
}
} else {
resuscitated = TRUE;
/* XXX double check this... I think it is ok to not synchronize this
* w.r.t. destruction of the pool, since the buffer is still live and
* the buffer holds a ref to the pool..
*/
g_async_queue_push (pool->avail_buffers, buffer);
}
} else {
GST_LOG_OBJECT (obj->element, "the pool is shutting down");
}
GST_LOG_OBJECT (pool,
"buffer %p (data %p, len %u) freed, unmapping",
buffer, meta->mem, meta->vbuffer.length);
v4l2_munmap (meta->mem, meta->vbuffer.length);
if (resuscitated) {
GST_LOG_OBJECT (obj->element, "reviving buffer %p, %d", buffer, index);
gst_buffer_ref (buffer);
pool->buffers[index] = buffer;
}
GST_V4L2_BUFFER_POOL_UNLOCK (pool);
if (!resuscitated) {
GST_LOG_OBJECT (obj->element,
"buffer %p (data %p, len %u) not recovered, unmapping",
buffer, meta->mem, meta->vbuffer.length);
v4l2_munmap (meta->mem, meta->vbuffer.length);
g_object_unref (pool);
}
gst_buffer_unref (buffer);
}
static GstBuffer *
gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index)
static GstFlowReturn
gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
GstBufferPoolParams * params)
{
GstBuffer *ret;
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
GstBuffer *newbuf;
GstMetaV4l2 *meta;
GstV4l2Object *obj;
GstVideoInfo *info;
guint index;
obj = pool->obj;
info = &obj->info;
ret = gst_buffer_new ();
GST_MINI_OBJECT_CAST (ret)->dispose =
(GstMiniObjectDisposeFunction) gst_v4l2_buffer_dispose;
newbuf = gst_buffer_new ();
meta = GST_META_V4L2_ADD (newbuf);
meta = GST_META_V4L2_ADD (ret);
index = pool->index;
GST_LOG_OBJECT (obj->element, "creating buffer %u, %p in pool %p", index,
ret, pool);
meta->pool = (GstV4l2BufferPool *) g_object_ref (pool);
GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf, pool);
meta->vbuffer.index = index;
meta->vbuffer.type = pool->type;
meta->vbuffer.type = obj->type;
meta->vbuffer.memory = V4L2_MEMORY_MMAP;
if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
goto querybuf_failed;
GST_LOG_OBJECT (obj->element, " index: %u", meta->vbuffer.index);
GST_LOG_OBJECT (obj->element, " type: %d", meta->vbuffer.type);
GST_LOG_OBJECT (obj->element, " bytesused: %u", meta->vbuffer.bytesused);
GST_LOG_OBJECT (obj->element, " flags: %08x", meta->vbuffer.flags);
GST_LOG_OBJECT (obj->element, " field: %d", meta->vbuffer.field);
GST_LOG_OBJECT (obj->element, " memory: %d", meta->vbuffer.memory);
GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
GST_LOG_OBJECT (pool, " bytesused: %u", meta->vbuffer.bytesused);
GST_LOG_OBJECT (pool, " flags: %08x", meta->vbuffer.flags);
GST_LOG_OBJECT (pool, " field: %d", meta->vbuffer.field);
GST_LOG_OBJECT (pool, " memory: %d", meta->vbuffer.memory);
if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
GST_LOG_OBJECT (obj->element, " MMAP offset: %u", meta->vbuffer.m.offset);
GST_LOG_OBJECT (obj->element, " length: %u", meta->vbuffer.length);
GST_LOG_OBJECT (obj->element, " input: %u", meta->vbuffer.input);
GST_LOG_OBJECT (pool, " MMAP offset: %u", meta->vbuffer.m.offset);
GST_LOG_OBJECT (pool, " length: %u", meta->vbuffer.length);
GST_LOG_OBJECT (pool, " input: %u", meta->vbuffer.input);
meta->mem = v4l2_mmap (0, meta->vbuffer.length,
PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
@ -172,7 +146,7 @@ gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index)
if (meta->mem == MAP_FAILED)
goto mmap_failed;
gst_buffer_take_memory (ret, -1,
gst_buffer_take_memory (newbuf, -1,
gst_memory_new_wrapped (0,
meta->mem, NULL, meta->vbuffer.length, 0, meta->vbuffer.length));
@ -184,13 +158,18 @@ gst_v4l2_buffer_new (GstV4l2BufferPool * pool, guint index)
offset[0] = 0;
stride[0] = obj->bytesperline;
GST_DEBUG ("adding video meta");
gst_buffer_add_meta_video_full (ret, info->flags,
GST_DEBUG_OBJECT (pool, "adding video meta");
gst_buffer_add_meta_video_full (newbuf, info->flags,
GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
offset, stride);
}
return ret;
pool->index++;
*buffer = newbuf;
return GST_FLOW_OK;
/* ERRORS */
querybuf_failed:
@ -198,38 +177,270 @@ querybuf_failed:
gint errnosave = errno;
GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
gst_buffer_unref (ret);
gst_buffer_unref (newbuf);
errno = errnosave;
return NULL;
return GST_FLOW_ERROR;
}
mmap_failed:
{
gint errnosave = errno;
GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
gst_buffer_unref (ret);
gst_buffer_unref (newbuf);
errno = errnosave;
return NULL;
return GST_FLOW_ERROR;
}
}
/*
* GstV4l2BufferPool:
*/
#define gst_v4l2_buffer_pool_parent_class parent_class
G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, G_TYPE_OBJECT);
static gboolean
gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
const GstCaps *caps;
guint size, min_buffers, max_buffers;
guint prefix, align;
GST_DEBUG_OBJECT (pool, "set config");
/* parse the config and keep around */
if (!gst_buffer_pool_config_get (config, &caps, &size, &min_buffers,
&max_buffers, &prefix, &align))
goto wrong_config;
GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
pool->min_buffers = min_buffers;
pool->max_buffers = max_buffers;
return TRUE;
wrong_config:
{
GST_WARNING_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
return FALSE;
}
}
static gboolean
gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
GstV4l2Object *obj = pool->obj;
gint n;
struct v4l2_requestbuffers breq;
gint num_buffers;
num_buffers = pool->max_buffers;
/* first, lets request buffers, and see how many we can get: */
GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers", num_buffers);
memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
breq.type = obj->type;
breq.count = num_buffers;
breq.memory = V4L2_MEMORY_MMAP;
if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
goto reqbufs_failed;
GST_LOG_OBJECT (pool, " count: %u", breq.count);
GST_LOG_OBJECT (pool, " type: %d", breq.type);
GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
if (breq.count < GST_V4L2_MIN_BUFFERS)
goto no_buffers;
if (num_buffers != breq.count) {
GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
num_buffers = breq.count;
}
pool->obj = obj;
pool->requeuebuf = (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? TRUE : FALSE);
pool->buffer_count = num_buffers;
pool->buffers = g_new0 (GstBuffer *, num_buffers);
pool->index = 0;
/* now, map the buffers: */
for (n = 0; n < num_buffers; n++) {
GstBuffer *buffer;
if (gst_v4l2_buffer_pool_alloc_buffer (bpool, &buffer, NULL) != GST_FLOW_OK)
goto buffer_new_failed;
if (pool->requeuebuf)
gst_v4l2_buffer_pool_qbuf (bpool, buffer);
}
return TRUE;
/* ERRORS */
reqbufs_failed:
{
GST_ELEMENT_ERROR (pool, RESOURCE, READ,
(_("Could not get buffers from device '%s'."),
obj->videodev),
("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
return FALSE;
}
no_buffers:
{
GST_ELEMENT_ERROR (pool, RESOURCE, READ,
(_("Could not get enough buffers from device '%s'."),
obj->videodev),
("we received %d from device '%s', we want at least %d",
breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS));
return FALSE;
}
buffer_new_failed:
{
return FALSE;
}
}
static gboolean
gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
guint n;
GST_DEBUG_OBJECT (pool, "stopping pool");
/* free the buffers: */
for (n = 0; n < pool->buffer_count; n++)
gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
return TRUE;
}
static GstFlowReturn
gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
GstBufferPoolParams * params)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
GstBuffer *outbuf;
struct v4l2_buffer vbuffer;
GstV4l2Object *obj = pool->obj;
if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
goto flushing;
memset (&vbuffer, 0x00, sizeof (vbuffer));
vbuffer.type = obj->type;
vbuffer.memory = V4L2_MEMORY_MMAP;
if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
goto error;
/* get our GstBuffer with that index from the pool, if the buffer was
* outstanding we have a serious problem.
*/
outbuf = pool->buffers[vbuffer.index];
if (outbuf == NULL)
goto no_buffers;
/* mark the buffer outstanding */
pool->buffers[vbuffer.index] = NULL;
GST_LOG_OBJECT (pool,
"dequeued frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
vbuffer.sequence, vbuffer.index, vbuffer.flags, pool->num_live_buffers,
outbuf);
pool->num_live_buffers++;
GST_DEBUG_OBJECT (pool, "num_live_buffers++: %d", pool->num_live_buffers);
/* set top/bottom field first if v4l2_buffer has the information */
if (vbuffer.field == V4L2_FIELD_INTERLACED_TB)
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
if (vbuffer.field == V4L2_FIELD_INTERLACED_BT)
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_TFF);
/* this can change at every frame, esp. with jpeg */
gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
*buffer = outbuf;
return GST_FLOW_OK;
/* ERRORS */
flushing:
{
return GST_FLOW_WRONG_STATE;
}
error:
{
GST_WARNING_OBJECT (pool,
"problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
vbuffer.sequence, vbuffer.index,
GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
switch (errno) {
case EAGAIN:
GST_WARNING_OBJECT (pool,
"Non-blocking I/O has been selected using O_NONBLOCK and"
" no buffer was in the outgoing queue. device %s", obj->videodev);
break;
case EINVAL:
GST_ELEMENT_ERROR (pool, RESOURCE, FAILED,
(_("Failed trying to get video frames from device '%s'."),
obj->videodev),
(_("The buffer type is not supported, or the index is out of bounds," " or no buffers have been allocated yet, or the userptr" " or length are invalid. device %s"), obj->videodev));
break;
case ENOMEM:
GST_ELEMENT_ERROR (pool, RESOURCE, FAILED,
(_("Failed trying to get video frames from device '%s'. Not enough memory."), obj->videodev), (_("insufficient memory to enqueue a user pointer buffer. device %s."), obj->videodev));
break;
case EIO:
GST_INFO_OBJECT (pool,
"VIDIOC_DQBUF failed due to an internal error."
" Can also indicate temporary problems like signal loss."
" Note the driver might dequeue an (empty) buffer despite"
" returning an error, or even stop capturing."
" device %s", obj->videodev);
/* have we de-queued a buffer ? */
if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
GST_DEBUG_OBJECT (pool, "reenqueing buffer");
/* FIXME ... should we do something here? */
}
break;
case EINTR:
GST_WARNING_OBJECT (pool,
"could not sync on a buffer on device %s", obj->videodev);
break;
default:
GST_WARNING_OBJECT (pool,
"Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
obj->videodev, errno, g_strerror (errno));
break;
}
return GST_FLOW_ERROR;
}
no_buffers:
{
GST_ELEMENT_ERROR (pool, RESOURCE, FAILED,
(_("Failed trying to get video frames from device '%s'."),
obj->videodev),
(_("No free buffers found in the pool at index %d."), vbuffer.index));
return GST_FLOW_ERROR;
}
}
static void
gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
GST_DEBUG_OBJECT (pool, "release");
if (pool->requeuebuf)
gst_v4l2_buffer_pool_qbuf (bpool, buffer);
}
static void
gst_v4l2_buffer_pool_finalize (GObject * object)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
g_mutex_free (pool->lock);
pool->lock = NULL;
g_async_queue_unref (pool->avail_buffers);
pool->avail_buffers = NULL;
if (pool->video_fd >= 0)
v4l2_close (pool->video_fd);
@ -244,17 +455,23 @@ gst_v4l2_buffer_pool_finalize (GObject * object)
static void
gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
{
pool->lock = g_mutex_new ();
pool->running = FALSE;
pool->num_live_buffers = 0;
}
static void
gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
object_class->finalize = gst_v4l2_buffer_pool_finalize;
bufferpool_class->start = gst_v4l2_buffer_pool_start;
bufferpool_class->stop = gst_v4l2_buffer_pool_stop;
bufferpool_class->set_config = gst_v4l2_buffer_pool_set_config;
bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
bufferpool_class->free_buffer = gst_v4l2_buffer_pool_free_buffer;
}
/**
@ -270,13 +487,10 @@ gst_v4l2_buffer_pool_class_init (GstV4l2BufferPoolClass * klass)
*
* Returns: the new pool, use gst_v4l2_buffer_pool_destroy() to free resources
*/
GstV4l2BufferPool *
gst_v4l2_buffer_pool_new (GstV4l2Object * obj, gint num_buffers,
gboolean requeuebuf)
GstBufferPool *
gst_v4l2_buffer_pool_new (GstV4l2Object * obj)
{
GstV4l2BufferPool *pool;
gint n;
struct v4l2_requestbuffers breq;
pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
@ -284,161 +498,20 @@ gst_v4l2_buffer_pool_new (GstV4l2Object * obj, gint num_buffers,
if (pool->video_fd < 0)
goto dup_failed;
/* first, lets request buffers, and see how many we can get: */
GST_DEBUG_OBJECT (obj->element, "STREAMING, requesting %d MMAP buffers",
num_buffers);
memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
breq.type = obj->type;
breq.count = num_buffers;
breq.memory = V4L2_MEMORY_MMAP;
if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
goto reqbufs_failed;
GST_LOG_OBJECT (obj->element, " count: %u", breq.count);
GST_LOG_OBJECT (obj->element, " type: %d", breq.type);
GST_LOG_OBJECT (obj->element, " memory: %d", breq.memory);
if (breq.count < GST_V4L2_MIN_BUFFERS)
goto no_buffers;
if (num_buffers != breq.count) {
GST_WARNING_OBJECT (obj->element, "using %u buffers instead", breq.count);
num_buffers = breq.count;
}
pool->obj = obj;
pool->requeuebuf = requeuebuf;
pool->type = obj->type;
pool->buffer_count = num_buffers;
pool->buffers = g_new0 (GstBuffer *, num_buffers);
pool->avail_buffers = g_async_queue_new ();
/* now, map the buffers: */
for (n = 0; n < num_buffers; n++) {
pool->buffers[n] = gst_v4l2_buffer_new (pool, n);
if (!pool->buffers[n])
goto buffer_new_failed;
pool->num_live_buffers++;
g_async_queue_push (pool->avail_buffers, pool->buffers[n]);
}
return pool;
return GST_BUFFER_POOL_CAST (pool);
/* ERRORS */
dup_failed:
{
gint errnosave = errno;
g_object_unref (pool);
gst_object_unref (pool);
errno = errnosave;
return NULL;
}
reqbufs_failed:
{
GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
(_("Could not get buffers from device '%s'."),
obj->videodev),
("error requesting %d buffers: %s", num_buffers, g_strerror (errno)));
return NULL;
}
no_buffers:
{
GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
(_("Could not get enough buffers from device '%s'."),
obj->videodev),
("we received %d from device '%s', we want at least %d",
breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS));
return NULL;
}
buffer_new_failed:
{
gint errnosave = errno;
gst_v4l2_buffer_pool_destroy (pool);
errno = errnosave;
return NULL;
}
}
/**
* gst_v4l2_buffer_pool_destroy:
* @pool: the pool
*
* Free all resources in the pool and the pool itself.
*/
void
gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool)
{
gint n;
GstV4l2Object *obj = pool->obj;
GST_V4L2_BUFFER_POOL_LOCK (pool);
pool->running = FALSE;
GST_V4L2_BUFFER_POOL_UNLOCK (pool);
GST_DEBUG_OBJECT (obj->element, "destroy pool");
/* after this point, no more buffers will be queued or dequeued; no buffer
* from pool->buffers that is NULL will be set to a buffer, and no buffer that
* is not NULL will be pushed out. */
/* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref
* the pool, we need to unref the buffer to properly finalize te pool */
for (n = 0; n < pool->buffer_count; n++) {
GstBuffer *buf;
GST_V4L2_BUFFER_POOL_LOCK (pool);
buf = pool->buffers[n];
GST_V4L2_BUFFER_POOL_UNLOCK (pool);
if (buf)
/* we own the ref if the buffer is in pool->buffers; drop it. */
gst_buffer_unref (buf);
}
g_object_unref (pool);
}
/**
* gst_v4l2_buffer_pool_get:
* @pool: the "this" object
* @blocking: should this call suspend until there is a buffer available
* in the buffer pool?
*
* Get an available buffer in the pool
*/
GstBuffer *
gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
{
GstBuffer *buf;
if (blocking) {
buf = g_async_queue_pop (pool->avail_buffers);
} else {
buf = g_async_queue_try_pop (pool->avail_buffers);
}
if (buf) {
GstMetaV4l2 *meta = GST_META_V4L2_GET (buf);
GST_V4L2_BUFFER_POOL_LOCK (pool);
gst_buffer_resize (buf, 0, meta->vbuffer.length);
GST_BUFFER_FLAG_UNSET (buf, 0xffffffff);
GST_V4L2_BUFFER_POOL_UNLOCK (pool);
}
pool->running = TRUE;
return buf;
}
/**
* gst_v4l2_buffer_pool_qbuf:
* @pool: the pool
@ -449,145 +522,42 @@ gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking)
* Returns: %TRUE for success
*/
gboolean
gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
gst_v4l2_buffer_pool_qbuf (GstBufferPool * bpool, GstBuffer * buf)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
GstMetaV4l2 *meta;
GstV4l2Object *obj = pool->obj;
gint index;
meta = GST_META_V4L2_GET (buf);
g_assert (meta != NULL);
GST_LOG_OBJECT (obj->element, "enqueue pool buffer %d", meta->vbuffer.index);
index = meta->vbuffer.index;
GST_LOG_OBJECT (pool, "enqueue pool buffer %d", index);
if (pool->buffers[index] != NULL)
goto already_queued;
if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
goto queue_failed;
pool->buffers[index] = buf;
pool->num_live_buffers--;
GST_DEBUG_OBJECT (obj->element, "num_live_buffers--: %d",
pool->num_live_buffers);
GST_DEBUG_OBJECT (pool, "num_live_buffers--: %d", pool->num_live_buffers);
return TRUE;
/* ERRORS */
queue_failed:
already_queued:
{
GST_WARNING_OBJECT (obj->element, "could not queue a buffer");
GST_WARNING_OBJECT (pool, "the buffer was already queued");
return FALSE;
}
}
/**
* gst_v4l2_buffer_pool_dqbuf:
* @pool: the pool
*
* Dequeue a buffer from the driver. Some generic error handling is done in
* this function, but any error handling specific to v4l2src (capture) or
* v4l2sink (output) can be done outside this function by checking 'errno'
*
* Returns: a buffer
*/
GstBuffer *
gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool)
{
GstBuffer *pool_buffer;
struct v4l2_buffer buffer;
GstV4l2Object *obj = pool->obj;
memset (&buffer, 0x00, sizeof (buffer));
buffer.type = pool->type;
buffer.memory = V4L2_MEMORY_MMAP;
if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) < 0)
goto error;
GST_V4L2_BUFFER_POOL_LOCK (pool);
/* get our GstBuffer with that index from the pool, if the buffer was
* outstanding we have a serious problem.
*/
pool_buffer = pool->buffers[buffer.index];
if (pool_buffer == NULL)
goto no_buffers;
GST_LOG_OBJECT (obj->element,
"grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p",
buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers,
pool_buffer);
pool->num_live_buffers++;
GST_DEBUG_OBJECT (obj->element, "num_live_buffers++: %d",
pool->num_live_buffers);
/* set top/bottom field first if v4l2_buffer has the information */
if (buffer.field == V4L2_FIELD_INTERLACED_TB)
GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF);
if (buffer.field == V4L2_FIELD_INTERLACED_BT)
GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF);
/* this can change at every frame, esp. with jpeg */
gst_buffer_resize (pool_buffer, 0, buffer.bytesused);
GST_V4L2_BUFFER_POOL_UNLOCK (pool);
return pool_buffer;
/* ERRORS */
error:
queue_failed:
{
GST_WARNING_OBJECT (obj->element,
"problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
buffer.sequence, buffer.index,
GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags);
switch (errno) {
case EAGAIN:
GST_WARNING_OBJECT (obj->element,
"Non-blocking I/O has been selected using O_NONBLOCK and"
" no buffer was in the outgoing queue. device %s", obj->videodev);
break;
case EINVAL:
GST_ELEMENT_ERROR (obj->element, RESOURCE, FAILED,
(_("Failed trying to get video frames from device '%s'."),
obj->videodev),
(_("The buffer type is not supported, or the index is out of bounds," " or no buffers have been allocated yet, or the userptr" " or length are invalid. device %s"), obj->videodev));
break;
case ENOMEM:
GST_ELEMENT_ERROR (obj->element, RESOURCE, FAILED,
(_("Failed trying to get video frames from device '%s'. Not enough memory."), obj->videodev), (_("insufficient memory to enqueue a user pointer buffer. device %s."), obj->videodev));
break;
case EIO:
GST_INFO_OBJECT (obj->element,
"VIDIOC_DQBUF failed due to an internal error."
" Can also indicate temporary problems like signal loss."
" Note the driver might dequeue an (empty) buffer despite"
" returning an error, or even stop capturing."
" device %s", obj->videodev);
/* have we de-queued a buffer ? */
if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
GST_DEBUG_OBJECT (obj->element, "reenqueing buffer");
/* FIXME ... should we do something here? */
}
break;
case EINTR:
GST_WARNING_OBJECT (obj->element,
"could not sync on a buffer on device %s", obj->videodev);
break;
default:
GST_WARNING_OBJECT (obj->element,
"Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
obj->videodev, errno, g_strerror (errno));
break;
}
return NULL;
}
no_buffers:
{
GST_ELEMENT_ERROR (obj->element, RESOURCE, FAILED,
(_("Failed trying to get video frames from device '%s'."),
obj->videodev),
(_("No free buffers found in the pool at index %d."), buffer.index));
GST_V4L2_BUFFER_POOL_UNLOCK (pool);
return NULL;
GST_WARNING_OBJECT (pool, "could not queue a buffer");
return FALSE;
}
}
@ -601,7 +571,9 @@ no_buffers:
* Returns: the number of buffers available.
*/
gint
gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool * pool)
gst_v4l2_buffer_pool_available_buffers (GstBufferPool * bpool)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
return pool->buffer_count - pool->num_live_buffers;
}

View file

@ -39,34 +39,31 @@ GST_DEBUG_CATEGORY_EXTERN (v4l2buffer_debug);
G_BEGIN_DECLS
GType gst_v4l2_buffer_pool_get_type (void);
#define GST_TYPE_V4L2_BUFFER_POOL (gst_v4l2_buffer_pool_get_type())
#define GST_IS_V4L2_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_V4L2_BUFFER_POOL))
#define GST_V4L2_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_V4L2_BUFFER_POOL, GstV4l2BufferPool))
#define GST_V4L2_BUFFER_POOL_LOCK(pool) g_mutex_lock ((pool)->lock)
#define GST_V4L2_BUFFER_POOL_UNLOCK(pool) g_mutex_unlock ((pool)->lock)
struct _GstV4l2BufferPool
{
GObject parent;
GstBufferPool parent;
GstV4l2Object *obj; /* the v4l2 object */
gboolean requeuebuf; /* if true, unusued buffers are automatically re-QBUF'd */
enum v4l2_buf_type type; /* V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT */
GMutex *lock;
gboolean running; /* with lock */
gint num_live_buffers; /* number of buffers not with driver */
GAsyncQueue* avail_buffers;/* pool of available buffers, not with the driver and which aren't held outside the bufferpool */
gint video_fd; /* a dup(2) of the v4l2object's video_fd */
gboolean requeuebuf; /* if true, unusued buffers are automatically re-QBUF'd */
guint min_buffers;
guint max_buffers;
guint buffer_count;
gint index;
gint num_live_buffers; /* number of buffers not with driver */
GstBuffer **buffers;
};
struct _GstV4l2BufferPoolClass
{
GObjectClass parent_class;
GstBufferPoolClass parent_class;
};
struct _GstMetaV4l2 {
@ -74,24 +71,19 @@ struct _GstMetaV4l2 {
gpointer mem;
struct v4l2_buffer vbuffer;
GstV4l2BufferPool *pool;
};
const GstMetaInfo * gst_meta_v4l2_get_info (void);
#define GST_META_V4L2_GET(buf) ((GstMetaV4l2 *)gst_buffer_get_meta(buf,gst_meta_v4l2_get_info()))
#define GST_META_V4L2_ADD(buf) ((GstMetaV4l2 *)gst_buffer_add_meta(buf,gst_meta_v4l2_get_info(),NULL))
GstV4l2BufferPool * gst_v4l2_buffer_pool_new (GstV4l2Object *obj, gint num_buffers, gboolean requeuebuf);
void gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool);
GType gst_v4l2_buffer_pool_get_type (void);
GstBuffer * gst_v4l2_buffer_pool_get (GstV4l2BufferPool *pool, gboolean blocking);
GstBufferPool * gst_v4l2_buffer_pool_new (GstV4l2Object *obj);
gboolean gst_v4l2_buffer_pool_qbuf (GstBufferPool * bpool, GstBuffer * buf);
gboolean gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool *pool, GstBuffer *buf);
GstBuffer * gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool *pool);
gint gst_v4l2_buffer_pool_available_buffers (GstV4l2BufferPool *pool);
gint gst_v4l2_buffer_pool_available_buffers (GstBufferPool *pool);
G_END_DECLS

View file

@ -2050,24 +2050,27 @@ gst_v4l2_object_setup_pool (GstV4l2Object * v4l2object)
GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);
if (v4l2object->vcap.capabilities & V4L2_CAP_STREAMING) {
gboolean requeuebuf;
guint num_buffers;
GstStructure *config;
requeuebuf = v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* keep track of current number of buffers */
num_buffers = v4l2object->num_buffers;
/* Map the buffers */
GST_LOG_OBJECT (v4l2object->element, "initiating buffer pool");
if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object,
v4l2object->num_buffers, requeuebuf)))
if (!(v4l2object->pool = gst_v4l2_buffer_pool_new (v4l2object)))
goto buffer_pool_new_failed;
GST_INFO_OBJECT (v4l2object->element, "capturing buffers via mmap()");
v4l2object->use_mmap = TRUE;
if (v4l2object->num_buffers != v4l2object->pool->buffer_count) {
v4l2object->num_buffers = v4l2object->pool->buffer_count;
g_object_notify (G_OBJECT (v4l2object->element), "queue-size");
}
config = gst_buffer_pool_get_config (v4l2object->pool);
gst_buffer_pool_config_set (config, NULL, 0, num_buffers, num_buffers,
0, 0);
gst_buffer_pool_set_config (v4l2object->pool, config);
gst_buffer_pool_set_active (v4l2object->pool, TRUE);
} else if (v4l2object->vcap.capabilities & V4L2_CAP_READWRITE) {
GST_INFO_OBJECT (v4l2object->element, "capturing buffers via read()");
@ -2335,22 +2338,18 @@ pool_failed:
gboolean
gst_v4l2_object_start (GstV4l2Object * v4l2object)
{
GstBuffer *buf;
GST_DEBUG_OBJECT (v4l2object->element, "starting");
GST_V4L2_CHECK_OPEN (v4l2object);
GST_V4L2_CHECK_ACTIVE (v4l2object);
if (v4l2object->pool)
if (!gst_buffer_pool_set_active (v4l2object->pool, TRUE))
goto activate_failed;
if (v4l2object->use_mmap) {
switch (v4l2object->type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
/* for capture, queue all the buffers so the device can start filling
* them */
GST_DEBUG_OBJECT (v4l2object->element, "queueing buffers");
while ((buf = gst_v4l2_buffer_pool_get (v4l2object->pool, FALSE)))
if (!gst_v4l2_buffer_pool_qbuf (v4l2object->pool, buf))
goto queue_failed;
break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
/* for output, we assume we already queued a buffer */
@ -2369,14 +2368,10 @@ gst_v4l2_object_start (GstV4l2Object * v4l2object)
return TRUE;
/* ERRORS */
queue_failed:
activate_failed:
{
GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ,
(_("Could not enqueue buffers in device '%s'."),
v4l2object->videodev),
("enqueing buffer %d/%d failed: %s",
GST_META_V4L2_GET (buf)->vbuffer.index, v4l2object->num_buffers,
g_strerror (errno)));
GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ,
(_("Error starting bufferpool")), (NULL));
return FALSE;
}
start_failed:
@ -2412,7 +2407,9 @@ gst_v4l2_object_stop (GstV4l2Object * v4l2object)
}
if (v4l2object->pool) {
gst_v4l2_buffer_pool_destroy (v4l2object->pool);
GST_DEBUG_OBJECT (v4l2object->element, "deactivating pool");
gst_buffer_pool_set_active (v4l2object->pool, FALSE);
gst_object_unref (v4l2object->pool);
v4l2object->pool = NULL;
}
@ -2516,8 +2513,9 @@ cleanup:
static GstFlowReturn
gst_v4l2_object_get_mmap (GstV4l2Object * v4l2object, GstBuffer ** buf)
{
GstFlowReturn res;
#define NUM_TRIALS 50
GstV4l2BufferPool *pool;
GstBufferPool *pool;
gint32 trials = NUM_TRIALS;
GstBuffer *pool_buffer;
gboolean need_copy;
@ -2546,8 +2544,8 @@ gst_v4l2_object_get_mmap (GstV4l2Object * v4l2object, GstBuffer ** buf)
}
}
pool_buffer = gst_v4l2_buffer_pool_dqbuf (pool);
if (pool_buffer == NULL)
res = gst_buffer_pool_acquire_buffer (pool, &pool_buffer, NULL);
if (res != GST_FLOW_OK)
goto no_buffer;
if (v4l2object->size > 0) {
@ -2651,7 +2649,7 @@ gst_v4l2_object_get_buffer (GstV4l2Object * v4l2object, GstBuffer ** buf)
}
break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
ret = GST_FLOW_ERROR;
ret = gst_buffer_pool_acquire_buffer (v4l2object->pool, buf, NULL);
break;
default:
ret = GST_FLOW_ERROR;
@ -2663,18 +2661,21 @@ gst_v4l2_object_get_buffer (GstV4l2Object * v4l2object, GstBuffer ** buf)
GstFlowReturn
gst_v4l2_object_output_buffer (GstV4l2Object * v4l2object, GstBuffer * buf)
{
GstFlowReturn ret;
GstBuffer *newbuf = NULL;
GstMetaV4l2 *meta;
meta = GST_META_V4L2_GET (buf);
if (meta == NULL || meta->pool != v4l2object->pool) {
if (meta == NULL || buf->pool != v4l2object->pool) {
guint8 *data;
gsize size;
/* not our buffer */
GST_DEBUG_OBJECT (v4l2object->element, "slow-path.. need to memcpy");
newbuf = gst_v4l2_buffer_pool_get (v4l2object->pool, TRUE);
ret = gst_buffer_pool_acquire_buffer (v4l2object->pool, &newbuf, NULL);
if (ret != GST_FLOW_OK)
goto acquire_failed;
if (v4l2object->info.finfo) {
GstVideoFrame src_frame, dest_frame;
@ -2710,6 +2711,7 @@ gst_v4l2_object_output_buffer (GstV4l2Object * v4l2object, GstBuffer * buf)
if (!newbuf)
gst_buffer_ref (buf);
#if 0
/* if the driver has more than one buffer, ie. more than just the one we
* just queued, then dequeue one immediately to make it available via
* _buffer_alloc():
@ -2727,9 +2729,15 @@ gst_v4l2_object_output_buffer (GstV4l2Object * v4l2object, GstBuffer * buf)
gst_buffer_unref (v4l2buf);
}
}
#endif
return GST_FLOW_OK;
/* ERRORS */
acquire_failed:
{
GST_DEBUG_OBJECT (v4l2object->element, "could not get buffer from pool");
return ret;
}
queue_failed:
{
GST_DEBUG_OBJECT (v4l2object->element, "failed to queue buffer");

View file

@ -119,7 +119,7 @@ struct _GstV4l2Object {
guint32 min_queued_bufs;
gboolean always_copy;
gboolean use_mmap;
GstV4l2BufferPool *pool;
GstBufferPool *pool;
/* the video device's capabilities */
struct v4l2_capability vcap;