AudioInputSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 System;
  20. using System.Threading;
  21. namespace Ryujinx.Audio.Input
  22. {
  23. /// <summary>
  24. /// Audio input system.
  25. /// </summary>
  26. public class AudioInputSystem : IDisposable
  27. {
  28. /// <summary>
  29. /// The session id associated to the <see cref="AudioInputSystem"/>.
  30. /// </summary>
  31. private int _sessionId;
  32. /// <summary>
  33. /// The session the <see cref="AudioInputSystem"/>.
  34. /// </summary>
  35. private AudioDeviceSession _session;
  36. /// <summary>
  37. /// The target device name of the <see cref="AudioInputSystem"/>.
  38. /// </summary>
  39. public string DeviceName { get; private set; }
  40. /// <summary>
  41. /// The target sample rate of the <see cref="AudioInputSystem"/>.
  42. /// </summary>
  43. public uint SampleRate { get; private set; }
  44. /// <summary>
  45. /// The target channel count of the <see cref="AudioInputSystem"/>.
  46. /// </summary>
  47. public uint ChannelCount { get; private set; }
  48. /// <summary>
  49. /// The target sample format of the <see cref="AudioInputSystem"/>.
  50. /// </summary>
  51. public SampleFormat SampleFormat { get; private set; }
  52. /// <summary>
  53. /// The <see cref="AudioInputManager"/> owning this.
  54. /// </summary>
  55. private AudioInputManager _manager;
  56. /// <summary>
  57. /// The lock of the parent.
  58. /// </summary>
  59. private object _parentLock;
  60. /// <summary>
  61. /// The dispose state.
  62. /// </summary>
  63. private int _disposeState;
  64. /// <summary>
  65. /// Create a new <see cref="AudioInputSystem"/>.
  66. /// </summary>
  67. /// <param name="manager">The manager instance</param>
  68. /// <param name="parentLock">The lock of the manager</param>
  69. /// <param name="deviceSession">The hardware device session</param>
  70. /// <param name="bufferEvent">The buffer release event of the audio input</param>
  71. public AudioInputSystem(AudioInputManager manager, object parentLock, IHardwareDeviceSession deviceSession, IWritableEvent bufferEvent)
  72. {
  73. _manager = manager;
  74. _parentLock = parentLock;
  75. _session = new AudioDeviceSession(deviceSession, bufferEvent);
  76. }
  77. /// <summary>
  78. /// Get the default device name on the system.
  79. /// </summary>
  80. /// <returns>The default device name on the system.</returns>
  81. private static string GetDeviceDefaultName()
  82. {
  83. return Constants.DefaultDeviceInputName;
  84. }
  85. /// <summary>
  86. /// Check if a given configuration and device name is valid on the system.
  87. /// </summary>
  88. /// <param name="configuration">The configuration to check.</param>
  89. /// <param name="deviceName">The device name to check.</param>
  90. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  91. private static ResultCode IsConfigurationValid(ref AudioInputConfiguration configuration, string deviceName)
  92. {
  93. if (deviceName.Length != 0 && !deviceName.Equals(GetDeviceDefaultName()))
  94. {
  95. return ResultCode.DeviceNotFound;
  96. }
  97. else if (configuration.SampleRate != 0 && configuration.SampleRate != Constants.TargetSampleRate)
  98. {
  99. return ResultCode.UnsupportedSampleRate;
  100. }
  101. else if (configuration.ChannelCount != 0 && configuration.ChannelCount != 1 && configuration.ChannelCount != 2 && configuration.ChannelCount != 6)
  102. {
  103. return ResultCode.UnsupportedChannelConfiguration;
  104. }
  105. return ResultCode.Success;
  106. }
  107. /// <summary>
  108. /// Get the released buffer event.
  109. /// </summary>
  110. /// <returns>The released buffer event</returns>
  111. public IWritableEvent RegisterBufferEvent()
  112. {
  113. lock (_parentLock)
  114. {
  115. return _session.GetBufferEvent();
  116. }
  117. }
  118. /// <summary>
  119. /// Update the <see cref="AudioInputSystem"/>.
  120. /// </summary>
  121. public void Update()
  122. {
  123. lock (_parentLock)
  124. {
  125. _session.Update();
  126. }
  127. }
  128. /// <summary>
  129. /// Get the id of this session.
  130. /// </summary>
  131. /// <returns>The id of this session</returns>
  132. public int GetSessionId()
  133. {
  134. return _sessionId;
  135. }
  136. /// <summary>
  137. /// Initialize the <see cref="AudioInputSystem"/>.
  138. /// </summary>
  139. /// <param name="inputDeviceName">The input device name wanted by the user</param>
  140. /// <param name="sampleFormat">The sample format to use</param>
  141. /// <param name="parameter">The user configuration</param>
  142. /// <param name="sessionId">The session id associated to this <see cref="AudioInputSystem"/></param>
  143. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  144. public ResultCode Initialize(string inputDeviceName, SampleFormat sampleFormat, ref AudioInputConfiguration parameter, int sessionId)
  145. {
  146. _sessionId = sessionId;
  147. ResultCode result = IsConfigurationValid(ref parameter, inputDeviceName);
  148. if (result == ResultCode.Success)
  149. {
  150. if (inputDeviceName.Length == 0)
  151. {
  152. DeviceName = GetDeviceDefaultName();
  153. }
  154. else
  155. {
  156. DeviceName = inputDeviceName;
  157. }
  158. if (parameter.ChannelCount == 6)
  159. {
  160. ChannelCount = 6;
  161. }
  162. else
  163. {
  164. ChannelCount = 2;
  165. }
  166. SampleFormat = sampleFormat;
  167. SampleRate = Constants.TargetSampleRate;
  168. }
  169. return result;
  170. }
  171. /// <summary>
  172. /// Append a new audio buffer to the audio input.
  173. /// </summary>
  174. /// <param name="bufferTag">The unique tag of this buffer.</param>
  175. /// <param name="userBuffer">The buffer informations.</param>
  176. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  177. public ResultCode AppendBuffer(ulong bufferTag, ref AudioUserBuffer userBuffer)
  178. {
  179. lock (_parentLock)
  180. {
  181. AudioBuffer buffer = new AudioBuffer
  182. {
  183. BufferTag = bufferTag,
  184. DataPointer = userBuffer.Data,
  185. DataSize = userBuffer.DataSize
  186. };
  187. if (_session.AppendBuffer(buffer))
  188. {
  189. return ResultCode.Success;
  190. }
  191. return ResultCode.BufferRingFull;
  192. }
  193. }
  194. /// <summary>
  195. /// Append a new audio buffer to the audio input.
  196. /// </summary>
  197. /// <remarks>This is broken by design, only added for completness.</remarks>
  198. /// <param name="bufferTag">The unique tag of this buffer.</param>
  199. /// <param name="userBuffer">The buffer informations.</param>
  200. /// <param name="handle">Some unknown handle.</param>
  201. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  202. public ResultCode AppendUacBuffer(ulong bufferTag, ref AudioUserBuffer userBuffer, uint handle)
  203. {
  204. lock (_parentLock)
  205. {
  206. AudioBuffer buffer = new AudioBuffer
  207. {
  208. BufferTag = bufferTag,
  209. DataPointer = userBuffer.Data,
  210. DataSize = userBuffer.DataSize
  211. };
  212. if (_session.AppendUacBuffer(buffer, handle))
  213. {
  214. return ResultCode.Success;
  215. }
  216. return ResultCode.BufferRingFull;
  217. }
  218. }
  219. /// <summary>
  220. /// Get the release buffers.
  221. /// </summary>
  222. /// <param name="releasedBuffers">The buffer to write the release buffers</param>
  223. /// <param name="releasedCount">The count of released buffers</param>
  224. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  225. public ResultCode GetReleasedBuffers(Span<ulong> releasedBuffers, out uint releasedCount)
  226. {
  227. releasedCount = 0;
  228. // Ensure that the first entry is set to zero if no entries are returned.
  229. if (releasedBuffers.Length > 0)
  230. {
  231. releasedBuffers[0] = 0;
  232. }
  233. lock (_parentLock)
  234. {
  235. for (int i = 0; i < releasedBuffers.Length; i++)
  236. {
  237. if (!_session.TryPopReleasedBuffer(out AudioBuffer buffer))
  238. {
  239. break;
  240. }
  241. releasedBuffers[i] = buffer.BufferTag;
  242. releasedCount++;
  243. }
  244. }
  245. return ResultCode.Success;
  246. }
  247. /// <summary>
  248. /// Get the current state of the <see cref="AudioInputSystem"/>.
  249. /// </summary>
  250. /// <returns>Return the curent sta\te of the <see cref="AudioInputSystem"/></returns>
  251. public AudioDeviceState GetState()
  252. {
  253. lock (_parentLock)
  254. {
  255. return _session.GetState();
  256. }
  257. }
  258. /// <summary>
  259. /// Start the audio session.
  260. /// </summary>
  261. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  262. public ResultCode Start()
  263. {
  264. lock (_parentLock)
  265. {
  266. return _session.Start();
  267. }
  268. }
  269. /// <summary>
  270. /// Stop the audio session.
  271. /// </summary>
  272. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  273. public ResultCode Stop()
  274. {
  275. lock (_parentLock)
  276. {
  277. return _session.Stop();
  278. }
  279. }
  280. /// <summary>
  281. /// Get the volume of the session.
  282. /// </summary>
  283. /// <returns>The volume of the session</returns>
  284. public float GetVolume()
  285. {
  286. lock (_parentLock)
  287. {
  288. return _session.GetVolume();
  289. }
  290. }
  291. /// <summary>
  292. /// Set the volume of the session.
  293. /// </summary>
  294. /// <param name="volume">The new volume to set</param>
  295. public void SetVolume(float volume)
  296. {
  297. lock (_parentLock)
  298. {
  299. _session.SetVolume(volume);
  300. }
  301. }
  302. /// <summary>
  303. /// Get the count of buffer currently in use (server + driver side).
  304. /// </summary>
  305. /// <returns>The count of buffer currently in use</returns>
  306. public uint GetBufferCount()
  307. {
  308. lock (_parentLock)
  309. {
  310. return _session.GetBufferCount();
  311. }
  312. }
  313. /// <summary>
  314. /// Check if a buffer is present.
  315. /// </summary>
  316. /// <param name="bufferTag">The unique tag of the buffer</param>
  317. /// <returns>Return true if a buffer is present</returns>
  318. public bool ContainsBuffer(ulong bufferTag)
  319. {
  320. lock (_parentLock)
  321. {
  322. return _session.ContainsBuffer(bufferTag);
  323. }
  324. }
  325. /// <summary>
  326. /// Get the count of sample played in this session.
  327. /// </summary>
  328. /// <returns>The count of sample played in this session</returns>
  329. public ulong GetPlayedSampleCount()
  330. {
  331. lock (_parentLock)
  332. {
  333. return _session.GetPlayedSampleCount();
  334. }
  335. }
  336. /// <summary>
  337. /// Flush all buffers to the initial state.
  338. /// </summary>
  339. /// <returns>True if any buffers was flushed</returns>
  340. public bool FlushBuffers()
  341. {
  342. lock (_parentLock)
  343. {
  344. return _session.FlushBuffers();
  345. }
  346. }
  347. public void Dispose()
  348. {
  349. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  350. {
  351. Dispose(true);
  352. }
  353. }
  354. protected virtual void Dispose(bool disposing)
  355. {
  356. if (disposing)
  357. {
  358. _session.Dispose();
  359. _manager.Unregister(this);
  360. }
  361. }
  362. }
  363. }