Duckstation/src/util/cubeb_audio_stream.cpp

321 lines
8.6 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
2023-08-27 06:00:06 +00:00
#include "host.h"
#include "imgui_manager.h"
#include "core/settings.h"
2020-01-11 03:28:40 +00:00
#include "common/assert.h"
#include "common/log.h"
2022-12-14 07:58:14 +00:00
#include "common/scoped_guard.h"
2022-07-27 14:42:41 +00:00
#include "common/string_util.h"
2023-08-27 06:00:06 +00:00
2022-07-27 14:42:41 +00:00
#include "cubeb/cubeb.h"
2022-12-14 07:58:14 +00:00
#include "fmt/format.h"
2020-01-11 03:28:40 +00:00
2021-06-28 10:16:48 +00:00
#ifdef _WIN32
#include "common/windows_headers.h"
#include <objbase.h>
#endif
2023-08-27 06:00:06 +00:00
Log_SetChannel(CubebAudioStream);
namespace {
class CubebAudioStream : public AudioStream
{
public:
CubebAudioStream(u32 sample_rate, u32 channels, u32 buffer_ms, AudioStretchMode stretch);
~CubebAudioStream();
void SetPaused(bool paused) override;
void SetOutputVolume(u32 volume) override;
bool Initialize(u32 latency_ms);
private:
static void LogCallback(const char* fmt, ...);
static long DataCallback(cubeb_stream* stm, void* user_ptr, const void* input_buffer, void* output_buffer,
long nframes);
static void StateCallback(cubeb_stream* stream, void* user_ptr, cubeb_state state);
void DestroyContextAndStream();
cubeb* m_context = nullptr;
cubeb_stream* stream = nullptr;
#ifdef _WIN32
bool m_com_initialized_by_us = false;
#endif
};
} // namespace
2022-07-27 14:42:41 +00:00
CubebAudioStream::CubebAudioStream(u32 sample_rate, u32 channels, u32 buffer_ms, AudioStretchMode stretch)
: AudioStream(sample_rate, channels, buffer_ms, stretch)
{
}
2020-01-11 03:28:40 +00:00
CubebAudioStream::~CubebAudioStream()
{
2022-07-27 14:42:41 +00:00
DestroyContextAndStream();
}
void CubebAudioStream::LogCallback(const char* fmt, ...)
{
LargeString str;
2022-07-27 14:42:41 +00:00
std::va_list ap;
va_start(ap, fmt);
str.vsprintf(fmt, ap);
2022-07-27 14:42:41 +00:00
va_end(ap);
Log_DevPrint(str);
2020-01-11 03:28:40 +00:00
}
2022-07-27 14:42:41 +00:00
void CubebAudioStream::DestroyContextAndStream()
2020-01-11 03:28:40 +00:00
{
2022-07-27 14:42:41 +00:00
if (stream)
{
cubeb_stream_stop(stream);
cubeb_stream_destroy(stream);
stream = nullptr;
}
if (m_context)
{
cubeb_destroy(m_context);
m_context = nullptr;
}
2020-01-11 03:28:40 +00:00
2022-07-27 14:42:41 +00:00
#ifdef _WIN32
if (m_com_initialized_by_us)
{
CoUninitialize();
m_com_initialized_by_us = false;
}
#endif
}
bool CubebAudioStream::Initialize(u32 latency_ms)
{
2021-06-28 10:16:48 +00:00
#ifdef _WIN32
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
m_com_initialized_by_us = SUCCEEDED(hr);
2022-07-27 14:42:41 +00:00
if (FAILED(hr) && hr != RPC_E_CHANGED_MODE)
{
2022-07-27 14:42:41 +00:00
Host::ReportErrorAsync("Error", "Failed to initialize COM for Cubeb");
return false;
}
#endif
2022-07-27 14:42:41 +00:00
cubeb_set_log_callback(CUBEB_LOG_NORMAL, LogCallback);
2022-08-05 07:50:28 +00:00
int rv =
cubeb_init(&m_context, "DuckStation", g_settings.audio_driver.empty() ? nullptr : g_settings.audio_driver.c_str());
2020-01-11 03:28:40 +00:00
if (rv != CUBEB_OK)
{
2022-07-27 14:42:41 +00:00
Host::ReportFormattedErrorAsync("Error", "Could not initialize cubeb context: %d", rv);
2020-01-11 03:28:40 +00:00
return false;
}
cubeb_stream_params params = {};
params.format = CUBEB_SAMPLE_S16LE;
2022-07-27 14:42:41 +00:00
params.rate = m_sample_rate;
2020-01-11 03:28:40 +00:00
params.channels = m_channels;
params.layout = CUBEB_LAYOUT_UNDEFINED;
2022-07-27 14:42:41 +00:00
params.prefs = CUBEB_STREAM_PREF_NONE;
2020-01-11 03:28:40 +00:00
2022-07-27 14:42:41 +00:00
u32 latency_frames = GetBufferSizeForMS(m_sample_rate, (latency_ms == 0) ? m_buffer_ms : latency_ms);
u32 min_latency_frames = 0;
rv = cubeb_get_min_latency(m_context, &params, &min_latency_frames);
if (rv == CUBEB_ERROR_NOT_SUPPORTED)
2020-01-11 03:28:40 +00:00
{
2022-07-27 14:42:41 +00:00
Log_DevPrintf("(Cubeb) Cubeb backend does not support latency queries, using latency of %d ms (%u frames).",
m_buffer_ms, latency_frames);
2020-01-11 03:28:40 +00:00
}
else
{
if (rv != CUBEB_OK)
{
2022-07-27 14:42:41 +00:00
Log_ErrorPrintf("(Cubeb) Could not get minimum latency: %d", rv);
DestroyContextAndStream();
return false;
}
2022-07-27 14:42:41 +00:00
const u32 minimum_latency_ms = GetMSForBufferSize(m_sample_rate, min_latency_frames);
Log_DevPrintf("(Cubeb) Minimum latency: %u ms (%u audio frames)", minimum_latency_ms, min_latency_frames);
if (latency_ms == 0)
{
2022-07-27 14:42:41 +00:00
// use minimum
latency_frames = min_latency_frames;
}
2022-07-27 14:42:41 +00:00
else if (minimum_latency_ms > latency_ms)
{
2022-07-27 14:42:41 +00:00
Log_WarningPrintf("(Cubeb) Minimum latency is above requested latency: %u vs %u, adjusting to compensate.",
min_latency_frames, latency_frames);
latency_frames = min_latency_frames;
}
}
2020-01-11 03:28:40 +00:00
2022-12-14 07:58:14 +00:00
cubeb_devid selected_device = nullptr;
const std::string& selected_device_name = g_settings.audio_output_device;
cubeb_device_collection devices;
bool devices_valid = false;
if (!selected_device_name.empty())
{
rv = cubeb_enumerate_devices(m_context, CUBEB_DEVICE_TYPE_OUTPUT, &devices);
devices_valid = (rv == CUBEB_OK);
if (rv == CUBEB_OK)
{
for (size_t i = 0; i < devices.count; i++)
{
const cubeb_device_info& di = devices.device[i];
if (di.device_id && selected_device_name == di.device_id)
{
Log_InfoPrintf("Using output device '%s' (%s).", di.device_id,
di.friendly_name ? di.friendly_name : di.device_id);
selected_device = di.devid;
break;
}
}
if (!selected_device)
{
Host::AddOSDMessage(
fmt::format("Requested audio output device '{}' not found, using default.", selected_device_name), 10.0f);
}
}
else
{
Log_WarningPrintf("cubeb_enumerate_devices() returned %d, using default device.", rv);
}
}
2022-07-27 14:42:41 +00:00
BaseInitialize();
m_volume = 100;
m_paused = false;
2020-01-11 03:28:40 +00:00
char stream_name[32];
2022-07-27 14:42:41 +00:00
std::snprintf(stream_name, sizeof(stream_name), "%p", this);
2020-01-11 03:28:40 +00:00
2022-12-14 07:58:14 +00:00
rv = cubeb_stream_init(m_context, &stream, stream_name, nullptr, nullptr, selected_device, &params, latency_frames,
2022-07-27 14:42:41 +00:00
&CubebAudioStream::DataCallback, StateCallback, this);
2022-12-14 07:58:14 +00:00
if (devices_valid)
cubeb_device_collection_destroy(m_context, &devices);
2020-01-11 03:28:40 +00:00
if (rv != CUBEB_OK)
{
2022-07-27 14:42:41 +00:00
Log_ErrorPrintf("(Cubeb) Could not create stream: %d", rv);
DestroyContextAndStream();
2020-01-11 03:28:40 +00:00
return false;
}
2022-07-27 14:42:41 +00:00
rv = cubeb_stream_start(stream);
2020-01-11 03:28:40 +00:00
if (rv != CUBEB_OK)
{
2022-07-27 14:42:41 +00:00
Log_ErrorPrintf("(Cubeb) Could not start stream: %d", rv);
DestroyContextAndStream();
return false;
2020-01-11 03:28:40 +00:00
}
2022-07-27 14:42:41 +00:00
return true;
2020-01-11 03:28:40 +00:00
}
void CubebAudioStream::StateCallback(cubeb_stream* stream, void* user_ptr, cubeb_state state)
2020-01-11 03:28:40 +00:00
{
2022-07-27 14:42:41 +00:00
// noop
2020-01-11 03:28:40 +00:00
}
long CubebAudioStream::DataCallback(cubeb_stream* stm, void* user_ptr, const void* input_buffer, void* output_buffer,
long nframes)
{
2022-07-27 14:42:41 +00:00
static_cast<CubebAudioStream*>(user_ptr)->ReadFrames(static_cast<s16*>(output_buffer), static_cast<u32>(nframes));
2020-01-11 03:28:40 +00:00
return nframes;
}
2022-07-27 14:42:41 +00:00
void CubebAudioStream::SetPaused(bool paused)
{
if (paused == m_paused || !stream)
return;
2020-01-11 03:28:40 +00:00
2022-07-27 14:42:41 +00:00
const int rv = paused ? cubeb_stream_stop(stream) : cubeb_stream_start(stream);
if (rv != CUBEB_OK)
{
Log_ErrorPrintf("Could not %s stream: %d", paused ? "pause" : "resume", rv);
return;
}
2020-01-11 03:28:40 +00:00
2022-07-27 14:42:41 +00:00
m_paused = paused;
}
2022-07-27 14:42:41 +00:00
void CubebAudioStream::SetOutputVolume(u32 volume)
{
2022-07-27 14:42:41 +00:00
if (volume == m_volume)
return;
2022-07-27 14:42:41 +00:00
int rv = cubeb_stream_set_volume(stream, static_cast<float>(volume) / 100.0f);
if (rv != CUBEB_OK)
{
Log_ErrorPrintf("cubeb_stream_set_volume() failed: %d", rv);
return;
}
m_volume = volume;
}
2023-08-13 06:28:28 +00:00
std::unique_ptr<AudioStream> AudioStream::CreateCubebAudioStream(u32 sample_rate, u32 channels, u32 buffer_ms,
u32 latency_ms, AudioStretchMode stretch)
2020-01-11 03:28:40 +00:00
{
2022-07-27 14:42:41 +00:00
std::unique_ptr<CubebAudioStream> stream(
std::make_unique<CubebAudioStream>(sample_rate, channels, buffer_ms, stretch));
if (!stream->Initialize(latency_ms))
stream.reset();
return stream;
2020-01-11 03:28:40 +00:00
}
2022-08-05 07:50:28 +00:00
2023-08-13 06:28:28 +00:00
std::vector<std::string> AudioStream::GetCubebDriverNames()
2022-08-05 07:50:28 +00:00
{
std::vector<std::string> names;
const char** cubeb_names = cubeb_get_backend_names();
for (u32 i = 0; cubeb_names[i] != nullptr; i++)
names.emplace_back(cubeb_names[i]);
return names;
}
2022-12-14 07:58:14 +00:00
2023-08-13 06:28:28 +00:00
std::vector<std::pair<std::string, std::string>> AudioStream::GetCubebOutputDevices(const char* driver)
2022-12-14 07:58:14 +00:00
{
std::vector<std::pair<std::string, std::string>> ret;
ret.emplace_back(std::string(), TRANSLATE_STR("CommonHost", "Default Output Device"));
2022-12-14 07:58:14 +00:00
cubeb* context;
int rv = cubeb_init(&context, "DuckStation", (driver && *driver) ? driver : nullptr);
if (rv != CUBEB_OK)
{
Log_ErrorPrintf("cubeb_init() failed: %d", rv);
return ret;
}
ScopedGuard context_cleanup([context]() { cubeb_destroy(context); });
cubeb_device_collection devices;
rv = cubeb_enumerate_devices(context, CUBEB_DEVICE_TYPE_OUTPUT, &devices);
if (rv != CUBEB_OK)
{
Log_ErrorPrintf("cubeb_enumerate_devices() failed: %d", rv);
return ret;
}
ScopedGuard devices_cleanup([context, &devices]() { cubeb_device_collection_destroy(context, &devices); });
for (size_t i = 0; i < devices.count; i++)
{
const cubeb_device_info& di = devices.device[i];
if (!di.device_id)
continue;
ret.emplace_back(di.device_id, di.friendly_name ? di.friendly_name : di.device_id);
}
return ret;
}