AudioRendererServer.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using System;
  6. using System.Buffers;
  7. namespace Ryujinx.HLE.HOS.Services.Audio.AudioRenderer
  8. {
  9. class AudioRendererServer : DisposableIpcService
  10. {
  11. private IAudioRenderer _impl;
  12. public AudioRendererServer(IAudioRenderer impl)
  13. {
  14. _impl = impl;
  15. }
  16. [CommandHipc(0)]
  17. // GetSampleRate() -> u32
  18. public ResultCode GetSampleRate(ServiceCtx context)
  19. {
  20. context.ResponseData.Write(_impl.GetSampleRate());
  21. return ResultCode.Success;
  22. }
  23. [CommandHipc(1)]
  24. // GetSampleCount() -> u32
  25. public ResultCode GetSampleCount(ServiceCtx context)
  26. {
  27. context.ResponseData.Write(_impl.GetSampleCount());
  28. return ResultCode.Success;
  29. }
  30. [CommandHipc(2)]
  31. // GetMixBufferCount() -> u32
  32. public ResultCode GetMixBufferCount(ServiceCtx context)
  33. {
  34. context.ResponseData.Write(_impl.GetMixBufferCount());
  35. return ResultCode.Success;
  36. }
  37. [CommandHipc(3)]
  38. // GetState() -> u32
  39. public ResultCode GetState(ServiceCtx context)
  40. {
  41. context.ResponseData.Write(_impl.GetState());
  42. return ResultCode.Success;
  43. }
  44. [CommandHipc(4)]
  45. // RequestUpdate(buffer<nn::audio::detail::AudioRendererUpdateDataHeader, 5> input)
  46. // -> (buffer<nn::audio::detail::AudioRendererUpdateDataHeader, 6> output, buffer<nn::audio::detail::AudioRendererUpdateDataHeader, 6> performanceOutput)
  47. public ResultCode RequestUpdate(ServiceCtx context)
  48. {
  49. ulong inputPosition = context.Request.SendBuff[0].Position;
  50. ulong inputSize = context.Request.SendBuff[0].Size;
  51. ulong outputPosition = context.Request.ReceiveBuff[0].Position;
  52. ulong outputSize = context.Request.ReceiveBuff[0].Size;
  53. ulong performanceOutputPosition = context.Request.ReceiveBuff[1].Position;
  54. ulong performanceOutputSize = context.Request.ReceiveBuff[1].Size;
  55. ReadOnlyMemory<byte> input = context.Memory.GetSpan(inputPosition, (int)inputSize).ToArray();
  56. Memory<byte> output = new byte[outputSize];
  57. Memory<byte> performanceOutput = new byte[performanceOutputSize];
  58. using MemoryHandle outputHandle = output.Pin();
  59. using MemoryHandle performanceOutputHandle = performanceOutput.Pin();
  60. ResultCode result = _impl.RequestUpdate(output, performanceOutput, input);
  61. if (result == ResultCode.Success)
  62. {
  63. context.Memory.Write(outputPosition, output.Span);
  64. context.Memory.Write(performanceOutputPosition, performanceOutput.Span);
  65. }
  66. else
  67. {
  68. Logger.Error?.Print(LogClass.ServiceAudio, $"Error while processing renderer update: 0x{result}");
  69. }
  70. return result;
  71. }
  72. [CommandHipc(5)]
  73. // Start()
  74. public ResultCode Start(ServiceCtx context)
  75. {
  76. return _impl.Start();
  77. }
  78. [CommandHipc(6)]
  79. // Stop()
  80. public ResultCode Stop(ServiceCtx context)
  81. {
  82. return _impl.Stop();
  83. }
  84. [CommandHipc(7)]
  85. // QuerySystemEvent() -> handle<copy, event>
  86. public ResultCode QuerySystemEvent(ServiceCtx context)
  87. {
  88. ResultCode result = _impl.QuerySystemEvent(out KEvent systemEvent);
  89. if (result == ResultCode.Success)
  90. {
  91. if (context.Process.HandleTable.GenerateHandle(systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  92. {
  93. throw new InvalidOperationException("Out of handles!");
  94. }
  95. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  96. }
  97. return result;
  98. }
  99. [CommandHipc(8)]
  100. // SetAudioRendererRenderingTimeLimit(u32 limit)
  101. public ResultCode SetAudioRendererRenderingTimeLimit(ServiceCtx context)
  102. {
  103. uint limit = context.RequestData.ReadUInt32();
  104. _impl.SetRenderingTimeLimit(limit);
  105. return ResultCode.Success;
  106. }
  107. [CommandHipc(9)]
  108. // GetAudioRendererRenderingTimeLimit() -> u32 limit
  109. public ResultCode GetAudioRendererRenderingTimeLimit(ServiceCtx context)
  110. {
  111. uint limit = _impl.GetRenderingTimeLimit();
  112. context.ResponseData.Write(limit);
  113. return ResultCode.Success;
  114. }
  115. [CommandHipc(10)] // 3.0.0+
  116. // RequestUpdateAuto(buffer<nn::audio::detail::AudioRendererUpdateDataHeader, 0x21> input)
  117. // -> (buffer<nn::audio::detail::AudioRendererUpdateDataHeader, 0x22> output, buffer<nn::audio::detail::AudioRendererUpdateDataHeader, 0x22> performanceOutput)
  118. public ResultCode RequestUpdateAuto(ServiceCtx context)
  119. {
  120. (ulong inputPosition, ulong inputSize) = context.Request.GetBufferType0x21();
  121. (ulong outputPosition, ulong outputSize) = context.Request.GetBufferType0x22(0);
  122. (ulong performanceOutputPosition, ulong performanceOutputSize) = context.Request.GetBufferType0x22(1);
  123. ReadOnlyMemory<byte> input = context.Memory.GetSpan(inputPosition, (int)inputSize).ToArray();
  124. Memory<byte> output = new byte[outputSize];
  125. Memory<byte> performanceOutput = new byte[performanceOutputSize];
  126. using MemoryHandle outputHandle = output.Pin();
  127. using MemoryHandle performanceOutputHandle = performanceOutput.Pin();
  128. ResultCode result = _impl.RequestUpdate(output, performanceOutput, input);
  129. if (result == ResultCode.Success)
  130. {
  131. context.Memory.Write(outputPosition, output.Span);
  132. context.Memory.Write(performanceOutputPosition, performanceOutput.Span);
  133. }
  134. return result;
  135. }
  136. protected override void Dispose(bool isDisposing)
  137. {
  138. if (isDisposing)
  139. {
  140. _impl.Dispose();
  141. }
  142. }
  143. }
  144. }