Merge "[media][sfplugin] fix -Wdangling-gsl"
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 2d769cf..a021866 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -2953,9 +2953,11 @@
             ALOG_ASSERT(mCallbackThread != 0);
             mCallbackThread->setWriteBlocked(mWriteAckSequence);
         }
+        ATRACE_BEGIN("write");
         // FIXME We should have an implementation of timestamps for direct output threads.
         // They are used e.g for multichannel PCM playback over HDMI.
         bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
+        ATRACE_END();
 
         if (mUseAsyncWrite &&
                 ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
@@ -5646,10 +5648,17 @@
             minFrames = 1;
         }
 
-        if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() &&
+        const size_t framesReady = track->framesReady();
+        const int trackId = track->id();
+        if (ATRACE_ENABLED()) {
+            std::string traceName("nRdy");
+            traceName += std::to_string(trackId);
+            ATRACE_INT(traceName.c_str(), framesReady);
+        }
+        if ((framesReady >= minFrames) && track->isReady() && !track->isPaused() &&
                 !track->isStopping_2() && !track->isStopped())
         {
-            ALOGVV("track(%d) s=%08x [OK]", track->id(), cblk->mServer);
+            ALOGVV("track(%d) s=%08x [OK]", trackId, cblk->mServer);
 
             if (track->mFillingUpStatus == Track::FS_FILLED) {
                 track->mFillingUpStatus = Track::FS_ACTIVE;
@@ -5726,7 +5735,7 @@
                 // fill a buffer, then remove it from active list.
                 // Only consider last track started for mixer state control
                 if (--(track->mRetryCount) <= 0) {
-                    ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", track->id());
+                    ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", trackId);
                     tracksToRemove->add(track);
                     // indicate to client process that the track was disabled because of underrun;
                     // it will then automatically call start() when data is available
@@ -5734,7 +5743,7 @@
                 } else if (last) {
                     ALOGW("pause because of UNDERRUN, framesReady = %zu,"
                             "minFrames = %u, mFormat = %#x",
-                            track->framesReady(), minFrames, mFormat);
+                            framesReady, minFrames, mFormat);
                     mixerStatus = MIXER_TRACKS_ENABLED;
                     if (mHwSupportsPause && !mHwPaused && !mStandby) {
                         doHwPause = true;
diff --git a/services/audioflinger/Tracks.cpp b/services/audioflinger/Tracks.cpp
index 932c32b..7c53ca0 100644
--- a/services/audioflinger/Tracks.cpp
+++ b/services/audioflinger/Tracks.cpp
@@ -2414,7 +2414,6 @@
     if (!stream) return NO_INIT;  // If there is no stream, RecordThread is not reading.
 
     status_t result = NO_ERROR;
-    struct timespec newTimeOut = *timeOut;
     size_t bytesRead = 0;
     {
         ATRACE_NAME("read");
@@ -2437,15 +2436,16 @@
     ALOGW_IF(buffer->mFrameCount < bytesRead / mFrameSize,
             "Lost %zu frames obtained from HAL", bytesRead / mFrameSize - buffer->mFrameCount);
     mUnconsumedFrames = buffer->mFrameCount;
-    // Correct newTimeOut by elapsed time.
+    struct timespec newTimeOut;
     if (startTimeNs) {
-        nsecs_t newTimeOutNs =
-                audio_utils_ns_from_timespec(&newTimeOut) - (systemTime() - startTimeNs);
+        // Correct the timeout by elapsed time.
+        nsecs_t newTimeOutNs = audio_utils_ns_from_timespec(timeOut) - (systemTime() - startTimeNs);
         if (newTimeOutNs < 0) newTimeOutNs = 0;
         newTimeOut.tv_sec = newTimeOutNs / NANOS_PER_SECOND;
         newTimeOut.tv_nsec = newTimeOutNs - newTimeOut.tv_sec * NANOS_PER_SECOND;
+        timeOut = &newTimeOut;
     }
-    return PatchRecord::obtainBuffer(buffer, &newTimeOut);
+    return PatchRecord::obtainBuffer(buffer, timeOut);
 
 stream_error:
     stream->standby();