From cf4cc30da4566c1b8a86ee5688383f90eaf0e967 Mon Sep 17 00:00:00 2001 From: Val Doroshchuk Date: Tue, 10 Oct 2017 09:03:48 +0200 Subject: wasapi: Initialize COM before use The COM library was not initialized on the calling thread before the COM library calls were made. Task-number: QTBUG-62598 Change-Id: Id6f4fc093a1ef72b6e0a3cd3a22c05cec7eaafa8 Reviewed-by: Oliver Wolff --- src/plugins/wasapi/qwasapiutils.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/plugins/wasapi/qwasapiutils.cpp b/src/plugins/wasapi/qwasapiutils.cpp index 727c94c23..0d03982de 100644 --- a/src/plugins/wasapi/qwasapiutils.cpp +++ b/src/plugins/wasapi/qwasapiutils.cpp @@ -90,6 +90,26 @@ struct DeviceMapping { Q_GLOBAL_STATIC(DeviceMapping, gMapping) } +struct CoInitializer +{ + CoInitializer() + { + const bool isGuiThread = QCoreApplication::instance() && + QThread::currentThread() == QCoreApplication::instance()->thread(); + CoInitializeEx(NULL, isGuiThread ? COINIT_APARTMENTTHREADED : COINIT_MULTITHREADED); + } + + ~CoInitializer() + { + CoUninitialize(); + } +}; + +static void CoInitIfNeeded() +{ + static CoInitializer initializer; +} + AudioInterface::AudioInterface() { qCDebug(lcMmAudioInterface) << __FUNCTION__; @@ -182,6 +202,7 @@ QByteArray QWasapiUtils::defaultDevice(QAudio::Mode mode) { qCDebug(lcMmUtils) << __FUNCTION__ << mode; + CoInitIfNeeded(); QList &deviceNames = mode == QAudio::AudioInput ? gMapping->inputDeviceNames : gMapping->outputDeviceNames; QList &deviceIds = mode == QAudio::AudioInput ? gMapping->inputDeviceIds : gMapping->outputDeviceIds; if (deviceNames.isEmpty() || deviceIds.isEmpty()) // Initialize @@ -214,6 +235,7 @@ QList QWasapiUtils::availableDevices(QAudio::Mode mode) { qCDebug(lcMmUtils) << __FUNCTION__ << mode; + CoInitIfNeeded(); ComPtr statics; HRESULT hr; @@ -290,6 +312,7 @@ Microsoft::WRL::ComPtr QWasapiUtils::createOrGetInterface(const { qCDebug(lcMmUtils) << __FUNCTION__ << dev << mode; Q_ASSERT((mode == QAudio::AudioInput ? gMapping->inputDeviceNames.indexOf(dev) : gMapping->outputDeviceNames.indexOf(dev)) != -1); + CoInitIfNeeded(); Microsoft::WRL::ComPtr result; HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([dev, mode, &result]() { -- cgit v1.2.1