2018-05-25 06:50:37 +01:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-09-09 08:37:34 +01:00
|
|
|
#include <cstdarg>
|
2018-08-31 18:42:28 +01:00
|
|
|
#include <mutex>
|
2018-05-25 06:50:37 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <cubeb/cubeb.h>
|
|
|
|
#include "audio_core/audio_types.h"
|
|
|
|
#include "audio_core/cubeb_sink.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
|
|
|
struct CubebSink::Impl {
|
|
|
|
cubeb* ctx = nullptr;
|
|
|
|
cubeb_stream* stream = nullptr;
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
std::function<void(s16*, std::size_t)> cb;
|
2018-05-25 06:50:37 +01:00
|
|
|
|
|
|
|
static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
|
|
|
void* output_buffer, long num_frames);
|
|
|
|
static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state);
|
2018-09-09 08:37:34 +01:00
|
|
|
static void LogCallback(char const* fmt, ...);
|
2018-05-25 06:50:37 +01:00
|
|
|
};
|
|
|
|
|
2018-12-13 21:23:31 +00:00
|
|
|
CubebSink::CubebSink(std::string_view target_device_name) : impl(std::make_unique<Impl>()) {
|
2019-02-27 22:13:51 +00:00
|
|
|
if (cubeb_init(&impl->ctx, "Citra Output", nullptr) != CUBEB_OK) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
|
2018-05-25 06:50:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-12 05:43:07 +01:00
|
|
|
if (cubeb_set_log_callback(CUBEB_LOG_NORMAL, &Impl::LogCallback) != CUBEB_OK) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "cubeb_set_log_callback failed");
|
|
|
|
return;
|
|
|
|
}
|
2018-05-25 06:50:37 +01:00
|
|
|
|
2023-07-12 05:43:07 +01:00
|
|
|
cubeb_stream_params params = {
|
|
|
|
.format = CUBEB_SAMPLE_S16LE,
|
|
|
|
.rate = native_sample_rate,
|
|
|
|
.channels = 2,
|
|
|
|
.layout = CUBEB_LAYOUT_STEREO,
|
|
|
|
};
|
2018-05-25 06:50:37 +01:00
|
|
|
|
2023-07-12 05:43:07 +01:00
|
|
|
u32 minimum_latency = 100 * native_sample_rate / 1000; // Firefox default
|
2018-09-09 08:37:34 +01:00
|
|
|
if (cubeb_get_min_latency(impl->ctx, ¶ms, &minimum_latency) != CUBEB_OK) {
|
2023-07-12 05:43:07 +01:00
|
|
|
LOG_WARNING(Audio_Sink,
|
|
|
|
"Error getting minimum output latency, falling back to default latency.");
|
2018-09-09 08:37:34 +01:00
|
|
|
}
|
2018-05-25 06:50:37 +01:00
|
|
|
|
2018-09-09 08:37:34 +01:00
|
|
|
cubeb_devid output_device = nullptr;
|
2018-07-12 15:52:06 +01:00
|
|
|
if (target_device_name != auto_device_name && !target_device_name.empty()) {
|
2018-07-02 14:03:14 +01:00
|
|
|
cubeb_device_collection collection;
|
2023-07-12 05:43:07 +01:00
|
|
|
if (cubeb_enumerate_devices(impl->ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) == CUBEB_OK) {
|
2018-09-14 17:20:48 +01:00
|
|
|
const auto collection_end{collection.device + collection.count};
|
|
|
|
const auto device{
|
|
|
|
std::find_if(collection.device, collection_end, [&](const cubeb_device_info& info) {
|
2018-10-26 05:32:41 +01:00
|
|
|
return info.friendly_name != nullptr &&
|
|
|
|
target_device_name == info.friendly_name;
|
2018-09-14 17:20:48 +01:00
|
|
|
})};
|
2018-07-02 14:03:14 +01:00
|
|
|
if (device != collection_end) {
|
|
|
|
output_device = device->devid;
|
2018-05-25 06:50:37 +01:00
|
|
|
}
|
2018-07-02 14:03:14 +01:00
|
|
|
cubeb_device_collection_destroy(impl->ctx, &collection);
|
2023-07-12 05:43:07 +01:00
|
|
|
} else {
|
|
|
|
LOG_WARNING(Audio_Sink,
|
|
|
|
"Audio output device enumeration not supported, using default device.");
|
2018-05-25 06:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 05:43:07 +01:00
|
|
|
auto stream_err = cubeb_stream_init(impl->ctx, &impl->stream, "CitraAudio", nullptr, nullptr,
|
|
|
|
output_device, ¶ms, std::max(512u, minimum_latency),
|
|
|
|
&Impl::DataCallback, &Impl::StateCallback, impl.get());
|
2018-09-09 08:37:34 +01:00
|
|
|
if (stream_err != CUBEB_OK) {
|
|
|
|
switch (stream_err) {
|
|
|
|
case CUBEB_ERROR:
|
|
|
|
default:
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream ({})", stream_err);
|
|
|
|
break;
|
|
|
|
case CUBEB_ERROR_INVALID_FORMAT:
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Invalid format when initializing cubeb stream");
|
|
|
|
break;
|
|
|
|
case CUBEB_ERROR_DEVICE_UNAVAILABLE:
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Device unavailable when initializing cubeb stream");
|
|
|
|
break;
|
|
|
|
}
|
2018-05-25 06:50:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cubeb_stream_start(impl->stream) != CUBEB_OK) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_CRITICAL(Audio_Sink, "Error starting cubeb stream");
|
2018-05-25 06:50:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CubebSink::~CubebSink() {
|
2023-07-12 05:43:07 +01:00
|
|
|
if (impl->stream) {
|
|
|
|
if (cubeb_stream_stop(impl->stream) != CUBEB_OK) {
|
|
|
|
LOG_ERROR(Audio_Sink, "Error stopping cubeb stream.");
|
|
|
|
}
|
|
|
|
cubeb_stream_destroy(impl->stream);
|
2018-09-09 08:37:34 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 05:43:07 +01:00
|
|
|
if (impl->ctx) {
|
|
|
|
cubeb_destroy(impl->ctx);
|
2018-05-25 06:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CubebSink::GetNativeSampleRate() const {
|
2023-07-12 05:43:07 +01:00
|
|
|
return native_sample_rate;
|
2018-05-25 06:50:37 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
void CubebSink::SetCallback(std::function<void(s16*, std::size_t)> cb) {
|
|
|
|
impl->cb = cb;
|
2018-05-25 06:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long CubebSink::Impl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
|
|
|
void* output_buffer, long num_frames) {
|
2020-12-07 15:06:16 +00:00
|
|
|
auto* impl = static_cast<Impl*>(user_data);
|
|
|
|
auto* buffer = static_cast<s16*>(output_buffer);
|
2018-05-25 06:50:37 +01:00
|
|
|
|
2018-09-09 08:37:34 +01:00
|
|
|
if (!impl || !impl->cb) {
|
2023-07-12 05:43:07 +01:00
|
|
|
LOG_DEBUG(Audio_Sink, "Missing internal data and/or audio callback, emitting zeroes.");
|
2018-09-09 08:37:34 +01:00
|
|
|
std::memset(output_buffer, 0, num_frames * 2 * sizeof(s16));
|
2023-07-12 05:43:07 +01:00
|
|
|
} else {
|
|
|
|
impl->cb(buffer, num_frames);
|
2018-09-09 08:37:34 +01:00
|
|
|
}
|
2018-05-25 06:50:37 +01:00
|
|
|
|
|
|
|
return num_frames;
|
|
|
|
}
|
|
|
|
|
2018-09-09 08:37:34 +01:00
|
|
|
void CubebSink::Impl::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {
|
|
|
|
switch (state) {
|
|
|
|
case CUBEB_STATE_STARTED:
|
|
|
|
LOG_INFO(Audio_Sink, "Cubeb Audio Stream Started");
|
|
|
|
break;
|
|
|
|
case CUBEB_STATE_STOPPED:
|
|
|
|
LOG_INFO(Audio_Sink, "Cubeb Audio Stream Stopped");
|
|
|
|
break;
|
|
|
|
case CUBEB_STATE_DRAINED:
|
|
|
|
LOG_INFO(Audio_Sink, "Cubeb Audio Stream Drained");
|
|
|
|
break;
|
|
|
|
case CUBEB_STATE_ERROR:
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Cubeb Audio Stream Errored");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CubebSink::Impl::LogCallback(char const* format, ...) {
|
2023-07-12 05:43:07 +01:00
|
|
|
std::array<char, 512> buffer{};
|
2018-09-09 08:37:34 +01:00
|
|
|
std::va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
vsprintf_s(buffer.data(), buffer.size(), format, args);
|
|
|
|
#else
|
|
|
|
vsnprintf(buffer.data(), buffer.size(), format, args);
|
|
|
|
#endif
|
|
|
|
va_end(args);
|
|
|
|
buffer.back() = '\0';
|
2020-08-20 19:22:50 +01:00
|
|
|
LOG_DEBUG(Audio_Sink, "{}", buffer.data());
|
2018-09-09 08:37:34 +01:00
|
|
|
}
|
2018-05-25 06:50:37 +01:00
|
|
|
|
2018-07-02 14:03:14 +01:00
|
|
|
std::vector<std::string> ListCubebSinkDevices() {
|
|
|
|
std::vector<std::string> device_list;
|
|
|
|
cubeb* ctx;
|
|
|
|
|
2023-07-12 05:43:07 +01:00
|
|
|
if (cubeb_init(&ctx, "Citra Output Device Enumerator", nullptr) != CUBEB_OK) {
|
2018-07-02 14:03:14 +01:00
|
|
|
LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_device_collection collection;
|
2023-07-12 05:43:07 +01:00
|
|
|
if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) == CUBEB_OK) {
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 0; i < collection.count; i++) {
|
2018-07-02 14:03:14 +01:00
|
|
|
const cubeb_device_info& device = collection.device[i];
|
2019-02-27 22:13:51 +00:00
|
|
|
if (device.state == CUBEB_DEVICE_STATE_ENABLED && device.friendly_name) {
|
2018-07-02 14:03:14 +01:00
|
|
|
device_list.emplace_back(device.friendly_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cubeb_device_collection_destroy(ctx, &collection);
|
2023-07-12 05:43:07 +01:00
|
|
|
} else {
|
|
|
|
LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported.");
|
2018-07-02 14:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_destroy(ctx);
|
|
|
|
return device_list;
|
|
|
|
}
|
|
|
|
|
2018-05-25 06:50:37 +01:00
|
|
|
} // namespace AudioCore
|