Skip to content

Commit

Permalink
Use current position code when checking if AudioTrack has pending data
Browse files Browse the repository at this point in the history
We currently rely on the raw playback head position to check if any
data is pending in the AudioTrack (e.g. to know if the renderer is
still ready). We can use the value returned from getCurrentPositionUs
instead to align the "isReady" logic with the playback position logic.

This has the side effect that getPlaybackHeadPosition position is called
less often when the position is obtained via getTimestamp.

PiperOrigin-RevId: 514747613
  • Loading branch information
tonihei committed Mar 14, 2023
1 parent 82ab327 commit fe71087
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ public void handleEndOfStream(long writtenFrames) {
* @return Whether the audio track has any pending data to play out.
*/
public boolean hasPendingData(long writtenFrames) {
return writtenFrames > getPlaybackHeadPosition() || forceHasPendingData();
return writtenFrames > durationUsToFrames(getCurrentPositionUs(/* sourceEnded= */ false))
|| forceHasPendingData();
}

/**
Expand Down Expand Up @@ -542,6 +543,10 @@ private long framesToDurationUs(long frameCount) {
return (frameCount * C.MICROS_PER_SECOND) / outputSampleRate;
}

private long durationUsToFrames(long durationUs) {
return (durationUs * outputSampleRate) / C.MICROS_PER_SECOND;
}

private void resetSyncParams() {
smoothedPlayheadOffsetUs = 0;
playheadOffsetCount = 0;
Expand Down Expand Up @@ -591,7 +596,7 @@ private long getPlaybackHeadPosition() {
long elapsedTimeSinceStopUs = (currentTimeMs * 1000) - stopTimestampUs;
long mediaTimeSinceStopUs =
Util.getMediaDurationForPlayoutDuration(elapsedTimeSinceStopUs, audioTrackPlaybackSpeed);
long framesSinceStop = (mediaTimeSinceStopUs * outputSampleRate) / C.MICROS_PER_SECOND;
long framesSinceStop = durationUsToFrames(mediaTimeSinceStopUs);
return min(endPlaybackHeadPosition, stopPlaybackHeadPosition + framesSinceStop);
}
if (currentTimeMs - lastRawPlaybackHeadPositionSampleTimeMs
Expand Down

0 comments on commit fe71087

Please sign in to comment.