Add hysteresis for audio discard

This commit is contained in:
Patrick Nicolas
2024-07-02 16:53:46 +02:00
parent b7de2b1943
commit bf16387fdd
+15 -11
View File
@@ -85,18 +85,22 @@ int32_t wivrn::android::audio::speaker_data_cb(AAudioStream * stream, void * use
} }
} }
// discard excess data (80ms buffer), so we don't accumulate latency // If we have more than 80ms of buffered data, discard some data
size_t target_buffer_size = frame_size * AAudioStream_getSampleRate(stream) * 0.08; if (self->buffer_size_bytes > frame_size * AAudioStream_getSampleRate(stream) * 0.08)
while (self->buffer_size_bytes > target_buffer_size and self->output_buffer.size() > 1)
{ {
auto tmp = self->output_buffer.read(); // discard excess data until we only have 40ms left
if (not tmp) size_t target_buffer_size = frame_size * AAudioStream_getSampleRate(stream) * 0.04;
break; while (self->buffer_size_bytes > target_buffer_size and self->output_buffer.size() > 1)
auto prev = self->buffer_size_bytes.fetch_sub(tmp->payload.size_bytes()); {
spdlog::info("Audio sync: discard {} bytes (buffer {} target {})", auto tmp = self->output_buffer.read();
tmp->payload.size_bytes(), if (not tmp)
prev, break;
target_buffer_size); auto prev = self->buffer_size_bytes.fetch_sub(tmp->payload.size_bytes());
spdlog::info("Audio sync: discard {} bytes (buffer {} target {})",
tmp->payload.size_bytes(),
prev,
target_buffer_size);
}
} }
return AAUDIO_CALLBACK_RESULT_CONTINUE; return AAUDIO_CALLBACK_RESULT_CONTINUE;