AudioOutputSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.Output
  22. {
  23. /// <summary>
  24. /// Audio output system.
  25. /// </summary>
  26. public class AudioOutputSystem : IDisposable
  27. {
  28. /// <summary>
  29. /// The session id associated to the <see cref="AudioOutputSystem"/>.
  30. /// </summary>
  31. private int _sessionId;
  32. /// <summary>
  33. /// The session the <see cref="AudioOutputSystem"/>.
  34. /// </summary>
  35. private AudioDeviceSession _session;
  36. /// <summary>
  37. /// The target device name of the <see cref="AudioOutputSystem"/>.
  38. /// </summary>
  39. public string DeviceName { get; private set; }
  40. /// <summary>
  41. /// The target sample rate of the <see cref="AudioOutputSystem"/>.
  42. /// </summary>
  43. public uint SampleRate { get; private set; }
  44. /// <summary>
  45. /// The target channel count of the <see cref="AudioOutputSystem"/>.
  46. /// </summary>
  47. public uint ChannelCount { get; private set; }
  48. /// <summary>
  49. /// The target sample format of the <see cref="AudioOutputSystem"/>.
  50. /// </summary>
  51. public SampleFormat SampleFormat { get; private set; }
  52. /// <summary>
  53. /// The <see cref="AudioOutputManager"/> owning this.
  54. /// </summary>
  55. private AudioOutputManager _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="AudioOutputSystem"/>.
  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 output</param>
  71. public AudioOutputSystem(AudioOutputManager 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.DefaultDeviceOutputName;
  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="AudioOutputSystem"/>.
  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="AudioOutputSystem"/>.
  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="AudioOutputSystem"/></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 output.
  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. /// Get the release buffers.
  196. /// </summary>
  197. /// <param name="releasedBuffers">The buffer to write the release buffers</param>
  198. /// <param name="releasedCount">The count of released buffers</param>
  199. /// <returns>A <see cref="ResultCode"/> reporting an error or a success.</returns>
  200. public ResultCode GetReleasedBuffer(Span<ulong> releasedBuffers, out uint releasedCount)
  201. {
  202. releasedCount = 0;
  203. // Ensure that the first entry is set to zero if no entries are returned.
  204. if (releasedBuffers.Length > 0)
  205. {
  206. releasedBuffers[0] = 0;
  207. }
  208. lock (_parentLock)
  209. {
  210. for (int i = 0; i < releasedBuffers.Length; i++)
  211. {
  212. if (!_session.TryPopReleasedBuffer(out AudioBuffer buffer))
  213. {
  214. break;
  215. }
  216. releasedBuffers[i] = buffer.BufferTag;
  217. releasedCount++;
  218. }
  219. }
  220. return ResultCode.Success;
  221. }
  222. /// <summary>
  223. /// Get the current state of the <see cref="AudioOutputSystem"/>.
  224. /// </summary>
  225. /// <returns>Return the curent sta\te of the <see cref="AudioOutputSystem"/></returns>
  226. /// <returns></returns>
  227. public AudioDeviceState GetState()
  228. {
  229. lock (_parentLock)
  230. {
  231. return _session.GetState();
  232. }
  233. }
  234. /// <summary>
  235. /// Start the audio session.
  236. /// </summary>
  237. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  238. public ResultCode Start()
  239. {
  240. lock (_parentLock)
  241. {
  242. return _session.Start();
  243. }
  244. }
  245. /// <summary>
  246. /// Stop the audio session.
  247. /// </summary>
  248. /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
  249. public ResultCode Stop()
  250. {
  251. lock (_parentLock)
  252. {
  253. return _session.Stop();
  254. }
  255. }
  256. /// <summary>
  257. /// Get the volume of the session.
  258. /// </summary>
  259. /// <returns>The volume of the session</returns>
  260. public float GetVolume()
  261. {
  262. lock (_parentLock)
  263. {
  264. return _session.GetVolume();
  265. }
  266. }
  267. /// <summary>
  268. /// Set the volume of the session.
  269. /// </summary>
  270. /// <param name="volume">The new volume to set</param>
  271. public void SetVolume(float volume)
  272. {
  273. lock (_parentLock)
  274. {
  275. _session.SetVolume(volume);
  276. }
  277. }
  278. /// <summary>
  279. /// Get the count of buffer currently in use (server + driver side).
  280. /// </summary>
  281. /// <returns>The count of buffer currently in use</returns>
  282. public uint GetBufferCount()
  283. {
  284. lock (_parentLock)
  285. {
  286. return _session.GetBufferCount();
  287. }
  288. }
  289. /// <summary>
  290. /// Check if a buffer is present.
  291. /// </summary>
  292. /// <param name="bufferTag">The unique tag of the buffer</param>
  293. /// <returns>Return true if a buffer is present</returns>
  294. public bool ContainsBuffer(ulong bufferTag)
  295. {
  296. lock (_parentLock)
  297. {
  298. return _session.ContainsBuffer(bufferTag);
  299. }
  300. }
  301. /// <summary>
  302. /// Get the count of sample played in this session.
  303. /// </summary>
  304. /// <returns>The count of sample played in this session</returns>
  305. public ulong GetPlayedSampleCount()
  306. {
  307. lock (_parentLock)
  308. {
  309. return _session.GetPlayedSampleCount();
  310. }
  311. }
  312. /// <summary>
  313. /// Flush all buffers to the initial state.
  314. /// </summary>
  315. /// <returns>True if any buffers was flushed</returns>
  316. public bool FlushBuffers()
  317. {
  318. lock (_parentLock)
  319. {
  320. return _session.FlushBuffers();
  321. }
  322. }
  323. public void Dispose()
  324. {
  325. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  326. {
  327. Dispose(true);
  328. }
  329. }
  330. protected virtual void Dispose(bool disposing)
  331. {
  332. if (disposing)
  333. {
  334. _session.Dispose();
  335. _manager.Unregister(this);
  336. }
  337. }
  338. }
  339. }