AudioFlinger: Fix direct output resource from being occupied for too long

When flush immediately follows pause, mState might be overwritten and not
processed. In this case, direct output will retry due to data underrun.
In the duration of retry, the ActiveCount in AudioPolicy will be
positive and user can't create new track.
Add isPausePending to make sure pause will be handled by the
direct output thread.

Test: Manual (see bug)
Test: Clarity MediaPlayer Offloaded with seek.
Bug: 175092336
Change-Id: Ief742d2ce44917da24885373a5770e47876bc3a5
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 0c66301..deb13af 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -5825,8 +5825,15 @@
         sp<Track> l = mActiveTracks.getLatest();
         bool last = l.get() == track;
 
-        if (track->isPausing()) {
-            track->setPaused();
+        if (track->isPausePending()) {
+            track->pauseAck();
+            // It is possible a track might have been flushed or stopped.
+            // Other operations such as flush pending might occur on the next prepare.
+            if (track->isPausing()) {
+                track->setPaused();
+            }
+            // Always perform pause, as an immediate flush will change
+            // the pause state to be no longer isPausing().
             if (mHwSupportsPause && last && !mHwPaused) {
                 doHwPause = true;
                 mHwPaused = true;
@@ -6368,8 +6375,15 @@
             continue;
         }
 
-        if (track->isPausing()) {
-            track->setPaused();
+        if (track->isPausePending()) {
+            track->pauseAck();
+            // It is possible a track might have been flushed or stopped.
+            // Other operations such as flush pending might occur on the next prepare.
+            if (track->isPausing()) {
+                track->setPaused();
+            }
+            // Always perform pause if last, as an immediate flush will change
+            // the pause state to be no longer isPausing().
             if (last) {
                 if (mHwSupportsPause && !mHwPaused) {
                     doHwPause = true;