package com.applovin.impl; import android.media.AudioTrack; import android.os.SystemClock; import androidx.work.WorkRequest; import com.mbridge.msdk.playercommon.exoplayer2.C; import java.lang.reflect.Method; /* loaded from: classes.dex */ final class u1 { private long A; private long B; private long C; private boolean D; private long E; private long F; private final a a; private final long[] b; private AudioTrack c; private int d; private int e; private t1 f; private int g; private boolean h; private long i; private float j; private boolean k; private long l; private long m; private Method n; private long o; private boolean p; private boolean q; private long r; private long s; private long t; private long u; private int v; private int w; private long x; private long y; private long z; public interface a { void a(int i, long j); void a(long j); void a(long j, long j2, long j3, long j4); void b(long j); void b(long j, long j2, long j3, long j4); } private void h() { this.l = 0L; this.w = 0; this.v = 0; this.m = 0L; this.C = 0L; this.F = 0L; this.k = false; } public u1(a aVar) { this.a = (a) b1.a(aVar); if (xp.a >= 18) { try { this.n = AudioTrack.class.getMethod("getLatency", null); } catch (NoSuchMethodException unused) { } } this.b = new long[10]; } public void i() { ((t1) b1.a(this.f)).f(); } public boolean g(long j) { int playState = ((AudioTrack) b1.a(this.c)).getPlayState(); if (this.h) { if (playState == 2) { this.p = false; return false; } if (playState == 1 && b() == 0) { return false; } } boolean z = this.p; boolean e = e(j); this.p = e; if (z && !e && playState != 1) { this.a.a(this.e, t2.b(this.i)); } return true; } public int b(long j) { return this.e - ((int) (j - (b() * this.d))); } public long c(long j) { return t2.b(a(j - b())); } public boolean f(long j) { return this.y != C.TIME_UNSET && j > 0 && SystemClock.elapsedRealtime() - this.y >= 200; } public void d(long j) { this.z = b(); this.x = SystemClock.elapsedRealtime() * 1000; this.A = j; } public boolean e(long j) { return j > b() || a(); } private void h(long j) { Method method; if (!this.q || (method = this.n) == null || j - this.r < 500000) { return; } try { long intValue = (((Integer) xp.a((Integer) method.invoke(b1.a(this.c), new Object[0]))).intValue() * 1000) - this.i; this.o = intValue; long max = Math.max(intValue, 0L); this.o = max; if (max > 5000000) { this.a.b(max); this.o = 0L; } } catch (Exception unused) { this.n = null; } this.r = j; } private boolean a() { return this.h && ((AudioTrack) b1.a(this.c)).getPlayState() == 2 && b() == 0; } public boolean d() { return ((AudioTrack) b1.a(this.c)).getPlayState() == 3; } public void g() { h(); this.c = null; this.f = null; } public boolean f() { h(); if (this.x != C.TIME_UNSET) { return false; } ((t1) b1.a(this.f)).f(); return true; } private void e() { long c = c(); if (c == 0) { return; } long nanoTime = System.nanoTime() / 1000; if (nanoTime - this.m >= WorkRequest.DEFAULT_BACKOFF_DELAY_MILLIS) { long[] jArr = this.b; int i = this.v; jArr[i] = c - nanoTime; this.v = (i + 1) % 10; int i2 = this.w; if (i2 < 10) { this.w = i2 + 1; } this.m = nanoTime; this.l = 0L; int i3 = 0; while (true) { int i4 = this.w; if (i3 >= i4) { break; } this.l += this.b[i3] / i4; i3++; } } if (this.h) { return; } a(nanoTime, c); h(nanoTime); } private long c() { return a(b()); } private long b() { AudioTrack audioTrack = (AudioTrack) b1.a(this.c); if (this.x != C.TIME_UNSET) { return Math.min(this.A, this.z + ((((SystemClock.elapsedRealtime() * 1000) - this.x) * this.g) / 1000000)); } int playState = audioTrack.getPlayState(); if (playState == 1) { return 0L; } long playbackHeadPosition = audioTrack.getPlaybackHeadPosition() & 4294967295L; if (this.h) { if (playState == 2 && playbackHeadPosition == 0) { this.u = this.s; } playbackHeadPosition += this.u; } if (xp.a <= 29) { if (playbackHeadPosition == 0 && this.s > 0 && playState == 3) { if (this.y == C.TIME_UNSET) { this.y = SystemClock.elapsedRealtime(); } return this.s; } this.y = C.TIME_UNSET; } if (this.s > playbackHeadPosition) { this.t++; } this.s = playbackHeadPosition; return playbackHeadPosition + (this.t << 32); } private long a(long j) { return (j * 1000000) / this.g; } public long a(boolean z) { long c; if (((AudioTrack) b1.a(this.c)).getPlayState() == 3) { e(); } long nanoTime = System.nanoTime() / 1000; t1 t1Var = (t1) b1.a(this.f); boolean d = t1Var.d(); if (d) { c = a(t1Var.b()) + xp.a(nanoTime - t1Var.c(), this.j); } else { c = this.w == 0 ? c() : this.l + nanoTime; if (!z) { c = Math.max(0L, c - this.o); } } if (this.D != d) { this.F = this.C; this.E = this.B; } long j = nanoTime - this.F; if (j < 1000000) { long a2 = this.E + xp.a(j, this.j); long j2 = (j * 1000) / 1000000; c = ((c * j2) + ((1000 - j2) * a2)) / 1000; } if (!this.k) { long j3 = this.B; if (c > j3) { this.k = true; this.a.a(System.currentTimeMillis() - t2.b(xp.b(t2.b(c - j3), this.j))); } } this.C = nanoTime; this.B = c; this.D = d; return c; } private void a(long j, long j2) { t1 t1Var = (t1) b1.a(this.f); if (t1Var.a(j)) { long c = t1Var.c(); long b = t1Var.b(); if (Math.abs(c - j) > 5000000) { this.a.b(b, c, j, j2); t1Var.e(); } else if (Math.abs(a(b) - j2) > 5000000) { this.a.a(b, c, j, j2); t1Var.e(); } else { t1Var.a(); } } } public void a(AudioTrack audioTrack, boolean z, int i, int i2, int i3) { this.c = audioTrack; this.d = i2; this.e = i3; this.f = new t1(audioTrack); this.g = audioTrack.getSampleRate(); this.h = z && a(i); boolean g = xp.g(i); this.q = g; this.i = g ? a(i3 / i2) : -9223372036854775807L; this.s = 0L; this.t = 0L; this.u = 0L; this.p = false; this.x = C.TIME_UNSET; this.y = C.TIME_UNSET; this.r = 0L; this.o = 0L; this.j = 1.0f; } public void a(float f) { this.j = f; t1 t1Var = this.f; if (t1Var != null) { t1Var.f(); } } private static boolean a(int i) { return xp.a < 23 && (i == 5 || i == 6); } }