From 4bd8eb0aa0bb2b16010eff76d13df2b38c124f3a Mon Sep 17 00:00:00 2001 From: David Reid Date: Wed, 7 Jan 2026 10:23:39 +1000 Subject: [PATCH] WASAPI: Handle AUDCLNT_E_DEVICE_INVALIDATED. When this is detected the device will be reinitialized. If this fails the device will be put into an errored state and will become unusable. --- miniaudio.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/miniaudio.h b/miniaudio.h index 3e7198f1..d8756986 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -24858,6 +24858,17 @@ static ma_result ma_device_step__wasapi(ma_device* pDevice, ma_blocking_mode blo } else { if (hr == MA_AUDCLNT_S_BUFFER_EMPTY || hr == MA_AUDCLNT_E_BUFFER_ERROR) { /* No data available. */ + } else if (hr == MA_AUDCLNT_E_DEVICE_INVALIDATED) { + ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_WARNING, "[WASAPI] Capture device invalidated. Attempting reinitialization."); + + result = ma_device_reroute__wasapi(pDevice, ma_device_type_capture); + if (result != MA_SUCCESS) { + ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_ERROR, "[WASAPI] Capture device invalidated and reinitialization failed. Stopping device."); + ma_device_set_errored(pDevice); + return result; + } + + ma_device_start__wasapi(pDevice); } else { /* An error occurred and we need to abort. */ ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_ERROR, "[WASAPI] Failed to retrieve internal buffer from capture device in preparation for reading from the device. HRESULT = %d. Stopping device.", (int)hr); @@ -24886,6 +24897,17 @@ static ma_result ma_device_step__wasapi(ma_device* pDevice, ma_blocking_mode blo } else { if (hr == MA_AUDCLNT_E_BUFFER_TOO_LARGE || hr == MA_AUDCLNT_E_BUFFER_ERROR) { /* Not enough data available. Nothing to do. */ + } else if (hr == MA_AUDCLNT_E_DEVICE_INVALIDATED) { + ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_WARNING, "[WASAPI] Playback device invalidated. Attempting reinitialization."); + + result = ma_device_reroute__wasapi(pDevice, ma_device_type_playback); + if (result != MA_SUCCESS) { + ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_ERROR, "[WASAPI] Playback device invalidated and reinitialization failed. Stopping device."); + ma_device_set_errored(pDevice); + return result; + } + + ma_device_start__wasapi(pDevice); } else { /* Some error occurred. We'll need to abort. */ ma_log_postf(ma_device_get_log(pDevice), MA_LOG_LEVEL_ERROR, "[WASAPI] Failed to retrieve internal buffer from playback device in preparation for writing to the device. HRESULT = %d. Stopping device.", (int)hr);