IAudioOut.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Audio;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace Ryujinx.HLE.HOS.Services.Aud.AudioOut
  9. {
  10. class IAudioOut : IpcService, IDisposable
  11. {
  12. private Dictionary<int, ServiceProcessRequest> _commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  14. private IAalOutput _audioOut;
  15. private KEvent _releaseEvent;
  16. private int _track;
  17. public IAudioOut(IAalOutput audioOut, KEvent releaseEvent, int track)
  18. {
  19. _commands = new Dictionary<int, ServiceProcessRequest>
  20. {
  21. { 0, GetAudioOutState },
  22. { 1, StartAudioOut },
  23. { 2, StopAudioOut },
  24. { 3, AppendAudioOutBuffer },
  25. { 4, RegisterBufferEvent },
  26. { 5, GetReleasedAudioOutBuffer },
  27. { 6, ContainsAudioOutBuffer },
  28. { 7, AppendAudioOutBufferAuto },
  29. { 8, GetReleasedAudioOutBufferAuto }
  30. };
  31. _audioOut = audioOut;
  32. _releaseEvent = releaseEvent;
  33. _track = track;
  34. }
  35. public long GetAudioOutState(ServiceCtx context)
  36. {
  37. context.ResponseData.Write((int)_audioOut.GetState(_track));
  38. return 0;
  39. }
  40. public long StartAudioOut(ServiceCtx context)
  41. {
  42. _audioOut.Start(_track);
  43. return 0;
  44. }
  45. public long StopAudioOut(ServiceCtx context)
  46. {
  47. _audioOut.Stop(_track);
  48. return 0;
  49. }
  50. public long AppendAudioOutBuffer(ServiceCtx context)
  51. {
  52. return AppendAudioOutBufferImpl(context, context.Request.SendBuff[0].Position);
  53. }
  54. public long RegisterBufferEvent(ServiceCtx context)
  55. {
  56. if (context.Process.HandleTable.GenerateHandle(_releaseEvent.ReadableEvent, out int handle) != KernelResult.Success)
  57. {
  58. throw new InvalidOperationException("Out of handles!");
  59. }
  60. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  61. return 0;
  62. }
  63. public long GetReleasedAudioOutBuffer(ServiceCtx context)
  64. {
  65. long position = context.Request.ReceiveBuff[0].Position;
  66. long size = context.Request.ReceiveBuff[0].Size;
  67. return GetReleasedAudioOutBufferImpl(context, position, size);
  68. }
  69. public long ContainsAudioOutBuffer(ServiceCtx context)
  70. {
  71. long tag = context.RequestData.ReadInt64();
  72. context.ResponseData.Write(_audioOut.ContainsBuffer(_track, tag) ? 1 : 0);
  73. return 0;
  74. }
  75. public long AppendAudioOutBufferAuto(ServiceCtx context)
  76. {
  77. (long position, long size) = context.Request.GetBufferType0x21();
  78. return AppendAudioOutBufferImpl(context, position);
  79. }
  80. public long AppendAudioOutBufferImpl(ServiceCtx context, long position)
  81. {
  82. long tag = context.RequestData.ReadInt64();
  83. AudioOutData data = MemoryHelper.Read<AudioOutData>(
  84. context.Memory,
  85. position);
  86. byte[] buffer = context.Memory.ReadBytes(
  87. data.SampleBufferPtr,
  88. data.SampleBufferSize);
  89. _audioOut.AppendBuffer(_track, tag, buffer);
  90. return 0;
  91. }
  92. public long GetReleasedAudioOutBufferAuto(ServiceCtx context)
  93. {
  94. (long position, long size) = context.Request.GetBufferType0x22();
  95. return GetReleasedAudioOutBufferImpl(context, position, size);
  96. }
  97. public long GetReleasedAudioOutBufferImpl(ServiceCtx context, long position, long size)
  98. {
  99. uint count = (uint)((ulong)size >> 3);
  100. long[] releasedBuffers = _audioOut.GetReleasedBuffers(_track, (int)count);
  101. for (uint index = 0; index < count; index++)
  102. {
  103. long tag = 0;
  104. if (index < releasedBuffers.Length)
  105. {
  106. tag = releasedBuffers[index];
  107. }
  108. context.Memory.WriteInt64(position + index * 8, tag);
  109. }
  110. context.ResponseData.Write(releasedBuffers.Length);
  111. return 0;
  112. }
  113. public void Dispose()
  114. {
  115. Dispose(true);
  116. }
  117. protected virtual void Dispose(bool disposing)
  118. {
  119. if (disposing)
  120. {
  121. _audioOut.CloseTrack(_track);
  122. }
  123. }
  124. }
  125. }