From a3703340fc5f2d10d8b8d1a248200f302831c432 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 1 Mar 2023 18:24:31 +0100 Subject: [PATCH] Fix possible race condition on video_buffer end The video_buffer thread clears the queue once it is stopped, but new frames might still be pushed asynchronously. To avoid the problem, do not push any frame once the video_buffer is stopped. --- app/src/video_buffer.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/video_buffer.c b/app/src/video_buffer.c index b3b29098..a8f7f20a 100644 --- a/app/src/video_buffer.c +++ b/app/src/video_buffer.c @@ -99,6 +99,8 @@ run_buffering(void *data) { } stopped: + assert(vb->b.stopped); + // Flush queue while (!sc_vecdeque_is_empty(&vb->b.queue)) { struct sc_video_buffer_frame *p = sc_vecdeque_popref(&vb->b.queue); @@ -206,6 +208,11 @@ sc_video_buffer_push(struct sc_video_buffer *vb, const AVFrame *frame) { sc_mutex_lock(&vb->b.mutex); + if (vb->b.stopped) { + sc_mutex_unlock(&vb->b.mutex); + return false; + } + sc_tick pts = SC_TICK_FROM_US(frame->pts); sc_clock_update(&vb->b.clock, sc_tick_now(), pts); sc_cond_signal(&vb->b.wait_cond);