diff options
author | Val Doroshchuk <valentyn.doroshchuk@qt.io> | 2017-10-10 09:03:48 +0200 |
---|---|---|
committer | VaL Doroshchuk <valentyn.doroshchuk@qt.io> | 2017-11-14 08:23:45 +0000 |
commit | cf4cc30da4566c1b8a86ee5688383f90eaf0e967 (patch) | |
tree | c2227d76c7ffa0b2a497f4466da2454821f839e2 /src | |
parent | 4a214b199337a02e8cab77aa0cdca0c2805fb6a2 (diff) | |
download | qtmultimedia-cf4cc30da4566c1b8a86ee5688383f90eaf0e967.tar.gz |
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 <oliver.wolff@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/wasapi/qwasapiutils.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
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<QByteArray> &deviceNames = mode == QAudio::AudioInput ? gMapping->inputDeviceNames : gMapping->outputDeviceNames; QList<QString> &deviceIds = mode == QAudio::AudioInput ? gMapping->inputDeviceIds : gMapping->outputDeviceIds; if (deviceNames.isEmpty() || deviceIds.isEmpty()) // Initialize @@ -214,6 +235,7 @@ QList<QByteArray> QWasapiUtils::availableDevices(QAudio::Mode mode) { qCDebug(lcMmUtils) << __FUNCTION__ << mode; + CoInitIfNeeded(); ComPtr<IDeviceInformationStatics> statics; HRESULT hr; @@ -290,6 +312,7 @@ Microsoft::WRL::ComPtr<AudioInterface> 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<AudioInterface> result; HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([dev, mode, &result]() { |