AudioDeviceServer.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using System;
  7. using System.Text;
  8. namespace Ryujinx.HLE.HOS.Services.Audio.AudioRenderer
  9. {
  10. class AudioDeviceServer : IpcService
  11. {
  12. private const int AudioDeviceNameSize = 0x100;
  13. private IAudioDevice _impl;
  14. public AudioDeviceServer(IAudioDevice impl)
  15. {
  16. _impl = impl;
  17. }
  18. [CommandHipc(0)]
  19. // ListAudioDeviceName() -> (u32, buffer<bytes, 6>)
  20. public ResultCode ListAudioDeviceName(ServiceCtx context)
  21. {
  22. string[] deviceNames = _impl.ListAudioDeviceName();
  23. ulong position = context.Request.ReceiveBuff[0].Position;
  24. ulong size = context.Request.ReceiveBuff[0].Size;
  25. ulong basePosition = position;
  26. int count = 0;
  27. foreach (string name in deviceNames)
  28. {
  29. byte[] buffer = Encoding.ASCII.GetBytes(name);
  30. if ((position - basePosition) + (ulong)buffer.Length > size)
  31. {
  32. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  33. break;
  34. }
  35. context.Memory.Write(position, buffer);
  36. MemoryHelper.FillWithZeros(context.Memory, position + (ulong)buffer.Length, AudioDeviceNameSize - buffer.Length);
  37. position += AudioDeviceNameSize;
  38. count++;
  39. }
  40. context.ResponseData.Write(count);
  41. return ResultCode.Success;
  42. }
  43. [CommandHipc(1)]
  44. // SetAudioDeviceOutputVolume(f32 volume, buffer<bytes, 5> name)
  45. public ResultCode SetAudioDeviceOutputVolume(ServiceCtx context)
  46. {
  47. float volume = context.RequestData.ReadSingle();
  48. ulong position = context.Request.SendBuff[0].Position;
  49. ulong size = context.Request.SendBuff[0].Size;
  50. string deviceName = MemoryHelper.ReadAsciiString(context.Memory, position, (long)size);
  51. return _impl.SetAudioDeviceOutputVolume(deviceName, volume);
  52. }
  53. [CommandHipc(2)]
  54. // GetAudioDeviceOutputVolume(buffer<bytes, 5> name) -> f32 volume
  55. public ResultCode GetAudioDeviceOutputVolume(ServiceCtx context)
  56. {
  57. ulong position = context.Request.SendBuff[0].Position;
  58. ulong size = context.Request.SendBuff[0].Size;
  59. string deviceName = MemoryHelper.ReadAsciiString(context.Memory, position, (long)size);
  60. ResultCode result = _impl.GetAudioDeviceOutputVolume(deviceName, out float volume);
  61. if (result == ResultCode.Success)
  62. {
  63. context.ResponseData.Write(volume);
  64. }
  65. return result;
  66. }
  67. [CommandHipc(3)]
  68. // GetActiveAudioDeviceName() -> buffer<bytes, 6>
  69. public ResultCode GetActiveAudioDeviceName(ServiceCtx context)
  70. {
  71. string name = _impl.GetActiveAudioDeviceName();
  72. ulong position = context.Request.ReceiveBuff[0].Position;
  73. ulong size = context.Request.ReceiveBuff[0].Size;
  74. byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(name + "\0");
  75. if ((ulong)deviceNameBuffer.Length <= size)
  76. {
  77. context.Memory.Write(position, deviceNameBuffer);
  78. }
  79. else
  80. {
  81. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  82. }
  83. return ResultCode.Success;
  84. }
  85. [CommandHipc(4)]
  86. // QueryAudioDeviceSystemEvent() -> handle<copy, event>
  87. public ResultCode QueryAudioDeviceSystemEvent(ServiceCtx context)
  88. {
  89. KEvent deviceSystemEvent = _impl.QueryAudioDeviceSystemEvent();
  90. if (context.Process.HandleTable.GenerateHandle(deviceSystemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  91. {
  92. throw new InvalidOperationException("Out of handles!");
  93. }
  94. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  95. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  96. return ResultCode.Success;
  97. }
  98. [CommandHipc(5)]
  99. // GetActiveChannelCount() -> u32
  100. public ResultCode GetActiveChannelCount(ServiceCtx context)
  101. {
  102. context.ResponseData.Write(_impl.GetActiveChannelCount());
  103. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  104. return ResultCode.Success;
  105. }
  106. [CommandHipc(6)] // 3.0.0+
  107. // ListAudioDeviceNameAuto() -> (u32, buffer<bytes, 0x22>)
  108. public ResultCode ListAudioDeviceNameAuto(ServiceCtx context)
  109. {
  110. string[] deviceNames = _impl.ListAudioDeviceName();
  111. (ulong position, ulong size) = context.Request.GetBufferType0x22();
  112. ulong basePosition = position;
  113. int count = 0;
  114. foreach (string name in deviceNames)
  115. {
  116. byte[] buffer = Encoding.ASCII.GetBytes(name);
  117. if ((position - basePosition) + (ulong)buffer.Length > size)
  118. {
  119. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  120. break;
  121. }
  122. context.Memory.Write(position, buffer);
  123. MemoryHelper.FillWithZeros(context.Memory, position + (ulong)buffer.Length, AudioDeviceNameSize - buffer.Length);
  124. position += AudioDeviceNameSize;
  125. count++;
  126. }
  127. context.ResponseData.Write(count);
  128. return ResultCode.Success;
  129. }
  130. [CommandHipc(7)] // 3.0.0+
  131. // SetAudioDeviceOutputVolumeAuto(f32 volume, buffer<bytes, 0x21> name)
  132. public ResultCode SetAudioDeviceOutputVolumeAuto(ServiceCtx context)
  133. {
  134. float volume = context.RequestData.ReadSingle();
  135. (ulong position, ulong size) = context.Request.GetBufferType0x21();
  136. string deviceName = MemoryHelper.ReadAsciiString(context.Memory, position, (long)size);
  137. return _impl.SetAudioDeviceOutputVolume(deviceName, volume);
  138. }
  139. [CommandHipc(8)] // 3.0.0+
  140. // GetAudioDeviceOutputVolumeAuto(buffer<bytes, 0x21> name) -> f32
  141. public ResultCode GetAudioDeviceOutputVolumeAuto(ServiceCtx context)
  142. {
  143. (ulong position, ulong size) = context.Request.GetBufferType0x21();
  144. string deviceName = MemoryHelper.ReadAsciiString(context.Memory, position, (long)size);
  145. ResultCode result = _impl.GetAudioDeviceOutputVolume(deviceName, out float volume);
  146. if (result == ResultCode.Success)
  147. {
  148. context.ResponseData.Write(volume);
  149. }
  150. return ResultCode.Success;
  151. }
  152. [CommandHipc(10)] // 3.0.0+
  153. // GetActiveAudioDeviceNameAuto() -> buffer<bytes, 0x22>
  154. public ResultCode GetActiveAudioDeviceNameAuto(ServiceCtx context)
  155. {
  156. string name = _impl.GetActiveAudioDeviceName();
  157. (ulong position, ulong size) = context.Request.GetBufferType0x22();
  158. byte[] deviceNameBuffer = Encoding.UTF8.GetBytes(name + '\0');
  159. if ((ulong)deviceNameBuffer.Length <= size)
  160. {
  161. context.Memory.Write(position, deviceNameBuffer);
  162. }
  163. else
  164. {
  165. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  166. }
  167. return ResultCode.Success;
  168. }
  169. [CommandHipc(11)] // 3.0.0+
  170. // QueryAudioDeviceInputEvent() -> handle<copy, event>
  171. public ResultCode QueryAudioDeviceInputEvent(ServiceCtx context)
  172. {
  173. KEvent deviceInputEvent = _impl.QueryAudioDeviceInputEvent();
  174. if (context.Process.HandleTable.GenerateHandle(deviceInputEvent.ReadableEvent, out int handle) != KernelResult.Success)
  175. {
  176. throw new InvalidOperationException("Out of handles!");
  177. }
  178. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  179. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  180. return ResultCode.Success;
  181. }
  182. [CommandHipc(12)] // 3.0.0+
  183. // QueryAudioDeviceOutputEvent() -> handle<copy, event>
  184. public ResultCode QueryAudioDeviceOutputEvent(ServiceCtx context)
  185. {
  186. KEvent deviceOutputEvent = _impl.QueryAudioDeviceOutputEvent();
  187. if (context.Process.HandleTable.GenerateHandle(deviceOutputEvent.ReadableEvent, out int handle) != KernelResult.Success)
  188. {
  189. throw new InvalidOperationException("Out of handles!");
  190. }
  191. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  192. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  193. return ResultCode.Success;
  194. }
  195. [CommandHipc(13)]
  196. // GetAudioSystemMasterVolumeSetting(buffer<bytes, 5> name) -> f32
  197. public ResultCode GetAudioSystemMasterVolumeSetting(ServiceCtx context)
  198. {
  199. ulong position = context.Request.SendBuff[0].Position;
  200. ulong size = context.Request.SendBuff[0].Size;
  201. string deviceName = MemoryHelper.ReadAsciiString(context.Memory, position, (long)size);
  202. ResultCode result = _impl.GetAudioSystemMasterVolumeSetting(deviceName, out float systemMasterVolume);
  203. if (result == ResultCode.Success)
  204. {
  205. context.ResponseData.Write(systemMasterVolume);
  206. }
  207. return result;
  208. }
  209. }
  210. }