package com.google.android.exoplayer2; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Clock; import com.google.android.exoplayer2.util.MediaClock; import com.google.android.exoplayer2.util.StandaloneMediaClock; /* loaded from: classes2.dex */ final class DefaultMediaClock implements MediaClock { private boolean isUsingStandaloneClock = true; private final PlaybackParametersListener listener; private MediaClock rendererClock; private Renderer rendererClockSource; private final StandaloneMediaClock standaloneClock; private boolean standaloneClockIsStarted; /* loaded from: classes2.dex */ public interface PlaybackParametersListener { void onPlaybackParametersChanged(PlaybackParameters playbackParameters); } public void onRendererDisabled(Renderer renderer) { if (renderer == this.rendererClockSource) { this.rendererClock = null; this.rendererClockSource = null; this.isUsingStandaloneClock = true; } } public DefaultMediaClock(PlaybackParametersListener playbackParametersListener, Clock clock) { this.listener = playbackParametersListener; this.standaloneClock = new StandaloneMediaClock(clock); } public void start() { this.standaloneClockIsStarted = true; this.standaloneClock.start(); } public void stop() { this.standaloneClockIsStarted = false; this.standaloneClock.stop(); } public void resetPosition(long j) { this.standaloneClock.resetPosition(j); } public void onRendererEnabled(Renderer renderer) throws ExoPlaybackException { MediaClock mediaClock; MediaClock mediaClock2 = renderer.getMediaClock(); if (mediaClock2 == null || mediaClock2 == (mediaClock = this.rendererClock)) { return; } if (mediaClock != null) { throw ExoPlaybackException.createForUnexpected(new IllegalStateException("Multiple renderer media clocks enabled.")); } this.rendererClock = mediaClock2; this.rendererClockSource = renderer; mediaClock2.setPlaybackParameters(this.standaloneClock.getPlaybackParameters()); } public long syncAndGetPositionUs(boolean z) { syncClocks(z); return getPositionUs(); } @Override // com.google.android.exoplayer2.util.MediaClock public long getPositionUs() { if (this.isUsingStandaloneClock) { return this.standaloneClock.getPositionUs(); } return ((MediaClock) Assertions.checkNotNull(this.rendererClock)).getPositionUs(); } @Override // com.google.android.exoplayer2.util.MediaClock public void setPlaybackParameters(PlaybackParameters playbackParameters) { MediaClock mediaClock = this.rendererClock; if (mediaClock != null) { mediaClock.setPlaybackParameters(playbackParameters); playbackParameters = this.rendererClock.getPlaybackParameters(); } this.standaloneClock.setPlaybackParameters(playbackParameters); } @Override // com.google.android.exoplayer2.util.MediaClock public PlaybackParameters getPlaybackParameters() { MediaClock mediaClock = this.rendererClock; if (mediaClock != null) { return mediaClock.getPlaybackParameters(); } return this.standaloneClock.getPlaybackParameters(); } private void syncClocks(boolean z) { if (shouldUseStandaloneClock(z)) { this.isUsingStandaloneClock = true; if (this.standaloneClockIsStarted) { this.standaloneClock.start(); return; } return; } MediaClock mediaClock = (MediaClock) Assertions.checkNotNull(this.rendererClock); long positionUs = mediaClock.getPositionUs(); if (this.isUsingStandaloneClock) { if (positionUs < this.standaloneClock.getPositionUs()) { this.standaloneClock.stop(); return; } else { this.isUsingStandaloneClock = false; if (this.standaloneClockIsStarted) { this.standaloneClock.start(); } } } this.standaloneClock.resetPosition(positionUs); PlaybackParameters playbackParameters = mediaClock.getPlaybackParameters(); if (playbackParameters.equals(this.standaloneClock.getPlaybackParameters())) { return; } this.standaloneClock.setPlaybackParameters(playbackParameters); this.listener.onPlaybackParametersChanged(playbackParameters); } private boolean shouldUseStandaloneClock(boolean z) { Renderer renderer = this.rendererClockSource; return renderer == null || renderer.isEnded() || (!this.rendererClockSource.isReady() && (z || this.rendererClockSource.hasReadStreamToEnd())); } }