aaudio: dynamic buffer tuning never grows the buffer (set_buffer_size_in_frames reports Err on success)

Open Beginner friendly
#1,414 0 comments 0 reactions 0 assignees View on GitHub

@dannyward630 is already working on this.

Since Oct 25, 2026.

  • #1415 by @dannyward630 — open

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

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

host - aaudio platform - android

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, and mixer_bursts stays at its initial value.
  • If aaudio.mixer_bursts is unset, cpal falls back to its default of 2.
  • Every underrun therefore sets the buffer to (2 + 1) × burst, never larger.

Observed

  • Device: Samsung Galaxy A56, Android 16. aaudio.mixer_bursts is unset, burst is 96 frames.
  • An AAudio LowLatency/Shared output 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RustAudio/cpal

All issues in RustAudio/cpal

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.