IAudioOut.cs 4.7 KB

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