IAudioDevice.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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);
  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.PrintError(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  34. break;
  35. }
  36. context.Memory.WriteBytes(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 = context.Memory.ReadBytes(position, size);
  49. string deviceName = Encoding.ASCII.GetString(deviceNameBuffer);
  50. Logger.PrintStub(LogClass.ServiceAudio);
  51. return ResultCode.Success;
  52. }
  53. [Command(3)]
  54. // GetActiveAudioDeviceName() -> buffer<bytes, 6>
  55. public ResultCode GetActiveAudioDeviceName(ServiceCtx context)
  56. {
  57. string name = context.Device.System.State.ActiveAudioOutput;
  58. long position = context.Request.ReceiveBuff[0].Position;
  59. long size = context.Request.ReceiveBuff[0].Size;
  60. byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(name + "\0");
  61. if ((ulong)deviceNameBuffer.Length <= (ulong)size)
  62. {
  63. context.Memory.WriteBytes(position, deviceNameBuffer);
  64. }
  65. else
  66. {
  67. Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  68. }
  69. return ResultCode.Success;
  70. }
  71. [Command(4)]
  72. // QueryAudioDeviceSystemEvent() -> handle<copy, event>
  73. public ResultCode QueryAudioDeviceSystemEvent(ServiceCtx context)
  74. {
  75. if (context.Process.HandleTable.GenerateHandle(_systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  76. {
  77. throw new InvalidOperationException("Out of handles!");
  78. }
  79. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  80. Logger.PrintStub(LogClass.ServiceAudio);
  81. return ResultCode.Success;
  82. }
  83. [Command(5)]
  84. // GetActiveChannelCount() -> u32
  85. public ResultCode GetActiveChannelCount(ServiceCtx context)
  86. {
  87. context.ResponseData.Write(2);
  88. Logger.PrintStub(LogClass.ServiceAudio);
  89. return ResultCode.Success;
  90. }
  91. [Command(6)]
  92. // ListAudioDeviceNameAuto() -> (u32, buffer<bytes, 0x22>)
  93. public ResultCode ListAudioDeviceNameAuto(ServiceCtx context)
  94. {
  95. string[] deviceNames = SystemStateMgr.AudioOutputs;
  96. context.ResponseData.Write(deviceNames.Length);
  97. (long position, long size) = context.Request.GetBufferType0x22();
  98. long basePosition = position;
  99. foreach (string name in deviceNames)
  100. {
  101. byte[] buffer = Encoding.UTF8.GetBytes(name + '\0');
  102. if ((position - basePosition) + buffer.Length > size)
  103. {
  104. Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  105. break;
  106. }
  107. context.Memory.WriteBytes(position, buffer);
  108. position += buffer.Length;
  109. }
  110. return ResultCode.Success;
  111. }
  112. [Command(7)]
  113. // SetAudioDeviceOutputVolumeAuto(u32, buffer<bytes, 0x21>)
  114. public ResultCode SetAudioDeviceOutputVolumeAuto(ServiceCtx context)
  115. {
  116. float volume = context.RequestData.ReadSingle();
  117. (long position, long size) = context.Request.GetBufferType0x21();
  118. byte[] deviceNameBuffer = context.Memory.ReadBytes(position, size);
  119. string deviceName = Encoding.UTF8.GetString(deviceNameBuffer);
  120. Logger.PrintStub(LogClass.ServiceAudio);
  121. return ResultCode.Success;
  122. }
  123. [Command(8)]
  124. // GetAudioDeviceOutputVolumeAuto(buffer<bytes, 0x21>) -> u32
  125. public ResultCode GetAudioDeviceOutputVolumeAuto(ServiceCtx context)
  126. {
  127. context.ResponseData.Write(1f);
  128. Logger.PrintStub(LogClass.ServiceAudio);
  129. return ResultCode.Success;
  130. }
  131. [Command(10)]
  132. // GetActiveAudioDeviceNameAuto() -> buffer<bytes, 0x22>
  133. public ResultCode GetActiveAudioDeviceNameAuto(ServiceCtx context)
  134. {
  135. string name = context.Device.System.State.ActiveAudioOutput;
  136. (long position, long size) = context.Request.GetBufferType0x22();
  137. byte[] deviceNameBuffer = Encoding.UTF8.GetBytes(name + '\0');
  138. if ((ulong)deviceNameBuffer.Length <= (ulong)size)
  139. {
  140. context.Memory.WriteBytes(position, deviceNameBuffer);
  141. }
  142. else
  143. {
  144. Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
  145. }
  146. return ResultCode.Success;
  147. }
  148. [Command(11)]
  149. // QueryAudioDeviceInputEvent() -> handle<copy, event>
  150. public ResultCode QueryAudioDeviceInputEvent(ServiceCtx context)
  151. {
  152. if (context.Process.HandleTable.GenerateHandle(_systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  153. {
  154. throw new InvalidOperationException("Out of handles!");
  155. }
  156. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  157. Logger.PrintStub(LogClass.ServiceAudio);
  158. return ResultCode.Success;
  159. }
  160. [Command(12)]
  161. // QueryAudioDeviceOutputEvent() -> handle<copy, event>
  162. public ResultCode QueryAudioDeviceOutputEvent(ServiceCtx context)
  163. {
  164. if (context.Process.HandleTable.GenerateHandle(_systemEvent.ReadableEvent, out int handle) != KernelResult.Success)
  165. {
  166. throw new InvalidOperationException("Out of handles!");
  167. }
  168. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  169. Logger.PrintStub(LogClass.ServiceAudio);
  170. return ResultCode.Success;
  171. }
  172. }
  173. }