Revert "AudioFlinger: Remove naked track pointers"

This reverts commit 062bfceaefab29f0f71db7d5a248b3f5f0572b6a.
Bug: 32728805

(cherry picked from commit 5850c4c8eecbe0db3cee8511a4f82cb443e27d08)
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 8818d8b..0e9ecc2 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -4625,7 +4625,7 @@
 {
 }
 
-void AudioFlinger::DirectOutputThread::processVolume_l(sp<Track> track, bool lastTrack)
+void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
 {
     float left, right;
 
@@ -4700,13 +4700,14 @@
     bool doHwResume = false;
 
     // find out which tracks need to be processed
-    for (const sp<Track> &track : mActiveTracks) {
-        if (track->isInvalid()) {
+    for (const sp<Track> &t : mActiveTracks) {
+        if (t->isInvalid()) {
             ALOGW("An invalidated track shouldn't be in active list");
-            tracksToRemove->add(track);
+            tracksToRemove->add(t);
             continue;
         }
 
+        Track* const track = t.get();
 #ifdef VERY_VERY_VERBOSE_LOGGING
         audio_track_cblk_t* cblk = track->cblk();
 #endif
@@ -4714,7 +4715,8 @@
         // In theory an older track could underrun and restart after the new one starts
         // but as we only care about the transition phase between two tracks on a
         // direct output, it is not a problem to ignore the underrun case.
-        bool last = mActiveTracks.getLatest() == track;
+        sp<Track> l = mActiveTracks.getLatest();
+        bool last = l.get() == track;
 
         if (track->isPausing()) {
             track->setPaused();
@@ -4786,7 +4788,7 @@
 
                 // reset retry count
                 track->mRetryCount = kMaxTrackRetriesDirect;
-                mActiveTrack = track; // save track as mActiveTracks may change without lock.
+                mActiveTrack = t;
                 mixerStatus = MIXER_TRACKS_READY;
                 if (mHwPaused) {
                     doHwResume = true;
@@ -5247,7 +5249,8 @@
     ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count);
 
     // find out which tracks need to be processed
-    for (const sp<Track> &track : mActiveTracks) {
+    for (const sp<Track> &t : mActiveTracks) {
+        Track* const track = t.get();
 #ifdef VERY_VERY_VERBOSE_LOGGING
         audio_track_cblk_t* cblk = track->cblk();
 #endif
@@ -5255,7 +5258,8 @@
         // In theory an older track could underrun and restart after the new one starts
         // but as we only care about the transition phase between two tracks on a
         // direct output, it is not a problem to ignore the underrun case.
-        bool last = mActiveTracks.getLatest() == track;
+        sp<Track> l = mActiveTracks.getLatest();
+        bool last = l.get() == track;
 
         if (track->isInvalid()) {
             ALOGW("An invalidated track shouldn't be in active list");
@@ -5361,7 +5365,7 @@
                 } else {
                     track->mRetryCount = kMaxTrackRetriesOffload;
                 }
-                mActiveTrack = track; // save track as mActiveTracks may change without lock.
+                mActiveTrack = t;
                 mixerStatus = MIXER_TRACKS_READY;
             }
         } else {
diff --git a/services/audioflinger/Threads.h b/services/audioflinger/Threads.h
index 862dc51..1fd2adc 100644
--- a/services/audioflinger/Threads.h
+++ b/services/audioflinger/Threads.h
@@ -1077,7 +1077,7 @@
     DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
                         audio_io_handle_t id, uint32_t device, ThreadBase::type_t type,
                         bool systemReady);
-    void processVolume_l(sp<Track> track, bool lastTrack);
+    void processVolume_l(Track *track, bool lastTrack);
 
     // prepareTracks_l() tells threadLoop_mix() the name of the single active track
     sp<Track>               mActiveTrack;