package com.google.android.gms.internal.ads;

import android.media.AudioTimestamp;
import android.media.AudioTrack;
import android.os.Build;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor;
import androidx.media3.exoplayer.dash.DashMediaSource;
import java.lang.reflect.Method;
import java.math.RoundingMode;

/* JADX INFO: loaded from: classes2.dex */
public final class fm2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final sl2 f11288a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final sl0 f11289b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AudioTrack f11291d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final int f11292e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final long f11293f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ll2 f11295h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final float f11296i;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @Nullable
    private Method zzm;

    public fm2(sl2 sl2Var, sl0 sl0Var, AudioTrack audioTrack, int i10, int i11, int i12) {
        this.f11288a = sl2Var;
        this.f11289b = sl0Var;
        this.f11291d = audioTrack;
        try {
            this.zzm = AudioTrack.class.getMethod("getLatency", null);
        } catch (NoSuchMethodException unused) {
        }
        this.f11290c = new long[10];
        this.f11312y = -9223372036854775807L;
        this.f11311x = -9223372036854775807L;
        this.f11295h = new ll2(audioTrack, sl2Var);
        int sampleRate = audioTrack.getSampleRate();
        this.f11292e = sampleRate;
        boolean zA = bb1.a(i10);
        this.f11294g = zA;
        this.f11293f = zA ? bb1.q(sampleRate, i12 / i11) : -9223372036854775807L;
        this.f11303p = 0L;
        this.f11304q = 0L;
        this.f11307t = -9223372036854775807L;
        this.f11308u = -9223372036854775807L;
        this.f11301n = 0L;
        this.f11300m = 0L;
        this.f11296i = 1.0f;
        this.f11297j = -9223372036854775807L;
    }

    /* JADX WARN: Removed duplicated region for block: B:28:0x00c2 A[Catch: Exception -> 0x00e8, TRY_LEAVE, TryCatch #0 {Exception -> 0x00e8, blocks: (B:26:0x009d, B:28:0x00c2), top: B:120:0x009d }] */
    /* JADX WARN: Removed duplicated region for block: B:34:0x00ff  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x0108  */
    /* JADX WARN: Removed duplicated region for block: B:77:0x02c3  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long a() {
        AudioTrack audioTrack;
        float f10;
        ll2 ll2Var;
        sl0 sl0Var;
        boolean z10;
        fm2 fm2Var;
        float f11;
        long jC;
        int i10;
        sl0 sl0Var2;
        int i11;
        AudioTimestamp audioTimestamp;
        boolean z11;
        long j10;
        boolean z12;
        Method method;
        long jMax;
        AudioTrack audioTrack2 = this.f11291d;
        audioTrack2.getClass();
        int playState = audioTrack2.getPlayState();
        float f12 = this.f11296i;
        ll2 ll2Var2 = this.f11295h;
        sl0 sl0Var3 = this.f11289b;
        if (playState == 3) {
            ((h71) sl0Var3).getClass();
            long jNanoTime = System.nanoTime() / 1000;
            if (jNanoTime - this.f11299l >= DashMediaSource.DEFAULT_FALLBACK_TARGET_LIVE_OFFSET_MS) {
                long jQ = bb1.q(this.f11292e, d());
                if (jQ == 0) {
                    audioTrack = audioTrack2;
                    f10 = f12;
                    sl0Var = sl0Var3;
                    z10 = false;
                    ll2Var = ll2Var2;
                } else {
                    int i12 = this.f11305r;
                    if (f12 != 1.0f) {
                        jQ = Math.round(jQ / ((double) f12));
                    }
                    long[] jArr = this.f11290c;
                    jArr[i12] = jQ - jNanoTime;
                    this.f11305r = (this.f11305r + 1) % 10;
                    int i13 = this.f11306s;
                    if (i13 < 10) {
                        this.f11306s = i13 + 1;
                    }
                    this.f11299l = jNanoTime;
                    this.f11298k = 0L;
                    int i14 = 0;
                    while (true) {
                        int i15 = this.f11306s;
                        if (i14 >= i15) {
                            break;
                        }
                        this.f11298k = (jArr[i14] / ((long) i15)) + this.f11298k;
                        i14++;
                        jArr = jArr;
                    }
                    if (this.f11294g && (method = this.zzm) != null && jNanoTime - this.f11301n >= 500000) {
                        try {
                            Integer num = (Integer) method.invoke(audioTrack2, new Object[0]);
                            String str = bb1.f9474a;
                            long jIntValue = (((long) num.intValue()) * 1000) - this.f11293f;
                            this.f11300m = jIntValue;
                            jMax = Math.max(jIntValue, 0L);
                            this.f11300m = jMax;
                            if (jMax > DashMediaSource.MIN_LIVE_DEFAULT_START_POSITION_US) {
                                this.f11288a.getClass();
                                StringBuilder sb = new StringBuilder(String.valueOf(jMax).length() + 41);
                                sb.append("Ignoring impossibly large audio latency: ");
                                sb.append(jMax);
                                vt0.c(sb.toString());
                                this.f11300m = 0L;
                            }
                        } catch (Exception unused) {
                            this.zzm = null;
                        }
                        this.f11301n = jNanoTime;
                    }
                    float f13 = this.f11296i;
                    long jC2 = c(jNanoTime);
                    ll2Var = ll2Var2;
                    if (jNanoTime - ll2Var.f13758g >= ll2Var.f13757f) {
                        audioTrack = audioTrack2;
                        f10 = f12;
                        sl0Var = sl0Var3;
                    } else {
                        ll2Var.f13758g = jNanoTime;
                        kl2 kl2Var = ll2Var.f13752a;
                        AudioTrack audioTrack3 = kl2Var.f13306a;
                        AudioTimestamp audioTimestamp2 = kl2Var.f13307b;
                        boolean timestamp = audioTrack3.getTimestamp(audioTimestamp2);
                        if (timestamp) {
                            sl0Var2 = sl0Var3;
                            long j11 = audioTimestamp2.framePosition;
                            audioTrack = audioTrack2;
                            if (kl2Var.f13309d > j11) {
                                kl2Var.f13308c++;
                            }
                            kl2Var.f13309d = j11;
                            kl2Var.f13310e = j11 + (kl2Var.f13308c << 32);
                        } else {
                            audioTrack = audioTrack2;
                            sl0Var2 = sl0Var3;
                        }
                        int i16 = ll2Var.f13753b;
                        if (timestamp) {
                            long j12 = audioTimestamp2.nanoTime / 1000;
                            f10 = f12;
                            audioTimestamp = audioTimestamp2;
                            z11 = timestamp;
                            long jT = bb1.t(jNanoTime - (kl2Var.f13307b.nanoTime / 1000), f13) + bb1.q(i16, kl2Var.f13310e);
                            long jAbs = Math.abs(j12 - jNanoTime);
                            sl2 sl2Var = ll2Var.f13754c;
                            sl0Var = sl0Var2;
                            if (jAbs > DashMediaSource.MIN_LIVE_DEFAULT_START_POSITION_US) {
                                long j13 = kl2Var.f13310e;
                                long jE = sl2Var.f16475a.e();
                                StringBuilder sb2 = new StringBuilder(String.valueOf(j13).length() + 52 + String.valueOf(j12).length() + 2 + String.valueOf(jNanoTime).length() + 2 + String.valueOf(jC2).length() + 2 + String.valueOf(jE).length());
                                defpackage.e.B(sb2, "Spurious audio timestamp (system clock mismatch): ", j13, ", ");
                                sb2.append(j12);
                                defpackage.e.B(sb2, ", ", jNanoTime, ", ");
                                sb2.append(jC2);
                                sb2.append(", ");
                                sb2.append(jE);
                                vt0.c(sb2.toString());
                                ll2Var.a(4);
                                i11 = i16;
                                j10 = jNanoTime;
                            } else {
                                j10 = jNanoTime;
                                if (Math.abs(jT - jC2) > DashMediaSource.MIN_LIVE_DEFAULT_START_POSITION_US) {
                                    long j14 = kl2Var.f13310e;
                                    i11 = i16;
                                    long jE2 = sl2Var.f16475a.e();
                                    StringBuilder sb3 = new StringBuilder(String.valueOf(j14).length() + 54 + String.valueOf(j12).length() + 2 + String.valueOf(j10).length() + 2 + String.valueOf(jC2).length() + 2 + String.valueOf(jE2).length());
                                    defpackage.e.B(sb3, "Spurious audio timestamp (frame position mismatch): ", j14, ", ");
                                    sb3.append(j12);
                                    defpackage.e.B(sb3, ", ", j10, ", ");
                                    sb3.append(jC2);
                                    sb3.append(", ");
                                    sb3.append(jE2);
                                    vt0.c(sb3.toString());
                                    ll2Var.a(4);
                                } else {
                                    i11 = i16;
                                    if (ll2Var.f13755d == 4) {
                                        ll2Var.a(0);
                                    }
                                }
                            }
                        } else {
                            i11 = i16;
                            f10 = f12;
                            audioTimestamp = audioTimestamp2;
                            z11 = timestamp;
                            sl0Var = sl0Var2;
                            j10 = jNanoTime;
                        }
                        int i17 = ll2Var.f13755d;
                        if (i17 == 0) {
                            AudioTimestamp audioTimestamp3 = audioTimestamp;
                            z10 = false;
                            if (z11) {
                                long j15 = audioTimestamp3.nanoTime;
                                if (j15 / 1000 >= ll2Var.f13756e) {
                                    ll2Var.f13759h = kl2Var.f13310e;
                                    ll2Var.f13760i = j15 / 1000;
                                    ll2Var.a(1);
                                }
                            } else if (j10 - ll2Var.f13756e > 500000) {
                                ll2Var.a(3);
                            }
                        } else if (i17 != 1) {
                            if (i17 == 2) {
                                z12 = false;
                                if (!z11) {
                                    ll2Var.a(0);
                                }
                            } else if (i17 == 3 && z11) {
                                z12 = false;
                                ll2Var.a(0);
                            }
                            z10 = z12;
                        } else if (z11) {
                            long j16 = kl2Var.f13310e;
                            long j17 = ll2Var.f13759h;
                            if (j16 > j17) {
                                int i18 = i11;
                                if (Math.abs((bb1.t(j10 - (kl2Var.f13307b.nanoTime / 1000), f13) + bb1.q(i18, kl2Var.f13310e)) - (bb1.t(j10 - ll2Var.f13760i, f13) + bb1.q(i18, j17))) < 1000) {
                                    ll2Var.a(2);
                                }
                            } else if (j10 - ll2Var.f13756e > SilenceSkippingAudioProcessor.DEFAULT_MAX_SILENCE_TO_KEEP_DURATION_US) {
                                ll2Var.a(3);
                            } else {
                                ll2Var.f13759h = kl2Var.f13310e;
                                ll2Var.f13760i = audioTimestamp.nanoTime / 1000;
                            }
                        } else {
                            z10 = false;
                            ll2Var.a(0);
                        }
                    }
                    z10 = false;
                }
            } else {
                if (this.f11294g) {
                    Integer num2 = (Integer) method.invoke(audioTrack2, new Object[0]);
                    String str2 = bb1.f9474a;
                    long jIntValue2 = (((long) num2.intValue()) * 1000) - this.f11293f;
                    this.f11300m = jIntValue2;
                    jMax = Math.max(jIntValue2, 0L);
                    this.f11300m = jMax;
                    if (jMax > DashMediaSource.MIN_LIVE_DEFAULT_START_POSITION_US) {
                    }
                    this.f11301n = jNanoTime;
                }
                float f132 = this.f11296i;
                long jC22 = c(jNanoTime);
                ll2Var = ll2Var2;
                if (jNanoTime - ll2Var.f13758g >= ll2Var.f13757f) {
                }
                z10 = false;
            }
        } else {
            audioTrack = audioTrack2;
            f10 = f12;
            ll2Var = ll2Var2;
            sl0Var = sl0Var3;
            z10 = false;
        }
        ((h71) sl0Var).getClass();
        long jNanoTime2 = System.nanoTime() / 1000;
        boolean z13 = ll2Var.f13755d == 2 ? true : z10;
        if (z13) {
            kl2 kl2Var2 = ll2Var.f13752a;
            f11 = f10;
            jC = bb1.t(jNanoTime2 - (kl2Var2.f13307b.nanoTime / 1000), f11) + bb1.q(ll2Var.f13753b, kl2Var2.f13310e);
            fm2Var = this;
        } else {
            fm2Var = this;
            f11 = f10;
            jC = fm2Var.c(jNanoTime2);
        }
        int playState2 = audioTrack.getPlayState();
        if (playState2 == 3) {
            if (z13 || ((i10 = ll2Var.f13755d) != 0 && i10 != 1)) {
                fm2Var.b(jC);
            }
            long j18 = fm2Var.f11312y;
            if (j18 != -9223372036854775807L) {
                long j19 = jC - fm2Var.f11311x;
                long jT2 = bb1.t(jNanoTime2 - j18, f11);
                long j20 = fm2Var.f11311x + jT2;
                long jAbs2 = Math.abs(j20 - jC);
                if (j19 != 0 && jAbs2 < 1000000) {
                    long j21 = (jT2 * 10) / 100;
                    jC = Math.max(j20 - j21, Math.min(jC, j20 + j21));
                }
            }
            fm2Var.f11312y = jNanoTime2;
            fm2Var.f11311x = jC;
        } else if (playState2 == 1) {
            fm2Var.b(jC);
            return jC;
        }
        return jC;
    }

    public final void b(long j10) {
        long j11 = this.f11297j;
        if (j11 == -9223372036854775807L || j10 < j11) {
            return;
        }
        float f10 = this.f11296i;
        long jRound = j10 - j11;
        String str = bb1.f9474a;
        if (f10 != 1.0f) {
            jRound = Math.round(jRound / ((double) f10));
        }
        long jO = bb1.o(jRound);
        ((h71) this.f11289b).getClass();
        long jCurrentTimeMillis = System.currentTimeMillis() - jO;
        this.f11297j = -9223372036854775807L;
        sl2 sl2Var = this.f11288a;
        sl2Var.getClass();
        androidx.media3.extractor.f0 f0Var = new androidx.media3.extractor.f0();
        f0Var.f3366a = jCurrentTimeMillis;
        lt0 lt0Var = sl2Var.f16475a.f19307f;
        lt0Var.c(-1, f0Var);
        lt0Var.d();
    }

    public final long c(long j10) {
        int i10 = this.f11306s;
        int i11 = this.f11292e;
        long jMax = Math.max(0L, (i10 == 0 ? this.f11307t != -9223372036854775807L ? bb1.q(i11, e()) : bb1.q(i11, d()) : bb1.t(j10 + this.f11298k, this.f11296i)) - this.f11300m);
        return this.f11307t != -9223372036854775807L ? Math.min(bb1.q(i11, this.f11310w), jMax) : jMax;
    }

    /* JADX WARN: Removed duplicated region for block: B:25:0x0069  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long d() {
        if (this.f11307t != -9223372036854775807L) {
            return Math.min(this.f11310w, e());
        }
        ((h71) this.f11289b).getClass();
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        if (jElapsedRealtime - this.f11302o >= 5) {
            AudioTrack audioTrack = this.f11291d;
            audioTrack.getClass();
            int playState = audioTrack.getPlayState();
            if (playState != 1) {
                long playbackHeadPosition = ((long) audioTrack.getPlaybackHeadPosition()) & 4294967295L;
                if (Build.VERSION.SDK_INT > 29) {
                    if (this.f11303p > playbackHeadPosition) {
                        this.f11304q++;
                    }
                    this.f11303p = playbackHeadPosition;
                } else if (playbackHeadPosition != 0 || this.f11303p <= 0 || playState != 3) {
                    this.f11308u = -9223372036854775807L;
                    if (this.f11303p > playbackHeadPosition) {
                    }
                    this.f11303p = playbackHeadPosition;
                } else if (this.f11308u == -9223372036854775807L) {
                    this.f11308u = jElapsedRealtime;
                }
            }
            this.f11302o = jElapsedRealtime;
        }
        return this.f11303p + (this.f11304q << 32);
    }

    public final long e() {
        AudioTrack audioTrack = this.f11291d;
        audioTrack.getClass();
        if (audioTrack.getPlayState() == 2) {
            return this.f11309v;
        }
        ((h71) this.f11289b).getClass();
        return this.f11309v + bb1.r(bb1.t(bb1.p(SystemClock.elapsedRealtime()) - this.f11307t, this.f11296i), this.f11292e, 1000000L, RoundingMode.UP);
    }
}
