audioflinger: fix offload track transition

Make sure that when transitioning from one active
offloaded track to the next we flush DSP content
if both tracks are not on the same audio session.
This happens when switching between two applications
playing the same type of content (e.g MP3, stereo, 44.1).
In this case, we reuse the same output thread because the track
formats are compatible.

Bug: 11247103.
Change-Id: I2b9031591149adeb70766da5e0d21ff2933a37e8
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index b8c1632..de408a0 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -3984,23 +3984,6 @@
         sp<Track> l = mLatestActiveTrack.promote();
         bool last = l.get() == track;
 
-        if (mPreviousTrack != NULL) {
-            if (track != mPreviousTrack) {
-                // Flush any data still being written from last track
-                mBytesRemaining = 0;
-                if (mPausedBytesRemaining) {
-                    // Last track was paused so we also need to flush saved
-                    // mixbuffer state and invalidate track so that it will
-                    // re-submit that unwritten data when it is next resumed
-                    mPausedBytesRemaining = 0;
-                    // Invalidate is a bit drastic - would be more efficient
-                    // to have a flag to tell client that some of the
-                    // previously written data was lost
-                    mPreviousTrack->invalidate();
-                }
-            }
-        }
-        mPreviousTrack = track;
         if (track->isPausing()) {
             track->setPaused();
             if (last) {
@@ -4048,6 +4031,30 @@
             }
 
             if (last) {
+                if (mPreviousTrack != NULL) {
+                    if (track != mPreviousTrack) {
+                        // Flush any data still being written from last track
+                        mBytesRemaining = 0;
+                        if (mPausedBytesRemaining) {
+                            // Last track was paused so we also need to flush saved
+                            // mixbuffer state and invalidate track so that it will
+                            // re-submit that unwritten data when it is next resumed
+                            mPausedBytesRemaining = 0;
+                            // Invalidate is a bit drastic - would be more efficient
+                            // to have a flag to tell client that some of the
+                            // previously written data was lost
+                            mPreviousTrack->invalidate();
+                        }
+                        // flush data already sent to the DSP if changing audio session as audio
+                        // comes from a different source. Also invalidate previous track to force a
+                        // seek when resuming.
+                        if (mPreviousTrack->sessionId() != track->sessionId()) {
+                            mPreviousTrack->invalidate();
+                            mFlushPending = true;
+                        }
+                    }
+                }
+                mPreviousTrack = track;
                 // reset retry count
                 track->mRetryCount = kMaxTrackRetriesOffload;
                 mActiveTrack = t;