From f4b999f9fcc0a6a5085aa6919d36163add136dad Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 5 Apr 2024 11:59:25 +0200 Subject: [PATCH] Prevent excessive audio output latency --- client/audio/android/audio.cpp | 13 +++++++++++++ client/audio/android/audio.h | 3 ++- common/utils/ring_buffer.h | 15 +++++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/client/audio/android/audio.cpp b/client/audio/android/audio.cpp index facdf7cb..bdf2b4a5 100644 --- a/client/audio/android/audio.cpp +++ b/client/audio/android/audio.cpp @@ -65,6 +65,7 @@ int32_t wivrn::android::audio::speaker_data_cb(AAudioStream * stream, void * use self->speaker_tmp.payload = self->speaker_tmp.payload.subspan(tmp_remain); audio_data += tmp_remain; num_frames -= tmp_remain / frame_size; + self->buffer_size_bytes.fetch_sub(tmp_remain); } else { @@ -78,6 +79,17 @@ int32_t wivrn::android::audio::speaker_data_cb(AAudioStream * stream, void * use } } + // discard excess data, so we don't accumulate latency + size_t target_buffer_size = 20'000'000 * frame_size / AAudioStream_getSampleRate(stream); + while (self->buffer_size_bytes > target_buffer_size and self->output_buffer.size() > 1) + { + auto tmp = self->output_buffer.read(); + if (not tmp) + break; + self->buffer_size_bytes.fetch_sub(tmp->payload.size_bytes()); + spdlog::info("Audio sync: discard {} bytes", tmp->payload.size_bytes()); + } + return AAUDIO_CALLBACK_RESULT_CONTINUE; } @@ -187,6 +199,7 @@ wivrn::android::audio::~audio() void wivrn::android::audio::operator()(xrt::drivers::wivrn::audio_data && data) { + buffer_size_bytes.fetch_add(data.payload.size_bytes()); output_buffer.write(std::move(data)); } diff --git a/client/audio/android/audio.h b/client/audio/android/audio.h index fd6efbdd..ae66b612 100644 --- a/client/audio/android/audio.h +++ b/client/audio/android/audio.h @@ -38,7 +38,8 @@ class audio static int32_t speaker_data_cb(AAudioStreamStruct *, void *, void *, int32_t); static int32_t microphone_data_cb(AAudioStreamStruct *, void *, void *, int32_t); - utils::ring_buffer output_buffer; + utils::ring_buffer output_buffer; + std::atomic buffer_size_bytes; xrt::drivers::wivrn::audio_data speaker_tmp; AAudioStreamStruct * speaker = nullptr; diff --git a/common/utils/ring_buffer.h b/common/utils/ring_buffer.h index b1dbb67e..7da6c9d7 100644 --- a/common/utils/ring_buffer.h +++ b/common/utils/ring_buffer.h @@ -27,10 +27,10 @@ namespace utils { // single writer/single reader rung buffer -template +template class ring_buffer { - std::array container; + std::array container; // last position read std::atomic read_index = 0; // last position written @@ -39,7 +39,7 @@ class ring_buffer public: bool write(T && t) { - size_t next_write = (write_index + 1) % size; + size_t next_write = (write_index + 1) % capacity; if (next_write == read_index) return false; container[next_write] = std::move(t); @@ -51,11 +51,18 @@ public: { if (read_index == write_index) return {}; - size_t next_read = (read_index + 1) % size; + size_t next_read = (read_index + 1) % capacity; T res = std::move(container[next_read]); read_index.store(next_read); return std::move(res); } + + size_t size() const + { + size_t w = write_index; + size_t r = read_index; + return std::min(w - r, capacity + w - r); + } }; } // namespace utils