Merge pull request #97032 from lalitshankarchowdhury/fix-audio-crash

Fix `AudioStreamMicrophone` crash on scene-reload
This commit is contained in:
Rémi Verschelde 2024-09-18 11:15:44 +02:00
commit 21134e146a
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 17 additions and 11 deletions

View file

@ -39,6 +39,9 @@
#include <functiondiscoverykeys.h> #include <functiondiscoverykeys.h>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
// Define IAudioClient3 if not already defined by MinGW headers // Define IAudioClient3 if not already defined by MinGW headers
#if defined __MINGW32__ || defined __MINGW64__ #if defined __MINGW32__ || defined __MINGW64__
@ -107,6 +110,12 @@ const IID IID_IAudioClient3 = __uuidof(IAudioClient3);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient); const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
#define SAFE_RELEASE(memory) \
if ((memory) != nullptr) { \
(memory)->Release(); \
(memory) = nullptr; \
}
#define REFTIMES_PER_SEC 10000000 #define REFTIMES_PER_SEC 10000000
#define REFTIMES_PER_MILLISEC 10000 #define REFTIMES_PER_MILLISEC 10000
@ -302,7 +311,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_i
audioProps.bIsOffload = FALSE; audioProps.bIsOffload = FALSE;
audioProps.eCategory = AudioCategory_GameEffects; audioProps.eCategory = AudioCategory_GameEffects;
hr = ((IAudioClient3 *)p_device->audio_client.Get())->SetClientProperties(&audioProps); hr = ((IAudioClient3 *)p_device->audio_client)->SetClientProperties(&audioProps);
ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: SetClientProperties failed with error 0x" + String::num_uint64(hr, 16) + "."); ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: SetClientProperties failed with error 0x" + String::num_uint64(hr, 16) + ".");
} }
@ -385,7 +394,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_i
} }
} else { } else {
IAudioClient3 *device_audio_client_3 = (IAudioClient3 *)p_device->audio_client.Get(); IAudioClient3 *device_audio_client_3 = (IAudioClient3 *)p_device->audio_client;
// AUDCLNT_STREAMFLAGS_RATEADJUST is an invalid flag with IAudioClient3, therefore we have to use // AUDCLNT_STREAMFLAGS_RATEADJUST is an invalid flag with IAudioClient3, therefore we have to use
// the closest supported mix rate supported by the audio driver. // the closest supported mix rate supported by the audio driver.
@ -516,9 +525,9 @@ Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) {
p_device->active.clear(); p_device->active.clear();
} }
p_device->audio_client.Reset(); SAFE_RELEASE(p_device->audio_client)
p_device->render_client.Reset(); SAFE_RELEASE(p_device->render_client)
p_device->capture_client.Reset(); SAFE_RELEASE(p_device->capture_client)
return OK; return OK;
} }

View file

@ -40,18 +40,15 @@
#include <audioclient.h> #include <audioclient.h>
#include <mmdeviceapi.h> #include <mmdeviceapi.h>
#include <wrl/client.h>
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
using Microsoft::WRL::ComPtr;
class AudioDriverWASAPI : public AudioDriver { class AudioDriverWASAPI : public AudioDriver {
class AudioDeviceWASAPI { class AudioDeviceWASAPI {
public: public:
ComPtr<IAudioClient> audio_client = nullptr; IAudioClient *audio_client = nullptr;
ComPtr<IAudioRenderClient> render_client = nullptr; // Output IAudioRenderClient *render_client = nullptr; // Output
ComPtr<IAudioCaptureClient> capture_client = nullptr; // Input IAudioCaptureClient *capture_client = nullptr; // Input
SafeFlag active; SafeFlag active;
WORD format_tag = 0; WORD format_tag = 0;