2009-08-04 07:14:20 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
2017-02-26 00:47:03 +00:00
|
|
|
* 2006 Edgard Lima <edgard.lima@gmail.com>
|
2009-08-04 07:14:20 +00:00
|
|
|
* 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
|
|
|
|
2017-07-13 00:49:47 +00:00
|
|
|
#include "gstv4l2object.h"
|
2022-03-25 13:59:23 +00:00
|
|
|
#include <glib/gi18n-lib.h>
|
2011-12-12 02:30:45 +00:00
|
|
|
#include <gst/glib-compat-private.h>
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2015-02-20 04:07:23 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
|
2016-02-20 11:51:56 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
|
2015-02-20 04:07:23 +00:00
|
|
|
#define GST_CAT_DEFAULT v4l2bufferpool_debug
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
#define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
enum _GstV4l2BufferPoolAcquireFlags
|
|
|
|
{
|
2014-11-03 10:44:28 +00:00
|
|
|
GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
|
|
|
|
GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
|
|
|
|
GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
|
2014-04-05 02:46:40 +00:00
|
|
|
};
|
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
/* Buffer state flags */
|
|
|
|
enum _GstV4l2BufferState
|
|
|
|
{
|
|
|
|
/* Buffer is free (either on the GstBufferPool free queue, or no GstBuffer has
|
|
|
|
* been allocated yet) */
|
|
|
|
BUFFER_STATE_FREE = 0,
|
|
|
|
|
|
|
|
/* Buffer had outstanding external users */
|
|
|
|
BUFFER_STATE_OUTSTANDING = 1,
|
|
|
|
|
|
|
|
/* Buffer is on one of the kernel queues */
|
|
|
|
BUFFER_STATE_QUEUED = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
|
|
|
|
GstBuffer * buffer, gboolean queued);
|
2011-07-15 11:35:14 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
static gboolean
|
2024-04-05 12:09:18 +00:00
|
|
|
gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group,
|
|
|
|
gboolean check_writable)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2014-04-05 02:46:40 +00:00
|
|
|
GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
gboolean valid = FALSE;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY))
|
|
|
|
goto done;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2014-04-10 20:26:34 +00:00
|
|
|
if (gst_is_dmabuf_memory (mem))
|
2014-07-10 22:11:20 +00:00
|
|
|
mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
GST_V4L2_MEMORY_QUARK);
|
2014-04-10 20:26:34 +00:00
|
|
|
|
2014-07-10 22:11:20 +00:00
|
|
|
if (mem && gst_is_v4l2_memory (mem)) {
|
2014-04-05 02:46:40 +00:00
|
|
|
GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
|
2014-11-07 02:21:40 +00:00
|
|
|
GstV4l2MemoryGroup *group = vmem->group;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (group->n_mem != gst_buffer_n_memory (buffer))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (i = 0; i < group->n_mem; i++) {
|
|
|
|
if (group->mem[i] != gst_buffer_peek_memory (buffer, i))
|
|
|
|
goto done;
|
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (check_writable && !gst_memory_is_writable (group->mem[i]))
|
2014-11-07 02:21:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
valid = TRUE;
|
2014-11-07 02:21:40 +00:00
|
|
|
if (out_group)
|
|
|
|
*out_group = group;
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
2014-04-05 02:46:40 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
return valid;
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 10:22:58 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_resize_buffer (GstBufferPool * bpool, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
GstV4l2MemoryGroup *group;
|
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (gst_v4l2_is_buffer_valid (buffer, &group, TRUE)) {
|
2022-07-08 10:22:58 +00:00
|
|
|
gst_v4l2_allocator_reset_group (pool->vallocator, group);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_copy_buffer (GstV4l2BufferPool * pool, GstBuffer * dest,
|
|
|
|
GstBuffer * src)
|
|
|
|
{
|
2014-05-12 22:03:18 +00:00
|
|
|
const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "copying buffer");
|
|
|
|
|
|
|
|
if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
|
|
|
|
finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
|
|
|
|
GstVideoFrame src_frame, dest_frame;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "copy video frame");
|
|
|
|
|
|
|
|
/* we have raw video, use videoframe copy to get strides right */
|
2014-05-12 22:03:18 +00:00
|
|
|
if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ))
|
2014-04-11 21:10:11 +00:00
|
|
|
goto invalid_buffer;
|
|
|
|
|
2014-05-12 22:03:18 +00:00
|
|
|
if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest,
|
2014-04-11 21:10:11 +00:00
|
|
|
GST_MAP_WRITE)) {
|
|
|
|
gst_video_frame_unmap (&src_frame);
|
|
|
|
goto invalid_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_frame_copy (&dest_frame, &src_frame);
|
|
|
|
|
|
|
|
gst_video_frame_unmap (&src_frame);
|
|
|
|
gst_video_frame_unmap (&dest_frame);
|
|
|
|
} else {
|
|
|
|
GstMapInfo map;
|
2023-07-25 16:19:22 +00:00
|
|
|
gsize filled_size;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "copy raw bytes");
|
|
|
|
|
|
|
|
if (!gst_buffer_map (src, &map, GST_MAP_READ))
|
|
|
|
goto invalid_buffer;
|
|
|
|
|
2023-07-25 16:19:22 +00:00
|
|
|
filled_size =
|
|
|
|
gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src));
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
gst_buffer_unmap (src, &map);
|
2023-07-25 16:19:22 +00:00
|
|
|
gst_buffer_resize (dest, 0, filled_size);
|
2014-04-11 21:10:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 06:00:08 +00:00
|
|
|
gst_buffer_copy_into (dest, src,
|
|
|
|
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
|
2015-07-20 08:59:40 +00:00
|
|
|
|
2016-02-20 11:51:56 +00:00
|
|
|
GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest);
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
invalid_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "could not map buffer");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UserPtrData
|
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
|
|
|
gboolean is_frame;
|
|
|
|
GstVideoFrame frame;
|
|
|
|
GstMapInfo map;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GQuark
|
|
|
|
gst_v4l2_buffer_pool_import_quark (void)
|
|
|
|
{
|
|
|
|
static GQuark quark = 0;
|
|
|
|
|
|
|
|
if (quark == 0)
|
|
|
|
quark = g_quark_from_string ("GstV4l2BufferPoolUsePtrData");
|
|
|
|
|
|
|
|
return quark;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_unmap_userptr_frame (struct UserPtrData *data)
|
|
|
|
{
|
|
|
|
if (data->is_frame)
|
|
|
|
gst_video_frame_unmap (&data->frame);
|
|
|
|
else
|
|
|
|
gst_buffer_unmap (data->buffer, &data->map);
|
|
|
|
|
|
|
|
if (data->buffer)
|
|
|
|
gst_buffer_unref (data->buffer);
|
|
|
|
|
2023-01-08 17:38:17 +00:00
|
|
|
g_free (data);
|
2014-04-11 21:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_import_userptr (GstV4l2BufferPool * pool,
|
|
|
|
GstBuffer * dest, GstBuffer * src)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstV4l2MemoryGroup *group = NULL;
|
|
|
|
GstMapFlags flags;
|
2014-05-12 22:03:18 +00:00
|
|
|
const GstVideoFormatInfo *finfo = pool->caps_info.finfo;
|
2014-04-11 21:10:11 +00:00
|
|
|
struct UserPtrData *data = NULL;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "importing userptr");
|
|
|
|
|
|
|
|
/* get the group */
|
2024-04-05 12:09:18 +00:00
|
|
|
if (!gst_v4l2_is_buffer_valid (dest, &group, TRUE))
|
2014-04-11 21:10:11 +00:00
|
|
|
goto not_our_buffer;
|
|
|
|
|
2014-05-24 22:43:28 +00:00
|
|
|
if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
|
2014-04-11 21:10:11 +00:00
|
|
|
flags = GST_MAP_READ;
|
|
|
|
else
|
|
|
|
flags = GST_MAP_WRITE;
|
|
|
|
|
2023-01-08 17:38:17 +00:00
|
|
|
data = g_new0 (struct UserPtrData, 1);
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
|
|
|
|
finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
|
2015-01-23 09:15:46 +00:00
|
|
|
gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
|
|
|
|
gint i;
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
data->is_frame = TRUE;
|
|
|
|
|
2014-05-12 22:03:18 +00:00
|
|
|
if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
|
2014-04-11 21:10:11 +00:00
|
|
|
goto invalid_buffer;
|
|
|
|
|
2015-01-23 09:15:46 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES (finfo); i++) {
|
|
|
|
if (GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) {
|
|
|
|
gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i);
|
2022-11-15 18:50:13 +00:00
|
|
|
size[i] = GST_VIDEO_TILE_X_TILES (tinfo) *
|
|
|
|
GST_VIDEO_TILE_Y_TILES (tinfo) *
|
|
|
|
GST_VIDEO_FORMAT_INFO_TILE_SIZE (finfo, i);
|
2015-01-23 09:15:46 +00:00
|
|
|
} else {
|
|
|
|
size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
|
|
|
|
GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 15:30:06 +00:00
|
|
|
/* In the single planar API, planes must be contiguous in memory and
|
|
|
|
* therefore they must have expected size. ie: no padding.
|
|
|
|
* To check these conditions, we check that plane 'i' start address
|
|
|
|
* + plane 'i' size equals to plane 'i+1' start address */
|
|
|
|
if (!V4L2_TYPE_IS_MULTIPLANAR (pool->obj->type)) {
|
|
|
|
for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES (finfo) - 1); i++) {
|
|
|
|
const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix;
|
|
|
|
gpointer tmp;
|
2022-01-25 19:08:47 +00:00
|
|
|
gint estride = gst_video_format_info_extrapolate_stride (finfo, i,
|
2014-10-06 15:30:06 +00:00
|
|
|
pix_fmt->bytesperline);
|
|
|
|
guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i,
|
|
|
|
pix_fmt->height);
|
|
|
|
|
|
|
|
tmp = ((guint8 *) data->frame.data[i]) + estride * eheight;
|
|
|
|
if (tmp != data->frame.data[i + 1])
|
|
|
|
goto non_contiguous_mem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
|
2015-01-23 09:15:46 +00:00
|
|
|
data->frame.info.size, finfo->n_planes, data->frame.data, size))
|
2014-04-11 21:10:11 +00:00
|
|
|
goto import_failed;
|
|
|
|
} else {
|
|
|
|
gpointer ptr[1];
|
2015-01-23 09:15:46 +00:00
|
|
|
gsize size[1];
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
data->is_frame = FALSE;
|
|
|
|
|
|
|
|
if (!gst_buffer_map (src, &data->map, flags))
|
|
|
|
goto invalid_buffer;
|
|
|
|
|
|
|
|
ptr[0] = data->map.data;
|
2015-01-23 09:15:46 +00:00
|
|
|
size[0] = data->map.size;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
|
2015-01-23 09:15:46 +00:00
|
|
|
data->map.size, 1, ptr, size))
|
2014-04-11 21:10:11 +00:00
|
|
|
goto import_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->buffer = gst_buffer_ref (src);
|
|
|
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
|
|
|
|
data, (GDestroyNotify) _unmap_userptr_frame);
|
|
|
|
|
2017-04-10 08:56:00 +00:00
|
|
|
gst_buffer_copy_into (dest, src,
|
|
|
|
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
not_our_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
invalid_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "could not map buffer");
|
2023-01-08 17:38:17 +00:00
|
|
|
g_free (data);
|
2014-04-11 21:10:11 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2014-10-06 15:30:06 +00:00
|
|
|
non_contiguous_mem:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
|
|
|
|
_unmap_userptr_frame (data);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2014-04-11 21:10:11 +00:00
|
|
|
import_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "failed to import data");
|
|
|
|
_unmap_userptr_frame (data);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_import_dmabuf (GstV4l2BufferPool * pool,
|
|
|
|
GstBuffer * dest, GstBuffer * src)
|
|
|
|
{
|
|
|
|
GstV4l2MemoryGroup *group = NULL;
|
|
|
|
GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 };
|
|
|
|
guint n_mem = gst_buffer_n_memory (src);
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "importing dmabuf");
|
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (!gst_v4l2_is_buffer_valid (dest, &group, TRUE))
|
2014-04-11 21:10:11 +00:00
|
|
|
goto not_our_buffer;
|
|
|
|
|
|
|
|
if (n_mem > GST_VIDEO_MAX_PLANES)
|
|
|
|
goto too_many_mems;
|
|
|
|
|
|
|
|
for (i = 0; i < n_mem; i++)
|
|
|
|
dma_mem[i] = gst_buffer_peek_memory (src, i);
|
|
|
|
|
|
|
|
if (!gst_v4l2_allocator_import_dmabuf (pool->vallocator, group, n_mem,
|
|
|
|
dma_mem))
|
|
|
|
goto import_failed;
|
|
|
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
|
|
|
|
gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
|
|
|
|
|
2017-04-10 08:56:00 +00:00
|
|
|
gst_buffer_copy_into (dest, src,
|
|
|
|
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
not_our_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
too_many_mems:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "could not map buffer");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
import_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "failed to import dmabuf");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_prepare_buffer (GstV4l2BufferPool * pool,
|
|
|
|
GstBuffer * dest, GstBuffer * src)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2014-04-29 16:57:08 +00:00
|
|
|
gboolean own_src = FALSE;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
if (src == NULL) {
|
|
|
|
if (pool->other_pool == NULL) {
|
|
|
|
GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_buffer_pool_acquire_buffer (pool->other_pool, &src, NULL);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool");
|
|
|
|
goto done;
|
|
|
|
}
|
2014-04-29 16:57:08 +00:00
|
|
|
|
|
|
|
own_src = TRUE;
|
2014-04-11 21:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (pool->obj->mode) {
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
ret = gst_v4l2_buffer_pool_copy_buffer (pool, dest, src);
|
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
ret = gst_v4l2_buffer_pool_import_userptr (pool, dest, src);
|
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
|
|
|
ret = gst_v4l2_buffer_pool_import_dmabuf (pool, dest, src);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-29 16:57:08 +00:00
|
|
|
if (own_src)
|
|
|
|
gst_buffer_unref (src);
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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);
|
2014-04-05 02:46:40 +00:00
|
|
|
GstV4l2MemoryGroup *group = NULL;
|
|
|
|
GstBuffer *newbuf = NULL;
|
2011-07-12 16:13:42 +00:00
|
|
|
GstV4l2Object *obj;
|
2011-07-13 11:37:58 +00:00
|
|
|
GstVideoInfo *info;
|
2011-07-12 16:13:42 +00:00
|
|
|
|
|
|
|
obj = pool->obj;
|
2011-07-13 11:37:58 +00:00
|
|
|
info = &obj->info;
|
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:
|
2014-04-05 02:46:40 +00:00
|
|
|
group = gst_v4l2_allocator_alloc_mmap (pool->vallocator);
|
|
|
|
break;
|
2013-02-19 10:47:20 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2014-04-10 20:26:34 +00:00
|
|
|
group = gst_v4l2_allocator_alloc_dmabuf (pool->vallocator,
|
|
|
|
pool->allocator);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
2014-04-11 21:10:11 +00:00
|
|
|
group = gst_v4l2_allocator_alloc_userptr (pool->vallocator);
|
|
|
|
break;
|
2014-04-14 16:19:39 +00:00
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-04-11 21:10:11 +00:00
|
|
|
group = gst_v4l2_allocator_alloc_dmabufin (pool->vallocator);
|
|
|
|
break;
|
2011-07-18 16:54:49 +00:00
|
|
|
default:
|
2012-08-08 10:31:59 +00:00
|
|
|
newbuf = NULL;
|
2011-07-18 16:54:49 +00:00
|
|
|
g_assert_not_reached ();
|
2014-04-05 02:46:40 +00:00
|
|
|
break;
|
2011-07-13 11:37:58 +00:00
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
if (group != NULL) {
|
|
|
|
gint i;
|
|
|
|
newbuf = gst_buffer_new ();
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
for (i = 0; i < group->n_mem; i++)
|
|
|
|
gst_buffer_append_memory (newbuf, group->mem[i]);
|
2021-09-27 15:52:22 +00:00
|
|
|
|
|
|
|
if (g_atomic_int_get (&pool->buffer_state[group->buffer.index])) {
|
|
|
|
GST_WARNING_OBJECT (pool, "newly allocated buffer %u is not free",
|
|
|
|
group->buffer.index);
|
|
|
|
}
|
2014-04-05 02:46:40 +00:00
|
|
|
} else if (newbuf == NULL) {
|
|
|
|
goto allocation_failed;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
/* add metadata to raw video buffers */
|
2021-11-09 05:36:28 +00:00
|
|
|
if (pool->add_videometa) {
|
|
|
|
GstVideoMeta *videometa =
|
|
|
|
gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
|
2014-05-06 16:58:59 +00:00
|
|
|
GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
|
|
|
|
info->offset, info->stride);
|
2021-11-09 05:36:28 +00:00
|
|
|
if (videometa)
|
|
|
|
gst_video_meta_set_alignment (videometa, obj->align);
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
*buffer = newbuf;
|
2013-04-30 11:07:37 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
allocation_failed:
|
|
|
|
{
|
2014-04-11 21:10:11 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "failed to allocate buffer");
|
2014-05-23 19:55:08 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2013-04-30 11:07:37 +00:00
|
|
|
}
|
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;
|
2014-04-05 02:46:40 +00:00
|
|
|
gboolean can_allocate = FALSE;
|
|
|
|
gboolean updated = FALSE;
|
|
|
|
gboolean ret;
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 (pool->allocator)
|
|
|
|
gst_object_unref (pool->allocator);
|
2014-04-05 02:46:40 +00:00
|
|
|
pool->allocator = NULL;
|
2014-01-17 06:46:09 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
pool->allocator = gst_dmabuf_allocator_new ();
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
|
2014-04-05 02:46:40 +00:00
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_MMAP:
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
|
2014-04-05 02:46:40 +00:00
|
|
|
break;
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate =
|
|
|
|
GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
|
2014-04-05 02:46:40 +00:00
|
|
|
break;
|
2014-04-11 21:10:11 +00:00
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
|
2014-04-11 21:10:11 +00:00
|
|
|
break;
|
2014-04-05 02:46:40 +00:00
|
|
|
case GST_V4L2_IO_RW:
|
2014-12-07 22:27:37 +00:00
|
|
|
if (allocator)
|
|
|
|
pool->allocator = g_object_ref (allocator);
|
2014-04-05 02:46:40 +00:00
|
|
|
pool->params = params;
|
|
|
|
/* No need to change the configuration */
|
|
|
|
goto done;
|
|
|
|
break;
|
2014-04-14 16:19:39 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2014-04-05 02:46:40 +00:00
|
|
|
}
|
2013-12-02 23:05:11 +00:00
|
|
|
|
2014-12-16 20:00:22 +00:00
|
|
|
/* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to
|
|
|
|
* instability and crash, disable it for now */
|
|
|
|
if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) {
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"libv4l2 converter detected, disabling CREATE_BUFS");
|
|
|
|
can_allocate = FALSE;
|
2015-02-20 04:08:34 +00:00
|
|
|
GST_OBJECT_FLAG_UNSET (pool->vallocator,
|
|
|
|
GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS
|
|
|
|
| GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS
|
|
|
|
| GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS);
|
2014-12-16 20:00:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:05:02 +00:00
|
|
|
if (min_buffers < GST_V4L2_MIN_BUFFERS (obj)) {
|
2014-04-05 02:46:40 +00:00
|
|
|
updated = TRUE;
|
2020-01-30 07:05:02 +00:00
|
|
|
min_buffers = GST_V4L2_MIN_BUFFERS (obj);
|
2014-04-05 02:46:40 +00:00
|
|
|
GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
|
|
|
|
}
|
2013-12-02 23:05:11 +00:00
|
|
|
|
2015-03-30 11:12:35 +00:00
|
|
|
/* respect driver requirements */
|
|
|
|
if (min_buffers < obj->min_buffers) {
|
|
|
|
updated = TRUE;
|
|
|
|
min_buffers = obj->min_buffers;
|
|
|
|
GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
|
|
|
|
}
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
|
|
|
|
updated = TRUE;
|
|
|
|
max_buffers = VIDEO_MAX_FRAME;
|
|
|
|
GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_buffers > max_buffers) {
|
|
|
|
updated = TRUE;
|
|
|
|
min_buffers = max_buffers;
|
|
|
|
GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers);
|
|
|
|
} else if (min_buffers != max_buffers) {
|
|
|
|
if (!can_allocate) {
|
|
|
|
updated = TRUE;
|
|
|
|
max_buffers = min_buffers;
|
|
|
|
GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 17:09:00 +00:00
|
|
|
if (!pool->add_videometa && obj->need_video_meta) {
|
|
|
|
GST_INFO_OBJECT (pool, "adding needed video meta");
|
|
|
|
updated = TRUE;
|
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
}
|
|
|
|
|
2014-12-19 17:30:03 +00:00
|
|
|
/* Always update the config to ensure the configured size matches */
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers,
|
|
|
|
max_buffers);
|
2014-04-05 02:46:40 +00:00
|
|
|
|
2014-05-12 22:03:18 +00:00
|
|
|
/* keep a GstVideoInfo with defaults for the when we need to copy */
|
|
|
|
gst_video_info_from_caps (&pool->caps_info, caps);
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
done:
|
|
|
|
ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
|
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
/* If anything was changed documentation recommend to return FALSE */
|
2014-04-05 02:46:40 +00:00
|
|
|
return !updated && ret;
|
2013-12-02 23:05:11 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_config:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
static GstFlowReturn
|
2018-10-11 00:24:53 +00:00
|
|
|
gst_v4l2_buffer_pool_resurrect_buffer (GstV4l2BufferPool * pool)
|
2017-08-05 16:23:30 +00:00
|
|
|
{
|
|
|
|
GstBufferPoolAcquireParams params = { 0 };
|
|
|
|
GstBuffer *buffer = NULL;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
|
|
|
|
|
|
|
|
/* block recursive calls to this function */
|
|
|
|
g_signal_handler_block (pool->vallocator, pool->group_released_handler);
|
|
|
|
|
|
|
|
params.flags =
|
|
|
|
(GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT |
|
|
|
|
GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
|
|
|
|
ret =
|
|
|
|
gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms);
|
|
|
|
|
|
|
|
if (ret == GST_FLOW_OK)
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
|
|
|
|
g_signal_handler_unblock (pool->vallocator, pool->group_released_handler);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
static gboolean
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
|
2013-12-02 23:05:11 +00:00
|
|
|
{
|
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
if (pool->streaming)
|
|
|
|
return TRUE;
|
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
2014-04-14 16:19:39 +00:00
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2017-08-05 16:23:30 +00:00
|
|
|
if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
|
2020-06-26 18:48:14 +00:00
|
|
|
guint num_queued;
|
|
|
|
guint i, n = 0;
|
|
|
|
|
2023-04-21 01:58:36 +00:00
|
|
|
GST_OBJECT_LOCK (pool);
|
2020-06-26 18:48:14 +00:00
|
|
|
num_queued = g_atomic_int_get (&pool->num_queued);
|
|
|
|
if (num_queued < pool->num_allocated)
|
|
|
|
n = pool->num_allocated - num_queued;
|
2023-04-21 01:58:36 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
2018-10-17 02:28:13 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
/* For captures, we need to enqueue buffers before we start streaming,
|
2019-09-02 12:27:35 +00:00
|
|
|
* so the driver don't underflow immediately. As we have put then back
|
2018-10-11 00:24:53 +00:00
|
|
|
* into the base class queue, resurrect them, then releasing will queue
|
2017-08-05 16:23:30 +00:00
|
|
|
* them back. */
|
2020-06-26 18:48:14 +00:00
|
|
|
for (i = 0; i < n; i++)
|
2018-10-17 02:28:13 +00:00
|
|
|
gst_v4l2_buffer_pool_resurrect_buffer (pool);
|
2017-08-05 16:23:30 +00:00
|
|
|
}
|
2014-04-29 20:49:52 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
|
|
|
|
goto streamon_failed;
|
2014-04-29 20:49:52 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
pool->streaming = TRUE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "Started streaming");
|
2014-05-25 00:20:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-04-29 20:49:52 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
return TRUE;
|
2014-05-04 23:51:48 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
streamon_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
|
|
|
|
g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 20:49:52 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
/* Call with streamlock held, or when streaming threads are down */
|
2015-01-08 16:37:23 +00:00
|
|
|
static void
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
|
|
|
|
{
|
2017-08-05 16:23:30 +00:00
|
|
|
GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
|
2014-05-25 00:20:07 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2017-08-05 16:23:30 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (!pool->streaming)
|
|
|
|
return;
|
2014-05-05 00:23:42 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_MMAP:
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF:
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-05-25 00:00:14 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
if (obj->ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
|
|
|
|
GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)",
|
|
|
|
errno, g_strerror (errno));
|
2014-05-05 00:23:42 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
pool->streaming = FALSE;
|
2015-01-08 16:37:23 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "Stopped streaming");
|
|
|
|
|
|
|
|
if (pool->vallocator)
|
|
|
|
gst_v4l2_allocator_flush (pool->vallocator);
|
2013-12-02 23:05:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-06-28 19:05:27 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
for (i = 0; i < VIDEO_MAX_FRAME; i++) {
|
2021-09-27 15:52:22 +00:00
|
|
|
gint old_buffer_state =
|
|
|
|
g_atomic_int_and (&pool->buffer_state[i], ~BUFFER_STATE_QUEUED);
|
2022-03-03 05:47:05 +00:00
|
|
|
if ((old_buffer_state & BUFFER_STATE_QUEUED) && pool->buffers[i]) {
|
2017-08-05 16:23:30 +00:00
|
|
|
GstBuffer *buffer = pool->buffers[i];
|
|
|
|
GstBufferPool *bpool = GST_BUFFER_POOL (pool);
|
2014-04-05 02:46:40 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
pool->buffers[i] = NULL;
|
2014-12-03 22:07:49 +00:00
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
if (!(old_buffer_state & BUFFER_STATE_OUTSTANDING)) {
|
|
|
|
if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
|
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, FALSE);
|
|
|
|
|
|
|
|
else /* Don't re-enqueue capture buffer on stop */
|
|
|
|
pclass->release_buffer (bpool, buffer);
|
|
|
|
}
|
2017-06-28 19:05:27 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
g_atomic_int_add (&pool->num_queued, -1);
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 02:46:40 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
2014-05-25 00:20:07 +00:00
|
|
|
GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
|
2013-12-02 23:05:11 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
|
|
|
GstStructure *config;
|
|
|
|
GstCaps *caps;
|
2014-04-05 02:46:40 +00:00
|
|
|
guint size, min_buffers, max_buffers;
|
2014-05-25 03:31:24 +00:00
|
|
|
guint max_latency, min_latency, copy_threshold = 0;
|
2017-08-05 16:23:30 +00:00
|
|
|
gboolean can_allocate = FALSE, ret = TRUE;
|
2013-12-02 23:05:11 +00:00
|
|
|
|
2014-05-23 19:56:24 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "activating pool");
|
|
|
|
|
2018-07-25 23:27:01 +00:00
|
|
|
if (pool->other_pool) {
|
|
|
|
GstBuffer *buffer;
|
|
|
|
|
2018-07-13 18:42:21 +00:00
|
|
|
if (!gst_buffer_pool_set_active (pool->other_pool, TRUE))
|
|
|
|
goto other_pool_failed;
|
|
|
|
|
2018-07-25 23:27:01 +00:00
|
|
|
if (gst_buffer_pool_acquire_buffer (pool->other_pool, &buffer, NULL) !=
|
|
|
|
GST_FLOW_OK)
|
|
|
|
goto other_pool_failed;
|
|
|
|
|
|
|
|
if (!gst_v4l2_object_try_import (obj, buffer)) {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
goto cannot_import;
|
|
|
|
}
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
}
|
|
|
|
|
2013-12-02 23:05:11 +00:00
|
|
|
config = gst_buffer_pool_get_config (bpool);
|
|
|
|
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
|
|
|
|
&max_buffers))
|
|
|
|
goto wrong_config;
|
|
|
|
|
2020-01-30 07:05:02 +00:00
|
|
|
min_latency = MAX (GST_V4L2_MIN_BUFFERS (obj), obj->min_buffers);
|
2014-05-25 03:31:24 +00:00
|
|
|
|
2012-04-23 16:01:31 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
2014-05-25 03:31:24 +00:00
|
|
|
can_allocate = TRUE;
|
2014-12-07 22:33:51 +00:00
|
|
|
#ifdef HAVE_LIBV4L2
|
|
|
|
/* This workaround a unfixable bug in libv4l2 when RW is emulated on top
|
|
|
|
* of MMAP. In this case, the first read initialize the queues, but the
|
|
|
|
* poll before that will always fail. Doing an empty read, forces the
|
|
|
|
* queue to be initialized now. We only do this if we have a streaming
|
|
|
|
* driver. */
|
2016-07-02 08:56:07 +00:00
|
|
|
if (obj->device_caps & V4L2_CAP_STREAMING)
|
2017-07-24 18:19:02 +00:00
|
|
|
obj->read (obj->video_fd, NULL, 0);
|
2014-12-07 22:33:51 +00:00
|
|
|
#endif
|
2012-04-23 16:01:31 +00:00
|
|
|
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:
|
|
|
|
{
|
2014-04-05 02:46:40 +00:00
|
|
|
guint count;
|
|
|
|
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
|
2012-04-23 16:01:31 +00:00
|
|
|
|
|
|
|
/* first, lets request buffers, and see how many we can get: */
|
2014-05-25 03:31:24 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
|
2012-04-23 16:01:31 +00:00
|
|
|
|
2014-05-25 03:31:24 +00:00
|
|
|
count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
|
2014-04-05 02:46:40 +00:00
|
|
|
V4L2_MEMORY_MMAP);
|
2018-10-17 02:28:13 +00:00
|
|
|
pool->num_allocated = count;
|
2012-04-23 16:01:31 +00:00
|
|
|
|
2020-01-30 07:05:02 +00:00
|
|
|
if (count < GST_V4L2_MIN_BUFFERS (obj)) {
|
2014-05-25 03:31:24 +00:00
|
|
|
min_buffers = count;
|
2012-04-23 16:01:31 +00:00
|
|
|
goto no_buffers;
|
|
|
|
}
|
2014-04-05 02:46:40 +00:00
|
|
|
|
|
|
|
/* V4L2 buffer pool are often very limited in the amount of buffers it
|
|
|
|
* can offer. The copy_threshold will workaround this limitation by
|
|
|
|
* falling back to copy if the pipeline needed more buffers. This also
|
2019-09-02 12:27:35 +00:00
|
|
|
* prevent having to do REQBUFS(N)/REQBUFS(0) every time configure is
|
2014-04-05 02:46:40 +00:00
|
|
|
* called. */
|
2015-02-20 04:12:31 +00:00
|
|
|
if (count != min_buffers || pool->enable_copy_threshold) {
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"Uncertain or not enough buffers, enabling copy threshold");
|
2014-05-25 03:31:24 +00:00
|
|
|
min_buffers = count;
|
|
|
|
copy_threshold = min_latency;
|
2012-04-23 16:01:31 +00:00
|
|
|
}
|
2013-12-02 23:05:11 +00:00
|
|
|
|
2012-04-23 16:01:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
2014-04-11 21:10:11 +00:00
|
|
|
{
|
|
|
|
guint count;
|
|
|
|
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate =
|
|
|
|
GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
|
2014-04-11 21:10:11 +00:00
|
|
|
|
2020-06-26 20:39:42 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
|
2014-04-11 21:10:11 +00:00
|
|
|
|
2020-06-26 20:39:42 +00:00
|
|
|
count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
|
2014-04-11 21:10:11 +00:00
|
|
|
V4L2_MEMORY_USERPTR);
|
2020-06-26 20:43:37 +00:00
|
|
|
pool->num_allocated = count;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
/* There is no rational to not get what we asked */
|
2014-05-25 03:31:24 +00:00
|
|
|
if (count < min_buffers) {
|
|
|
|
min_buffers = count;
|
2014-04-11 21:10:11 +00:00
|
|
|
goto no_buffers;
|
|
|
|
}
|
|
|
|
|
2014-05-25 03:31:24 +00:00
|
|
|
min_buffers = count;
|
2014-04-11 21:10:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-04-14 16:19:39 +00:00
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-04-11 21:10:11 +00:00
|
|
|
{
|
|
|
|
guint count;
|
|
|
|
|
2014-11-24 15:36:30 +00:00
|
|
|
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
|
2014-04-11 21:10:11 +00:00
|
|
|
|
2020-06-26 20:37:06 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
|
2014-04-11 21:10:11 +00:00
|
|
|
|
2020-06-26 20:37:06 +00:00
|
|
|
count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
|
2014-04-11 21:10:11 +00:00
|
|
|
V4L2_MEMORY_DMABUF);
|
2020-06-26 20:43:37 +00:00
|
|
|
pool->num_allocated = count;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
/* There is no rational to not get what we asked */
|
2014-05-25 03:31:24 +00:00
|
|
|
if (count < min_buffers) {
|
|
|
|
min_buffers = count;
|
2014-04-11 21:10:11 +00:00
|
|
|
goto no_buffers;
|
|
|
|
}
|
|
|
|
|
2014-05-25 03:31:24 +00:00
|
|
|
min_buffers = count;
|
2014-04-11 21:10:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-04-23 16:01:31 +00:00
|
|
|
default:
|
2014-05-25 03:31:24 +00:00
|
|
|
min_buffers = 0;
|
2012-04-23 16:01:31 +00:00
|
|
|
copy_threshold = 0;
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-25 03:31:24 +00:00
|
|
|
if (can_allocate)
|
|
|
|
max_latency = max_buffers;
|
|
|
|
else
|
|
|
|
max_latency = min_buffers;
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
pool->size = size;
|
2012-04-23 16:01:31 +00:00
|
|
|
pool->copy_threshold = copy_threshold;
|
2014-05-25 03:31:24 +00:00
|
|
|
pool->max_latency = max_latency;
|
|
|
|
pool->min_latency = min_latency;
|
2014-04-05 02:46:40 +00:00
|
|
|
pool->num_queued = 0;
|
2013-02-19 10:47:20 +00:00
|
|
|
|
2014-12-07 22:27:37 +00:00
|
|
|
if (max_buffers != 0 && max_buffers < min_buffers)
|
2014-05-15 15:39:39 +00:00
|
|
|
max_buffers = min_buffers;
|
|
|
|
|
2012-03-15 21:11:17 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
|
|
|
|
max_buffers);
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->set_config (bpool, config);
|
2014-04-05 02:46:40 +00:00
|
|
|
gst_structure_free (config);
|
2013-12-02 23:05:11 +00:00
|
|
|
|
|
|
|
/* now, allocate the buffers: */
|
2014-05-25 00:20:07 +00:00
|
|
|
if (!pclass->start (bpool))
|
2013-12-02 23:05:11 +00:00
|
|
|
goto start_failed;
|
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
if (!V4L2_TYPE_IS_OUTPUT (obj->type)) {
|
2020-06-26 20:43:37 +00:00
|
|
|
if (g_atomic_int_get (&pool->num_queued) < pool->num_allocated)
|
2018-07-13 20:51:24 +00:00
|
|
|
goto queue_failed;
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
pool->group_released_handler =
|
|
|
|
g_signal_connect_swapped (pool->vallocator, "group-released",
|
2018-10-11 00:24:53 +00:00
|
|
|
G_CALLBACK (gst_v4l2_buffer_pool_resurrect_buffer), pool);
|
2017-08-05 16:23:30 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_streamon (pool);
|
|
|
|
}
|
2014-04-05 02:46:40 +00:00
|
|
|
|
2017-08-05 16:23:30 +00:00
|
|
|
return ret;
|
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);
|
2014-05-09 15:56:52 +00:00
|
|
|
gst_structure_free (config);
|
2011-07-15 09:27:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-04-23 16:01:31 +00:00
|
|
|
no_buffers:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool,
|
2014-04-05 02:46:40 +00:00
|
|
|
"we received %d buffer from device '%s', we want at least %d",
|
2020-01-30 07:05:02 +00:00
|
|
|
min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS (obj));
|
2014-05-09 15:56:52 +00:00
|
|
|
gst_structure_free (config);
|
2012-04-23 16:01:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
start_failed:
|
|
|
|
{
|
2015-10-05 15:30:46 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "allocate failed");
|
2011-07-18 16:54:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-04-11 21:10:11 +00:00
|
|
|
other_pool_failed:
|
|
|
|
{
|
2018-07-26 02:16:59 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "failed to activate the other pool %"
|
2014-04-11 21:10:11 +00:00
|
|
|
GST_PTR_FORMAT, pool->other_pool);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-07-13 20:51:24 +00:00
|
|
|
queue_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-07-25 23:27:01 +00:00
|
|
|
cannot_import:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "cannot import buffers from downstream pool");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 04:12:31 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_buffer_pool_vallocator_stop (GstV4l2BufferPool * pool)
|
|
|
|
{
|
|
|
|
GstV4l2Return vret;
|
|
|
|
|
|
|
|
if (!pool->vallocator)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
vret = gst_v4l2_allocator_stop (pool->vallocator);
|
|
|
|
|
|
|
|
if (vret == GST_V4L2_BUSY)
|
|
|
|
GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
|
|
|
|
|
|
|
|
return (vret == GST_V4L2_OK);
|
|
|
|
}
|
|
|
|
|
2014-05-25 00:00:14 +00:00
|
|
|
static gboolean
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
|
2014-05-23 18:12:10 +00:00
|
|
|
{
|
2014-05-25 00:20:07 +00:00
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
gboolean ret;
|
2014-05-23 18:12:10 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "stopping pool");
|
|
|
|
|
|
|
|
if (pool->group_released_handler > 0) {
|
|
|
|
g_signal_handler_disconnect (pool->vallocator,
|
|
|
|
pool->group_released_handler);
|
|
|
|
pool->group_released_handler = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->other_pool) {
|
2015-02-18 01:26:55 +00:00
|
|
|
gst_buffer_pool_set_active (pool->other_pool, FALSE);
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_object_unref (pool->other_pool);
|
|
|
|
pool->other_pool = NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-25 18:15:51 +00:00
|
|
|
if (!pool->orphaned)
|
|
|
|
gst_v4l2_buffer_pool_streamoff (pool);
|
2014-05-25 00:20:07 +00:00
|
|
|
|
|
|
|
ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
|
|
|
|
|
2019-05-30 04:12:31 +00:00
|
|
|
if (ret)
|
|
|
|
ret = gst_v4l2_buffer_pool_vallocator_stop (pool);
|
2014-05-23 18:12:10 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:12:13 +00:00
|
|
|
gboolean
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_v4l2_buffer_pool_orphan (GstV4l2Object * v4l2object)
|
2019-01-24 15:12:13 +00:00
|
|
|
{
|
2022-11-28 22:26:50 +00:00
|
|
|
GstBufferPool *bpool = gst_v4l2_object_get_buffer_pool (v4l2object);
|
|
|
|
GstV4l2BufferPool *pool;
|
2019-01-24 15:12:13 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
2023-04-17 20:05:35 +00:00
|
|
|
/* Nothing to do if there is no pool */
|
|
|
|
if (!bpool)
|
|
|
|
return TRUE;
|
2020-06-25 18:15:51 +00:00
|
|
|
|
2022-11-28 22:26:50 +00:00
|
|
|
pool = GST_V4L2_BUFFER_POOL (bpool);
|
2019-01-24 15:12:13 +00:00
|
|
|
|
2022-11-28 22:26:50 +00:00
|
|
|
if (pool->orphaned != FALSE
|
|
|
|
|| !GST_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS (pool->vallocator)
|
|
|
|
|| g_getenv ("GST_V4L2_FORCE_DRAIN")) {
|
|
|
|
gst_object_unref (bpool);
|
2019-01-24 15:12:13 +00:00
|
|
|
return FALSE;
|
2022-11-28 22:26:50 +00:00
|
|
|
}
|
2019-01-24 15:12:13 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "orphaning pool");
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_buffer_pool_set_active (bpool, FALSE);
|
2019-05-30 02:13:07 +00:00
|
|
|
|
2020-06-25 18:15:51 +00:00
|
|
|
/* We lock to prevent racing with a return buffer in QBuf, and has a
|
|
|
|
* workaround of not being able to use the pool hidden activation lock. */
|
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
|
|
|
|
gst_v4l2_buffer_pool_streamoff (pool);
|
|
|
|
ret = gst_v4l2_allocator_orphan (pool->vallocator);
|
|
|
|
if (ret)
|
|
|
|
pool->orphaned = TRUE;
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
2019-01-24 15:12:13 +00:00
|
|
|
|
2020-06-25 18:15:51 +00:00
|
|
|
if (ret) {
|
2022-11-28 22:26:50 +00:00
|
|
|
GstBufferPool *old_pool;
|
|
|
|
GST_OBJECT_LOCK (v4l2object->element);
|
|
|
|
old_pool = v4l2object->pool;
|
|
|
|
v4l2object->pool = NULL;
|
|
|
|
GST_OBJECT_UNLOCK (v4l2object->element);
|
|
|
|
if (old_pool)
|
|
|
|
gst_object_unref (old_pool);
|
2020-06-25 18:15:51 +00:00
|
|
|
}
|
2019-05-30 02:13:07 +00:00
|
|
|
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_object_unref (bpool);
|
|
|
|
|
2019-01-24 15:12:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "start flushing");
|
|
|
|
|
2023-04-15 02:22:06 +00:00
|
|
|
gst_poll_set_flushing (pool->obj->poll, TRUE);
|
2014-05-25 00:20:07 +00:00
|
|
|
|
2014-07-03 19:28:45 +00:00
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
pool->empty = FALSE;
|
|
|
|
g_cond_broadcast (&pool->empty_cond);
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
|
2020-06-26 15:05:25 +00:00
|
|
|
if (pool->other_pool && gst_buffer_pool_is_active (pool->other_pool))
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "stop flushing");
|
|
|
|
|
2020-06-26 15:05:25 +00:00
|
|
|
if (pool->other_pool && gst_buffer_pool_is_active (pool->other_pool))
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
|
|
|
|
|
2023-04-15 02:22:06 +00:00
|
|
|
gst_poll_set_flushing (pool->obj->poll, FALSE);
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2018-04-02 16:59:33 +00:00
|
|
|
gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool, gboolean wait)
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
2018-04-02 16:59:33 +00:00
|
|
|
GstClockTime timeout;
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
timeout = GST_CLOCK_TIME_NONE;
|
|
|
|
else
|
|
|
|
timeout = 0;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2014-12-07 22:27:37 +00:00
|
|
|
/* In RW mode there is no queue, hence no need to wait while the queue is
|
|
|
|
* empty */
|
|
|
|
if (pool->obj->mode != GST_V4L2_IO_RW) {
|
|
|
|
GST_OBJECT_LOCK (pool);
|
2018-04-02 16:59:33 +00:00
|
|
|
|
|
|
|
if (!wait && pool->empty) {
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
2023-04-15 02:22:06 +00:00
|
|
|
return GST_V4L2_FLOW_LAST_BUFFER;
|
2018-04-02 16:59:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 22:27:37 +00:00
|
|
|
while (pool->empty)
|
|
|
|
g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
|
2018-04-02 16:59:33 +00:00
|
|
|
|
2014-12-07 22:27:37 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
}
|
2014-07-03 19:28:45 +00:00
|
|
|
|
2023-04-15 02:22:06 +00:00
|
|
|
if (!pool->obj->can_poll_device) {
|
2018-04-02 16:59:33 +00:00
|
|
|
if (wait)
|
2023-04-15 02:22:06 +00:00
|
|
|
return GST_FLOW_OK;
|
2018-04-02 16:59:33 +00:00
|
|
|
else
|
2023-04-15 02:22:06 +00:00
|
|
|
return GST_V4L2_FLOW_LAST_BUFFER;
|
2021-02-04 19:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 02:22:06 +00:00
|
|
|
return gst_v4l2_object_poll (pool->obj, timeout);
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2018-07-12 19:11:39 +00:00
|
|
|
gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf,
|
2020-10-19 14:56:04 +00:00
|
|
|
GstV4l2MemoryGroup * group, guint32 * frame_number)
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
2015-01-15 15:11:53 +00:00
|
|
|
const GstV4l2Object *obj = pool->obj;
|
2021-09-27 15:52:22 +00:00
|
|
|
gint old_buffer_state;
|
2011-07-18 16:54:49 +00:00
|
|
|
gint index;
|
|
|
|
|
2023-04-21 01:58:36 +00:00
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
index = group->buffer.index;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
old_buffer_state =
|
|
|
|
g_atomic_int_or (&pool->buffer_state[index], BUFFER_STATE_QUEUED);
|
|
|
|
if (old_buffer_state & BUFFER_STATE_QUEUED)
|
2014-05-26 16:34:42 +00:00
|
|
|
goto already_queued;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
GST_LOG_OBJECT (pool, "queuing buffer %i, previous-state = %i", index,
|
|
|
|
old_buffer_state);
|
2014-04-05 02:46:40 +00:00
|
|
|
|
2015-01-15 15:11:53 +00:00
|
|
|
if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
|
|
|
|
enum v4l2_field field;
|
|
|
|
|
2018-08-08 07:27:19 +00:00
|
|
|
/* Buffer field is the same as the one defined in format */
|
2015-01-15 15:11:53 +00:00
|
|
|
if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
|
|
|
|
field = obj->format.fmt.pix_mp.field;
|
|
|
|
else
|
|
|
|
field = obj->format.fmt.pix.field;
|
|
|
|
|
|
|
|
group->buffer.field = field;
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:56:04 +00:00
|
|
|
if (frame_number) {
|
|
|
|
group->buffer.timestamp.tv_sec = *frame_number;
|
|
|
|
group->buffer.timestamp.tv_usec = 0;
|
|
|
|
} else {
|
|
|
|
if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
|
|
|
|
GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buf);
|
|
|
|
GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp);
|
|
|
|
} else {
|
|
|
|
group->buffer.timestamp.tv_sec = -1;
|
|
|
|
group->buffer.timestamp.tv_usec = -1;
|
|
|
|
}
|
2015-07-20 08:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 18:15:51 +00:00
|
|
|
/* If the pool was orphaned, don't try to queue any returned buffers.
|
|
|
|
* This is done with the objet lock in order to synchronize with
|
|
|
|
* orphaning. */
|
|
|
|
if (pool->orphaned)
|
|
|
|
goto was_orphaned;
|
|
|
|
|
2016-07-28 16:51:24 +00:00
|
|
|
g_atomic_int_inc (&pool->num_queued);
|
|
|
|
pool->buffers[index] = buf;
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
|
2011-07-18 16:54:49 +00:00
|
|
|
goto queue_failed;
|
|
|
|
|
2014-07-03 19:28:45 +00:00
|
|
|
pool->empty = FALSE;
|
|
|
|
g_cond_signal (&pool->empty_cond);
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
already_queued:
|
|
|
|
{
|
2014-05-26 16:34:42 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index);
|
2023-04-21 01:58:36 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
2014-05-26 16:34:42 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
2020-06-25 18:15:51 +00:00
|
|
|
was_orphaned:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (pool, "pool was orphaned, not queuing back buffer.");
|
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
|
2021-09-27 15:52:22 +00:00
|
|
|
g_atomic_int_and (&pool->buffer_state[index], ~BUFFER_STATE_QUEUED);
|
2020-06-25 18:15:51 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
return GST_FLOW_FLUSHING;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
queue_failed:
|
|
|
|
{
|
2014-04-05 02:46:40 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
|
2014-04-29 16:57:08 +00:00
|
|
|
/* Mark broken buffer to the allocator */
|
2014-04-05 02:46:40 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
|
2014-05-23 23:10:21 +00:00
|
|
|
g_atomic_int_add (&pool->num_queued, -1);
|
|
|
|
pool->buffers[index] = NULL;
|
2021-09-27 15:52:22 +00:00
|
|
|
g_atomic_int_and (&pool->buffer_state[index], ~BUFFER_STATE_QUEUED);
|
2016-07-28 16:51:24 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
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
|
2018-04-02 16:59:33 +00:00
|
|
|
gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
|
2021-09-27 15:52:22 +00:00
|
|
|
gboolean * outstanding, gboolean wait)
|
2009-08-04 07:14:20 +00:00
|
|
|
{
|
2024-04-09 02:29:46 +00:00
|
|
|
GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool);
|
2011-07-18 16:54:49 +00:00
|
|
|
GstFlowReturn res;
|
2018-04-02 16:59:33 +00:00
|
|
|
GstBuffer *outbuf = NULL;
|
2011-07-12 16:13:42 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2012-04-24 10:37:33 +00:00
|
|
|
GstClockTime timestamp;
|
2014-04-05 02:46:40 +00:00
|
|
|
GstV4l2MemoryGroup *group;
|
2023-06-22 14:10:43 +00:00
|
|
|
const GstVideoInfo *info = &obj->info;
|
2013-11-19 17:16:27 +00:00
|
|
|
gint i;
|
2021-09-27 15:52:22 +00:00
|
|
|
gint old_buffer_state;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2018-04-02 16:59:33 +00:00
|
|
|
if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
|
2014-04-05 02:46:40 +00:00
|
|
|
goto poll_failed;
|
2013-11-13 12:05:40 +00:00
|
|
|
|
2021-02-04 15:10:34 +00:00
|
|
|
if (res == GST_V4L2_FLOW_LAST_BUFFER) {
|
2018-04-02 16:59:33 +00:00
|
|
|
GST_LOG_OBJECT (pool, "nothing to dequeue");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:13:32 +00:00
|
|
|
if (res == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
|
|
|
|
GST_INFO_OBJECT (pool, "Resolution change detected.");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
GST_LOG_OBJECT (pool, "dequeueing a buffer");
|
|
|
|
|
2015-01-21 17:09:03 +00:00
|
|
|
res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
|
2023-05-18 15:58:51 +00:00
|
|
|
if (res == GST_V4L2_FLOW_LAST_BUFFER)
|
2015-01-21 17:09:03 +00:00
|
|
|
goto eos;
|
|
|
|
if (res != GST_FLOW_OK)
|
2014-04-05 02:46:40 +00:00
|
|
|
goto dqbuf_failed;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
old_buffer_state =
|
|
|
|
g_atomic_int_and (&pool->buffer_state[group->buffer.index],
|
|
|
|
~BUFFER_STATE_QUEUED);
|
|
|
|
if (!(old_buffer_state & BUFFER_STATE_QUEUED))
|
|
|
|
goto no_buffer;
|
|
|
|
|
|
|
|
if (outstanding) {
|
|
|
|
*outstanding = (old_buffer_state & BUFFER_STATE_OUTSTANDING) != 0;
|
|
|
|
} else if (old_buffer_state & BUFFER_STATE_OUTSTANDING) {
|
|
|
|
GST_WARNING_OBJECT (pool, "unexpected outstanding buffer %u",
|
|
|
|
group->buffer.index);
|
|
|
|
}
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
outbuf = pool->buffers[group->buffer.index];
|
2011-07-15 09:27:18 +00:00
|
|
|
if (outbuf == NULL)
|
2011-07-19 15:59:55 +00:00
|
|
|
goto no_buffer;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
pool->buffers[group->buffer.index] = NULL;
|
2014-07-03 19:28:45 +00:00
|
|
|
if (g_atomic_int_dec_and_test (&pool->num_queued)) {
|
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
pool->empty = TRUE;
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2024-04-09 02:29:46 +00:00
|
|
|
if (group->buffer.flags & V4L2_BUF_FLAG_LAST &&
|
|
|
|
group->planes[0].bytesused == 0) {
|
|
|
|
GST_DEBUG_OBJECT (pool, "Empty last buffer, signalling eos.");
|
2024-09-09 15:23:53 +00:00
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, outbuf, FALSE);
|
|
|
|
*buffer = NULL;
|
2024-04-09 02:29:46 +00:00
|
|
|
goto eos;
|
|
|
|
}
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
|
|
|
|
|
|
|
|
for (i = 0; i < group->n_mem; i++) {
|
2023-06-22 14:10:43 +00:00
|
|
|
const GstVideoFormatInfo *finfo = info->finfo;
|
|
|
|
|
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 %"
|
2021-09-27 15:52:22 +00:00
|
|
|
GST_TIME_FORMAT ", pool-queued=%d, buffer=%p, previous-state=%i",
|
|
|
|
outbuf, group->buffer.sequence, group->buffer.index, group->mem[i],
|
2014-04-05 02:46:40 +00:00
|
|
|
group->planes[i].bytesused, i, group->buffer.flags,
|
2021-09-27 15:52:22 +00:00
|
|
|
GST_TIME_ARGS (timestamp), pool->num_queued, outbuf, old_buffer_state);
|
2017-10-06 21:36:34 +00:00
|
|
|
|
2023-09-05 20:15:19 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) == GST_VIDEO_FORMAT_ENCODED)
|
|
|
|
break;
|
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
|
|
|
|
obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
|
|
|
|
/* Ensure our offset matches the expected plane size, or image size if
|
|
|
|
* there is only one memory */
|
|
|
|
if (group->n_mem == 1) {
|
|
|
|
gst_memory_resize (group->mem[0], 0, info->size + info->offset[0]);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-22 14:10:43 +00:00
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (!GST_VIDEO_FORMAT_INFO_IS_TILED (finfo))
|
|
|
|
gst_memory_resize (group->mem[i], 0, obj->plane_size[i]);
|
|
|
|
}
|
2013-11-13 12:05:40 +00:00
|
|
|
}
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2015-03-08 21:15:53 +00:00
|
|
|
/* Ignore timestamp and field for OUTPUT device */
|
|
|
|
if (V4L2_TYPE_IS_OUTPUT (obj->type))
|
|
|
|
goto done;
|
|
|
|
|
2015-03-01 18:46:18 +00:00
|
|
|
/* Check for driver bug in reporting feild */
|
|
|
|
if (group->buffer.field == V4L2_FIELD_ANY) {
|
|
|
|
/* Only warn once to avoid the spamming */
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (!pool->has_warned_on_buggy_field) {
|
|
|
|
pool->has_warned_on_buggy_field = TRUE;
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"Driver should never set v4l2_buffer.field to ANY");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Use the value from the format (works for UVC bug) */
|
|
|
|
group->buffer.field = obj->format.fmt.pix.field;
|
|
|
|
|
|
|
|
/* If driver also has buggy S_FMT, assume progressive */
|
|
|
|
if (group->buffer.field == V4L2_FIELD_ANY) {
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
if (!pool->has_warned_on_buggy_field) {
|
|
|
|
pool->has_warned_on_buggy_field = TRUE;
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"Driver should never set v4l2_format.pix.field to ANY");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
group->buffer.field = V4L2_FIELD_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 10:04:57 +00:00
|
|
|
/* set top/bottom field first if v4l2_buffer has the information */
|
v4l2: Clean up interlace support
Rather than try and guess interlace support as part of checking supported
sizes, look for interlace support specifically in its own function.
As a cleanup, use V4L2_FIELD_ANY when probing sizes, which should result in
the driver doing the right thing.
With my capture setup, this gets me the following sample caps:
For 1080i resolution:
video/x-raw, format=(string)YUY2, width=(int)1920, height=(int)1080, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)interleaved, framerate=(fraction){ 25/1, 30/1 }
For 720p resolution:
video/x-raw, format=(string)YUY2, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)progressive, framerate=(fraction){ 50/1, 60/1 }
For 576i/p resolution (both possible at the point of query):
video/x-raw, format=(string)YUY2, width=(int)720, height=(int)576, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string){ progressive, interleaved }, framerate=(fraction){ 25/1, 50/1 }
This, in turn, makes 576i work correctly; with the old code,
the caps would be interlace-mode=progressive for interlaced video.
https://bugzilla.gnome.org/show_bug.cgi?id=726194
2014-10-30 17:41:19 +00:00
|
|
|
switch (group->buffer.field) {
|
|
|
|
case V4L2_FIELD_NONE:
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
break;
|
2018-08-08 07:27:19 +00:00
|
|
|
case V4L2_FIELD_TOP:
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
|
|
|
|
break;
|
|
|
|
case V4L2_FIELD_BOTTOM:
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
|
|
|
|
break;
|
v4l2: Clean up interlace support
Rather than try and guess interlace support as part of checking supported
sizes, look for interlace support specifically in its own function.
As a cleanup, use V4L2_FIELD_ANY when probing sizes, which should result in
the driver doing the right thing.
With my capture setup, this gets me the following sample caps:
For 1080i resolution:
video/x-raw, format=(string)YUY2, width=(int)1920, height=(int)1080, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)interleaved, framerate=(fraction){ 25/1, 30/1 }
For 720p resolution:
video/x-raw, format=(string)YUY2, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)progressive, framerate=(fraction){ 50/1, 60/1 }
For 576i/p resolution (both possible at the point of query):
video/x-raw, format=(string)YUY2, width=(int)720, height=(int)576, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string){ progressive, interleaved }, framerate=(fraction){ 25/1, 50/1 }
This, in turn, makes 576i work correctly; with the old code,
the caps would be interlace-mode=progressive for interlaced video.
https://bugzilla.gnome.org/show_bug.cgi?id=726194
2014-10-30 17:41:19 +00:00
|
|
|
case V4L2_FIELD_INTERLACED_TB:
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
break;
|
|
|
|
case V4L2_FIELD_INTERLACED_BT:
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
break;
|
2014-12-09 13:01:50 +00:00
|
|
|
case V4L2_FIELD_INTERLACED:
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
if (obj->tv_norm == V4L2_STD_NTSC_M ||
|
|
|
|
obj->tv_norm == V4L2_STD_NTSC_M_JP ||
|
|
|
|
obj->tv_norm == V4L2_STD_NTSC_M_KR) {
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
}
|
|
|
|
break;
|
v4l2: Clean up interlace support
Rather than try and guess interlace support as part of checking supported
sizes, look for interlace support specifically in its own function.
As a cleanup, use V4L2_FIELD_ANY when probing sizes, which should result in
the driver doing the right thing.
With my capture setup, this gets me the following sample caps:
For 1080i resolution:
video/x-raw, format=(string)YUY2, width=(int)1920, height=(int)1080, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)interleaved, framerate=(fraction){ 25/1, 30/1 }
For 720p resolution:
video/x-raw, format=(string)YUY2, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)progressive, framerate=(fraction){ 50/1, 60/1 }
For 576i/p resolution (both possible at the point of query):
video/x-raw, format=(string)YUY2, width=(int)720, height=(int)576, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string){ progressive, interleaved }, framerate=(fraction){ 25/1, 50/1 }
This, in turn, makes 576i work correctly; with the old code,
the caps would be interlace-mode=progressive for interlaced video.
https://bugzilla.gnome.org/show_bug.cgi?id=726194
2014-10-30 17:41:19 +00:00
|
|
|
default:
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
GST_FIXME_OBJECT (pool,
|
|
|
|
"Unhandled enum v4l2_field %d - treating as progressive",
|
|
|
|
group->buffer.field);
|
2015-03-01 18:48:45 +00:00
|
|
|
break;
|
2011-12-19 18:03:52 +00:00
|
|
|
}
|
2014-02-21 19:46:44 +00:00
|
|
|
|
2023-04-20 21:31:32 +00:00
|
|
|
if (!gst_v4l2_object_is_raw (obj)) {
|
2017-08-08 10:37:12 +00:00
|
|
|
if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) ||
|
|
|
|
GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG ||
|
|
|
|
GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG ||
|
|
|
|
GST_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG)
|
2013-09-12 14:56:56 +00:00
|
|
|
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
|
|
|
|
2014-11-16 17:34:17 +00:00
|
|
|
if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
|
|
|
|
|
2012-04-24 10:37:33 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
|
2014-11-14 12:48:51 +00:00
|
|
|
GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
|
|
|
|
GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
|
2012-04-24 10:37:33 +00:00
|
|
|
|
2015-03-08 21:15:53 +00:00
|
|
|
done:
|
2011-07-15 09:27:18 +00:00
|
|
|
*buffer = outbuf;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2018-04-02 16:59:33 +00:00
|
|
|
return res;
|
2009-08-04 07:14:20 +00:00
|
|
|
|
2011-07-11 10:04:57 +00:00
|
|
|
/* ERRORS */
|
2014-04-05 02:46:40 +00:00
|
|
|
poll_failed:
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
2015-01-21 17:09:03 +00:00
|
|
|
eos:
|
|
|
|
{
|
2023-05-18 15:58:51 +00:00
|
|
|
return GST_V4L2_FLOW_LAST_BUFFER;
|
2015-01-21 17:09:03 +00:00
|
|
|
}
|
2014-04-05 02:46:40 +00:00
|
|
|
dqbuf_failed:
|
2011-07-11 10:04:57 +00:00
|
|
|
{
|
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.",
|
2014-04-05 02:46:40 +00:00
|
|
|
group->buffer.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);
|
2014-05-25 00:20:07 +00:00
|
|
|
GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
|
2011-07-18 16:54:49 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2011-07-15 11:35:14 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "acquire");
|
|
|
|
|
2018-10-11 00:24:53 +00:00
|
|
|
/* If this is being called to resurrect a lost buffer */
|
2014-11-03 10:44:28 +00:00
|
|
|
if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
|
2014-05-25 00:20:07 +00:00
|
|
|
ret = pclass->acquire_buffer (bpool, buffer, params);
|
2014-04-11 21:10:11 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
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:
|
2014-04-11 21:10:11 +00:00
|
|
|
{
|
2011-07-18 16:54:49 +00:00
|
|
|
/* take empty buffer from the pool */
|
2014-05-25 00:20:07 +00:00
|
|
|
ret = pclass->acquire_buffer (bpool, buffer, params);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
2014-04-11 21:10:11 +00:00
|
|
|
}
|
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:
|
2014-12-03 21:40:49 +00:00
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-04-11 21:10:11 +00:00
|
|
|
{
|
2011-07-18 16:54:49 +00:00
|
|
|
/* 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. */
|
2021-09-27 15:52:22 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer, NULL, TRUE);
|
2014-04-11 21:10:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
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;
|
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
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 */
|
2014-05-25 00:20:07 +00:00
|
|
|
ret = pclass->acquire_buffer (bpool, buffer, params);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_MMAP:
|
2014-04-10 20:26:34 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2014-04-11 21:10:11 +00:00
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2011-07-25 13:47:05 +00:00
|
|
|
/* get a free unqueued buffer */
|
2014-05-25 00:20:07 +00:00
|
|
|
ret = pclass->acquire_buffer (bpool, buffer, params);
|
2011-07-18 16:54:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
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:
|
2021-09-27 15:52:22 +00:00
|
|
|
/* Mark buffer as outstanding */
|
|
|
|
if (ret == GST_FLOW_OK) {
|
|
|
|
GstV4l2MemoryGroup *group;
|
2024-04-05 12:09:18 +00:00
|
|
|
if (gst_v4l2_is_buffer_valid (*buffer, &group, TRUE)) {
|
2021-09-27 15:52:22 +00:00
|
|
|
GST_LOG_OBJECT (pool, "mark buffer %u outstanding", group->buffer.index);
|
|
|
|
g_atomic_int_or (&pool->buffer_state[group->buffer.index],
|
|
|
|
BUFFER_STATE_OUTSTANDING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 11:35:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
/*
|
|
|
|
* Completes a release buffer operation
|
|
|
|
*
|
|
|
|
* Before calling this function:
|
|
|
|
* - The buffer state (if applicable) must have already been updated.
|
|
|
|
* - The buffer must not be outstanding.
|
|
|
|
* - The "queued" argument contains whether the buffer is currently queued.
|
|
|
|
*/
|
2011-07-15 09:27:18 +00:00
|
|
|
static void
|
2021-09-27 15:52:22 +00:00
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (GstBufferPool * bpool,
|
|
|
|
GstBuffer * buffer, gboolean queued)
|
2011-07-15 09:27:18 +00:00
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
2014-05-25 00:20:07 +00:00
|
|
|
GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
|
2011-07-18 16:54:49 +00:00
|
|
|
GstV4l2Object *obj = pool->obj;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "complete release buffer %p (queued = %s)", buffer,
|
|
|
|
queued ? "yes" : "no");
|
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. */
|
2021-09-27 15:52:22 +00:00
|
|
|
if (queued) {
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"capture buffer %p was release while still queued", buffer);
|
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* release back in the pool */
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->release_buffer (bpool, buffer);
|
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:
|
2014-04-11 21:10:11 +00:00
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-04-05 02:46:40 +00:00
|
|
|
{
|
2015-03-02 11:04:00 +00:00
|
|
|
GstV4l2MemoryGroup *group;
|
2024-04-05 12:09:18 +00:00
|
|
|
if (gst_v4l2_is_buffer_valid (buffer, &group, TRUE)) {
|
2018-07-24 16:07:22 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
|
2015-03-02 11:04:00 +00:00
|
|
|
gst_v4l2_allocator_reset_group (pool->vallocator, group);
|
2014-04-05 02:46:40 +00:00
|
|
|
/* queue back in the device */
|
2014-04-11 21:10:11 +00:00
|
|
|
if (pool->other_pool)
|
2018-07-24 16:07:22 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
|
|
|
|
if (ret != GST_FLOW_OK ||
|
2020-10-19 14:56:04 +00:00
|
|
|
gst_v4l2_buffer_pool_qbuf (pool, buffer, group,
|
|
|
|
NULL) != GST_FLOW_OK)
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->release_buffer (bpool, buffer);
|
2014-04-05 02:46:40 +00:00
|
|
|
} else {
|
2019-09-02 12:27:35 +00:00
|
|
|
/* Simply release invalid/modified buffer, the allocator will
|
2014-04-05 02:46:40 +00:00
|
|
|
* give it back later */
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->release_buffer (bpool, buffer);
|
2014-04-05 02:46:40 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
2014-04-05 02:46:40 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
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 */
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->release_buffer (bpool, buffer);
|
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_MMAP:
|
2014-04-10 20:26:34 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2014-04-11 21:10:11 +00:00
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2011-07-26 09:56:07 +00:00
|
|
|
{
|
2014-04-05 02:46:40 +00:00
|
|
|
GstV4l2MemoryGroup *group;
|
2013-04-03 09:09:37 +00:00
|
|
|
guint index;
|
2011-07-26 09:56:07 +00:00
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (!gst_v4l2_is_buffer_valid (buffer, &group, TRUE)) {
|
2019-09-02 12:27:35 +00:00
|
|
|
/* Simply release invalid/modified buffer, the allocator will
|
2014-04-05 02:46:40 +00:00
|
|
|
* give it back later */
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->release_buffer (bpool, buffer);
|
2014-04-05 02:46:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-26 09:56:07 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
index = group->buffer.index;
|
2013-04-03 09:09:37 +00:00
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
if (!queued) {
|
2013-04-03 09:09:37 +00:00
|
|
|
GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
|
|
|
|
index);
|
2012-05-03 14:07:27 +00:00
|
|
|
|
2014-04-18 21:02:50 +00:00
|
|
|
/* Remove qdata, this will unmap any map data in userptr */
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (buffer),
|
|
|
|
GST_V4L2_IMPORT_QUARK, NULL, NULL);
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
/* reset to default size */
|
2014-04-18 21:02:50 +00:00
|
|
|
gst_v4l2_allocator_reset_group (pool->vallocator, group);
|
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. */
|
2014-05-25 00:20:07 +00:00
|
|
|
pclass->release_buffer (bpool, buffer);
|
2011-07-26 09:56:07 +00:00
|
|
|
} else {
|
2015-01-07 22:48:31 +00:00
|
|
|
/* 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. */
|
|
|
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
GstV4l2MemoryGroup *group;
|
|
|
|
gboolean queued = FALSE;
|
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
if (gst_v4l2_is_buffer_valid (buffer, &group, TRUE)) {
|
2021-09-27 15:52:22 +00:00
|
|
|
gint old_buffer_state =
|
|
|
|
g_atomic_int_and (&pool->buffer_state[group->buffer.index],
|
|
|
|
~BUFFER_STATE_OUTSTANDING);
|
|
|
|
queued = (old_buffer_state & BUFFER_STATE_QUEUED) != 0;
|
|
|
|
GST_LOG_OBJECT (pool, "mark buffer %u not outstanding",
|
|
|
|
group->buffer.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer, queued);
|
|
|
|
}
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
static void
|
2014-10-08 08:31:21 +00:00
|
|
|
gst_v4l2_buffer_pool_dispose (GObject * object)
|
2011-07-15 09:27:18 +00:00
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
|
2013-06-03 15:07:10 +00:00
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
if (pool->vallocator)
|
|
|
|
gst_object_unref (pool->vallocator);
|
2014-10-08 08:31:21 +00:00
|
|
|
pool->vallocator = NULL;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
2012-04-23 16:01:31 +00:00
|
|
|
if (pool->allocator)
|
2012-07-09 14:27:10 +00:00
|
|
|
gst_object_unref (pool->allocator);
|
2014-10-08 08:31:21 +00:00
|
|
|
pool->allocator = NULL;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
if (pool->other_pool)
|
|
|
|
gst_object_unref (pool->other_pool);
|
2014-10-08 08:31:21 +00:00
|
|
|
pool->other_pool = NULL;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
|
|
|
|
|
|
|
|
if (pool->video_fd >= 0)
|
2017-07-24 20:45:40 +00:00
|
|
|
pool->obj->close (pool->video_fd);
|
2014-10-08 08:31:21 +00:00
|
|
|
|
2017-07-24 20:13:56 +00:00
|
|
|
/* This can't be done in dispose method because we must not set pointer
|
2014-10-08 08:31:21 +00:00
|
|
|
* to NULL as it is part of the v4l2object and dispose could be called
|
|
|
|
* multiple times */
|
2013-06-05 14:32:30 +00:00
|
|
|
gst_object_unref (pool->obj->element);
|
|
|
|
|
2014-07-26 15:06:39 +00:00
|
|
|
g_cond_clear (&pool->empty_cond);
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
/* FIXME have we done enough here ? */
|
|
|
|
|
2011-07-15 09:27:18 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
|
|
|
|
{
|
2014-07-03 19:28:45 +00:00
|
|
|
g_cond_init (&pool->empty_cond);
|
|
|
|
pool->empty = TRUE;
|
2019-01-24 15:12:13 +00:00
|
|
|
pool->orphaned = FALSE;
|
2021-09-27 15:52:22 +00:00
|
|
|
for (gint i = 0; i < VIDEO_MAX_FRAME; i++)
|
|
|
|
g_atomic_int_set (&pool->buffer_state[i], BUFFER_STATE_FREE);
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-10-08 08:31:21 +00:00
|
|
|
object_class->dispose = gst_v4l2_buffer_pool_dispose;
|
2011-07-15 09:27:18 +00:00
|
|
|
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;
|
2014-05-25 00:20:07 +00:00
|
|
|
bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
|
|
|
|
bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
|
2015-02-20 04:07:23 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
|
|
|
|
"V4L2 Buffer Pool");
|
2016-02-20 11:51:56 +00:00
|
|
|
GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2014-04-05 02:46:40 +00:00
|
|
|
gchar *name, *parent_name;
|
2011-07-18 08:51:21 +00:00
|
|
|
gint fd;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2017-07-24 20:45:40 +00:00
|
|
|
fd = obj->dup (obj->video_fd);
|
2011-07-18 08:51:21 +00:00
|
|
|
if (fd < 0)
|
2011-07-15 09:27:18 +00:00
|
|
|
goto dup_failed;
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
/* setting a significant unique name */
|
|
|
|
parent_name = gst_object_get_name (GST_OBJECT (obj->element));
|
2020-03-23 07:34:46 +00:00
|
|
|
name = g_strdup_printf ("%s:pool%u:%s",
|
|
|
|
parent_name, obj->pool_seq++,
|
|
|
|
V4L2_TYPE_IS_OUTPUT (obj->type) ? "sink" : "src");
|
2014-04-05 02:46:40 +00:00
|
|
|
g_free (parent_name);
|
|
|
|
|
|
|
|
pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
|
|
|
|
"name", name, NULL);
|
2017-05-15 16:51:47 +00:00
|
|
|
g_object_ref_sink (pool);
|
2014-04-05 02:46:40 +00:00
|
|
|
g_free (name);
|
|
|
|
|
2011-07-18 08:51:21 +00:00
|
|
|
pool->video_fd = fd;
|
2011-07-15 09:27:18 +00:00
|
|
|
pool->obj = obj;
|
2014-03-27 22:41:07 +00:00
|
|
|
|
2017-07-24 18:19:02 +00:00
|
|
|
pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
|
2014-06-12 20:36:24 +00:00
|
|
|
if (pool->vallocator == NULL)
|
|
|
|
goto allocator_failed;
|
2014-04-05 02:46:40 +00:00
|
|
|
|
2014-04-15 17:30:02 +00:00
|
|
|
gst_object_ref (obj->element);
|
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-05-06 16:58:59 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0);
|
2014-04-15 17:30:02 +00:00
|
|
|
/* This will simply set a default config, but will not configure the pool
|
|
|
|
* because min and max are not valid */
|
|
|
|
gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pool), config);
|
2013-06-05 14:32:30 +00:00
|
|
|
|
2011-08-04 11:50:01 +00:00
|
|
|
return GST_BUFFER_POOL (pool);
|
2011-07-15 09:27:18 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
dup_failed:
|
|
|
|
{
|
2014-04-05 02:46:40 +00:00
|
|
|
GST_ERROR ("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-06-12 20:36:24 +00:00
|
|
|
allocator_failed:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
|
2014-11-07 15:41:52 +00:00
|
|
|
gst_object_unref (pool);
|
2014-06-12 20:36:24 +00:00
|
|
|
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
|
|
|
|
2014-05-06 16:58:59 +00:00
|
|
|
toread = obj->info.size;
|
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 {
|
2018-04-02 16:59:33 +00:00
|
|
|
if ((res = gst_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK)
|
2011-07-18 16:54:49 +00:00
|
|
|
goto poll_error;
|
2011-07-15 09:27:18 +00:00
|
|
|
|
2017-07-24 18:19:02 +00:00
|
|
|
amount = obj->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
|
2014-04-29 17:05:41 +00:00
|
|
|
* @buf: a #GstBuffer, maybe be replaced
|
2020-10-19 14:56:04 +00:00
|
|
|
* @frame_number: 32 bit frame number or %NULL
|
2011-07-18 16:54:49 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2020-10-19 14:56:04 +00:00
|
|
|
* If non-%NULL and an output device, @frame_number is stored inside the timestamp for output devices and read
|
|
|
|
* back from the timestamp for capture devices.
|
|
|
|
*
|
2011-07-18 16:54:49 +00:00
|
|
|
* Returns: %GST_FLOW_OK on success.
|
|
|
|
*/
|
|
|
|
GstFlowReturn
|
2020-10-19 14:56:04 +00:00
|
|
|
gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf,
|
|
|
|
guint32 * frame_number)
|
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;
|
|
|
|
|
2022-08-15 18:30:50 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "process buffer %p", *buf);
|
2011-07-19 15:59:55 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
if (GST_BUFFER_POOL_IS_FLUSHING (pool))
|
|
|
|
return GST_FLOW_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 */
|
|
|
|
switch (obj->mode) {
|
|
|
|
case GST_V4L2_IO_RW:
|
|
|
|
/* capture into the buffer */
|
2014-04-29 17:05:41 +00:00
|
|
|
ret = gst_v4l2_do_read (pool, *buf);
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_V4L2_IO_MMAP:
|
2014-04-10 20:26:34 +00:00
|
|
|
case GST_V4L2_IO_DMABUF:
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
|
|
|
GstBuffer *tmp;
|
|
|
|
|
2014-04-29 17:05:41 +00:00
|
|
|
if ((*buf)->pool == bpool) {
|
2014-12-03 22:07:49 +00:00
|
|
|
guint num_queued;
|
2016-05-31 19:34:04 +00:00
|
|
|
gsize size = gst_buffer_get_size (*buf);
|
2014-12-03 22:07:49 +00:00
|
|
|
|
2018-07-13 20:02:02 +00:00
|
|
|
/* Legacy M2M devices return empty buffer when drained */
|
2022-07-08 10:22:58 +00:00
|
|
|
if (size == 0 && GST_V4L2_IS_M2M (obj->device_caps)) {
|
|
|
|
gst_v4l2_buffer_pool_resize_buffer (bpool, *buf);
|
2018-07-13 20:02:02 +00:00
|
|
|
goto eos;
|
2022-07-08 10:22:58 +00:00
|
|
|
}
|
2014-07-11 17:35:10 +00:00
|
|
|
|
2018-05-28 19:19:52 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) !=
|
|
|
|
GST_VIDEO_FORMAT_ENCODED && size < pool->size)
|
|
|
|
goto buffer_truncated;
|
|
|
|
|
2014-12-03 22:07:49 +00:00
|
|
|
num_queued = g_atomic_int_get (&pool->num_queued);
|
|
|
|
GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.",
|
|
|
|
num_queued);
|
|
|
|
|
|
|
|
/* If we have no more buffer, and can allocate it time to do so */
|
|
|
|
if (num_queued == 0) {
|
|
|
|
if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
|
2023-04-21 17:30:43 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "Resurrect for empty queue");
|
2018-10-11 00:24:53 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
|
2023-04-21 17:30:43 +00:00
|
|
|
if (ret == GST_FLOW_OK || ret == GST_FLOW_FLUSHING)
|
2014-12-03 22:07:49 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:35:10 +00:00
|
|
|
/* start copying buffers when we are running low on buffers */
|
2014-12-03 22:07:49 +00:00
|
|
|
if (num_queued < pool->copy_threshold) {
|
2014-07-11 17:35:10 +00:00
|
|
|
GstBuffer *copy;
|
|
|
|
|
2014-11-24 15:36:30 +00:00
|
|
|
if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
|
2023-04-21 17:30:43 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "Resurrect for threshold");
|
2018-10-11 00:24:53 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_resurrect_buffer (pool);
|
2023-04-21 17:30:43 +00:00
|
|
|
if (ret == GST_FLOW_OK || ret == GST_FLOW_FLUSHING)
|
2014-07-11 17:35:10 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the buffer */
|
|
|
|
copy = gst_buffer_copy_region (*buf,
|
|
|
|
GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
|
|
|
|
GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy);
|
|
|
|
|
|
|
|
/* and requeue so that we can continue capturing */
|
|
|
|
gst_buffer_unref (*buf);
|
|
|
|
*buf = copy;
|
|
|
|
}
|
|
|
|
|
2015-12-01 17:20:23 +00:00
|
|
|
ret = GST_FLOW_OK;
|
2014-07-11 17:35:10 +00:00
|
|
|
/* nothing, data was inside the buffer when we did _acquire() */
|
|
|
|
goto done;
|
2014-03-25 19:21:03 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
|
|
|
|
/* buffer not from our pool, grab a frame and copy it into the target */
|
2021-09-27 15:52:22 +00:00
|
|
|
if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp, NULL, TRUE))
|
2018-04-02 16:59:33 +00:00
|
|
|
!= 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) {
|
2021-09-27 15:52:22 +00:00
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
|
2014-11-16 17:34:17 +00:00
|
|
|
|
2018-07-13 20:02:02 +00:00
|
|
|
/* Legacy M2M devices return empty buffer when drained */
|
2022-07-08 10:22:58 +00:00
|
|
|
if (GST_V4L2_IS_M2M (obj->device_caps)) {
|
|
|
|
gst_v4l2_buffer_pool_resize_buffer (bpool, *buf);
|
2014-11-16 17:34:17 +00:00
|
|
|
goto eos;
|
2022-07-08 10:22:58 +00:00
|
|
|
}
|
2014-03-25 19:21:03 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 16:57:08 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2011-07-19 15:59:55 +00:00
|
|
|
/* an queue the buffer again after the copy */
|
2021-09-27 15:52:22 +00:00
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, tmp, FALSE);
|
2014-04-29 16:57:08 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto copy_failed;
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
2014-04-29 17:05:41 +00:00
|
|
|
{
|
|
|
|
struct UserPtrData *data;
|
2017-08-01 13:22:43 +00:00
|
|
|
GstBuffer *tmp;
|
2014-04-29 17:05:41 +00:00
|
|
|
|
|
|
|
/* Replace our buffer with downstream allocated buffer */
|
|
|
|
data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
|
|
|
|
GST_V4L2_IMPORT_QUARK);
|
2017-08-01 13:22:43 +00:00
|
|
|
tmp = gst_buffer_ref (data->buffer);
|
2014-04-29 17:05:41 +00:00
|
|
|
_unmap_userptr_frame (data);
|
2017-08-01 13:22:43 +00:00
|
|
|
|
|
|
|
/* Now tmp is writable, copy the flags and timestamp */
|
|
|
|
gst_buffer_copy_into (tmp, *buf,
|
|
|
|
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
|
|
|
|
|
|
|
|
gst_buffer_replace (buf, tmp);
|
|
|
|
gst_buffer_unref (tmp);
|
2014-04-29 17:05:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-14 16:19:39 +00:00
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
2014-04-11 21:10:11 +00:00
|
|
|
{
|
2014-04-29 17:05:41 +00:00
|
|
|
GstBuffer *tmp;
|
|
|
|
|
|
|
|
/* Replace our buffer with downstream allocated buffer */
|
|
|
|
tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
|
|
|
|
GST_V4L2_IMPORT_QUARK);
|
2017-08-01 13:22:43 +00:00
|
|
|
|
|
|
|
gst_buffer_copy_into (tmp, *buf,
|
|
|
|
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
|
|
|
|
|
2014-04-29 17:05:41 +00:00
|
|
|
gst_buffer_replace (buf, tmp);
|
|
|
|
gst_buffer_unref (tmp);
|
2014-04-11 21:10:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-18 16:54:49 +00:00
|
|
|
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;
|
2014-04-11 21:10:11 +00:00
|
|
|
|
|
|
|
case GST_V4L2_IO_USERPTR:
|
|
|
|
case GST_V4L2_IO_DMABUF_IMPORT:
|
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:
|
|
|
|
{
|
2014-05-26 16:34:42 +00:00
|
|
|
GstBuffer *to_queue = NULL;
|
2018-04-02 16:59:33 +00:00
|
|
|
GstBuffer *buffer;
|
2014-05-26 16:34:42 +00:00
|
|
|
GstV4l2MemoryGroup *group;
|
|
|
|
gint index;
|
2021-09-27 15:52:22 +00:00
|
|
|
gboolean outstanding;
|
2023-07-25 16:19:22 +00:00
|
|
|
gsize queued_size = 0;
|
|
|
|
gsize remaining_size = 0;
|
2023-08-03 18:46:56 +00:00
|
|
|
guint split_count = 1;
|
|
|
|
guint num_queued;
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2014-05-26 16:34:42 +00:00
|
|
|
if ((*buf)->pool != bpool)
|
|
|
|
goto copying;
|
|
|
|
|
2024-04-05 12:09:18 +00:00
|
|
|
/* Output buffers don't have to be writable */
|
|
|
|
if (!gst_v4l2_is_buffer_valid (*buf, &group, FALSE))
|
2014-05-26 16:34:42 +00:00
|
|
|
goto copying;
|
|
|
|
|
|
|
|
index = group->buffer.index;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
|
|
|
|
|
2021-09-27 15:52:22 +00:00
|
|
|
if (g_atomic_int_get (&pool->buffer_state[index]) &
|
|
|
|
BUFFER_STATE_QUEUED) {
|
2014-05-26 16:34:42 +00:00
|
|
|
GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index);
|
|
|
|
goto copying;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we can queue directly */
|
|
|
|
to_queue = gst_buffer_ref (*buf);
|
|
|
|
|
|
|
|
copying:
|
|
|
|
if (to_queue == NULL) {
|
2014-04-05 02:46:40 +00:00
|
|
|
GstBufferPoolAcquireParams params = { 0 };
|
|
|
|
|
2011-08-04 11:50:01 +00:00
|
|
|
GST_LOG_OBJECT (pool, "alloc buffer from our pool");
|
|
|
|
|
2014-04-05 02:46:40 +00:00
|
|
|
/* this can return EOS if all buffers are outstanding which would
|
|
|
|
* be strange because we would expect the upstream element to have
|
2011-07-25 13:47:05 +00:00
|
|
|
* allocated them and returned to us.. */
|
2014-04-05 02:46:40 +00:00
|
|
|
params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
|
|
|
|
ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms);
|
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
|
|
|
|
2014-04-29 17:05:41 +00:00
|
|
|
ret = gst_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf);
|
2014-04-29 16:57:08 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
gst_buffer_unref (to_queue);
|
2014-04-11 21:10:11 +00:00
|
|
|
goto prepare_failed;
|
2014-04-29 16:57:08 +00:00
|
|
|
}
|
2018-07-12 19:11:39 +00:00
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
/* retrieve the group */
|
2024-04-05 12:09:18 +00:00
|
|
|
gst_v4l2_is_buffer_valid (to_queue, &group, TRUE);
|
2011-07-19 15:59:55 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
|
2020-10-19 14:56:04 +00:00
|
|
|
if ((ret =
|
|
|
|
gst_v4l2_buffer_pool_qbuf (pool, to_queue, group,
|
|
|
|
frame_number))
|
2018-07-12 19:11:39 +00:00
|
|
|
!= GST_FLOW_OK)
|
2014-04-29 16:57:08 +00:00
|
|
|
goto queue_failed;
|
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 */
|
2014-05-25 00:20:07 +00:00
|
|
|
if (!gst_v4l2_buffer_pool_streamon (pool)) {
|
2014-10-07 13:29:33 +00:00
|
|
|
/* don't check return value because qbuf would have failed */
|
2024-04-05 12:09:18 +00:00
|
|
|
gst_v4l2_is_buffer_valid (to_queue, &group, TRUE);
|
2014-10-07 13:29:33 +00:00
|
|
|
|
2015-01-07 22:48:31 +00:00
|
|
|
/* qbuf has stored to_queue buffer but we are not in
|
2014-10-07 13:29:33 +00:00
|
|
|
* streaming state, so the flush logic won't be performed.
|
|
|
|
* To avoid leaks, flush the allocator and restore the queued
|
|
|
|
* buffer as non-queued */
|
|
|
|
gst_v4l2_allocator_flush (pool->vallocator);
|
|
|
|
|
|
|
|
pool->buffers[group->buffer.index] = NULL;
|
2021-09-27 15:52:22 +00:00
|
|
|
g_atomic_int_and (&pool->buffer_state[group->buffer.index],
|
|
|
|
~BUFFER_STATE_QUEUED);
|
2014-10-07 13:29:33 +00:00
|
|
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
|
|
|
|
GST_V4L2_IMPORT_QUARK, NULL, NULL);
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_buffer_unref (to_queue);
|
2014-10-07 13:29:33 +00:00
|
|
|
g_atomic_int_add (&pool->num_queued, -1);
|
2014-05-25 00:20:07 +00:00
|
|
|
goto start_failed;
|
2014-04-29 16:57:08 +00:00
|
|
|
}
|
2011-07-25 13:47:05 +00:00
|
|
|
|
2023-07-25 16:19:22 +00:00
|
|
|
/* Save the amount of data that has been submitted for encoded data */
|
|
|
|
if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) ==
|
|
|
|
GST_VIDEO_FORMAT_ENCODED) {
|
|
|
|
queued_size = gst_buffer_get_size (to_queue);
|
|
|
|
remaining_size = gst_buffer_get_size (*buf) - queued_size;
|
|
|
|
}
|
|
|
|
|
2015-01-07 22:48:31 +00:00
|
|
|
/* Remove our ref, we will still hold this buffer in acquire as needed,
|
|
|
|
* otherwise the pool will think it is outstanding and will refuse to stop. */
|
2023-08-03 18:46:56 +00:00
|
|
|
gst_clear_buffer (&to_queue);
|
2015-01-07 22:48:31 +00:00
|
|
|
|
2018-04-02 16:59:33 +00:00
|
|
|
/* release as many buffer as possible */
|
2021-09-27 15:52:22 +00:00
|
|
|
while (gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding,
|
|
|
|
FALSE) == GST_FLOW_OK) {
|
|
|
|
if (!outstanding)
|
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
|
|
|
|
FALSE);
|
2018-04-02 16:59:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 18:46:56 +00:00
|
|
|
num_queued = g_atomic_int_get (&pool->num_queued);
|
|
|
|
if (num_queued >= pool->min_latency && num_queued > split_count) {
|
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. */
|
2021-09-27 15:52:22 +00:00
|
|
|
ret =
|
|
|
|
gst_v4l2_buffer_pool_dqbuf (pool, &buffer, &outstanding, TRUE);
|
|
|
|
if (ret == GST_FLOW_OK && !outstanding)
|
2014-04-29 16:57:08 +00:00
|
|
|
/* release the rendered buffer back into the pool. This wakes up any
|
|
|
|
* thread waiting for a buffer in _acquire(). */
|
2021-09-27 15:52:22 +00:00
|
|
|
gst_v4l2_buffer_pool_complete_release_buffer (bpool, buffer,
|
|
|
|
FALSE);
|
2011-07-25 13:47:05 +00:00
|
|
|
}
|
2023-07-25 16:19:22 +00:00
|
|
|
|
|
|
|
/* For encoded data, just queue de remaining in the next available
|
|
|
|
* buffer. */
|
|
|
|
if (remaining_size) {
|
|
|
|
*buf = gst_buffer_make_writable (*buf);
|
|
|
|
gst_buffer_resize (*buf, queued_size, -1);
|
2023-08-03 18:46:56 +00:00
|
|
|
split_count++;
|
|
|
|
goto copying;
|
2023-07-25 16:19:22 +00:00
|
|
|
}
|
2011-07-18 16:54:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2014-04-29 16:57:08 +00:00
|
|
|
copy_failed:
|
|
|
|
{
|
2014-05-23 21:01:53 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "failed to copy buffer");
|
2014-04-29 16:57:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2018-05-28 19:19:52 +00:00
|
|
|
buffer_truncated:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (pool,
|
|
|
|
"Dropping truncated buffer, this is likely a driver bug.");
|
|
|
|
gst_buffer_unref (*buf);
|
|
|
|
*buf = NULL;
|
|
|
|
return GST_V4L2_FLOW_CORRUPTED_BUFFER;
|
|
|
|
}
|
2014-04-29 16:57:08 +00:00
|
|
|
eos:
|
|
|
|
{
|
2023-05-18 15:58:51 +00:00
|
|
|
GST_DEBUG_OBJECT (pool, "end of sequence reached");
|
2014-11-16 17:34:17 +00:00
|
|
|
gst_buffer_unref (*buf);
|
|
|
|
*buf = NULL;
|
|
|
|
return GST_V4L2_FLOW_LAST_BUFFER;
|
2014-04-29 16:57:08 +00:00
|
|
|
}
|
2011-08-04 11:50:01 +00:00
|
|
|
acquire_failed:
|
|
|
|
{
|
2014-05-23 21:01:53 +00:00
|
|
|
if (ret == GST_FLOW_FLUSHING)
|
|
|
|
GST_DEBUG_OBJECT (pool, "flushing");
|
|
|
|
else
|
|
|
|
GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s",
|
|
|
|
gst_flow_get_name (ret));
|
2011-08-04 11:50:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-11 21:10:11 +00:00
|
|
|
prepare_failed:
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
2014-05-23 21:01:53 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "failed to prepare data");
|
2014-04-11 21:10:11 +00:00
|
|
|
return ret;
|
2011-07-18 16:54:49 +00:00
|
|
|
}
|
2014-04-29 16:57:08 +00:00
|
|
|
queue_failed:
|
2011-07-18 16:54:49 +00:00
|
|
|
{
|
2014-05-23 21:01:53 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "failed to queue buffer");
|
2014-04-29 16:57:08 +00:00
|
|
|
return ret;
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
2014-04-29 16:57:08 +00:00
|
|
|
start_failed:
|
2014-04-11 21:10:11 +00:00
|
|
|
{
|
2014-05-23 21:01:53 +00:00
|
|
|
GST_ERROR_OBJECT (pool, "failed to start streaming");
|
2014-04-11 21:10:11 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2011-07-15 09:27:18 +00:00
|
|
|
}
|
2013-12-18 18:37:23 +00:00
|
|
|
|
2014-04-11 21:10:11 +00:00
|
|
|
void
|
|
|
|
gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
|
|
|
|
GstBufferPool * other_pool)
|
|
|
|
{
|
|
|
|
g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
|
|
|
|
|
|
|
|
if (pool->other_pool)
|
|
|
|
gst_object_unref (pool->other_pool);
|
|
|
|
pool->other_pool = gst_object_ref (other_pool);
|
|
|
|
}
|
2015-02-20 04:12:31 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gst_v4l2_buffer_pool_copy_at_threshold (GstV4l2BufferPool * pool, gboolean copy)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
pool->enable_copy_threshold = copy;
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
}
|
2017-08-05 16:23:30 +00:00
|
|
|
|
2023-04-26 19:58:23 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_buffer_pool_flush_events (GstV4l2Object * v4l2object)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
gboolean event_found;
|
|
|
|
|
|
|
|
/* FIXME simplify this when we drop legacy support for driver without poll()
|
|
|
|
* support. When we do, we can switch the video_fd to non blocking, and just
|
|
|
|
* pop the events directly. */
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct v4l2_event event = { 0, };
|
|
|
|
gint poll_ret;
|
|
|
|
|
|
|
|
event_found = FALSE;
|
|
|
|
|
|
|
|
gst_poll_set_flushing (v4l2object->poll, FALSE);
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* GstPoll don't have 0ns timeout, but 1 will do */
|
|
|
|
poll_ret = gst_poll_wait (v4l2object->poll, 1);
|
|
|
|
} while (poll_ret == EAGAIN || poll_ret == EINTR);
|
|
|
|
|
|
|
|
if (gst_poll_fd_has_pri (v4l2object->poll, &v4l2object->pollfd)) {
|
|
|
|
if (!gst_v4l2_dequeue_event (v4l2object, &event))
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
event_found = TRUE;
|
|
|
|
|
|
|
|
if (event.type == V4L2_EVENT_SOURCE_CHANGE &&
|
|
|
|
(event.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION)) {
|
|
|
|
GST_DEBUG_OBJECT (v4l2object->dbg_obj,
|
|
|
|
"Can't streamon capture as the resolution have changed.");
|
|
|
|
ret = GST_V4L2_FLOW_RESOLUTION_CHANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (event_found);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstFlowReturn
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_v4l2_buffer_pool_flush (GstV4l2Object * v4l2object)
|
2017-08-05 16:23:30 +00:00
|
|
|
{
|
2022-11-28 22:26:50 +00:00
|
|
|
GstBufferPool *bpool = gst_v4l2_object_get_buffer_pool (v4l2object);
|
|
|
|
GstV4l2BufferPool *pool;
|
2023-04-26 19:58:23 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2017-08-05 16:23:30 +00:00
|
|
|
|
2022-11-28 22:26:50 +00:00
|
|
|
if (!bpool)
|
2023-04-26 19:58:23 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2022-11-28 22:26:50 +00:00
|
|
|
|
|
|
|
pool = GST_V4L2_BUFFER_POOL (bpool);
|
|
|
|
|
2023-04-21 01:58:36 +00:00
|
|
|
GST_OBJECT_LOCK (pool);
|
2017-08-05 16:23:30 +00:00
|
|
|
gst_v4l2_buffer_pool_streamoff (pool);
|
2023-04-21 01:58:36 +00:00
|
|
|
GST_OBJECT_UNLOCK (pool);
|
2017-08-05 16:23:30 +00:00
|
|
|
|
2023-04-26 19:58:23 +00:00
|
|
|
if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
|
|
|
|
ret = gst_v4l2_buffer_pool_flush_events (v4l2object);
|
|
|
|
|
|
|
|
/* If the format haven't change, avoid reallocation to go back to
|
|
|
|
* streaming */
|
|
|
|
if (ret == GST_FLOW_OK)
|
|
|
|
ret = gst_v4l2_buffer_pool_streamon (pool);
|
|
|
|
}
|
2017-08-05 16:23:30 +00:00
|
|
|
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_object_unref (bpool);
|
2017-08-05 16:23:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2021-02-04 19:13:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_v4l2_buffer_pool_enable_resolution_change:
|
|
|
|
* @pool: a #GstBufferPool
|
|
|
|
*
|
|
|
|
* When this is called, the pool will subscribe to the
|
|
|
|
* %V4L2_EVENT_SOURCE_CHANGE. Upon receiving this event, it will notify
|
|
|
|
* the element acquiring buffer with the special flow return
|
|
|
|
* %GST_V4L2_FLOW_RESOLUTION_CHANGE.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_v4l2_buffer_pool_enable_resolution_change (GstV4l2BufferPool * pool)
|
|
|
|
{
|
2023-04-15 02:22:06 +00:00
|
|
|
gst_v4l2_object_subscribe_event (pool->obj, V4L2_EVENT_SOURCE_CHANGE);
|
2021-02-04 19:13:32 +00:00
|
|
|
}
|