From f8177902a5e4a2f99bd166b9d9f85f468dfce7d5 Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Sun, 29 Dec 2024 02:46:59 -0800 Subject: [PATCH] cubeb_audio: Make sure COM is initialized on Windows. (#1958) --- src/core/libraries/audio/audioout_backend.h | 3 +++ src/core/libraries/audio/cubeb_audio.cpp | 23 +++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/core/libraries/audio/audioout_backend.h b/src/core/libraries/audio/audioout_backend.h index ecc4cf7c..f423d496 100644 --- a/src/core/libraries/audio/audioout_backend.h +++ b/src/core/libraries/audio/audioout_backend.h @@ -34,6 +34,9 @@ public: private: cubeb* ctx = nullptr; +#ifdef _WIN32 + bool owns_com = false; +#endif }; class SDLAudioOut final : public AudioOutBackend { diff --git a/src/core/libraries/audio/cubeb_audio.cpp b/src/core/libraries/audio/cubeb_audio.cpp index e1195558..4127931b 100644 --- a/src/core/libraries/audio/cubeb_audio.cpp +++ b/src/core/libraries/audio/cubeb_audio.cpp @@ -10,9 +10,11 @@ #include "core/libraries/audio/audioout.h" #include "core/libraries/audio/audioout_backend.h" -namespace Libraries::AudioOut { +#ifdef _WIN32 +#include +#endif -constexpr int AUDIO_STREAM_BUFFER_THRESHOLD = 65536; // Define constant for buffer threshold +namespace Libraries::AudioOut { class CubebPortBackend : public PortBackend { public: @@ -143,17 +145,26 @@ private: }; CubebAudioOut::CubebAudioOut() { +#ifdef _WIN32 + // Need to initialize COM for this thread on Windows, in case WASAPI backend is used. + owns_com = CoInitializeEx(nullptr, COINIT_MULTITHREADED) == S_OK; +#endif if (const auto ret = cubeb_init(&ctx, "shadPS4", nullptr); ret != CUBEB_OK) { LOG_CRITICAL(Lib_AudioOut, "Failed to create cubeb context: {}", ret); } } CubebAudioOut::~CubebAudioOut() { - if (!ctx) { - return; + if (ctx) { + cubeb_destroy(ctx); + ctx = nullptr; } - cubeb_destroy(ctx); - ctx = nullptr; +#ifdef _WIN32 + if (owns_com) { + CoUninitialize(); + owns_com = false; + } +#endif } std::unique_ptr CubebAudioOut::Open(PortOut& port) {