AudioFlinger: Remove naked track pointers

Test: Play Music offloaded and kill audioserver
Change-Id: I3114a67f248e067e0b5334d76e4b5e6c39858040
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 737d21e..e3ebc81 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -4764,7 +4764,7 @@
 {
 }
 
-void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
+void AudioFlinger::DirectOutputThread::processVolume_l(sp<Track> track, bool lastTrack)
 {
     float left, right;
 
@@ -4839,14 +4839,13 @@
     bool doHwResume = false;
 
     // find out which tracks need to be processed
-    for (const sp<Track> &t : mActiveTracks) {
-        if (t->isInvalid()) {
+    for (const sp<Track> &track : mActiveTracks) {
+        if (track->isInvalid()) {
             ALOGW("An invalidated track shouldn't be in active list");
-            tracksToRemove->add(t);
+            tracksToRemove->add(track);
             continue;
         }
 
-        Track* const track = t.get();
 #ifdef VERY_VERY_VERBOSE_LOGGING
         audio_track_cblk_t* cblk = track->cblk();
 #endif
@@ -4854,8 +4853,7 @@
         // 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.
-        sp<Track> l = mActiveTracks.getLatest();
-        bool last = l.get() == track;
+        bool last = mActiveTracks.getLatest() == track;
 
         if (track->isPausing()) {
             track->setPaused();
@@ -4927,7 +4925,7 @@
 
                 // reset retry count
                 track->mRetryCount = kMaxTrackRetriesDirect;
-                mActiveTrack = t;
+                mActiveTrack = track; // save track as mActiveTracks may change without lock.
                 mixerStatus = MIXER_TRACKS_READY;
                 if (mHwPaused) {
                     doHwResume = true;
@@ -5388,8 +5386,7 @@
     ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count);
 
     // find out which tracks need to be processed
-    for (const sp<Track> &t : mActiveTracks) {
-        Track* const track = t.get();
+    for (const sp<Track> &track : mActiveTracks) {
 #ifdef VERY_VERY_VERBOSE_LOGGING
         audio_track_cblk_t* cblk = track->cblk();
 #endif
@@ -5397,8 +5394,7 @@
         // 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.
-        sp<Track> l = mActiveTracks.getLatest();
-        bool last = l.get() == track;
+        bool last = mActiveTracks.getLatest() == track;
 
         if (track->isInvalid()) {
             ALOGW("An invalidated track shouldn't be in active list");
@@ -5504,7 +5500,7 @@
                 } else {
                     track->mRetryCount = kMaxTrackRetriesOffload;
                 }
-                mActiveTrack = t;
+                mActiveTrack = track; // save track as mActiveTracks may change without lock.
                 mixerStatus = MIXER_TRACKS_READY;
             }
         } else {