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.Threading;
  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> _commands;
  18. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  19. public IAudioOutManager()
  20. {
  21. _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 = MemoryHelper.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. }