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> m_Commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  14. private KEvent SystemEvent;
  15. public IAudioDevice(Horizon System)
  16. {
  17. m_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. }