/* GStreamer
*
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
- * 2006 Edgard Lima <edgard.lima@indt.org.br>
+ * 2006 Edgard Lima <edgard.lima@gmail.com>
* 2009 Texas Instruments, Inc - http://www.ti.com/
*
* gstv4l2bufferpool.c V4L2 buffer pool class
#include <gstv4l2bufferpool.h>
-#include "v4l2_calls.h"
+#include "gstv4l2object.h"
#include "gst/gst-i18n-plugin.h"
#include <gst/glib-compat-private.h>
-GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
-GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
-#define GST_CAT_DEFAULT v4l2_debug
+GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
+GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
+#define GST_CAT_DEFAULT v4l2bufferpool_debug
#define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
enum _GstV4l2BufferPoolAcquireFlags
{
- GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT = GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
- GST_V4L2_BUFFER_POOL_ACQUIRE_FAG_LAST
+ GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT =
+ GST_BUFFER_POOL_ACQUIRE_FLAG_LAST,
+ GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST
};
static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
GstBuffer * buffer);
static gboolean
-gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** group)
+gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
{
GstMemory *mem = gst_buffer_peek_memory (buffer, 0);
gboolean valid = FALSE;
goto done;
if (gst_is_dmabuf_memory (mem))
- mem = mem->parent;
+ mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
+ GST_V4L2_MEMORY_QUARK);
- if (gst_is_v4l2_memory (mem)) {
+ if (mem && gst_is_v4l2_memory (mem)) {
GstV4l2Memory *vmem = (GstV4l2Memory *) mem;
+ 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;
+
+ if (!gst_memory_is_writable (group->mem[i]))
+ goto done;
+ }
+
valid = TRUE;
- if (group)
- *group = vmem->group;
+ if (out_group)
+ *out_group = group;
}
done:
gst_buffer_resize (dest, 0, gst_buffer_get_size (src));
}
- GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, pool, "slow copy into buffer %p",
- dest);
+ gst_buffer_copy_into (dest, src,
+ GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
+
+ GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest);
return GST_FLOW_OK;
if (!gst_v4l2_is_buffer_valid (dest, &group))
goto not_our_buffer;
- if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type))
+ if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
flags = GST_MAP_READ;
else
flags = GST_MAP_WRITE;
if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN &&
finfo->format != GST_VIDEO_FORMAT_ENCODED)) {
+ gsize size[GST_VIDEO_MAX_PLANES] = { 0, };
+ gint i;
+
data->is_frame = TRUE;
if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags))
goto invalid_buffer;
+ 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);
+ gint pstride;
+ guint pheight;
+
+ pstride = GST_VIDEO_TILE_X_TILES (tinfo) <<
+ GST_VIDEO_FORMAT_INFO_TILE_WS (finfo);
+
+ pheight = GST_VIDEO_TILE_Y_TILES (tinfo) <<
+ GST_VIDEO_FORMAT_INFO_TILE_HS (finfo);
+
+ size[i] = pstride * pheight;
+ } else {
+ size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) *
+ GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i);
+ }
+ }
+
+ /* 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;
+ gint estride = gst_v4l2_object_extrapolate_stride (finfo, i,
+ 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;
+ }
+ }
+
if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
- data->frame.info.size, finfo->n_planes, data->frame.data,
- data->frame.info.offset))
+ data->frame.info.size, finfo->n_planes, data->frame.data, size))
goto import_failed;
} else {
- gsize offset[1] = { 0 };
gpointer ptr[1];
+ gsize size[1];
data->is_frame = FALSE;
goto invalid_buffer;
ptr[0] = data->map.data;
+ size[0] = data->map.size;
if (!gst_v4l2_allocator_import_userptr (pool->vallocator, group,
- data->map.size, 1, ptr, offset))
+ data->map.size, 1, ptr, size))
goto import_failed;
}
gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
data, (GDestroyNotify) _unmap_userptr_frame);
+ gst_buffer_copy_into (dest, src,
+ GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
+
return ret;
not_our_buffer:
g_slice_free (struct UserPtrData, data);
return GST_FLOW_ERROR;
}
+non_contiguous_mem:
+ {
+ GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch");
+ _unmap_userptr_frame (data);
+ return GST_FLOW_ERROR;
+ }
import_failed:
{
GST_ERROR_OBJECT (pool, "failed to import data");
gst_mini_object_set_qdata (GST_MINI_OBJECT (dest), GST_V4L2_IMPORT_QUARK,
gst_buffer_ref (src), (GDestroyNotify) gst_buffer_unref);
+ gst_buffer_copy_into (dest, src,
+ GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
+
return GST_FLOW_OK;
not_our_buffer:
allocation_failed:
{
GST_ERROR_OBJECT (pool, "failed to allocate buffer");
- return FALSE;
+ return GST_FLOW_ERROR;
}
}
can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
break;
case GST_V4L2_IO_RW:
- pool->allocator = g_object_ref (allocator);
+ if (allocator)
+ pool->allocator = g_object_ref (allocator);
pool->params = params;
/* No need to change the configuration */
goto done;
break;
}
+ /* 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;
+ 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);
+ }
+
if (min_buffers < GST_V4L2_MIN_BUFFERS) {
updated = TRUE;
min_buffers = GST_V4L2_MIN_BUFFERS;
GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers);
}
+ /* 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);
+ }
+
if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) {
updated = TRUE;
max_buffers = VIDEO_MAX_FRAME;
GST_BUFFER_POOL_OPTION_VIDEO_META);
}
- if (updated)
- gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
- max_buffers);
+ /* 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);
/* keep a GstVideoInfo with defaults for the when we need to copy */
gst_video_info_from_caps (&pool->caps_info, caps);
}
}
+static GstFlowReturn
+gst_v4l2_buffer_pool_resurect_buffer (GstV4l2BufferPool * pool)
+{
+ 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;
+}
+
static gboolean
-start_streaming (GstV4l2BufferPool * pool)
+gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
{
GstV4l2Object *obj = pool->obj;
- GST_DEBUG_OBJECT (pool, "start streaming");
-
if (pool->streaming)
return TRUE;
switch (obj->mode) {
- case GST_V4L2_IO_RW:
- break;
case GST_V4L2_IO_MMAP:
case GST_V4L2_IO_USERPTR:
case GST_V4L2_IO_DMABUF:
case GST_V4L2_IO_DMABUF_IMPORT:
- {
- /* For capture device, we need to re-enqueue buffers before be can let
- * the driver stream again */
- if (!V4L2_TYPE_IS_OUTPUT (obj->type) && pool->vallocator) {
- GstBufferPool *bpool = GST_BUFFER_POOL (pool);
- GstBufferPoolAcquireParams params = { 0 };
- gsize num_allocated, num_to_queue;
- GstFlowReturn ret;
-
- num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
- num_to_queue = num_allocated - pool->num_queued;
-
- while (num_to_queue > 0) {
- GstBuffer *buf;
-
- params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
- ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
- &buf, ¶ms);
-
- if (ret != GST_FLOW_OK)
- goto requeue_failed;
-
- gst_v4l2_buffer_pool_release_buffer (bpool, buf);
- num_to_queue--;
- }
-
- if (num_allocated != pool->num_queued)
- goto requeue_failed;
+ if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
+ /* For captures, we need to enqueue buffers before we start streaming,
+ * so the driver don't underflow immediatly. As we have put then back
+ * into the base class queue, resurect them, then releasing will queue
+ * them back. */
+ while (gst_v4l2_buffer_pool_resurect_buffer (pool) == GST_FLOW_OK)
+ continue;
}
- if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
- goto start_failed;
+ if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
+ goto streamon_failed;
- GST_DEBUG_OBJECT (pool, "STREAMON");
+ pool->streaming = TRUE;
+ GST_DEBUG_OBJECT (pool, "Started streaming");
break;
- }
default:
- g_assert_not_reached ();
break;
}
- pool->streaming = TRUE;
-
return TRUE;
- /* ERRORS */
-start_failed:
+streamon_failed:
{
GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno,
g_strerror (errno));
return FALSE;
}
-requeue_failed:
- {
- GST_ERROR_OBJECT (pool, "failed to re-enqueue buffers");
- return FALSE;
- }
}
+/* Call with streamlock held, or when streaming threads are down */
static void
-gst_v4l2_buffer_pool_group_released (GstV4l2BufferPool * pool)
+gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
{
- GstBufferPoolAcquireParams params = { 0 };
- GstBuffer *buffer = NULL;
- GstFlowReturn ret;
+ GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
+ GstV4l2Object *obj = pool->obj;
+ gint i;
- GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it");
+ if (!pool->streaming)
+ return;
- params.flags = GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT;
- ret = gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer,
- ¶ms);
+ switch (obj->mode) {
+ case GST_V4L2_IO_MMAP:
+ case GST_V4L2_IO_USERPTR:
+ case GST_V4L2_IO_DMABUF:
+ case GST_V4L2_IO_DMABUF_IMPORT:
- if (ret == GST_FLOW_OK)
- gst_buffer_unref (buffer);
+ 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));
+
+ pool->streaming = FALSE;
+
+ GST_DEBUG_OBJECT (pool, "Stopped streaming");
+
+ if (pool->vallocator)
+ gst_v4l2_allocator_flush (pool->vallocator);
+ break;
+ default:
+ break;
+ }
+
+ for (i = 0; i < VIDEO_MAX_FRAME; i++) {
+ if (pool->buffers[i]) {
+ GstBuffer *buffer = pool->buffers[i];
+ GstBufferPool *bpool = GST_BUFFER_POOL (pool);
+
+ pool->buffers[i] = NULL;
+
+ if (V4L2_TYPE_IS_OUTPUT (pool->obj->type))
+ gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
+ else /* Don't re-enqueue capture buffer on stop */
+ pclass->release_buffer (bpool, buffer);
+
+ g_atomic_int_add (&pool->num_queued, -1);
+ }
+ }
}
static gboolean
gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
+ GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
GstV4l2Object *obj = pool->obj;
GstStructure *config;
GstCaps *caps;
guint size, min_buffers, max_buffers;
- guint num_buffers = 0, copy_threshold = 0;
+ guint max_latency, min_latency, copy_threshold = 0;
+ gboolean can_allocate = FALSE, ret = TRUE;
+
+ GST_DEBUG_OBJECT (pool, "activating pool");
config = gst_buffer_pool_get_config (bpool);
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
&max_buffers))
goto wrong_config;
+ min_latency = MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers);
+
switch (obj->mode) {
case GST_V4L2_IO_RW:
- /* this value also instructs the latency calculation to have min_buffers
- * frame latency max */
- num_buffers = min_buffers;
+ can_allocate = TRUE;
+#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. */
+ if (obj->device_caps & V4L2_CAP_STREAMING)
+ obj->read (obj->video_fd, NULL, 0);
+#endif
break;
case GST_V4L2_IO_DMABUF:
case GST_V4L2_IO_MMAP:
{
guint count;
- if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
- num_buffers = min_buffers;
- } else {
- num_buffers = max_buffers;
- }
+ can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP);
/* first, lets request buffers, and see how many we can get: */
- GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", num_buffers);
+ GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers);
- count = gst_v4l2_allocator_start (pool->vallocator, num_buffers,
+ count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
V4L2_MEMORY_MMAP);
if (count < GST_V4L2_MIN_BUFFERS) {
- num_buffers = count;
+ min_buffers = count;
goto no_buffers;
}
* falling back to copy if the pipeline needed more buffers. This also
* prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is
* called. */
- if (count != num_buffers) {
- GST_WARNING_OBJECT (pool, "using %u buffers instead of %u",
- count, num_buffers);
- num_buffers = count;
- copy_threshold =
- MAX (GST_V4L2_MIN_BUFFERS, obj->min_buffers_for_capture);
-
- /* Ensure GstBufferPool don't expect initial minimum */
- if (min_buffers > count)
- min_buffers = count;
+ if (count != min_buffers || pool->enable_copy_threshold) {
+ GST_WARNING_OBJECT (pool,
+ "Uncertain or not enough buffers, enabling copy threshold");
+ min_buffers = count;
+ copy_threshold = min_latency;
}
break;
{
guint count;
- if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR)) {
- num_buffers = min_buffers;
- } else {
- num_buffers = max_buffers;
- }
+ can_allocate =
+ GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, USERPTR);
- GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", num_buffers);
+ GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers);
- count = gst_v4l2_allocator_start (pool->vallocator, num_buffers,
+ count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
V4L2_MEMORY_USERPTR);
/* There is no rational to not get what we asked */
- if (count < num_buffers) {
- num_buffers = count;
+ if (count < min_buffers) {
+ min_buffers = count;
goto no_buffers;
}
- num_buffers = count;
+ min_buffers = count;
break;
}
case GST_V4L2_IO_DMABUF_IMPORT:
{
guint count;
- if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF)) {
- num_buffers = min_buffers;
- } else {
- num_buffers = max_buffers;
- }
+ can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, DMABUF);
- GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", num_buffers);
+ GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers);
- count = gst_v4l2_allocator_start (pool->vallocator, num_buffers,
+ count = gst_v4l2_allocator_start (pool->vallocator, min_buffers,
V4L2_MEMORY_DMABUF);
/* There is no rational to not get what we asked */
- if (count < num_buffers) {
- num_buffers = count;
+ if (count < min_buffers) {
+ min_buffers = count;
goto no_buffers;
}
- num_buffers = count;
+ min_buffers = count;
break;
}
default:
- num_buffers = 0;
+ min_buffers = 0;
copy_threshold = 0;
g_assert_not_reached ();
break;
}
+ if (can_allocate)
+ max_latency = max_buffers;
+ else
+ max_latency = min_buffers;
+
pool->size = size;
pool->copy_threshold = copy_threshold;
- pool->num_buffers = num_buffers;
+ pool->max_latency = max_latency;
+ pool->min_latency = min_latency;
pool->num_queued = 0;
+ if (max_buffers != 0 && max_buffers < min_buffers)
+ max_buffers = min_buffers;
+
gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
max_buffers);
- GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config);
+ pclass->set_config (bpool, config);
gst_structure_free (config);
if (pool->other_pool)
goto other_pool_failed;
/* now, allocate the buffers: */
- if (!GST_BUFFER_POOL_CLASS (parent_class)->start (bpool))
+ if (!pclass->start (bpool))
goto start_failed;
- /* we can start capturing now, we wait for the playback case until we queued
- * the first buffer */
- if (!V4L2_TYPE_IS_OUTPUT (obj->type))
- if (!start_streaming (pool))
- goto start_failed;
+ if (!V4L2_TYPE_IS_OUTPUT (obj->type)) {
+ if (g_atomic_int_get (&pool->num_queued) < min_buffers)
+ goto queue_failed;
- if (!V4L2_TYPE_IS_OUTPUT (obj->type))
pool->group_released_handler =
g_signal_connect_swapped (pool->vallocator, "group-released",
- G_CALLBACK (gst_v4l2_buffer_pool_group_released), pool);
-
- gst_poll_set_flushing (obj->poll, FALSE);
+ G_CALLBACK (gst_v4l2_buffer_pool_resurect_buffer), pool);
+ ret = gst_v4l2_buffer_pool_streamon (pool);
+ }
- return TRUE;
+ return ret;
/* ERRORS */
wrong_config:
{
GST_ERROR_OBJECT (pool,
"we received %d buffer from device '%s', we want at least %d",
- num_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
+ min_buffers, obj->videodev, GST_V4L2_MIN_BUFFERS);
gst_structure_free (config);
return FALSE;
}
start_failed:
{
- GST_ERROR_OBJECT (pool, "failed to start streaming");
+ GST_ERROR_OBJECT (pool, "allocate failed");
return FALSE;
}
other_pool_failed:
GST_PTR_FORMAT, pool->other_pool);
return FALSE;
}
+queue_failed:
+ {
+ GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue");
+ return FALSE;
+ }
}
-
static gboolean
-stop_streaming (GstV4l2BufferPool * pool)
+gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
{
- GstV4l2Object *obj = pool->obj;
- gint i;
-
- GST_DEBUG_OBJECT (pool, "stopping stream");
+ GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
+ gboolean ret;
- gst_poll_set_flushing (obj->poll, TRUE);
+ GST_DEBUG_OBJECT (pool, "stopping pool");
- if (!pool->streaming) {
- /* it avoid error: STREAMOFF 22 (Invalid argument) when
- * attempting to stop a stream not previously started */
- GST_DEBUG_OBJECT (pool, "no need to stop, was not previously started");
- return TRUE;
+ if (pool->group_released_handler > 0) {
+ g_signal_handler_disconnect (pool->vallocator,
+ pool->group_released_handler);
+ pool->group_released_handler = 0;
}
- pool->flushing = TRUE;
-
- switch (obj->mode) {
- case GST_V4L2_IO_RW:
- break;
- case GST_V4L2_IO_MMAP:
- case GST_V4L2_IO_USERPTR:
- case GST_V4L2_IO_DMABUF:
- case GST_V4L2_IO_DMABUF_IMPORT:
- {
- gsize num_allocated;
+ if (pool->other_pool) {
+ gst_buffer_pool_set_active (pool->other_pool, FALSE);
+ gst_object_unref (pool->other_pool);
+ pool->other_pool = NULL;
+ }
- if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0)
- goto stop_failed;
+ gst_v4l2_buffer_pool_streamoff (pool);
- GST_DEBUG_OBJECT (pool, "STREAMOFF");
+ ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
- gst_v4l2_allocator_flush (pool->vallocator);
+ if (ret && pool->vallocator) {
+ GstV4l2Return vret;
- num_allocated = gst_v4l2_allocator_num_allocated (pool->vallocator);
+ vret = gst_v4l2_allocator_stop (pool->vallocator);
- for (i = 0; i < num_allocated; i++) {
- if (pool->buffers[i]) {
- GstBufferPool *bpool = (GstBufferPool *) pool;
- GstBuffer *buffer = pool->buffers[i];
+ if (vret == GST_V4L2_BUSY)
+ GST_WARNING_OBJECT (pool, "some buffers are still outstanding");
- pool->buffers[i] = NULL;
- pool->num_queued--;
+ ret = (vret == GST_V4L2_OK);
+ }
- /* 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);
+ return ret;
+}
- if (V4L2_TYPE_IS_OUTPUT (obj->type))
- gst_buffer_unref (buffer);
- else
- /* Give back the outstanding buffer to the pool */
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
- buffer);
- }
- }
- g_return_val_if_fail (pool->num_queued == 0, FALSE);
+static void
+gst_v4l2_buffer_pool_flush_start (GstBufferPool * bpool)
+{
+ GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
- break;
- }
- default:
- g_return_val_if_reached (FALSE);
- break;
- }
+ GST_DEBUG_OBJECT (pool, "start flushing");
- pool->flushing = FALSE;
- pool->streaming = FALSE;
+ gst_poll_set_flushing (pool->poll, TRUE);
- return TRUE;
+ GST_OBJECT_LOCK (pool);
+ pool->empty = FALSE;
+ g_cond_broadcast (&pool->empty_cond);
+ GST_OBJECT_UNLOCK (pool);
- /* ERRORS */
-stop_failed:
- {
- GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
- g_strerror (errno));
- return FALSE;
- }
+ if (pool->other_pool)
+ gst_buffer_pool_set_flushing (pool->other_pool, TRUE);
}
-static gboolean
-gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
+static void
+gst_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool)
{
- gboolean ret;
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
- GstV4l2Object *obj = pool->obj;
- GST_DEBUG_OBJECT (pool, "stopping pool");
+ GST_DEBUG_OBJECT (pool, "stop flushing");
- 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)
+ gst_buffer_pool_set_flushing (pool->other_pool, FALSE);
- gst_poll_set_flushing (obj->poll, TRUE);
- if (!stop_streaming (pool))
- goto stop_failed;
+ gst_poll_set_flushing (pool->poll, FALSE);
+}
- ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
+static GstFlowReturn
+gst_v4l2_buffer_pool_poll (GstV4l2BufferPool * pool, gboolean wait)
+{
+ gint ret;
+ GstClockTime timeout;
- if (ret) {
- GstV4l2Return vret;
+ if (wait)
+ timeout = GST_CLOCK_TIME_NONE;
+ else
+ timeout = 0;
- vret = gst_v4l2_allocator_stop (pool->vallocator);
+ /* 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);
- if (vret == GST_V4L2_BUSY) {
- GST_WARNING_OBJECT (pool, "allocated buffer need to be reclaimed");
- /* FIXME deal with reclaiming */
- } else if (vret == GST_V4L2_ERROR) {
- ret = FALSE;
+ if (!wait && pool->empty) {
+ GST_OBJECT_UNLOCK (pool);
+ goto no_buffers;
}
- }
- return ret;
+ while (pool->empty)
+ g_cond_wait (&pool->empty_cond, GST_OBJECT_GET_LOCK (pool));
- /* ERRORS */
-stop_failed:
- {
- GST_ERROR_OBJECT (pool, "error with STREAMOFF %d (%s)", errno,
- g_strerror (errno));
- return FALSE;
+ GST_OBJECT_UNLOCK (pool);
}
-}
-static GstFlowReturn
-gst_v4l2_object_poll (GstV4l2Object * v4l2object)
-{
- gint ret;
+ if (!pool->can_poll_device) {
+ if (wait)
+ goto done;
+ else
+ goto no_buffers;
+ }
+
+ GST_LOG_OBJECT (pool, "polling device");
- if (v4l2object->can_poll_device) {
- GST_LOG_OBJECT (v4l2object->element, "polling device");
- ret = gst_poll_wait (v4l2object->poll, GST_CLOCK_TIME_NONE);
- if (G_UNLIKELY (ret < 0)) {
- if (errno == EBUSY)
+again:
+ ret = gst_poll_wait (pool->poll, timeout);
+ if (G_UNLIKELY (ret < 0)) {
+ switch (errno) {
+ case EBUSY:
goto stopped;
- if (errno == ENXIO) {
- GST_WARNING_OBJECT (v4l2object->element,
- "v4l2 device doesn't support polling. Disabling");
- v4l2object->can_poll_device = FALSE;
- } else {
- if (errno != EAGAIN && errno != EINTR)
- goto select_error;
- }
+ case EAGAIN:
+ case EINTR:
+ goto again;
+ case ENXIO:
+ GST_WARNING_OBJECT (pool,
+ "v4l2 device doesn't support polling. Disabling"
+ " using libv4l2 in this case may cause deadlocks");
+ pool->can_poll_device = FALSE;
+ goto done;
+ default:
+ goto select_error;
}
}
+
+ if (gst_poll_fd_has_error (pool->poll, &pool->pollfd))
+ goto select_error;
+
+ if (ret == 0)
+ goto no_buffers;
+
+done:
return GST_FLOW_OK;
/* ERRORS */
stopped:
{
- GST_DEBUG ("stop called");
+ GST_DEBUG_OBJECT (pool, "stop called");
return GST_FLOW_FLUSHING;
}
select_error:
{
- GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, READ, (NULL),
+ GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL),
("poll error %d: %s (%d)", ret, g_strerror (errno), errno));
return GST_FLOW_ERROR;
}
+no_buffers:
+ return GST_FLOW_CUSTOM_SUCCESS;
}
static GstFlowReturn
-gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
+gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf,
+ GstV4l2MemoryGroup * group)
{
- GstV4l2MemoryGroup *group = NULL;
+ const GstV4l2Object *obj = pool->obj;
+ GstClockTime timestamp;
gint index;
- if (!gst_v4l2_is_buffer_valid (buf, &group)) {
- GST_LOG_OBJECT (pool, "unref copied/invalid buffer %p", buf);
- gst_buffer_unref (buf);
- return GST_FLOW_OK;
- }
-
index = group->buffer.index;
if (pool->buffers[index] != NULL)
GST_LOG_OBJECT (pool, "queuing buffer %i", index);
+ if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
+ enum v4l2_field field;
+
+ /* Except when field is set to alternate, buffer field is the same as
+ * the one defined in format */
+ if (V4L2_TYPE_IS_MULTIPLANAR (obj->type))
+ field = obj->format.fmt.pix_mp.field;
+ else
+ field = obj->format.fmt.pix.field;
+
+ /* NB: At this moment, we can't have alternate mode because it not handled
+ * yet */
+ if (field == V4L2_FIELD_ALTERNATE) {
+ if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_FRAME_FLAG_TFF))
+ field = V4L2_FIELD_TOP;
+ else
+ field = V4L2_FIELD_BOTTOM;
+ }
+
+ group->buffer.field = field;
+ }
+
+ if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
+ timestamp = GST_BUFFER_TIMESTAMP (buf);
+ GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp);
+ }
+
+ GST_OBJECT_LOCK (pool);
+ g_atomic_int_inc (&pool->num_queued);
+ pool->buffers[index] = buf;
+
if (!gst_v4l2_allocator_qbuf (pool->vallocator, group))
goto queue_failed;
- pool->buffers[index] = buf;
- pool->num_queued++;
+ pool->empty = FALSE;
+ g_cond_signal (&pool->empty_cond);
+ GST_OBJECT_UNLOCK (pool);
return GST_FLOW_OK;
GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index);
/* Mark broken buffer to the allocator */
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY);
+ g_atomic_int_add (&pool->num_queued, -1);
+ pool->buffers[index] = NULL;
+ GST_OBJECT_UNLOCK (pool);
return GST_FLOW_ERROR;
}
}
static GstFlowReturn
-gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
+gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
+ gboolean wait)
{
GstFlowReturn res;
- GstBuffer *outbuf;
+ GstBuffer *outbuf = NULL;
GstV4l2Object *obj = pool->obj;
GstClockTime timestamp;
GstV4l2MemoryGroup *group;
+ GstVideoMeta *vmeta;
+ gsize size;
gint i;
- if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
+ if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
goto poll_failed;
+ if (res == GST_FLOW_CUSTOM_SUCCESS) {
+ GST_LOG_OBJECT (pool, "nothing to dequeue");
+ goto done;
+ }
+
GST_LOG_OBJECT (pool, "dequeueing a buffer");
- group = gst_v4l2_allocator_dqbuf (pool->vallocator);
- if (group == NULL)
+ res = gst_v4l2_allocator_dqbuf (pool->vallocator, &group);
+ if (res == GST_FLOW_EOS)
+ goto eos;
+ if (res != GST_FLOW_OK)
goto dqbuf_failed;
/* get our GstBuffer with that index from the pool, if the buffer was
/* mark the buffer outstanding */
pool->buffers[group->buffer.index] = NULL;
- pool->num_queued--;
+ if (g_atomic_int_dec_and_test (&pool->num_queued)) {
+ GST_OBJECT_LOCK (pool);
+ pool->empty = TRUE;
+ GST_OBJECT_UNLOCK (pool);
+ }
timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp);
-#ifndef GST_DISABLE_GST_DEBUG
+ size = 0;
+ vmeta = gst_buffer_get_video_meta (outbuf);
for (i = 0; i < group->n_mem; i++) {
GST_LOG_OBJECT (pool,
"dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %"
group->buffer.sequence, group->buffer.index, group->mem[i],
group->planes[i].bytesused, i, group->buffer.flags,
GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
+
+ if (vmeta) {
+ vmeta->offset[i] = size;
+ size += gst_memory_get_sizes (group->mem[i], NULL, NULL);
+ }
}
+
+ /* Ignore timestamp and field for OUTPUT device */
+ if (V4L2_TYPE_IS_OUTPUT (obj->type))
+ goto done;
+
+ /* 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;
+ }
+ }
+
/* set top/bottom field first if v4l2_buffer has the information */
- if (group->buffer.field == V4L2_FIELD_INTERLACED_TB) {
- GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
- GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
- } else if (group->buffer.field == V4L2_FIELD_INTERLACED_BT) {
- GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
- GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
- } else {
- GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
- GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
+ 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;
+ 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;
+ 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;
+ 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);
+ break;
}
if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) {
- if (group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME)
+ 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)
GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
else
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
}
+ if (group->buffer.flags & V4L2_BUF_FLAG_ERROR)
+ GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED);
+
GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
+ GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
+ GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
+done:
*buffer = outbuf;
- return GST_FLOW_OK;
+ return res;
/* ERRORS */
poll_failed:
GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res));
return res;
}
+eos:
+ {
+ return GST_FLOW_EOS;
+ }
dqbuf_failed:
{
return GST_FLOW_ERROR;
{
GstFlowReturn ret;
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
+ GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
GstV4l2Object *obj = pool->obj;
GST_DEBUG_OBJECT (pool, "acquire");
- if (GST_BUFFER_POOL_IS_FLUSHING (bpool))
- goto flushing;
-
/* If this is being called to resurect a lost buffer */
- if (params && params->flags & GST_V4L2_POOL_ACQUIRE_FLAG_RESURECT) {
- ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool, buffer,
- params);
+ if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) {
+ ret = pclass->acquire_buffer (bpool, buffer, params);
goto done;
}
case GST_V4L2_IO_RW:
{
/* take empty buffer from the pool */
- ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
- buffer, params);
+ ret = pclass->acquire_buffer (bpool, buffer, params);
break;
}
case GST_V4L2_IO_DMABUF:
case GST_V4L2_IO_MMAP:
+ case GST_V4L2_IO_USERPTR:
+ case GST_V4L2_IO_DMABUF_IMPORT:
{
/* just dequeue a buffer, we basically use the queue of v4l2 as the
* storage for our buffers. This function does poll first so we can
* interrupt it fine. */
- ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
- if (G_UNLIKELY (ret != GST_FLOW_OK))
- goto done;
-
- /* start copying buffers when we are running low on buffers */
- if (pool->num_queued < pool->copy_threshold) {
- GstBuffer *copy;
-
- if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
- if (GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
- ©, params) == GST_FLOW_OK) {
- gst_v4l2_buffer_pool_release_buffer (bpool, copy);
- break;
- }
- }
-
- /* copy the buffer */
- copy = gst_buffer_copy_region (*buffer,
- GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1);
- GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
-
- /* and requeue so that we can continue capturing */
- gst_v4l2_buffer_pool_release_buffer (bpool, *buffer);
- *buffer = copy;
- }
- break;
- }
- case GST_V4L2_IO_USERPTR:
- case GST_V4L2_IO_DMABUF_IMPORT:
- {
- /* dequeue filled buffer */
- ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
+ ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer, TRUE);
break;
}
default:
switch (obj->mode) {
case GST_V4L2_IO_RW:
/* get an empty buffer */
- ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
- buffer, params);
+ ret = pclass->acquire_buffer (bpool, buffer, params);
break;
case GST_V4L2_IO_MMAP:
case GST_V4L2_IO_USERPTR:
case GST_V4L2_IO_DMABUF_IMPORT:
/* get a free unqueued buffer */
- ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
- buffer, params);
+ ret = pclass->acquire_buffer (bpool, buffer, params);
break;
default:
}
done:
return ret;
-
- /* ERRORS */
-flushing:
- {
- GST_DEBUG_OBJECT (pool, "We are flushing");
- return GST_FLOW_FLUSHING;
- }
}
static void
gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
+ GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
GstV4l2Object *obj = pool->obj;
GST_DEBUG_OBJECT (pool, "release buffer %p", buffer);
switch (obj->mode) {
case GST_V4L2_IO_RW:
/* release back in the pool */
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
+ pclass->release_buffer (bpool, buffer);
break;
case GST_V4L2_IO_DMABUF:
case GST_V4L2_IO_USERPTR:
case GST_V4L2_IO_DMABUF_IMPORT:
{
- if (pool->flushing) {
- /* put back on outstanding list */
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
- buffer);
- } else if (gst_v4l2_is_buffer_valid (buffer, NULL)) {
+ GstV4l2MemoryGroup *group;
+ if (gst_v4l2_is_buffer_valid (buffer, &group)) {
+ gst_v4l2_allocator_reset_group (pool->vallocator, group);
/* queue back in the device */
if (pool->other_pool)
gst_v4l2_buffer_pool_prepare_buffer (pool, buffer, NULL);
- if (gst_v4l2_buffer_pool_qbuf (pool, buffer) != GST_FLOW_OK)
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
- buffer);
+ if (gst_v4l2_buffer_pool_qbuf (pool, buffer, group) != GST_FLOW_OK)
+ pclass->release_buffer (bpool, buffer);
} else {
/* Simply release invalide/modified buffer, the allocator will
* give it back later */
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
- buffer);
+ pclass->release_buffer (bpool, buffer);
}
break;
}
switch (obj->mode) {
case GST_V4L2_IO_RW:
/* release back in the pool */
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
+ pclass->release_buffer (bpool, buffer);
break;
case GST_V4L2_IO_MMAP:
/* Simply release invalide/modified buffer, the allocator will
* give it back later */
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
- buffer);
+ pclass->release_buffer (bpool, buffer);
break;
}
gst_v4l2_allocator_reset_group (pool->vallocator, group);
/* playback, put the buffer back in the queue to refill later. */
- GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
- buffer);
+ pclass->release_buffer (bpool, buffer);
} else {
- /* We keep a ref on queued buffer, so this should never happen */
- g_assert_not_reached ();
+ /* 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);
}
break;
}
}
static void
-gst_v4l2_buffer_pool_finalize (GObject * object)
+gst_v4l2_buffer_pool_dispose (GObject * object)
{
GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (object);
- gint i;
-
- for (i = 0; i < VIDEO_MAX_FRAME; i++) {
- if (pool->buffers[i])
- gst_buffer_replace (&(pool->buffers[i]), NULL);
- }
-
- if (pool->video_fd >= 0)
- v4l2_close (pool->video_fd);
if (pool->vallocator)
gst_object_unref (pool->vallocator);
+ pool->vallocator = NULL;
if (pool->allocator)
gst_object_unref (pool->allocator);
+ pool->allocator = NULL;
if (pool->other_pool)
gst_object_unref (pool->other_pool);
+ 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)
+ pool->obj->close (pool->video_fd);
+
+ gst_poll_free (pool->poll);
- /* FIXME Is this required to keep around ? */
+ /* This can't be done in dispose method because we must not set pointer
+ * to NULL as it is part of the v4l2object and dispose could be called
+ * multiple times */
gst_object_unref (pool->obj->element);
+ g_cond_clear (&pool->empty_cond);
+
/* FIXME have we done enough here ? */
G_OBJECT_CLASS (parent_class)->finalize (object);
static void
gst_v4l2_buffer_pool_init (GstV4l2BufferPool * pool)
{
+ pool->poll = gst_poll_new (TRUE);
+ pool->can_poll_device = TRUE;
+ g_cond_init (&pool->empty_cond);
+ pool->empty = TRUE;
}
static void
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass);
+ object_class->dispose = gst_v4l2_buffer_pool_dispose;
object_class->finalize = gst_v4l2_buffer_pool_finalize;
bufferpool_class->start = gst_v4l2_buffer_pool_start;
bufferpool_class->alloc_buffer = gst_v4l2_buffer_pool_alloc_buffer;
bufferpool_class->acquire_buffer = gst_v4l2_buffer_pool_acquire_buffer;
bufferpool_class->release_buffer = gst_v4l2_buffer_pool_release_buffer;
+ bufferpool_class->flush_start = gst_v4l2_buffer_pool_flush_start;
+ bufferpool_class->flush_stop = gst_v4l2_buffer_pool_flush_stop;
+
+ GST_DEBUG_CATEGORY_INIT (v4l2bufferpool_debug, "v4l2bufferpool", 0,
+ "V4L2 Buffer Pool");
+ GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
}
/**
gchar *name, *parent_name;
gint fd;
- fd = v4l2_dup (obj->video_fd);
+ fd = obj->dup (obj->video_fd);
if (fd < 0)
goto dup_failed;
pool = (GstV4l2BufferPool *) g_object_new (GST_TYPE_V4L2_BUFFER_POOL,
"name", name, NULL);
+ g_object_ref_sink (pool);
g_free (name);
+ gst_poll_fd_init (&pool->pollfd);
+ pool->pollfd.fd = fd;
+ gst_poll_add_fd (pool->poll, &pool->pollfd);
+ if (V4L2_TYPE_IS_OUTPUT (obj->type))
+ gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE);
+ else
+ gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE);
+
pool->video_fd = fd;
pool->obj = obj;
+ pool->can_poll_device = TRUE;
- pool->vallocator =
- gst_v4l2_allocator_new (GST_OBJECT (pool), obj->video_fd, &obj->format);
+ pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
+ if (pool->vallocator == NULL)
+ goto allocator_failed;
gst_object_ref (obj->element);
GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno));
return NULL;
}
+allocator_failed:
+ {
+ GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
+ gst_object_unref (pool);
+ return NULL;
+ }
}
static GstFlowReturn
gst_buffer_map (buf, &map, GST_MAP_WRITE);
do {
- if ((res = gst_v4l2_object_poll (obj)) != GST_FLOW_OK)
+ if ((res = gst_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK)
goto poll_error;
- amount = v4l2_read (obj->video_fd, map.data, toread);
+ amount = obj->read (obj->video_fd, map.data, toread);
if (amount == toread) {
break;
GST_DEBUG_OBJECT (pool, "process buffer %p", buf);
- g_return_val_if_fail (gst_buffer_pool_is_active (bpool), GST_FLOW_ERROR);
+ if (GST_BUFFER_POOL_IS_FLUSHING (pool))
+ return GST_FLOW_FLUSHING;
switch (obj->type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
GstBuffer *tmp;
if ((*buf)->pool == bpool) {
- if (gst_buffer_get_size (*buf) == 0)
+ guint num_queued;
+ gsize size = gst_buffer_get_size (*buf);
+
+ /* Legacy M2M devices return empty buffer when drained */
+ if (size == 0 && GST_V4L2_IS_M2M (obj->device_caps))
goto eos;
- else
- /* nothing, data was inside the buffer when we did _acquire() */
- goto done;
+
+ if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) !=
+ GST_VIDEO_FORMAT_ENCODED && size < pool->size)
+ goto buffer_truncated;
+
+ 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)) {
+ ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
+ if (ret == GST_FLOW_OK)
+ goto done;
+ }
+ }
+
+ /* start copying buffers when we are running low on buffers */
+ if (num_queued < pool->copy_threshold) {
+ GstBuffer *copy;
+
+ if (GST_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) {
+ ret = gst_v4l2_buffer_pool_resurect_buffer (pool);
+ if (ret == GST_FLOW_OK)
+ 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;
+ }
+
+ ret = GST_FLOW_OK;
+ /* nothing, data was inside the buffer when we did _acquire() */
+ goto done;
}
/* buffer not from our pool, grab a frame and copy it into the target */
- if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp)) != GST_FLOW_OK)
+ if ((ret = gst_v4l2_buffer_pool_dqbuf (pool, &tmp, TRUE))
+ != GST_FLOW_OK)
goto done;
/* An empty buffer on capture indicates the end of stream */
if (gst_buffer_get_size (tmp) == 0) {
gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
- goto eos;
+
+ /* Legacy M2M devices return empty buffer when drained */
+ if (GST_V4L2_IS_M2M (obj->device_caps))
+ goto eos;
}
ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
case GST_V4L2_IO_USERPTR:
{
struct UserPtrData *data;
+ GstBuffer *tmp;
/* Replace our buffer with downstream allocated buffer */
data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
GST_V4L2_IMPORT_QUARK);
- gst_buffer_replace (buf, data->buffer);
+ tmp = gst_buffer_ref (data->buffer);
_unmap_userptr_frame (data);
+
+ /* 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);
break;
}
/* Replace our buffer with downstream allocated buffer */
tmp = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf),
GST_V4L2_IMPORT_QUARK);
+
+ 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);
break;
case GST_V4L2_IO_DMABUF:
case GST_V4L2_IO_MMAP:
{
- GstBuffer *to_queue;
+ GstBuffer *to_queue = NULL;
+ GstBuffer *buffer;
+ GstV4l2MemoryGroup *group;
+ gint index;
- if ((*buf)->pool == bpool) {
- /* nothing, we can queue directly */
- to_queue = gst_buffer_ref (*buf);
- GST_LOG_OBJECT (pool, "processing buffer from our pool");
- } else {
+ if ((*buf)->pool != bpool)
+ goto copying;
+
+ if (!gst_v4l2_is_buffer_valid (*buf, &group))
+ goto copying;
+
+ index = group->buffer.index;
+
+ GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index);
+
+ if (pool->buffers[index] != NULL) {
+ 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) {
GstBufferPoolAcquireParams params = { 0 };
GST_LOG_OBJECT (pool, "alloc buffer from our pool");
gst_buffer_unref (to_queue);
goto prepare_failed;
}
+
+ /* retreive the group */
+ gst_v4l2_is_buffer_valid (to_queue, &group);
}
- if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue)) != GST_FLOW_OK)
+ if ((ret = gst_v4l2_buffer_pool_qbuf (pool, to_queue, group))
+ != GST_FLOW_OK)
goto queue_failed;
/* if we are not streaming yet (this is the first buffer, start
* streaming now */
- if (!pool->streaming) {
- if (!start_streaming (pool)) {
- gst_buffer_unref (to_queue);
- goto start_failed;
- }
+ if (!gst_v4l2_buffer_pool_streamon (pool)) {
+ /* don't check return value because qbuf would have failed */
+ gst_v4l2_is_buffer_valid (to_queue, &group);
+
+ /* qbuf has stored to_queue buffer but we are not in
+ * 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;
+
+ gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue),
+ GST_V4L2_IMPORT_QUARK, NULL, NULL);
+ gst_buffer_unref (to_queue);
+ g_atomic_int_add (&pool->num_queued, -1);
+ goto start_failed;
+ }
+
+ /* 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. */
+ gst_buffer_unref (to_queue);
+
+ /* release as many buffer as possible */
+ while (gst_v4l2_buffer_pool_dqbuf (pool, &buffer, FALSE) ==
+ GST_FLOW_OK) {
+ if (buffer->pool == NULL)
+ gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
}
- if (pool->num_queued ==
- gst_v4l2_allocator_num_allocated (pool->vallocator)) {
- GstBuffer *out;
+ if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) {
/* all buffers are queued, try to dequeue one and release it back
* into the pool so that _acquire can get to it again. */
- ret = gst_v4l2_buffer_pool_dqbuf (pool, &out);
- if (ret == GST_FLOW_OK)
+ ret = gst_v4l2_buffer_pool_dqbuf (pool, &buffer, TRUE);
+ if (ret == GST_FLOW_OK && buffer->pool == NULL)
/* release the rendered buffer back into the pool. This wakes up any
* thread waiting for a buffer in _acquire(). */
- gst_buffer_unref (out);
+ gst_v4l2_buffer_pool_release_buffer (bpool, buffer);
}
break;
}
/* ERRORS */
copy_failed:
{
- GST_ERROR_OBJECT (obj->element, "failed to copy buffer");
+ GST_ERROR_OBJECT (pool, "failed to copy buffer");
return ret;
}
+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;
+ }
eos:
{
- GST_DEBUG_OBJECT (obj->element, "end of stream reached");
- return GST_FLOW_EOS;
+ GST_DEBUG_OBJECT (pool, "end of stream reached");
+ gst_buffer_unref (*buf);
+ *buf = NULL;
+ return GST_V4L2_FLOW_LAST_BUFFER;
}
acquire_failed:
{
- GST_WARNING_OBJECT (obj->element, "failed to acquire a buffer: %s",
- gst_flow_get_name (ret));
+ 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));
return ret;
}
prepare_failed:
{
- GST_ERROR_OBJECT (obj->element, "failed to prepare data");
+ GST_ERROR_OBJECT (pool, "failed to prepare data");
return ret;
}
queue_failed:
{
- GST_ERROR_OBJECT (obj->element, "failed to queue buffer");
+ GST_ERROR_OBJECT (pool, "failed to queue buffer");
return ret;
}
start_failed:
{
- GST_ERROR_OBJECT (obj->element, "failed to start streaming");
+ GST_ERROR_OBJECT (pool, "failed to start streaming");
return GST_FLOW_ERROR;
}
}
-
-/**
- * gst_v4l2_buffer_pool_stop_streaming:
- * @bpool: a #GstBufferPool
- *
- * First, set obj->poll to be flushing
- * Call STREAMOFF to clear QUEUED flag on every driver buffers.
- * Then release all buffers that are in pool->buffers array.
- *
- * Returns: TRUE on success.
- */
-gboolean
-gst_v4l2_buffer_pool_stop_streaming (GstV4l2BufferPool * pool)
+void
+gst_v4l2_buffer_pool_set_other_pool (GstV4l2BufferPool * pool,
+ GstBufferPool * other_pool)
{
- GST_DEBUG_OBJECT (pool, "stop streaming");
-
- if (!stop_streaming (pool))
- goto stop_failed;
+ g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool)));
- return TRUE;
+ if (pool->other_pool)
+ gst_object_unref (pool->other_pool);
+ pool->other_pool = gst_object_ref (other_pool);
+}
- /* ERRORS */
-stop_failed:
- {
- GST_ERROR_OBJECT (pool, "failed to stop streaming");
- return FALSE;
- }
+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);
}
gboolean
-gst_v4l2_buffer_pool_start_streaming (GstV4l2BufferPool * pool)
+gst_v4l2_buffer_pool_flush (GstBufferPool * bpool)
{
- GST_DEBUG_OBJECT (pool, "start straming");
-
- if (!start_streaming (pool))
- goto start_failed;
+ GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
+ gboolean ret = TRUE;
- return TRUE;
+ gst_v4l2_buffer_pool_streamoff (pool);
- /* ERRORS */
-start_failed:
- {
- GST_ERROR_OBJECT (pool, "failed to start streaming");
- return FALSE;
- }
-}
-
-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 (!V4L2_TYPE_IS_OUTPUT (pool->obj->type))
+ ret = gst_v4l2_buffer_pool_streamon (pool);
- if (pool->other_pool)
- gst_object_unref (pool->other_pool);
- pool->other_pool = gst_object_ref (other_pool);
+ return ret;
}