summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/AudioPlayer.cpp
diff options
context:
space:
mode:
authorAndreas Huber <andih@google.com>2012-05-14 15:55:48 -0700
committerAndreas Huber <andih@google.com>2012-05-14 15:55:48 -0700
commite4451a91a61a341014f5eff61db356156c3ecb37 (patch)
treee08424d79d2b0c341b51a180065faac801465463 /media/libstagefright/AudioPlayer.cpp
parent44f84ded62bffd5f04e0f5961e8304427a7b9f34 (diff)
downloadframeworks_av-e4451a91a61a341014f5eff61db356156c3ecb37.zip
frameworks_av-e4451a91a61a341014f5eff61db356156c3ecb37.tar.gz
frameworks_av-e4451a91a61a341014f5eff61db356156c3ecb37.tar.bz2
Smoothen audio "real" time by compensating with system_time()
for the delay since the last buffer submission to the audio sink. Change-Id: Ib1a8672f96ba8bbef07d23372f1076fbb1f0bdcc related-to-bug: 6479613
Diffstat (limited to 'media/libstagefright/AudioPlayer.cpp')
-rw-r--r--media/libstagefright/AudioPlayer.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/media/libstagefright/AudioPlayer.cpp b/media/libstagefright/AudioPlayer.cpp
index ca49782..f729a78 100644
--- a/media/libstagefright/AudioPlayer.cpp
+++ b/media/libstagefright/AudioPlayer.cpp
@@ -21,6 +21,7 @@
#include <binder/IPCThreadState.h>
#include <media/AudioTrack.h>
#include <media/stagefright/foundation/ADebug.h>
+#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/AudioPlayer.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaErrors.h>
@@ -41,6 +42,7 @@ AudioPlayer::AudioPlayer(
mLatencyUs(0),
mFrameSize(0),
mNumFramesPlayed(0),
+ mNumFramesPlayedSysTimeUs(ALooper::GetNowUs()),
mPositionTimeMediaUs(-1),
mPositionTimeRealUs(-1),
mSeeking(false),
@@ -200,6 +202,7 @@ void AudioPlayer::pause(bool playPendingSamples) {
}
mNumFramesPlayed = 0;
+ mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
} else {
if (mAudioSink.get() != NULL) {
mAudioSink->pause();
@@ -260,6 +263,7 @@ void AudioPlayer::reset() {
IPCThreadState::self()->flushCommands();
mNumFramesPlayed = 0;
+ mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
mPositionTimeMediaUs = -1;
mPositionTimeRealUs = -1;
mSeeking = false;
@@ -485,6 +489,7 @@ size_t AudioPlayer::fillBuffer(void *data, size_t size) {
{
Mutex::Autolock autoLock(mLock);
mNumFramesPlayed += size_done / mFrameSize;
+ mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
}
if (postEOS) {
@@ -506,7 +511,14 @@ int64_t AudioPlayer::getRealTimeUs() {
int64_t AudioPlayer::getRealTimeUsLocked() const {
CHECK(mStarted);
CHECK_NE(mSampleRate, 0);
- return -mLatencyUs + (mNumFramesPlayed * 1000000) / mSampleRate;
+ int64_t result = -mLatencyUs + (mNumFramesPlayed * 1000000) / mSampleRate;
+
+ // Compensate for large audio buffers, updates of mNumFramesPlayed
+ // are less frequent, therefore to get a "smoother" notion of time we
+ // compensate using system time.
+ int64_t diffUs = ALooper::GetNowUs() - mNumFramesPlayedSysTimeUs;
+
+ return result + diffUs;
}
int64_t AudioPlayer::getMediaTimeUs() {
@@ -548,6 +560,7 @@ status_t AudioPlayer::seekTo(int64_t time_us) {
// Flush resets the number of played frames
mNumFramesPlayed = 0;
+ mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
if (mAudioSink != NULL) {
mAudioSink->flush();