IAudioOutManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Audio;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.HLE.HOS.Ipc;
  5. using Ryujinx.HLE.HOS.Kernel;
  6. using Ryujinx.HLE.HOS.Services.Aud.AudioOut;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using static Ryujinx.HLE.HOS.ErrorCode;
  10. namespace Ryujinx.HLE.HOS.Services.Aud
  11. {
  12. class IAudioOutManager : IpcService
  13. {
  14. private const string DefaultAudioOutput = "DeviceOut";
  15. private const int DefaultSampleRate = 48000;
  16. private const int DefaultChannelsCount = 2;
  17. private Dictionary<int, ServiceProcessRequest> m_Commands;
  18. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  19. public IAudioOutManager()
  20. {
  21. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  22. {
  23. { 0, ListAudioOuts },
  24. { 1, OpenAudioOut },
  25. { 2, ListAudioOutsAuto },
  26. { 3, OpenAudioOutAuto }
  27. };
  28. }
  29. public long ListAudioOuts(ServiceCtx Context)
  30. {
  31. return ListAudioOutsImpl(
  32. Context,
  33. Context.Request.ReceiveBuff[0].Position,
  34. Context.Request.ReceiveBuff[0].Size);
  35. }
  36. public long OpenAudioOut(ServiceCtx Context)
  37. {
  38. return OpenAudioOutImpl(
  39. Context,
  40. Context.Request.SendBuff[0].Position,
  41. Context.Request.SendBuff[0].Size,
  42. Context.Request.ReceiveBuff[0].Position,
  43. Context.Request.ReceiveBuff[0].Size);
  44. }
  45. public long ListAudioOutsAuto(ServiceCtx Context)
  46. {
  47. (long RecvPosition, long RecvSize) = Context.Request.GetBufferType0x22();
  48. return ListAudioOutsImpl(Context, RecvPosition, RecvSize);
  49. }
  50. public long OpenAudioOutAuto(ServiceCtx Context)
  51. {
  52. (long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
  53. (long RecvPosition, long RecvSize) = Context.Request.GetBufferType0x22();
  54. return OpenAudioOutImpl(
  55. Context,
  56. SendPosition,
  57. SendSize,
  58. RecvPosition,
  59. RecvSize);
  60. }
  61. private long ListAudioOutsImpl(ServiceCtx Context, long Position, long Size)
  62. {
  63. int NameCount = 0;
  64. byte[] DeviceNameBuffer = Encoding.ASCII.GetBytes(DefaultAudioOutput + "\0");
  65. if ((ulong)DeviceNameBuffer.Length <= (ulong)Size)
  66. {
  67. Context.Memory.WriteBytes(Position, DeviceNameBuffer);
  68. NameCount++;
  69. }
  70. else
  71. {
  72. Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {Size} too small!");
  73. }
  74. Context.ResponseData.Write(NameCount);
  75. return 0;
  76. }
  77. private long OpenAudioOutImpl(ServiceCtx Context, long SendPosition, long SendSize, long ReceivePosition, long ReceiveSize)
  78. {
  79. string DeviceName = AMemoryHelper.ReadAsciiString(
  80. Context.Memory,
  81. SendPosition,
  82. SendSize);
  83. if (DeviceName == string.Empty)
  84. {
  85. DeviceName = DefaultAudioOutput;
  86. }
  87. if (DeviceName != DefaultAudioOutput)
  88. {
  89. Logger.PrintWarning(LogClass.Audio, "Invalid device name!");
  90. return MakeError(ErrorModule.Audio, AudErr.DeviceNotFound);
  91. }
  92. byte[] DeviceNameBuffer = Encoding.ASCII.GetBytes(DeviceName + "\0");
  93. if ((ulong)DeviceNameBuffer.Length <= (ulong)ReceiveSize)
  94. {
  95. Context.Memory.WriteBytes(ReceivePosition, DeviceNameBuffer);
  96. }
  97. else
  98. {
  99. Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {ReceiveSize} too small!");
  100. }
  101. int SampleRate = Context.RequestData.ReadInt32();
  102. int Channels = Context.RequestData.ReadInt32();
  103. if (SampleRate == 0)
  104. {
  105. SampleRate = DefaultSampleRate;
  106. }
  107. if (SampleRate != DefaultSampleRate)
  108. {
  109. Logger.PrintWarning(LogClass.Audio, "Invalid sample rate!");
  110. return MakeError(ErrorModule.Audio, AudErr.UnsupportedSampleRate);
  111. }
  112. Channels = (ushort)Channels;
  113. if (Channels == 0)
  114. {
  115. Channels = DefaultChannelsCount;
  116. }
  117. KEvent ReleaseEvent = new KEvent(Context.Device.System);
  118. ReleaseCallback Callback = () =>
  119. {
  120. ReleaseEvent.ReadableEvent.Signal();
  121. };
  122. IAalOutput AudioOut = Context.Device.AudioOut;
  123. int Track = AudioOut.OpenTrack(SampleRate, Channels, Callback);
  124. MakeObject(Context, new IAudioOut(AudioOut, ReleaseEvent, Track));
  125. Context.ResponseData.Write(SampleRate);
  126. Context.ResponseData.Write(Channels);
  127. Context.ResponseData.Write((int)SampleFormat.PcmInt16);
  128. Context.ResponseData.Write((int)PlaybackState.Stopped);
  129. return 0;
  130. }
  131. }
  132. }