Skip to content

Commit

Permalink
Fix position tracking bug for inaccurate audio processors
Browse files Browse the repository at this point in the history
If audio processors report a drifting position, we currently update
the media position parameters to correct this drift. However, this
means we pass in the wrong value to
audioProcessorChain.getMediaDuration, which reuqires the time since
the last flush.

To fix this problem, we can instead save the drift seperately and
apply it where needed.

PiperOrigin-RevId: 692202219
(cherry picked from commit 06718c5)
  • Loading branch information
tonihei authored and ivanbuper committed Nov 5, 2024
1 parent 7839f42 commit caf7c2b
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1740,23 +1740,17 @@ private long applyMediaPositionParameters(long positionUs) {
audioProcessorChain.getMediaDuration(playoutDurationSinceLastCheckpointUs);
long currentMediaPositionUs =
mediaPositionParameters.mediaTimeUs + actualMediaDurationSinceLastCheckpointUs;
long mediaDurationEstimateDiffUs =
mediaPositionParameters.mediaPositionDriftUs =
actualMediaDurationSinceLastCheckpointUs - estimatedMediaDurationSinceLastCheckpointUs;
if (Math.abs(mediaDurationEstimateDiffUs) > 10000) {
// Update current media position parameters if the estimate drifted from the actual
// media duration created by the audio processor chain. This ensures the estimate is always
// fairly accurate and we can rely on it once we enter the else-branch below.
mediaPositionParameters =
new MediaPositionParameters(
mediaPositionParameters.playbackParameters, currentMediaPositionUs, positionUs);
}
return currentMediaPositionUs;
} else {
// The processor chain has been configured with new parameters, but we're still playing audio
// that was processed using previous parameters. We can't scale the playout duration using the
// processor chain in this case, so we fall back to scaling using the previous parameters'
// target speed instead.
return mediaPositionParameters.mediaTimeUs + estimatedMediaDurationSinceLastCheckpointUs;
return mediaPositionParameters.mediaTimeUs
+ estimatedMediaDurationSinceLastCheckpointUs
+ mediaPositionParameters.mediaPositionDriftUs;
}
}

Expand Down Expand Up @@ -2100,6 +2094,12 @@ private static final class MediaPositionParameters {
/** The audio track position from which the playback parameters apply, in microseconds. */
public final long audioTrackPositionUs;

/**
* An updatable value for the observed drift between the actual media time and the one that can
* be calculated from the other parameters.
*/
public long mediaPositionDriftUs;

private MediaPositionParameters(
PlaybackParameters playbackParameters, long mediaTimeUs, long audioTrackPositionUs) {
this.playbackParameters = playbackParameters;
Expand Down

0 comments on commit caf7c2b

Please sign in to comment.