audioflinger: refine latency latency calculation.

There is an audio pipe between the normal mixer output and the fast
mixer to cope for scheduling delays and buffer size difference.
This pipe depth was not taken into account in latency calculation.

Adding the pipe contribution to the latency significantly improves A/V sync.

Bug 6520569.

Change-Id: I5584908e8aa8a02170eb38b22b4370eea800a235
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 3c60e5a..68be7a7 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -1804,11 +1804,25 @@
     return track;
 }
 
+uint32_t AudioFlinger::MixerThread::correctLatency(uint32_t latency) const
+{
+    if (mFastMixer != NULL) {
+        MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
+        latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
+    }
+    return latency;
+}
+
+uint32_t AudioFlinger::PlaybackThread::correctLatency(uint32_t latency) const
+{
+    return latency;
+}
+
 uint32_t AudioFlinger::PlaybackThread::latency() const
 {
     Mutex::Autolock _l(mLock);
     if (initCheck() == NO_ERROR) {
-        return mOutput->stream->get_latency(mOutput->stream);
+        return correctLatency(mOutput->stream->get_latency(mOutput->stream));
     } else {
         return 0;
     }
@@ -2020,6 +2034,7 @@
     }
 }
 
+
 status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
 {
     if (halFrames == NULL || dspFrames == NULL) {