sink_stream.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <atomic>
  6. #include <chrono>
  7. #include <memory>
  8. #include <mutex>
  9. #include <span>
  10. #include <vector>
  11. #include "audio_core/common/common.h"
  12. #include "common/common_types.h"
  13. #include "common/polyfill_thread.h"
  14. #include "common/reader_writer_queue.h"
  15. #include "common/ring_buffer.h"
  16. #include "common/thread.h"
  17. namespace Core {
  18. class System;
  19. } // namespace Core
  20. namespace AudioCore::Sink {
  21. enum class StreamType {
  22. Render,
  23. Out,
  24. In,
  25. };
  26. struct SinkBuffer {
  27. u64 frames;
  28. u64 frames_played;
  29. u64 tag;
  30. bool consumed;
  31. };
  32. /**
  33. * Contains a real backend stream for outputting samples to hardware,
  34. * created only via a Sink (See Sink::AcquireSinkStream).
  35. *
  36. * Accepts a SinkBuffer and samples in PCM16 format to be output (see AppendBuffer).
  37. * Appended buffers act as a FIFO queue, and will be held until played.
  38. * You should regularly call IsBufferConsumed with the unique SinkBuffer tag to check if the buffer
  39. * has been consumed.
  40. *
  41. * Since these are a FIFO queue, IsBufferConsumed must be checked in the same order buffers were
  42. * appended, skipping a buffer will result in the queue getting stuck, and all following buffers to
  43. * never release.
  44. *
  45. * If the buffers appear to be stuck, you can stop and re-open an IAudioIn/IAudioOut service (this
  46. * is what games do), or call ClearQueue to flush all of the buffers without a full restart.
  47. */
  48. class SinkStream {
  49. public:
  50. explicit SinkStream(Core::System& system_, StreamType type_) : system{system_}, type{type_} {}
  51. virtual ~SinkStream() {}
  52. /**
  53. * Finalize the sink stream.
  54. */
  55. virtual void Finalize() {}
  56. /**
  57. * Start the sink stream.
  58. *
  59. * @param resume - Set to true if this is resuming the stream a previously-active stream.
  60. * Default false.
  61. */
  62. virtual void Start(bool resume = false) {}
  63. /**
  64. * Stop the sink stream.
  65. */
  66. virtual void Stop() {}
  67. /**
  68. * Check if the stream is paused.
  69. *
  70. * @return True if paused, otherwise false.
  71. */
  72. bool IsPaused() const {
  73. return paused;
  74. }
  75. /**
  76. * Get the number of system channels in this stream.
  77. *
  78. * @return Number of system channels.
  79. */
  80. u32 GetSystemChannels() const {
  81. return system_channels;
  82. }
  83. /**
  84. * Set the number of channels the system expects.
  85. *
  86. * @param channels - New number of system channels.
  87. */
  88. void SetSystemChannels(u32 channels) {
  89. system_channels = channels;
  90. }
  91. /**
  92. * Get the number of channels the hardware supports.
  93. *
  94. * @return Number of channels supported.
  95. */
  96. u32 GetDeviceChannels() const {
  97. return device_channels;
  98. }
  99. /**
  100. * Get the system volume.
  101. *
  102. * @return The current system volume.
  103. */
  104. f32 GetSystemVolume() const {
  105. return system_volume;
  106. }
  107. /**
  108. * Get the device volume.
  109. *
  110. * @return The current device volume.
  111. */
  112. f32 GetDeviceVolume() const {
  113. return device_volume;
  114. }
  115. /**
  116. * Set the system volume.
  117. *
  118. * @param volume_ - The new system volume.
  119. */
  120. void SetSystemVolume(f32 volume_) {
  121. system_volume = volume_;
  122. }
  123. /**
  124. * Set the device volume.
  125. *
  126. * @param volume_ - The new device volume.
  127. */
  128. void SetDeviceVolume(f32 volume_) {
  129. device_volume = volume_;
  130. }
  131. /**
  132. * Get the number of queued audio buffers.
  133. *
  134. * @return The number of queued buffers.
  135. */
  136. u32 GetQueueSize() const {
  137. return queued_buffers.load();
  138. }
  139. /**
  140. * Set the maximum buffer queue size.
  141. */
  142. void SetRingSize(u32 ring_size) {
  143. max_queue_size = ring_size;
  144. }
  145. /**
  146. * Append a new buffer and its samples to a waiting queue to play.
  147. *
  148. * @param buffer - Audio buffer information to be queued.
  149. * @param samples - The s16 samples to be queue for playback.
  150. */
  151. virtual void AppendBuffer(SinkBuffer& buffer, std::span<s16> samples);
  152. /**
  153. * Release a buffer. Audio In only, will fill a buffer with recorded samples.
  154. *
  155. * @param num_samples - Maximum number of samples to receive.
  156. * @return Vector of recorded samples. May have fewer than num_samples.
  157. */
  158. virtual std::vector<s16> ReleaseBuffer(u64 num_samples);
  159. /**
  160. * Empty out the buffer queue.
  161. */
  162. void ClearQueue();
  163. /**
  164. * Callback for AudioIn.
  165. *
  166. * @param input_buffer - Input buffer to be filled with samples.
  167. * @param num_frames - Number of frames to be filled.
  168. */
  169. void ProcessAudioIn(std::span<const s16> input_buffer, std::size_t num_frames);
  170. /**
  171. * Callback for AudioOut and AudioRenderer.
  172. *
  173. * @param output_buffer - Output buffer to be filled with samples.
  174. * @param num_frames - Number of frames to be filled.
  175. */
  176. void ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames);
  177. /**
  178. * Get the total number of samples expected to have been played by this stream.
  179. *
  180. * @return The number of samples.
  181. */
  182. u64 GetExpectedPlayedSampleCount();
  183. /**
  184. * Waits for free space in the sample ring buffer
  185. */
  186. void WaitFreeSpace(std::stop_token stop_token);
  187. protected:
  188. /**
  189. * Unblocks the ADSP if the stream is paused.
  190. */
  191. void SignalPause();
  192. protected:
  193. /// Core system
  194. Core::System& system;
  195. /// Type of this stream
  196. StreamType type;
  197. /// Set by the audio render/in/out system which uses this stream
  198. u32 system_channels{2};
  199. /// Channels supported by hardware
  200. u32 device_channels{2};
  201. /// Is this stream currently paused?
  202. std::atomic<bool> paused{true};
  203. /// Name of this stream
  204. std::string name{};
  205. private:
  206. /// Ring buffer of the samples waiting to be played or consumed
  207. Common::RingBuffer<s16, 0x10000> samples_buffer;
  208. /// Audio buffers queued and waiting to play
  209. Common::ReaderWriterQueue<SinkBuffer> queue;
  210. /// The currently-playing audio buffer
  211. SinkBuffer playing_buffer{};
  212. /// The last played (or received) frame of audio, used when the callback underruns
  213. std::array<s16, MaxChannels> last_frame{};
  214. /// Number of buffers waiting to be played
  215. std::atomic<u32> queued_buffers{};
  216. /// The ring size for audio out buffers (usually 4, rarely 2 or 8)
  217. u32 max_queue_size{};
  218. /// Locks access to sample count tracking info
  219. std::mutex sample_count_lock;
  220. /// Minimum number of total samples that have been played since the last callback
  221. u64 min_played_sample_count{};
  222. /// Maximum number of total samples that can be played since the last callback
  223. u64 max_played_sample_count{};
  224. /// The time the two above tracking variables were last written to
  225. std::chrono::nanoseconds last_sample_count_update_time{};
  226. /// Set by the audio render/in/out system which uses this stream
  227. f32 system_volume{1.0f};
  228. /// Set via IAudioDevice service calls
  229. f32 device_volume{1.0f};
  230. /// Signalled when ring buffer entries are consumed
  231. std::condition_variable_any release_cv;
  232. std::mutex release_mutex;
  233. };
  234. using SinkStreamPtr = std::unique_ptr<SinkStream>;
  235. } // namespace AudioCore::Sink