diff options
author | Paul Adenot <[email protected]> | 2024-05-22 15:08:36 +0200 |
---|---|---|
committer | Paul Adenot <[email protected]> | 2024-05-22 15:13:58 +0200 |
commit | d52193ada50329b7805f2e946d9c157f88b7eedc (patch) | |
tree | aee64899fbdf7155f3212783d8edd2a4d2078d8f | |
parent | 6c1a6e151c1f981a2800d40af7c041cfcccc710e (diff) | |
download | cubeb-fix-negative-latency-aaudio.tar.gz cubeb-fix-negative-latency-aaudio.zip |
Clamp to positive value when computing latency for an output streamfix-negative-latency-aaudio
Diagnosed by [email protected], the same code is also found in Chromium.
-rw-r--r-- | src/cubeb_aaudio.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/cubeb_aaudio.cpp b/src/cubeb_aaudio.cpp index 8b5eb23..915faee 100644 --- a/src/cubeb_aaudio.cpp +++ b/src/cubeb_aaudio.cpp @@ -644,9 +644,11 @@ aaudio_get_latency(cubeb_stream * stm, aaudio_direction_t direction, // Extrapolate from the known timestamp for a particular frame presented. int64_t app_frame_hw_time = hw_tstamp + frame_time_delta; // For an output stream, the latency is positive, for an input stream, it's - // negative. - int64_t latency_ns = is_output ? app_frame_hw_time - signed_tstamp_ns - : signed_tstamp_ns - app_frame_hw_time; + // negative. It can happen in some instances, e.g. around start of the stream + // that the latency for output is negative, return 0 in this case. + int64_t latency_ns = is_output + ? std::max(0ll, app_frame_hw_time - signed_tstamp_ns) + : signed_tstamp_ns - app_frame_hw_time; int64_t latency_frames = stm->sample_rate * latency_ns / NS_PER_S; LOGV("Latency in frames (%s): %d (%dms)", is_output ? "output" : "input", |