Commit fd13e9e9 authored by Nicolas Dufresne's avatar Nicolas Dufresne

Implement V4l2 Allocator

This goal of this allocator is mainly to allow tracking the memory.
Currently, when a buffer memory has been modified, the buffer and it's
memory is disposed and lost until the stream is restarted.
parent 1b4561cf
......@@ -3,6 +3,7 @@ plugin_LTLIBRARIES = libgstvideo4linux2.la
include $(top_srcdir)/common/gst-glib-gen.mak
libgstvideo4linux2_la_SOURCES = gstv4l2.c \
gstv4l2allocator.c \
gstv4l2colorbalance.c \
gstv4l2devicemonitor.c \
gstv4l2object.c \
......@@ -43,6 +44,7 @@ noinst_HEADERS = \
ext/v4l2-common.h \
ext/v4l2-controls.h \
ext/videodev2.h \
gstv4l2allocator.h \
gstv4l2bufferpool.h \
gstv4l2colorbalance.h \
gstv4l2devicemonitor.h \
......
/*
* Copyright (C) 2014 Collabora Ltd.
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "ext/videodev2.h"
#include "gstv4l2allocator.h"
#include "v4l2_calls.h"
#include <string.h>
#define GST_V4L2_MEMORY_TYPE "V4l2Memory"
#define gst_v4l2_allocator_parent_class parent_class
G_DEFINE_TYPE (GstV4l2Allocator, gst_v4l2_allocator, GST_TYPE_ALLOCATOR);
GST_DEBUG_CATEGORY_STATIC (v4l2allocator_debug);
#define GST_CAT_DEFAULT v4l2allocator_debug
enum
{
GROUP_RELEASED,
LAST_SIGNAL
};
static guint gst_v4l2_allocator_signals[LAST_SIGNAL] = { 0 };
static void gst_v4l2_allocator_release (GstV4l2Allocator * allocator,
GstV4l2Memory * mem);
static const gchar *
memory_type_to_str (guint32 memory)
{
switch (memory) {
case V4L2_MEMORY_MMAP:
return "mmap";
case V4L2_MEMORY_USERPTR:
return "userptr";
case V4L2_MEMORY_DMABUF:
return "dmabuf";
default:
return "unknown";
}
}
/*************************************/
/* GstV4lMemory implementation */
/*************************************/
static gpointer
_v4l2mem_map (GstV4l2Memory * mem, gsize maxsize, GstMapFlags flags)
{
gpointer data = NULL;
switch (mem->group->buffer.memory) {
case V4L2_MEMORY_MMAP:
case V4L2_MEMORY_USERPTR:
data = mem->data;
break;
case V4L2_MEMORY_DMABUF:
/* v4l2 dmabuf memory are not shared with downstream */
g_assert_not_reached ();
break;
default:
GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
break;
}
return data;
}
static gboolean
_v4l2mem_unmap (GstV4l2Memory * mem)
{
gboolean ret = FALSE;
switch (mem->group->buffer.memory) {
case V4L2_MEMORY_MMAP:
case V4L2_MEMORY_USERPTR:
ret = TRUE;
break;
case V4L2_MEMORY_DMABUF:
/* v4l2 dmabuf memory are not share with downstream */
g_assert_not_reached ();
break;
default:
GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
break;
}
return ret;
}
static gboolean
_v4l2mem_dispose (GstV4l2Memory * mem)
{
GstV4l2Allocator *allocator = (GstV4l2Allocator *) mem->mem.allocator;
GstV4l2MemoryGroup *group = mem->group;
gboolean ret;
if (group->mem[mem->plane]) {
gst_memory_ref ((GstMemory *) mem);
gst_v4l2_allocator_release (allocator, mem);
ret = FALSE;
} else {
gst_object_ref (allocator);
ret = TRUE;
}
return ret;
}
static void
_v4l2mem_free (GstV4l2Memory * mem)
{
g_slice_free (GstV4l2Memory, mem);
}
static inline GstV4l2Memory *
_v4l2mem_new (GstMemoryFlags flags, GstAllocator * allocator,
GstMemory * parent, gsize maxsize, gsize align, gsize offset, gsize size,
gint plane, gpointer data, GstV4l2MemoryGroup * group)
{
GstV4l2Memory *mem;
mem = g_slice_new0 (GstV4l2Memory);
gst_memory_init (GST_MEMORY_CAST (mem),
flags, allocator, parent, maxsize, align, offset, size);
if (parent == NULL)
mem->mem.mini_object.dispose =
(GstMiniObjectDisposeFunction) _v4l2mem_dispose;
mem->plane = plane;
mem->data = data;
mem->group = group;
return mem;
}
static GstV4l2Memory *
_v4l2mem_share (GstV4l2Memory * mem, gssize offset, gsize size)
{
GstV4l2Memory *sub;
GstMemory *parent;
/* find the real parent */
if ((parent = mem->mem.parent) == NULL)
parent = (GstMemory *) mem;
if (size == -1)
size = mem->mem.size - offset;
/* the shared memory is always readonly */
sub = _v4l2mem_new (GST_MINI_OBJECT_FLAGS (parent) |
GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
mem->mem.maxsize, mem->mem.align, offset, size, mem->plane, mem->data,
mem->group);
return sub;
}
static gboolean
_v4l2mem_is_span (GstV4l2Memory * mem1, GstV4l2Memory * mem2, gsize * offset)
{
if (offset)
*offset = mem1->mem.offset - mem1->mem.parent->offset;
/* and memory is contiguous */
return mem1->mem.offset + mem1->mem.size == mem2->mem.offset;
}
gboolean
gst_is_v4l2_memory (GstMemory * mem)
{
return gst_memory_is_type (mem, GST_V4L2_MEMORY_TYPE);
}
/*************************************/
/* GstV4l2MemoryGroup implementation */
/*************************************/
static void
gst_v4l2_memory_group_free (GstV4l2MemoryGroup * group)
{
gint i;
for (i = 0; i < group->n_mem; i++) {
GstMemory *mem = group->mem[i];
group->mem[i] = NULL;
if (mem)
gst_memory_unref (mem);
}
g_slice_free (GstV4l2MemoryGroup, group);
}
static GstV4l2MemoryGroup *
gst_v4l2_memory_group_new (GstV4l2Allocator * allocator, guint32 index)
{
gint video_fd = allocator->video_fd;
guint32 memory = allocator->memory;
struct v4l2_format *format = &allocator->format;
GstV4l2MemoryGroup *group;
gsize img_size, buf_size;
group = g_slice_new0 (GstV4l2MemoryGroup);
group->buffer.type = format->type;
group->buffer.index = index;
group->buffer.memory = memory;
if (V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
group->n_mem = group->buffer.length = format->fmt.pix_mp.num_planes;
group->buffer.m.planes = group->planes;
} else {
group->n_mem = 1;
}
if (v4l2_ioctl (video_fd, VIDIOC_QUERYBUF, &group->buffer) < 0)
goto querybuf_failed;
/* Check that provided size matches the format we have negotiation. Failing
* there usually means a driver of libv4l bug. */
if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
gint i;
for (i = 0; i < group->n_mem; i++) {
img_size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
buf_size = group->planes[i].length;
if (buf_size < img_size)
goto buffer_too_short;
}
} else {
img_size = allocator->format.fmt.pix.sizeimage;
buf_size = group->buffer.length;
if (buf_size < img_size)
goto buffer_too_short;
}
/* We save non planar buffer information into the multi-planar plane array
* to avoid duplicating the code later */
if (!V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
group->planes[0].bytesused = group->buffer.bytesused;
group->planes[0].length = group->buffer.length;
g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
}
GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory));
GST_LOG_OBJECT (allocator, " index: %u", group->buffer.index);
GST_LOG_OBJECT (allocator, " type: %d", group->buffer.type);
GST_LOG_OBJECT (allocator, " flags: %08x", group->buffer.flags);
GST_LOG_OBJECT (allocator, " field: %d", group->buffer.field);
GST_LOG_OBJECT (allocator, " memory: %d", group->buffer.memory);
GST_LOG_OBJECT (allocator, " planes: %d", group->n_mem);
#ifndef GST_DISABLE_GST_DEBUG
if (memory == V4L2_MEMORY_MMAP) {
gint i;
for (i = 0; i < group->n_mem; i++) {
GST_LOG_OBJECT (allocator, " [%u] bytesused: %u, length: %u", i,
group->planes[i].bytesused, group->planes[i].length);
GST_LOG_OBJECT (allocator, " [%u] MMAP offset: %u", i,
group->planes[i].m.mem_offset);
}
}
#endif
return group;
querybuf_failed:
{
GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno));
goto failed;
}
buffer_too_short:
{
GST_ERROR ("buffer size %" G_GSIZE_FORMAT
" is smaller then negotiated size %" G_GSIZE_FORMAT
", this is usually the result of a bug in the v4l2 driver or libv4l.",
buf_size, img_size);
goto failed;
}
failed:
gst_v4l2_memory_group_free (group);
return NULL;
}
/*************************************/
/* GstV4lAllocator implementation */
/*************************************/
static void
gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
{
GstV4l2MemoryGroup *group = mem->group;
GST_LOG_OBJECT (allocator, "plane %i of buffer %u released",
mem->plane, group->buffer.index);
/* When all memory are back, put the group back in the free queue */
if (g_atomic_int_dec_and_test (&group->mems_allocated)) {
GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index);
gst_atomic_queue_push (allocator->free_queue, group);
g_signal_emit (allocator, gst_v4l2_allocator_signals[GROUP_RELEASED], 0);
}
/* Keep last, allocator may be freed after this call */
g_object_unref (allocator);
}
static void
gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
{
GstV4l2Allocator *allocator = (GstV4l2Allocator *) gallocator;
GstV4l2Memory *mem = (GstV4l2Memory *) gmem;
GstV4l2MemoryGroup *group = mem->group;
GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u",
mem->plane, group->buffer.index);
switch (allocator->memory) {
case V4L2_MEMORY_MMAP:
if (mem->data) {
munmap (mem->data, group->planes[mem->plane].length);
} else if (group->planes[mem->plane].m.fd > 0) {
close (group->planes[mem->plane].m.fd);
}
break;
default:
/* Nothing to do */
break;
}
_v4l2mem_free (mem);
}
static void
gst_v4l2_allocator_dispose (GObject * obj)
{
GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
gint i;
GST_LOG_OBJECT (obj, "called");
for (i = 0; i < allocator->count; i++) {
GstV4l2MemoryGroup *group = allocator->groups[i];
allocator->groups[i] = NULL;
if (group)
gst_v4l2_memory_group_free (group);
}
G_OBJECT_CLASS (parent_class)->dispose (obj);
}
static void
gst_v4l2_allocator_finalize (GObject * obj)
{
GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
GST_LOG_OBJECT (obj, "called");
v4l2_close (allocator->video_fd);
gst_atomic_queue_unref (allocator->free_queue);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
static void
gst_v4l2_allocator_class_init (GstV4l2AllocatorClass * klass)
{
GObjectClass *object_class;
GstAllocatorClass *allocator_class;
allocator_class = (GstAllocatorClass *) klass;
object_class = (GObjectClass *) klass;
allocator_class->alloc = NULL;
allocator_class->free = gst_v4l2_allocator_free;
object_class->dispose = gst_v4l2_allocator_dispose;
object_class->finalize = gst_v4l2_allocator_finalize;
gst_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new ("group-released",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
G_TYPE_NONE, 0);
GST_DEBUG_CATEGORY_INIT (v4l2allocator_debug, "v4l2allocator", 0,
"V4L2 Allocator");
}
static void
gst_v4l2_allocator_init (GstV4l2Allocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_type = GST_V4L2_MEMORY_TYPE;
alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map;
alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap;
alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share;
alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span;
/* Use the default, fallback copy function */
allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME);
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
#define GST_V4L2_ALLOCATOR_PROBE(obj,type) \
gst_v4l2_allocator_probe ((obj), V4L2_MEMORY_ ## type, \
GST_V4L2_ALLOCATOR_FLAG_ ## type ## _REQBUF, \
GST_V4L2_ALLOCATOR_FLAG_ ## type ## _CREATE_BUF)
static guint32
gst_v4l2_allocator_probe (GstV4l2Allocator * allocator, guint32 memory,
guint32 breq_flag, guint32 bcreate_flag)
{
struct v4l2_requestbuffers breq = { 0 };
guint32 flags = 0;
breq.type = allocator->type;
breq.count = 0;
breq.memory = memory;
if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) == 0) {
struct v4l2_create_buffers bcreate = { 0 };
flags |= breq_flag;
bcreate.memory = V4L2_MEMORY_MMAP;
bcreate.format = allocator->format;
if ((v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) == 0))
flags |= bcreate_flag;
}
return flags;
}
GstV4l2Allocator *
gst_v4l2_allocator_new (GstObject * parent, gint video_fd,
struct v4l2_format * format)
{
GstV4l2Allocator *allocator;
guint32 flags = 0;
gchar *name, *parent_name;
parent_name = gst_object_get_name (parent);
name = g_strconcat (parent_name, ":allocator", NULL);
g_free (parent_name);
allocator = g_object_new (GST_TYPE_V4L2_ALLOCATOR, "name", name, NULL);
g_free (name);
/* Save everything */
allocator->video_fd = v4l2_dup (video_fd);
allocator->type = format->type;
allocator->format = *format;
flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, MMAP);
flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, USERPTR);
flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, DMABUF);
if (flags == 0)
goto not_supported;
return allocator;
not_supported:
{
GST_ERROR_OBJECT (allocator,
"No memory model supported by GStreamer for this device");
g_object_unref (allocator);
return NULL;
}
}
guint
gst_v4l2_allocator_start (GstV4l2Allocator * allocator, guint32 count,
guint32 memory)
{
struct v4l2_requestbuffers breq = { count, allocator->type, memory };
gboolean can_allocate;
gint i;
g_return_val_if_fail (count != 0, 0);
GST_OBJECT_LOCK (allocator);
if (allocator->active)
goto already_active;
if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
goto reqbufs_failed;
if (breq.count < 1)
goto out_of_memory;
switch (memory) {
case V4L2_MEMORY_MMAP:
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, MMAP);
break;
case V4L2_MEMORY_USERPTR:
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, USERPTR);
break;
case V4L2_MEMORY_DMABUF:
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, DMABUF);
break;
default:
can_allocate = FALSE;
break;
}
GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested",
breq.count, memory_type_to_str (memory), count);
allocator->can_allocate = can_allocate;
allocator->count = breq.count;
allocator->memory = memory;
/* Create memory groups */
for (i = 0; i < allocator->count; i++) {
allocator->groups[i] = gst_v4l2_memory_group_new (allocator, i);
if (allocator->groups[i] == NULL)
goto error;
gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]);
}
g_atomic_int_set (&allocator->active, TRUE);
done:
GST_OBJECT_UNLOCK (allocator);
return breq.count;
already_active:
{
GST_ERROR_OBJECT (allocator,
"error requesting %d buffers: %s", count, g_strerror (errno));
goto error;
}
reqbufs_failed:
{
GST_ERROR_OBJECT (allocator,
"error requesting %d buffers: %s", count, g_strerror (errno));
goto error;
}
out_of_memory:
{
GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers");
goto error;
}
error:
{
breq.count = 0;
goto done;
}
}
GstV4l2Return
gst_v4l2_allocator_stop (GstV4l2Allocator * allocator)
{
struct v4l2_requestbuffers breq = { 0, allocator->type, allocator->memory };
gint i = 0;
GstV4l2Return ret = GST_V4L2_OK;
GST_DEBUG_OBJECT (allocator, "stop allocator");
GST_OBJECT_LOCK (allocator);
if (!allocator->active)
goto done;
if (gst_atomic_queue_length (allocator->free_queue) != allocator->count) {
GST_DEBUG_OBJECT (allocator, "allocator is still in use");
ret = GST_V4L2_BUSY;
goto done;
}
while (gst_atomic_queue_pop (allocator->free_queue)) {
/* nothing */
};
for (i = 0; i < allocator->count; i++) {
GstV4l2MemoryGroup *group = allocator->groups[i];
allocator->groups[i] = NULL;
if (group)
gst_v4l2_memory_group_free (group);
}
if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
goto reqbufs_failed;