Repository navigation
Low, constant latency audio path (playout buffer with drift correction) - #186
Merged
Merged
Conversation
The PC kept up to 1 s of audio in a plain FIFO with nothing holding its level, so network bursts and the phone/PC clock difference filled it up and the latency stayed there. Timing also jittered from TCP Nagle, large capture chunks, wifi power saving and the UI back pressuring the network task. PC: - new playout buffer feeding the sound card: holds a fixed jitter margin (configurable, default 30 ms), cancels the clock drift with a band limited resampler (flat to 20 kHz), and replaces late audio with silence then skips it, so the timeline never shifts - the drift comes from line fits of frames received (network side, last packet of each burst) and frames played over time, not from the jumpy buffer level - after start the lowest buffer level is probed for 3 s and playback is held back once if needed, so bursty phones get the full margin - UDP: late packets are dropped and lost ones are filled with silence - audio wave updates no longer block the network task - "Jitter buffer" setting in the connection section Android: - TCP_NODELAY and one write per packet (tcp and usb) - 5 ms packets, read on an urgent audio priority thread, with a large record buffer that is kept drained (falls back to the minimum size) - keep streaming silence while muted instead of stopping - wifi high perf / low latency lock while streaming over wifi Measured on a Galaxy S24 Ultra over ADB, beeps every 204.53 ms played by the PC speakers and recorded on VB-Cable: interval error 0.0025 ms rms, 0.0078 ms worst; end to end 232 ms (103 ms of it is VB-Cable). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- the overflow jump keeps the margin added by the startup probe, instead of undoing it - bursts are detected relative to the packet length, so slow processing (denoise, resampling) doesn't split them - warning and label match the "Jitter buffer" setting Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Some devices (e.g. Samsung) don't report PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED but still accept the unprocessed source. "Unprocessed (forced)" skips the support check and only falls back to voice recognition if the recorder fails to initialize. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
Thank you for this PR! This playout buffer is exactly what we need. While I'm not knowledgeable enough to review the math of the playout implementation, but I read through the code and it looks good. Also thanks for the other optimizations for latency! |
Contributor
Author
|
@teamclouday |
Contributor
|
I understand we needed this feature, but i hope not too much AI PR will get merged because the code will became unmaintainable otherwise |
ID-VerNe
pushed a commit
to ID-VerNe/AndroidMic
that referenced
this pull request
Nov 16, 2026
Low, constant latency audio path (playout buffer with drift correction)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
End-to-end latency was around 1 s and drifted. This PR keeps it low and constant: if two sounds reach the phone mic 204.53 ms apart, apps listening on the virtual mic also hear them 204.53 ms apart (measured within 0.014 ms, below). That matters for real-time analyzers and other measurement tools, and it also helps calls and recording.
What caused the delay
minBufferSize(often 20–40 ms) before anything was sent.Changes
PC (
RustApp)New
audio/playout.rssits between the network side and the cpal output stream:try_send, so they never block the network task.target_latency_ms, default 30 ms). The stream is rebuilt when the slider is released.f32, and is converted to the device format in the callback.Android
TCP_NODELAY, and one write per packet for TCP and USB.THREAD_PRIORITY_URGENT_AUDIOthread. The 500 ms record buffer is kept drained, so it adds no latency; if the device refuses that size it falls back to the minimum.WAKE_LOCKpermission).PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSEDbut still accept the unprocessed source.Measurements
Galaxy S24 Ultra (Android 16), ADB mode, 48 kHz stereo i24, jitter buffer 20 ms, Windows 11 with VB-Cable. PC speakers played beeps every 204.53 ms; the phone picked them up and the beeps were recorded from
CABLE Output. Each measurement took 40 s, after the latency lock.playout.rsadd clock drift from −200 to +300 ppm, 15 ms of jitter, and a 150 ms stall. They check that latency stays within 0.03 ms and each interval within 0.005 ms, and that the stall doesn't shift the timeline.Testing
cargo test --releasepasses;cargo check --benchespasses../gradlew assembleReleasepasses.Notes
🤖 Generated with Claude Code