sink_stream.h 7.2 KB

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