AudioFlinger: Fix FastMixer balance initialization

Fixes potential race conditon.
Fixes potential double application of balance in DuplicatingThread.

Flag: EXEMPT bugfix
Test: Balance settings work
Bug: 339571376
Bug: 339753488
Merged-In: Ica7c0deaa0d61fc356ceed0cbdc467a7d88770ac
Change-Id: Ica7c0deaa0d61fc356ceed0cbdc467a7d88770ac
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index ce9dd99..5e3c9ea 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -5092,7 +5092,6 @@
         // mPipeSink below
         // mNormalSink below
 {
-    setMasterBalance(afThreadCallback->getMasterBalance_l());
     ALOGV("MixerThread() id=%d type=%d", id, type);
     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, "
             "mFrameCount=%zu, mNormalFrameCount=%zu",
@@ -5104,6 +5103,8 @@
         // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks
         // (downstream MixerThreads) in DuplicatingThread::threadLoop_write().
         // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink.
+        // Balance is *not* set in the DuplicatingThread here (or from AudioFlinger),
+        // as the downstream MixerThreads implement it.
         return;
     }
     // create an NBAIO sink for the HAL output stream, and negotiate
@@ -5263,6 +5264,9 @@
         mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
         break;
     }
+    // setMasterBalance needs to be called after the FastMixer
+    // (if any) is set up, in order to deliver the balance settings to it.
+    setMasterBalance(afThreadCallback->getMasterBalance_l());
 }
 
 MixerThread::~MixerThread()