audout_u.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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(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 = 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 = 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. 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. const auto& buffer = ctx.BufferDescriptorB()[0];
  81. // TODO(st4rk): this is how libtransistor currently implements the
  82. // GetReleasedAudioOutBuffer, it should return the key (a VAddr) to the APP and this address
  83. // is used to know which buffer should be filled with data and send again to the service
  84. // through AppendAudioOutBuffer. Check if this is the proper way to do it.
  85. u64 key{0};
  86. if (queue_keys.size()) {
  87. key = queue_keys.back();
  88. queue_keys.pop_back();
  89. }
  90. Memory::WriteBlock(buffer.Address(), &key, sizeof(u64));
  91. IPC::ResponseBuilder rb{ctx, 3};
  92. rb.Push(RESULT_SUCCESS);
  93. // TODO(st4rk): This might be the total of released buffers, needs to be verified on
  94. // hardware
  95. rb.Push<u32>(static_cast<u32>(queue_keys.size()));
  96. }
  97. void UpdateAudioBuffersCallback() {
  98. if (audio_out_state != Started) {
  99. return;
  100. }
  101. if (queue_keys.empty()) {
  102. return;
  103. }
  104. buffer_event->Signal();
  105. }
  106. enum AudioState : u32 {
  107. Started,
  108. Stopped,
  109. };
  110. /// This is used to trigger the audio event callback that is going to read the samples from the
  111. /// audio_buffer list and enqueue the samples using the sink (audio_core).
  112. CoreTiming::EventType* audio_event;
  113. /// This is the evend handle used to check if the audio buffer was released
  114. Kernel::SharedPtr<Kernel::Event> buffer_event;
  115. /// (st4rk): this is just a temporary workaround for the future implementation. Libtransistor
  116. /// uses the key as an address in the App, so we need to return when the
  117. /// GetReleasedAudioOutBuffer_1 is called, otherwise we'll run in problems, because
  118. /// libtransistor uses the key returned as an pointer;
  119. std::vector<u64> queue_keys;
  120. AudioState audio_out_state;
  121. };
  122. void AudOutU::ListAudioOuts(Kernel::HLERequestContext& ctx) {
  123. LOG_WARNING(Service_Audio, "(STUBBED) called");
  124. IPC::RequestParser rp{ctx};
  125. auto& buffer = ctx.BufferDescriptorB()[0];
  126. const std::string audio_interface = "AudioInterface";
  127. Memory::WriteBlock(buffer.Address(), &audio_interface[0], 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
  131. // (stringlist size)
  132. // however, it's highly possible to have more than one interface (despite that
  133. // libtransistor
  134. // requires only one).
  135. rb.Push<u32>(1);
  136. }
  137. void AudOutU::OpenAudioOut(Kernel::HLERequestContext& ctx) {
  138. LOG_WARNING(Service_Audio, "(STUBBED) called");
  139. if (!audio_out_interface) {
  140. audio_out_interface = std::make_shared<IAudioOut>();
  141. }
  142. auto sessions = Kernel::ServerSession::CreateSessionPair(audio_out_interface->GetServiceName());
  143. auto server = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions);
  144. auto client = std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions);
  145. audio_out_interface->ClientConnected(server);
  146. LOG_DEBUG(Service, "called, initialized IAudioOut -> session=%u", client->GetObjectId());
  147. IPC::ResponseBuilder rb{ctx, 6, 0, 1};
  148. rb.Push(RESULT_SUCCESS);
  149. rb.Push<u32>(sample_rate);
  150. rb.Push<u32>(audio_channels);
  151. rb.Push<u32>(static_cast<u32>(PcmFormat::Int16));
  152. // this field is unknown
  153. rb.Push<u32>(0);
  154. rb.PushMoveObjects(std::move(client));
  155. }
  156. AudOutU::AudOutU() : ServiceFramework("audout:u") {
  157. static const FunctionInfo functions[] = {{0x00000000, &AudOutU::ListAudioOuts, "ListAudioOuts"},
  158. {0x00000001, &AudOutU::OpenAudioOut, "OpenAudioOut"},
  159. {0x00000002, nullptr, "Unknown2"},
  160. {0x00000003, nullptr, "Unknown3"}};
  161. RegisterHandlers(functions);
  162. }
  163. } // namespace Audio
  164. } // namespace Service