IAudioDevice.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Ryujinx.HLE.HOS.SystemState;
  6. using System;
  7. using System.Text;
  8. namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
  9. {
  10. class IAudioDevice : IpcService
  11. {
  12. private KEvent _systemEvent;
  13. public IAudioDevice(Horizon system)
  14. {
  15. _systemEvent = new KEvent(system.KernelContext);
  16. // TODO: We shouldn't be signaling this here.
  17. _systemEvent.ReadableEvent.Signal();
  18. }
  19. [Command(0)]
  20. // ListAudioDeviceName() -> (u32, buffer<bytes, 6>)
  21. public ResultCode ListAudioDeviceName(ServiceCtx context)
  22. {
  23. string[] deviceNames = SystemStateMgr.AudioOutputs;
  24. context.ResponseData.Write(deviceNames.Length);
  25. long position = context.Request.ReceiveBuff[0].Position;
  26. long size = context.Request.ReceiveBuff[0].Size;
  27. long basePosition = position;
  28. foreach (string name in deviceNames)
  29. {
  30. byte[] buffer = Encoding.ASCII.GetBytes(name + "\0");
  31. if ((position - basePosition) + buffer.Length > size)
  32. {
  33. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  34. break;
  35. }
  36. context.Memory.Write((ulong)position, buffer);
  37. position += buffer.Length;
  38. }
  39. return ResultCode.Success;
  40. }
  41. [Command(1)]
  42. // SetAudioDeviceOutputVolume(u32, buffer<bytes, 5>)
  43. public ResultCode SetAudioDeviceOutputVolume(ServiceCtx context)
  44. {
  45. float volume = context.RequestData.ReadSingle();
  46. long position = context.Request.SendBuff[0].Position;
  47. long size = context.Request.SendBuff[0].Size;
  48. byte[] deviceNameBuffer = new byte[size];
  49. context.Memory.Read((ulong)position, deviceNameBuffer);
  50. string deviceName = Encoding.ASCII.GetString(deviceNameBuffer);
  51. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  52. return ResultCode.Success;
  53. }
  54. [Command(3)]
  55. // GetActiveAudioDeviceName() -> buffer<bytes, 6>
  56. public ResultCode GetActiveAudioDeviceName(ServiceCtx context)
  57. {
  58. string name = context.Device.System.State.ActiveAudioOutput;
  59. long position = context.Request.ReceiveBuff[0].Position;
  60. long size = context.Request.ReceiveBuff[0].Size;
  61. byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(name + "\0");
  62. if ((ulong)deviceNameBuffer.Length <= (ulong)size)
  63. {
  64. context.Memory.Write((ulong)position, deviceNameBuffer);
  65. }
  66. else
  67. {
  68. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  69. }
  70. return ResultCode.Success;
  71. }
  72. [Command(4)]
  73. // QueryAudioDeviceSystemEvent() -> handle<copy, event>
  74. public ResultCode QueryAudioDeviceSystemEvent(ServiceCtx context)
  75. {
  76. if (context.Process.HandleTable.GenerateHandle(_systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  77. {
  78. throw new InvalidOperationException("Out of handles!");
  79. }
  80. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  81. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  82. return ResultCode.Success;
  83. }
  84. [Command(5)]
  85. // GetActiveChannelCount() -> u32
  86. public ResultCode GetActiveChannelCount(ServiceCtx context)
  87. {
  88. context.ResponseData.Write(2);
  89. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  90. return ResultCode.Success;
  91. }
  92. [Command(6)]
  93. // ListAudioDeviceNameAuto() -> (u32, buffer<bytes, 0x22>)
  94. public ResultCode ListAudioDeviceNameAuto(ServiceCtx context)
  95. {
  96. string[] deviceNames = SystemStateMgr.AudioOutputs;
  97. context.ResponseData.Write(deviceNames.Length);
  98. (long position, long size) = context.Request.GetBufferType0x22();
  99. long basePosition = position;
  100. foreach (string name in deviceNames)
  101. {
  102. byte[] buffer = Encoding.UTF8.GetBytes(name + '\0');
  103. if ((position - basePosition) + buffer.Length > size)
  104. {
  105. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  106. break;
  107. }
  108. context.Memory.Write((ulong)position, buffer);
  109. position += buffer.Length;
  110. }
  111. return ResultCode.Success;
  112. }
  113. [Command(7)]
  114. // SetAudioDeviceOutputVolumeAuto(u32, buffer<bytes, 0x21>)
  115. public ResultCode SetAudioDeviceOutputVolumeAuto(ServiceCtx context)
  116. {
  117. float volume = context.RequestData.ReadSingle();
  118. (long position, long size) = context.Request.GetBufferType0x21();
  119. byte[] deviceNameBuffer = new byte[size];
  120. context.Memory.Read((ulong)position, deviceNameBuffer);
  121. string deviceName = Encoding.UTF8.GetString(deviceNameBuffer);
  122. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  123. return ResultCode.Success;
  124. }
  125. [Command(8)]
  126. // GetAudioDeviceOutputVolumeAuto(buffer<bytes, 0x21>) -> u32
  127. public ResultCode GetAudioDeviceOutputVolumeAuto(ServiceCtx context)
  128. {
  129. context.ResponseData.Write(1f);
  130. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  131. return ResultCode.Success;
  132. }
  133. [Command(10)]
  134. // GetActiveAudioDeviceNameAuto() -> buffer<bytes, 0x22>
  135. public ResultCode GetActiveAudioDeviceNameAuto(ServiceCtx context)
  136. {
  137. string name = context.Device.System.State.ActiveAudioOutput;
  138. (long position, long size) = context.Request.GetBufferType0x22();
  139. byte[] deviceNameBuffer = Encoding.UTF8.GetBytes(name + '\0');
  140. if ((ulong)deviceNameBuffer.Length <= (ulong)size)
  141. {
  142. context.Memory.Write((ulong)position, deviceNameBuffer);
  143. }
  144. else
  145. {
  146. Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  147. }
  148. return ResultCode.Success;
  149. }
  150. [Command(11)]
  151. // QueryAudioDeviceInputEvent() -> handle<copy, event>
  152. public ResultCode QueryAudioDeviceInputEvent(ServiceCtx context)
  153. {
  154. if (context.Process.HandleTable.GenerateHandle(_systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  155. {
  156. throw new InvalidOperationException("Out of handles!");
  157. }
  158. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  159. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  160. return ResultCode.Success;
  161. }
  162. [Command(12)]
  163. // QueryAudioDeviceOutputEvent() -> handle<copy, event>
  164. public ResultCode QueryAudioDeviceOutputEvent(ServiceCtx context)
  165. {
  166. if (context.Process.HandleTable.GenerateHandle(_systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  167. {
  168. throw new InvalidOperationException("Out of handles!");
  169. }
  170. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  171. Logger.Stub?.PrintStub(LogClass.ServiceAudio);
  172. return ResultCode.Success;
  173. }
  174. }
  175. }