package androidx.media3.common.util;

import androidx.media3.common.C;
import java.util.concurrent.TimeoutException;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class TimestampAdjuster {
    private static final long MAX_PTS_PLUS_ONE = 8589934592L;
    public static final long MODE_NO_OFFSET = Long.MAX_VALUE;
    public static final long MODE_SHARED = 9223372036854775806L;
    private long firstSampleTimestampUs;
    private long lastUnadjustedTimestampUs;
    private final ThreadLocal<Long> nextSampleTimestampUs = new ThreadLocal<>();
    private long timestampOffsetUs;

    public TimestampAdjuster(long j4) {
        reset(j4);
    }

    public static long ptsToUs(long j4) {
        return (j4 * 1000000) / 90000;
    }

    public static long usToNonWrappedPts(long j4) {
        return (j4 * 90000) / 1000000;
    }

    public static long usToWrappedPts(long j4) {
        return usToNonWrappedPts(j4) % MAX_PTS_PLUS_ONE;
    }

    public synchronized long adjustSampleTimestamp(long j4) {
        if (j4 == C.TIME_UNSET) {
            return C.TIME_UNSET;
        }
        try {
            if (!isInitialized()) {
                long jLongValue = this.firstSampleTimestampUs;
                if (jLongValue == MODE_SHARED) {
                    jLongValue = ((Long) Assertions.checkNotNull(this.nextSampleTimestampUs.get())).longValue();
                }
                this.timestampOffsetUs = jLongValue - j4;
                notifyAll();
            }
            this.lastUnadjustedTimestampUs = j4;
            return j4 + this.timestampOffsetUs;
        } catch (Throwable th) {
            throw th;
        }
    }

    public synchronized long adjustTsTimestamp(long j4) {
        if (j4 == C.TIME_UNSET) {
            return C.TIME_UNSET;
        }
        try {
            long j6 = this.lastUnadjustedTimestampUs;
            if (j6 != C.TIME_UNSET) {
                long jUsToNonWrappedPts = usToNonWrappedPts(j6);
                long j7 = (4294967296L + jUsToNonWrappedPts) / MAX_PTS_PLUS_ONE;
                long j8 = ((j7 - 1) * MAX_PTS_PLUS_ONE) + j4;
                long j9 = (j7 * MAX_PTS_PLUS_ONE) + j4;
                j4 = Math.abs(j8 - jUsToNonWrappedPts) < Math.abs(j9 - jUsToNonWrappedPts) ? j8 : j9;
            }
            return adjustSampleTimestamp(ptsToUs(j4));
        } catch (Throwable th) {
            throw th;
        }
    }

    public synchronized long adjustTsTimestampGreaterThanPreviousTimestamp(long j4) {
        if (j4 == C.TIME_UNSET) {
            return C.TIME_UNSET;
        }
        try {
            long j6 = this.lastUnadjustedTimestampUs;
            if (j6 != C.TIME_UNSET) {
                long jUsToNonWrappedPts = usToNonWrappedPts(j6);
                long j7 = jUsToNonWrappedPts / MAX_PTS_PLUS_ONE;
                long j8 = (j7 * MAX_PTS_PLUS_ONE) + j4;
                j4 = j8 >= jUsToNonWrappedPts ? j8 : ((j7 + 1) * MAX_PTS_PLUS_ONE) + j4;
            }
            return adjustSampleTimestamp(ptsToUs(j4));
        } catch (Throwable th) {
            throw th;
        }
    }

    public synchronized long getFirstSampleTimestampUs() {
        long j4;
        j4 = this.firstSampleTimestampUs;
        if (j4 == Long.MAX_VALUE || j4 == MODE_SHARED) {
            j4 = C.TIME_UNSET;
        }
        return j4;
    }

    public synchronized long getLastAdjustedTimestampUs() {
        long j4;
        try {
            j4 = this.lastUnadjustedTimestampUs;
        } catch (Throwable th) {
            throw th;
        }
        return j4 != C.TIME_UNSET ? j4 + this.timestampOffsetUs : getFirstSampleTimestampUs();
    }

    public synchronized long getTimestampOffsetUs() {
        return this.timestampOffsetUs;
    }

    public synchronized boolean isInitialized() {
        return this.timestampOffsetUs != C.TIME_UNSET;
    }

    public synchronized void reset(long j4) {
        this.firstSampleTimestampUs = j4;
        this.timestampOffsetUs = j4 == Long.MAX_VALUE ? 0L : -9223372036854775807L;
        this.lastUnadjustedTimestampUs = C.TIME_UNSET;
    }

    public synchronized void sharedInitializeOrWait(boolean z6, long j4, long j6) {
        try {
            Assertions.checkState(this.firstSampleTimestampUs == MODE_SHARED);
            if (isInitialized()) {
                return;
            }
            if (z6) {
                this.nextSampleTimestampUs.set(Long.valueOf(j4));
            } else {
                long jElapsedRealtime = 0;
                long j7 = j6;
                while (!isInitialized()) {
                    if (j6 == 0) {
                        wait();
                    } else {
                        Assertions.checkState(j7 > 0);
                        long jElapsedRealtime2 = android.os.SystemClock.elapsedRealtime();
                        wait(j7);
                        jElapsedRealtime += android.os.SystemClock.elapsedRealtime() - jElapsedRealtime2;
                        if (jElapsedRealtime >= j6 && !isInitialized()) {
                            throw new TimeoutException("TimestampAdjuster failed to initialize in " + j6 + " milliseconds");
                        }
                        j7 = j6 - jElapsedRealtime;
                    }
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }
}
