package androidx.media3.exoplayer;

import android.os.SystemClock;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class DefaultLivePlaybackSpeedControl implements LivePlaybackSpeedControl {
    public static final float DEFAULT_FALLBACK_MAX_PLAYBACK_SPEED = 1.03f;
    public static final float DEFAULT_FALLBACK_MIN_PLAYBACK_SPEED = 0.97f;
    public static final long DEFAULT_MAX_LIVE_OFFSET_ERROR_MS_FOR_UNIT_SPEED = 20;
    public static final float DEFAULT_MIN_POSSIBLE_LIVE_OFFSET_SMOOTHING_FACTOR = 0.999f;
    public static final long DEFAULT_MIN_UPDATE_INTERVAL_MS = 1000;
    public static final float DEFAULT_PROPORTIONAL_CONTROL_FACTOR = 0.1f;
    public static final long DEFAULT_TARGET_LIVE_OFFSET_INCREMENT_ON_REBUFFER_MS = 500;
    private float adjustedPlaybackSpeed;
    private long currentTargetLiveOffsetUs;
    private final float fallbackMaxPlaybackSpeed;
    private final float fallbackMinPlaybackSpeed;
    private long idealTargetLiveOffsetUs;
    private long lastPlaybackSpeedUpdateMs;
    private final long maxLiveOffsetErrorUsForUnitSpeed;
    private float maxPlaybackSpeed;
    private long maxTargetLiveOffsetUs;
    private long mediaConfigurationTargetLiveOffsetUs;
    private float minPlaybackSpeed;
    private final float minPossibleLiveOffsetSmoothingFactor;
    private long minTargetLiveOffsetUs;
    private final long minUpdateIntervalMs;
    private final float proportionalControlFactor;
    private long smoothedMinPossibleLiveOffsetDeviationUs;
    private long smoothedMinPossibleLiveOffsetUs;
    private long targetLiveOffsetOverrideUs;
    private final long targetLiveOffsetRebufferDeltaUs;

    public static final class Builder {
        private float fallbackMinPlaybackSpeed = 0.97f;
        private float fallbackMaxPlaybackSpeed = 1.03f;
        private long minUpdateIntervalMs = 1000;
        private float proportionalControlFactorUs = 1.0E-7f;
        private long maxLiveOffsetErrorUsForUnitSpeed = Util.msToUs(20);
        private long targetLiveOffsetIncrementOnRebufferUs = Util.msToUs(500);
        private float minPossibleLiveOffsetSmoothingFactor = 0.999f;

        public DefaultLivePlaybackSpeedControl build() {
            return new DefaultLivePlaybackSpeedControl(this.fallbackMinPlaybackSpeed, this.fallbackMaxPlaybackSpeed, this.minUpdateIntervalMs, this.proportionalControlFactorUs, this.maxLiveOffsetErrorUsForUnitSpeed, this.targetLiveOffsetIncrementOnRebufferUs, this.minPossibleLiveOffsetSmoothingFactor);
        }

        public Builder setFallbackMaxPlaybackSpeed(float f6) {
            Assertions.checkArgument(f6 >= 1.0f);
            this.fallbackMaxPlaybackSpeed = f6;
            return this;
        }

        public Builder setFallbackMinPlaybackSpeed(float f6) {
            Assertions.checkArgument(0.0f < f6 && f6 <= 1.0f);
            this.fallbackMinPlaybackSpeed = f6;
            return this;
        }

        public Builder setMaxLiveOffsetErrorMsForUnitSpeed(long j4) {
            Assertions.checkArgument(j4 > 0);
            this.maxLiveOffsetErrorUsForUnitSpeed = Util.msToUs(j4);
            return this;
        }

        public Builder setMinPossibleLiveOffsetSmoothingFactor(float f6) {
            Assertions.checkArgument(f6 >= 0.0f && f6 < 1.0f);
            this.minPossibleLiveOffsetSmoothingFactor = f6;
            return this;
        }

        public Builder setMinUpdateIntervalMs(long j4) {
            Assertions.checkArgument(j4 > 0);
            this.minUpdateIntervalMs = j4;
            return this;
        }

        public Builder setProportionalControlFactor(float f6) {
            Assertions.checkArgument(f6 > 0.0f);
            this.proportionalControlFactorUs = f6 / 1000000.0f;
            return this;
        }

        public Builder setTargetLiveOffsetIncrementOnRebufferMs(long j4) {
            Assertions.checkArgument(j4 >= 0);
            this.targetLiveOffsetIncrementOnRebufferUs = Util.msToUs(j4);
            return this;
        }
    }

    private void adjustTargetLiveOffsetUs(long j4) {
        long j6 = (this.smoothedMinPossibleLiveOffsetDeviationUs * 3) + this.smoothedMinPossibleLiveOffsetUs;
        if (this.currentTargetLiveOffsetUs <= j6) {
            long jConstrainValue = Util.constrainValue(j4 - ((long) (Math.max(0.0f, this.adjustedPlaybackSpeed - 1.0f) / this.proportionalControlFactor)), this.currentTargetLiveOffsetUs, j6);
            this.currentTargetLiveOffsetUs = jConstrainValue;
            long j7 = this.maxTargetLiveOffsetUs;
            if (j7 == androidx.media3.common.C.TIME_UNSET || jConstrainValue <= j7) {
                return;
            }
            this.currentTargetLiveOffsetUs = j7;
            return;
        }
        float fMsToUs = Util.msToUs(this.minUpdateIntervalMs);
        long[] jArr = {j6, this.idealTargetLiveOffsetUs, this.currentTargetLiveOffsetUs - (((long) ((this.adjustedPlaybackSpeed - 1.0f) * fMsToUs)) + ((long) ((this.maxPlaybackSpeed - 1.0f) * fMsToUs)))};
        long j8 = jArr[0];
        for (int i6 = 1; i6 < 3; i6++) {
            long j9 = jArr[i6];
            if (j9 > j8) {
                j8 = j9;
            }
        }
        this.currentTargetLiveOffsetUs = j8;
    }

    private void maybeResetTargetLiveOffsetUs() {
        long j4;
        long j6 = this.mediaConfigurationTargetLiveOffsetUs;
        if (j6 != androidx.media3.common.C.TIME_UNSET) {
            j4 = this.targetLiveOffsetOverrideUs;
            if (j4 == androidx.media3.common.C.TIME_UNSET) {
                long j7 = this.minTargetLiveOffsetUs;
                if (j7 != androidx.media3.common.C.TIME_UNSET && j6 < j7) {
                    j6 = j7;
                }
                j4 = this.maxTargetLiveOffsetUs;
                if (j4 == androidx.media3.common.C.TIME_UNSET || j6 <= j4) {
                    j4 = j6;
                }
            }
        } else {
            j4 = -9223372036854775807L;
        }
        if (this.idealTargetLiveOffsetUs == j4) {
            return;
        }
        this.idealTargetLiveOffsetUs = j4;
        this.currentTargetLiveOffsetUs = j4;
        this.smoothedMinPossibleLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.smoothedMinPossibleLiveOffsetDeviationUs = androidx.media3.common.C.TIME_UNSET;
        this.lastPlaybackSpeedUpdateMs = androidx.media3.common.C.TIME_UNSET;
    }

    private static long smooth(long j4, long j6, float f6) {
        return (long) (((1.0f - f6) * j6) + (j4 * f6));
    }

    private void updateSmoothedMinPossibleLiveOffsetUs(long j4, long j6) {
        long j7 = j4 - j6;
        long j8 = this.smoothedMinPossibleLiveOffsetUs;
        if (j8 == androidx.media3.common.C.TIME_UNSET) {
            this.smoothedMinPossibleLiveOffsetUs = j7;
            this.smoothedMinPossibleLiveOffsetDeviationUs = 0L;
        } else {
            long jMax = Math.max(j7, smooth(j8, j7, this.minPossibleLiveOffsetSmoothingFactor));
            this.smoothedMinPossibleLiveOffsetUs = jMax;
            this.smoothedMinPossibleLiveOffsetDeviationUs = smooth(this.smoothedMinPossibleLiveOffsetDeviationUs, Math.abs(j7 - jMax), this.minPossibleLiveOffsetSmoothingFactor);
        }
    }

    @Override // androidx.media3.exoplayer.LivePlaybackSpeedControl
    public float getAdjustedPlaybackSpeed(long j4, long j6) {
        if (this.mediaConfigurationTargetLiveOffsetUs == androidx.media3.common.C.TIME_UNSET) {
            return 1.0f;
        }
        updateSmoothedMinPossibleLiveOffsetUs(j4, j6);
        if (this.lastPlaybackSpeedUpdateMs != androidx.media3.common.C.TIME_UNSET && SystemClock.elapsedRealtime() - this.lastPlaybackSpeedUpdateMs < this.minUpdateIntervalMs) {
            return this.adjustedPlaybackSpeed;
        }
        this.lastPlaybackSpeedUpdateMs = SystemClock.elapsedRealtime();
        adjustTargetLiveOffsetUs(j4);
        long j7 = j4 - this.currentTargetLiveOffsetUs;
        if (Math.abs(j7) < this.maxLiveOffsetErrorUsForUnitSpeed) {
            this.adjustedPlaybackSpeed = 1.0f;
        } else {
            this.adjustedPlaybackSpeed = Util.constrainValue((this.proportionalControlFactor * j7) + 1.0f, this.minPlaybackSpeed, this.maxPlaybackSpeed);
        }
        return this.adjustedPlaybackSpeed;
    }

    @Override // androidx.media3.exoplayer.LivePlaybackSpeedControl
    public long getTargetLiveOffsetUs() {
        return this.currentTargetLiveOffsetUs;
    }

    @Override // androidx.media3.exoplayer.LivePlaybackSpeedControl
    public void notifyRebuffer() {
        long j4 = this.currentTargetLiveOffsetUs;
        if (j4 == androidx.media3.common.C.TIME_UNSET) {
            return;
        }
        long j6 = j4 + this.targetLiveOffsetRebufferDeltaUs;
        this.currentTargetLiveOffsetUs = j6;
        long j7 = this.maxTargetLiveOffsetUs;
        if (j7 != androidx.media3.common.C.TIME_UNSET && j6 > j7) {
            this.currentTargetLiveOffsetUs = j7;
        }
        this.lastPlaybackSpeedUpdateMs = androidx.media3.common.C.TIME_UNSET;
    }

    @Override // androidx.media3.exoplayer.LivePlaybackSpeedControl
    public void setLiveConfiguration(MediaItem.LiveConfiguration liveConfiguration) {
        this.mediaConfigurationTargetLiveOffsetUs = Util.msToUs(liveConfiguration.targetOffsetMs);
        this.minTargetLiveOffsetUs = Util.msToUs(liveConfiguration.minOffsetMs);
        this.maxTargetLiveOffsetUs = Util.msToUs(liveConfiguration.maxOffsetMs);
        float f6 = liveConfiguration.minPlaybackSpeed;
        if (f6 == -3.4028235E38f) {
            f6 = this.fallbackMinPlaybackSpeed;
        }
        this.minPlaybackSpeed = f6;
        float f7 = liveConfiguration.maxPlaybackSpeed;
        if (f7 == -3.4028235E38f) {
            f7 = this.fallbackMaxPlaybackSpeed;
        }
        this.maxPlaybackSpeed = f7;
        if (f6 == 1.0f && f7 == 1.0f) {
            this.mediaConfigurationTargetLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        }
        maybeResetTargetLiveOffsetUs();
    }

    @Override // androidx.media3.exoplayer.LivePlaybackSpeedControl
    public void setTargetLiveOffsetOverrideUs(long j4) {
        this.targetLiveOffsetOverrideUs = j4;
        maybeResetTargetLiveOffsetUs();
    }

    private DefaultLivePlaybackSpeedControl(float f6, float f7, long j4, float f8, long j6, long j7, float f9) {
        this.fallbackMinPlaybackSpeed = f6;
        this.fallbackMaxPlaybackSpeed = f7;
        this.minUpdateIntervalMs = j4;
        this.proportionalControlFactor = f8;
        this.maxLiveOffsetErrorUsForUnitSpeed = j6;
        this.targetLiveOffsetRebufferDeltaUs = j7;
        this.minPossibleLiveOffsetSmoothingFactor = f9;
        this.mediaConfigurationTargetLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.targetLiveOffsetOverrideUs = androidx.media3.common.C.TIME_UNSET;
        this.minTargetLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.maxTargetLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.minPlaybackSpeed = f6;
        this.maxPlaybackSpeed = f7;
        this.adjustedPlaybackSpeed = 1.0f;
        this.lastPlaybackSpeedUpdateMs = androidx.media3.common.C.TIME_UNSET;
        this.idealTargetLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.currentTargetLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.smoothedMinPossibleLiveOffsetUs = androidx.media3.common.C.TIME_UNSET;
        this.smoothedMinPossibleLiveOffsetDeviationUs = androidx.media3.common.C.TIME_UNSET;
    }
}
