From 1eb0e4a26059f2fe9bd353f04d1b646edbf38473 Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Fri, 9 Mar 2012 10:34:37 -0800 Subject: [PATCH] Improve AudioPlayer position reporting The latency was not taken into account when updating mPositionTimeRealUs inside of the fillBuffer hook, contrary to what the getRealTimeUsLocked() method does. This caused the realTimeOffset calculated in the getMediaTimeUs to always be negative, causing the reported position to always be equal to mPositionTimeMediaUs, which is updated infrequently. With this change, the reported position is updated more frequently, allowing apps to perform smoother UI updates. Change-Id: I61e05c1a8b53d46b9091afb0d18a6289d13a7a5e --- media/libstagefright/AudioPlayer.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/media/libstagefright/AudioPlayer.cpp b/media/libstagefright/AudioPlayer.cpp index 650b6c43ba4ae..2b3cb1ac37582 100644 --- a/media/libstagefright/AudioPlayer.cpp +++ b/media/libstagefright/AudioPlayer.cpp @@ -437,8 +437,11 @@ size_t AudioPlayer::fillBuffer(void *data, size_t size) { kKeyTime, &mPositionTimeMediaUs)); mPositionTimeRealUs = - ((mNumFramesPlayed + size_done / mFrameSize) * 1000000) + -mLatencyUs + ((mNumFramesPlayed + size_done / mFrameSize) * 1000000) / mSampleRate; + if (mPositionTimeRealUs < 0) { + mPositionTimeRealUs = 0; + } ALOGV("buffer->size() = %d, " "mPositionTimeMediaUs=%.2f mPositionTimeRealUs=%.2f", @@ -493,7 +496,9 @@ int64_t AudioPlayer::getRealTimeUs() { int64_t AudioPlayer::getRealTimeUsLocked() const { CHECK(mStarted); CHECK_NE(mSampleRate, 0); - return -mLatencyUs + (mNumFramesPlayed * 1000000) / mSampleRate; + int64_t t = -mLatencyUs + (mNumFramesPlayed * 1000000) / mSampleRate; + if (t < 0) return 0; + return t; } int64_t AudioPlayer::getMediaTimeUs() {