Skip to content

Commit caf7c2b

Browse files
toniheiivanbuper
authored andcommitted
Fix position tracking bug for inaccurate audio processors
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)
1 parent 7839f42 commit caf7c2b

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/DefaultAudioSink.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,23 +1740,17 @@ private long applyMediaPositionParameters(long positionUs) {
17401740
audioProcessorChain.getMediaDuration(playoutDurationSinceLastCheckpointUs);
17411741
long currentMediaPositionUs =
17421742
mediaPositionParameters.mediaTimeUs + actualMediaDurationSinceLastCheckpointUs;
1743-
long mediaDurationEstimateDiffUs =
1743+
mediaPositionParameters.mediaPositionDriftUs =
17441744
actualMediaDurationSinceLastCheckpointUs - estimatedMediaDurationSinceLastCheckpointUs;
1745-
if (Math.abs(mediaDurationEstimateDiffUs) > 10000) {
1746-
// Update current media position parameters if the estimate drifted from the actual
1747-
// media duration created by the audio processor chain. This ensures the estimate is always
1748-
// fairly accurate and we can rely on it once we enter the else-branch below.
1749-
mediaPositionParameters =
1750-
new MediaPositionParameters(
1751-
mediaPositionParameters.playbackParameters, currentMediaPositionUs, positionUs);
1752-
}
17531745
return currentMediaPositionUs;
17541746
} else {
17551747
// The processor chain has been configured with new parameters, but we're still playing audio
17561748
// that was processed using previous parameters. We can't scale the playout duration using the
17571749
// processor chain in this case, so we fall back to scaling using the previous parameters'
17581750
// target speed instead.
1759-
return mediaPositionParameters.mediaTimeUs + estimatedMediaDurationSinceLastCheckpointUs;
1751+
return mediaPositionParameters.mediaTimeUs
1752+
+ estimatedMediaDurationSinceLastCheckpointUs
1753+
+ mediaPositionParameters.mediaPositionDriftUs;
17601754
}
17611755
}
17621756

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

2097+
/**
2098+
* An updatable value for the observed drift between the actual media time and the one that can
2099+
* be calculated from the other parameters.
2100+
*/
2101+
public long mediaPositionDriftUs;
2102+
21032103
private MediaPositionParameters(
21042104
PlaybackParameters playbackParameters, long mediaTimeUs, long audioTrackPositionUs) {
21052105
this.playbackParameters = playbackParameters;

0 commit comments

Comments
 (0)