From 5347dca057618fd5aec3a9f11e357d69632b60e6 Mon Sep 17 00:00:00 2001 From: Phil Burk Date: Wed, 8 Apr 2020 16:31:07 -0700 Subject: [PATCH] aaudio: use minimum of one burst in buffer The old minimum was zero, which minimized latency but was probably too aggressive for some devices. It is better to avoid glitches. Because of other changes in AAudio MMAP, this will have the same latency as earlier version, but better immunity from glitches. Bug: 153589709 Test: launch OboeTester Test: tap TEST OUTPUT Test: tap OPEN and START Test: notice that actual buffer size will be 1 * burstSize Change-Id: Ie53352a0589c710be11f74e995bd12be4f7c45a8 --- media/libaaudio/src/client/AudioStreamInternal.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/media/libaaudio/src/client/AudioStreamInternal.cpp b/media/libaaudio/src/client/AudioStreamInternal.cpp index fcccf03112..6723ec9225 100644 --- a/media/libaaudio/src/client/AudioStreamInternal.cpp +++ b/media/libaaudio/src/client/AudioStreamInternal.cpp @@ -714,12 +714,8 @@ void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { int32_t adjustedFrames = requestedFrames; const int32_t maximumSize = getBufferCapacity() - mFramesPerBurst; - // The buffer size can be set to zero. - // This means that the callback may be called when the internal buffer becomes empty. - // This will be fine on some devices in ideal circumstances and will result in the - // lowest possible latency. - // If there are glitches then they should be detected as XRuns and the size can be increased. - static const int32_t minimumSize = 0; + // Minimum size should be a multiple number of bursts. + const int32_t minimumSize = 1 * mFramesPerBurst; // Clip to minimum size so that rounding up will work better. adjustedFrames = std::max(minimumSize, adjustedFrames); @@ -731,12 +727,14 @@ aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { // Round to the next highest burst size. int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst; adjustedFrames = numBursts * mFramesPerBurst; + // Clip just in case maximumSize is not a multiple of mFramesPerBurst. + adjustedFrames = std::min(maximumSize, adjustedFrames); } // Clip against the actual size from the endpoint. int32_t actualFrames = 0; mAudioEndpoint.setBufferSizeInFrames(maximumSize, &actualFrames); - // actualFrames should be <= maximumSize + // actualFrames should be <= actual maximum size of endpoint adjustedFrames = std::min(actualFrames, adjustedFrames); mBufferSizeInFrames = adjustedFrames;