Merge pull request #79 from jacquesh/wasapi-device-error-handling

WASAPI: Correctly handle no input or output devices instead of crashing.
This commit is contained in:
Andrew Kelley 2016-06-04 14:20:45 -07:00
commit c2df644493

View file

@ -25,6 +25,9 @@
#include <stdio.h> #include <stdio.h>
// Some HRESULT values are not defined by the windows headers
#define E_NOTFOUND 0x80070490
#ifdef __cplusplus #ifdef __cplusplus
// In C++ mode, IsEqualGUID() takes its arguments by reference // In C++ mode, IsEqualGUID() takes its arguments by reference
#define IS_EQUAL_GUID(a, b) IsEqualGUID(*(a), *(b)) #define IS_EQUAL_GUID(a, b) IsEqualGUID(*(a), *(b))
@ -666,40 +669,48 @@ static int refresh_devices(struct SoundIoPrivate *si) {
if (FAILED(hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(siw->device_enumerator, eRender, if (FAILED(hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(siw->device_enumerator, eRender,
eMultimedia, &rd.default_render_device))) eMultimedia, &rd.default_render_device)))
{ {
deinit_refresh_devices(&rd); if(hr != E_NOTFOUND) {
return SoundIoErrorOpeningDevice; deinit_refresh_devices(&rd);
return SoundIoErrorOpeningDevice;
}
} }
if (rd.lpwstr) { if(rd.default_render_device) {
CoTaskMemFree(rd.lpwstr); if (rd.lpwstr) {
rd.lpwstr = NULL; CoTaskMemFree(rd.lpwstr);
} rd.lpwstr = NULL;
if (FAILED(hr = IMMDevice_GetId(rd.default_render_device, &rd.lpwstr))) { }
deinit_refresh_devices(&rd); if (FAILED(hr = IMMDevice_GetId(rd.default_render_device, &rd.lpwstr))) {
return SoundIoErrorOpeningDevice; 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); if ((err = from_lpwstr(rd.lpwstr, &rd.default_render_id, &rd.default_render_id_len))) {
return err; deinit_refresh_devices(&rd);
return err;
}
} }
if (FAILED(hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(siw->device_enumerator, eCapture, if (FAILED(hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(siw->device_enumerator, eCapture,
eMultimedia, &rd.default_capture_device))) eMultimedia, &rd.default_capture_device)))
{ {
deinit_refresh_devices(&rd); if(hr != E_NOTFOUND) {
return SoundIoErrorOpeningDevice; deinit_refresh_devices(&rd);
return SoundIoErrorOpeningDevice;
}
} }
if (rd.lpwstr) { if(rd.default_capture_device) {
CoTaskMemFree(rd.lpwstr); if (rd.lpwstr) {
rd.lpwstr = NULL; CoTaskMemFree(rd.lpwstr);
} rd.lpwstr = NULL;
if (FAILED(hr = IMMDevice_GetId(rd.default_capture_device, &rd.lpwstr))) { }
deinit_refresh_devices(&rd); if (FAILED(hr = IMMDevice_GetId(rd.default_capture_device, &rd.lpwstr))) {
return SoundIoErrorOpeningDevice; 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); if ((err = from_lpwstr(rd.lpwstr, &rd.default_capture_id, &rd.default_capture_id_len))) {
return err; deinit_refresh_devices(&rd);
return err;
}
} }