sink_details.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifdef HAVE_SDL2
  14. #include "audio_core/sdl2_sink.h"
  15. #endif
  16. #include "common/logging/log.h"
  17. namespace AudioCore {
  18. namespace {
  19. struct SinkDetails {
  20. using FactoryFn = std::unique_ptr<Sink> (*)(std::string_view);
  21. using ListDevicesFn = std::vector<std::string> (*)();
  22. /// Name for this sink.
  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. // sink_details is ordered in terms of desirability, with the best choice at the top.
  30. constexpr SinkDetails sink_details[] = {
  31. #ifdef HAVE_CUBEB
  32. SinkDetails{"cubeb",
  33. [](std::string_view device_id) -> std::unique_ptr<Sink> {
  34. return std::make_unique<CubebSink>(device_id);
  35. },
  36. &ListCubebSinkDevices},
  37. #endif
  38. #ifdef HAVE_SDL2
  39. SinkDetails{"sdl2",
  40. [](std::string_view device_id) -> std::unique_ptr<Sink> {
  41. return std::make_unique<SDLSink>(device_id);
  42. },
  43. &ListSDLSinkDevices},
  44. #endif
  45. SinkDetails{"null",
  46. [](std::string_view device_id) -> std::unique_ptr<Sink> {
  47. return std::make_unique<NullSink>(device_id);
  48. },
  49. [] { return std::vector<std::string>{"null"}; }},
  50. };
  51. const SinkDetails& GetSinkDetails(std::string_view sink_id) {
  52. auto iter =
  53. std::find_if(std::begin(sink_details), std::end(sink_details),
  54. [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; });
  55. if (sink_id == "auto" || iter == std::end(sink_details)) {
  56. if (sink_id != "auto") {
  57. LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id {}", sink_id);
  58. }
  59. // Auto-select.
  60. // sink_details is ordered in terms of desirability, with the best choice at the front.
  61. iter = std::begin(sink_details);
  62. }
  63. return *iter;
  64. }
  65. } // Anonymous namespace
  66. std::vector<const char*> GetSinkIDs() {
  67. std::vector<const char*> sink_ids(std::size(sink_details));
  68. std::transform(std::begin(sink_details), std::end(sink_details), std::begin(sink_ids),
  69. [](const auto& sink) { return sink.id; });
  70. return sink_ids;
  71. }
  72. std::vector<std::string> GetDeviceListForSink(std::string_view sink_id) {
  73. return GetSinkDetails(sink_id).list_devices();
  74. }
  75. std::unique_ptr<Sink> CreateSinkFromID(std::string_view sink_id, std::string_view device_id) {
  76. return GetSinkDetails(sink_id).factory(device_id);
  77. }
  78. } // namespace AudioCore