From 68d594e93ad3d5ed2ec8f5e80a1d2f269f546018 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 30 Nov 2024 21:11:58 +0100 Subject: [PATCH] Safer threads for audio recreate --- client/audio/android/audio.cpp | 71 +++++++++++++++++++--------------- client/audio/android/audio.h | 7 +++- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/client/audio/android/audio.cpp b/client/audio/android/audio.cpp index f6305457..6e26058f 100644 --- a/client/audio/android/audio.cpp +++ b/client/audio/android/audio.cpp @@ -34,7 +34,7 @@ void wivrn::android::audio::exit() speaker_stop_ack.wait(false); AAudioStream_requestStop(speaker); } - if (microphone and AAudioStream_getState(microphone) == AAUDIO_STREAM_STATE_STARTED) + if (microphone and mic_running) { microphone_stop_ack.wait(false); AAudioStream_requestStop(microphone); @@ -122,6 +122,7 @@ int32_t wivrn::android::audio::microphone_data_cb(AAudioStream * stream, void * { self->microphone_stop_ack = true; self->microphone_stop_ack.notify_all(); + self->mic_running = false; return AAUDIO_CALLBACK_RESULT_STOP; } @@ -138,6 +139,7 @@ int32_t wivrn::android::audio::microphone_data_cb(AAudioStream * stream, void * { self->microphone_stop_ack = true; self->microphone_stop_ack.notify_all(); + self->mic_running = false; return AAUDIO_CALLBACK_RESULT_STOP; } @@ -189,59 +191,60 @@ void wivrn::android::audio::build_speaker(AAudioStreamBuilder * builder, int32_t } } -void wivrn::android::audio::recreate_stream(audio * self, AAudioStreamStruct * stream) +void wivrn::android::audio::recreate_stream(AAudioStreamStruct * stream) { - size_t num_channels = AAudioStream_getChannelCount(stream); - size_t sample_rate = AAudioStream_getSampleRate(stream); + if (exiting) + return; + std::unique_lock lock(mutex); - AAudioStream_requestStop(stream); - AAudioStream_close(stream); - - AAudioStreamBuilder * builder; - aaudio_result_t result = AAudio_createStreamBuilder(&builder); - if (result != AAUDIO_OK) - throw std::runtime_error(std::string("Cannot create stream builder: ") + AAudio_convertResultToText(result)); - - if (stream == self->speaker) - self->build_speaker(builder, sample_rate, num_channels); - else if (stream == self->microphone) + if (recreate_thread.joinable()) { - self->build_microphone(builder, sample_rate, num_channels); - - result = AAudioStream_requestStart(self->microphone); - if (result == AAUDIO_OK) - spdlog::info("Microphone stream started"); - else - { - AAudioStream_close(self->microphone); - spdlog::warn("Microphone stream failed to start: {}", AAudio_convertResultToText(result)); - } + assert(recreate_thread.get_id() != std::this_thread::get_id()); + recreate_thread.join(); } - else - spdlog::error("Stream to recreate is neither speaker, not microphone!"); - AAudioStreamBuilder_delete(builder); + recreate_thread = utils::named_thread("recreate_audio", [=, this]() { + std::unique_lock lock(mutex); + size_t num_channels = AAudioStream_getChannelCount(stream); + size_t sample_rate = AAudioStream_getSampleRate(stream); + + AAudioStream_requestStop(stream); + AAudioStream_close(stream); + + AAudioStreamBuilder * builder; + aaudio_result_t result = AAudio_createStreamBuilder(&builder); + if (result != AAUDIO_OK) + spdlog::error("Cannot create stream builder: {}", AAudio_convertResultToText(result)); + + if (stream == speaker) + build_speaker(builder, sample_rate, num_channels); + else if (stream == microphone) + build_microphone(builder, sample_rate, num_channels); + else + spdlog::error("Stream to recreate is neither speaker, not microphone!"); + + AAudioStreamBuilder_delete(builder); + }); } void wivrn::android::audio::speaker_error_cb(AAudioStream * stream, void * userdata, aaudio_result_t error) { auto self = (wivrn::android::audio *)userdata; spdlog::warn("Speaker stream interrupted: {}", AAudio_convertResultToText(error)); - std::thread recreate_thread = utils::named_thread("recreate_stream", &self->recreate_stream, self, stream); - recreate_thread.detach(); + self->recreate_stream(stream); } void wivrn::android::audio::microphone_error_cb(AAudioStream * stream, void * userdata, aaudio_result_t error) { auto self = (wivrn::android::audio *)userdata; spdlog::warn("Microphone stream interrupted: {}", AAudio_convertResultToText(error)); - std::thread recreate_thread = utils::named_thread("recreate_stream", &self->recreate_stream, self, stream); - recreate_thread.detach(); + self->recreate_stream(stream); } wivrn::android::audio::audio(const wivrn::to_headset::audio_stream_description & desc, wivrn_session & session, xr::instance & instance) : session(session), instance(instance) { + std::unique_lock lock(mutex); AAudioStreamBuilder * builder; aaudio_result_t result = AAudio_createStreamBuilder(&builder); if (result != AAUDIO_OK) @@ -265,6 +268,10 @@ wivrn::android::audio::~audio() if (stream) AAudioStream_close(stream); } + + std::unique_lock lock(mutex); + if (recreate_thread.joinable()) + recreate_thread.join(); } void wivrn::android::audio::operator()(wivrn::audio_data && data) diff --git a/client/audio/android/audio.h b/client/audio/android/audio.h index 4d056cd4..11edda85 100644 --- a/client/audio/android/audio.h +++ b/client/audio/android/audio.h @@ -22,6 +22,8 @@ #include "utils/ring_buffer.h" #include "wivrn_packets.h" #include +#include +#include struct AAudioStreamStruct; struct AAudioStreamBuilderStruct; @@ -43,8 +45,9 @@ class audio static void microphone_error_cb(AAudioStreamStruct *, void *, int32_t); static void speaker_error_cb(AAudioStreamStruct *, void *, int32_t); - static void recreate_stream(audio *, AAudioStreamStruct *); + void recreate_stream(AAudioStreamStruct *); + // must own the mutex to call the method void build_microphone(AAudioStreamBuilderStruct *, int32_t, int32_t); void build_speaker(AAudioStreamBuilderStruct *, int32_t, int32_t); @@ -61,7 +64,9 @@ class audio wivrn_session & session; xr::instance & instance; + std::mutex mutex; std::atomic exiting = false; + std::thread recreate_thread; void exit();