Commit d40a385e authored by Your Name's avatar Your Name Committed by David Reid

Make the COM-related code more reasonable

parent 7df2a50d
...@@ -21605,11 +21605,14 @@ static ma_result ma_context_get_MMDevice__wasapi(ma_context* pContext, ma_device ...@@ -21605,11 +21605,14 @@ static ma_result ma_context_get_MMDevice__wasapi(ma_context* pContext, ma_device
over to a single thread to make everything safer, but in the meantime while we wait for that to come online I'm over to a single thread to make everything safer, but in the meantime while we wait for that to come online I'm
happy enough to use this hack instead. happy enough to use this hack instead.
*/ */
ma_CoInitializeEx(pContext, NULL, MA_COINIT_VALUE); /* If ma_CoInitializeEx returns an error (such as 0x80010106 (RPC_E_CHANGED_MODE)), it may indicate that COM has already been initialized outside of miniaudio with a different thread concurrency model.
In this case, there is no need to call ma_CoUninitialize.*/
HRESULT CoInitializeResult = ma_CoInitializeEx(pContext, NULL, MA_COINIT_VALUE);
{ {
hr = ma_CoCreateInstance(pContext, &MA_CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &MA_IID_IMMDeviceEnumerator, (void**)&pDeviceEnumerator); hr = ma_CoCreateInstance(pContext, &MA_CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &MA_IID_IMMDeviceEnumerator, (void**)&pDeviceEnumerator);
} }
ma_CoUninitialize(pContext); if (CoInitializeResult == S_OK || CoInitializeResult == S_FALSE)
ma_CoUninitialize(pContext);
if (FAILED(hr)) { /* <-- This is checking the call above to ma_CoCreateInstance(). */ if (FAILED(hr)) { /* <-- This is checking the call above to ma_CoCreateInstance(). */
ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_ERROR, "[WASAPI] Failed to create IMMDeviceEnumerator.\n"); ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_ERROR, "[WASAPI] Failed to create IMMDeviceEnumerator.\n");
...@@ -41355,7 +41358,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData) ...@@ -41355,7 +41358,7 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData)
} }
#ifdef MA_WIN32 #ifdef MA_WIN32
if (CoInitializeResult == S_OK) { if (CoInitializeResult == S_OK || CoInitializeResult == S_FALSE) {
ma_CoUninitialize(pDevice->pContext); ma_CoUninitialize(pDevice->pContext);
} }
#endif #endif
...@@ -41380,7 +41383,7 @@ static ma_result ma_context_uninit_backend_apis__win32(ma_context* pContext) ...@@ -41380,7 +41383,7 @@ static ma_result ma_context_uninit_backend_apis__win32(ma_context* pContext)
{ {
/* For some reason UWP complains when CoUninitialize() is called. I'm just not going to call it on UWP. */ /* For some reason UWP complains when CoUninitialize() is called. I'm just not going to call it on UWP. */
#if defined(MA_WIN32_DESKTOP) || defined(MA_WIN32_GDK) #if defined(MA_WIN32_DESKTOP) || defined(MA_WIN32_GDK)
if (pContext->win32.CoInitializeResult == S_OK) { if (pContext->win32.CoInitializeResult == S_OK || pContext->win32.CoInitializeResult == S_FALSE) {
ma_CoUninitialize(pContext); ma_CoUninitialize(pContext);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment