2017-04-24 06:55:37 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-04-24 06:55:37 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "AudioCommon/CubebUtils.h"
|
|
|
|
|
2017-04-24 06:55:37 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/Logging/LogManager.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include <cubeb/cubeb.h>
|
|
|
|
|
|
|
|
static ptrdiff_t s_path_cutoff_point = 0;
|
|
|
|
|
|
|
|
static void LogCallback(const char* format, ...)
|
|
|
|
{
|
2020-10-20 19:37:02 +00:00
|
|
|
auto* instance = Common::Log::LogManager::GetInstance();
|
|
|
|
if (instance == nullptr)
|
2017-04-24 06:55:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
const char* filename = va_arg(args, const char*) + s_path_cutoff_point;
|
2020-10-20 19:37:02 +00:00
|
|
|
const int lineno = va_arg(args, int);
|
|
|
|
const std::string adapted_format(StripSpaces(format + strlen("%s:%d:")));
|
|
|
|
const std::string message = StringFromFormatV(adapted_format.c_str(), args);
|
2017-04-24 06:55:37 +00:00
|
|
|
va_end(args);
|
2020-10-20 19:37:02 +00:00
|
|
|
|
2021-10-21 19:11:07 +00:00
|
|
|
instance->Log(Common::Log::LogLevel::LNOTICE, Common::Log::LogType::AUDIO, filename, lineno,
|
|
|
|
message.c_str());
|
2017-04-24 06:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void DestroyContext(cubeb* ctx)
|
|
|
|
{
|
|
|
|
cubeb_destroy(ctx);
|
|
|
|
if (cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr) != CUBEB_OK)
|
|
|
|
{
|
2020-10-21 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error removing cubeb log callback");
|
2017-04-24 06:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<cubeb> CubebUtils::GetContext()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
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 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error setting cubeb log callback");
|
2017-04-24 06:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cubeb* ctx;
|
|
|
|
if (cubeb_init(&ctx, "Dolphin", nullptr) != CUBEB_OK)
|
|
|
|
{
|
2020-10-21 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error initializing cubeb library");
|
2017-04-24 06:55:37 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-21 17:32:25 +00:00
|
|
|
INFO_LOG_FMT(AUDIO, "Cubeb initialized using {} backend", cubeb_get_backend_id(ctx));
|
2017-04-24 06:55:37 +00:00
|
|
|
|
|
|
|
weak = shared = {ctx, DestroyContext};
|
|
|
|
return shared;
|
|
|
|
}
|