AudioManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Threading;
  3. namespace Ryujinx.Audio
  4. {
  5. /// <summary>
  6. /// Manage audio input and output system.
  7. /// </summary>
  8. public class AudioManager : IDisposable
  9. {
  10. /// <summary>
  11. /// Lock used to control the waiters registration.
  12. /// </summary>
  13. private object _lock = new object();
  14. /// <summary>
  15. /// Events signaled when the driver played audio buffers.
  16. /// </summary>
  17. private ManualResetEvent[] _updateRequiredEvents;
  18. /// <summary>
  19. /// Action to execute when the driver played audio buffers.
  20. /// </summary>
  21. private Action[] _actions;
  22. /// <summary>
  23. /// The worker thread in charge of handling sessions update.
  24. /// </summary>
  25. private Thread _workerThread;
  26. private bool _isRunning;
  27. /// <summary>
  28. /// Create a new <see cref="AudioManager"/>.
  29. /// </summary>
  30. public AudioManager()
  31. {
  32. _updateRequiredEvents = new ManualResetEvent[2];
  33. _actions = new Action[2];
  34. _isRunning = false;
  35. // Termination event.
  36. _updateRequiredEvents[1] = new ManualResetEvent(false);
  37. _workerThread = new Thread(Update)
  38. {
  39. Name = "AudioManager.Worker"
  40. };
  41. }
  42. /// <summary>
  43. /// Start the <see cref="AudioManager"/>.
  44. /// </summary>
  45. public void Start()
  46. {
  47. if (_workerThread.IsAlive)
  48. {
  49. throw new InvalidOperationException();
  50. }
  51. _isRunning = true;
  52. _workerThread.Start();
  53. }
  54. /// <summary>
  55. /// Initialize update handlers.
  56. /// </summary>
  57. /// <param name="updatedRequiredEvent ">The driver event that will get signaled by the device driver when an audio buffer finished playing/being captured</param>
  58. /// <param name="outputCallback">The callback to call when an audio buffer finished playing</param>
  59. /// <param name="inputCallback">The callback to call when an audio buffer was captured</param>
  60. public void Initialize(ManualResetEvent updatedRequiredEvent, Action outputCallback, Action inputCallback)
  61. {
  62. lock (_lock)
  63. {
  64. _updateRequiredEvents[0] = updatedRequiredEvent;
  65. _actions[0] = outputCallback;
  66. _actions[1] = inputCallback;
  67. }
  68. }
  69. /// <summary>
  70. /// Entrypoint of the <see cref="_workerThread"/> in charge of updating the <see cref="AudioManager"/>.
  71. /// </summary>
  72. private void Update()
  73. {
  74. while (_isRunning)
  75. {
  76. int index = WaitHandle.WaitAny(_updateRequiredEvents);
  77. // Last index is here to indicate thread termination.
  78. if (index + 1 == _updateRequiredEvents.Length)
  79. {
  80. break;
  81. }
  82. lock (_lock)
  83. {
  84. foreach (Action action in _actions)
  85. {
  86. action?.Invoke();
  87. }
  88. _updateRequiredEvents[0].Reset();
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Stop updating the <see cref="AudioManager"/> without stopping the worker thread.
  94. /// </summary>
  95. public void StopUpdates()
  96. {
  97. _isRunning = false;
  98. }
  99. public void Dispose()
  100. {
  101. Dispose(true);
  102. }
  103. protected virtual void Dispose(bool disposing)
  104. {
  105. if (disposing)
  106. {
  107. _updateRequiredEvents[1].Set();
  108. _workerThread.Join();
  109. _updateRequiredEvents[1].Dispose();
  110. }
  111. }
  112. }
  113. }