Repository navigation
fix android: getHardwareOutputLatency drifts when the AAudio position rate differs - #621
Merged
Merged
Conversation
… 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.
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.
I'll move the test. Thanks again! |
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).
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.
Description
Fixes #620.
On some devices the
framePositionfromAAudioStream_getTimestamp()does not advance at the stream rate. On a Samsung SM-G991N (Android 15) a 44.1 kHz stream'sframePositionadvances ~0.912× as fast asframesWritten, sogetHardwareOutputLatency()grows by ~88 ms per second of playback.This PR measures the ratio between the two counters on the same stream and converts
framePositioninto written frames before subtracting: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.)pStreamPlayback, or a counter that goes backwards (a reroute that reopens the stream), restarts the measurement.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.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.shreplays 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 flattest/layout. If #617 lands first, I'll move the files totest/common/aaudio_timestamp_latency_test.{cpp,sh}.Limitations:
I've seen the slow
framePositionon 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:
main(a7e9f8d)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