AudioManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Threading;
  19. namespace Ryujinx.Audio
  20. {
  21. /// <summary>
  22. /// Manage audio input and output system.
  23. /// </summary>
  24. public class AudioManager : IDisposable
  25. {
  26. /// <summary>
  27. /// Lock used to control the waiters registration.
  28. /// </summary>
  29. private object _lock = new object();
  30. /// <summary>
  31. /// Events signaled when the driver played audio buffers.
  32. /// </summary>
  33. private ManualResetEvent[] _updateRequiredEvents;
  34. /// <summary>
  35. /// Action to execute when the driver played audio buffers.
  36. /// </summary>
  37. private Action[] _actions;
  38. /// <summary>
  39. /// The worker thread in charge of handling sessions update.
  40. /// </summary>
  41. private Thread _workerThread;
  42. /// <summary>
  43. /// Create a new <see cref="AudioManager"/>.
  44. /// </summary>
  45. public AudioManager()
  46. {
  47. _updateRequiredEvents = new ManualResetEvent[2];
  48. _actions = new Action[2];
  49. // Termination event.
  50. _updateRequiredEvents[1] = new ManualResetEvent(false);
  51. _workerThread = new Thread(Update)
  52. {
  53. Name = "AudioManager.Worker"
  54. };
  55. }
  56. /// <summary>
  57. /// Start the <see cref="AudioManager"/>.
  58. /// </summary>
  59. public void Start()
  60. {
  61. if (_workerThread.IsAlive)
  62. {
  63. throw new InvalidOperationException();
  64. }
  65. _workerThread.Start();
  66. }
  67. /// <summary>
  68. /// Initialize update handlers.
  69. /// </summary>
  70. /// <param name="updatedRequiredEvent ">The driver event that will get signaled by the device driver when an audio buffer finished playing/being captured</param>
  71. /// <param name="outputCallback">The callback to call when an audio buffer finished playing</param>
  72. /// <param name="inputCallback">The callback to call when an audio buffer was captured</param>
  73. public void Initialize(ManualResetEvent updatedRequiredEvent, Action outputCallback, Action inputCallback)
  74. {
  75. lock (_lock)
  76. {
  77. _updateRequiredEvents[0] = updatedRequiredEvent;
  78. _actions[0] = outputCallback;
  79. _actions[1] = inputCallback;
  80. }
  81. }
  82. /// <summary>
  83. /// Entrypoint of the <see cref="_workerThread"/> in charge of updating the <see cref="AudioManager"/>.
  84. /// </summary>
  85. private void Update()
  86. {
  87. while (true)
  88. {
  89. int index = WaitHandle.WaitAny(_updateRequiredEvents);
  90. // Last index is here to indicate thread termination.
  91. if (index + 1 == _updateRequiredEvents.Length)
  92. {
  93. break;
  94. }
  95. lock (_lock)
  96. {
  97. foreach (Action action in _actions)
  98. {
  99. action?.Invoke();
  100. }
  101. _updateRequiredEvents[0].Reset();
  102. }
  103. }
  104. }
  105. public void Dispose()
  106. {
  107. Dispose(true);
  108. }
  109. protected virtual void Dispose(bool disposing)
  110. {
  111. if (disposing)
  112. {
  113. _updateRequiredEvents[1].Set();
  114. _workerThread.Join();
  115. _updateRequiredEvents[1].Dispose();
  116. }
  117. }
  118. }
  119. }