diff --git a/lib/libv4l-gst/libv4l-gst.c b/lib/libv4l-gst/libv4l-gst.c index bf07059..15a9cbb 100644 --- a/lib/libv4l-gst/libv4l-gst.c +++ b/lib/libv4l-gst/libv4l-gst.c @@ -20,9 +20,11 @@ #include "config.h" #include +#include #include #include "v4l-gst.h" +#include "v4l-gst-internal.h" #include "debug.h" #if HAVE_VISIBILITY @@ -62,6 +64,9 @@ static int plugin_ioctl(void *dev_ops_priv, int fd, (void)fd; /* unused */ + if (!v4l_gst_core_enter_operation(priv)) + goto out; + switch (cmd) { case VIDIOC_QUERYCAP: ret = v4l_gst_ioctl_querycap(priv, arg); @@ -141,6 +146,9 @@ static int plugin_ioctl(void *dev_ops_priv, int fd, break; } +out: + v4l_gst_core_leave_operation(priv); + return ret; } @@ -149,8 +157,19 @@ plugin_mmap(void *dev_ops_priv, void *start, size_t length, int prot, int flags, int fd, int64_t offset) { struct v4l_gst *priv = dev_ops_priv; - return v4l_gst_mmap(priv, start, length, prot, flags, fd, - offset); + void *ret; + + if (!v4l_gst_core_enter_operation(priv)) { + ret = MAP_FAILED; + goto out; + } + + ret = v4l_gst_mmap(priv, start, length, prot, flags, fd, offset); + +out: + v4l_gst_core_leave_operation(priv); + + return ret; } PLUGIN_PUBLIC const struct libv4l_dev_ops libv4l2_plugin = { diff --git a/lib/libv4l-gst/v4l-gst-buf.c b/lib/libv4l-gst/v4l-gst-buf.c index 849b97f..2e293ec 100644 --- a/lib/libv4l-gst/v4l-gst-buf.c +++ b/lib/libv4l-gst/v4l-gst-buf.c @@ -161,9 +161,27 @@ qbuf_ioctl_out(struct v4l_gst *priv, struct v4l2_buffer *v4l2buf) buffer = &priv->out.buffers[v4l2buf->index]; + if (buffer->state == V4L_GST_BUFFER_QUEUED) { + GST_ERROR("Buffer %u is already queued", v4l2buf->index); + errno = EINVAL; + return -1; + } + if (v4l2buf->m.planes[0].bytesused == 0) { + /* Transfer the acquired reference to EOS, rather than adding + one: the callback must return this buffer to the pool. Publish + it before appsrc can deliver EOS on its streaming thread. */ + g_mutex_lock(&priv->queue_mutex); + if (priv->eos_gstbuf) { + g_mutex_unlock(&priv->queue_mutex); + errno = EBUSY; + return -1; + } + priv->eos_gstbuf = buffer->gstbuf; flow_ret = gst_app_src_end_of_stream(GST_APP_SRC(priv->appsrc)); if (flow_ret != GST_FLOW_OK) { + priv->eos_gstbuf = NULL; + g_mutex_unlock(&priv->queue_mutex); GST_ERROR("Failed to send an EOS event"); errno = EINVAL; return -1; @@ -174,17 +192,11 @@ qbuf_ioctl_out(struct v4l_gst *priv, struct v4l2_buffer *v4l2buf) memset(&buffer->info, 0, sizeof(buffer->info)); buffer->state = V4L_GST_BUFFER_QUEUED; - priv->eos_gstbuf = buffer->gstbuf; + g_mutex_unlock(&priv->queue_mutex); return 0; } - if (buffer->state == V4L_GST_BUFFER_QUEUED) { - GST_ERROR("Buffer %u is already queued", v4l2buf->index); - errno = EINVAL; - return -1; - } - /* Push the OUTPUT caps to the appsrc before the first buffer so the base source can negotiate its caps before the first buffer is pushed. Otherwise the caps sit behind the buffers in the appsrc @@ -467,11 +479,17 @@ dequeue_blocking(struct v4l_gst *priv, GQueue *queue, GCond *cond) { GstBuffer *gstbuf; - gstbuf = g_queue_pop_head(queue); - while (!gstbuf && priv->is_pipeline_started) { + gstbuf = g_queue_pop_head(queue); + while (!gstbuf && priv->is_pipeline_started && + !g_atomic_int_get(&priv->shutting_down)) { g_cond_wait(cond, &priv->queue_mutex); gstbuf = g_queue_pop_head(queue); } + if (!gstbuf && g_atomic_int_get(&priv->shutting_down)) { + /* Streaming is being stopped by v4l_gst_deinit(); report it + the same way a real device does after STREAMOFF. */ + errno = EPERM; + } return gstbuf; } @@ -1143,6 +1161,12 @@ reqbuf_ioctl_out(struct v4l_gst *priv, g_mutex_lock(&priv->dev_lock); + if (g_atomic_int_get(&priv->shutting_down)) { + errno = EBUSY; + ret = -1; + goto unlock; + } + if (req->count == 0) { GST_DEBUG("req->count == 0"); @@ -1269,19 +1293,27 @@ peek_first_cap_buffer(struct v4l_gst *priv) } -static gboolean -wait_for_all_bufs_collected(struct v4l_gst *priv, - guint max_buffers) +/* + * Waits until max_buffers buffers are collected from appsink, or until + * the stream ends (EOS), whichever comes first. A stream shorter than + * the buffer pool would otherwise stall this until the timeout expires + * and VIDIOC_REQBUF would fail even though the decoder worked + * correctly. Returns the number of buffers actually collected. + */ +static guint +wait_for_bufs_collected(struct v4l_gst *priv, + guint max_buffers) { GQueue *queue = priv->out.gstbufs_queue; - gboolean succeeded; + guint collected; gboolean timed_out = FALSE; gint64 end_time; g_mutex_lock(&priv->queue_mutex); end_time = g_get_monotonic_time() + INITIAL_BUFFER_WAIT_TIMEOUT; while (g_queue_get_length(queue) < max_buffers && - priv->is_pipeline_started) { + priv->is_pipeline_started && + priv->eos_state != EOS_GOT) { if (!g_cond_wait_until(&priv->queue_cond, &priv->queue_mutex, end_time)) { @@ -1289,13 +1321,14 @@ wait_for_all_bufs_collected(struct v4l_gst *priv, break; } } - succeeded = g_queue_get_length(queue) >= max_buffers; + collected = g_queue_get_length(queue); g_mutex_unlock(&priv->queue_mutex); - if (timed_out && !succeeded) - GST_WARNING("Timed out waiting CAPTURE buffers."); + if (timed_out && collected < max_buffers) + GST_WARNING("Timed out waiting CAPTURE buffers " + "(collected %u of %u).", collected, max_buffers); - return succeeded; + return collected; } @@ -1333,8 +1366,8 @@ create_cap_buffers_list(struct v4l_gst *priv) { GstBuffer *first_gstbuf; guint actual_max_buffers; + guint collected; gint i, j; - gboolean succeeded; if (priv->cap.buffers) /* Cannot realloc the buffers without stopping the pipeline, @@ -1399,15 +1432,26 @@ create_cap_buffers_list(struct v4l_gst *priv) /* We wait for buffers from appsink to be collected for the maximum number of the buffer pool. */ g_mutex_unlock(&priv->dev_lock); - succeeded = wait_for_all_bufs_collected(priv, actual_max_buffers); + collected = wait_for_bufs_collected(priv, actual_max_buffers); g_mutex_lock(&priv->dev_lock); - if (!succeeded) { + if (collected == 0) { GST_ERROR("Failed to collect buffers for CAPTURE."); errno = EINVAL; return 0; } + if (collected < actual_max_buffers) { + /* The stream produced fewer frames than the buffer pool + holds (e.g. a short stream or an EOS). Use what we + have; VIDIOC_REQBUF is allowed to return fewer buffers + than requested. */ + GST_DEBUG("The stream produced only %u of %u buffers; " + "using %u", collected, actual_max_buffers, + collected); + actual_max_buffers = collected; + } + priv->cap.buffers = g_new0(struct v4l_gst_buffer, actual_max_buffers); for (i = 0; i < actual_max_buffers; i++) { @@ -1512,52 +1556,71 @@ static int streamon_ioctl_out(struct v4l_gst *priv) { GstState state; + GstBuffer *eos_gstbuf; + int ret = -1; + + g_mutex_lock(&priv->dev_lock); + /* An operation admitted before close may only reach here after + shutdown has started. Serialize this check and PLAYING with the + final shutdown stop, using the same device lock. */ + if (g_atomic_int_get(&priv->shutting_down)) { + errno = EBUSY; + goto unlock; + } if (priv->is_pipeline_started) { GST_ERROR("The pipeline is already running"); errno = EBUSY; - return -1; + goto unlock; } GST_OBJECT_LOCK(priv->pipeline); state = GST_STATE(priv->pipeline); GST_OBJECT_UNLOCK(priv->pipeline); - g_mutex_lock(&priv->dev_lock); - if (state == GST_STATE_NULL) { if (!priv->out_caps_set && !v4l_gst_pipeline_set_out_format(priv)) - return -1; + goto unlock; if (!v4l_gst_pipeline_set_cap_format(priv)) - return -1; + goto unlock; } if (!gst_buffer_pool_is_active(priv->out.pool)) { if (!gst_buffer_pool_set_active(priv->out.pool, TRUE)) { GST_ERROR("Failed to activate buffer pool"); errno = EINVAL; - return -1; + goto unlock; } /* Restore the extra reference counted up in the streamoff */ gst_buffer_unref(priv->out.buffers[0].gstbuf); } + /* Release a buffer cached by a previous QBUF(bytesused=0) whose + EOS event never arrived, e.g. because of a STREAMOFF. */ + g_mutex_lock(&priv->queue_mutex); + eos_gstbuf = priv->eos_gstbuf; + priv->eos_gstbuf = NULL; priv->eos_state = EOS_NONE; + g_mutex_unlock(&priv->queue_mutex); + if (eos_gstbuf) + gst_buffer_unref(eos_gstbuf); v4l_gst_core_reset_cap_timestamp_state(priv); v4l_gst_core_set_pipeline_started(priv, TRUE); gst_element_set_state(priv->pipeline, GST_STATE_PLAYING); - g_mutex_unlock(&priv->dev_lock); - GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(priv->pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "v4l-gst.streamon.snapshot"); - return 0; + ret = 0; + + unlock: + g_mutex_unlock(&priv->dev_lock); + return ret; } diff --git a/lib/libv4l-gst/v4l-gst-core.c b/lib/libv4l-gst/v4l-gst-core.c index c284adf..24ce98b 100644 --- a/lib/libv4l-gst/v4l-gst-core.c +++ b/lib/libv4l-gst/v4l-gst-core.c @@ -852,6 +852,12 @@ v4l_gst_core_push_source_change_event(struct v4l_gst *priv) void v4l_gst_core_set_pipeline_started(struct v4l_gst *priv, gboolean started) { + /* Once the plugin is shutting down the pipeline can never start + again, so that interruptible waits cannot re-enter a sleep while + v4l_gst_deinit() is draining them. */ + if (started && g_atomic_int_get(&priv->shutting_down)) + started = FALSE; + g_mutex_lock(&priv->cap.reqbuf_mutex); priv->cap.cancel_reqbuf_wait = !started; if (!started) @@ -866,6 +872,30 @@ v4l_gst_core_set_pipeline_started(struct v4l_gst *priv, gboolean started) } +gboolean +v4l_gst_core_enter_operation(struct v4l_gst *priv) +{ + /* Count this call as in-flight so that v4l_gst_deinit() can wait + until no other thread is inside the plugin before freeing state. + The caller must always pair this with v4l_gst_core_leave_operation() + regardless of the return value. */ + g_atomic_int_inc(&priv->inflight_ioctls); + if (g_atomic_int_get(&priv->shutting_down)) { + errno = EBUSY; + return FALSE; + } + + return TRUE; +} + + +void +v4l_gst_core_leave_operation(struct v4l_gst *priv) +{ + g_atomic_int_dec_and_test(&priv->inflight_ioctls); +} + + static gboolean init_app_elements(struct v4l_gst *priv) { @@ -1089,14 +1119,156 @@ v4l_gst_init(int fd) } +/* + * The helpers below implement the "quiet" phase of the shutdown. They + * are guards for applications that close the file descriptor without + * following the expected sequence (sending DECODER_CMD STOP, STREAMOFF, + * returning all dequeued buffers and finishing their own threads). + * For a well-behaved application each of them is a no-op. + */ + +/* + * Guard for applications that close without returning the buffers they + * dequeued: drop the references held on their behalf. The blocked + * streaming threads that this situation produces are woken up by + * inactivate_buffer_pools(); this helper runs after the drain, so that + * it cannot race with a client thread still touching its buffers. + */ +static void +return_client_buffers_to_pools(struct v4l_gst *priv) +{ + gint i; + + if (priv->cap.buffers) { + for (i = 0; i < priv->cap.buffers_num; i++) { + if (priv->cap.buffers[i].state == + V4L_GST_BUFFER_DEQUEUED) { + gst_buffer_unref(priv->cap.buffers[i].gstbuf); + priv->cap.buffers[i].state = + V4L_GST_BUFFER_QUEUED; + } + } + } + if (priv->out.buffers) { + for (i = 0; i < priv->out.buffers_num; i++) { + if (priv->out.buffers[i].state == + V4L_GST_BUFFER_DEQUEUED) { + gst_buffer_unref(priv->out.buffers[i].gstbuf); + priv->out.buffers[i].state = + V4L_GST_BUFFER_QUEUED; + } + } + } +} + +/* + * Guard for applications that close without STREAMOFF. Inactivating the + * pools wakes up threads blocked in gst_buffer_pool_acquire_buffer(): + * client threads on OUTPUT and streaming threads on CAPTURE. + */ +static void +inactivate_buffer_pools(struct v4l_gst *priv) +{ + if (priv->out.pool && gst_buffer_pool_is_active(priv->out.pool)) { + if (!gst_buffer_pool_set_active(priv->out.pool, FALSE)) + GST_WARNING("Failed to inactivate the OUTPUT " + "buffer pool"); + } + if (priv->cap.pool && gst_buffer_pool_is_active(priv->cap.pool)) { + if (!gst_buffer_pool_set_active(priv->cap.pool, FALSE)) + GST_WARNING("Failed to inactivate the CAPTURE " + "buffer pool"); + } +} + +/* + * Guard for applications that close while the pipeline is still running, + * i.e. without STREAMOFF or DECODER_CMD STOP. Normally the pipeline is + * already stopped at this point; if it is not, stop it so that no streaming + * thread, pad probe or appsink callback can touch the plugin state anymore. + * NULL transitions are synchronous: only an unexpected ASYNC result has + * a bounded completion wait, not set_state(NULL) itself. + */ +static void +stop_pipeline_for_shutdown(struct v4l_gst *priv) +{ + GstStateChangeReturn state_ret; + gint64 deadline; + + if (!priv->pipeline) + return; + + state_ret = gst_element_set_state(priv->pipeline, GST_STATE_NULL); + if (state_ret == GST_STATE_CHANGE_ASYNC) { + deadline = g_get_monotonic_time() + INITIAL_BUFFER_WAIT_TIMEOUT; + do { + state_ret = gst_element_get_state(priv->pipeline, + NULL, NULL, + GST_SECOND); + } while (state_ret == GST_STATE_CHANGE_ASYNC && + g_get_monotonic_time() < deadline); + } + if (state_ret != GST_STATE_CHANGE_SUCCESS) { + GST_WARNING("Failed to stop the pipeline cleanly (ret:%s)", + gst_element_state_change_return_get_name(state_ret)); + } +} + +/* + * Guard for applications that close while their own threads are still + * inside an ioctl or mmap. All interruptible waits bail out on + * shutting_down, so this drain is bounded; the timeout is only a safety + * net. + */ +static void +drain_inflight_operations(struct v4l_gst *priv) +{ + gint64 deadline; + + deadline = g_get_monotonic_time() + INITIAL_BUFFER_WAIT_TIMEOUT; + while (g_atomic_int_get(&priv->inflight_ioctls) > 0) { + if (g_get_monotonic_time() >= deadline) { + GST_WARNING("Timed out draining in-flight ioctls " + "(%d still running)", + g_atomic_int_get(&priv->inflight_ioctls)); + break; + } + g_usleep(1000); + } +} + void v4l_gst_deinit(struct v4l_gst *priv) { GST_DEBUG("v4l_gst_deinit start"); + /* Reject new ioctls and wake up all interruptible waits so that + client threads leave their ioctl handlers. */ + g_atomic_int_set(&priv->shutting_down, 1); v4l_gst_core_set_pipeline_started(priv, FALSE); - g_mutex_clear(&priv->dev_lock); + /* Make the plugin quiet before freeing anything. Each step below + is a guard for applications that skipped the corresponding part + of the normal shutdown sequence; see the helper comments. + First wake pool acquisitions before taking dev_lock: a blocked + OUTPUT ioctl may hold that lock. Repeat inactivation under the + lock, in case an already-running ioctl reactivated a pool. */ + inactivate_buffer_pools(priv); + g_mutex_lock(&priv->dev_lock); + inactivate_buffer_pools(priv); + stop_pipeline_for_shutdown(priv); + g_mutex_unlock(&priv->dev_lock); + drain_inflight_operations(priv); + return_client_buffers_to_pools(priv); + /* An empty OUTPUT QBUF may never receive EOS (e.g. close before + STREAMON). Its acquired reference is still owned by this cache. */ + if (priv->eos_gstbuf) { + gst_buffer_unref(priv->eos_gstbuf); + priv->eos_gstbuf = NULL; + } + + /* From here on no other thread references the plugin state, so it + is safe to remove probes, free shared objects and clear mutexes. */ if (priv->v4l2events.queue) { g_queue_free_full(priv->v4l2events.queue, @@ -1157,6 +1329,8 @@ v4l_gst_deinit(struct v4l_gst *priv) delete_event_state(priv->event_state); + g_mutex_clear(&priv->dev_lock); + g_free(priv); GST_DEBUG("v4l_gst_deinit end"); diff --git a/lib/libv4l-gst/v4l-gst-internal.h b/lib/libv4l-gst/v4l-gst-internal.h index 81fd972..d4480e3 100644 --- a/lib/libv4l-gst/v4l-gst-internal.h +++ b/lib/libv4l-gst/v4l-gst-internal.h @@ -189,6 +189,19 @@ struct v4l_gst { GQueue *queue; } v4l2events; + /* + * Shutdown coordination, accessed only through g_atomic_int_*(): + * + * shutting_down : set once by v4l_gst_deinit() before freeing any + * shared state; interruptible waits and new ioctls + * bail out when it is set. + * inflight_ioctls : number of client threads currently inside a + * plugin ioctl/mmap entry; v4l_gst_deinit() waits + * for it to drop to 0 before freeing shared state. + */ + volatile gint shutting_down; + volatile gint inflight_ioctls; + GMutex dev_lock; }; @@ -216,6 +229,8 @@ void v4l_gst_core_push_source_change_event (struct v4l_gst *priv); void v4l_gst_core_reset_cap_timestamp_state (struct v4l_gst *priv); void v4l_gst_core_set_pipeline_started (struct v4l_gst *priv, gboolean started); +gboolean v4l_gst_core_enter_operation (struct v4l_gst *priv); +void v4l_gst_core_leave_operation (struct v4l_gst *priv); /* pipeline */ GstCaps * v4l_gst_pipeline_get_codec_caps_from_fourcc (guint fourcc); diff --git a/lib/libv4l-gst/v4l-gst-pipeline.c b/lib/libv4l-gst/v4l-gst-pipeline.c index 866d141..cba5f0f 100644 --- a/lib/libv4l-gst/v4l-gst-pipeline.c +++ b/lib/libv4l-gst/v4l-gst-pipeline.c @@ -352,12 +352,21 @@ static void appsink_callback_eos(GstAppSink *appsink, gpointer user_data) { struct v4l_gst *priv = user_data; - if (priv->eos_gstbuf) - v4l_gst_buf_release_out_buffer(priv, priv->eos_gstbuf); + GstBuffer *eos_gstbuf; + + g_mutex_lock(&priv->queue_mutex); + eos_gstbuf = priv->eos_gstbuf; + priv->eos_gstbuf = NULL; + g_mutex_unlock(&priv->queue_mutex); + if (eos_gstbuf) + v4l_gst_buf_release_out_buffer(priv, eos_gstbuf); g_mutex_lock(&priv->queue_mutex); GST_DEBUG("EOS: Got from AppSink. Cached buffers: %u", g_queue_get_length(priv->cap.gstbufs_queue)); priv->eos_state = EOS_GOT; + /* Wake up waiters that stop early once the stream has ended, + e.g. wait_for_bufs_collected(). */ + g_cond_signal(&priv->queue_cond); if (priv->cap.gstbufs_queue && !g_queue_is_empty(priv->cap.gstbufs_queue)) { set_event(priv->event_state, POLLOUT); @@ -587,6 +596,7 @@ int v4l_gst_pipeline_stop(struct v4l_gst *priv) { GstStateChangeReturn state_ret; + gint64 deadline; int ret = 0; gint i; @@ -594,15 +604,30 @@ v4l_gst_pipeline_stop(struct v4l_gst *priv) v4l_gst_core_set_pipeline_started(priv, FALSE); + /* Guard for applications that stop the pipeline while still holding + all dequeued CAPTURE buffers: a streaming thread blocked in + gst_buffer_pool_acquire_buffer() would keep the pipeline from + reaching GST_STATE_NULL forever. Flushing the pools wakes up + blocked acquisitions. */ + if (priv->out.pool) + gst_buffer_pool_set_flushing(priv->out.pool, TRUE); + if (priv->cap.pool) + gst_buffer_pool_set_flushing(priv->cap.pool, TRUE); + state_ret = gst_element_set_state(priv->pipeline, GST_STATE_NULL); - while (state_ret == GST_STATE_CHANGE_ASYNC) { - /* This API blocks up to the ASYNC state change completion. */ - g_mutex_unlock(&priv->dev_lock); - state_ret = gst_element_get_state(priv->pipeline, NULL, - NULL, - GST_CLOCK_TIME_NONE); - g_mutex_lock(&priv->dev_lock); + if (state_ret == GST_STATE_CHANGE_ASYNC) { + /* Bound only an unexpected ASYNC result. The synchronous + set_state(NULL) call above is not covered by this timeout. */ + deadline = g_get_monotonic_time() + INITIAL_BUFFER_WAIT_TIMEOUT; + do { + g_mutex_unlock(&priv->dev_lock); + state_ret = gst_element_get_state(priv->pipeline, NULL, + NULL, + GST_SECOND); + g_mutex_lock(&priv->dev_lock); + } while (state_ret == GST_STATE_CHANGE_ASYNC && + g_get_monotonic_time() < deadline); } if (state_ret != GST_STATE_CHANGE_SUCCESS) { @@ -639,6 +664,13 @@ v4l_gst_pipeline_stop(struct v4l_gst *priv) priv->cap.buffers_num = 0; v4l_gst_fmt_init_decoded_frame_params(&priv->cap.fmt); + /* The next streaming cycle must start without the flushing state + that was used to wake up the blocked acquisitions above. */ + if (priv->out.pool) + gst_buffer_pool_set_flushing(priv->out.pool, FALSE); + if (priv->cap.pool) + gst_buffer_pool_set_flushing(priv->cap.pool, FALSE); + return ret; } diff --git a/tests/test-gst-backend.c b/tests/test-gst-backend.c index d7304ac..3fb83b7 100644 --- a/tests/test-gst-backend.c +++ b/tests/test-gst-backend.c @@ -1,4 +1,5 @@ #include "v4l-gst-testhooks.h" +#include "v4l-gst.h" #include #include #include @@ -1813,3 +1814,501 @@ test_h264dec_decoder_streaming_produces_nv12(void) g_free(stream); } + +/* + * Shared setup for the shutdown tests: configure a real H264 decoder and + * optionally queue an OUTPUT payload. start_h264dec_stream() also starts + * streaming; a zero-length payload leaves all OUTPUT buffers dequeued. + */ +static void +prepare_h264dec_stream(const guint8 *stream, gsize stream_len) +{ + struct v4l2_format format = { 0, }; + struct v4l2_requestbuffers req = { 0, }; + struct v4l2_buffer buf = { 0, }; + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + const guint32 w = 320; + const guint32 h = 240; + const gsize frame_size = w * h * 3 / 2; + void *map; + int ret; + + prepare_pipeline_backend_fixture(backend, V4L_GST_ROLE_DECODER, + "h264parse ! avdec_h264 ! videoconvert", + V4L2_PIX_FMT_H264, V4L2_PIX_FMT_NV12, + w, h, frame_size, 4 * 1024 * 1024); + assert_role(V4L_GST_ROLE_DECODER); + + format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264; + format.fmt.pix_mp.plane_fmt[0].sizeimage = 4 * 1024 * 1024; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_S_FMT, &format); + cut_assert(ret == 0); + + memset(&format, 0, sizeof(format)); + format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; + format.fmt.pix_mp.width = w; + format.fmt.pix_mp.height = h; + format.fmt.pix_mp.num_planes = 1; + format.fmt.pix_mp.plane_fmt[0].bytesperline = w; + format.fmt.pix_mp.plane_fmt[0].sizeimage = frame_size; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_S_FMT, &format); + cut_assert(ret == 0); + + memset(&req, 0, sizeof(req)); + req.count = 4; + req.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_REQBUFS, &req); + cut_assert(ret == 0); + cut_assert(req.count >= 1); + if (stream_len == 0) + return; + + memset(&buf, 0, sizeof(buf)); + buf.index = 0; + buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + buf.memory = V4L2_MEMORY_MMAP; + buf.m.planes = planes; + buf.length = 1; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_QUERYBUF, &buf); + cut_assert(ret == 0); + + map = libv4l2_plugin.mmap(backend, NULL, buf.m.planes[0].length, + PROT_READ | PROT_WRITE, MAP_SHARED, -1, + (int64_t) buf.m.planes[0].m.mem_offset); + cut_assert_not_null(map); + cut_assert(stream_len <= (gsize) buf.m.planes[0].length); + memcpy(map, stream, stream_len); + buf.m.planes[0].bytesused = (unsigned int) stream_len; + munmap(map, buf.m.planes[0].length); + + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_QBUF, &buf); + cut_assert(ret == 0); +} + +static void +start_h264dec_stream(const guint8 *stream, gsize stream_len) +{ + enum v4l2_buf_type type; + int ret; + + prepare_h264dec_stream(stream, stream_len); + + type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_STREAMON, &type); + cut_assert(ret == 0); +} + +static gboolean +h264dec_pipeline_available(void) +{ + return gst_element_factory_find("h264parse") != NULL && + gst_element_factory_find("avdec_h264") != NULL && + gst_element_factory_find("videoconvert") != NULL; +} + +/* + * Regression test for the shutdown deadlock: start decoding and close the + * backend immediately after STREAMON without draining anything. Before the + * deinit lifecycle fix, close() freed shared state while the streaming + * threads were still running, which hung or crashed the process. + */ +void +test_h264dec_close_immediately_after_streamon(void) +{ + guint8 *stream; + gsize stream_len = 0; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + + stream = generate_h264_sample(100, 320, 240, &stream_len); + if (!stream) + cut_pend("failed to generate an h264 sample stream"); + + start_h264dec_stream(stream, stream_len); + g_free(stream); + + /* Return immediately; teardown() closes the backend while the decoder + is still running. The test passes if close() returns. */ +} + +struct dqbuf_thread_args { + int ret; + int err; +}; + +static gpointer +dqbuf_cap_until_shutdown(gpointer data) +{ + struct dqbuf_thread_args *args = data; + struct v4l2_buffer buf = { 0, }; + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + buf.memory = V4L2_MEMORY_MMAP; + buf.m.planes = planes; + buf.length = 1; + + /* Keep dequeuing until the stream is torn down: a blocked DQBUF is + cancelled with EPERM, a later re-entry is rejected with EBUSY. */ + for (;;) { + errno = 0; + args->ret = v4l_gst_ioctl(VIDIOC_DQBUF, &buf); + if (args->ret == 0) + continue; + args->err = errno; + if (args->err == EPERM || args->err == EBUSY) + break; + g_usleep(1000); + } + + return NULL; +} + +/* + * Close the backend while another thread is blocked in a blocking + * VIDIOC_DQBUF on CAPTURE. The blocked call must be cancelled by the + * shutdown instead of hanging forever. + */ +void +test_h264dec_dqbuf_blocking_close_race(void) +{ + struct dqbuf_thread_args args = { 0, }; + struct v4l2_requestbuffers req = { 0, }; + guint8 *stream; + gsize stream_len = 0; + GThread *thread; + int ret; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + + stream = generate_h264_sample(100, 320, 240, &stream_len); + if (!stream) + cut_pend("failed to generate an h264 sample stream"); + + start_h264dec_stream(stream, stream_len); + g_free(stream); + + memset(&req, 0, sizeof(req)); + req.count = 8; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_REQBUFS, &req); + cut_assert(ret == 0); + cut_assert(req.count > 0); + + args.ret = 0; + thread = g_thread_new("dqbuf-cap", dqbuf_cap_until_shutdown, &args); + g_usleep(200 * 1000); + + /* Close while the thread is inside VIDIOC_DQBUF. */ + libv4l2_plugin.close(backend); + backend = NULL; + + g_thread_join(thread); + cut_assert(args.ret == -1); + cut_assert(args.err == EPERM || args.err == EBUSY); +} + +static gpointer +reqbuf_cap_once(gpointer data) +{ + struct dqbuf_thread_args *args = data; + struct v4l2_requestbuffers req = { 0, }; + + req.count = 8; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + errno = 0; + args->ret = v4l_gst_ioctl(VIDIOC_REQBUFS, &req); + args->err = errno; + + return NULL; +} + +/* + * Close the backend while another thread is inside a VIDIOC_REQBUFS on + * CAPTURE waiting for the first decoded buffer. The payload carries no + * valid H264 data, so that wait can only end when the shutdown cancels it; + * it must not block until the initial-buffer timeout expires. + */ +void +test_h264dec_reqbuf_cap_close_race(void) +{ + struct dqbuf_thread_args args = { 0, }; + const guint8 garbage[] = { 0x00 }; + GThread *thread; + gint64 close_time, joined_time; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + + start_h264dec_stream(garbage, sizeof(garbage)); + + args.ret = 0; + thread = g_thread_new("reqbuf-cap", reqbuf_cap_once, &args); + g_usleep(200 * 1000); + + close_time = g_get_monotonic_time(); + libv4l2_plugin.close(backend); + backend = NULL; + + g_thread_join(thread); + joined_time = g_get_monotonic_time(); + + cut_assert(args.ret == -1); + cut_assert(args.err == EINVAL); + /* The wait must have been cancelled by the shutdown, not run to the + INITIAL_BUFFER_WAIT_TIMEOUT. */ + cut_assert(joined_time - close_time < 5 * G_TIME_SPAN_SECOND); +} + +/* + * Stop the pipeline with VIDIOC_REQBUF(count=0) while the client still + * holds every dequeued CAPTURE buffer. The decoder is then blocked + * acquiring a CAPTURE buffer for the next frame, and before the + * bounded-stop fix the state change to GST_STATE_NULL waited forever + * for that streaming thread. + */ +void +test_h264dec_reqbuf_zero_while_holding_all_cap_buffers(void) +{ + struct v4l2_requestbuffers req = { 0, }; + guint8 *stream; + gsize stream_len = 0; + gint64 start_time; + int ret; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + + stream = generate_h264_sample(100, 320, 240, &stream_len); + if (!stream) + cut_pend("failed to generate an h264 sample stream"); + + start_h264dec_stream(stream, stream_len); + g_free(stream); + + memset(&req, 0, sizeof(req)); + req.count = 8; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_REQBUFS, &req); + cut_assert(ret == 0); + cut_assert(req.count > 0); + + /* Every CAPTURE buffer is in the DEQUEUED state held by the + client, so the decoder cannot produce more frames. */ + memset(&req, 0, sizeof(req)); + req.count = 0; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + start_time = g_get_monotonic_time(); + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_REQBUFS, &req); + cut_assert(ret == 0); + cut_assert(g_get_monotonic_time() - start_time < + 5 * G_TIME_SPAN_SECOND); +} + +/* + * A stream shorter than the requested CAPTURE buffer count must not + * stall VIDIOC_REQBUF until the collection timeout: the EOS event sent + * by QBUF(bytesused=0) ends the stream, and VIDIOC_REQBUF is allowed to + * return fewer buffers than requested. + */ +void +test_h264dec_reqbuf_cap_short_stream_with_eos(void) +{ + struct v4l2_requestbuffers req = { 0, }; + struct v4l2_buffer buf = { 0, }; + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + guint8 *stream; + gsize stream_len = 0; + gint64 start_time; + int ret; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + + stream = generate_h264_sample(3, 320, 240, &stream_len); + if (!stream) + cut_pend("failed to generate an h264 sample stream"); + + start_h264dec_stream(stream, stream_len); + g_free(stream); + + /* QBUF with bytesused == 0 sends an EOS event to the pipeline. */ + memset(&buf, 0, sizeof(buf)); + buf.index = 1; + buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + buf.memory = V4L2_MEMORY_MMAP; + buf.m.planes = planes; + buf.length = 1; + buf.m.planes[0].bytesused = 0; + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_QBUF, &buf); + cut_assert(ret == 0); + + memset(&req, 0, sizeof(req)); + req.count = 8; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + start_time = g_get_monotonic_time(); + errno = 0; + ret = v4l_gst_ioctl(VIDIOC_REQBUFS, &req); + cut_assert(ret == 0); + cut_assert(req.count >= 1); + cut_assert(req.count < 8); + /* The wait must have ended at the EOS, not at the collection + timeout. */ + cut_assert(g_get_monotonic_time() - start_time < + 5 * G_TIME_SPAN_SECOND); +} + +/* EOS must return both OUTPUT buffers, not merely signal POLLIN while + * retaining the empty buffer outside the pool. Nonblocking DQBUF makes + * this regression fail instead of hanging the test process. */ +void +test_h264dec_eos_returns_empty_output_buffer(void) +{ + struct v4l2_requestbuffers req = { 0, }; + struct v4l2_buffer buf = { 0, }; + struct v4l2_plane planes[VIDEO_MAX_PLANES] = { 0, }; + guint8 *stream; + gsize stream_len = 0; + gboolean returned[2] = { FALSE, FALSE }; + gboolean got_eos; + guint count = 0; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + stream = generate_h264_sample(3, 320, 240, &stream_len); + cut_assert_not_null(stream); + start_h264dec_stream(stream, stream_len); + g_free(stream); + + buf.index = 1; + buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + buf.memory = V4L2_MEMORY_MMAP; + buf.m.planes = planes; + buf.length = 1; + cut_assert_equal_int(0, v4l_gst_ioctl(VIDIOC_QBUF, &buf)); + + req.count = 8; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + cut_assert_equal_int(0, v4l_gst_ioctl(VIDIOC_REQBUFS, &req)); + g_mutex_lock(&backend->queue_mutex); + got_eos = backend->eos_state == EOS_GOT; + g_mutex_unlock(&backend->queue_mutex); + cut_assert(got_eos); + + backend->is_non_blocking = TRUE; + while (v4l_gst_ioctl(VIDIOC_DQBUF, &buf) == 0) { + cut_assert(buf.index < 2); + cut_assert(!returned[buf.index]); + returned[buf.index] = TRUE; + count++; + } + cut_assert_equal_int(EAGAIN, errno); + cut_assert_equal_uint(2, count); + cut_assert_equal_int(0, backend->out.returned_cnt); +} + +/* Stage an operation paused between enter_operation() and dispatch, then + * let shutdown stop the pipeline before resuming that admitted handler. + * New plugin calls are also checked through the actual public wrapper. */ +void +test_shutdown_rejects_pre_admitted_streamon_and_reqbuf_out(void) +{ + const guint8 garbage[] = { 0 }; + enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + struct v4l2_requestbuffers req = { 0, }; + int streamon_ret, streamon_errno, reqbuf_ret, reqbuf_errno; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + start_h264dec_stream(garbage, sizeof(garbage)); + cut_assert(v4l_gst_core_enter_operation(backend)); + cut_assert(v4l_gst_core_enter_operation(backend)); + g_atomic_int_set(&backend->shutting_down, 1); + v4l_gst_core_set_pipeline_started(backend, FALSE); + gst_buffer_pool_set_active(backend->out.pool, FALSE); + gst_buffer_pool_set_active(backend->cap.pool, FALSE); + cut_assert_equal_int(GST_STATE_CHANGE_SUCCESS, + gst_element_set_state(backend->pipeline, GST_STATE_NULL)); + + streamon_ret = v4l_gst_ioctl_streamon(backend, &type); + streamon_errno = errno; + v4l_gst_core_leave_operation(backend); + req.count = 4; + req.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + req.memory = V4L2_MEMORY_MMAP; + reqbuf_ret = v4l_gst_ioctl_reqbuf(backend, &req); + reqbuf_errno = errno; + v4l_gst_core_leave_operation(backend); + + cut_assert_equal_int(-1, streamon_ret); + cut_assert_equal_int(EBUSY, streamon_errno); + cut_assert_equal_int(-1, reqbuf_ret); + cut_assert_equal_int(EBUSY, reqbuf_errno); + cut_assert_equal_int(GST_STATE_NULL, GST_STATE_TARGET(backend->pipeline)); + cut_assert(!gst_buffer_pool_is_active(backend->out.pool)); + cut_assert_equal_int(0, g_atomic_int_get(&backend->inflight_ioctls)); + cut_assert_equal_int(-1, v4l_gst_ioctl(VIDIOC_STREAMON, &type)); + cut_assert_equal_int(EBUSY, errno); + cut_assert(libv4l2_plugin.mmap(backend, NULL, 4096, PROT_READ, + MAP_SHARED, -1, 0) == MAP_FAILED); + cut_assert_equal_int(EBUSY, errno); + cut_assert_equal_int(0, g_atomic_int_get(&backend->inflight_ioctls)); +} + +static void +mark_eos_buffer_freed(gpointer data, GstMiniObject *object) +{ + (void)object; + *(gboolean *)data = TRUE; +} + +/* Closing without another STREAMON or an EOS callback must release the + * cached acquired reference. Also reject a second pending empty QBUF. */ +void +test_h264dec_close_releases_pending_eos_buffer(void) +{ + struct v4l2_buffer buf = { 0, }; + struct v4l2_plane planes[VIDEO_MAX_PLANES] = { 0, }; + gboolean freed = FALSE; + + if (!h264dec_pipeline_available()) + cut_pend("h264parse, avdec_h264 or videoconvert is not available"); + prepare_h264dec_stream(NULL, 0); + buf.index = 1; + buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + buf.memory = V4L2_MEMORY_MMAP; + buf.m.planes = planes; + buf.length = 1; + cut_assert_equal_int(0, v4l_gst_ioctl(VIDIOC_QBUF, &buf)); + cut_assert_equal_int(-1, v4l_gst_ioctl(VIDIOC_QBUF, &buf)); + cut_assert_equal_int(EINVAL, errno); + buf.index = 2; + cut_assert_equal_int(-1, v4l_gst_ioctl(VIDIOC_QBUF, &buf)); + cut_assert_equal_int(EBUSY, errno); + gst_mini_object_weak_ref(GST_MINI_OBJECT(backend->out.buffers[1].gstbuf), + mark_eos_buffer_freed, &freed); + libv4l2_plugin.close(backend); + backend = NULL; + cut_assert(freed); +}