audioflinger: longer offload thread standby delay

- Increase offloaded output thread standby delay to 1 second
to allow transition between tracks with going to stanby
if reusing the same audio track (gapless)

- Make sure pause/flush/resume sequence is sent to the HAL
in the right order

- Fix format display in track dump

Bug: 8174034.
Change-Id: I43ef6f8fdbf7427e4eff6cc2d0665d7d1463ea8a
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 1b5a9a9..885f72e 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -109,6 +109,9 @@
 // maximum normal mix buffer size
 static const uint32_t kMaxNormalMixBufferSizeMs = 24;
 
+// Offloaded output thread standby delay: allows track transition without going to standby
+static const nsecs_t kOffloadStandbyDelayNs = seconds(1);
+
 // Whether to use fast mixer
 static const enum {
     FastMixer_Never,    // never initialize or use: for debugging only
@@ -2137,13 +2140,11 @@
                 mWaitWorkCV.wait(mLock);
                 ALOGV("async completion/wake");
                 acquireWakeLock_l();
+                standbyTime = systemTime() + standbyDelay;
+                sleepTime = 0;
                 if (exitPending()) {
                     break;
                 }
-                if (!mActiveTracks.size() && (systemTime() > standbyTime)) {
-                    continue;
-                }
-                sleepTime = 0;
             } else if ((!mActiveTracks.size() && systemTime() > standbyTime) ||
                                    isSuspended()) {
                 // put audio hardware into standby after short delay
@@ -3701,7 +3702,11 @@
 
     // use shorter standby delay as on normal output to release
     // hardware resources as soon as possible
-    standbyDelay = microseconds(activeSleepTime*2);
+    if (audio_is_linear_pcm(mFormat)) {
+        standbyDelay = microseconds(activeSleepTime*2);
+    } else {
+        standbyDelay = kOffloadStandbyDelayNs;
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -3837,6 +3842,9 @@
     size_t count = mActiveTracks.size();
 
     mixer_state mixerStatus = MIXER_IDLE;
+    bool doHwPause = false;
+    bool doHwResume = false;
+
     // find out which tracks need to be processed
     for (size_t i = 0; i < count; i++) {
         sp<Track> t = mActiveTracks[i].promote();
@@ -3868,7 +3876,7 @@
             track->setPaused();
             if (last) {
                 if (!mHwPaused) {
-                    mOutput->stream->pause(mOutput->stream);
+                    doHwPause = true;
                     mHwPaused = true;
                 }
                 // If we were part way through writing the mixbuffer to
@@ -3901,7 +3909,7 @@
 
             if (last) {
                 if (mHwPaused) {
-                    mOutput->stream->resume(mOutput->stream);
+                    doHwResume = true;
                     mHwPaused = false;
                     // threadLoop_mix() will handle the case that we need to
                     // resume an interrupted write
@@ -3963,10 +3971,17 @@
         processVolume_l(track, last);
     }
 
+    // make sure the pause/flush/resume sequence is executed in the right order
+    if (doHwPause) {
+        mOutput->stream->pause(mOutput->stream);
+    }
     if (mFlushPending) {
         flushHw_l();
         mFlushPending = false;
     }
+    if (doHwResume) {
+        mOutput->stream->resume(mOutput->stream);
+    }
 
     // remove all the tracks that need to be...
     removeTracks_l(*tracksToRemove);