Prevent excessive audio output latency
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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<xrt::drivers::wivrn::audio_data, 1000> output_buffer;
|
||||
utils::ring_buffer<xrt::drivers::wivrn::audio_data, 100> output_buffer;
|
||||
std::atomic<size_t> buffer_size_bytes;
|
||||
|
||||
xrt::drivers::wivrn::audio_data speaker_tmp;
|
||||
AAudioStreamStruct * speaker = nullptr;
|
||||
|
||||
@@ -27,10 +27,10 @@ namespace utils
|
||||
{
|
||||
|
||||
// single writer/single reader rung buffer
|
||||
template <typename T, size_t size>
|
||||
template <typename T, size_t capacity>
|
||||
class ring_buffer
|
||||
{
|
||||
std::array<T, size> container;
|
||||
std::array<T, capacity> container;
|
||||
// last position read
|
||||
std::atomic<size_t> 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
|
||||
|
||||
Reference in New Issue
Block a user