2009-08-04 07:14:20 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
* 2006 Edgard Lima <edgard.lima@indt.org.br>
|
|
|
|
* 2009 Texas Instruments, Inc - http://www.ti.com/
|
|
|
|
*
|
2010-01-18 12:55:38 +00:00
|
|
|
* gstv4l2bufferpool.c V4L2 buffer pool class
|
2009-08-04 07:14:20 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-08-04 07:14:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-04 11:34:09 +00:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
# define _GNU_SOURCE /* O_CLOEXEC */
|
|
|
|
#endif
|
2013-02-19 10:47:20 +00:00
|
|
|
#include <fcntl.h>
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2013-10-04 11:34:09 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-11-04 18:31:45 +00:00
|
|
|
#include "gst/video/video.h"
|
2011-10-31 01:24:04 +00:00
|
|
|
#include "gst/video/gstvideometa.h"
|
2011-07-29 15:21:03 +00:00
|
|
|
#include "gst/video/gstvideopool.h"
|
2013-02-19 10:47:20 +00:00
|
|
|
#include "gst/allocators/gstdmabuf.h"
|
2010-11-04 18:31:45 +00:00
|
|
|
|
2009-08-04 07:14:20 +00:00
|
|
|
#include <gstv4l2bufferpool.h>
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2009-08-04 07:14:20 +00:00
|
|
|
#include "v4l2_calls.h"
|
|
|
|
#include "gst/gst-i18n-plugin.h"
|
2011-12-12 02:30:45 +00:00
|
|
|
#include <gst/glib-compat-private.h>
|
2009-08-04 07:14:20 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
|
|
|
|
#define GST_CAT_DEFAULT v4l2_debug
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GstV4l2Buffer:
|
|
|
|
*/
|
2012-02-29 16:26:01 +00:00
|
|
|
GType
|
|
|
|
gst_v4l2_meta_api_get_type (void)
|
|
|
|
{
|
|
|
|
static volatile GType type;
|
2013-08-21 22:03:48 +00:00
|
|
|
static const gchar *tags[] =
|
|
|
|
{ GST_META_TAG_VIDEO_STR, GST_META_TAG_MEMORY_STR, NULL };
|
2012-02-29 16:26:01 +00:00
|
|
|
|
|
|
|
if (g_once_init_enter (&type)) {
|
|
|
|
GType _type = gst_meta_api_type_register ("GstV4l2MetaAPI", tags);
|
|
|
|
g_once_init_leave (&type, _type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:16:52 +00:00
|
|
|
const GstMetaInfo *
|
2011-10-31 01:24:04 +00:00
|
|
|
gst_v4l2_meta_get_info (void)
|
2011-02-28 09:16:52 +00:00
|
|
|
{
|
|
|
|
static const GstMetaInfo *meta_info = NULL;
|
|
|
|
|
2012-10-03 17:51:45 +00:00
|
|
|
if (g_once_init_enter (&meta_info)) {
|
|
|
|
const GstMetaInfo *meta =
|
2012-02-29 16:26:01 +00:00
|
|
|
gst_meta_register (gst_v4l2_meta_api_get_type (), "GstV4l2Meta",
|
2011-10-31 01:24:04 +00:00
|
|
|
sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
|
2012-02-29 16:26:01 +00:00
|
|
|
(GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
|
2012-10-03 17:51:45 +00:00
|
|
|
g_once_init_leave (&meta_info, meta);
|
2011-02-28 09:16:52 +00:00
|
|
|
}
|
|
|
|
return meta_info;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
/*
|
|
|
|
* GstV4l2BufferPool:
|
|
|
|
*/
|
|
|
|
#define gst_v4l2_buffer_pool_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstV4l2BufferPool, gst_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL);
|
|
|
|
|
2011-07-15 11:35:14 +00:00
|
|
|
static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
|
|
|
|
GstBuffer * buffer);
|
|
|
|
|
2009-08-04 07:14:20 +00:00
|
|
|
static void
|
2011-07-15 09:27:18 +00:00
|
|
|
gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2011-07-15 09:27:18 +00:00
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
2011-07-12 16:13:42 +00:00
|
|
|
GstV4l2Object *obj;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-12 16:13:42 +00:00
|
|
|
obj = pool->obj;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
{
|
2011-10-31 01:24:04 +00:00
|
|
|
GstV4l2Meta *meta;
|
2011-07-18 16:54:49 +00:00
|
|
|
gint index;
|
2013-11-19 17:16:27 +00:00
|
|
|
gint i = 0;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-10-31 01:24:04 +00:00
|
|
|
meta = GST_V4L2_META_GET (buffer);
|
2011-07-18 16:54:49 +00:00
|
|
|
g_assert (meta != NULL);
|
|
|
|
|
|
|
|
index = meta->vbuffer.index;
|
|
|
|
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
2013-11-13 12:05:40 +00:00
|
|
|
GST_LOG_OBJECT (pool,
|
2013-11-19 17:16:27 +00:00
|
|
|
"unmap multiplanar buffer %p idx %d (data %p, len %u, plane %u)",
|
|
|
|
buffer, index, meta->mem[i], meta->vplanes[i].length, i);
|
2013-11-13 12:05:40 +00:00
|
|
|
|
2013-11-19 17:16:27 +00:00
|
|
|
v4l2_munmap (meta->mem[i], meta->vplanes[i].length);
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
pool->buffers[index] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
gst_buffer_unref (buffer);
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
|
2012-03-15 13:06:40 +00:00
|
|
|
GstBufferPoolAcquireParams * params)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2011-07-15 09:27:18 +00:00
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
GstBuffer *newbuf;
|
2011-10-31 01:24:04 +00:00
|
|
|
GstV4l2Meta *meta;
|
2011-07-12 16:13:42 +00:00
|
|
|
GstV4l2Object *obj;
|
2011-07-13 11:37:58 +00:00
|
|
|
GstVideoInfo *info;
|
2014-04-15 19:07:23 +00:00
|
|
|
GstVideoAlignment *align;
|
2011-07-15 09:27:18 +00:00
|
|
|
guint index;
|
2013-11-19 17:16:27 +00:00
|
|
|
gint i;
|
2011-07-12 16:13:42 +00:00
|
|
|
|
|
|
|
obj = pool->obj;
|
2011-07-13 11:37:58 +00:00
|
|
|
info = &obj->info;
|
2014-04-15 19:07:23 +00:00
|
|
|
align = &obj->align;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
{
|
|
|
|
newbuf =
|
2012-03-15 12:36:17 +00:00
|
|
|
gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_V4L2_IO_MMAP:
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
2013-05-02 07:41:01 +00:00
|
|
|
if (pool->num_allocated == pool->num_buffers) {
|
|
|
|
struct v4l2_create_buffers create_bufs;
|
|
|
|
|
|
|
|
memset (&create_bufs, 0, sizeof (struct v4l2_create_buffers));
|
|
|
|
create_bufs.count = 1;
|
|
|
|
create_bufs.memory = V4L2_MEMORY_MMAP;
|
2014-03-27 22:41:07 +00:00
|
|
|
create_bufs.format = obj->format;
|
2013-05-02 07:41:01 +00:00
|
|
|
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_CREATE_BUFS, &create_bufs) < 0)
|
|
|
|
goto create_bufs_failed;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "created buffer with index: %u",
|
|
|
|
create_bufs.index);
|
|
|
|
pool->num_buffers++;
|
|
|
|
pool->buffers = g_renew (GstBuffer *, pool->buffers, pool->num_buffers);
|
|
|
|
pool->buffers[pool->num_buffers - 1] = NULL;
|
|
|
|
}
|
2014-03-15 10:13:05 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
newbuf = gst_buffer_new ();
|
2011-10-31 01:24:04 +00:00
|
|
|
meta = GST_V4L2_META_ADD (newbuf);
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-07-25 13:47:05 +00:00
|
|
|
index = pool->num_allocated;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-11-22 01:40:39 +00:00
|
|
|
GST_LOG_OBJECT (pool, "creating buffer %u, %p", index, newbuf);
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* prepare the buffer */
|
2013-11-08 11:09:21 +00:00
|
|
|
memset (&meta->vbuffer, 0x0, sizeof (struct v4l2_buffer));
|
2011-07-18 16:54:49 +00:00
|
|
|
meta->vbuffer.index = index;
|
|
|
|
meta->vbuffer.type = obj->type;
|
|
|
|
meta->vbuffer.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
2013-11-19 17:16:27 +00:00
|
|
|
/* main information */
|
|
|
|
meta->n_planes = obj->n_v4l2_planes;
|
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* prepare the planes of the buffer */
|
|
|
|
if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
|
|
|
|
/* length is the number of elements in the
|
|
|
|
* vplanes array */
|
|
|
|
meta->vbuffer.length = obj->n_v4l2_planes;
|
|
|
|
meta->vbuffer.m.planes = meta->vplanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the buffer is prepared, now fill in it with meaningful values */
|
2011-07-18 16:54:49 +00:00
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_QUERYBUF, &meta->vbuffer) < 0)
|
|
|
|
goto querybuf_failed;
|
|
|
|
|
2013-11-19 17:16:27 +00:00
|
|
|
/* in non MPLANE mode we emulate one plane in order to
|
|
|
|
* factorize the code */
|
|
|
|
if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
|
|
|
|
/* here meta->n_planes == 1 */
|
|
|
|
meta->vplanes[0].length = meta->vbuffer.length;
|
|
|
|
meta->vplanes[0].bytesused = meta->vbuffer.bytesused;
|
|
|
|
meta->vplanes[0].m.mem_offset = meta->vbuffer.m.offset;
|
|
|
|
meta->vplanes[0].data_offset = 0;
|
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
GST_LOG_OBJECT (pool, " index: %u", meta->vbuffer.index);
|
|
|
|
GST_LOG_OBJECT (pool, " type: %d", meta->vbuffer.type);
|
|
|
|
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);
|
2013-11-19 17:16:27 +00:00
|
|
|
GST_LOG_OBJECT (pool, " planes: %d", meta->n_planes);
|
2013-11-13 12:05:40 +00:00
|
|
|
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (meta->vbuffer.memory == V4L2_MEMORY_MMAP) {
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
|
|
|
GST_LOG_OBJECT (pool, " bytesused: %u, plane: %u",
|
|
|
|
meta->vplanes[i].bytesused, i);
|
|
|
|
GST_LOG_OBJECT (pool, " MMAP offset: %u, plane: %u",
|
|
|
|
meta->vplanes[i].m.mem_offset, i);
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2013-02-19 10:47:20 +00:00
|
|
|
if (obj->mode == GST_V4L2_IO_MMAP) {
|
2013-11-19 17:16:27 +00:00
|
|
|
/* append one gstmemory for each plane */
|
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
|
|
|
meta->mem[i] = v4l2_mmap (0, meta->vplanes[i].length,
|
2013-11-13 12:05:40 +00:00
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
|
2013-11-19 17:16:27 +00:00
|
|
|
meta->vplanes[i].m.mem_offset);
|
|
|
|
if (meta->mem[i] == MAP_FAILED)
|
2013-11-13 12:05:40 +00:00
|
|
|
goto mmap_failed;
|
|
|
|
|
2013-11-19 17:16:27 +00:00
|
|
|
GST_LOG_OBJECT (pool, " buffer length %d, data offset %d, plane %d",
|
|
|
|
meta->vplanes[i].length, meta->vplanes[i].data_offset, i);
|
2013-11-13 12:05:40 +00:00
|
|
|
|
|
|
|
gst_buffer_append_memory (newbuf,
|
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
|
2013-11-19 17:16:27 +00:00
|
|
|
meta->mem[i], meta->vplanes[i].length,
|
|
|
|
meta->vplanes[i].data_offset,
|
|
|
|
meta->vplanes[i].length, NULL, NULL));
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
2013-02-19 10:47:20 +00:00
|
|
|
}
|
2014-03-15 10:13:05 +00:00
|
|
|
|
2013-02-19 10:47:20 +00:00
|
|
|
if (obj->mode == GST_V4L2_IO_DMABUF) {
|
|
|
|
struct v4l2_exportbuffer expbuf;
|
|
|
|
|
2013-04-30 11:07:37 +00:00
|
|
|
memset (&expbuf, 0, sizeof (struct v4l2_exportbuffer));
|
2013-02-19 10:47:20 +00:00
|
|
|
expbuf.type = meta->vbuffer.type;
|
|
|
|
expbuf.index = meta->vbuffer.index;
|
|
|
|
expbuf.flags = O_CLOEXEC;
|
2013-11-21 12:29:28 +00:00
|
|
|
|
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
|
|
|
expbuf.plane = i;
|
|
|
|
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
|
|
|
|
goto expbuf_failed;
|
|
|
|
|
|
|
|
gst_buffer_append_memory (newbuf,
|
|
|
|
gst_dmabuf_allocator_alloc (pool->allocator, expbuf.fd,
|
|
|
|
meta->vplanes[i].length));
|
|
|
|
}
|
2013-02-19 10:47:20 +00:00
|
|
|
|
|
|
|
meta->vbuffer.memory = V4L2_MEMORY_DMABUF;
|
2013-11-21 12:29:28 +00:00
|
|
|
|
|
|
|
/* in non-MPLANE mode our meta is not automatically updated
|
|
|
|
* because the plane is emulated (not referenced by
|
|
|
|
* meta->vbuffer) */
|
|
|
|
if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type))
|
|
|
|
meta->vplanes[0].m.fd = meta->vbuffer.m.fd;
|
2013-02-19 10:47:20 +00:00
|
|
|
}
|
2014-03-15 10:13:05 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
/* add metadata to raw video buffers */
|
2011-07-27 11:41:28 +00:00
|
|
|
if (pool->add_videometa && info->finfo) {
|
2012-06-26 09:14:59 +00:00
|
|
|
const GstVideoFormatInfo *finfo = info->finfo;
|
2011-07-18 16:54:49 +00:00
|
|
|
gsize offset[GST_VIDEO_MAX_PLANES];
|
2013-11-13 12:05:40 +00:00
|
|
|
gint width, height, n_gst_planes, offs, i, stride[GST_VIDEO_MAX_PLANES];
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2012-06-26 09:14:59 +00:00
|
|
|
width = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
height = GST_VIDEO_INFO_HEIGHT (info);
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* n_gst_planes is the number of planes
|
|
|
|
* (RGB: 1, YUY2: 1, NV12: 2, I420: 3)
|
|
|
|
* It's greater or equal than the number of v4l2 planes. */
|
|
|
|
n_gst_planes = GST_VIDEO_INFO_N_PLANES (info);
|
2012-06-26 09:14:59 +00:00
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* the basic are common between MPLANE mode and non MPLANE mode
|
|
|
|
* except a special case inside the loop at the end
|
|
|
|
*/
|
2012-06-26 09:14:59 +00:00
|
|
|
offs = 0;
|
2013-11-13 12:05:40 +00:00
|
|
|
for (i = 0; i < n_gst_planes; i++) {
|
|
|
|
GST_DEBUG_OBJECT (pool, "adding video meta, bytesperline %d",
|
|
|
|
obj->bytesperline[i]);
|
|
|
|
|
2012-06-26 09:14:59 +00:00
|
|
|
offset[i] = offs;
|
2013-12-10 19:29:55 +00:00
|
|
|
|
|
|
|
if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
|
|
|
|
guint x_tiles, y_tiles, ws, hs, tile_height;
|
|
|
|
|
|
|
|
ws = GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
|
|
|
|
hs = GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
|
|
|
|
tile_height = 1 << hs;
|
|
|
|
|
|
|
|
x_tiles = obj->bytesperline[i] >> ws;
|
|
|
|
y_tiles = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
|
|
|
|
GST_ROUND_UP_N (height, tile_height) >> hs);
|
|
|
|
stride[i] = GST_VIDEO_TILE_MAKE_STRIDE (x_tiles, y_tiles);
|
|
|
|
} else {
|
|
|
|
stride[i] = obj->bytesperline[i];
|
|
|
|
}
|
2013-09-09 12:44:58 +00:00
|
|
|
|
2013-12-04 21:23:18 +00:00
|
|
|
/* when using multiplanar mode and if there is more then one v4l
|
|
|
|
* plane for each gst plane
|
2013-11-13 12:05:40 +00:00
|
|
|
*/
|
2013-11-19 17:16:27 +00:00
|
|
|
if (V4L2_TYPE_IS_MULTIPLANAR (obj->type) && meta->n_planes > 1)
|
2013-11-13 12:05:40 +00:00
|
|
|
/* non_contiguous case here so we have to make sure that gst goes to the
|
|
|
|
* next plane (using default gstvideometa.c::default_map).
|
|
|
|
* And the next plane is after length bytes of the previous one from
|
|
|
|
* the gst buffer point of view. */
|
|
|
|
offs += meta->vplanes[i].length;
|
|
|
|
else
|
2013-12-10 19:29:55 +00:00
|
|
|
offs += obj->bytesperline[i] *
|
|
|
|
GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, height);
|
2012-06-26 09:14:59 +00:00
|
|
|
}
|
2013-12-10 19:29:55 +00:00
|
|
|
|
2012-04-19 12:16:01 +00:00
|
|
|
gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
|
2013-11-13 12:05:40 +00:00
|
|
|
GST_VIDEO_INFO_FORMAT (info), width, height, n_gst_planes,
|
2011-07-18 16:54:49 +00:00
|
|
|
offset, stride);
|
|
|
|
}
|
2014-04-15 19:07:23 +00:00
|
|
|
|
|
|
|
if (pool->add_cropmeta) {
|
|
|
|
GstVideoCropMeta *crop;
|
|
|
|
crop = gst_buffer_add_video_crop_meta (newbuf);
|
|
|
|
crop->x = align->padding_left;
|
|
|
|
crop->y = align->padding_top;
|
|
|
|
crop->width = info->width;
|
|
|
|
crop->width = info->height;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
2012-08-08 10:31:59 +00:00
|
|
|
newbuf = NULL;
|
2011-07-18 16:54:49 +00:00
|
|
|
g_assert_not_reached ();
|
2011-07-13 11:37:58 +00:00
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-25 13:47:05 +00:00
|
|
|
pool->num_allocated++;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
|
|
|
*buffer = newbuf;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
2013-05-02 07:41:01 +00:00
|
|
|
create_bufs_failed:
|
|
|
|
{
|
|
|
|
gint errnosave = errno;
|
|
|
|
|
|
|
|
GST_WARNING ("Failed CREATE_BUFS: %s", g_strerror (errnosave));
|
|
|
|
errno = errnosave;
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
querybuf_failed:
|
|
|
|
{
|
|
|
|
gint errnosave = errno;
|
|
|
|
|
|
|
|
GST_WARNING ("Failed QUERYBUF: %s", g_strerror (errnosave));
|
2011-07-15 09:27:18 +00:00
|
|
|
gst_buffer_unref (newbuf);
|
2009-08-04 07:14:20 +00:00
|
|
|
errno = errnosave;
|
2011-07-15 09:27:18 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
mmap_failed:
|
|
|
|
{
|
|
|
|
gint errnosave = errno;
|
|
|
|
|
|
|
|
GST_WARNING ("Failed to mmap: %s", g_strerror (errnosave));
|
2011-07-15 09:27:18 +00:00
|
|
|
gst_buffer_unref (newbuf);
|
2009-08-04 07:14:20 +00:00
|
|
|
errno = errnosave;
|
2011-07-15 09:27:18 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
2013-04-30 11:07:37 +00:00
|
|
|
expbuf_failed:
|
|
|
|
{
|
|
|
|
gint errnosave = errno;
|
|
|
|
|
|
|
|
GST_WARNING ("Failed EXPBUF: %s", g_strerror (errnosave));
|
|
|
|
gst_buffer_unref (newbuf);
|
|
|
|
errno = errnosave;
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2011-07-15 09:27:18 +00:00
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
2011-07-27 11:41:28 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2012-03-27 10:44:46 +00:00
|
|
|
GstCaps *caps;
|
2013-12-02 23:05:11 +00:00
|
|
|
guint size, min_buffers, max_buffers;
|
2012-03-15 21:11:17 +00:00
|
|
|
GstAllocator *allocator;
|
|
|
|
GstAllocationParams params;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "set config");
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-27 11:41:28 +00:00
|
|
|
pool->add_videometa =
|
2011-07-29 15:21:03 +00:00
|
|
|
gst_buffer_pool_config_has_option (config,
|
2011-10-31 01:24:04 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META);
|
2011-07-27 11:41:28 +00:00
|
|
|
|
2014-03-18 19:49:49 +00:00
|
|
|
if (!pool->add_videometa && obj->need_video_meta)
|
|
|
|
goto missing_video_api;
|
2011-07-27 11:41:28 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
/* parse the config and keep around */
|
2012-03-15 21:11:17 +00:00
|
|
|
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
|
|
|
|
&max_buffers))
|
|
|
|
goto wrong_config;
|
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
/* FIXME Check alignement, and S_FMT with new size if different */
|
|
|
|
|
2012-03-15 21:11:17 +00:00
|
|
|
if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms))
|
2011-07-15 09:27:18 +00:00
|
|
|
goto wrong_config;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
if (obj->mode == GST_V4L2_IO_DMABUF)
|
|
|
|
allocator = gst_dmabuf_allocator_new ();
|
2014-01-17 06:46:09 +00:00
|
|
|
else if (allocator)
|
|
|
|
gst_object_ref (allocator);
|
2013-12-02 23:05:11 +00:00
|
|
|
|
|
|
|
if (pool->allocator)
|
|
|
|
gst_object_unref (pool->allocator);
|
2014-01-17 06:46:09 +00:00
|
|
|
pool->allocator = allocator;
|
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
pool->params = params;
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
|
|
|
|
max_buffers);
|
|
|
|
|
|
|
|
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
missing_video_api:
|
|
|
|
{
|
2014-03-18 19:49:49 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "missing GstMetaVideo API in config");
|
2013-12-02 23:05:11 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
wrong_config:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
start_streaming (GstV4l2BufferPool * pool)
|
|
|
|
{
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
GST_DEBUG_OBJECT (pool, "STREAMON");
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
|
|
|
|
goto start_failed;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->streaming = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
start_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
|
|
|
|
g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
GstStructure *config;
|
|
|
|
GstCaps *caps;
|
|
|
|
guint size, num_buffers, min_buffers, max_buffers, copy_threshold;
|
|
|
|
struct v4l2_requestbuffers breq;
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (bpool);
|
|
|
|
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
|
|
|
|
&max_buffers))
|
|
|
|
goto wrong_config;
|
|
|
|
|
2012-04-23 16:01:31 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* we preallocate 1 buffer, this value also instructs the latency
|
|
|
|
* calculation to have 1 frame latency max */
|
|
|
|
num_buffers = 1;
|
|
|
|
copy_threshold = 0;
|
|
|
|
break;
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2012-04-23 16:01:31 +00:00
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
{
|
|
|
|
/* request a reasonable number of buffers when no max specified. We will
|
|
|
|
* copy when we run out of buffers */
|
|
|
|
if (max_buffers == 0)
|
2013-12-06 00:34:44 +00:00
|
|
|
num_buffers = MAX (4, min_buffers);
|
2012-04-23 16:01:31 +00:00
|
|
|
else
|
|
|
|
num_buffers = 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;
|
|
|
|
}
|
|
|
|
/* update min buffers with the amount of buffers we just reserved. We need
|
|
|
|
* to configure this value in the bufferpool so that the default start
|
|
|
|
* implementation calls our allocate function */
|
|
|
|
min_buffers = breq.count;
|
|
|
|
|
|
|
|
if (max_buffers == 0 || num_buffers < max_buffers) {
|
|
|
|
/* if we are asked to provide more buffers than we have allocated, start
|
|
|
|
* copying buffers when we only have 2 buffers left in the pool */
|
|
|
|
copy_threshold = 2;
|
|
|
|
} else {
|
|
|
|
/* we are certain that we have enough buffers so we don't need to
|
|
|
|
* copy */
|
|
|
|
copy_threshold = 0;
|
|
|
|
}
|
2013-12-02 23:05:11 +00:00
|
|
|
|
|
|
|
/* FIXME try to call CREATEBUFS with count 0 to check if max shall
|
|
|
|
* remain 0 */
|
2012-04-23 16:01:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
|
|
|
num_buffers = 0;
|
|
|
|
copy_threshold = 0;
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
pool->size = size;
|
2012-04-23 16:01:31 +00:00
|
|
|
pool->num_buffers = num_buffers;
|
|
|
|
pool->copy_threshold = copy_threshold;
|
2013-02-19 10:47:20 +00:00
|
|
|
|
2012-03-15 21:11:17 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
|
|
|
|
max_buffers);
|
2013-12-02 23:05:11 +00:00
|
|
|
GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
|
2011-07-19 15:59:55 +00:00
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
pool->obj = obj;
|
|
|
|
pool->buffers = g_new0 (GstBuffer *, pool->num_buffers);
|
|
|
|
pool->num_allocated = 0;
|
|
|
|
|
|
|
|
/* now, allocate the buffers: */
|
|
|
|
if (!GST_BUFFER_POOL_CLASS (parent_class)->start (bpool))
|
|
|
|
goto start_failed;
|
|
|
|
|
|
|
|
/* we can start capturing now, we wait for the playback case until we queued
|
|
|
|
* the first buffer */
|
|
|
|
if (!V4L2_TYPE_IS_OUTPUT (obj->type))
|
|
|
|
if (!start_streaming (pool))
|
|
|
|
goto start_failed;
|
|
|
|
|
|
|
|
gst_poll_set_flushing (obj->poll, FALSE);
|
|
|
|
|
|
|
|
return TRUE;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-27 11:41:28 +00:00
|
|
|
/* ERRORS */
|
2011-07-15 09:27:18 +00:00
|
|
|
wrong_config:
|
|
|
|
{
|
2011-07-27 11:41:28 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
|
2011-07-15 09:27:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-04-23 16:01:31 +00:00
|
|
|
reqbufs_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool,
|
|
|
|
"error requesting %d buffers: %s", num_buffers, g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_buffers:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool,
|
|
|
|
"we received %d from device '%s', we want at least %d",
|
|
|
|
breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
start_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "failed to start streaming");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 15:07:10 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_free_buffers (GstV4l2BufferPool * pool)
|
|
|
|
{
|
|
|
|
if (pool->num_buffers > 0) {
|
|
|
|
struct v4l2_requestbuffers breq;
|
|
|
|
memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
|
|
|
|
breq.type = pool->obj->type;
|
|
|
|
breq.count = 0;
|
|
|
|
breq.memory = V4L2_MEMORY_MMAP;
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0) {
|
|
|
|
GST_ERROR_OBJECT (pool, "error releasing buffers: %s",
|
|
|
|
g_strerror (errno));
|
|
|
|
}
|
|
|
|
pool->num_buffers = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:37:23 +00:00
|
|
|
static gboolean
|
|
|
|
stop_streaming (GstV4l2BufferPool * pool)
|
|
|
|
{
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "stopping stream");
|
|
|
|
|
|
|
|
gst_poll_set_flushing (obj->poll, TRUE);
|
|
|
|
|
2014-01-08 16:51:21 +00:00
|
|
|
if (!pool->streaming) {
|
|
|
|
/* it avoid error: STREAMOFF 22 (Invalid argument) when
|
|
|
|
* attempting to stop a stream not previously started */
|
|
|
|
GST_DEBUG_OBJECT (pool, "no need to stop, was not previously started");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:37:23 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
GST_DEBUG_OBJECT (pool, "STREAMOFF");
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
|
|
|
|
goto stop_failed;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->streaming = FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
stop_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
|
|
|
|
g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2011-07-15 12:52:38 +00:00
|
|
|
gboolean ret;
|
2011-07-15 09:27:18 +00:00
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
2011-07-18 16:54:49 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2011-07-15 09:27:18 +00:00
|
|
|
guint n;
|
2011-02-24 12:51:32 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "stopping pool");
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-25 13:38:38 +00:00
|
|
|
gst_poll_set_flushing (obj->poll, TRUE);
|
|
|
|
|
2011-07-19 17:25:29 +00:00
|
|
|
if (pool->streaming) {
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-19 17:25:29 +00:00
|
|
|
/* we actually need to sync on all queued buffers but not
|
|
|
|
* on the non-queued ones */
|
|
|
|
GST_DEBUG_OBJECT (pool, "STREAMOFF");
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
|
|
|
|
goto stop_failed;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pool->streaming = FALSE;
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 12:52:38 +00:00
|
|
|
/* first free the buffers in the queue */
|
|
|
|
ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
|
|
|
|
|
|
|
|
/* then free the remaining buffers */
|
2013-04-03 09:09:50 +00:00
|
|
|
for (n = 0; n < pool->num_buffers; n++) {
|
|
|
|
if (pool->buffers[n])
|
|
|
|
gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
|
2011-07-15 12:07:11 +00:00
|
|
|
}
|
2012-06-26 11:19:02 +00:00
|
|
|
pool->num_queued = 0;
|
2011-08-04 11:50:01 +00:00
|
|
|
g_free (pool->buffers);
|
|
|
|
pool->buffers = NULL;
|
|
|
|
|
2013-06-03 15:07:10 +00:00
|
|
|
gst_v4l2_buffer_pool_free_buffers (pool);
|
2013-05-06 14:54:02 +00:00
|
|
|
|
2011-07-15 12:52:38 +00:00
|
|
|
return ret;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
stop_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
|
|
|
|
g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_object_poll (GstV4l2Object * v4l2object)
|
|
|
|
{
|
|
|
|
gint ret;
|
|
|
|
|
|
|
|
if (v4l2object->can_poll_device) {
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_LOG_OBJECT (v4l2object->element, "polling device");
|
2011-07-18 16:54:49 +00:00
|
|
|
ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
|
|
|
|
if (G_UNLIKELY (ret < 0)) {
|
|
|
|
if (errno == EBUSY)
|
|
|
|
goto stopped;
|
|
|
|
if (errno == ENXIO) {
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_WARNING_OBJECT (v4l2object->element,
|
2011-07-18 16:54:49 +00:00
|
|
|
"v4l2 device doesn't support polling. Disabling");
|
|
|
|
v4l2object->can_poll_device = FALSE;
|
|
|
|
} else {
|
|
|
|
if (errno != EAGAIN && errno != EINTR)
|
|
|
|
goto select_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
stopped:
|
|
|
|
{
|
|
|
|
GST_DEBUG ("stop called");
|
2012-02-08 15:34:00 +00:00
|
|
|
return GST_FLOW_FLUSHING;
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
|
|
|
select_error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
|
|
|
|
("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-07-19 15:59:55 +00:00
|
|
|
gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
2011-10-31 01:24:04 +00:00
|
|
|
GstV4l2Meta *meta;
|
2011-07-18 16:54:49 +00:00
|
|
|
gint index;
|
2013-11-19 17:16:27 +00:00
|
|
|
gint i = 0;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-10-31 01:24:04 +00:00
|
|
|
meta = GST_V4L2_META_GET (buf);
|
2012-04-23 16:01:31 +00:00
|
|
|
if (meta == NULL) {
|
|
|
|
GST_LOG_OBJECT (pool, "unref copied buffer %p", buf);
|
|
|
|
/* no meta, it was a copied buffer that we can unref */
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
index = meta->vbuffer.index;
|
2013-11-13 12:05:40 +00:00
|
|
|
|
|
|
|
/* this field is common to MPLANE and not MPLANE */
|
2012-08-20 07:52:32 +00:00
|
|
|
meta->vbuffer.bytesused = gst_buffer_get_size (buf);
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
|
|
|
meta->vplanes[i].bytesused =
|
|
|
|
gst_buffer_get_sizes_range (buf, i, 1, NULL, NULL);
|
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
GST_LOG_OBJECT (pool,
|
2013-11-19 17:16:27 +00:00
|
|
|
"enqueue buffer %p, index:%d, queued:%d, flags:%08x mem:%p used:%d, plane:%d",
|
2013-11-13 12:05:40 +00:00
|
|
|
buf, index, pool->num_queued, meta->vbuffer.flags,
|
2013-11-19 17:16:27 +00:00
|
|
|
meta->mem[i], meta->vplanes[i].bytesused, i);
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
if (pool->buffers[index] != NULL)
|
|
|
|
goto already_queued;
|
|
|
|
|
2012-04-23 16:01:31 +00:00
|
|
|
GST_LOG_OBJECT (pool, "doing QBUF");
|
2011-07-18 16:54:49 +00:00
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
|
|
|
|
goto queue_failed;
|
|
|
|
|
|
|
|
pool->buffers[index] = buf;
|
|
|
|
pool->num_queued++;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
already_queued:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool, "the buffer was already queued");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
queue_failed:
|
|
|
|
{
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_WARNING_OBJECT (pool, "could not queue a buffer %d (%s)", errno,
|
|
|
|
g_strerror (errno));
|
2011-07-18 16:54:49 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
static GstFlowReturn
|
2011-07-19 15:59:55 +00:00
|
|
|
gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2011-07-18 16:54:49 +00:00
|
|
|
GstFlowReturn res;
|
2011-07-15 09:27:18 +00:00
|
|
|
GstBuffer *outbuf;
|
|
|
|
struct v4l2_buffer vbuffer;
|
2013-11-13 12:05:40 +00:00
|
|
|
struct v4l2_plane vplanes[GST_VIDEO_MAX_PLANES];
|
2011-07-12 16:13:42 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2012-04-24 10:37:33 +00:00
|
|
|
GstClockTime timestamp;
|
2013-09-19 21:11:34 +00:00
|
|
|
GstV4l2Meta *meta;
|
2013-11-19 17:16:27 +00:00
|
|
|
gint i;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2012-09-05 14:39:31 +00:00
|
|
|
if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
|
|
|
|
goto poll_error;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* prepare the buffer */
|
2011-07-15 09:27:18 +00:00
|
|
|
memset (&vbuffer, 0x00, sizeof (vbuffer));
|
|
|
|
vbuffer.type = obj->type;
|
2014-03-15 10:13:05 +00:00
|
|
|
|
2013-02-19 10:47:20 +00:00
|
|
|
if (obj->mode == GST_V4L2_IO_DMABUF)
|
|
|
|
vbuffer.memory = V4L2_MEMORY_DMABUF;
|
|
|
|
else
|
|
|
|
vbuffer.memory = V4L2_MEMORY_MMAP;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* prepare the planes of the buffer */
|
|
|
|
if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
|
|
|
|
/* length is the number of elements in the
|
|
|
|
* vplanes array */
|
|
|
|
vbuffer.length = obj->n_v4l2_planes;
|
|
|
|
vbuffer.m.planes = vplanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the buffer is prepared, now fill in it with meaningful values */
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_LOG_OBJECT (pool, "doing DQBUF");
|
2011-07-15 09:27:18 +00:00
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
|
|
|
|
goto error;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-11 10:04:57 +00:00
|
|
|
/* get our GstBuffer with that index from the pool, if the buffer was
|
|
|
|
* outstanding we have a serious problem.
|
|
|
|
*/
|
2011-07-15 09:27:18 +00:00
|
|
|
outbuf = pool->buffers[vbuffer.index];
|
|
|
|
if (outbuf == NULL)
|
2011-07-19 15:59:55 +00:00
|
|
|
goto no_buffer;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
/* mark the buffer outstanding */
|
|
|
|
pool->buffers[vbuffer.index] = NULL;
|
2011-07-18 16:54:49 +00:00
|
|
|
pool->num_queued--;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2012-04-24 10:37:33 +00:00
|
|
|
timestamp = GST_TIMEVAL_TO_TIME (vbuffer.timestamp);
|
|
|
|
|
2013-09-19 21:11:34 +00:00
|
|
|
meta = GST_V4L2_META_GET (outbuf);
|
|
|
|
g_assert (meta != NULL);
|
|
|
|
|
2013-11-13 12:05:40 +00:00
|
|
|
/* The size can change at every frame, esp. with jpeg. The GstMemory
|
|
|
|
* inside the GstBuffer could have been changed by some other
|
|
|
|
* element. So update our meta */
|
|
|
|
if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
|
|
|
|
|| obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
|
|
|
|
|
|
|
|
/* this field is common to MPLANE and not MPLANE */
|
|
|
|
meta->vbuffer.length = vbuffer.length;
|
|
|
|
meta->vbuffer.bytesused = vbuffer.bytesused;
|
|
|
|
|
|
|
|
if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
2013-11-13 12:05:40 +00:00
|
|
|
/* the following also update meta->vbuffer.m.planes[i].length */
|
|
|
|
meta->vplanes[i].length = vbuffer.m.planes[i].length;
|
|
|
|
/* the following also update meta->vbuffer.m.planes[i].bytesused */
|
|
|
|
meta->vplanes[i].bytesused = vbuffer.m.planes[i].bytesused;
|
|
|
|
/* the following also update meta->vbuffer.m.planes[i].data_offset */
|
|
|
|
meta->vplanes[i].data_offset = vbuffer.m.planes[i].data_offset;
|
|
|
|
}
|
2013-11-19 17:16:27 +00:00
|
|
|
} else {
|
|
|
|
meta->vplanes[0].length = vbuffer.length;
|
|
|
|
meta->vplanes[0].bytesused = vbuffer.bytesused;
|
|
|
|
meta->vplanes[0].data_offset = 0;
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
2013-11-13 12:05:40 +00:00
|
|
|
GST_LOG_OBJECT (pool,
|
2013-11-19 17:16:27 +00:00
|
|
|
"dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
|
|
|
|
GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf,
|
|
|
|
vbuffer.sequence, vbuffer.index, meta->mem[i],
|
|
|
|
meta->vplanes[i].bytesused, i, vbuffer.flags,
|
2013-11-13 12:05:40 +00:00
|
|
|
GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
|
|
|
|
}
|
|
|
|
#endif
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-11 10:04:57 +00:00
|
|
|
/* set top/bottom field first if v4l2_buffer has the information */
|
2011-12-19 18:03:52 +00:00
|
|
|
if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
|
2014-02-21 14:31:59 +00:00
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
2011-08-25 14:41:23 +00:00
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
2014-02-21 19:46:44 +00:00
|
|
|
} else if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
|
2014-02-21 14:31:59 +00:00
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
2011-08-25 14:41:23 +00:00
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
2014-02-21 19:46:44 +00:00
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
2011-12-19 18:03:52 +00:00
|
|
|
}
|
2014-02-21 19:46:44 +00:00
|
|
|
|
2013-09-12 14:56:56 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
|
|
|
|
if (vbuffer.flags & V4L2_BUF_FLAG_KEYFRAME)
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
else
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2013-09-19 21:11:34 +00:00
|
|
|
/* The size can change at every frame, esp. with jpeg. The GstMemory
|
|
|
|
* inside the GstBuffer could have been changed by some other
|
|
|
|
* element, so just put back the original one. We always set it as
|
|
|
|
* no share, so if it's not there, it's not used at all.
|
|
|
|
*/
|
2013-11-19 17:16:27 +00:00
|
|
|
if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE
|
|
|
|
|| obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
|
2013-11-13 12:05:40 +00:00
|
|
|
gst_buffer_remove_all_memory (outbuf);
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++) {
|
2013-11-13 12:05:40 +00:00
|
|
|
gst_buffer_append_memory (outbuf,
|
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
|
2013-11-19 17:16:27 +00:00
|
|
|
meta->mem[i], meta->vplanes[i].length,
|
|
|
|
meta->vplanes[i].data_offset,
|
|
|
|
meta->vplanes[i].bytesused, NULL, NULL));
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
2013-09-19 21:11:34 +00:00
|
|
|
}
|
2010-04-04 11:39:52 +00:00
|
|
|
|
2012-04-24 10:37:33 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
*buffer = outbuf;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
return GST_FLOW_OK;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-11 10:04:57 +00:00
|
|
|
/* ERRORS */
|
2011-07-18 16:54:49 +00:00
|
|
|
poll_error:
|
|
|
|
{
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
|
2011-07-18 16:54:49 +00:00
|
|
|
return res;
|
|
|
|
}
|
2011-07-11 10:04:57 +00:00
|
|
|
error:
|
|
|
|
{
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_WARNING_OBJECT (pool,
|
2011-07-15 11:35:14 +00:00
|
|
|
"problem dequeuing frame %d (ix=%d), pool-ct=%d, buf.flags=%d",
|
2011-07-15 09:27:18 +00:00
|
|
|
vbuffer.sequence, vbuffer.index,
|
|
|
|
GST_MINI_OBJECT_REFCOUNT (pool), vbuffer.flags);
|
2011-07-11 10:04:57 +00:00
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_WARNING_OBJECT (pool,
|
2011-07-11 10:04:57 +00:00
|
|
|
"Non-blocking I/O has been selected using O_NONBLOCK and"
|
2011-07-12 16:13:42 +00:00
|
|
|
" no buffer was in the outgoing queue. device %s", obj->videodev);
|
2011-07-11 10:04:57 +00:00
|
|
|
break;
|
|
|
|
case EINVAL:
|
2011-07-15 10:00:54 +00:00
|
|
|
GST_ERROR_OBJECT (pool,
|
|
|
|
"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);
|
2011-07-11 10:04:57 +00:00
|
|
|
break;
|
|
|
|
case ENOMEM:
|
2011-07-15 10:00:54 +00:00
|
|
|
GST_ERROR_OBJECT (pool,
|
|
|
|
"insufficient memory to enqueue a user pointer buffer");
|
2011-07-11 10:04:57 +00:00
|
|
|
break;
|
|
|
|
case EIO:
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_INFO_OBJECT (pool,
|
2011-07-11 10:04:57 +00:00
|
|
|
"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."
|
2011-07-12 16:13:42 +00:00
|
|
|
" device %s", obj->videodev);
|
2011-07-11 10:04:57 +00:00
|
|
|
/* have we de-queued a buffer ? */
|
2011-07-15 09:27:18 +00:00
|
|
|
if (!(vbuffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) {
|
|
|
|
GST_DEBUG_OBJECT (pool, "reenqueing buffer");
|
2011-07-11 10:04:57 +00:00
|
|
|
/* FIXME ... should we do something here? */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EINTR:
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_WARNING_OBJECT (pool,
|
2011-07-12 16:13:42 +00:00
|
|
|
"could not sync on a buffer on device %s", obj->videodev);
|
2011-07-11 10:04:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-15 09:27:18 +00:00
|
|
|
GST_WARNING_OBJECT (pool,
|
2011-07-11 10:04:57 +00:00
|
|
|
"Grabbing frame got interrupted on %s unexpectedly. %d: %s.",
|
2011-07-12 16:13:42 +00:00
|
|
|
obj->videodev, errno, g_strerror (errno));
|
2011-07-11 10:04:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2011-07-11 10:04:57 +00:00
|
|
|
}
|
2011-07-19 15:59:55 +00:00
|
|
|
no_buffer:
|
2011-07-11 10:04:57 +00:00
|
|
|
{
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.",
|
2011-07-15 10:00:54 +00:00
|
|
|
vbuffer.index);
|
2011-07-15 09:27:18 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 11:35:14 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
|
2012-03-15 13:06:40 +00:00
|
|
|
GstBufferPoolAcquireParams * params)
|
2011-07-15 11:35:14 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn ret;
|
2011-07-18 16:54:49 +00:00
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
2011-07-15 11:35:14 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "acquire");
|
|
|
|
|
|
|
|
if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
|
|
|
|
goto flushing;
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
switch (obj->type) {
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
|
2013-11-13 12:05:40 +00:00
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
|
2011-07-18 16:54:49 +00:00
|
|
|
/* capture, This function should return a buffer with new captured data */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* take empty buffer from the pool */
|
|
|
|
ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
|
|
|
|
buffer, params);
|
|
|
|
break;
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-18 16:54:49 +00:00
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
/* just dequeue a buffer, we basically use the queue of v4l2 as the
|
2011-07-25 13:47:05 +00:00
|
|
|
* storage for our buffers. This function does poll first so we can
|
|
|
|
* interrupt it fine. */
|
2011-07-19 15:59:55 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
|
2012-04-23 16:01:31 +00:00
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* start copying buffers when we are running low on buffers */
|
|
|
|
if (pool->num_queued < pool->copy_threshold) {
|
|
|
|
GstBuffer *copy;
|
2014-03-15 10:13:05 +00:00
|
|
|
|
2013-05-02 07:41:01 +00:00
|
|
|
if (pool->can_alloc) {
|
|
|
|
if (GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
|
|
|
|
©, params) == GST_FLOW_OK) {
|
|
|
|
gst_v4l2_buffer_pool_release_buffer (bpool, copy);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
pool->can_alloc = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 16:01:31 +00:00
|
|
|
|
2013-09-19 21:11:34 +00:00
|
|
|
/* copy the buffer */
|
|
|
|
copy = gst_buffer_copy_region (*buffer,
|
|
|
|
GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
|
2012-04-23 16:01:31 +00:00
|
|
|
GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
|
|
|
|
|
|
|
|
/* and requeue so that we can continue capturing */
|
|
|
|
ret = gst_v4l2_buffer_pool_qbuf (pool, *buffer);
|
|
|
|
*buffer = copy;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
2012-08-08 10:31:59 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2011-07-18 16:54:49 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
|
2013-11-13 12:05:40 +00:00
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
|
2011-07-18 16:54:49 +00:00
|
|
|
/* playback, This function should return an empty buffer */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* get an empty buffer */
|
|
|
|
ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
|
|
|
|
buffer, params);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_MMAP:
|
2011-07-25 13:47:05 +00:00
|
|
|
/* get a free unqueued buffer */
|
2011-07-18 16:54:49 +00:00
|
|
|
ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
|
2011-07-25 13:47:05 +00:00
|
|
|
buffer, params);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
2012-08-08 10:31:59 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2011-07-18 16:54:49 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-08-08 10:31:59 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2011-07-18 16:54:49 +00:00
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2011-07-15 12:07:11 +00:00
|
|
|
}
|
2012-04-23 16:01:31 +00:00
|
|
|
done:
|
2011-07-15 11:35:14 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
flushing:
|
|
|
|
{
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "We are flushing");
|
2012-02-08 15:34:00 +00:00
|
|
|
return GST_FLOW_FLUSHING;
|
2011-07-15 11:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
2011-07-18 16:54:49 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
switch (obj->type) {
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
|
2013-11-13 12:05:40 +00:00
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
|
2011-07-18 16:54:49 +00:00
|
|
|
/* capture, put the buffer back in the queue so that we can refill it
|
|
|
|
* later. */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* release back in the pool */
|
|
|
|
GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
|
|
|
|
break;
|
|
|
|
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-18 16:54:49 +00:00
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
/* queue back in the device */
|
2011-07-19 15:59:55 +00:00
|
|
|
gst_v4l2_buffer_pool_qbuf (pool, buffer);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
|
2013-11-13 12:05:40 +00:00
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
|
2011-07-26 09:56:07 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* release back in the pool */
|
|
|
|
GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
|
|
|
|
break;
|
2011-07-19 15:59:55 +00:00
|
|
|
|
2011-07-26 09:56:07 +00:00
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
{
|
2011-10-31 01:24:04 +00:00
|
|
|
GstV4l2Meta *meta;
|
2013-04-03 09:09:37 +00:00
|
|
|
guint index;
|
2011-07-26 09:56:07 +00:00
|
|
|
|
2011-10-31 01:24:04 +00:00
|
|
|
meta = GST_V4L2_META_GET (buffer);
|
2011-07-26 09:56:07 +00:00
|
|
|
g_assert (meta != NULL);
|
|
|
|
|
2013-04-03 09:09:37 +00:00
|
|
|
index = meta->vbuffer.index;
|
|
|
|
|
|
|
|
if (pool->buffers[index] == NULL) {
|
|
|
|
GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
|
|
|
|
index);
|
2012-05-03 14:07:27 +00:00
|
|
|
|
|
|
|
/* reset to the full length, in case it was changed */
|
2013-11-13 12:05:40 +00:00
|
|
|
if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
|
|
|
|
gint i = 0;
|
|
|
|
gint total_length = 0;
|
2013-11-19 17:16:27 +00:00
|
|
|
for (i = 0; i < meta->n_planes; i++)
|
|
|
|
total_length += meta->vplanes[i].length;
|
2013-11-13 12:05:40 +00:00
|
|
|
|
2013-12-07 19:03:53 +00:00
|
|
|
if (total_length != gst_buffer_get_size (buffer) &&
|
|
|
|
obj->info.finfo->n_planes > 1) {
|
2013-11-13 12:05:40 +00:00
|
|
|
/* FIXME if the lengths has actually changed it may require
|
|
|
|
* to restore the sizes of the individual memories and
|
|
|
|
* re-add them */
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"lengths changed, more work required");
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_resize (buffer, 0, total_length);
|
|
|
|
} else {
|
|
|
|
gst_buffer_resize (buffer, 0, meta->vbuffer.length);
|
|
|
|
}
|
2012-05-03 14:07:27 +00:00
|
|
|
|
2011-07-26 09:56:07 +00:00
|
|
|
/* playback, put the buffer back in the queue to refill later. */
|
|
|
|
GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
|
|
|
|
buffer);
|
|
|
|
} else {
|
|
|
|
/* the buffer is queued in the device but maybe not played yet. We just
|
|
|
|
* leave it there and not make it available for future calls to acquire
|
|
|
|
* for now. The buffer will be dequeued and reused later. */
|
2013-04-03 09:09:37 +00:00
|
|
|
GST_LOG_OBJECT (pool, "buffer %u is queued", index);
|
2011-07-26 09:56:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-07-19 15:59:55 +00:00
|
|
|
|
2011-07-26 09:56:07 +00:00
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2011-07-19 15:59:55 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
|
|
|
|
|
2013-06-03 15:07:10 +00:00
|
|
|
gst_v4l2_buffer_pool_free_buffers (pool);
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
if (pool->video_fd >= 0)
|
|
|
|
v4l2_close (pool->video_fd);
|
2012-04-23 16:01:31 +00:00
|
|
|
if (pool->allocator)
|
2012-07-09 14:27:10 +00:00
|
|
|
gst_object_unref (pool->allocator);
|
2011-08-04 11:50:01 +00:00
|
|
|
g_free (pool->buffers);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2013-06-05 14:32:30 +00:00
|
|
|
gst_object_unref (pool->obj->element);
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_v4l2_buffer_pool_new:
|
|
|
|
* @obj: the v4l2 object owning the pool
|
|
|
|
*
|
|
|
|
* Construct a new buffer pool.
|
|
|
|
*
|
2011-07-25 13:47:05 +00:00
|
|
|
* Returns: the new pool, use gst_object_unref() to free resources
|
2011-07-15 09:27:18 +00:00
|
|
|
*/
|
2011-08-04 11:50:01 +00:00
|
|
|
GstBufferPool *
|
|
|
|
gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
|
2011-07-15 09:27:18 +00:00
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool;
|
2014-01-07 11:58:23 +00:00
|
|
|
GstStructure *config;
|
|
|
|
gboolean res = FALSE;
|
2011-07-18 08:51:21 +00:00
|
|
|
gint fd;
|
2014-03-27 22:41:07 +00:00
|
|
|
guint min, max;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-18 08:51:21 +00:00
|
|
|
fd = v4l2_dup (obj->video_fd);
|
|
|
|
if (fd < 0)
|
2011-07-15 09:27:18 +00:00
|
|
|
goto dup_failed;
|
|
|
|
|
2011-07-18 08:51:21 +00:00
|
|
|
pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL, NULL);
|
|
|
|
pool->video_fd = fd;
|
2011-07-15 09:27:18 +00:00
|
|
|
pool->obj = obj;
|
2014-03-27 22:41:07 +00:00
|
|
|
pool->can_alloc = FALSE;
|
|
|
|
min = max = 2;
|
|
|
|
|
|
|
|
/* Check for CREATE_BUFS support */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
{
|
|
|
|
struct v4l2_create_buffers create_bufs = { 0 };
|
|
|
|
create_bufs.count = 0;
|
|
|
|
create_bufs.memory = V4L2_MEMORY_MMAP;
|
|
|
|
create_bufs.format = obj->format;
|
|
|
|
if (v4l2_ioctl (pool->video_fd, VIDIOC_CREATE_BUFS, &create_bufs) == 0) {
|
|
|
|
pool->can_alloc = TRUE;
|
|
|
|
max = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2014-01-07 11:58:23 +00:00
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
|
2014-03-27 22:41:07 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps, obj->sizeimage, min, max);
|
2014-01-07 11:58:23 +00:00
|
|
|
|
2014-03-18 15:53:57 +00:00
|
|
|
/* Ensure our internal pool has required features */
|
|
|
|
if (obj->need_video_meta)
|
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
|
|
|
|
if (obj->need_crop_meta)
|
|
|
|
gst_v4l2_buffer_pool_add_crop_meta (pool, obj->need_crop_meta);
|
|
|
|
|
2014-01-07 11:58:23 +00:00
|
|
|
res = gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
|
|
|
|
if (!res)
|
|
|
|
goto config_failed;
|
2011-08-04 11:50:01 +00:00
|
|
|
|
2013-06-05 14:32:30 +00:00
|
|
|
gst_object_ref (obj->element);
|
|
|
|
|
2011-08-04 11:50:01 +00:00
|
|
|
return GST_BUFFER_POOL (pool);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
dup_failed:
|
|
|
|
{
|
2011-07-18 08:51:21 +00:00
|
|
|
GST_DEBUG ("failed to dup fd %d (%s)", errno, g_strerror (errno));
|
2011-07-11 10:04:57 +00:00
|
|
|
return NULL;
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
2014-01-07 11:58:23 +00:00
|
|
|
config_failed:
|
|
|
|
{
|
|
|
|
GST_WARNING ("failed to set pool config");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
static GstFlowReturn
|
2011-07-19 15:59:55 +00:00
|
|
|
gst_v4l2_do_read (GstV4l2BufferPool * pool, GstBuffer * buf)
|
2011-07-15 09:27:18 +00:00
|
|
|
{
|
2011-07-18 16:54:49 +00:00
|
|
|
GstFlowReturn res;
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
gint amount;
|
2012-01-24 13:38:58 +00:00
|
|
|
GstMapInfo map;
|
2011-07-27 14:46:46 +00:00
|
|
|
gint toread;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-27 14:46:46 +00:00
|
|
|
toread = obj->sizeimage;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-27 14:46:46 +00:00
|
|
|
GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2012-01-24 13:38:58 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_WRITE);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
do {
|
|
|
|
if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
|
|
|
|
goto poll_error;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2012-01-24 13:38:58 +00:00
|
|
|
amount = v4l2_read (obj->video_fd, map.data, toread);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-27 14:46:46 +00:00
|
|
|
if (amount == toread) {
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
} else if (amount == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
goto read_error;
|
|
|
|
} else {
|
|
|
|
/* short reads can happen if a signal interrupts the read */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (TRUE);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
GST_LOG_OBJECT (pool, "read %d bytes", amount);
|
2012-01-24 13:38:58 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
|
|
|
gst_buffer_resize (buf, 0, amount);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
return GST_FLOW_OK;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
2011-07-18 16:54:49 +00:00
|
|
|
poll_error:
|
2011-07-15 09:27:18 +00:00
|
|
|
{
|
2011-07-18 16:54:49 +00:00
|
|
|
GST_DEBUG ("poll error %s", gst_flow_get_name (res));
|
|
|
|
goto cleanup;
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
read_error:
|
2011-07-15 09:27:18 +00:00
|
|
|
{
|
2011-07-18 16:54:49 +00:00
|
|
|
GST_ELEMENT_ERROR (obj->element, RESOURCE, READ,
|
|
|
|
(_("Error reading %d bytes from device '%s'."),
|
2011-07-27 14:46:46 +00:00
|
|
|
toread, obj->videodev), GST_ERROR_SYSTEM);
|
2011-07-18 16:54:49 +00:00
|
|
|
res = GST_FLOW_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
cleanup:
|
|
|
|
{
|
2012-01-24 13:38:58 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
|
|
|
gst_buffer_resize (buf, 0, 0);
|
2011-07-18 16:54:49 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_v4l2_buffer_pool_process:
|
|
|
|
* @bpool: a #GstBufferPool
|
|
|
|
* @buf: a #GstBuffer
|
|
|
|
*
|
|
|
|
* Process @buf in @bpool. For capture devices, this functions fills @buf with
|
|
|
|
* data from the device. For output devices, this functions send the contents of
|
|
|
|
* @buf to the device for playback.
|
|
|
|
*
|
|
|
|
* Returns: %GST_FLOW_OK on success.
|
|
|
|
*/
|
|
|
|
GstFlowReturn
|
2011-07-27 14:46:46 +00:00
|
|
|
gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2011-07-27 14:46:46 +00:00
|
|
|
GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
|
2011-07-18 16:54:49 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
|
2011-07-19 15:59:55 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
|
|
|
|
|
2014-03-16 15:55:43 +00:00
|
|
|
g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
switch (obj->type) {
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
|
2013-11-13 12:05:40 +00:00
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
|
2011-07-18 16:54:49 +00:00
|
|
|
/* capture */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* capture into the buffer */
|
2011-07-19 15:59:55 +00:00
|
|
|
ret = gst_v4l2_do_read (pool, buf);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
{
|
|
|
|
GstBuffer *tmp;
|
|
|
|
|
2014-03-25 19:21:03 +00:00
|
|
|
if (buf->pool == bpool) {
|
|
|
|
if (gst_buffer_get_size (buf) == 0)
|
|
|
|
goto eos;
|
|
|
|
else
|
|
|
|
/* nothing, data was inside the buffer when we did _acquire() */
|
|
|
|
goto done;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
/* buffer not from our pool, grab a frame and copy it into the target */
|
2011-07-19 15:59:55 +00:00
|
|
|
if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
|
2011-07-18 16:54:49 +00:00
|
|
|
goto done;
|
|
|
|
|
2014-03-25 19:21:03 +00:00
|
|
|
/* An empty buffer on capture indicates the end of stream */
|
|
|
|
if (gst_buffer_get_size (tmp) == 0) {
|
|
|
|
gst_buffer_unref (tmp);
|
|
|
|
goto eos;
|
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
if (!gst_v4l2_object_copy (obj, buf, tmp))
|
|
|
|
goto copy_failed;
|
|
|
|
|
2011-07-19 15:59:55 +00:00
|
|
|
/* an queue the buffer again after the copy */
|
|
|
|
if ((ret = gst_v4l2_buffer_pool_qbuf (pool, tmp)) != GST_FLOW_OK)
|
2011-07-18 16:54:49 +00:00
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
|
2013-11-13 12:05:40 +00:00
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
|
2011-07-18 16:54:49 +00:00
|
|
|
/* playback */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* FIXME, do write() */
|
2011-07-26 09:56:07 +00:00
|
|
|
GST_WARNING_OBJECT (pool, "implement write()");
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-18 16:54:49 +00:00
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
{
|
2011-07-19 15:59:55 +00:00
|
|
|
GstBuffer *to_queue;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
if (buf->pool == bpool) {
|
|
|
|
/* nothing, we can queue directly */
|
2013-05-21 10:02:51 +00:00
|
|
|
to_queue = gst_buffer_ref (buf);
|
2011-08-04 11:50:01 +00:00
|
|
|
GST_LOG_OBJECT (pool, "processing buffer from our pool");
|
2011-07-18 16:54:49 +00:00
|
|
|
} else {
|
2011-08-04 11:50:01 +00:00
|
|
|
GST_LOG_OBJECT (pool, "alloc buffer from our pool");
|
|
|
|
|
2011-07-25 13:47:05 +00:00
|
|
|
/* this can block if all buffers are outstanding which would be
|
|
|
|
* strange because we would expect the upstream element to have
|
|
|
|
* allocated them and returned to us.. */
|
2013-05-21 10:02:51 +00:00
|
|
|
ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, NULL);
|
2011-07-18 16:54:49 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
2011-08-04 11:50:01 +00:00
|
|
|
goto acquire_failed;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
/* copy into it and queue */
|
2011-07-19 15:59:55 +00:00
|
|
|
if (!gst_v4l2_object_copy (obj, to_queue, buf))
|
2011-07-18 16:54:49 +00:00
|
|
|
goto copy_failed;
|
2011-07-19 15:59:55 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-07-19 15:59:55 +00:00
|
|
|
if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
|
|
|
|
goto done;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-07-19 15:59:55 +00:00
|
|
|
/* if we are not streaming yet (this is the first buffer, start
|
|
|
|
* streaming now */
|
|
|
|
if (!pool->streaming)
|
|
|
|
if (!start_streaming (pool))
|
|
|
|
goto start_failed;
|
2011-07-25 13:47:05 +00:00
|
|
|
|
|
|
|
if (pool->num_queued == pool->num_allocated) {
|
2013-05-28 17:14:15 +00:00
|
|
|
GstBuffer *out;
|
2011-07-25 13:47:05 +00:00
|
|
|
/* all buffers are queued, try to dequeue one and release it back
|
|
|
|
* into the pool so that _acquire can get to it again. */
|
2013-05-28 17:14:15 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
|
2013-07-05 09:51:04 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
gst_buffer_unref (to_queue);
|
2011-07-25 13:47:05 +00:00
|
|
|
goto done;
|
2013-07-05 09:51:04 +00:00
|
|
|
}
|
2011-07-25 13:47:05 +00:00
|
|
|
|
|
|
|
/* release the rendered buffer back into the pool. This wakes up any
|
2013-05-28 17:14:15 +00:00
|
|
|
* thread waiting for a buffer in _acquire(). If the buffer still has
|
|
|
|
* a pool then this will happen when the refcount reaches 0 */
|
|
|
|
if (!out->pool)
|
|
|
|
gst_v4l2_buffer_pool_release_buffer (bpool, out);
|
2011-07-25 13:47:05 +00:00
|
|
|
}
|
2013-05-28 17:14:15 +00:00
|
|
|
gst_buffer_unref (to_queue);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2011-08-04 11:50:01 +00:00
|
|
|
acquire_failed:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
return ret;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
copy_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (obj->element, "failed to copy data");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
start_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (obj->element, "failed to start streaming");
|
|
|
|
return GST_FLOW_ERROR;
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
2014-03-25 19:21:03 +00:00
|
|
|
eos:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (obj->element, "end of stream reached");
|
|
|
|
return GST_FLOW_EOS;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
2013-12-18 18:37:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_v4l2_buffer_pool_flush:
|
|
|
|
* @bpool: a #GstBufferPool
|
|
|
|
*
|
|
|
|
* First, set obj->poll to be flushing
|
|
|
|
* Call STREAMOFF to clear QUEUED flag on every driver buffers.
|
|
|
|
* Then release all buffers that are in pool->buffers array.
|
|
|
|
* Finally call STREAMON if CAPTURE type
|
|
|
|
* The caller is responsible to unset flushing on obj->pool
|
|
|
|
*
|
|
|
|
* Returns: TRUE on success.
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_v4l2_buffer_pool_flush (GstV4l2BufferPool * pool)
|
|
|
|
{
|
|
|
|
GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
gint i = 0;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "flush");
|
|
|
|
|
|
|
|
stop_streaming (pool);
|
|
|
|
|
|
|
|
switch (obj->type) {
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
|
|
|
|
case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
{
|
|
|
|
for (i = 0; i < pool->num_buffers; i++) {
|
|
|
|
GstBuffer *buf = pool->buffers[i];
|
|
|
|
if (buf) {
|
|
|
|
/* it's necessary to set to NULL before to call
|
|
|
|
* gst_v4l2_buffer_pool_release_buffer
|
|
|
|
* otherwise it won't go back to the pool */
|
|
|
|
pool->buffers[i] = NULL;
|
|
|
|
|
|
|
|
/* dicrease counter */
|
|
|
|
pool->num_queued--;
|
|
|
|
|
|
|
|
/* in CAPTURE mode the pool->num_queued will be re-incremented
|
|
|
|
* because the buffers are queued when released */
|
|
|
|
if (buf->pool)
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
else
|
|
|
|
gst_v4l2_buffer_pool_release_buffer (bpool, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do not set pool->num_queued to 0 because
|
|
|
|
* the buffers are queued when released */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* we can start capturing now, we wait for the playback
|
|
|
|
* case until we queued the first buffer */
|
|
|
|
if (!V4L2_TYPE_IS_OUTPUT (obj->type))
|
|
|
|
if (!start_streaming (pool))
|
|
|
|
goto start_failed;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
start_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "failed to start streaming");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 19:07:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gst_v4l2_buffer_pool_add_crop_meta (GstV4l2BufferPool * bpool, gboolean add)
|
|
|
|
{
|
|
|
|
bpool->add_cropmeta = add;
|
|
|
|
}
|