From 8169c72ce63ba9315888fd05fe8a7885f93e6f4f Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 8 Nov 2026 14:53:37 -0600 Subject: CubebUtils: Initialize cubeb_device_collection objects to avoid UB inside cubeb enumerate_devices calls on failure. --- Source/Core/AudioCommon/CubebUtils.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Source/Core/AudioCommon/CubebUtils.cpp') diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index 0be4fdeb81..4dc34a0d44 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -83,7 +83,7 @@ std::vector> ListInputDevices() { std::vector> devices; - cubeb_device_collection collection; + cubeb_device_collection collection{}; auto cubeb_ctx = GetContext(); const int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection); @@ -147,7 +147,7 @@ cubeb_devid GetInputDeviceById(std::string_view id) if (id.empty()) return nullptr; - cubeb_device_collection collection; + cubeb_device_collection collection{}; auto cubeb_ctx = CubebUtils::GetContext(); const int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection); @@ -181,7 +181,7 @@ std::vector> ListOutputDevices() { std::vector> devices; - cubeb_device_collection collection; + cubeb_device_collection collection{}; auto cubeb_ctx = GetContext(); if (!cubeb_ctx) return devices; @@ -215,7 +215,7 @@ const void* GetOutputDeviceById(std::string_view id) if (id.empty()) return nullptr; - cubeb_device_collection collection; + cubeb_device_collection collection{}; auto cubeb_ctx = GetContext(); if (!cubeb_ctx) return nullptr; -- cgit v1.2.3 From eaad965f5785a63112949535ce14798d32dd38bf Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 15 Nov 2026 22:33:58 -0600 Subject: AudioCommon: Allow MAIN_AUDIO_BACKEND to optionally specify the Cubeb backend after a slash. e.g. "Cubeb/wasapi". --- Source/Core/AudioCommon/CubebUtils.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/CubebUtils.cpp') diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index 4dc34a0d44..ff1a304880 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -10,6 +10,7 @@ #include "Common/Logging/Log.h" #include "Common/Logging/LogManager.h" #include "Common/StringUtil.h" +#include "Core/Config/MainSettings.h" #include @@ -67,8 +68,15 @@ std::shared_ptr GetContext() ERROR_LOG_FMT(AUDIO, "Error setting cubeb log callback"); } + // Cubeb may have its own backend specified after a slash. + // e.g. "Cubeb/wasapi" + const auto audio_backend = Config::Get(Config::MAIN_AUDIO_BACKEND); + const auto cubeb_backend_delimiter_pos = audio_backend.find('/'); + const char* const backend_name = (cubeb_backend_delimiter_pos == std::string::npos) ? + nullptr : + audio_backend.c_str() + cubeb_backend_delimiter_pos + 1; cubeb* ctx; - if (cubeb_init(&ctx, "Dolphin Emulator", nullptr) != CUBEB_OK) + if (cubeb_init(&ctx, "Dolphin Emulator", backend_name) != CUBEB_OK) { ERROR_LOG_FMT(AUDIO, "Error initializing cubeb library"); return nullptr; -- cgit v1.2.3 From 2e0a63c918aeec3d3a7ffe08cd41ec3ad45ea0f8 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 16 Nov 2026 01:13:25 -0600 Subject: CubebUtils: Initialize Cubeb based on the configured audio backend even when it's set to just "Pulse" or "ALSA". --- Source/Core/AudioCommon/CubebUtils.cpp | 35 +++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'Source/Core/AudioCommon/CubebUtils.cpp') diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index ff1a304880..b86283dcdf 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -7,6 +7,7 @@ #include #include +#include "AudioCommon/AudioCommon.h" #include "Common/Logging/Log.h" #include "Common/Logging/LogManager.h" #include "Common/StringUtil.h" @@ -52,6 +53,30 @@ static void DestroyContext(cubeb* ctx) } } +static std::string GetCubebBackendName() +{ + std::string audio_backend = Config::Get(Config::MAIN_AUDIO_BACKEND); + + if (!AudioCommon::IsCubebBackend(audio_backend)) + { + // A non-Cubeb audio backend is configured. + // A lowercase version of this string will work some of the time. + // e.g. for "Pulse" or "ALSA". + // Cubeb will internally use a fallback if it's not a valid string. + Common::ToLower(&audio_backend); + return audio_backend; + } + + // Cubeb may have its own backend specified after a slash. + // e.g. "Cubeb/wasapi" + const auto cubeb_backend_delimiter_pos = audio_backend.find('/'); + + if (cubeb_backend_delimiter_pos == std::string::npos) + return {}; + + return std::move(audio_backend).substr(cubeb_backend_delimiter_pos + 1); +} + namespace CubebUtils { std::shared_ptr GetContext() @@ -68,13 +93,9 @@ std::shared_ptr GetContext() ERROR_LOG_FMT(AUDIO, "Error setting cubeb log callback"); } - // Cubeb may have its own backend specified after a slash. - // e.g. "Cubeb/wasapi" - const auto audio_backend = Config::Get(Config::MAIN_AUDIO_BACKEND); - const auto cubeb_backend_delimiter_pos = audio_backend.find('/'); - const char* const backend_name = (cubeb_backend_delimiter_pos == std::string::npos) ? - nullptr : - audio_backend.c_str() + cubeb_backend_delimiter_pos + 1; + const auto backend_name_str = GetCubebBackendName(); + const char* const backend_name = backend_name_str.empty() ? nullptr : backend_name_str.c_str(); + cubeb* ctx; if (cubeb_init(&ctx, "Dolphin Emulator", backend_name) != CUBEB_OK) { -- cgit v1.2.3 From a906348b9143610d958af908c2a1d4adcb86f810 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Mon, 2 Nov 2026 01:34:23 -0600 Subject: CubebUtils: Add functions to get a list of supported backend names and the display name for a backend. --- Source/Core/AudioCommon/CubebUtils.cpp | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'Source/Core/AudioCommon/CubebUtils.cpp') diff --git a/Source/Core/AudioCommon/CubebUtils.cpp b/Source/Core/AudioCommon/CubebUtils.cpp index b86283dcdf..feaa5f2392 100644 --- a/Source/Core/AudioCommon/CubebUtils.cpp +++ b/Source/Core/AudioCommon/CubebUtils.cpp @@ -10,6 +10,7 @@ #include "AudioCommon/AudioCommon.h" #include "Common/Logging/Log.h" #include "Common/Logging/LogManager.h" +#include "Common/Projection.h" #include "Common/StringUtil.h" #include "Core/Config/MainSettings.h" @@ -276,6 +277,45 @@ const void* GetOutputDeviceById(std::string_view id) return device_id; } +std::vector GetBackendNames() +{ + // Some tricks to only call `cubeb_get_backend_names` if it exists. + return [](auto... args) -> std::vector { + if constexpr (requires { cubeb_get_backend_names(args...); }) + { + const auto names = cubeb_get_backend_names(args...); + return {names.names, names.names + names.count}; + } + else + { // The included version of Cubeb lacks the `cubeb_get_backend_names` function. + return {}; + } + }(); +} + +std::string_view GetCubebBackendDisplayName(std::string_view backend_name) +{ + static constexpr std::pair name_map[] = { + {"pulse", "PulseAudio"}, + {"pulse-rust", "PulseAudio Rust"}, + {"jack", "JACK"}, + {"alsa", "ALSA"}, + {"audiounit", "AudioUnit"}, + {"audiounit-rust", "AudioUnit Rust"}, + {"wasapi", "WASAPI"}, + {"winmm", "WinMM"}, + {"sun", "Sun"}, + {"opensl", "OpenSL"}, + {"oss", "OSS"}, + {"aaudio", "AAudio"}, + {"audiotrack", "AudioTrack"}, + {"kai", "KAI"}, + }; + + const auto it = std::ranges::find(name_map, backend_name, Common::Projection::First{}); + return (it != std::end(name_map)) ? it->second : backend_name; +} + CoInitSyncWorker::CoInitSyncWorker([[maybe_unused]] std::string worker_name) #ifdef _WIN32 : m_work_queue{std::move(worker_name)} -- cgit v1.2.3