sink_details.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "audio_core/null_sink.h"
  9. #include "audio_core/sink_details.h"
  10. #ifdef HAVE_CUBEB
  11. #include "audio_core/cubeb_sink.h"
  12. #endif
  13. #include "common/logging/log.h"
  14. namespace AudioCore {
  15. namespace {
  16. struct SinkDetails {
  17. using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
  18. using ListDevicesFn = std::vector<std::string> (*)();
  19. /// Name for this sink.
  20. const char* id;
  21. /// A method to call to construct an instance of this type of sink.
  22. FactoryFn factory;
  23. /// A method to call to list available devices.
  24. ListDevicesFn list_devices;
  25. };
  26. // sink_details is ordered in terms of desirability, with the best choice at the top.
  27. constexpr SinkDetails sink_details[] = {
  28. #ifdef HAVE_CUBEB
  29. SinkDetails{"cubeb",
  30. [](std::string_view device_id) -> std::unique_ptr<Sink> {
  31. return std::make_unique<CubebSink>(device_id);
  32. },
  33. &ListCubebSinkDevices},
  34. #endif
  35. SinkDetails{"null",
  36. [](std::string_view device_id) -> std::unique_ptr<Sink> {
  37. return std::make_unique<NullSink>(device_id);
  38. },
  39. [] { return std::vector<std::string>{"null"}; }},
  40. };
  41. const SinkDetails& GetSinkDetails(std::string_view sink_id) {
  42. auto iter =
  43. std::find_if(std::begin(sink_details), std::end(sink_details),
  44. [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; });
  45. if (sink_id == "auto" || iter == std::end(sink_details)) {
  46. if (sink_id != "auto") {
  47. LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id {}", sink_id);
  48. }
  49. // Auto-select.
  50. // sink_details is ordered in terms of desirability, with the best choice at the front.
  51. iter = std::begin(sink_details);
  52. }
  53. return *iter;
  54. }
  55. } // Anonymous namespace
  56. std::vector<const char*> GetSinkIDs() {
  57. std::vector<const char*> sink_ids(std::size(sink_details));
  58. std::transform(std::begin(sink_details), std::end(sink_details), std::begin(sink_ids),
  59. [](const auto& sink) { return sink.id; });
  60. return sink_ids;
  61. }
  62. std::vector<std::string> GetDeviceListForSink(std::string_view sink_id) {
  63. return GetSinkDetails(sink_id).list_devices();
  64. }
  65. std::unique_ptr<Sink> CreateSinkFromID(std::string_view sink_id, std::string_view device_id) {
  66. return GetSinkDetails(sink_id).factory(device_id);
  67. }
  68. } // namespace AudioCore