summaryrefslogtreecommitdiff
path: root/src/audio_core/sink_details.cpp
diff options
context:
space:
mode:
authorGravatar Lioncash2018-12-13 16:23:31 -0500
committerGravatar Lioncash2018-12-13 16:44:32 -0500
commit6beb823f150c965adf057c3fd423aa23dcb77997 (patch)
treeba63f98b43c3d674aa719b018cf1fb4faa70a650 /src/audio_core/sink_details.cpp
parentMerge pull request #1846 from lioncash/dir (diff)
downloadyuzu-6beb823f150c965adf057c3fd423aa23dcb77997.tar.gz
yuzu-6beb823f150c965adf057c3fd423aa23dcb77997.tar.xz
yuzu-6beb823f150c965adf057c3fd423aa23dcb77997.zip
audio_core: Make g_sink_details internally linked
We can hide the direct array from external view and instead provide functions to retrieve the necessary info. This has the benefit of completely hiding the makeup of the SinkDetails structure from the rest of the code. Given that this makes the array hidden, we can also make the array constexpr by altering the members slightly. This gets rid of several static constructor calls related to std::vector and std::function. Now we don't have heap allocations here that need to occur before the program can even enter main(). It also has the benefit of saving a little bit of heap space, but this doesn't matter too much, since the savings in that regard are pretty tiny.
Diffstat (limited to 'src/audio_core/sink_details.cpp')
-rw-r--r--src/audio_core/sink_details.cpp53
1 files changed, 45 insertions, 8 deletions
diff --git a/src/audio_core/sink_details.cpp b/src/audio_core/sink_details.cpp
index 67cf1f3b2..a848eb1c9 100644
--- a/src/audio_core/sink_details.cpp
+++ b/src/audio_core/sink_details.cpp
@@ -14,31 +14,68 @@
14#include "common/logging/log.h" 14#include "common/logging/log.h"
15 15
16namespace AudioCore { 16namespace AudioCore {
17namespace {
18struct SinkDetails {
19 using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
20 using ListDevicesFn = std::vector<std::string> (*)();
17 21
18// g_sink_details is ordered in terms of desirability, with the best choice at the top. 22 /// Name for this sink.
19const std::vector<SinkDetails> g_sink_details = { 23 const char* id;
24 /// A method to call to construct an instance of this type of sink.
25 FactoryFn factory;
26 /// A method to call to list available devices.
27 ListDevicesFn list_devices;
28};
29
30// sink_details is ordered in terms of desirability, with the best choice at the top.
31constexpr SinkDetails sink_details[] = {
20#ifdef HAVE_CUBEB 32#ifdef HAVE_CUBEB
21 SinkDetails{"cubeb", &std::make_unique<CubebSink, std::string>, &ListCubebSinkDevices}, 33 SinkDetails{"cubeb",
34 [](std::string_view device_id) -> std::unique_ptr<Sink> {
35 return std::make_unique<CubebSink>(device_id);
36 },
37 &ListCubebSinkDevices},
22#endif 38#endif
23 SinkDetails{"null", &std::make_unique<NullSink, std::string>, 39 SinkDetails{"null",
40 [](std::string_view device_id) -> std::unique_ptr<Sink> {
41 return std::make_unique<NullSink>(device_id);
42 },
24 [] { return std::vector<std::string>{"null"}; }}, 43 [] { return std::vector<std::string>{"null"}; }},
25}; 44};
26 45
27const SinkDetails& GetSinkDetails(std::string_view sink_id) { 46const SinkDetails& GetSinkDetails(std::string_view sink_id) {
28 auto iter = 47 auto iter =
29 std::find_if(g_sink_details.begin(), g_sink_details.end(), 48 std::find_if(std::begin(sink_details), std::end(sink_details),
30 [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; }); 49 [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; });
31 50
32 if (sink_id == "auto" || iter == g_sink_details.end()) { 51 if (sink_id == "auto" || iter == std::end(sink_details)) {
33 if (sink_id != "auto") { 52 if (sink_id != "auto") {
34 LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id {}", sink_id); 53 LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id {}", sink_id);
35 } 54 }
36 // Auto-select. 55 // Auto-select.
37 // g_sink_details is ordered in terms of desirability, with the best choice at the front. 56 // sink_details is ordered in terms of desirability, with the best choice at the front.
38 iter = g_sink_details.begin(); 57 iter = std::begin(sink_details);
39 } 58 }
40 59
41 return *iter; 60 return *iter;
42} 61}
62} // Anonymous namespace
63
64std::vector<const char*> GetSinkIDs() {
65 std::vector<const char*> sink_ids(std::size(sink_details));
66
67 std::transform(std::begin(sink_details), std::end(sink_details), std::begin(sink_ids),
68 [](const auto& sink) { return sink.id; });
69
70 return sink_ids;
71}
72
73std::vector<std::string> GetDeviceListForSink(std::string_view sink_id) {
74 return GetSinkDetails(sink_id).list_devices();
75}
76
77std::unique_ptr<Sink> CreateSinkFromID(std::string_view sink_id, std::string_view device_id) {
78 return GetSinkDetails(sink_id).factory(device_id);
79}
43 80
44} // namespace AudioCore 81} // namespace AudioCore