Ensure getPlaybackHeadPosition isn't called if not needed
Once the value returned from AudioTimestampPoller advances, we only need getPlaybackHeadPosition to sample sync params and verify the returned timestamp. Both of these happen less often and we can avoid calling getPlaybackHeadPosition if we don't actually need it. PiperOrigin-RevId: 512882170 (cherry picked from commit 408b4449ff75e29a9bda7adc1b530b993fc47814)
This commit is contained in:
parent
f690ebdf46
commit
b44fb574b5
@ -451,13 +451,13 @@ import java.lang.reflect.Method;
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void maybeSampleSyncParams() {
|
private void maybeSampleSyncParams() {
|
||||||
long playbackPositionUs = getPlaybackHeadPositionUs();
|
|
||||||
if (playbackPositionUs == 0) {
|
|
||||||
// The AudioTrack hasn't output anything yet.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
long systemTimeUs = System.nanoTime() / 1000;
|
long systemTimeUs = System.nanoTime() / 1000;
|
||||||
if (systemTimeUs - lastPlayheadSampleTimeUs >= MIN_PLAYHEAD_OFFSET_SAMPLE_INTERVAL_US) {
|
if (systemTimeUs - lastPlayheadSampleTimeUs >= MIN_PLAYHEAD_OFFSET_SAMPLE_INTERVAL_US) {
|
||||||
|
long playbackPositionUs = getPlaybackHeadPositionUs();
|
||||||
|
if (playbackPositionUs == 0) {
|
||||||
|
// The AudioTrack hasn't output anything yet.
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Take a new sample and update the smoothed offset between the system clock and the playhead.
|
// Take a new sample and update the smoothed offset between the system clock and the playhead.
|
||||||
playheadOffsets[nextPlayheadOffsetIndex] =
|
playheadOffsets[nextPlayheadOffsetIndex] =
|
||||||
Util.getPlayoutDurationForMediaDuration(playbackPositionUs, audioTrackPlaybackSpeed)
|
Util.getPlayoutDurationForMediaDuration(playbackPositionUs, audioTrackPlaybackSpeed)
|
||||||
@ -479,11 +479,11 @@ import java.lang.reflect.Method;
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
maybePollAndCheckTimestamp(systemTimeUs, playbackPositionUs);
|
maybePollAndCheckTimestamp(systemTimeUs);
|
||||||
maybeUpdateLatency(systemTimeUs);
|
maybeUpdateLatency(systemTimeUs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybePollAndCheckTimestamp(long systemTimeUs, long playbackPositionUs) {
|
private void maybePollAndCheckTimestamp(long systemTimeUs) {
|
||||||
AudioTimestampPoller audioTimestampPoller = checkNotNull(this.audioTimestampPoller);
|
AudioTimestampPoller audioTimestampPoller = checkNotNull(this.audioTimestampPoller);
|
||||||
if (!audioTimestampPoller.maybePollTimestamp(systemTimeUs)) {
|
if (!audioTimestampPoller.maybePollTimestamp(systemTimeUs)) {
|
||||||
return;
|
return;
|
||||||
@ -492,6 +492,7 @@ import java.lang.reflect.Method;
|
|||||||
// Check the timestamp and accept/reject it.
|
// Check the timestamp and accept/reject it.
|
||||||
long audioTimestampSystemTimeUs = audioTimestampPoller.getTimestampSystemTimeUs();
|
long audioTimestampSystemTimeUs = audioTimestampPoller.getTimestampSystemTimeUs();
|
||||||
long audioTimestampPositionFrames = audioTimestampPoller.getTimestampPositionFrames();
|
long audioTimestampPositionFrames = audioTimestampPoller.getTimestampPositionFrames();
|
||||||
|
long playbackPositionUs = getPlaybackHeadPositionUs();
|
||||||
if (Math.abs(audioTimestampSystemTimeUs - systemTimeUs) > MAX_AUDIO_TIMESTAMP_OFFSET_US) {
|
if (Math.abs(audioTimestampSystemTimeUs - systemTimeUs) > MAX_AUDIO_TIMESTAMP_OFFSET_US) {
|
||||||
listener.onSystemTimeUsMismatch(
|
listener.onSystemTimeUsMismatch(
|
||||||
audioTimestampPositionFrames,
|
audioTimestampPositionFrames,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user