WASAPI: Fix device enumeration.

This commit is contained in:
David Reid
2026-01-12 06:37:14 +10:00
parent 6a3d5fde05
commit 4d583a4508
+46 -9
View File
@@ -23475,8 +23475,22 @@ static ma_device_enumeration_result ma_context_enumerate_device_from_type_UWP__w
} }
#endif #endif
static ma_result ma_context_enumerate_devices__wasapi(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData) typedef struct
{ {
ma_context* pContext;
ma_enum_devices_callback_proc callback;
void* pUserData;
ma_result result;
} ma_context_enumerate_devices_thread_data__wasapi;
static ma_thread_result MA_THREADCALL ma_context_enumerate_devices_thread__wasapi(void* pUserData)
{
ma_context_enumerate_devices_thread_data__wasapi* pData = (ma_context_enumerate_devices_thread_data__wasapi*)pUserData;
pData->result = MA_SUCCESS;
ma_CoInitializeEx(pData->pContext, NULL, MA_COINIT_VALUE);
{
/* Different enumeration for desktop and UWP. */ /* Different enumeration for desktop and UWP. */
#if defined(MA_WIN32_DESKTOP) || defined(MA_WIN32_GDK) #if defined(MA_WIN32_DESKTOP) || defined(MA_WIN32_GDK)
{ {
@@ -23484,14 +23498,15 @@ static ma_result ma_context_enumerate_devices__wasapi(ma_context* pContext, ma_e
HRESULT hr; HRESULT hr;
ma_IMMDeviceEnumerator* pDeviceEnumerator; ma_IMMDeviceEnumerator* pDeviceEnumerator;
hr = ma_CoCreateInstance(pContext, &MA_CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &MA_IID_IMMDeviceEnumerator, (void**)&pDeviceEnumerator); hr = ma_CoCreateInstance(pData->pContext, &MA_CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &MA_IID_IMMDeviceEnumerator, (void**)&pDeviceEnumerator);
if (FAILED(hr)) { if (FAILED(hr)) {
ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_ERROR, "[WASAPI] Failed to create device enumerator."); ma_log_postf(ma_context_get_log(pData->pContext), MA_LOG_LEVEL_ERROR, "[WASAPI] Failed to create device enumerator.");
return ma_result_from_HRESULT(hr); pData->result = ma_result_from_HRESULT(hr);
return 1;
} }
ma_context_enumerate_devices_by_type__wasapi(pContext, pDeviceEnumerator, ma_device_type_playback, callback, pUserData); ma_context_enumerate_devices_by_type__wasapi(pData->pContext, pDeviceEnumerator, ma_device_type_playback, pData->callback, pData->pUserData);
ma_context_enumerate_devices_by_type__wasapi(pContext, pDeviceEnumerator, ma_device_type_capture, callback, pUserData); ma_context_enumerate_devices_by_type__wasapi(pData->pContext, pDeviceEnumerator, ma_device_type_capture, pData->callback, pData->pUserData);
ma_IMMDeviceEnumerator_Release(pDeviceEnumerator); ma_IMMDeviceEnumerator_Release(pDeviceEnumerator);
} }
@@ -23510,20 +23525,42 @@ static ma_result ma_context_enumerate_devices__wasapi(ma_context* pContext, ma_e
/* Playback. */ /* Playback. */
if (cbResult == MA_DEVICE_ENUMERATION_CONTINUE) { if (cbResult == MA_DEVICE_ENUMERATION_CONTINUE) {
cbResult = ma_context_enumerate_device_from_type_UWP__wasapi(pContext, ma_device_type_playback, callback, pUserData); cbResult = ma_context_enumerate_device_from_type_UWP__wasapi(pData->pContext, ma_device_type_playback, pData->callback, pData->pUserData);
} }
/* Capture. */ /* Capture. */
if (cbResult == MA_DEVICE_ENUMERATION_CONTINUE) { if (cbResult == MA_DEVICE_ENUMERATION_CONTINUE) {
cbResult = ma_context_enumerate_device_from_type_UWP__wasapi(pContext, ma_device_type_capture, callback, pUserData); cbResult = ma_context_enumerate_device_from_type_UWP__wasapi(pData->pContext, ma_device_type_capture, pData->callback, pData->pUserData);
} }
(void)cbResult; (void)cbResult;
} }
} }
#endif #endif
}
ma_CoUninitialize(pData->pContext);
return MA_SUCCESS; return 0;
}
static ma_result ma_context_enumerate_devices__wasapi(ma_context* pContext, ma_enum_devices_callback_proc callback, void* pUserData)
{
ma_result result;
ma_thread thread;
ma_context_enumerate_devices_thread_data__wasapi data;
data.pContext = pContext;
data.callback = callback;
data.pUserData = pUserData;
result = ma_thread_create(&thread, ma_thread_priority_default, 0, ma_context_enumerate_devices_thread__wasapi, &data, ma_context_get_allocation_callbacks(pContext));
if (result != MA_SUCCESS) {
return result;
}
ma_thread_wait(&thread);
return data.result;
} }