audioflinger: fix noise when skipping to next song

When audio effects are enabled, a noise can be heard at the
beginning of the new song when skipping to next song in music app.

This is because some effects (especially virtualizer) have a tail.
This tail was not played when previous song was stopped because effects were
not processed when no tracks were present on a given session. This is to
reduce CPU load when effects are enabled but no audio is playing.
The tail was then rendered when the new song was started.

Added a delay before stopping effect process after all tracks have been removed from a session.

Issue 5584880.

Change-Id: I815e0f7441f9302e8dfe413dc269a94e4cc6fd95
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index 4b794ef..897bc78 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -1247,6 +1247,10 @@
         // corresponding to a suspend all request.
         static const int        kKeyForSuspendAll = 0;
 
+        // minimum duration during which we force calling effect process when last track on
+        // a session is stopped or removed to allow effect tail to be rendered
+        static const int        kProcessTailDurationMs = 1000;
+
         void process_l();
 
         void lock() {
@@ -1287,7 +1291,8 @@
         void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
         int32_t trackCnt() { return mTrackCnt;}
 
-        void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
+        void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
+                                   mTailBufferCount = mMaxTailBuffers; }
         void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
         int32_t activeTrackCnt() { return mActiveTrackCnt;}
 
@@ -1338,6 +1343,8 @@
         int16_t *mOutBuffer;        // chain output buffer
         volatile int32_t mActiveTrackCnt;  // number of active tracks connected
         volatile int32_t mTrackCnt;        // number of tracks connected
+        int32_t mTailBufferCount;   // current effect tail buffer count
+        int32_t mMaxTailBuffers;    // maximum effect tail buffers
         bool mOwnInBuffer;          // true if the chain owns its input buffer
         int mVolumeCtrlIdx;         // index of insert effect having control over volume
         uint32_t mLeftVolume;       // previous volume on left channel