audout_u.cpp 7.3 KB

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