From 91982ecc7bc35bdf565511a4245a3276109e0dea Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Tue, 11 Feb 2020 14:15:31 +0100 Subject: [PATCH] Some updates --- .../serverconnection/src/audio/driver/AudioDriver.h | 2 +- native/serverconnection/src/audio/driver/SoundIO.h | 1 + .../src/audio/driver/SoundIORecord.cpp | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/native/serverconnection/src/audio/driver/AudioDriver.h b/native/serverconnection/src/audio/driver/AudioDriver.h index b0544f1..23b27cd 100644 --- a/native/serverconnection/src/audio/driver/AudioDriver.h +++ b/native/serverconnection/src/audio/driver/AudioDriver.h @@ -31,7 +31,7 @@ namespace tc::audio { virtual bool impl_start(std::string& /* error */) = 0; virtual void impl_stop() = 0; - std::mutex state_lock{}; + std::timed_mutex state_lock{}; bool running{false}; bool stream_invalid{false}; diff --git a/native/serverconnection/src/audio/driver/SoundIO.h b/native/serverconnection/src/audio/driver/SoundIO.h index 9176066..401a90d 100644 --- a/native/serverconnection/src/audio/driver/SoundIO.h +++ b/native/serverconnection/src/audio/driver/SoundIO.h @@ -90,6 +90,7 @@ namespace tc::audio { struct ::SoundIoRingBuffer* buffer{nullptr}; + bool stop_requested{false}; /* protected via fail_cv_mutex */ void execute_recovery(); void read_callback(int frame_count_min, int frame_count_max); }; diff --git a/native/serverconnection/src/audio/driver/SoundIORecord.cpp b/native/serverconnection/src/audio/driver/SoundIORecord.cpp index 8343dbc..7022045 100644 --- a/native/serverconnection/src/audio/driver/SoundIORecord.cpp +++ b/native/serverconnection/src/audio/driver/SoundIORecord.cpp @@ -52,9 +52,14 @@ void SoundIORecord::execute_recovery() { if(fc == 0) fc = 1; else if(fc > 10) return; this->fail_cv.wait_for(cv_lock, std::chrono::seconds{(fc - 1) * 10}); + if(!this->running || this->stop_requested) return; } - std::lock_guard slock{this->state_lock}; + std::unique_lock slock{this->state_lock, std::defer_lock}; + if(!slock.try_lock_for(std::chrono::milliseconds{20})) { + log_info(category::audio, tr("Failed lock state mutex. Exit input recover thread.")); + return; + } if(!this->running) return; std::string error{}; @@ -131,6 +136,7 @@ bool SoundIORecord::impl_start(std::string &error) { //TODO: Test for interleaved channel layout! + this->stop_requested = false; return true; error_cleanup: @@ -140,6 +146,7 @@ bool SoundIORecord::impl_start(std::string &error) { if(this->buffer) soundio_ring_buffer_destroy(this->buffer); this->buffer = nullptr; + return false; } @@ -147,6 +154,10 @@ void SoundIORecord::impl_stop() { if(!this->stream) return; if(this->fail_recover_thread.joinable() && std::this_thread::get_id() != this->fail_recover_thread.get_id()) { + { + std::lock_guard flock{this->fail_cv_mutex}; + this->stop_requested = true; + } this->fail_cv.notify_all(); this->fail_recover_thread.join(); }