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> m_Commands;
  12. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  13. private IAalOutput AudioOut;
  14. private KEvent ReleaseEvent;
  15. private int Track;
  16. public IAudioOut(IAalOutput AudioOut, KEvent ReleaseEvent, int Track)
  17. {
  18. m_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. this.AudioOut = AudioOut;
  31. this.ReleaseEvent = ReleaseEvent;
  32. this.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 = AMemoryHelper.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. }