Rabbit-R1/original r1/java/sources/androidx/media3/common/util/TimestampAdjuster.java
2024-05-21 17:08:36 -04:00

119 lines
4.1 KiB
Java

package androidx.media3.common.util;
import java.util.concurrent.TimeoutException;
/* 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<Long> nextSampleTimestampUs = new ThreadLocal<>();
private long timestampOffsetUs;
public TimestampAdjuster(long j) {
reset(j);
}
public synchronized void sharedInitializeOrWait(boolean z, long j, long j2) throws InterruptedException, TimeoutException {
Assertions.checkState(this.firstSampleTimestampUs == 9223372036854775806L);
if (isInitialized()) {
return;
}
if (z) {
this.nextSampleTimestampUs.set(Long.valueOf(j));
} else {
long j3 = 0;
long j4 = j2;
while (!isInitialized()) {
if (j2 == 0) {
wait();
} else {
Assertions.checkState(j4 > 0);
long elapsedRealtime = android.os.SystemClock.elapsedRealtime();
wait(j4);
j3 += android.os.SystemClock.elapsedRealtime() - elapsedRealtime;
if (j3 >= j2 && !isInitialized()) {
throw new TimeoutException("TimestampAdjuster failed to initialize in " + j2 + " milliseconds");
}
j4 = j2 - j3;
}
}
}
}
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 (!isInitialized()) {
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 synchronized boolean isInitialized() {
return this.timestampOffsetUs != -9223372036854775807L;
}
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;
}
}