From 4bb9bbad3c558280c474f754bb1218f7f036ef5b Mon Sep 17 00:00:00 2001 From: casey langen Date: Mon, 27 Dec 2021 22:41:57 -0800 Subject: [PATCH] Fix race condition by adding required locking in WasapiOut to avoid rare crash. --- CHANGELOG.txt | 4 ++++ src/plugins/wasapiout/WasapiOut.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 624a5818c..0bc1e935d 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,7 @@ +0.96.10 + +* fixed race condition in WasapiOut that caused the app to crash. + 0.96.9 * fixed `-rpath` to use `$ORIGIN` instead of `./` to appease `Feodra 35`. diff --git a/src/plugins/wasapiout/WasapiOut.cpp b/src/plugins/wasapiout/WasapiOut.cpp index d4c9b7914..a308d172b 100644 --- a/src/plugins/wasapiout/WasapiOut.cpp +++ b/src/plugins/wasapiout/WasapiOut.cpp @@ -359,6 +359,8 @@ OutputState WasapiOut::Play(IBuffer *buffer, IBufferProvider *provider) { } void WasapiOut::Reset() { + Lock lock(this->stateMutex); + if (this->simpleAudioVolume) { this->simpleAudioVolume->Release(); } @@ -516,6 +518,7 @@ found_or_done: int WasapiOut::GetDefaultSampleRate() { int result = -1; + Lock lock(this->stateMutex); this->InitializeAudioClient(); if (this->audioClient) { WAVEFORMATEX* deviceFormat = nullptr; @@ -529,6 +532,8 @@ int WasapiOut::GetDefaultSampleRate() { } bool WasapiOut::InitializeAudioClient() { + /* assumes stateMutex is locked */ + CoInitializeEx(nullptr, COINIT_MULTITHREADED); HRESULT result = S_FALSE; @@ -584,6 +589,7 @@ bool WasapiOut::InitializeAudioClient() { } bool WasapiOut::Configure(IBuffer *buffer) { + /* assumes stateMutex is locked */ if (this->audioClient && waveFormat.Format.nChannels == buffer->Channels() && waveFormat.Format.nSamplesPerSec == buffer->SampleRate())