Skip to content

Commit dc63f3f

Browse files
author
Eric Laurent
committed
MediaPlayerService: fix AudioSink latency
The AudioSink latency is currently cached when the associated AudioTrack is created. However, the AudioTrack latency can change if the AudioTrack is moved from one output stream to another. The AudioPlayer must also periodically update its view of the latency as it is needed to compensate the real audio time used for A/V sync. This fixes an A/V sync problem seen when switching A2DP on and off while playing a video. Change-Id: I28b24049ca114e1af3e24791dcc900f463536ba4 Conflicts: media/libmediaplayerservice/MediaPlayerService.cpp
1 parent 0df6894 commit dc63f3f

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

media/libmediaplayerservice/MediaPlayerService.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& a
320320
mStreamType, mLeftVolume, mRightVolume);
321321
result.append(buffer);
322322
snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
323-
mMsecsPerFrame, mLatency);
323+
mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
324324
result.append(buffer);
325325
snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
326326
mAuxEffectId, mSendLevel);
@@ -1265,7 +1265,6 @@ MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
12651265
mStreamType = AUDIO_STREAM_MUSIC;
12661266
mLeftVolume = 1.0;
12671267
mRightVolume = 1.0;
1268-
mLatency = 0;
12691268
mMsecsPerFrame = 0;
12701269
mAuxEffectId = 0;
12711270
mSendLevel = 0.0;
@@ -1324,7 +1323,8 @@ ssize_t MediaPlayerService::AudioOutput::frameSize() const
13241323

13251324
uint32_t MediaPlayerService::AudioOutput::latency () const
13261325
{
1327-
return mLatency;
1326+
if (mTrack == 0) return 0;
1327+
return mTrack->latency();
13281328
}
13291329

13301330
float MediaPlayerService::AudioOutput::msecsPerFrame() const
@@ -1403,7 +1403,6 @@ status_t MediaPlayerService::AudioOutput::open(
14031403
t->setVolume(mLeftVolume, mRightVolume);
14041404

14051405
mMsecsPerFrame = 1.e3 / (float) sampleRate;
1406-
mLatency = t->latency();
14071406
mTrack = t;
14081407

14091408
t->setAuxEffectSendLevel(mSendLevel);

media/libmediaplayerservice/MediaPlayerService.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ class MediaPlayerService : public BnMediaPlayerService
112112
float mLeftVolume;
113113
float mRightVolume;
114114
float mMsecsPerFrame;
115-
uint32_t mLatency;
116115
int mSessionId;
117116
float mSendLevel;
118117
int mAuxEffectId;

media/libstagefright/AudioPlayer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ size_t AudioPlayer::fillBuffer(void *data, size_t size) {
408408
break;
409409
}
410410

411+
if (mAudioSink != NULL) {
412+
mLatencyUs = (int64_t)mAudioSink->latency() * 1000;
413+
} else {
414+
mLatencyUs = (int64_t)mAudioTrack->latency() * 1000;
415+
}
416+
411417
CHECK(mInputBuffer->meta_data()->findInt64(
412418
kKeyTime, &mPositionTimeMediaUs));
413419

0 commit comments

Comments
 (0)