From fe8de893ca58f3cce53819bc8409fc33d416387c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 11 Apr 2021 15:01:05 +0200 Subject: [PATCH] Privatize recorder threading The fact that the recorder uses a separate thread is an internal detail, so the functions _start(), _stop() and _join() should not be exposed. Instead, start the thread on _open() and _stop()+_join() on close(). This paves the way to expose the recorder as a packet sink trait. --- app/src/recorder.c | 36 +++++++++++++----------------------- app/src/recorder.h | 11 +---------- app/src/stream.c | 13 +------------ 3 files changed, 15 insertions(+), 45 deletions(-) diff --git a/app/src/recorder.c b/app/src/recorder.c index c24393de..387d8f79 100644 --- a/app/src/recorder.c +++ b/app/src/recorder.c @@ -259,6 +259,16 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) { return false; } + LOGD("Starting recorder thread"); + bool ok = sc_thread_create(&recorder->thread, run_recorder, "recorder", + recorder); + if (!ok) { + LOGC("Could not start recorder thread"); + avio_close(recorder->ctx->pb); + avformat_free_context(recorder->ctx); + return false; + } + LOGI("Recording started to %s file: %s", format_name, recorder->filename); return true; @@ -266,35 +276,15 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) { void recorder_close(struct recorder *recorder) { - avio_close(recorder->ctx->pb); - avformat_free_context(recorder->ctx); -} - -bool -recorder_start(struct recorder *recorder) { - LOGD("Starting recorder thread"); - - bool ok = sc_thread_create(&recorder->thread, run_recorder, "recorder", - recorder); - if (!ok) { - LOGC("Could not start recorder thread"); - return false; - } - - return true; -} - -void -recorder_stop(struct recorder *recorder) { sc_mutex_lock(&recorder->mutex); recorder->stopped = true; sc_cond_signal(&recorder->queue_cond); sc_mutex_unlock(&recorder->mutex); -} -void -recorder_join(struct recorder *recorder) { sc_thread_join(&recorder->thread, NULL); + + avio_close(recorder->ctx->pb); + avformat_free_context(recorder->ctx); } bool diff --git a/app/src/recorder.h b/app/src/recorder.h index be2b2dff..97e499bc 100644 --- a/app/src/recorder.h +++ b/app/src/recorder.h @@ -28,7 +28,7 @@ struct recorder { sc_thread thread; sc_mutex mutex; sc_cond queue_cond; - bool stopped; // set on recorder_stop() by the stream reader + bool stopped; // set on recorder_close() bool failed; // set on packet write failure struct recorder_queue queue; @@ -52,15 +52,6 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec); void recorder_close(struct recorder *recorder); -bool -recorder_start(struct recorder *recorder); - -void -recorder_stop(struct recorder *recorder); - -void -recorder_join(struct recorder *recorder); - bool recorder_push(struct recorder *recorder, const AVPacket *packet); diff --git a/app/src/stream.c b/app/src/stream.c index e0a223be..787e9515 100644 --- a/app/src/stream.c +++ b/app/src/stream.c @@ -203,17 +203,12 @@ run_stream(void *data) { LOGE("Could not open recorder"); goto finally_close_decoder; } - - if (!recorder_start(stream->recorder)) { - LOGE("Could not start recorder"); - goto finally_close_recorder; - } } stream->parser = av_parser_init(AV_CODEC_ID_H264); if (!stream->parser) { LOGE("Could not initialize parser"); - goto finally_stop_and_join_recorder; + goto finally_close_recorder; } // We must only pass complete frames to av_parser_parse2()! @@ -243,12 +238,6 @@ run_stream(void *data) { } av_parser_close(stream->parser); -finally_stop_and_join_recorder: - if (stream->recorder) { - recorder_stop(stream->recorder); - LOGI("Finishing recording..."); - recorder_join(stream->recorder); - } finally_close_recorder: if (stream->recorder) { recorder_close(stream->recorder);