aaudio: dynamic buffer tuning never grows the buffer (set_buffer_size_in_frames reports Err on success)
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 79/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- android, rust
- Domain
- audio-video-rtc, backend
Research direction
Start in src/host/aaudio.rs at build_output_stream and trace the xrun-driven buffer tuning path. Check how ndk 0.9.0 reports set_buffer_size_in_frames results and whether reading back buffer_size_in_frames() confirms the requested size. Update the success check so mixer_bursts advances when resizing succeeds, then verify the tuner no longer requests a smaller buffer after an underrun.
Written by the indexing model from the issue text.
Description
On Android, the AAudio host's dynamic buffer tuning never grows the buffer. On a low-latency stream it can actually shrink the buffer after the first underrun.
Cause
build_output_stream in src/host/aaudio.rs turns tuning on when config.buffer_size == BufferSize::Default. On each xrun-count increase it runs:
if stream.set_buffer_size_in_frames(buffer_size).is_ok() {
tuning_for_callback.mixer_bursts.store(new_mixer_bursts, Ordering::Relaxed);
}
With ndk 0.9.0, set_buffer_size_in_frames returns Err(AudioError::__Unknown(new_size)) whenever the call succeeds (rust-mobile/ndk#520). So:
.is_ok()is always false, andmixer_burstsstays at its initial value.- If
aaudio.mixer_burstsis unset, cpal falls back to its default of2. - Every underrun therefore sets the buffer to
(2 + 1) × burst, never larger.
Observed
- Device: Samsung Galaxy A56, Android 16.
aaudio.mixer_burstsis unset, burst is 96 frames. - An AAudio
LowLatency/Sharedoutput stream on this device opens with a 768-frame buffer (8 bursts). - With the code above, the first underrun resizes it to 288 frames (3 bursts), which is a shrink.
- Later underruns set 288 again, so the tuner can never back off.
I confirmed the ndk return value directly on the device: set_buffer_size_in_frames(192) returns Err(__Unknown(192)), and buffer_size_in_frames() reads 192 afterwards. The cpal tuner path follows from the code; I didn't instrument it in cpal itself. This is in cpal 0.18.2 and still on master.
Suggested fix
Until ndk is fixed, treat a positive __Unknown value as success, or read the size back:
let _ = stream.set_buffer_size_in_frames(buffer_size);
if stream.buffer_size_in_frames() >= buffer_size {
tuning_for_callback.mixer_bursts.store(new_mixer_bursts, Ordering::Relaxed);
}
Reading back also handles AAudio rounding the requested size up.
- Dominant language
- Rust
- Stars
- 4k
- Forks
- 554
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 1
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from RustAudio/cpal
-
API design
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
correctness
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
WebAudio wasm build fails with wasm-bindgen panic=unwind closuresPossibly taken @bogusyogi claimed this 48 days ago. Openhost - webaudio platform - web
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
PipeWire: dropping callback captures on the realtime worker can terminate the process with SIGXCPUOpenhost - pipewire platform - linux
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
WebAudio: after a main-thread stall, playback runs at 2x speed until the time cursor catches upOpenhost - webaudio platform - web
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
ci: migrate pinned macos-15 runners to macos-26 and macos-26-intelPossibly taken @hawkingrei claimed this today. Openenhancement
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
nowledge-co/hawdb#2224 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
eclipse-score/coverage_tool#95 ·
Maintainers usually reply within 2 days
-
bug nvim
Difficulty 2/5 1-3 hours Newbie friendliness 74/100