audout_u.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <vector>
  6. #include "common/logging/log.h"
  7. #include "core/core.h"
  8. #include "core/hle/ipc_helpers.h"
  9. #include "core/hle/kernel/event.h"
  10. #include "core/hle/kernel/hle_ipc.h"
  11. #include "core/hle/service/audio/audout_u.h"
  12. namespace Service::Audio {
  13. namespace ErrCodes {
  14. enum {
  15. ErrorUnknown = 2,
  16. BufferCountExceeded = 8,
  17. };
  18. }
  19. constexpr std::array<char, 10> DefaultDevice{{"DeviceOut"}};
  20. constexpr int DefaultSampleRate{48000};
  21. class IAudioOut final : public ServiceFramework<IAudioOut> {
  22. public:
  23. IAudioOut(AudoutParams audio_params, AudioCore::AudioOut& audio_core)
  24. : ServiceFramework("IAudioOut"), audio_params(audio_params), audio_core(audio_core) {
  25. static const FunctionInfo functions[] = {
  26. {0, &IAudioOut::GetAudioOutState, "GetAudioOutState"},
  27. {1, &IAudioOut::StartAudioOut, "StartAudioOut"},
  28. {2, &IAudioOut::StopAudioOut, "StopAudioOut"},
  29. {3, &IAudioOut::AppendAudioOutBufferImpl, "AppendAudioOutBuffer"},
  30. {4, &IAudioOut::RegisterBufferEvent, "RegisterBufferEvent"},
  31. {5, &IAudioOut::GetReleasedAudioOutBufferImpl, "GetReleasedAudioOutBuffer"},
  32. {6, &IAudioOut::ContainsAudioOutBuffer, "ContainsAudioOutBuffer"},
  33. {7, &IAudioOut::AppendAudioOutBufferImpl, "AppendAudioOutBufferAuto"},
  34. {8, &IAudioOut::GetReleasedAudioOutBufferImpl, "GetReleasedAudioOutBufferAuto"},
  35. {9, &IAudioOut::GetAudioOutBufferCount, "GetAudioOutBufferCount"},
  36. {10, nullptr, "GetAudioOutPlayedSampleCount"},
  37. {11, nullptr, "FlushAudioOutBuffers"},
  38. };
  39. RegisterHandlers(functions);
  40. // This is the event handle used to check if the audio buffer was released
  41. buffer_event = Kernel::Event::Create(Kernel::ResetType::Sticky, "IAudioOutBufferReleased");
  42. stream = audio_core.OpenStream(audio_params.sample_rate, audio_params.channel_count,
  43. [=]() { buffer_event->Signal(); });
  44. }
  45. private:
  46. struct AudioBuffer {
  47. u64_le next;
  48. u64_le buffer;
  49. u64_le buffer_capacity;
  50. u64_le buffer_size;
  51. u64_le offset;
  52. };
  53. static_assert(sizeof(AudioBuffer) == 0x28, "AudioBuffer is an invalid size");
  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>(stream->IsPlaying() ? AudioState::Started : AudioState::Stopped));
  59. }
  60. void StartAudioOut(Kernel::HLERequestContext& ctx) {
  61. LOG_DEBUG(Service_Audio, "called");
  62. if (stream->IsPlaying()) {
  63. IPC::ResponseBuilder rb{ctx, 2};
  64. rb.Push(ResultCode(ErrorModule::Audio, ErrCodes::ErrorUnknown));
  65. return;
  66. }
  67. audio_core.StartStream(stream);
  68. IPC::ResponseBuilder rb{ctx, 2};
  69. rb.Push(RESULT_SUCCESS);
  70. }
  71. void StopAudioOut(Kernel::HLERequestContext& ctx) {
  72. LOG_DEBUG(Service_Audio, "called");
  73. audio_core.StopStream(stream);
  74. IPC::ResponseBuilder rb{ctx, 2};
  75. rb.Push(RESULT_SUCCESS);
  76. }
  77. void RegisterBufferEvent(Kernel::HLERequestContext& ctx) {
  78. LOG_DEBUG(Service_Audio, "called");
  79. IPC::ResponseBuilder rb{ctx, 2, 1};
  80. rb.Push(RESULT_SUCCESS);
  81. rb.PushCopyObjects(buffer_event);
  82. }
  83. void AppendAudioOutBufferImpl(Kernel::HLERequestContext& ctx) {
  84. LOG_DEBUG(Service_Audio, "(STUBBED) called {}", ctx.Description());
  85. IPC::RequestParser rp{ctx};
  86. const auto& input_buffer{ctx.ReadBuffer()};
  87. ASSERT_MSG(input_buffer.size() == sizeof(AudioBuffer),
  88. "AudioBuffer input is an invalid size!");
  89. AudioBuffer audio_buffer{};
  90. std::memcpy(&audio_buffer, input_buffer.data(), sizeof(AudioBuffer));
  91. const u64 tag{rp.Pop<u64>()};
  92. std::vector<u8> data(audio_buffer.buffer_size);
  93. Memory::ReadBlock(audio_buffer.buffer, data.data(), data.size());
  94. if (!audio_core.QueueBuffer(stream, tag, std::move(data))) {
  95. IPC::ResponseBuilder rb{ctx, 2};
  96. rb.Push(ResultCode(ErrorModule::Audio, ErrCodes::BufferCountExceeded));
  97. }
  98. IPC::ResponseBuilder rb{ctx, 2};
  99. rb.Push(RESULT_SUCCESS);
  100. }
  101. void GetReleasedAudioOutBufferImpl(Kernel::HLERequestContext& ctx) {
  102. LOG_DEBUG(Service_Audio, "called {}", ctx.Description());
  103. IPC::RequestParser rp{ctx};
  104. const u64 max_count{ctx.GetWriteBufferSize() / sizeof(u64)};
  105. const auto released_buffers{audio_core.GetTagsAndReleaseBuffers(stream, max_count)};
  106. std::vector<u64> tags{released_buffers};
  107. tags.resize(max_count);
  108. ctx.WriteBuffer(tags);
  109. IPC::ResponseBuilder rb{ctx, 3};
  110. rb.Push(RESULT_SUCCESS);
  111. rb.Push<u32>(static_cast<u32>(released_buffers.size()));
  112. }
  113. void ContainsAudioOutBuffer(Kernel::HLERequestContext& ctx) {
  114. LOG_DEBUG(Service_Audio, "called");
  115. IPC::RequestParser rp{ctx};
  116. const u64 tag{rp.Pop<u64>()};
  117. IPC::ResponseBuilder rb{ctx, 3};
  118. rb.Push(RESULT_SUCCESS);
  119. rb.Push(stream->ContainsBuffer(tag));
  120. }
  121. void GetAudioOutBufferCount(Kernel::HLERequestContext& ctx) {
  122. LOG_DEBUG(Service_Audio, "called");
  123. IPC::ResponseBuilder rb{ctx, 3};
  124. rb.Push(RESULT_SUCCESS);
  125. rb.Push(static_cast<u32>(stream->GetQueueSize()));
  126. }
  127. AudioCore::AudioOut& audio_core;
  128. AudioCore::StreamPtr stream;
  129. AudoutParams audio_params{};
  130. /// This is the evend handle used to check if the audio buffer was released
  131. Kernel::SharedPtr<Kernel::Event> buffer_event;
  132. };
  133. void AudOutU::ListAudioOutsImpl(Kernel::HLERequestContext& ctx) {
  134. LOG_DEBUG(Service_Audio, "called");
  135. IPC::RequestParser rp{ctx};
  136. ctx.WriteBuffer(DefaultDevice);
  137. IPC::ResponseBuilder rb = rp.MakeBuilder(3, 0, 0);
  138. rb.Push(RESULT_SUCCESS);
  139. rb.Push<u32>(1); // Amount of audio devices
  140. }
  141. void AudOutU::OpenAudioOutImpl(Kernel::HLERequestContext& ctx) {
  142. LOG_DEBUG(Service_Audio, "called");
  143. ctx.WriteBuffer(DefaultDevice);
  144. IPC::RequestParser rp{ctx};
  145. auto params{rp.PopRaw<AudoutParams>()};
  146. if (params.channel_count <= 2) {
  147. // Mono does not exist for audout
  148. params.channel_count = 2;
  149. } else {
  150. params.channel_count = 6;
  151. }
  152. if (!params.sample_rate) {
  153. params.sample_rate = DefaultSampleRate;
  154. }
  155. // TODO(bunnei): Support more than one IAudioOut interface. When we add this, ListAudioOutsImpl
  156. // will likely need to be updated as well.
  157. ASSERT_MSG(!audio_out_interface, "Unimplemented");
  158. audio_out_interface = std::make_shared<IAudioOut>(std::move(params), *audio_core);
  159. IPC::ResponseBuilder rb{ctx, 6, 0, 1};
  160. rb.Push(RESULT_SUCCESS);
  161. rb.Push<u32>(DefaultSampleRate);
  162. rb.Push<u32>(params.channel_count);
  163. rb.Push<u32>(static_cast<u32>(PcmFormat::Int16));
  164. rb.Push<u32>(static_cast<u32>(AudioState::Stopped));
  165. rb.PushIpcInterface<Audio::IAudioOut>(audio_out_interface);
  166. }
  167. AudOutU::AudOutU() : ServiceFramework("audout:u") {
  168. static const FunctionInfo functions[] = {{0, &AudOutU::ListAudioOutsImpl, "ListAudioOuts"},
  169. {1, &AudOutU::OpenAudioOutImpl, "OpenAudioOut"},
  170. {2, &AudOutU::ListAudioOutsImpl, "ListAudioOutsAuto"},
  171. {3, &AudOutU::OpenAudioOutImpl, "OpenAudioOutAuto"}};
  172. RegisterHandlers(functions);
  173. audio_core = std::make_unique<AudioCore::AudioOut>();
  174. }
  175. } // namespace Service::Audio