From 4b95b79f27cda2560f014f7b15e81f7dcb71e461 Mon Sep 17 00:00:00 2001 From: Jacques Heunis Date: Sat, 4 Jun 2016 14:38:11 +0200 Subject: [PATCH 1/2] WASAPI: Correctly handle no input or output devices instead of crashing. --- src/wasapi.c | 60 +++++++++++++++++++++++++++++----------------------- src/wasapi.h | 2 ++ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/wasapi.c b/src/wasapi.c index 8cff4bf..a120aa9 100644 --- a/src/wasapi.c +++ b/src/wasapi.c @@ -666,40 +666,48 @@ static int refresh_devices(struct SoundIoPrivate *si) { if (FAILED(hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(siw->device_enumerator, eRender, eMultimedia, &rd.default_render_device))) { - deinit_refresh_devices(&rd); - return SoundIoErrorOpeningDevice; + if(hr != E_NOTFOUND) { + deinit_refresh_devices(&rd); + return SoundIoErrorOpeningDevice; + } } - if (rd.lpwstr) { - CoTaskMemFree(rd.lpwstr); - rd.lpwstr = NULL; - } - if (FAILED(hr = IMMDevice_GetId(rd.default_render_device, &rd.lpwstr))) { - deinit_refresh_devices(&rd); - return SoundIoErrorOpeningDevice; - } - if ((err = from_lpwstr(rd.lpwstr, &rd.default_render_id, &rd.default_render_id_len))) { - deinit_refresh_devices(&rd); - return err; + if(rd.default_render_device) { + if (rd.lpwstr) { + CoTaskMemFree(rd.lpwstr); + rd.lpwstr = NULL; + } + if (FAILED(hr = IMMDevice_GetId(rd.default_render_device, &rd.lpwstr))) { + deinit_refresh_devices(&rd); + return SoundIoErrorOpeningDevice; + } + if ((err = from_lpwstr(rd.lpwstr, &rd.default_render_id, &rd.default_render_id_len))) { + deinit_refresh_devices(&rd); + return err; + } } if (FAILED(hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(siw->device_enumerator, eCapture, eMultimedia, &rd.default_capture_device))) { - deinit_refresh_devices(&rd); - return SoundIoErrorOpeningDevice; + if(hr != E_NOTFOUND) { + deinit_refresh_devices(&rd); + return SoundIoErrorOpeningDevice; + } } - if (rd.lpwstr) { - CoTaskMemFree(rd.lpwstr); - rd.lpwstr = NULL; - } - if (FAILED(hr = IMMDevice_GetId(rd.default_capture_device, &rd.lpwstr))) { - deinit_refresh_devices(&rd); - return SoundIoErrorOpeningDevice; - } - if ((err = from_lpwstr(rd.lpwstr, &rd.default_capture_id, &rd.default_capture_id_len))) { - deinit_refresh_devices(&rd); - return err; + if(rd.default_capture_device) { + if (rd.lpwstr) { + CoTaskMemFree(rd.lpwstr); + rd.lpwstr = NULL; + } + if (FAILED(hr = IMMDevice_GetId(rd.default_capture_device, &rd.lpwstr))) { + deinit_refresh_devices(&rd); + return SoundIoErrorOpeningDevice; + } + if ((err = from_lpwstr(rd.lpwstr, &rd.default_capture_id, &rd.default_capture_id_len))) { + deinit_refresh_devices(&rd); + return err; + } } diff --git a/src/wasapi.h b/src/wasapi.h index 5860f86..968c401 100644 --- a/src/wasapi.h +++ b/src/wasapi.h @@ -24,6 +24,8 @@ #include #include +#define E_NOTFOUND 0x80070490 + struct SoundIoPrivate; int soundio_wasapi_init(struct SoundIoPrivate *si); From 17dc688ddccbcba49609e51cf987ff662fa3e335 Mon Sep 17 00:00:00 2001 From: Jacques Heunis Date: Sat, 4 Jun 2016 23:09:11 +0200 Subject: [PATCH 2/2] Move the E_NOTFOUND define out of wasapi.h --- src/wasapi.c | 3 +++ src/wasapi.h | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wasapi.c b/src/wasapi.c index a120aa9..84bc695 100644 --- a/src/wasapi.c +++ b/src/wasapi.c @@ -25,6 +25,9 @@ #include +// Some HRESULT values are not defined by the windows headers +#define E_NOTFOUND 0x80070490 + #ifdef __cplusplus // In C++ mode, IsEqualGUID() takes its arguments by reference #define IS_EQUAL_GUID(a, b) IsEqualGUID(*(a), *(b)) diff --git a/src/wasapi.h b/src/wasapi.h index 968c401..5860f86 100644 --- a/src/wasapi.h +++ b/src/wasapi.h @@ -24,8 +24,6 @@ #include #include -#define E_NOTFOUND 0x80070490 - struct SoundIoPrivate; int soundio_wasapi_init(struct SoundIoPrivate *si);