Fix fast track leak if out of normal track names

Bug: 6580402
Change-Id: I3ac7f012062c35833147f47ba822eb4bf532a824
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index eea3cd2..7d6b121 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -2841,6 +2841,8 @@
             // at the identical fast mixer slot within the same normal mix cycle,
             // is impossible because the slot isn't marked available until the end of each cycle.
             int j = track->mFastIndex;
+            ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks);
+            ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
             FastTrack *fastTrack = &state->mFastTracks[j];
 
             // Determine whether the track is currently in underrun condition,
@@ -4295,6 +4297,13 @@
         // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
         // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
         mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t);
+        // to avoid leaking a track name, do not allocate one unless there is an mCblk
+        mName = thread->getTrackName_l((audio_channel_mask_t)channelMask);
+        if (mName < 0) {
+            ALOGE("no more track names available");
+            return;
+        }
+        // only allocate a fast track index if we were able to allocate a normal track name
         if (flags & IAudioFlinger::TRACK_FAST) {
             mCblk->flags |= CBLK_FAST;  // atomic op not needed yet
             ALOG_ASSERT(thread->mFastTrackAvailMask != 0);
@@ -4309,14 +4318,6 @@
             mObservedUnderruns = thread->getFastTrackUnderruns(i);
             thread->mFastTrackAvailMask &= ~(1 << i);
         }
-        // to avoid leaking a track name, do not allocate one unless there is an mCblk
-        mName = thread->getTrackName_l((audio_channel_mask_t)channelMask);
-        if (mName < 0) {
-            ALOGE("no more track names available");
-            // FIXME bug - if sufficient fast track indices, but insufficient normal mixer names,
-            // then we leak a fast track index.  Should swap these two sections, or better yet
-            // only allocate a normal mixer name for normal tracks.
-        }
     }
     ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
 }
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index 19390b1..160e4cd 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -778,6 +778,7 @@
             int                 mName;      // track name on the normal mixer,
                                             // allocated statically at track creation time,
                                             // and is even allocated (though unused) for fast tracks
+                                            // FIXME don't allocate track name for fast tracks
             int16_t             *mMainBuffer;
             int32_t             *mAuxBuffer;
             int                 mAuxEffectId;
@@ -789,7 +790,7 @@
 
             // The following fields are only for fast tracks, and should be in a subclass
             int                 mFastIndex; // index within FastMixerState::mFastTracks[];
-                                            // either mFastIndex == -1
+                                            // either mFastIndex == -1 if not isFastTrack()
                                             // or 0 < mFastIndex < FastMixerState::kMaxFast because
                                             // index 0 is reserved for normal mixer's submix;
                                             // index is allocated statically at track creation time