summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorMarcelo Fernandez <marcelofg55@gmail.com>2017-10-31 13:07:35 -0300
committerMarcelo Fernandez <marcelofg55@gmail.com>2017-10-31 13:07:35 -0300
commit16327bff8a5924c4475c20c61485ffc589efb2e5 (patch)
treef799dff6a4f4fc1e0a20acb505518dee86732969 /drivers
parent157fa55e34b9bf21a22876aecef89ed0a9491412 (diff)
Improved WASAPI driver logic when devices are connected or disconnected
Diffstat (limited to 'drivers')
-rw-r--r--drivers/wasapi/audio_driver_wasapi.cpp53
-rw-r--r--drivers/wasapi/audio_driver_wasapi.h2
2 files changed, 40 insertions, 15 deletions
diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp
index eb86491dec..0671ee408e 100644
--- a/drivers/wasapi/audio_driver_wasapi.cpp
+++ b/drivers/wasapi/audio_driver_wasapi.cpp
@@ -39,7 +39,7 @@ const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
-Error AudioDriverWASAPI::init_device() {
+Error AudioDriverWASAPI::init_device(bool reinit) {
WAVEFORMATEX *pwfex;
IMMDeviceEnumerator *enumerator = NULL;
@@ -51,10 +51,24 @@ Error AudioDriverWASAPI::init_device() {
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
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);
- 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);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
@@ -152,7 +166,9 @@ Error AudioDriverWASAPI::finish_device() {
Error AudioDriverWASAPI::init() {
Error err = init_device();
- ERR_FAIL_COND_V(err != OK, err);
+ if (err != OK) {
+ ERR_PRINT("WASAPI: init_device error");
+ }
active = false;
exit_thread = false;
@@ -209,7 +225,7 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
unsigned int left_frames = ad->buffer_frames;
unsigned int buffer_idx = 0;
- while (left_frames > 0) {
+ while (left_frames > 0 && ad->audio_client) {
WaitForSingleObject(ad->event, 1000);
UINT32 cur_frames;
@@ -271,9 +287,9 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
} else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
// Device is not valid anymore, reopen it
- Error err = ad->reopen();
+ Error err = ad->finish_device();
if (err != OK) {
- ad->exit_thread = true;
+ ERR_PRINT("WASAPI: finish_device error");
} else {
// We reopened the device and samples_in may have resized, so invalidate the current left_frames
left_frames = 0;
@@ -285,9 +301,9 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
} else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
// Device is not valid anymore, reopen it
- Error err = ad->reopen();
+ Error err = ad->finish_device();
if (err != OK) {
- ad->exit_thread = true;
+ ERR_PRINT("WASAPI: finish_device error");
} else {
// We reopened the device and samples_in may have resized, so invalidate the current left_frames
left_frames = 0;
@@ -296,6 +312,13 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
ERR_PRINT("WASAPI: GetCurrentPadding error");
}
}
+
+ if (!ad->audio_client) {
+ Error err = ad->init_device(true);
+ if (err == OK) {
+ ad->start();
+ }
+ }
}
ad->thread_exited = true;
@@ -303,11 +326,13 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
void AudioDriverWASAPI::start() {
- HRESULT hr = audio_client->Start();
- if (hr != S_OK) {
- ERR_PRINT("WASAPI: Start failed");
- } else {
- active = true;
+ if (audio_client) {
+ HRESULT hr = audio_client->Start();
+ if (hr != S_OK) {
+ ERR_PRINT("WASAPI: Start failed");
+ } else {
+ active = true;
+ }
}
}
diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h
index fab8ab3250..87a2db724c 100644
--- a/drivers/wasapi/audio_driver_wasapi.h
+++ b/drivers/wasapi/audio_driver_wasapi.h
@@ -64,7 +64,7 @@ class AudioDriverWASAPI : public AudioDriver {
static void thread_func(void *p_udata);
- Error init_device();
+ Error init_device(bool reinit = false);
Error finish_device();
Error reopen();