audio_buffers.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <mutex>
  6. #include <span>
  7. #include <vector>
  8. #include "audio_buffer.h"
  9. #include "audio_core/device/device_session.h"
  10. #include "core/core_timing.h"
  11. namespace AudioCore {
  12. constexpr s32 BufferAppendLimit = 4;
  13. /**
  14. * A ringbuffer of N audio buffers.
  15. * The buffer contains 3 sections:
  16. * Appended - Buffers added to the ring, but have yet to be sent to the audio backend.
  17. * Registered - Buffers sent to the backend and queued for playback.
  18. * Released - Buffers which have been played, and can now be recycled.
  19. * Any others are free/untracked.
  20. *
  21. * @tparam N - Maximum number of buffers in the ring.
  22. */
  23. template <size_t N>
  24. class AudioBuffers {
  25. public:
  26. explicit AudioBuffers(size_t limit) : append_limit{static_cast<u32>(limit)} {}
  27. /**
  28. * Append a new audio buffer to the ring.
  29. *
  30. * @param buffer - The new buffer.
  31. */
  32. void AppendBuffer(AudioBuffer& buffer) {
  33. std::scoped_lock l{lock};
  34. buffers[appended_index] = buffer;
  35. appended_count++;
  36. appended_index = (appended_index + 1) % append_limit;
  37. }
  38. /**
  39. * Register waiting buffers, up to a maximum of BufferAppendLimit.
  40. *
  41. * @param out_buffers - The buffers which were registered.
  42. */
  43. void RegisterBuffers(std::vector<AudioBuffer>& out_buffers) {
  44. std::scoped_lock l{lock};
  45. const s32 to_register{std::min(std::min(appended_count, BufferAppendLimit),
  46. BufferAppendLimit - registered_count)};
  47. for (s32 i = 0; i < to_register; i++) {
  48. s32 index{appended_index - appended_count};
  49. if (index < 0) {
  50. index += N;
  51. }
  52. out_buffers.push_back(buffers[index]);
  53. registered_count++;
  54. registered_index = (registered_index + 1) % append_limit;
  55. appended_count--;
  56. if (appended_count == 0) {
  57. break;
  58. }
  59. }
  60. }
  61. /**
  62. * Release a single buffer. Must be already registered.
  63. *
  64. * @param index - The buffer index to release.
  65. * @param timestamp - The released timestamp for this buffer.
  66. */
  67. void ReleaseBuffer(s32 index, s64 timestamp) {
  68. std::scoped_lock l{lock};
  69. buffers[index].played_timestamp = timestamp;
  70. registered_count--;
  71. released_count++;
  72. released_index = (released_index + 1) % append_limit;
  73. }
  74. /**
  75. * Release all registered buffers.
  76. *
  77. * @param core_timing - The CoreTiming instance
  78. * @param session - The device session
  79. *
  80. * @return Is the buffer was released.
  81. */
  82. bool ReleaseBuffers(Core::Timing::CoreTiming& core_timing, DeviceSession& session) {
  83. std::scoped_lock l{lock};
  84. bool buffer_released{false};
  85. while (registered_count > 0) {
  86. auto index{registered_index - registered_count};
  87. if (index < 0) {
  88. index += N;
  89. }
  90. // Check with the backend if this buffer can be released yet.
  91. if (!session.IsBufferConsumed(buffers[index])) {
  92. break;
  93. }
  94. ReleaseBuffer(index, core_timing.GetGlobalTimeNs().count());
  95. buffer_released = true;
  96. }
  97. return buffer_released || registered_count == 0;
  98. }
  99. /**
  100. * Get all released buffers.
  101. *
  102. * @param tags - Container to be filled with the released buffers' tags.
  103. * @return The number of buffers released.
  104. */
  105. u32 GetReleasedBuffers(std::span<u64> tags) {
  106. std::scoped_lock l{lock};
  107. u32 released{0};
  108. while (released_count > 0) {
  109. auto index{released_index - released_count};
  110. if (index < 0) {
  111. index += N;
  112. }
  113. auto& buffer{buffers[index]};
  114. released_count--;
  115. auto tag{buffer.tag};
  116. buffer.played_timestamp = 0;
  117. buffer.samples = 0;
  118. buffer.tag = 0;
  119. buffer.size = 0;
  120. if (tag == 0) {
  121. break;
  122. }
  123. tags[released++] = tag;
  124. if (released >= tags.size()) {
  125. break;
  126. }
  127. }
  128. return released;
  129. }
  130. /**
  131. * Get all appended and registered buffers.
  132. *
  133. * @param buffers_flushed - Output vector for the buffers which are released.
  134. * @param max_buffers - Maximum number of buffers to released.
  135. * @return The number of buffers released.
  136. */
  137. u32 GetRegisteredAppendedBuffers(std::vector<AudioBuffer>& buffers_flushed, u32 max_buffers) {
  138. std::scoped_lock l{lock};
  139. if (registered_count + appended_count == 0) {
  140. return 0;
  141. }
  142. size_t buffers_to_flush{
  143. std::min(static_cast<u32>(registered_count + appended_count), max_buffers)};
  144. if (buffers_to_flush == 0) {
  145. return 0;
  146. }
  147. while (registered_count > 0) {
  148. auto index{registered_index - registered_count};
  149. if (index < 0) {
  150. index += N;
  151. }
  152. buffers_flushed.push_back(buffers[index]);
  153. registered_count--;
  154. released_count++;
  155. released_index = (released_index + 1) % append_limit;
  156. if (buffers_flushed.size() >= buffers_to_flush) {
  157. break;
  158. }
  159. }
  160. while (appended_count > 0) {
  161. auto index{appended_index - appended_count};
  162. if (index < 0) {
  163. index += N;
  164. }
  165. buffers_flushed.push_back(buffers[index]);
  166. appended_count--;
  167. released_count++;
  168. released_index = (released_index + 1) % append_limit;
  169. if (buffers_flushed.size() >= buffers_to_flush) {
  170. break;
  171. }
  172. }
  173. return static_cast<u32>(buffers_flushed.size());
  174. }
  175. /**
  176. * Check if the given tag is in the buffers.
  177. *
  178. * @param tag - Unique tag of the buffer to search for.
  179. * @return True if the buffer is still in the ring, otherwise false.
  180. */
  181. bool ContainsBuffer(const u64 tag) const {
  182. std::scoped_lock l{lock};
  183. const auto registered_buffers{appended_count + registered_count + released_count};
  184. if (registered_buffers == 0) {
  185. return false;
  186. }
  187. auto index{released_index - released_count};
  188. if (index < 0) {
  189. index += append_limit;
  190. }
  191. for (s32 i = 0; i < registered_buffers; i++) {
  192. if (buffers[index].tag == tag) {
  193. return true;
  194. }
  195. index = (index + 1) % append_limit;
  196. }
  197. return false;
  198. }
  199. /**
  200. * Get the number of active buffers in the ring.
  201. * That is, appended, registered and released buffers.
  202. *
  203. * @return Number of active buffers.
  204. */
  205. u32 GetAppendedRegisteredCount() const {
  206. std::scoped_lock l{lock};
  207. return appended_count + registered_count;
  208. }
  209. /**
  210. * Get the total number of active buffers in the ring.
  211. * That is, appended, registered and released buffers.
  212. *
  213. * @return Number of active buffers.
  214. */
  215. u32 GetTotalBufferCount() const {
  216. std::scoped_lock l{lock};
  217. return static_cast<u32>(appended_count + registered_count + released_count);
  218. }
  219. /**
  220. * Flush all of the currently appended and registered buffers
  221. *
  222. * @param buffers_released - Output count for the number of buffers released.
  223. * @return True if buffers were successfully flushed, otherwise false.
  224. */
  225. bool FlushBuffers(u32& buffers_released) {
  226. std::scoped_lock l{lock};
  227. std::vector<AudioBuffer> buffers_flushed{};
  228. buffers_released = GetRegisteredAppendedBuffers(buffers_flushed, append_limit);
  229. if (registered_count > 0) {
  230. return false;
  231. }
  232. if (static_cast<u32>(released_count + appended_count) > append_limit) {
  233. return false;
  234. }
  235. return true;
  236. }
  237. u64 GetNextTimestamp() const {
  238. // Iterate backwards through the buffer queue, and take the most recent buffer's end
  239. std::scoped_lock l{lock};
  240. auto index{appended_index - 1};
  241. if (index < 0) {
  242. index += append_limit;
  243. }
  244. return buffers[index].end_timestamp;
  245. }
  246. private:
  247. /// Buffer lock
  248. mutable std::recursive_mutex lock{};
  249. /// The audio buffers
  250. std::array<AudioBuffer, N> buffers{};
  251. /// Current released index
  252. s32 released_index{};
  253. /// Number of released buffers
  254. s32 released_count{};
  255. /// Current registered index
  256. s32 registered_index{};
  257. /// Number of registered buffers
  258. s32 registered_count{};
  259. /// Current appended index
  260. s32 appended_index{};
  261. /// Number of appended buffers
  262. s32 appended_count{};
  263. /// Maximum number of buffers (default 32)
  264. u32 append_limit{};
  265. };
  266. } // namespace AudioCore