From 0609231f4fa415cc8198d706ee1c6b5f5e911023 Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Mon, 23 Jan 2012 18:56:59 -0800 Subject: [PATCH] AudioFlinger: refine mixer sleep time logic When an AudioTrack is in underrun state, the AudioFlinger mixer will sleep for a short period of time to give the app a chance to fill the AudioTrack buffer. If the AudioTrack is still not ready during next mixing round, the mixer will proceed with other tracks. If an application keeps a steady underrun condition, the AudioFlinger mixer will alternate between ready and not ready states. In the longer term this will cause the audio HAL to underrun. There is a mechanism to reduce the sleep period if the mixer is not ready several times in a row but this mechanism is defeated by the alternating ready/not ready conditions. The fix consists in only increasing sleep time if the mixer is ready for at least two consecutive times. Issue 5904527. Change-Id: Id0139bca9be8c4e425ec6d428515c4d8f718e8c9 --- services/audioflinger/AudioFlinger.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp index 133dabdfc5d84..ce701ca297c9e 100644 --- a/services/audioflinger/AudioFlinger.cpp +++ b/services/audioflinger/AudioFlinger.cpp @@ -1973,11 +1973,14 @@ bool AudioFlinger::MixerThread::threadLoop() if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { // mix buffers... mAudioMixer->process(); - sleepTime = 0; - // increase sleep time progressively when application underrun condition clears - if (sleepTimeShift > 0) { + // increase sleep time progressively when application underrun condition clears. + // Only increase sleep time if the mixer is ready for two consecutive times to avoid + // that a steady state of alternating ready/not ready conditions keeps the sleep time + // such that we would underrun the audio HAL. + if ((sleepTime == 0) && (sleepTimeShift > 0)) { sleepTimeShift--; } + sleepTime = 0; standbyTime = systemTime() + kStandbyTimeInNsecs; //TODO: delay standby when effects have a tail } else {