mirror of
https://github.com/Pinball3D/Rabbit-R1.git
synced 2024-12-25 08:32:38 -06:00
251 lines
8.9 KiB
Java
251 lines
8.9 KiB
Java
package androidx.media3.exoplayer.util;
|
|
|
|
import android.os.SystemClock;
|
|
import androidx.media3.exoplayer.upstream.Loader;
|
|
import com.google.common.base.Ascii;
|
|
import java.io.IOException;
|
|
import java.net.DatagramPacket;
|
|
import java.net.DatagramSocket;
|
|
import java.net.InetAddress;
|
|
import java.util.Arrays;
|
|
import java.util.ConcurrentModificationException;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public final class SntpClient {
|
|
public static final String DEFAULT_NTP_HOST = "time.android.com";
|
|
private static final int NTP_LEAP_NOSYNC = 3;
|
|
private static final int NTP_MODE_BROADCAST = 5;
|
|
private static final int NTP_MODE_CLIENT = 3;
|
|
private static final int NTP_MODE_SERVER = 4;
|
|
private static final int NTP_PACKET_SIZE = 48;
|
|
private static final int NTP_PORT = 123;
|
|
private static final int NTP_STRATUM_DEATH = 0;
|
|
private static final int NTP_STRATUM_MAX = 15;
|
|
private static final int NTP_VERSION = 3;
|
|
private static final long OFFSET_1900_TO_1970 = 2208988800L;
|
|
private static final int ORIGINATE_TIME_OFFSET = 24;
|
|
private static final int RECEIVE_TIME_OFFSET = 32;
|
|
private static final int TIMEOUT_MS = 10000;
|
|
private static final int TRANSMIT_TIME_OFFSET = 40;
|
|
private static long elapsedRealtimeOffsetMs = 0;
|
|
private static boolean isInitialized = false;
|
|
private static String ntpHost = "time.android.com";
|
|
private static final Object loaderLock = new Object();
|
|
private static final Object valueLock = new Object();
|
|
|
|
/* loaded from: classes2.dex */
|
|
public interface InitializationCallback {
|
|
void onInitializationFailed(IOException iOException);
|
|
|
|
void onInitialized();
|
|
}
|
|
|
|
static /* synthetic */ long access$400() throws IOException {
|
|
return loadNtpTimeOffsetMs();
|
|
}
|
|
|
|
private SntpClient() {
|
|
}
|
|
|
|
public static String getNtpHost() {
|
|
String str;
|
|
synchronized (valueLock) {
|
|
str = ntpHost;
|
|
}
|
|
return str;
|
|
}
|
|
|
|
public static void setNtpHost(String str) {
|
|
synchronized (valueLock) {
|
|
if (!ntpHost.equals(str)) {
|
|
ntpHost = str;
|
|
isInitialized = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean isInitialized() {
|
|
boolean z;
|
|
synchronized (valueLock) {
|
|
z = isInitialized;
|
|
}
|
|
return z;
|
|
}
|
|
|
|
public static long getElapsedRealtimeOffsetMs() {
|
|
long j;
|
|
synchronized (valueLock) {
|
|
j = isInitialized ? elapsedRealtimeOffsetMs : -9223372036854775807L;
|
|
}
|
|
return j;
|
|
}
|
|
|
|
public static void initialize(Loader loader, InitializationCallback initializationCallback) {
|
|
if (isInitialized()) {
|
|
if (initializationCallback != null) {
|
|
initializationCallback.onInitialized();
|
|
}
|
|
} else {
|
|
if (loader == null) {
|
|
loader = new Loader("SntpClient");
|
|
}
|
|
loader.startLoading(new NtpTimeLoadable(), new NtpTimeCallback(initializationCallback), 1);
|
|
}
|
|
}
|
|
|
|
private static long loadNtpTimeOffsetMs() throws IOException {
|
|
InetAddress byName = InetAddress.getByName(getNtpHost());
|
|
DatagramSocket datagramSocket = new DatagramSocket();
|
|
try {
|
|
datagramSocket.setSoTimeout(10000);
|
|
byte[] bArr = new byte[48];
|
|
DatagramPacket datagramPacket = new DatagramPacket(bArr, 48, byName, NTP_PORT);
|
|
bArr[0] = Ascii.ESC;
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
long elapsedRealtime = SystemClock.elapsedRealtime();
|
|
writeTimestamp(bArr, 40, currentTimeMillis);
|
|
datagramSocket.send(datagramPacket);
|
|
datagramSocket.receive(new DatagramPacket(bArr, 48));
|
|
long elapsedRealtime2 = SystemClock.elapsedRealtime();
|
|
long j = currentTimeMillis + (elapsedRealtime2 - elapsedRealtime);
|
|
byte b = bArr[0];
|
|
int i = bArr[1] & 255;
|
|
long readTimestamp = readTimestamp(bArr, 24);
|
|
long readTimestamp2 = readTimestamp(bArr, 32);
|
|
long readTimestamp3 = readTimestamp(bArr, 40);
|
|
checkValidServerReply((byte) ((b >> 6) & 3), (byte) (b & 7), i, readTimestamp3);
|
|
long j2 = (j + (((readTimestamp2 - readTimestamp) + (readTimestamp3 - j)) / 2)) - elapsedRealtime2;
|
|
datagramSocket.close();
|
|
return j2;
|
|
} catch (Throwable th) {
|
|
try {
|
|
datagramSocket.close();
|
|
} catch (Throwable th2) {
|
|
th.addSuppressed(th2);
|
|
}
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
private static long readTimestamp(byte[] bArr, int i) {
|
|
long read32 = read32(bArr, i);
|
|
long read322 = read32(bArr, i + 4);
|
|
if (read32 == 0 && read322 == 0) {
|
|
return 0L;
|
|
}
|
|
return ((read32 - OFFSET_1900_TO_1970) * 1000) + ((read322 * 1000) / 4294967296L);
|
|
}
|
|
|
|
private static void writeTimestamp(byte[] bArr, int i, long j) {
|
|
if (j == 0) {
|
|
Arrays.fill(bArr, i, i + 8, (byte) 0);
|
|
return;
|
|
}
|
|
long j2 = j / 1000;
|
|
long j3 = j - (j2 * 1000);
|
|
long j4 = j2 + OFFSET_1900_TO_1970;
|
|
bArr[i] = (byte) (j4 >> 24);
|
|
bArr[i + 1] = (byte) (j4 >> 16);
|
|
bArr[i + 2] = (byte) (j4 >> 8);
|
|
bArr[i + 3] = (byte) j4;
|
|
long j5 = (j3 * 4294967296L) / 1000;
|
|
bArr[i + 4] = (byte) (j5 >> 24);
|
|
bArr[i + 5] = (byte) (j5 >> 16);
|
|
bArr[i + 6] = (byte) (j5 >> 8);
|
|
bArr[i + 7] = (byte) (Math.random() * 255.0d);
|
|
}
|
|
|
|
private static long read32(byte[] bArr, int i) {
|
|
int i2 = bArr[i];
|
|
int i3 = bArr[i + 1];
|
|
int i4 = bArr[i + 2];
|
|
int i5 = bArr[i + 3];
|
|
if ((i2 & 128) == 128) {
|
|
i2 = (i2 & 127) + 128;
|
|
}
|
|
if ((i3 & 128) == 128) {
|
|
i3 = (i3 & 127) + 128;
|
|
}
|
|
if ((i4 & 128) == 128) {
|
|
i4 = (i4 & 127) + 128;
|
|
}
|
|
if ((i5 & 128) == 128) {
|
|
i5 = (i5 & 127) + 128;
|
|
}
|
|
return (i2 << 24) + (i3 << 16) + (i4 << 8) + i5;
|
|
}
|
|
|
|
private static void checkValidServerReply(byte b, byte b2, int i, long j) throws IOException {
|
|
if (b == 3) {
|
|
throw new IOException("SNTP: Unsynchronized server");
|
|
}
|
|
if (b2 != 4 && b2 != 5) {
|
|
throw new IOException("SNTP: Untrusted mode: " + ((int) b2));
|
|
}
|
|
if (i == 0 || i > 15) {
|
|
throw new IOException("SNTP: Untrusted stratum: " + i);
|
|
}
|
|
if (j == 0) {
|
|
throw new IOException("SNTP: Zero transmitTime");
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes2.dex */
|
|
private static final class NtpTimeLoadable implements Loader.Loadable {
|
|
@Override // androidx.media3.exoplayer.upstream.Loader.Loadable
|
|
public void cancelLoad() {
|
|
}
|
|
|
|
private NtpTimeLoadable() {
|
|
}
|
|
|
|
@Override // androidx.media3.exoplayer.upstream.Loader.Loadable
|
|
public void load() throws IOException {
|
|
synchronized (SntpClient.loaderLock) {
|
|
synchronized (SntpClient.valueLock) {
|
|
if (SntpClient.isInitialized) {
|
|
return;
|
|
}
|
|
long access$400 = SntpClient.access$400();
|
|
synchronized (SntpClient.valueLock) {
|
|
long unused = SntpClient.elapsedRealtimeOffsetMs = access$400;
|
|
boolean unused2 = SntpClient.isInitialized = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes2.dex */
|
|
private static final class NtpTimeCallback implements Loader.Callback<Loader.Loadable> {
|
|
private final InitializationCallback callback;
|
|
|
|
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
|
|
public void onLoadCanceled(Loader.Loadable loadable, long j, long j2, boolean z) {
|
|
}
|
|
|
|
public NtpTimeCallback(InitializationCallback initializationCallback) {
|
|
this.callback = initializationCallback;
|
|
}
|
|
|
|
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
|
|
public void onLoadCompleted(Loader.Loadable loadable, long j, long j2) {
|
|
if (this.callback != null) {
|
|
if (!SntpClient.isInitialized()) {
|
|
this.callback.onInitializationFailed(new IOException(new ConcurrentModificationException()));
|
|
} else {
|
|
this.callback.onInitialized();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
|
|
public Loader.LoadErrorAction onLoadError(Loader.Loadable loadable, long j, long j2, IOException iOException, int i) {
|
|
InitializationCallback initializationCallback = this.callback;
|
|
if (initializationCallback != null) {
|
|
initializationCallback.onInitializationFailed(iOException);
|
|
}
|
|
return Loader.DONT_RETRY;
|
|
}
|
|
}
|
|
}
|