AudioManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. private bool _isRunning;
  43. /// <summary>
  44. /// Create a new <see cref="AudioManager"/>.
  45. /// </summary>
  46. public AudioManager()
  47. {
  48. _updateRequiredEvents = new ManualResetEvent[2];
  49. _actions = new Action[2];
  50. _isRunning = false;
  51. // Termination event.
  52. _updateRequiredEvents[1] = new ManualResetEvent(false);
  53. _workerThread = new Thread(Update)
  54. {
  55. Name = "AudioManager.Worker"
  56. };
  57. }
  58. /// <summary>
  59. /// Start the <see cref="AudioManager"/>.
  60. /// </summary>
  61. public void Start()
  62. {
  63. if (_workerThread.IsAlive)
  64. {
  65. throw new InvalidOperationException();
  66. }
  67. _isRunning = true;
  68. _workerThread.Start();
  69. }
  70. /// <summary>
  71. /// Initialize update handlers.
  72. /// </summary>
  73. /// <param name="updatedRequiredEvent ">The driver event that will get signaled by the device driver when an audio buffer finished playing/being captured</param>
  74. /// <param name="outputCallback">The callback to call when an audio buffer finished playing</param>
  75. /// <param name="inputCallback">The callback to call when an audio buffer was captured</param>
  76. public void Initialize(ManualResetEvent updatedRequiredEvent, Action outputCallback, Action inputCallback)
  77. {
  78. lock (_lock)
  79. {
  80. _updateRequiredEvents[0] = updatedRequiredEvent;
  81. _actions[0] = outputCallback;
  82. _actions[1] = inputCallback;
  83. }
  84. }
  85. /// <summary>
  86. /// Entrypoint of the <see cref="_workerThread"/> in charge of updating the <see cref="AudioManager"/>.
  87. /// </summary>
  88. private void Update()
  89. {
  90. while (_isRunning)
  91. {
  92. int index = WaitHandle.WaitAny(_updateRequiredEvents);
  93. // Last index is here to indicate thread termination.
  94. if (index + 1 == _updateRequiredEvents.Length)
  95. {
  96. break;
  97. }
  98. lock (_lock)
  99. {
  100. foreach (Action action in _actions)
  101. {
  102. action?.Invoke();
  103. }
  104. _updateRequiredEvents[0].Reset();
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Stop updating the <see cref="AudioManager"/> without stopping the worker thread.
  110. /// </summary>
  111. public void StopUpdates()
  112. {
  113. _isRunning = false;
  114. }
  115. public void Dispose()
  116. {
  117. Dispose(true);
  118. }
  119. protected virtual void Dispose(bool disposing)
  120. {
  121. if (disposing)
  122. {
  123. _updateRequiredEvents[1].Set();
  124. _workerThread.Join();
  125. _updateRequiredEvents[1].Dispose();
  126. }
  127. }
  128. }
  129. }