audout_u.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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() {
  48. CoreTiming::UnscheduleEvent(audio_event, 0);
  49. }
  50. private:
  51. void StartAudioOut(Kernel::HLERequestContext& ctx) {
  52. LOG_WARNING(Service_Audio, "(STUBBED) called");
  53. // Start audio
  54. audio_out_state = AudioState::Started;
  55. IPC::ResponseBuilder rb{ctx, 2};
  56. rb.Push(RESULT_SUCCESS);
  57. }
  58. void StopAudioOut(Kernel::HLERequestContext& ctx) {
  59. LOG_WARNING(Service_Audio, "(STUBBED) called");
  60. // Stop audio
  61. audio_out_state = AudioState::Stopped;
  62. queue_keys.clear();
  63. IPC::ResponseBuilder rb{ctx, 2};
  64. rb.Push(RESULT_SUCCESS);
  65. }
  66. void RegisterBufferEvent(Kernel::HLERequestContext& ctx) {
  67. LOG_WARNING(Service_Audio, "(STUBBED) called");
  68. IPC::ResponseBuilder rb{ctx, 2, 1};
  69. rb.Push(RESULT_SUCCESS);
  70. rb.PushCopyObjects(buffer_event);
  71. }
  72. void AppendAudioOutBuffer_1(Kernel::HLERequestContext& ctx) {
  73. LOG_WARNING(Service_Audio, "(STUBBED) called");
  74. IPC::RequestParser rp{ctx};
  75. const u64 key{rp.Pop<u64>()};
  76. queue_keys.insert(queue_keys.begin(), key);
  77. IPC::ResponseBuilder rb{ctx, 2};
  78. rb.Push(RESULT_SUCCESS);
  79. }
  80. void GetReleasedAudioOutBuffer_1(Kernel::HLERequestContext& ctx) {
  81. LOG_WARNING(Service_Audio, "(STUBBED) called");
  82. // TODO(st4rk): This is how libtransistor currently implements the
  83. // GetReleasedAudioOutBuffer, it should return the key (a VAddr) to the app and this address
  84. // is used to know which buffer should be filled with data and send again to the service
  85. // through AppendAudioOutBuffer. Check if this is the proper way to do it.
  86. u64 key{0};
  87. if (queue_keys.size()) {
  88. key = queue_keys.back();
  89. queue_keys.pop_back();
  90. }
  91. ctx.WriteBuffer(&key, sizeof(u64));
  92. IPC::ResponseBuilder rb{ctx, 3};
  93. rb.Push(RESULT_SUCCESS);
  94. // TODO(st4rk): This might be the total of released buffers, needs to be verified on
  95. // hardware
  96. rb.Push<u32>(static_cast<u32>(queue_keys.size()));
  97. }
  98. void UpdateAudioBuffersCallback() {
  99. if (audio_out_state != AudioState::Started) {
  100. return;
  101. }
  102. if (queue_keys.empty()) {
  103. return;
  104. }
  105. buffer_event->Signal();
  106. }
  107. enum class AudioState : u32 {
  108. Started,
  109. Stopped,
  110. };
  111. /// This is used to trigger the audio event callback that is going to read the samples from the
  112. /// audio_buffer list and enqueue the samples using the sink (audio_core).
  113. CoreTiming::EventType* audio_event;
  114. /// This is the evend handle used to check if the audio buffer was released
  115. Kernel::SharedPtr<Kernel::Event> buffer_event;
  116. /// (st4rk): This is just a temporary workaround for the future implementation. Libtransistor
  117. /// uses the key as an address in the App, so we need to return when the
  118. /// GetReleasedAudioOutBuffer_1 is called, otherwise we'll run in problems, because
  119. /// libtransistor uses the key returned as an pointer.
  120. std::vector<u64> queue_keys;
  121. AudioState audio_out_state;
  122. };
  123. void AudOutU::ListAudioOuts(Kernel::HLERequestContext& ctx) {
  124. LOG_WARNING(Service_Audio, "(STUBBED) called");
  125. IPC::RequestParser rp{ctx};
  126. const std::string audio_interface = "AudioInterface";
  127. ctx.WriteBuffer(audio_interface.c_str(), audio_interface.size());
  128. IPC::ResponseBuilder rb = rp.MakeBuilder(3, 0, 0);
  129. rb.Push(RESULT_SUCCESS);
  130. // TODO(st4rk): We're currently returning only one audio interface (stringlist size). However,
  131. // it's highly possible to have more than one interface (despite that libtransistor requires
  132. // only one).
  133. rb.Push<u32>(1);
  134. }
  135. void AudOutU::OpenAudioOut(Kernel::HLERequestContext& ctx) {
  136. LOG_WARNING(Service_Audio, "(STUBBED) called");
  137. if (!audio_out_interface) {
  138. audio_out_interface = std::make_shared<IAudioOut>();
  139. }
  140. IPC::ResponseBuilder rb{ctx, 6, 0, 1};
  141. rb.Push(RESULT_SUCCESS);
  142. rb.Push<u32>(sample_rate);
  143. rb.Push<u32>(audio_channels);
  144. rb.Push<u32>(static_cast<u32>(PcmFormat::Int16));
  145. rb.Push<u32>(0); // This field is unknown
  146. rb.PushIpcInterface<Audio::IAudioOut>(audio_out_interface);
  147. }
  148. AudOutU::AudOutU() : ServiceFramework("audout:u") {
  149. static const FunctionInfo functions[] = {{0x00000000, &AudOutU::ListAudioOuts, "ListAudioOuts"},
  150. {0x00000001, &AudOutU::OpenAudioOut, "OpenAudioOut"},
  151. {0x00000002, nullptr, "Unknown2"},
  152. {0x00000003, nullptr, "Unknown3"}};
  153. RegisterHandlers(functions);
  154. }
  155. } // namespace Audio
  156. } // namespace Service