AudioFlinger: Do not permit fast patch tracks if not compatible

Test: atest AudioPlaybackCaptureTest
Bug: 132346038
Change-Id: I67939ed7f98c546d41148d36764d93723353c893
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 5e5ea11..612c5e1 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -827,6 +827,12 @@
                     ALOGE("Secondary output patchRecord init failed: %d", status);
                     continue;
                 }
+
+                // TODO: We could check compatibility of the secondaryThread with the PatchTrack
+                // for fast usage: thread has fast mixer, sample rate matches, etc.;
+                // for now, we exclude fast tracks by removing the Fast flag.
+                const audio_output_flags_t outputFlags =
+                        (audio_output_flags_t)(output.flags & ~AUDIO_OUTPUT_FLAG_FAST);
                 sp patchTrack = new PlaybackThread::PatchTrack(secondaryThread,
                                                                streamType,
                                                                output.sampleRate,
@@ -835,7 +841,7 @@
                                                                frameCount,
                                                                patchRecord->buffer(),
                                                                patchRecord->bufferSize(),
-                                                               output.flags,
+                                                               outputFlags,
                                                                0ns /* timeout */);
                 status = patchTrack->initCheck();
                 if (status != NO_ERROR) {
diff --git a/services/audioflinger/PatchPanel.cpp b/services/audioflinger/PatchPanel.cpp
index a210a6d..edb331d 100644
--- a/services/audioflinger/PatchPanel.cpp
+++ b/services/audioflinger/PatchPanel.cpp
@@ -506,7 +506,10 @@
     }
     if (mPlayback.thread()->hasFastMixer()) {
         // Create a fast track if the playback thread has fast mixer to get better performance.
+        // Note: we should have matching channel mask, sample rate, and format by the logic above.
         outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
+    } else {
+        outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
     }
 
     // create a special playback track to render to playback thread.
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 7daa929..1d99b88 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -4618,6 +4618,10 @@
 
         // process fast tracks
         if (track->isFastTrack()) {
+            LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr,
+                    "%s(%d): FastTrack(%d) present without FastMixer",
+                     __func__, id(), track->id());
+
             if (track->getHapticPlaybackEnabled()) {
                 noFastHapticTrack = false;
             }