2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-28 17:40:50 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2022-08-01 01:58:13 +00:00
|
|
|
|
2022-07-16 22:48:45 +00:00
|
|
|
#include "audio_core/sink/sink_details.h"
|
2018-07-28 17:44:50 +00:00
|
|
|
#ifdef HAVE_CUBEB
|
2022-07-16 22:48:45 +00:00
|
|
|
#include "audio_core/sink/cubeb_sink.h"
|
2018-07-28 17:44:50 +00:00
|
|
|
#endif
|
2021-06-05 13:35:57 +00:00
|
|
|
#ifdef HAVE_SDL2
|
2022-07-16 22:48:45 +00:00
|
|
|
#include "audio_core/sink/sdl2_sink.h"
|
2021-06-05 13:35:57 +00:00
|
|
|
#endif
|
2022-08-01 01:58:13 +00:00
|
|
|
#include "audio_core/sink/null_sink.h"
|
2018-07-28 17:40:50 +00:00
|
|
|
#include "common/logging/log.h"
|
2023-05-18 21:54:22 +00:00
|
|
|
#include "common/settings_enums.h"
|
2018-07-28 17:40:50 +00:00
|
|
|
|
2022-07-16 22:48:45 +00:00
|
|
|
namespace AudioCore::Sink {
|
2018-12-13 21:23:31 +00:00
|
|
|
namespace {
|
|
|
|
struct SinkDetails {
|
|
|
|
using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
|
2022-07-16 22:48:45 +00:00
|
|
|
using ListDevicesFn = std::vector<std::string> (*)(bool);
|
2023-08-23 06:14:40 +00:00
|
|
|
using SuitableFn = bool (*)();
|
2018-07-28 17:40:50 +00:00
|
|
|
|
2018-12-13 21:23:31 +00:00
|
|
|
/// Name for this sink.
|
2023-05-18 21:54:22 +00:00
|
|
|
Settings::AudioEngine id;
|
2018-12-13 21:23:31 +00:00
|
|
|
/// A method to call to construct an instance of this type of sink.
|
|
|
|
FactoryFn factory;
|
|
|
|
/// A method to call to list available devices.
|
|
|
|
ListDevicesFn list_devices;
|
2023-08-23 06:14:40 +00:00
|
|
|
/// Check whether this backend is suitable to be used.
|
|
|
|
SuitableFn is_suitable;
|
2018-12-13 21:23:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// sink_details is ordered in terms of desirability, with the best choice at the top.
|
|
|
|
constexpr SinkDetails sink_details[] = {
|
2018-07-28 17:44:50 +00:00
|
|
|
#ifdef HAVE_CUBEB
|
2022-10-08 18:27:54 +00:00
|
|
|
SinkDetails{
|
2023-05-18 21:54:22 +00:00
|
|
|
Settings::AudioEngine::Cubeb,
|
2022-10-08 18:27:54 +00:00
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<CubebSink>(device_id);
|
|
|
|
},
|
|
|
|
&ListCubebSinkDevices,
|
2023-08-23 06:14:40 +00:00
|
|
|
&IsCubebSuitable,
|
2022-10-08 18:27:54 +00:00
|
|
|
},
|
2021-06-05 13:35:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SDL2
|
2022-10-08 18:27:54 +00:00
|
|
|
SinkDetails{
|
2023-05-18 21:54:22 +00:00
|
|
|
Settings::AudioEngine::Sdl2,
|
2022-10-08 18:27:54 +00:00
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<SDLSink>(device_id);
|
|
|
|
},
|
|
|
|
&ListSDLSinkDevices,
|
2023-08-23 06:14:40 +00:00
|
|
|
&IsSDLSuitable,
|
2022-10-08 18:27:54 +00:00
|
|
|
},
|
2018-07-28 17:44:50 +00:00
|
|
|
#endif
|
2023-08-23 06:14:40 +00:00
|
|
|
SinkDetails{
|
|
|
|
Settings::AudioEngine::Null,
|
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<NullSink>(device_id);
|
|
|
|
},
|
|
|
|
[](bool capture) { return std::vector<std::string>{"null"}; },
|
|
|
|
[]() { return true; },
|
|
|
|
},
|
2018-07-28 17:40:50 +00:00
|
|
|
};
|
|
|
|
|
2023-05-18 21:54:22 +00:00
|
|
|
const SinkDetails& GetOutputSinkDetails(Settings::AudioEngine sink_id) {
|
|
|
|
const auto find_backend{[](Settings::AudioEngine id) {
|
2022-10-08 18:27:54 +00:00
|
|
|
return std::find_if(std::begin(sink_details), std::end(sink_details),
|
|
|
|
[&id](const auto& sink_detail) { return sink_detail.id == id; });
|
|
|
|
}};
|
2018-07-28 17:40:50 +00:00
|
|
|
|
2022-10-08 18:27:54 +00:00
|
|
|
auto iter = find_backend(sink_id);
|
|
|
|
|
2023-05-18 21:54:22 +00:00
|
|
|
if (sink_id == Settings::AudioEngine::Auto) {
|
2023-08-23 06:14:40 +00:00
|
|
|
// Auto-select a backend. Use the sink details ordering, preferring cubeb first, checking
|
|
|
|
// that the backend is available and suitable to use.
|
|
|
|
for (auto& details : sink_details) {
|
|
|
|
if (details.is_suitable()) {
|
|
|
|
iter = &details;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 16:11:10 +00:00
|
|
|
LOG_INFO(Service_Audio, "Auto-selecting the {} backend",
|
|
|
|
Settings::CanonicalizeEnum(iter->id));
|
2023-08-23 06:14:40 +00:00
|
|
|
} else {
|
|
|
|
if (iter != std::end(sink_details) && !iter->is_suitable()) {
|
|
|
|
LOG_ERROR(Service_Audio, "Selected backend {} is not suitable, falling back to null",
|
|
|
|
Settings::CanonicalizeEnum(iter->id));
|
|
|
|
iter = find_backend(Settings::AudioEngine::Null);
|
2018-07-28 17:40:50 +00:00
|
|
|
}
|
2022-10-08 18:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == std::end(sink_details)) {
|
2023-06-07 05:52:23 +00:00
|
|
|
LOG_ERROR(Audio, "Invalid sink_id {}", Settings::CanonicalizeEnum(sink_id));
|
2023-05-18 21:54:22 +00:00
|
|
|
iter = find_backend(Settings::AudioEngine::Null);
|
2018-07-28 17:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *iter;
|
|
|
|
}
|
2018-12-13 21:23:31 +00:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2023-05-18 21:54:22 +00:00
|
|
|
std::vector<Settings::AudioEngine> GetSinkIDs() {
|
|
|
|
std::vector<Settings::AudioEngine> sink_ids(std::size(sink_details));
|
2018-12-13 21:23:31 +00:00
|
|
|
|
|
|
|
std::transform(std::begin(sink_details), std::end(sink_details), std::begin(sink_ids),
|
|
|
|
[](const auto& sink) { return sink.id; });
|
|
|
|
|
|
|
|
return sink_ids;
|
|
|
|
}
|
|
|
|
|
2023-05-18 21:54:22 +00:00
|
|
|
std::vector<std::string> GetDeviceListForSink(Settings::AudioEngine sink_id, bool capture) {
|
2022-07-16 22:48:45 +00:00
|
|
|
return GetOutputSinkDetails(sink_id).list_devices(capture);
|
2018-12-13 21:23:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 21:54:22 +00:00
|
|
|
std::unique_ptr<Sink> CreateSinkFromID(Settings::AudioEngine sink_id, std::string_view device_id) {
|
2022-07-16 22:48:45 +00:00
|
|
|
return GetOutputSinkDetails(sink_id).factory(device_id);
|
2018-12-13 21:23:31 +00:00
|
|
|
}
|
2018-07-28 17:40:50 +00:00
|
|
|
|
2022-07-16 22:48:45 +00:00
|
|
|
} // namespace AudioCore::Sink
|