Improved WASAPI driver logic when devices are connected or disconnected

This commit is contained in:
Marcelo Fernandez 2017-11-01 10:35:58 -03:00
parent 290b32ee57
commit 9dfdddd827
2 changed files with 41 additions and 16 deletions

View file

@ -39,7 +39,7 @@ const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient); const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
Error AudioDriverWASAPI::init_device() { Error AudioDriverWASAPI::init_device(bool reinit) {
WAVEFORMATEX *pwfex; WAVEFORMATEX *pwfex;
IMMDeviceEnumerator *enumerator = NULL; IMMDeviceEnumerator *enumerator = NULL;
@ -51,10 +51,24 @@ Error AudioDriverWASAPI::init_device() {
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device); hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); if (reinit) {
// In case we're trying to re-initialize the device prevent throwing this error on the console,
// otherwise if there is currently no devie available this will spam the console.
if (hr != S_OK) {
return ERR_CANT_OPEN;
}
} else {
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
}
hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&audio_client); hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&audio_client);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); if (reinit) {
if (hr != S_OK) {
return ERR_CANT_OPEN;
}
} else {
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
}
hr = audio_client->GetMixFormat(&pwfex); hr = audio_client->GetMixFormat(&pwfex);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
@ -150,7 +164,9 @@ Error AudioDriverWASAPI::finish_device() {
Error AudioDriverWASAPI::init() { Error AudioDriverWASAPI::init() {
Error err = init_device(); Error err = init_device();
ERR_FAIL_COND_V(err != OK, err); if (err != OK) {
ERR_PRINT("WASAPI: init_device error");
}
active = false; active = false;
exit_thread = false; exit_thread = false;
@ -207,7 +223,7 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
unsigned int left_frames = ad->buffer_frames; unsigned int left_frames = ad->buffer_frames;
unsigned int buffer_idx = 0; unsigned int buffer_idx = 0;
while (left_frames > 0) { while (left_frames > 0 && ad->audio_client) {
WaitForSingleObject(ad->event, 1000); WaitForSingleObject(ad->event, 1000);
UINT32 cur_frames; UINT32 cur_frames;
@ -269,9 +285,9 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
} else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) { } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
// Device is not valid anymore, reopen it // Device is not valid anymore, reopen it
Error err = ad->reopen(); Error err = ad->finish_device();
if (err != OK) { if (err != OK) {
ad->exit_thread = true; ERR_PRINT("WASAPI: finish_device error");
} else { } else {
// We reopened the device and samples_in may have resized, so invalidate the current left_frames // We reopened the device and samples_in may have resized, so invalidate the current left_frames
left_frames = 0; left_frames = 0;
@ -283,9 +299,9 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
} else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) { } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
// Device is not valid anymore, reopen it // Device is not valid anymore, reopen it
Error err = ad->reopen(); Error err = ad->finish_device();
if (err != OK) { if (err != OK) {
ad->exit_thread = true; ERR_PRINT("WASAPI: finish_device error");
} else { } else {
// We reopened the device and samples_in may have resized, so invalidate the current left_frames // We reopened the device and samples_in may have resized, so invalidate the current left_frames
left_frames = 0; left_frames = 0;
@ -294,6 +310,13 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
ERR_PRINT("WASAPI: GetCurrentPadding error"); ERR_PRINT("WASAPI: GetCurrentPadding error");
} }
} }
if (!ad->audio_client) {
Error err = ad->init_device(true);
if (err == OK) {
ad->start();
}
}
} }
ad->thread_exited = true; ad->thread_exited = true;
@ -301,11 +324,13 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
void AudioDriverWASAPI::start() { void AudioDriverWASAPI::start() {
HRESULT hr = audio_client->Start(); if (audio_client) {
if (hr != S_OK) { HRESULT hr = audio_client->Start();
ERR_PRINT("WASAPI: Start failed"); if (hr != S_OK) {
} else { ERR_PRINT("WASAPI: Start failed");
active = true; } else {
active = true;
}
} }
} }
@ -355,7 +380,7 @@ AudioDriverWASAPI::AudioDriverWASAPI() {
buffer_size = 0; buffer_size = 0;
channels = 0; channels = 0;
mix_rate = 0; mix_rate = 44100;
buffer_frames = 0; buffer_frames = 0;
thread_exited = false; thread_exited = false;

View file

@ -62,7 +62,7 @@ class AudioDriverWASAPI : public AudioDriverSW {
static void thread_func(void *p_udata); static void thread_func(void *p_udata);
Error init_device(); Error init_device(bool reinit = false);
Error finish_device(); Error finish_device();
Error reopen(); Error reopen();