Commit 4cf7d3c7 by tonihei

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
parent 05d36524
...@@ -451,13 +451,13 @@ import java.lang.reflect.Method; ...@@ -451,13 +451,13 @@ import java.lang.reflect.Method;
} }
private void maybeSampleSyncParams() { private void maybeSampleSyncParams() {
long systemTimeUs = System.nanoTime() / 1000;
if (systemTimeUs - lastPlayheadSampleTimeUs >= MIN_PLAYHEAD_OFFSET_SAMPLE_INTERVAL_US) {
long playbackPositionUs = getPlaybackHeadPositionUs(); long playbackPositionUs = getPlaybackHeadPositionUs();
if (playbackPositionUs == 0) { if (playbackPositionUs == 0) {
// The AudioTrack hasn't output anything yet. // The AudioTrack hasn't output anything yet.
return; return;
} }
long systemTimeUs = System.nanoTime() / 1000;
if (systemTimeUs - lastPlayheadSampleTimeUs >= MIN_PLAYHEAD_OFFSET_SAMPLE_INTERVAL_US) {
// 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; ...@@ -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; ...@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment