2018-07-28 20:40:50 +03:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "audio_core/null_sink.h"
|
|
|
|
#include "audio_core/sink_details.h"
|
2018-07-28 20:44:50 +03:00
|
|
|
#ifdef HAVE_CUBEB
|
|
|
|
#include "audio_core/cubeb_sink.h"
|
|
|
|
#endif
|
2018-07-28 20:40:50 +03:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
namespace AudioCore {
|
2018-12-14 00:23:31 +03:00
|
|
|
namespace {
|
|
|
|
struct SinkDetails {
|
|
|
|
using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
|
|
|
|
using ListDevicesFn = std::vector<std::string> (*)();
|
2018-07-28 20:40:50 +03:00
|
|
|
|
2018-12-14 00:23:31 +03:00
|
|
|
/// Name for this sink.
|
|
|
|
const char* id;
|
|
|
|
/// 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.
|
|
|
|
constexpr SinkDetails sink_details[] = {
|
2018-07-28 20:44:50 +03:00
|
|
|
#ifdef HAVE_CUBEB
|
2018-12-14 00:23:31 +03:00
|
|
|
SinkDetails{"cubeb",
|
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<CubebSink>(device_id);
|
|
|
|
},
|
|
|
|
&ListCubebSinkDevices},
|
2018-07-28 20:44:50 +03:00
|
|
|
#endif
|
2018-12-14 00:23:31 +03:00
|
|
|
SinkDetails{"null",
|
|
|
|
[](std::string_view device_id) -> std::unique_ptr<Sink> {
|
|
|
|
return std::make_unique<NullSink>(device_id);
|
|
|
|
},
|
2018-07-28 20:40:50 +03:00
|
|
|
[] { return std::vector<std::string>{"null"}; }},
|
|
|
|
};
|
|
|
|
|
2018-09-12 04:36:07 +03:00
|
|
|
const SinkDetails& GetSinkDetails(std::string_view sink_id) {
|
2018-07-28 20:40:50 +03:00
|
|
|
auto iter =
|
2018-12-14 00:23:31 +03:00
|
|
|
std::find_if(std::begin(sink_details), std::end(sink_details),
|
2018-07-28 20:40:50 +03:00
|
|
|
[sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; });
|
|
|
|
|
2018-12-14 00:23:31 +03:00
|
|
|
if (sink_id == "auto" || iter == std::end(sink_details)) {
|
2018-07-28 20:40:50 +03:00
|
|
|
if (sink_id != "auto") {
|
|
|
|
LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id {}", sink_id);
|
|
|
|
}
|
|
|
|
// Auto-select.
|
2018-12-14 00:23:31 +03:00
|
|
|
// sink_details is ordered in terms of desirability, with the best choice at the front.
|
|
|
|
iter = std::begin(sink_details);
|
2018-07-28 20:40:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return *iter;
|
|
|
|
}
|
2018-12-14 00:23:31 +03:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
std::vector<const char*> GetSinkIDs() {
|
|
|
|
std::vector<const char*> sink_ids(std::size(sink_details));
|
|
|
|
|
|
|
|
std::transform(std::begin(sink_details), std::end(sink_details), std::begin(sink_ids),
|
|
|
|
[](const auto& sink) { return sink.id; });
|
|
|
|
|
|
|
|
return sink_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> GetDeviceListForSink(std::string_view sink_id) {
|
|
|
|
return GetSinkDetails(sink_id).list_devices();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Sink> CreateSinkFromID(std::string_view sink_id, std::string_view device_id) {
|
|
|
|
return GetSinkDetails(sink_id).factory(device_id);
|
|
|
|
}
|
2018-07-28 20:40:50 +03:00
|
|
|
|
|
|
|
} // namespace AudioCore
|