From d92e2eff3a133dc5f1077ebff0f33a9d7195df41 Mon Sep 17 00:00:00 2001 From: Sepalani Date: Sun, 25 Aug 2024 00:06:50 +0400 Subject: [PATCH] CubebUtils: Add COM helper class --- Source/Core/AudioCommon/CubebUtils.cpp | 73 ++++++++++++++- Source/Core/AudioCommon/CubebUtils.h | 23 +++++ .../Core/Core/IOS/USB/Emulated/Microphone.cpp | 89 +++---------------- .../Core/Core/IOS/USB/Emulated/Microphone.h | 9 +- 4 files changed, 106 insertions(+), 88 deletions(-) diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index 1535e2c83969..9910234a8057 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -16,6 +16,13 @@ #include +#ifdef _WIN32 +#include + +#include "Common/Event.h" +#include "Common/ScopeGuard.h" +#endif + static ptrdiff_t s_path_cutoff_point = 0; static void LogCallback(const char* format, ...) @@ -49,7 +56,9 @@ static void DestroyContext(cubeb* ctx) } } -std::shared_ptr CubebUtils::GetContext() +namespace CubebUtils +{ +std::shared_ptr GetContext() { static std::weak_ptr weak; @@ -83,12 +92,12 @@ std::shared_ptr CubebUtils::GetContext() return shared; } -std::vector> CubebUtils::ListInputDevices() +std::vector> ListInputDevices() { std::vector> devices; cubeb_device_collection collection; - auto cubeb_ctx = CubebUtils::GetContext(); + auto cubeb_ctx = GetContext(); int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection); if (r != CUBEB_OK) @@ -141,7 +150,7 @@ cubeb_devid GetInputDeviceById(std::string_view id) return nullptr; cubeb_device_collection collection; - auto cubeb_ctx = CubebUtils::GetContext(); + auto cubeb_ctx = GetContext(); int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection); if (r != CUBEB_OK) @@ -169,3 +178,59 @@ cubeb_devid GetInputDeviceById(std::string_view id) return device_id; } + +CoInitSyncWorker::CoInitSyncWorker([[maybe_unused]] std::string_view worker_name) +#ifdef _WIN32 + : m_work_queue +{ + worker_name, [](const CoInitSyncWorker::FunctionType& f) { f(); } +} +#endif +{ +#ifdef _WIN32 + Common::Event sync_event; + m_work_queue.EmplaceItem([this, &sync_event] { + Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); + auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); + m_coinit_success = result == S_OK; + m_should_couninit = result == S_OK || result == S_FALSE; + }); + sync_event.Wait(); +#endif +} + +CoInitSyncWorker::~CoInitSyncWorker() +{ +#ifdef _WIN32 + if (m_should_couninit) + { + Common::Event sync_event; + m_work_queue.EmplaceItem([this, &sync_event] { + Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); + m_should_couninit = false; + CoUninitialize(); + }); + sync_event.Wait(); + } + m_coinit_success = false; +#endif +} + +bool CoInitSyncWorker::Execute(FunctionType f) +{ +#ifdef _WIN32 + if (!m_coinit_success) + return false; + + Common::Event sync_event; + m_work_queue.EmplaceItem([&sync_event, f] { + Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); +#endif + f(); +#ifdef _WIN32 + }); + sync_event.Wait(); +#endif + return true; +} +} // namespace CubebUtils diff --git a/Source/Core/AudioCommon/CubebUtils.h b/Source/Core/AudioCommon/CubebUtils.h index 943ea833593b..ac60fd434f76 100644 --- a/Source/Core/AudioCommon/CubebUtils.h +++ b/Source/Core/AudioCommon/CubebUtils.h @@ -10,6 +10,10 @@ #include #include +#ifdef _WIN32 +#include "Common/WorkQueueThread.h" +#endif + struct cubeb; namespace CubebUtils @@ -17,4 +21,23 @@ namespace CubebUtils std::shared_ptr GetContext(); std::vector> ListInputDevices(); const void* GetInputDeviceById(std::string_view id); + +// Helper used to handle Windows COM library for cubeb WASAPI backend +class CoInitSyncWorker +{ +public: + using FunctionType = std::function; + + CoInitSyncWorker(std::string_view worker_name); + ~CoInitSyncWorker(); + + bool Execute(FunctionType f); + +#ifdef _WIN32 +private: + Common::WorkQueueThread m_work_queue; + bool m_coinit_success = false; + bool m_should_couninit = false; +#endif +}; } // namespace CubebUtils diff --git a/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp b/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp index 0afc89f96cc5..e370d719d7f9 100644 --- a/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp +++ b/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp @@ -28,56 +28,21 @@ namespace IOS::HLE::USB { Microphone::Microphone(const WiiSpeakState& sampler) : m_sampler(sampler) { -#ifdef _WIN32 - Common::Event sync_event; - m_work_queue.EmplaceItem([this, &sync_event] { - Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); - auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); - m_coinit_success = result == S_OK; - m_should_couninit = result == S_OK || result == S_FALSE; - }); - sync_event.Wait(); -#endif - StreamInit(); } Microphone::~Microphone() { StreamTerminate(); - -#ifdef _WIN32 - if (m_should_couninit) - { - Common::Event sync_event; - m_work_queue.EmplaceItem([this, &sync_event] { - Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); - m_should_couninit = false; - CoUninitialize(); - }); - sync_event.Wait(); - } - m_coinit_success = false; -#endif } void Microphone::StreamInit() { -#ifdef _WIN32 - if (!m_coinit_success) + if (!m_worker.Execute([this] { m_cubeb_ctx = CubebUtils::GetContext(); })) { ERROR_LOG_FMT(IOS_USB, "Failed to init Wii Speak stream"); return; } - Common::Event sync_event; - m_work_queue.EmplaceItem([this, &sync_event] { - Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); -#endif - m_cubeb_ctx = CubebUtils::GetContext(); -#ifdef _WIN32 - }); - sync_event.Wait(); -#endif // TODO: Not here but rather inside the WiiSpeak device if possible? StreamStart(); @@ -88,20 +53,7 @@ void Microphone::StreamTerminate() StopStream(); if (m_cubeb_ctx) - { -#ifdef _WIN32 - if (!m_coinit_success) - return; - Common::Event sync_event; - m_work_queue.EmplaceItem([this, &sync_event] { - Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); -#endif - m_cubeb_ctx.reset(); -#ifdef _WIN32 - }); - sync_event.Wait(); -#endif - } + m_worker.Execute([this] { m_cubeb_ctx.reset(); }); } static void state_callback(cubeb_stream* stream, void* user_data, cubeb_state state) @@ -113,14 +65,7 @@ void Microphone::StreamStart() if (!m_cubeb_ctx) return; -#ifdef _WIN32 - if (!m_coinit_success) - return; - Common::Event sync_event; - m_work_queue.EmplaceItem([this, &sync_event] { - Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); -#endif - + m_worker.Execute([this] { #ifdef ANDROID JNIEnv* env = IDCache::GetEnvForThread(); if (jboolean result = env->CallStaticBooleanMethod( @@ -164,30 +109,20 @@ void Microphone::StreamStart() } INFO_LOG_FMT(IOS_USB, "started cubeb stream"); -#ifdef _WIN32 }); - sync_event.Wait(); -#endif } void Microphone::StopStream() { - if (m_cubeb_stream) - { -#ifdef _WIN32 - Common::Event sync_event; - m_work_queue.EmplaceItem([this, &sync_event] { - Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); }); -#endif - if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK) - ERROR_LOG_FMT(IOS_USB, "Error stopping cubeb stream"); - cubeb_stream_destroy(m_cubeb_stream); - m_cubeb_stream = nullptr; -#ifdef _WIN32 - }); - sync_event.Wait(); -#endif - } + if (!m_cubeb_stream) + return; + + m_worker.Execute([this] { + if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK) + ERROR_LOG_FMT(IOS_USB, "Error stopping cubeb stream"); + cubeb_stream_destroy(m_cubeb_stream); + m_cubeb_stream = nullptr; + }); } long Microphone::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, diff --git a/Source/Core/Core/IOS/USB/Emulated/Microphone.h b/Source/Core/Core/IOS/USB/Emulated/Microphone.h index 5c85e612ca50..83ab16bde386 100644 --- a/Source/Core/Core/IOS/USB/Emulated/Microphone.h +++ b/Source/Core/Core/IOS/USB/Emulated/Microphone.h @@ -7,8 +7,8 @@ #include #include +#include "AudioCommon/CubebUtils.h" #include "Common/CommonTypes.h" -#include "Common/WorkQueueThread.h" struct cubeb; struct cubeb_stream; @@ -52,11 +52,6 @@ class Microphone final const WiiSpeakState& m_sampler; -#ifdef _WIN32 - Common::WorkQueueThread> m_work_queue{ - "Wii Speak Worker", [](const std::function& func) { func(); }}; - bool m_coinit_success = false; - bool m_should_couninit = false; -#endif + CubebUtils::CoInitSyncWorker m_worker{"Wii Speak Worker"}; }; } // namespace IOS::HLE::USB