2016-04-28 15:28:59 +02:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-01-26 04:33:26 +01:00
|
|
|
#include <algorithm>
|
2016-04-28 15:28:59 +02:00
|
|
|
#include <memory>
|
2017-12-20 19:44:32 +01:00
|
|
|
#include <string>
|
2016-04-28 15:28:59 +02:00
|
|
|
#include <vector>
|
|
|
|
#include "audio_core/null_sink.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "audio_core/sink_details.h"
|
2016-04-27 11:57:29 +02:00
|
|
|
#ifdef HAVE_SDL2
|
|
|
|
#include "audio_core/sdl2_sink.h"
|
|
|
|
#endif
|
2018-05-25 07:58:53 +02:00
|
|
|
#ifdef HAVE_CUBEB
|
2018-05-25 07:50:37 +02:00
|
|
|
#include "audio_core/cubeb_sink.h"
|
2018-05-25 07:58:53 +02:00
|
|
|
#endif
|
2023-05-01 21:17:45 +02:00
|
|
|
#ifdef HAVE_OPENAL
|
|
|
|
#include "audio_core/openal_sink.h"
|
|
|
|
#endif
|
2017-01-26 04:33:26 +01:00
|
|
|
#include "common/logging/log.h"
|
2016-04-27 11:57:29 +02:00
|
|
|
|
2016-04-28 15:28:59 +02:00
|
|
|
namespace AudioCore {
|
2018-12-13 22:23:31 +01:00
|
|
|
namespace {
|
|
|
|
struct SinkDetails {
|
|
|
|
using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
|
|
|
|
using ListDevicesFn = std::vector<std::string> (*)();
|
2016-04-28 15:28:59 +02:00
|
|
|
|
2023-05-01 21:17:45 +02:00
|
|
|
/// Type of this sink.
|
|
|
|
SinkType type;
|
2018-12-13 22:23:31 +01:00
|
|
|
/// Name for this sink.
|
2023-05-01 21:17:45 +02:00
|
|
|
std::string_view name;
|
2018-12-13 22:23:31 +01: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// sink_details is ordered in terms of desirability, with the best choice at the top.
|
2023-05-01 21:17:45 +02:00
|
|
|
constexpr std::array sink_details = {
|
2018-05-25 07:58:53 +02:00
|
|
|
#ifdef HAVE_CUBEB
|
2023-05-01 21:17:45 +02:00
|
|
|
SinkDetails{SinkType::Cubeb, "Cubeb",
|
2018-12-13 22:23:31 +01:00
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<CubebSink>(device_id);
|
|
|
|
},
|
|
|
|
&ListCubebSinkDevices},
|
2018-05-25 07:58:53 +02:00
|
|
|
#endif
|
2023-05-01 21:17:45 +02:00
|
|
|
#ifdef HAVE_OPENAL
|
|
|
|
SinkDetails{SinkType::OpenAL, "OpenAL",
|
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<OpenALSink>(std::string(device_id));
|
|
|
|
},
|
|
|
|
&ListOpenALSinkDevices},
|
|
|
|
#endif
|
2016-04-27 11:57:29 +02:00
|
|
|
#ifdef HAVE_SDL2
|
2023-05-01 21:17:45 +02:00
|
|
|
SinkDetails{SinkType::SDL2, "SDL2",
|
2018-12-13 22:23:31 +01:00
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<SDL2Sink>(std::string(device_id));
|
|
|
|
},
|
|
|
|
&ListSDL2SinkDevices},
|
2016-04-27 11:57:29 +02:00
|
|
|
#endif
|
2023-05-01 21:17:45 +02:00
|
|
|
SinkDetails{SinkType::Null, "None",
|
2018-12-13 22:23:31 +01:00
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<NullSink>(device_id);
|
|
|
|
},
|
2023-05-01 21:17:45 +02:00
|
|
|
[] { return std::vector<std::string>{"None"}; }},
|
2016-04-28 15:28:59 +02:00
|
|
|
};
|
|
|
|
|
2023-05-01 21:17:45 +02:00
|
|
|
const SinkDetails& GetSinkDetails(SinkType sink_type) {
|
|
|
|
auto iter = std::find_if(
|
|
|
|
sink_details.begin(), sink_details.end(),
|
|
|
|
[sink_type](const auto& sink_detail) { return sink_detail.type == sink_type; });
|
2017-01-26 04:33:26 +01:00
|
|
|
|
2023-05-01 21:17:45 +02:00
|
|
|
if (sink_type == SinkType::Auto || iter == sink_details.end()) {
|
|
|
|
if (sink_type != SinkType::Auto) {
|
|
|
|
LOG_ERROR(Audio, "AudioCore::GetSinkDetails given invalid sink_type {}", sink_type);
|
2017-01-26 04:33:26 +01:00
|
|
|
}
|
|
|
|
// Auto-select.
|
2018-12-13 22:23:31 +01:00
|
|
|
// sink_details is ordered in terms of desirability, with the best choice at the front.
|
2023-05-01 21:17:45 +02:00
|
|
|
iter = sink_details.begin();
|
2017-01-26 04:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *iter;
|
|
|
|
}
|
2018-12-13 22:23:31 +01:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2023-05-01 21:17:45 +02:00
|
|
|
std::string_view GetSinkName(SinkType sink_type) {
|
|
|
|
if (sink_type == SinkType::Auto) {
|
|
|
|
return "Auto";
|
|
|
|
}
|
|
|
|
return GetSinkDetails(sink_type).name;
|
2018-12-13 22:23:31 +01:00
|
|
|
}
|
|
|
|
|
2023-05-01 21:17:45 +02:00
|
|
|
std::vector<std::string> GetDeviceListForSink(SinkType sink_type) {
|
|
|
|
return GetSinkDetails(sink_type).list_devices();
|
2018-12-13 22:23:31 +01:00
|
|
|
}
|
|
|
|
|
2023-05-01 21:17:45 +02:00
|
|
|
std::unique_ptr<Sink> CreateSinkFromID(SinkType sink_type, std::string_view device_id) {
|
|
|
|
return GetSinkDetails(sink_type).factory(device_id);
|
2018-12-13 22:23:31 +01:00
|
|
|
}
|
2017-01-26 04:33:26 +01:00
|
|
|
|
2016-04-28 15:28:59 +02:00
|
|
|
} // namespace AudioCore
|