AudioTrack: Fix timestamp jitter

When HAL out_get_presentation_position returns error,
use server position adjusted for latency.

Bug: 28250436
Change-Id: I3dbcb9b7c4e56d34e7e161d1a02d8f64afd602b9
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index b9138f2..9821831 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -504,6 +504,8 @@
     mPreviousTimestampValid = false;
     mTimestampStartupGlitchReported = false;
     mRetrogradeMotionReported = false;
+    mPreviousLocation = ExtendedTimestamp::LOCATION_INVALID;
+    mComputedLatencyMs = 0.;
     mUnderrunCountOffset = 0;
     mFramesWritten = 0;
     mFramesWrittenServerOffset = 0;
@@ -536,6 +538,8 @@
         mPreviousTimestampValid = false;
         mTimestampStartupGlitchReported = false;
         mRetrogradeMotionReported = false;
+        mPreviousLocation = ExtendedTimestamp::LOCATION_INVALID;
+        mComputedLatencyMs = 0.;
 
         // read last server side position change via timestamp.
         ExtendedTimestamp ets;
@@ -2303,7 +2307,40 @@
         ExtendedTimestamp ets;
         status = mProxy->getTimestamp(&ets);
         if (status == OK) {
-            status = ets.getBestTimestamp(&timestamp);
+            ExtendedTimestamp::Location location;
+            status = ets.getBestTimestamp(&timestamp, &location);
+
+            if (status == OK) {
+                // It is possible that the best location has moved from the kernel to the server.
+                // In this case we adjust the position from the previous computed latency.
+                if (location == ExtendedTimestamp::LOCATION_SERVER) {
+                    ALOGW_IF(mPreviousLocation == ExtendedTimestamp::LOCATION_KERNEL,
+                            "getTimestamp() location moved from kernel to server");
+                    const double latencyMs = mComputedLatencyMs > 0.
+                            ? mComputedLatencyMs : mAfLatency;
+                    const int64_t frames =
+                            int64_t(latencyMs * mSampleRate * mPlaybackRate.mSpeed / 1000);
+                    ALOGV("mComputedLatencyMs:%lf  mAfLatency:%u  frame adjustment:%lld",
+                            mComputedLatencyMs, mAfLatency, (long long)frames);
+                    if (frames >= ets.mPosition[location]) {
+                        timestamp.mPosition = 0;
+                    } else {
+                        timestamp.mPosition = (uint32_t)(ets.mPosition[location] - frames);
+                    }
+                } else if (location == ExtendedTimestamp::LOCATION_KERNEL) {
+                    const double bufferDiffMs =
+                            (double)(ets.mPosition[ExtendedTimestamp::LOCATION_SERVER]
+                                   - ets.mPosition[ExtendedTimestamp::LOCATION_KERNEL])
+                                   * 1000 / ((double)mSampleRate * mPlaybackRate.mSpeed);
+                    mComputedLatencyMs = bufferDiffMs > 0. ? bufferDiffMs : 0.;
+                    ALOGV("mComputedLatencyMs:%lf  mAfLatency:%d",
+                            mComputedLatencyMs, mAfLatency);
+                }
+                mPreviousLocation = location;
+            } else {
+                // right after AudioTrack is started, one may not find a timestamp
+                ALOGV("getBestTimestamp did not find timestamp");
+            }
         }
         if (status == INVALID_OPERATION) {
             status = WOULD_BLOCK;