IAudioOut.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Audio;
  3. using Ryujinx.HLE.OsHle.Handles;
  4. using Ryujinx.HLE.OsHle.Ipc;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace Ryujinx.HLE.OsHle.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. int Handle = Context.Process.HandleTable.OpenHandle(ReleaseEvent);
  56. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  57. return 0;
  58. }
  59. public long GetReleasedAudioOutBuffer(ServiceCtx Context)
  60. {
  61. long Position = Context.Request.ReceiveBuff[0].Position;
  62. long Size = Context.Request.ReceiveBuff[0].Size;
  63. return GetReleasedAudioOutBufferImpl(Context, Position, Size);
  64. }
  65. public long ContainsAudioOutBuffer(ServiceCtx Context)
  66. {
  67. long Tag = Context.RequestData.ReadInt64();
  68. Context.ResponseData.Write(AudioOut.ContainsBuffer(Track, Tag) ? 1 : 0);
  69. return 0;
  70. }
  71. public long AppendAudioOutBufferAuto(ServiceCtx Context)
  72. {
  73. (long Position, long Size) = Context.Request.GetBufferType0x21();
  74. return AppendAudioOutBufferImpl(Context, Position);
  75. }
  76. public long AppendAudioOutBufferImpl(ServiceCtx Context, long Position)
  77. {
  78. long Tag = Context.RequestData.ReadInt64();
  79. AudioOutData Data = AMemoryHelper.Read<AudioOutData>(
  80. Context.Memory,
  81. Position);
  82. byte[] Buffer = Context.Memory.ReadBytes(
  83. Data.SampleBufferPtr,
  84. Data.SampleBufferSize);
  85. AudioOut.AppendBuffer(Track, Tag, Buffer);
  86. return 0;
  87. }
  88. public long GetReleasedAudioOutBufferAuto(ServiceCtx Context)
  89. {
  90. (long Position, long Size) = Context.Request.GetBufferType0x22();
  91. return GetReleasedAudioOutBufferImpl(Context, Position, Size);
  92. }
  93. public long GetReleasedAudioOutBufferImpl(ServiceCtx Context, long Position, long Size)
  94. {
  95. uint Count = (uint)((ulong)Size >> 3);
  96. long[] ReleasedBuffers = AudioOut.GetReleasedBuffers(Track, (int)Count);
  97. for (uint Index = 0; Index < Count; Index++)
  98. {
  99. long Tag = 0;
  100. if (Index < ReleasedBuffers.Length)
  101. {
  102. Tag = ReleasedBuffers[Index];
  103. }
  104. Context.Memory.WriteInt64(Position + Index * 8, Tag);
  105. }
  106. Context.ResponseData.Write(ReleasedBuffers.Length);
  107. return 0;
  108. }
  109. public void Dispose()
  110. {
  111. Dispose(true);
  112. }
  113. protected virtual void Dispose(bool Disposing)
  114. {
  115. if (Disposing)
  116. {
  117. AudioOut.CloseTrack(Track);
  118. ReleaseEvent.Dispose();
  119. }
  120. }
  121. }
  122. }