2017-04-23 23:55:37 -07:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-04-23 23:55:37 -07:00
|
|
|
|
2021-12-09 18:22:16 -08:00
|
|
|
#include "AudioCommon/CubebUtils.h"
|
|
|
|
|
2017-04-23 23:55:37 -07:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2022-06-18 00:39:41 +02:00
|
|
|
#include <thread>
|
2017-04-23 23:55:37 -07:00
|
|
|
|
2022-06-18 00:39:41 +02:00
|
|
|
#include "Common/Assert.h"
|
2017-04-23 23:55:37 -07:00
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/Logging/LogManager.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include <cubeb/cubeb.h>
|
|
|
|
|
2024-08-25 00:06:50 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Objbase.h>
|
|
|
|
|
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/ScopeGuard.h"
|
|
|
|
#endif
|
|
|
|
|
2017-04-23 23:55:37 -07:00
|
|
|
static ptrdiff_t s_path_cutoff_point = 0;
|
|
|
|
|
|
|
|
static void LogCallback(const char* format, ...)
|
|
|
|
{
|
2020-10-20 15:37:02 -04:00
|
|
|
auto* instance = Common::Log::LogManager::GetInstance();
|
|
|
|
if (instance == nullptr)
|
2017-04-23 23:55:37 -07:00
|
|
|
return;
|
|
|
|
|
2022-07-17 19:12:04 -07:00
|
|
|
constexpr auto log_type = Common::Log::LogType::AUDIO;
|
2024-03-17 01:28:23 +00:00
|
|
|
constexpr auto log_level = Common::Log::LogLevel::LINFO;
|
|
|
|
if (!instance->IsEnabled(log_type, log_level))
|
2022-07-17 19:12:04 -07:00
|
|
|
return;
|
|
|
|
|
2017-04-23 23:55:37 -07:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
const char* filename = va_arg(args, const char*) + s_path_cutoff_point;
|
2020-10-20 15:37:02 -04:00
|
|
|
const int lineno = va_arg(args, int);
|
2022-07-19 15:13:26 -07:00
|
|
|
const std::string adapted_format(StripWhitespace(format + strlen("%s:%d:")));
|
2020-10-20 15:37:02 -04:00
|
|
|
const std::string message = StringFromFormatV(adapted_format.c_str(), args);
|
2017-04-23 23:55:37 -07:00
|
|
|
va_end(args);
|
2020-10-20 15:37:02 -04:00
|
|
|
|
2024-03-17 01:28:23 +00:00
|
|
|
instance->LogWithFullPath(log_level, log_type, filename, lineno, message.c_str());
|
2017-04-23 23:55:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void DestroyContext(cubeb* ctx)
|
|
|
|
{
|
|
|
|
cubeb_destroy(ctx);
|
|
|
|
if (cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr) != CUBEB_OK)
|
|
|
|
{
|
2020-10-21 13:32:25 -04:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error removing cubeb log callback");
|
2017-04-23 23:55:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-25 00:06:50 +04:00
|
|
|
namespace CubebUtils
|
|
|
|
{
|
|
|
|
std::shared_ptr<cubeb> GetContext()
|
2017-04-23 23:55:37 -07:00
|
|
|
{
|
|
|
|
static std::weak_ptr<cubeb> weak;
|
|
|
|
|
|
|
|
std::shared_ptr<cubeb> shared = weak.lock();
|
|
|
|
// Already initialized
|
|
|
|
if (shared)
|
|
|
|
return shared;
|
|
|
|
|
|
|
|
const char* filename = __FILE__;
|
|
|
|
const char* match_point = strstr(filename, DIR_SEP "Source" DIR_SEP "Core" DIR_SEP);
|
2022-07-17 19:12:04 -07:00
|
|
|
if (!match_point)
|
|
|
|
match_point = strstr(filename, R"(\Source\Core\)");
|
2017-04-23 23:55:37 -07:00
|
|
|
if (match_point)
|
|
|
|
{
|
|
|
|
s_path_cutoff_point = match_point - filename + strlen(DIR_SEP "Externals" DIR_SEP);
|
|
|
|
}
|
|
|
|
if (cubeb_set_log_callback(CUBEB_LOG_NORMAL, LogCallback) != CUBEB_OK)
|
|
|
|
{
|
2020-10-21 13:32:25 -04:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error setting cubeb log callback");
|
2017-04-23 23:55:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cubeb* ctx;
|
2023-07-22 23:12:34 +02:00
|
|
|
if (cubeb_init(&ctx, "Dolphin Emulator", nullptr) != CUBEB_OK)
|
2017-04-23 23:55:37 -07:00
|
|
|
{
|
2020-10-21 13:32:25 -04:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error initializing cubeb library");
|
2017-04-23 23:55:37 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-21 13:32:25 -04:00
|
|
|
INFO_LOG_FMT(AUDIO, "Cubeb initialized using {} backend", cubeb_get_backend_id(ctx));
|
2017-04-23 23:55:37 -07:00
|
|
|
|
|
|
|
weak = shared = {ctx, DestroyContext};
|
|
|
|
return shared;
|
|
|
|
}
|
2024-05-09 14:51:30 +04:00
|
|
|
|
2024-08-25 00:06:50 +04:00
|
|
|
std::vector<std::pair<std::string, std::string>> ListInputDevices()
|
2024-05-09 14:51:30 +04:00
|
|
|
{
|
|
|
|
std::vector<std::pair<std::string, std::string>> devices;
|
|
|
|
|
|
|
|
cubeb_device_collection collection;
|
2024-08-25 00:06:50 +04:00
|
|
|
auto cubeb_ctx = GetContext();
|
2024-05-09 14:51:30 +04:00
|
|
|
int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection);
|
|
|
|
|
|
|
|
if (r != CUBEB_OK)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(AUDIO, "Error listing cubeb input devices");
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
INFO_LOG_FMT(AUDIO, "Listing cubeb input devices:");
|
|
|
|
for (uint32_t i = 0; i < collection.count; i++)
|
|
|
|
{
|
|
|
|
auto& info = collection.device[i];
|
|
|
|
auto& device_state = info.state;
|
|
|
|
const char* state_name = [device_state] {
|
|
|
|
switch (device_state)
|
|
|
|
{
|
|
|
|
case CUBEB_DEVICE_STATE_DISABLED:
|
|
|
|
return "disabled";
|
|
|
|
case CUBEB_DEVICE_STATE_UNPLUGGED:
|
|
|
|
return "unplugged";
|
|
|
|
case CUBEB_DEVICE_STATE_ENABLED:
|
|
|
|
return "enabled";
|
|
|
|
default:
|
|
|
|
return "unknown?";
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
INFO_LOG_FMT(AUDIO,
|
|
|
|
"[{}] Device ID: {}\n"
|
|
|
|
"\tName: {}\n"
|
|
|
|
"\tGroup ID: {}\n"
|
|
|
|
"\tVendor: {}\n"
|
|
|
|
"\tState: {}",
|
|
|
|
i, info.device_id, info.friendly_name, info.group_id,
|
|
|
|
(info.vendor_name == nullptr) ? "(null)" : info.vendor_name, state_name);
|
|
|
|
if (info.state == CUBEB_DEVICE_STATE_ENABLED)
|
|
|
|
{
|
|
|
|
devices.emplace_back(info.device_id, info.friendly_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_device_collection_destroy(cubeb_ctx.get(), &collection);
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
2024-08-25 00:06:50 +04:00
|
|
|
cubeb_devid GetInputDeviceById(std::string_view id)
|
2024-05-09 14:51:30 +04:00
|
|
|
{
|
|
|
|
if (id.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
cubeb_device_collection collection;
|
2024-08-25 00:06:50 +04:00
|
|
|
auto cubeb_ctx = GetContext();
|
2024-05-09 14:51:30 +04:00
|
|
|
int r = cubeb_enumerate_devices(cubeb_ctx.get(), CUBEB_DEVICE_TYPE_INPUT, &collection);
|
|
|
|
|
|
|
|
if (r != CUBEB_OK)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(AUDIO, "Error enumerating cubeb input devices");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_devid device_id = nullptr;
|
|
|
|
for (uint32_t i = 0; i < collection.count; i++)
|
|
|
|
{
|
|
|
|
auto& info = collection.device[i];
|
|
|
|
if (id.compare(info.device_id) == 0)
|
|
|
|
{
|
|
|
|
device_id = info.devid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (device_id == nullptr)
|
|
|
|
{
|
|
|
|
WARN_LOG_FMT(AUDIO, "Failed to find selected input device, defaulting to system preferences");
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_device_collection_destroy(cubeb_ctx.get(), &collection);
|
|
|
|
|
|
|
|
return device_id;
|
|
|
|
}
|
2024-08-25 00:06:50 +04:00
|
|
|
|
|
|
|
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
|