IAudioDevice.cs 7.2 KB

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