AudioInputManager.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 Ryujinx.Audio.Common;
  18. using Ryujinx.Audio.Integration;
  19. using Ryujinx.Common.Logging;
  20. using Ryujinx.Memory;
  21. using System;
  22. using System.Diagnostics;
  23. using System.Linq;
  24. using System.Threading;
  25. namespace Ryujinx.Audio.Input
  26. {
  27. /// <summary>
  28. /// The audio input manager.
  29. /// </summary>
  30. public class AudioInputManager : IDisposable
  31. {
  32. private object _lock = new object();
  33. /// <summary>
  34. /// Lock used for session allocation.
  35. /// </summary>
  36. private object _sessionLock = new object();
  37. /// <summary>
  38. /// The session ids allocation table.
  39. /// </summary>
  40. private int[] _sessionIds;
  41. /// <summary>
  42. /// The device driver.
  43. /// </summary>
  44. private IHardwareDeviceDriver _deviceDriver;
  45. /// <summary>
  46. /// The events linked to each session.
  47. /// </summary>
  48. private IWritableEvent[] _sessionsBufferEvents;
  49. /// <summary>
  50. /// The <see cref="AudioInputSystem"/> session instances.
  51. /// </summary>
  52. private AudioInputSystem[] _sessions;
  53. /// <summary>
  54. /// The count of active sessions.
  55. /// </summary>
  56. private int _activeSessionCount;
  57. /// <summary>
  58. /// The dispose state.
  59. /// </summary>
  60. private int _disposeState;
  61. /// <summary>
  62. /// Create a new <see cref="AudioInputManager"/>.
  63. /// </summary>
  64. public AudioInputManager()
  65. {
  66. _sessionIds = new int[Constants.AudioInSessionCountMax];
  67. _sessions = new AudioInputSystem[Constants.AudioInSessionCountMax];
  68. _activeSessionCount = 0;
  69. for (int i = 0; i < _sessionIds.Length; i++)
  70. {
  71. _sessionIds[i] = i;
  72. }
  73. }
  74. /// <summary>
  75. /// Initialize the <see cref="AudioInputManager"/>.
  76. /// </summary>
  77. /// <param name="deviceDriver">The device driver.</param>
  78. /// <param name="sessionRegisterEvents">The events associated to each session.</param>
  79. public void Initialize(IHardwareDeviceDriver deviceDriver, IWritableEvent[] sessionRegisterEvents)
  80. {
  81. _deviceDriver = deviceDriver;
  82. _sessionsBufferEvents = sessionRegisterEvents;
  83. }
  84. /// <summary>
  85. /// Acquire a new session id.
  86. /// </summary>
  87. /// <returns>A new session id.</returns>
  88. private int AcquireSessionId()
  89. {
  90. lock (_sessionLock)
  91. {
  92. int index = _activeSessionCount;
  93. Debug.Assert(index < _sessionIds.Length);
  94. int sessionId = _sessionIds[index];
  95. _sessionIds[index] = -1;
  96. _activeSessionCount++;
  97. Logger.Info?.Print(LogClass.AudioRenderer, $"Registered new input ({sessionId})");
  98. return sessionId;
  99. }
  100. }
  101. /// <summary>
  102. /// Release a given <paramref name="sessionId"/>.
  103. /// </summary>
  104. /// <param name="sessionId">The session id to release.</param>
  105. private void ReleaseSessionId(int sessionId)
  106. {
  107. lock (_sessionLock)
  108. {
  109. Debug.Assert(_activeSessionCount > 0);
  110. int newIndex = --_activeSessionCount;
  111. _sessionIds[newIndex] = sessionId;
  112. }
  113. Logger.Info?.Print(LogClass.AudioRenderer, $"Unregistered input ({sessionId})");
  114. }
  115. /// <summary>
  116. /// Used to update audio input system.
  117. /// </summary>
  118. public void Update()
  119. {
  120. lock (_sessionLock)
  121. {
  122. foreach (AudioInputSystem input in _sessions)
  123. {
  124. input?.Update();
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// Register a new <see cref="AudioInputSystem"/>.
  130. /// </summary>
  131. /// <param name="input">The <see cref="AudioInputSystem"/> to register.</param>
  132. private void Register(AudioInputSystem input)
  133. {
  134. lock (_sessionLock)
  135. {
  136. _sessions[input.GetSessionId()] = input;
  137. }
  138. }
  139. /// <summary>
  140. /// Unregister a new <see cref="AudioInputSystem"/>.
  141. /// </summary>
  142. /// <param name="input">The <see cref="AudioInputSystem"/> to unregister.</param>
  143. internal void Unregister(AudioInputSystem input)
  144. {
  145. lock (_sessionLock)
  146. {
  147. int sessionId = input.GetSessionId();
  148. _sessions[input.GetSessionId()] = null;
  149. ReleaseSessionId(sessionId);
  150. }
  151. }
  152. /// <summary>
  153. /// Get the list of all audio inputs names.
  154. /// </summary>
  155. /// <param name="filtered">If true, filter disconnected devices</param>
  156. /// <returns>The list of all audio inputs name</returns>
  157. public string[] ListAudioIns(bool filtered)
  158. {
  159. if (filtered)
  160. {
  161. // TODO: Detect if the driver supports audio input
  162. }
  163. return new string[] { Constants.DefaultDeviceInputName };
  164. }
  165. /// <summary>
  166. /// Open a new <see cref="AudioInputSystem"/>.
  167. /// </summary>
  168. /// <param name="outputDeviceName">The output device name selected by the <see cref="AudioInputSystem"/></param>
  169. /// <param name="outputConfiguration">The output audio configuration selected by the <see cref="AudioInputSystem"/></param>
  170. /// <param name="obj">The new <see cref="AudioInputSystem"/></param>
  171. /// <param name="memoryManager">The memory manager that will be used for all guest memory operations</param>
  172. /// <param name="inputDeviceName">The input device name wanted by the user</param>
  173. /// <param name="sampleFormat">The sample format to use</param>
  174. /// <param name="parameter">The user configuration</param>
  175. /// <param name="appletResourceUserId">The applet resource user id of the application</param>
  176. /// <param name="processHandle">The process handle of the application</param>
  177. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  178. public ResultCode OpenAudioIn(out string outputDeviceName,
  179. out AudioOutputConfiguration outputConfiguration,
  180. out AudioInputSystem obj,
  181. IVirtualMemoryManager memoryManager,
  182. string inputDeviceName,
  183. SampleFormat sampleFormat,
  184. ref AudioInputConfiguration parameter,
  185. ulong appletResourceUserId,
  186. uint processHandle)
  187. {
  188. int sessionId = AcquireSessionId();
  189. _sessionsBufferEvents[sessionId].Clear();
  190. IHardwareDeviceSession deviceSession = _deviceDriver.OpenDeviceSession(IHardwareDeviceDriver.Direction.Input, memoryManager, sampleFormat, parameter.SampleRate, parameter.ChannelCount);
  191. AudioInputSystem audioIn = new AudioInputSystem(this, _lock, deviceSession, _sessionsBufferEvents[sessionId]);
  192. ResultCode result = audioIn.Initialize(inputDeviceName, sampleFormat, ref parameter, sessionId);
  193. if (result == ResultCode.Success)
  194. {
  195. outputDeviceName = audioIn.DeviceName;
  196. outputConfiguration = new AudioOutputConfiguration
  197. {
  198. ChannelCount = audioIn.ChannelCount,
  199. SampleFormat = audioIn.SampleFormat,
  200. SampleRate = audioIn.SampleRate,
  201. AudioOutState = audioIn.GetState(),
  202. };
  203. obj = audioIn;
  204. Register(audioIn);
  205. }
  206. else
  207. {
  208. ReleaseSessionId(sessionId);
  209. obj = null;
  210. outputDeviceName = null;
  211. outputConfiguration = default;
  212. }
  213. return result;
  214. }
  215. public void Dispose()
  216. {
  217. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  218. {
  219. Dispose(true);
  220. }
  221. }
  222. protected virtual void Dispose(bool disposing)
  223. {
  224. if (disposing)
  225. {
  226. // Clone the sessions array to dispose them outside the lock.
  227. AudioInputSystem[] sessions;
  228. lock (_sessionLock)
  229. {
  230. sessions = _sessions.ToArray();
  231. }
  232. foreach (AudioInputSystem input in sessions)
  233. {
  234. input?.Dispose();
  235. }
  236. }
  237. }
  238. }
  239. }