Commit fe710871 by tonihei

Use current position code when checking if AudioTrack has pending data

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
parent 82ab327e
...@@ -420,7 +420,8 @@ import java.lang.reflect.Method; ...@@ -420,7 +420,8 @@ import java.lang.reflect.Method;
* @return Whether the audio track has any pending data to play out. * @return Whether the audio track has any pending data to play out.
*/ */
public boolean hasPendingData(long writtenFrames) { public boolean hasPendingData(long writtenFrames) {
return writtenFrames > getPlaybackHeadPosition() || forceHasPendingData(); return writtenFrames > durationUsToFrames(getCurrentPositionUs(/* sourceEnded= */ false))
|| forceHasPendingData();
} }
/** /**
...@@ -542,6 +543,10 @@ import java.lang.reflect.Method; ...@@ -542,6 +543,10 @@ import java.lang.reflect.Method;
return (frameCount * C.MICROS_PER_SECOND) / outputSampleRate; return (frameCount * C.MICROS_PER_SECOND) / outputSampleRate;
} }
private long durationUsToFrames(long durationUs) {
return (durationUs * outputSampleRate) / C.MICROS_PER_SECOND;
}
private void resetSyncParams() { private void resetSyncParams() {
smoothedPlayheadOffsetUs = 0; smoothedPlayheadOffsetUs = 0;
playheadOffsetCount = 0; playheadOffsetCount = 0;
...@@ -591,7 +596,7 @@ import java.lang.reflect.Method; ...@@ -591,7 +596,7 @@ import java.lang.reflect.Method;
long elapsedTimeSinceStopUs = (currentTimeMs * 1000) - stopTimestampUs; long elapsedTimeSinceStopUs = (currentTimeMs * 1000) - stopTimestampUs;
long mediaTimeSinceStopUs = long mediaTimeSinceStopUs =
Util.getMediaDurationForPlayoutDuration(elapsedTimeSinceStopUs, audioTrackPlaybackSpeed); Util.getMediaDurationForPlayoutDuration(elapsedTimeSinceStopUs, audioTrackPlaybackSpeed);
long framesSinceStop = (mediaTimeSinceStopUs * outputSampleRate) / C.MICROS_PER_SECOND; long framesSinceStop = durationUsToFrames(mediaTimeSinceStopUs);
return min(endPlaybackHeadPosition, stopPlaybackHeadPosition + framesSinceStop); return min(endPlaybackHeadPosition, stopPlaybackHeadPosition + framesSinceStop);
} }
if (currentTimeMs - lastRawPlaybackHeadPositionSampleTimeMs if (currentTimeMs - lastRawPlaybackHeadPositionSampleTimeMs
......
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