Skip to content

fix android: getHardwareOutputLatency drifts when the AAudio position rate differs - #621

Merged
alnitak merged 1 commit into
alnitak:mainfrom
dotkebi:fix/aaudio-hw-latency-drift-pr
Nov 25, 2026
Merged

alnitak merged 1 commit into
alnitak:mainfrom
dotkebi:fix/aaudio-hw-latency-drift-pr

Conversation

@dotkebi

@dotkebi dotkebi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #620.

On some devices the framePosition from AAudioStream_getTimestamp() does not advance at the stream rate. On a Samsung SM-G991N (Android 15) a 44.1 kHz stream's framePosition advances ~0.912× as fast as framesWritten, so getHardwareOutputLatency() grows by ~88 ms per second of playback.

This PR measures the ratio between the two counters on the same stream and converts framePosition into written frames before subtracting:

ratio   = Δ framePosition / Δ framesWritten   (since the first sample of the stream, ≥ 1 s of frames)
latency = (framesWritten − framePosition / ratio) / sampleRate − (now − timestamp)
  • Unchanged on conforming devices: a ratio within 2% of 1 is taken as exactly 1, so the result is the same formula as before.
  • Device stop/start: the ratio is measured against framesWritten, not the wall clock. Both counters stand still while the device is stopped (e.g. by the idle timeout), so a restart doesn't disturb the measurement and doesn't start a new warm-up. (An earlier version I tried measured the rate against the wall clock; on the device it read 0 for good after the first idle stop.)
  • New stream: a different pStreamPlayback, or a counter that goes backwards (a reroute that reopens the stream), restarts the measurement.
  • Warm-up: for the first second of a stream the ratio isn't known yet, so it returns 0. That matches the documented "warm-up → Duration.zero" behaviour, but on conforming devices it's one second longer than before. I can fall back to the old formula during that second if you prefer.
  • A ratio outside [0.5, 2] is treated as a bad reading (returns 0).
  • The state is behind a mutex, since the call can come from more than one isolate.

The math lives in src/soloud/src/backend/miniaudio/aaudio_timestamp_latency.h, which has no platform dependencies, so it can be tested on the host. test/run_aaudio_timestamp_latency_test.sh replays the SM-G991N readings (the result stays at 25–28 ms where the old formula goes from 171 ms to 2.2 s) and simulated streams: a conforming device (identical to the old formula), a slow-position device over 2 minutes, stop/restart gaps of 30 s and 10 min, a new stream, a reopened stream, and an implausible ratio. I used the current flat test/ layout. If #617 lands first, I'll move the files to test/common/aaudio_timestamp_latency_test.{cpp,sh}.

Limitations:

  • I've seen the slow framePosition on one device only, and I don't know what it actually counts (0.9117 isn't 44100/48000).

  • If an app first calls the function long after the stream started, the first values right after warm-up are less precise (the ratio error is multiplied by the frames already played). They converge as the measuring span grows.

  • Verified on SM-G991N (Android 15) with this branch, playing a looping sine and reading the value every 200 ms through three playback periods (15 s, 10 s, 10 s) separated by 3 s and 15 s pauses, which let the 500 ms idle timeout stop the device:

    Build 1st play after 3 s idle stop after 15 s idle stop
    main (a7e9f8d) 65 → 1,386 ms 1,474 → 2,324 ms 2,395 → 3,251 ms
    this PR 0 for 1.0 s, then 30.7–31.1 ms 30.8–30.9 ms (valid 0.2 s after restart) 30.9 ms (valid 0.4 s after restart)

    I haven't tested it on a device whose counters agree. There the ratio is snapped to 1, so the result is the old formula (covered by the host test).

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

… rate differs

On some devices AAudioStream_getTimestamp()'s framePosition does not
advance at the stream rate. On a Samsung SM-G991N (Android 15) a 44.1 kHz
stream's framePosition advanced ~0.912x as fast as framesWritten, so
(framesWritten - framePosition) / sampleRate grew by ~88 ms per second of
playback (171 ms -> 2.2 s within 25 s).

Measure the ratio between the two counters on the same stream and convert
framePosition to written frames before subtracting. The ratio is taken
against framesWritten, not the wall clock, so stopping the device (idle
timeout) and starting it again does not disturb it. Ratios within 2% of 1
are taken as 1, so devices whose counters agree get the same value as
before. Returns 0 for the first second of a stream, as documented for
warm-up. The state is guarded by a mutex.

The math lives in a header with no platform dependencies so it can be
tested on the host: test/run_aaudio_timestamp_latency_test.sh replays the
SM-G991N readings and simulated streams.
@alnitak

alnitak commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for the detailed issue and the fix, the analysis and the host tests are great!

I tested your branch on my Galaxy S22 (SM-S908B, Android 16, 44.1 kHz) with the same play/idle sequence you used. There the counters agree: the ratio snaps to 1 and after warm-up the readings match the old formula exactly (20–27 ms, steady), including after the idle stops. So it looks good to me.

If #617 lands first, I'll move the files to test/common/aaudio_timestamp_latency_test.{cpp,sh}.

I'll move the test. Thanks again!

@alnitak
alnitak merged commit c86f525 into alnitak:main Nov 25, 2026
1 check passed
alnitak added a commit that referenced this pull request Nov 25, 2026
Follow-up to #621.

- Bound the error when the first call comes long after the stream
  started: a corrected (ratio != 1) value is only returned once the
  measured span covers the frames played before the measurement began.
- Shrink the unit-ratio tolerance as the measured span grows, so a
  device whose ratio is only slightly off (e.g. 0.99) is corrected too.
- During warm-up return the plain formula when the measurement started
  early in the stream, instead of 0.
- Keep the last value of the stream and return it when
  AAudioStream_getTimestamp() fails after a stop/start on the legacy
  AudioTrack path (AAUDIO_ERROR_INVALID_STATE on a Samsung SM-N975F,
  Android 12), which made it return 0 for the rest of the playback.
- Move the test to test/common/ following test/README.md, add cases for
  the above, and regenerate test/CMakeLists.txt.

Verified on a Galaxy S22 (SM-S908B, Android 16) and a Galaxy Note10+
(SM-N975F, Android 12).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: getHardwareOutputLatency() keeps growing during playback on Android (AAudio framePosition advances slower than framesWritten)

2 participants