cubeb_audio: Make sure COM is initialized on Windows. (#1958)

This commit is contained in:
squidbus 2024-12-29 02:46:59 -08:00 committed by GitHub
parent 4b2db61120
commit f8177902a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 6 deletions

View file

@ -34,6 +34,9 @@ public:
private: private:
cubeb* ctx = nullptr; cubeb* ctx = nullptr;
#ifdef _WIN32
bool owns_com = false;
#endif
}; };
class SDLAudioOut final : public AudioOutBackend { class SDLAudioOut final : public AudioOutBackend {

View file

@ -10,9 +10,11 @@
#include "core/libraries/audio/audioout.h" #include "core/libraries/audio/audioout.h"
#include "core/libraries/audio/audioout_backend.h" #include "core/libraries/audio/audioout_backend.h"
namespace Libraries::AudioOut { #ifdef _WIN32
#include <Objbase.h>
#endif
constexpr int AUDIO_STREAM_BUFFER_THRESHOLD = 65536; // Define constant for buffer threshold namespace Libraries::AudioOut {
class CubebPortBackend : public PortBackend { class CubebPortBackend : public PortBackend {
public: public:
@ -143,17 +145,26 @@ private:
}; };
CubebAudioOut::CubebAudioOut() { 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) { if (const auto ret = cubeb_init(&ctx, "shadPS4", nullptr); ret != CUBEB_OK) {
LOG_CRITICAL(Lib_AudioOut, "Failed to create cubeb context: {}", ret); LOG_CRITICAL(Lib_AudioOut, "Failed to create cubeb context: {}", ret);
} }
} }
CubebAudioOut::~CubebAudioOut() { CubebAudioOut::~CubebAudioOut() {
if (!ctx) { if (ctx) {
return;
}
cubeb_destroy(ctx); cubeb_destroy(ctx);
ctx = nullptr; ctx = nullptr;
}
#ifdef _WIN32
if (owns_com) {
CoUninitialize();
owns_com = false;
}
#endif
} }
std::unique_ptr<PortBackend> CubebAudioOut::Open(PortOut& port) { std::unique_ptr<PortBackend> CubebAudioOut::Open(PortOut& port) {