package D0;

import android.media.AudioTimestamp;
import android.media.AudioTrack;
import android.os.Handler;
import android.os.SystemClock;
import java.lang.reflect.Method;
import java.math.RoundingMode;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class u {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public long f1596A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public long f1597B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public long f1598C;
    public boolean D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public long f1599E;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public long f1600F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public boolean f1601G;
    public long H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public x0.q f1602I;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final K4.c f1603a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long[] f1604b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public AudioTrack f1605c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f1606d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public t f1607e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f1608f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f1609g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public long f1610h;
    public float i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f1611j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public long f1612k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public long f1613l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public Method f1614m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public long f1615n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public boolean f1616o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public boolean f1617p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public long f1618q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public long f1619r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public long f1620s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public long f1621t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public long f1622u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public int f1623v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public int f1624w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public long f1625x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public long f1626y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public long f1627z;

    public u(K4.c cVar) {
        this.f1603a = cVar;
        try {
            this.f1614m = AudioTrack.class.getMethod("getLatency", null);
        } catch (NoSuchMethodException unused) {
        }
        this.f1604b = new long[10];
        this.f1602I = x0.q.f18545a;
    }

    /* JADX WARN: Removed duplicated region for block: B:69:0x0143  */
    /* JADX WARN: Removed duplicated region for block: B:70:0x0148  */
    /* JADX WARN: Removed duplicated region for block: B:88:0x0208  */
    /* JADX WARN: Removed duplicated region for block: B:95:0x023a A[Catch: Exception -> 0x0250, TRY_LEAVE, TryCatch #0 {Exception -> 0x0250, blocks: (B:93:0x0215, B:95:0x023a), top: B:143:0x0215 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long a() {
        AudioTrack audioTrack;
        long j8;
        long j9;
        long jMax;
        m mVar;
        Handler handler;
        long j10;
        boolean timestamp;
        long j11;
        Method method;
        long jMax2;
        AudioTrack audioTrack2 = this.f1605c;
        audioTrack2.getClass();
        int playState = audioTrack2.getPlayState();
        F f3 = (F) this.f1603a.f3604r;
        if (playState == 3) {
            this.f1602I.getClass();
            long jNanoTime = System.nanoTime() / 1000;
            j9 = -9223372036854775807L;
            if (jNanoTime - this.f1613l >= 30000) {
                long jQ = x0.v.Q(b(), this.f1608f);
                if (jQ != 0) {
                    int i = this.f1623v;
                    j8 = 1000;
                    long jZ = x0.v.z(jQ, this.i) - jNanoTime;
                    long[] jArr = this.f1604b;
                    jArr[i] = jZ;
                    this.f1623v = (this.f1623v + 1) % 10;
                    int i5 = this.f1624w;
                    if (i5 < 10) {
                        this.f1624w = i5 + 1;
                    }
                    this.f1613l = jNanoTime;
                    this.f1612k = 0L;
                    int i8 = 0;
                    while (true) {
                        int i9 = this.f1624w;
                        if (i8 >= i9) {
                            break;
                        }
                        this.f1612k = (jArr[i8] / ((long) i9)) + this.f1612k;
                        i8++;
                    }
                } else {
                    audioTrack = audioTrack2;
                    j8 = 1000;
                }
            } else {
                j8 = 1000;
            }
            if (this.f1609g) {
                audioTrack = audioTrack2;
            } else {
                t tVar = this.f1607e;
                tVar.getClass();
                s sVar = tVar.f1590a;
                if (sVar != null) {
                    j10 = 500000;
                    if (jNanoTime - tVar.f1594e >= tVar.f1593d) {
                        tVar.f1594e = jNanoTime;
                        AudioTrack audioTrack3 = sVar.f1583a;
                        AudioTimestamp audioTimestamp = sVar.f1584b;
                        timestamp = audioTrack3.getTimestamp(audioTimestamp);
                        if (timestamp) {
                            long j12 = audioTimestamp.framePosition;
                            long j13 = sVar.f1586d;
                            if (j13 <= j12) {
                                audioTrack = audioTrack2;
                            } else if (sVar.f1588f) {
                                audioTrack = audioTrack2;
                                sVar.f1589g += j13;
                                sVar.f1588f = false;
                            } else {
                                audioTrack = audioTrack2;
                                sVar.f1585c++;
                            }
                            sVar.f1586d = j12;
                            sVar.f1587e = j12 + sVar.f1589g + (sVar.f1585c << 32);
                        } else {
                            audioTrack = audioTrack2;
                        }
                        int i10 = tVar.f1591b;
                        if (i10 != 0) {
                            if (i10 != 1) {
                                if (i10 != 2) {
                                    if (i10 != 3) {
                                        if (i10 != 4) {
                                            throw new IllegalStateException();
                                        }
                                    } else if (timestamp) {
                                        tVar.a();
                                    }
                                } else if (!timestamp) {
                                    tVar.a();
                                }
                            } else if (!timestamp) {
                                tVar.a();
                            } else if (sVar.f1587e > tVar.f1595f) {
                                tVar.b(2);
                            }
                        } else if (timestamp) {
                            if (audioTimestamp.nanoTime / j8 >= tVar.f1592c) {
                                tVar.f1595f = sVar.f1587e;
                                tVar.b(1);
                            }
                        } else if (jNanoTime - tVar.f1592c > 500000) {
                            tVar.b(3);
                        }
                        if (timestamp) {
                            long j14 = sVar != null ? sVar.f1584b.nanoTime / j8 : -9223372036854775807L;
                            long j15 = sVar != null ? sVar.f1587e : -1L;
                            j11 = 5000000;
                            long jQ2 = x0.v.Q(b(), this.f1608f);
                            if (Math.abs(j14 - jNanoTime) > 5000000) {
                                AbstractC1709a.B("DefaultAudioSink", "Spurious audio timestamp (system clock mismatch): " + j15 + ", " + j14 + ", " + jNanoTime + ", " + jQ2 + ", " + f3.j() + ", " + f3.k());
                                tVar.b(4);
                            } else if (Math.abs(x0.v.Q(j15, this.f1608f) - jQ2) > 5000000) {
                                AbstractC1709a.B("DefaultAudioSink", "Spurious audio timestamp (frame position mismatch): " + j15 + ", " + j14 + ", " + jNanoTime + ", " + jQ2 + ", " + f3.j() + ", " + f3.k());
                                tVar.b(4);
                            } else if (tVar.f1591b == 4) {
                                tVar.a();
                            }
                        } else {
                            j11 = 5000000;
                        }
                        if (this.f1617p && (method = this.f1614m) != null && jNanoTime - this.f1618q >= j10) {
                            try {
                                AudioTrack audioTrack4 = this.f1605c;
                                audioTrack4.getClass();
                                Integer num = (Integer) method.invoke(audioTrack4, null);
                                int i11 = x0.v.f18553a;
                                long jIntValue = (((long) num.intValue()) * j8) - this.f1610h;
                                this.f1615n = jIntValue;
                                jMax2 = Math.max(jIntValue, 0L);
                                this.f1615n = jMax2;
                                if (jMax2 > j11) {
                                    AbstractC1709a.B("DefaultAudioSink", "Ignoring impossibly large audio latency: " + jMax2);
                                    this.f1615n = 0L;
                                }
                            } catch (Exception unused) {
                                this.f1614m = null;
                            }
                            this.f1618q = jNanoTime;
                        }
                    }
                    timestamp = false;
                    if (timestamp) {
                    }
                    if (this.f1617p) {
                        AudioTrack audioTrack42 = this.f1605c;
                        audioTrack42.getClass();
                        Integer num2 = (Integer) method.invoke(audioTrack42, null);
                        int i112 = x0.v.f18553a;
                        long jIntValue2 = (((long) num2.intValue()) * j8) - this.f1610h;
                        this.f1615n = jIntValue2;
                        jMax2 = Math.max(jIntValue2, 0L);
                        this.f1615n = jMax2;
                        if (jMax2 > j11) {
                        }
                        this.f1618q = jNanoTime;
                    }
                } else {
                    j10 = 500000;
                }
                audioTrack = audioTrack2;
                timestamp = false;
                if (timestamp) {
                }
                if (this.f1617p) {
                }
            }
        } else {
            audioTrack = audioTrack2;
            j8 = 1000;
            j9 = -9223372036854775807L;
        }
        this.f1602I.getClass();
        long jNanoTime2 = System.nanoTime() / j8;
        t tVar2 = this.f1607e;
        tVar2.getClass();
        boolean z8 = tVar2.f1591b == 2;
        if (z8) {
            s sVar2 = tVar2.f1590a;
            jMax = x0.v.x(jNanoTime2 - (sVar2 != null ? sVar2.f1584b.nanoTime / j8 : j9), this.i) + x0.v.Q(sVar2 != null ? sVar2.f1587e : -1L, this.f1608f);
        } else {
            jMax = Math.max(0L, (this.f1624w == 0 ? this.f1625x != j9 ? x0.v.Q(c(), this.f1608f) : x0.v.Q(b(), this.f1608f) : x0.v.x(this.f1612k + jNanoTime2, this.i)) - this.f1615n);
            if (this.f1625x != j9) {
                jMax = Math.min(x0.v.Q(this.f1596A, this.f1608f), jMax);
            }
        }
        if (this.D != z8) {
            this.f1600F = this.f1598C;
            this.f1599E = this.f1597B;
        }
        long j16 = jNanoTime2 - this.f1600F;
        if (j16 < 1000000) {
            long jX = x0.v.x(j16, this.i) + this.f1599E;
            long j17 = (j16 * j8) / 1000000;
            jMax = (((j8 - j17) * jX) + (jMax * j17)) / j8;
        }
        if (!this.f1611j && jMax > this.f1597B && audioTrack.getPlayState() == 3) {
            this.f1611j = true;
            long jZ2 = x0.v.z(x0.v.W(jMax - this.f1597B), this.i);
            this.f1602I.getClass();
            long jCurrentTimeMillis = System.currentTimeMillis() - x0.v.W(jZ2);
            K4.c cVar = f3.f1486r;
            if (cVar != null && (handler = (mVar = ((H) cVar.f3604r).f1498T0).f1569b) != null) {
                handler.post(new RunnableC0074k(mVar, jCurrentTimeMillis));
            }
        }
        this.f1598C = jNanoTime2;
        this.f1597B = jMax;
        this.D = z8;
        return jMax;
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0079  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long b() {
        long j8;
        if (this.f1625x != -9223372036854775807L) {
            return Math.min(this.f1596A, c());
        }
        this.f1602I.getClass();
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        if (jElapsedRealtime - this.f1619r >= 5) {
            AudioTrack audioTrack = this.f1605c;
            audioTrack.getClass();
            int playState = audioTrack.getPlayState();
            if (playState != 1) {
                long playbackHeadPosition = ((long) audioTrack.getPlaybackHeadPosition()) & 4294967295L;
                if (this.f1609g) {
                    if (playState == 2 && playbackHeadPosition == 0) {
                        this.f1622u = this.f1620s;
                    }
                    playbackHeadPosition += this.f1622u;
                }
                if (x0.v.f18553a > 29) {
                    j8 = this.f1620s;
                    if (j8 > playbackHeadPosition) {
                        if (this.f1601G) {
                            this.H += j8;
                            this.f1601G = false;
                        } else {
                            this.f1621t++;
                        }
                    }
                    this.f1620s = playbackHeadPosition;
                } else if (playbackHeadPosition != 0 || this.f1620s <= 0 || playState != 3) {
                    this.f1626y = -9223372036854775807L;
                    j8 = this.f1620s;
                    if (j8 > playbackHeadPosition) {
                    }
                    this.f1620s = playbackHeadPosition;
                } else if (this.f1626y == -9223372036854775807L) {
                    this.f1626y = jElapsedRealtime;
                }
            }
            this.f1619r = jElapsedRealtime;
        }
        return this.f1620s + this.H + (this.f1621t << 32);
    }

    public final long c() {
        AudioTrack audioTrack = this.f1605c;
        audioTrack.getClass();
        if (audioTrack.getPlayState() == 2) {
            return this.f1627z;
        }
        this.f1602I.getClass();
        return this.f1627z + x0.v.S(x0.v.x(x0.v.L(SystemClock.elapsedRealtime()) - this.f1625x, this.i), this.f1608f, 1000000L, RoundingMode.UP);
    }

    public final boolean d(long j8) {
        long jA = a();
        int i = this.f1608f;
        int i5 = x0.v.f18553a;
        if (j8 > x0.v.S(jA, i, 1000000L, RoundingMode.UP)) {
            return true;
        }
        if (!this.f1609g) {
            return false;
        }
        AudioTrack audioTrack = this.f1605c;
        audioTrack.getClass();
        return audioTrack.getPlayState() == 2 && b() == 0;
    }

    public final void e() {
        this.f1612k = 0L;
        this.f1624w = 0;
        this.f1623v = 0;
        this.f1613l = 0L;
        this.f1598C = 0L;
        this.f1600F = 0L;
        this.f1611j = false;
    }
}
