IAudioDevice.cs 7.2 KB

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