WASAPI backend function ma_device_uninit() deadlock
During periodic unplugging of headphones, I call the device stop function ma_device_stop() and ma_device_uninit(), then re-initialize and start the available audio device (speaker or headphones). The program often deadlock on the ma_device_uninit() function. Previously, I could call these functions from different threads, but then I changed it and call from one thread. But the deadlock remains. Tell me how I can get rid of the application deadlock?
I found in which case the ma_device_uninit() function is blocked by placing logs. Then, I couldn't think of anything better than commenting out some of the calls in miniaudio to eliminate blocking. I attach the diff file
`igor ~/projects/cmp/src/lib/miniaudio $ git diff 95fd952b miniaudio.h diff --git a/src/lib/miniaudio/miniaudio.h b/src/lib/miniaudio/miniaudio.h index 19146ac2..97cf34ff 100644 --- a/src/lib/miniaudio/miniaudio.h +++ b/src/lib/miniaudio/miniaudio.h @@ -22087,7 +22087,9 @@ static ma_result ma_device_uninit__wasapi(ma_device* pDevice) #if defined(MA_WIN32_DESKTOP) || defined(MA_WIN32_GDK) { if (pDevice->wasapi.pDeviceEnumerator) { +#ifndef MA_NO_WASAPI_NOTIFICATION ((ma_IMMDeviceEnumerator*)pDevice->wasapi.pDeviceEnumerator)->lpVtbl->UnregisterEndpointNotificationCallback((ma_IMMDeviceEnumerator*)pDevice->wasapi.pDeviceEnumerator, &pDevice->wasapi.notificationClient); +#endif ma_IMMDeviceEnumerator_Release((ma_IMMDeviceEnumerator*)pDevice->wasapi.pDeviceEnumerator); }
@@ -22980,11 +22982,13 @@ static ma_result ma_device_init__wasapi(ma_device* pDevice, const ma_device_conf return ma_result_from_HRESULT(hr); }
+#ifndef MA_NO_WASAPI_NOTIFICATION pDevice->wasapi.notificationClient.lpVtbl = (void*)&g_maNotificationCientVtbl; pDevice->wasapi.notificationClient.counter = 1; pDevice->wasapi.notificationClient.pDevice = pDevice;
hr = pDeviceEnumerator->lpVtbl->RegisterEndpointNotificationCallback(pDeviceEnumerator, &pDevice->wasapi.notificationClient);
+#endif if (SUCCEEDED(hr)) { pDevice->wasapi.pDeviceEnumerator = (ma_ptr)pDeviceEnumerator; } else { `
hangs on this function:
((ma_IMMDeviceEnumerator*)pDevice->wasapi.pDeviceEnumerator)->lpVtbl->UnregisterEndpointNotificationCallback((ma_IMMDeviceEnumerator*)pDevice->wasapi.pDeviceEnumerator, &pDevice->wasapi.notificationClient);
Thanks for the report. If you haven't already, try the dev branch. I don't think it'll help, but maybe worth a shot. But otherwise, I'm going to sit on this because I'm currently in the process of doing a big refactor of the backend system, and WASAPI is one of the backends I'm planning on updating. The handling of device rerouting is one of those things that will be effected, so hopefully this will get fixed naturally with those changes. Basically, I'm making it so all WASAPI calls are made from a single thread, including rerouting, which I think should improve overall stability of the WASAPI backend. That'll only be in version 0.12 however (miniaudio is currently 0.11.x).
Tried the sources from the dev branch, the situation with freezing repeated. Waiting for fixes in version 0.12. Thanks. For now I'll sit on my patch