From 97f18d63538ad8f0b1c08f0cad9fcce0f57f5492 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 6 May 2023 23:29:39 -0500 Subject: [PATCH] Fix audio capture reinitialization We returned instead of continuing, so audio never worked after reinit. We also had no retry logic if no audio device was available. --- src/audio.cpp | 15 ++++++++------- src/thread_safe.h | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index d182db14..8115a2a3 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -222,14 +222,15 @@ namespace audio { case platf::capture_e::timeout: continue; case platf::capture_e::reinit: + BOOST_LOG(info) << "Reinitializing audio capture"sv; mic.reset(); - mic = control->microphone(stream->mapping, stream->channelCount, stream->sampleRate, frame_size); - if (!mic) { - BOOST_LOG(error) << "Couldn't re-initialize audio input"sv; - - return; - } - return; + do { + mic = control->microphone(stream->mapping, stream->channelCount, stream->sampleRate, frame_size); + if (!mic) { + BOOST_LOG(warning) << "Couldn't re-initialize audio input"sv; + } + } while (!mic && !shutdown_event->view(5s)); + continue; default: return; } diff --git a/src/thread_safe.h b/src/thread_safe.h index f69f0a5b..de8575d5 100644 --- a/src/thread_safe.h +++ b/src/thread_safe.h @@ -100,6 +100,25 @@ namespace safe { return _status; } + // pop and view shoud not be used interchangeably + template + status_t + view(std::chrono::duration delay) { + std::unique_lock ul { _lock }; + + if (!_continue) { + return util::false_v; + } + + while (!_status) { + if (!_continue || _cv.wait_for(ul, delay) == std::cv_status::timeout) { + return util::false_v; + } + } + + return _status; + } + bool peek() { return _continue && (bool) _status;