From 3e93c392d77938b1b5e4a0345aad45ea01366551 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 10 Mar 2021 17:21:26 +0100 Subject: [PATCH] output/wasapi: make `enumerator` a local variable --- .../plugins/wasapi/WasapiOutputPlugin.cxx | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/output/plugins/wasapi/WasapiOutputPlugin.cxx b/src/output/plugins/wasapi/WasapiOutputPlugin.cxx index eed47591b..5d1e546b9 100644 --- a/src/output/plugins/wasapi/WasapiOutputPlugin.cxx +++ b/src/output/plugins/wasapi/WasapiOutputPlugin.cxx @@ -233,7 +233,6 @@ class WasapiOutput final : public AudioOutput { #endif std::string device_config; std::shared_ptr com_worker; - ComPtr enumerator; ComPtr device; ComPtr client; WAVEFORMATEXTENSIBLE device_format; @@ -293,9 +292,11 @@ private: bool TryFormatExclusive(const AudioFormat &audio_format); void FindExclusiveFormatSupported(AudioFormat &audio_format); void FindSharedFormatSupported(AudioFormat &audio_format); - void EnumerateDevices(); - ComPtr GetDevice(unsigned int index); - ComPtr SearchDevice(std::string_view name); + static void EnumerateDevices(IMMDeviceEnumerator &enumerator); + static ComPtr GetDevice(IMMDeviceEnumerator &enumerator, + unsigned index); + static ComPtr SearchDevice(IMMDeviceEnumerator &enumerator, + std::string_view name); }; WasapiOutput & @@ -434,7 +435,6 @@ WasapiOutput::DoDisable() noexcept assert(!thread); device.reset(); - enumerator.reset(); } /// run inside COMWorkerThread @@ -695,12 +695,13 @@ WasapiOutput::Cancel() noexcept void WasapiOutput::OpenDevice() { + ComPtr enumerator; enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER); if (enumerate_devices) { try { - EnumerateDevices(); + EnumerateDevices(*enumerator); } catch (...) { LogError(std::current_exception()); } @@ -709,12 +710,12 @@ WasapiOutput::OpenDevice() if (!device_config.empty()) { unsigned int id; if (!SafeSilenceTry([this, &id]() { id = std::stoul(device_config); })) { - device = SearchDevice(device_config); + device = SearchDevice(*enumerator, device_config); if (!device) throw FormatRuntimeError("Device '%s' not found", device_config.c_str()); } else - device = GetDevice(id); + device = GetDevice(*enumerator, id); } else { device = GetDefaultAudioEndpoint(*enumerator); } @@ -915,9 +916,9 @@ WasapiOutput::FindSharedFormatSupported(AudioFormat &audio_format) /// run inside COMWorkerThread void -WasapiOutput::EnumerateDevices() +WasapiOutput::EnumerateDevices(IMMDeviceEnumerator &enumerator) { - const auto device_collection = EnumAudioEndpoints(*enumerator); + const auto device_collection = EnumAudioEndpoints(enumerator); const UINT count = GetCount(*device_collection); for (UINT i = 0; i < count; ++i) { @@ -938,17 +939,18 @@ WasapiOutput::EnumerateDevices() /// run inside COMWorkerThread ComPtr -WasapiOutput::GetDevice(unsigned int index) +WasapiOutput::GetDevice(IMMDeviceEnumerator &enumerator, unsigned index) { - const auto device_collection = EnumAudioEndpoints(*enumerator); + const auto device_collection = EnumAudioEndpoints(enumerator); return Item(*device_collection, index); } /// run inside COMWorkerThread ComPtr -WasapiOutput::SearchDevice(std::string_view name) +WasapiOutput::SearchDevice(IMMDeviceEnumerator &enumerator, + std::string_view name) { - const auto device_collection = EnumAudioEndpoints(*enumerator); + const auto device_collection = EnumAudioEndpoints(enumerator); const UINT count = GetCount(*device_collection); for (UINT i = 0; i < count; ++i) {