sink_stream.h 7.0 KB

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