audout_u.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <vector>
  5. #include "common/logging/log.h"
  6. #include "core/core_timing.h"
  7. #include "core/hle/ipc_helpers.h"
  8. #include "core/hle/kernel/event.h"
  9. #include "core/hle/kernel/hle_ipc.h"
  10. #include "core/hle/service/audio/audout_u.h"
  11. namespace Service {
  12. namespace Audio {
  13. /// Switch sample rate frequency
  14. constexpr u32 sample_rate{48000};
  15. /// TODO(st4rk): dynamic number of channels, as I think Switch has support
  16. /// to more audio channels (probably when Docked I guess)
  17. constexpr u32 audio_channels{2};
  18. /// TODO(st4rk): find a proper value for the audio_ticks
  19. constexpr u64 audio_ticks{static_cast<u64>(BASE_CLOCK_RATE / 500)};
  20. class IAudioOut final : public ServiceFramework<IAudioOut> {
  21. public:
  22. IAudioOut() : ServiceFramework("IAudioOut"), audio_out_state(AudioState::Stopped) {
  23. static const FunctionInfo functions[] = {
  24. {0x0, nullptr, "GetAudioOutState"},
  25. {0x1, &IAudioOut::StartAudioOut, "StartAudioOut"},
  26. {0x2, &IAudioOut::StopAudioOut, "StopAudioOut"},
  27. {0x3, &IAudioOut::AppendAudioOutBuffer_1, "AppendAudioOutBuffer_1"},
  28. {0x4, &IAudioOut::RegisterBufferEvent, "RegisterBufferEvent"},
  29. {0x5, &IAudioOut::GetReleasedAudioOutBuffer_1, "GetReleasedAudioOutBuffer_1"},
  30. {0x6, nullptr, "ContainsAudioOutBuffer"},
  31. {0x7, nullptr, "AppendAudioOutBuffer_2"},
  32. {0x8, nullptr, "GetReleasedAudioOutBuffer_2"},
  33. };
  34. RegisterHandlers(functions);
  35. // This is the event handle used to check if the audio buffer was released
  36. buffer_event =
  37. Kernel::Event::Create(Kernel::ResetType::OneShot, "IAudioOutBufferReleasedEvent");
  38. // Register event callback to update the Audio Buffer
  39. audio_event = CoreTiming::RegisterEvent(
  40. "IAudioOut::UpdateAudioBuffersCallback", [this](u64 userdata, int cycles_late) {
  41. UpdateAudioBuffersCallback();
  42. CoreTiming::ScheduleEvent(audio_ticks - cycles_late, audio_event);
  43. });
  44. // Start the audio event
  45. CoreTiming::ScheduleEvent(audio_ticks, audio_event);
  46. }
  47. ~IAudioOut() = default;
  48. private:
  49. void StartAudioOut(Kernel::HLERequestContext& ctx) {
  50. LOG_WARNING(Service_Audio, "(STUBBED) called");
  51. // Start audio
  52. audio_out_state = AudioState::Started;
  53. IPC::ResponseBuilder rb{ctx, 2};
  54. rb.Push(RESULT_SUCCESS);
  55. }
  56. void StopAudioOut(Kernel::HLERequestContext& ctx) {
  57. LOG_WARNING(Service_Audio, "(STUBBED) called");
  58. // Stop audio
  59. audio_out_state = AudioState::Stopped;
  60. queue_keys.clear();
  61. IPC::ResponseBuilder rb{ctx, 2};
  62. rb.Push(RESULT_SUCCESS);
  63. }
  64. void RegisterBufferEvent(Kernel::HLERequestContext& ctx) {
  65. LOG_WARNING(Service_Audio, "(STUBBED) called");
  66. IPC::ResponseBuilder rb{ctx, 2, 1};
  67. rb.Push(RESULT_SUCCESS);
  68. rb.PushCopyObjects(buffer_event);
  69. }
  70. void AppendAudioOutBuffer_1(Kernel::HLERequestContext& ctx) {
  71. LOG_WARNING(Service_Audio, "(STUBBED) called");
  72. IPC::RequestParser rp{ctx};
  73. const u64 key{rp.Pop<u64>()};
  74. queue_keys.insert(queue_keys.begin(), key);
  75. IPC::ResponseBuilder rb{ctx, 2};
  76. rb.Push(RESULT_SUCCESS);
  77. }
  78. void GetReleasedAudioOutBuffer_1(Kernel::HLERequestContext& ctx) {
  79. LOG_WARNING(Service_Audio, "(STUBBED) called");
  80. // TODO(st4rk): This is how libtransistor currently implements the
  81. // GetReleasedAudioOutBuffer, it should return the key (a VAddr) to the app and this address
  82. // is used to know which buffer should be filled with data and send again to the service
  83. // through AppendAudioOutBuffer. Check if this is the proper way to do it.
  84. u64 key{0};
  85. if (queue_keys.size()) {
  86. key = queue_keys.back();
  87. queue_keys.pop_back();
  88. }
  89. ctx.WriteBuffer(&key, sizeof(u64));
  90. IPC::ResponseBuilder rb{ctx, 3};
  91. rb.Push(RESULT_SUCCESS);
  92. // TODO(st4rk): This might be the total of released buffers, needs to be verified on
  93. // hardware
  94. rb.Push<u32>(static_cast<u32>(queue_keys.size()));
  95. }
  96. void UpdateAudioBuffersCallback() {
  97. if (audio_out_state != AudioState::Started) {
  98. return;
  99. }
  100. if (queue_keys.empty()) {
  101. return;
  102. }
  103. buffer_event->Signal();
  104. }
  105. enum class AudioState : u32 {
  106. Started,
  107. Stopped,
  108. };
  109. /// This is used to trigger the audio event callback that is going to read the samples from the
  110. /// audio_buffer list and enqueue the samples using the sink (audio_core).
  111. CoreTiming::EventType* audio_event;
  112. /// This is the evend handle used to check if the audio buffer was released
  113. Kernel::SharedPtr<Kernel::Event> buffer_event;
  114. /// (st4rk): This is just a temporary workaround for the future implementation. Libtransistor
  115. /// uses the key as an address in the App, so we need to return when the
  116. /// GetReleasedAudioOutBuffer_1 is called, otherwise we'll run in problems, because
  117. /// libtransistor uses the key returned as an pointer.
  118. std::vector<u64> queue_keys;
  119. AudioState audio_out_state;
  120. };
  121. void AudOutU::ListAudioOuts(Kernel::HLERequestContext& ctx) {
  122. LOG_WARNING(Service_Audio, "(STUBBED) called");
  123. IPC::RequestParser rp{ctx};
  124. const std::string audio_interface = "AudioInterface";
  125. ctx.WriteBuffer(audio_interface.c_str(), audio_interface.size());
  126. IPC::ResponseBuilder rb = rp.MakeBuilder(3, 0, 0);
  127. rb.Push(RESULT_SUCCESS);
  128. // TODO(st4rk): We're currently returning only one audio interface (stringlist size). However,
  129. // it's highly possible to have more than one interface (despite that libtransistor requires
  130. // only one).
  131. rb.Push<u32>(1);
  132. }
  133. void AudOutU::OpenAudioOut(Kernel::HLERequestContext& ctx) {
  134. LOG_WARNING(Service_Audio, "(STUBBED) called");
  135. if (!audio_out_interface) {
  136. audio_out_interface = std::make_shared<IAudioOut>();
  137. }
  138. IPC::ResponseBuilder rb{ctx, 6, 0, 1};
  139. rb.Push(RESULT_SUCCESS);
  140. rb.Push<u32>(sample_rate);
  141. rb.Push<u32>(audio_channels);
  142. rb.Push<u32>(static_cast<u32>(PcmFormat::Int16));
  143. rb.Push<u32>(0); // This field is unknown
  144. rb.PushIpcInterface<Audio::IAudioOut>(audio_out_interface);
  145. }
  146. AudOutU::AudOutU() : ServiceFramework("audout:u") {
  147. static const FunctionInfo functions[] = {{0x00000000, &AudOutU::ListAudioOuts, "ListAudioOuts"},
  148. {0x00000001, &AudOutU::OpenAudioOut, "OpenAudioOut"},
  149. {0x00000002, nullptr, "Unknown2"},
  150. {0x00000003, nullptr, "Unknown3"}};
  151. RegisterHandlers(functions);
  152. }
  153. } // namespace Audio
  154. } // namespace Service