package com.google.android.exoplayer2.util; /* loaded from: classes2.dex */ 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 nextSampleTimestampUs = new ThreadLocal<>(); private long timestampOffsetUs; public TimestampAdjuster(long j) { reset(j); } public synchronized void sharedInitializeOrWait(boolean z, long j) throws InterruptedException { Assertions.checkState(this.firstSampleTimestampUs == 9223372036854775806L); if (this.timestampOffsetUs != -9223372036854775807L) { return; } if (z) { this.nextSampleTimestampUs.set(Long.valueOf(j)); } else { while (this.timestampOffsetUs == -9223372036854775807L) { wait(); } } } public synchronized long getFirstSampleTimestampUs() { long j; j = this.firstSampleTimestampUs; if (j == Long.MAX_VALUE || j == 9223372036854775806L) { j = -9223372036854775807L; } return j; } public synchronized long getLastAdjustedTimestampUs() { long j; j = this.lastUnadjustedTimestampUs; return j != -9223372036854775807L ? j + this.timestampOffsetUs : getFirstSampleTimestampUs(); } public synchronized long getTimestampOffsetUs() { return this.timestampOffsetUs; } public synchronized void reset(long j) { this.firstSampleTimestampUs = j; this.timestampOffsetUs = j == Long.MAX_VALUE ? 0L : -9223372036854775807L; this.lastUnadjustedTimestampUs = -9223372036854775807L; } public synchronized long adjustTsTimestamp(long j) { if (j == -9223372036854775807L) { return -9223372036854775807L; } long j2 = this.lastUnadjustedTimestampUs; if (j2 != -9223372036854775807L) { long usToNonWrappedPts = usToNonWrappedPts(j2); long j3 = (4294967296L + usToNonWrappedPts) / MAX_PTS_PLUS_ONE; long j4 = ((j3 - 1) * MAX_PTS_PLUS_ONE) + j; j += j3 * MAX_PTS_PLUS_ONE; if (Math.abs(j4 - usToNonWrappedPts) < Math.abs(j - usToNonWrappedPts)) { j = j4; } } return adjustSampleTimestamp(ptsToUs(j)); } public synchronized long adjustSampleTimestamp(long j) { if (j == -9223372036854775807L) { return -9223372036854775807L; } if (this.timestampOffsetUs == -9223372036854775807L) { long j2 = this.firstSampleTimestampUs; if (j2 == 9223372036854775806L) { j2 = ((Long) Assertions.checkNotNull(this.nextSampleTimestampUs.get())).longValue(); } this.timestampOffsetUs = j2 - j; notifyAll(); } this.lastUnadjustedTimestampUs = j; return j + this.timestampOffsetUs; } public static long ptsToUs(long j) { return (j * 1000000) / 90000; } public static long usToWrappedPts(long j) { return usToNonWrappedPts(j) % MAX_PTS_PLUS_ONE; } public static long usToNonWrappedPts(long j) { return (j * 90000) / 1000000; } }