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

import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.metrics.LogSessionId;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.PlaybackException;
import androidx.media3.exoplayer.RendererCapabilities;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes2.dex */
public final class qm2 implements jl2 {
    public static final AtomicInteger N = new AtomicInteger();
    public boolean A;
    public boolean B;
    public boolean C;
    public boolean D;
    public int E;
    public boolean F;
    public bs0 G;
    public int H;
    public long I;
    public long J;
    public long K;
    public Handler L;
    public final mm2 M;

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ub0 f15793c;

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

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

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public lm2 f15799i;

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final cl2 f15801k;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @Nullable
    private ByteBuffer zzH;

    @Nullable
    private ByteBuffer zzJ;

    @Nullable
    private AudioDeviceInfo zzR;

    @Nullable
    private final Context zzb;

    @Nullable
    private im2 zzi;

    @Nullable
    private mk2 zzl;

    @Nullable
    private il2 zzm;

    @Nullable
    private lm2 zzn;

    @Nullable
    private vk2 zzs;

    @Nullable
    private om2 zzu;

    public qm2(km2 km2Var) {
        int deviceId;
        this.zzb = km2Var.b() == null ? null : km2Var.b().getApplicationContext();
        this.f15803m = ud0.f17210a;
        this.M = km2Var.c();
        this.f15801k = km2Var.f13320d;
        gm2 gm2Var = new gm2();
        this.f15791a = gm2Var;
        xm2 xm2Var = new xm2();
        xm2Var.f18480m = bb1.f9475b;
        this.f15792b = xm2Var;
        this.f15793c = new ub0();
        this.f15794d = new wm2();
        this.f15795e = zzguf.p(xm2Var, gm2Var);
        this.f15815y = 1.0f;
        this.E = 0;
        this.G = new bs0();
        wd wdVar = wd.f18030d;
        this.f15804n = new om2(wdVar, 0L, 0L);
        this.f15805o = wdVar;
        this.f15806p = false;
        this.f15796f = new ArrayDeque();
        this.f15797g = new pm2();
        this.f15798h = new pm2();
        int i10 = -1;
        if (Build.VERSION.SDK_INT >= 34 && km2Var.b() != null && (deviceId = km2Var.b().getDeviceId()) != 0 && deviceId != -1) {
            i10 = deviceId;
        }
        this.H = i10;
    }

    /* JADX WARN: Removed duplicated region for block: B:60:0x00f4  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static int c(int i10, ByteBuffer byteBuffer) {
        int i11;
        int i12;
        int i13;
        byte b10;
        int i14;
        int i15;
        int i16;
        int i17;
        int i18;
        if (i10 == 20) {
            if ((byteBuffer.get(5) & 2) == 0) {
                i11 = 0;
            } else {
                byte b11 = byteBuffer.get(26);
                int i19 = 28;
                int i20 = 28;
                for (int i21 = 0; i21 < b11; i21++) {
                    i20 += byteBuffer.get(i21 + 27);
                }
                byte b12 = byteBuffer.get(i20 + 26);
                for (int i22 = 0; i22 < b12; i22++) {
                    i19 += byteBuffer.get(i20 + 27 + i22);
                }
                i11 = i20 + i19;
            }
            int i23 = byteBuffer.get(i11 + 26) + 27 + i11;
            return (int) ((za2.P(byteBuffer.get(i23), byteBuffer.limit() - i23 > 1 ? byteBuffer.get(i23 + 1) : (byte) 0) * 48000) / 1000000);
        }
        if (i10 != 30) {
            switch (i10) {
                case 5:
                case 6:
                    break;
                case 7:
                case 8:
                    break;
                case 9:
                    int iPosition = byteBuffer.position();
                    String str = bb1.f9474a;
                    int iReverseBytes = byteBuffer.getInt(iPosition);
                    if (byteBuffer.order() != ByteOrder.BIG_ENDIAN) {
                        iReverseBytes = Integer.reverseBytes(iReverseBytes);
                    }
                    if ((iReverseBytes & (-2097152)) != -2097152 || (i16 = (iReverseBytes >>> 19) & 3) == 1 || (i17 = (iReverseBytes >>> 17) & 3) == 0) {
                        i15 = -1;
                    } else {
                        int i24 = iReverseBytes >>> 12;
                        int i25 = (iReverseBytes >>> 10) & 3;
                        int i26 = i24 & 15;
                        if (i26 != 0 && i26 != 15 && i25 != 3) {
                            i15 = 1152;
                            if (i17 != 1) {
                                if (i17 != 2) {
                                    i15 = RendererCapabilities.DECODER_SUPPORT_MASK;
                                }
                            } else if (i16 != 3) {
                                i15 = 576;
                            }
                        }
                    }
                    if (i15 != -1) {
                        return i15;
                    }
                    throw new IllegalArgumentException();
                case 10:
                    return 1024;
                case 11:
                case 12:
                    return 2048;
                default:
                    switch (i10) {
                        case 14:
                            int iPosition2 = byteBuffer.position();
                            int iLimit = byteBuffer.limit() - 10;
                            int i27 = iPosition2;
                            while (true) {
                                if (i27 <= iLimit) {
                                    String str2 = bb1.f9474a;
                                    int iReverseBytes2 = byteBuffer.getInt(i27 + 4);
                                    if (byteBuffer.order() != ByteOrder.BIG_ENDIAN) {
                                        iReverseBytes2 = Integer.reverseBytes(iReverseBytes2);
                                    }
                                    if ((iReverseBytes2 & (-2)) == -126718022) {
                                        i18 = i27 - iPosition2;
                                    } else {
                                        i27++;
                                    }
                                } else {
                                    i18 = -1;
                                }
                            }
                            if (i18 != -1) {
                                return (40 << ((byteBuffer.get((byteBuffer.position() + i18) + ((byteBuffer.get((byteBuffer.position() + i18) + 7) & 255) == 187 ? 9 : 8)) >> 4) & 7)) * 16;
                            }
                            return 0;
                        case 15:
                            return 512;
                        case 16:
                            return 1024;
                        case 17:
                            byte[] bArr = new byte[16];
                            int iPosition3 = byteBuffer.position();
                            byteBuffer.get(bArr);
                            byteBuffer.position(iPosition3);
                            return y1.a(new androidx.media3.extractor.b1(bArr, 16)).f15560c;
                        case 18:
                            break;
                        default:
                            throw new IllegalStateException(defpackage.e.n(new StringBuilder(String.valueOf(i10).length() + 27), "Unexpected audio encoding: ", i10));
                    }
                    break;
            }
            if (((byteBuffer.get(byteBuffer.position() + 5) & 248) >> 3) > 10) {
                return x1.f18285a[((byteBuffer.get(byteBuffer.position() + 4) & 192) >> 6) != 3 ? (byteBuffer.get(byteBuffer.position() + 4) & 48) >> 4 : 3] * 256;
            }
            return 1536;
        }
        if (byteBuffer.getInt(0) == -233094848 || byteBuffer.getInt(0) == -398277519) {
            return 1024;
        }
        if (byteBuffer.getInt(0) == 622876772) {
            return 4096;
        }
        int iPosition4 = byteBuffer.position();
        byte b13 = byteBuffer.get(iPosition4);
        if (b13 != -2) {
            if (b13 == -1) {
                i13 = (byteBuffer.get(iPosition4 + 4) & 7) << 4;
                b10 = byteBuffer.get(iPosition4 + 7);
            } else if (b13 != 31) {
                i13 = (byteBuffer.get(iPosition4 + 4) & 1) << 6;
                i14 = byteBuffer.get(iPosition4 + 5) & 252;
                i12 = (i14 >> 2) | i13;
            } else {
                i13 = (byteBuffer.get(iPosition4 + 5) & 7) << 4;
                b10 = byteBuffer.get(iPosition4 + 6);
            }
            i14 = b10 & 60;
            i12 = (i14 >> 2) | i13;
        } else {
            i12 = ((byteBuffer.get(iPosition4 + 5) & 1) << 6) | ((byteBuffer.get(iPosition4 + 4) & 252) >> 2);
        }
        return (i12 + 1) * 32;
    }

    public final void A(bs0 bs0Var) {
        if (this.G.equals(bs0Var)) {
            return;
        }
        if (this.zzs != null) {
            this.G.getClass();
        }
        this.G = bs0Var;
    }

    public final long B() {
        if (!s()) {
            return -9223372036854775807L;
        }
        if (this.f15799i.a()) {
            return bb1.q(this.f15799i.f13793e.f9570b, ((zl2) this.zzs).f19302a.getBufferSizeInFrames());
        }
        long bufferSizeInFrames = ((zl2) this.zzs).f19302a.getBufferSizeInFrames();
        int iA = r2.a(this.f15799i.f13793e.f9569a);
        ih1.b1(iA != -2147483647);
        return bb1.r(bufferSizeInFrames, 1000000L, iA, RoundingMode.DOWN);
    }

    public final void C() {
        vk2 vk2Var = this.zzs;
        if (vk2Var != null) {
            ((zl2) vk2Var).c();
        }
    }

    public final void D() {
        this.D = false;
        if (s()) {
            zl2 zl2Var = (zl2) this.zzs;
            fm2 fm2Var = zl2Var.f19304c;
            fm2Var.f11298k = 0L;
            fm2Var.f11306s = 0;
            fm2Var.f11305r = 0;
            fm2Var.f11299l = 0L;
            fm2Var.f11311x = -9223372036854775807L;
            fm2Var.f11312y = -9223372036854775807L;
            if (fm2Var.f11307t == -9223372036854775807L) {
                fm2Var.f11295h.a(0);
            }
            fm2Var.f11309v = fm2Var.d();
            if (!zl2Var.f19308g || zl2Var.c()) {
                zl2Var.f19302a.pause();
            }
        }
    }

    public final void a() {
        if (s()) {
            this.f15807q = 0L;
            this.f15808r = 0L;
            this.f15809s = 0L;
            this.f15810t = 0L;
            this.f15811u = 0;
            this.f15804n = new om2(this.f15805o, 0L, 0L);
            this.f15814x = 0L;
            this.zzu = null;
            this.f15796f.clear();
            this.zzH = null;
            this.f15816z = 0;
            this.zzJ = null;
            this.B = false;
            this.A = false;
            this.C = false;
            this.f15792b.f18482o = 0L;
            g60 g60Var = this.f15799i.f13794f;
            this.f15800j = g60Var;
            g60Var.b(g70.f11543b);
            this.zzi = null;
            lm2 lm2Var = this.zzn;
            if (lm2Var != null) {
                this.f15799i = lm2Var;
                this.zzn = null;
            }
            N.incrementAndGet();
            ((zl2) this.zzs).b();
            this.zzs = null;
        }
        this.f15798h.c();
        this.f15797g.c();
        this.J = 0L;
        this.K = 0L;
        Handler handler = this.L;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }

    public final void b() {
        a();
        zzgwa zzgwaVar = (zzgwa) this.f15795e;
        int i10 = zzgwaVar.f19788d;
        for (int i11 = 0; i11 < i10; i11++) {
            ((i80) zzgwaVar.get(i11)).zzj();
        }
        this.f15793c.zzj();
        this.f15794d.zzj();
        g60 g60Var = this.f15800j;
        if (g60Var != null) {
            g60Var.e();
        }
        this.D = false;
    }

    public final /* synthetic */ void d() {
        if (this.K >= 300000) {
            ((rm2) this.zzm).f16167a.a1 = true;
            this.K = 0L;
        }
    }

    public final /* synthetic */ void e() {
        il2 il2Var = this.zzm;
        if (il2Var != null) {
            ((rm2) il2Var).f16167a.s();
        }
    }

    public final /* synthetic */ im2 f() {
        return this.zzi;
    }

    public final /* synthetic */ il2 g() {
        return this.zzm;
    }

    public final /* synthetic */ vk2 h() {
        return this.zzs;
    }

    public final zl2 i(bl2 bl2Var) throws zzrf {
        try {
            return ((em2) this.f15801k).c(bl2Var);
        } catch (zzqi e10) {
            zzrf zzrfVar = new zzrf(0, bl2Var.f9570b, bl2Var.f9571c, bl2Var.f9569a, bl2Var.f9572d, this.f15799i.f13789a, false, e10);
            il2 il2Var = this.zzm;
            if (il2Var == null) {
                throw zzrfVar;
            }
            vt0.zzf("MediaCodecAudioRenderer", "Audio sink error", zzrfVar);
            ((rm2) il2Var).f16167a.Q0.j(zzrfVar);
            throw zzrfVar;
        }
    }

    public final void j(long j10) throws zzri {
        ByteBuffer byteBuffer;
        m(j10);
        if (this.zzJ != null) {
            return;
        }
        if (!this.f15800j.c()) {
            ByteBuffer byteBuffer2 = this.zzH;
            if (byteBuffer2 != null) {
                l(byteBuffer2);
                m(j10);
                return;
            }
            return;
        }
        while (!this.f15800j.d()) {
            do {
                g60 g60Var = this.f15800j;
                if (g60Var.c()) {
                    ByteBuffer byteBuffer3 = g60Var.f11537c[g60Var.g()];
                    if (byteBuffer3.hasRemaining()) {
                        byteBuffer = byteBuffer3;
                    } else {
                        g60Var.f(i80.f12407a);
                        byteBuffer = g60Var.f11537c[g60Var.g()];
                    }
                } else {
                    byteBuffer = i80.f12407a;
                }
                if (byteBuffer.hasRemaining()) {
                    l(byteBuffer);
                    m(j10);
                } else {
                    ByteBuffer byteBuffer4 = this.zzH;
                    if (byteBuffer4 == null || !byteBuffer4.hasRemaining()) {
                        return;
                    }
                    g60 g60Var2 = this.f15800j;
                    ByteBuffer byteBuffer5 = this.zzH;
                    if (g60Var2.c() && !g60Var2.f11538d) {
                        g60Var2.f(byteBuffer5);
                    }
                }
            } while (this.zzJ == null);
            return;
        }
    }

    public final boolean k() throws zzri {
        if (!this.f15800j.c()) {
            m(Long.MIN_VALUE);
            return this.zzJ == null;
        }
        g60 g60Var = this.f15800j;
        if (g60Var.c() && !g60Var.f11538d) {
            g60Var.f11538d = true;
            ((i80) g60Var.f11536b.get(0)).zze();
        }
        j(Long.MIN_VALUE);
        if (!this.f15800j.d()) {
            return false;
        }
        ByteBuffer byteBuffer = this.zzJ;
        return byteBuffer == null || !byteBuffer.hasRemaining();
    }

    /* JADX WARN: Removed duplicated region for block: B:45:0x0149  */
    /* JADX WARN: Removed duplicated region for block: B:68:0x01e3  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x020a  */
    /* JADX WARN: Removed duplicated region for block: B:80:0x01f8 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:82:0x0055 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void l(ByteBuffer byteBuffer) {
        ByteBuffer byteBuffer2;
        int i10;
        int i11;
        int i12;
        float f10;
        int i13;
        int i14;
        int i15;
        int i16;
        int i17;
        int i18;
        int i19;
        ih1.b1(this.zzJ == null);
        if (byteBuffer.hasRemaining()) {
            if (this.f15799i.a()) {
                int iR = (int) bb1.r(bb1.p(20L), this.f15799i.f13793e.f9570b, 1000000L, RoundingMode.UP);
                long jT = t();
                long j10 = iR;
                if (jT < j10) {
                    lm2 lm2Var = this.f15799i;
                    int i20 = lm2Var.f13793e.f9569a;
                    ByteBuffer byteBufferOrder = ByteBuffer.allocateDirect(byteBuffer.remaining()).order(ByteOrder.nativeOrder());
                    int iPosition = byteBuffer.position();
                    int i21 = (int) jT;
                    while (byteBuffer.hasRemaining() && i21 < iR) {
                        if (i20 != 2) {
                            if (i20 == 3) {
                                i12 = (byteBuffer.get() & 255) << 24;
                            } else if (i20 != 4) {
                                if (i20 != 21) {
                                    if (i20 == 22) {
                                        i16 = byteBuffer.get() & 255;
                                        i17 = (byteBuffer.get() & 255) << 8;
                                        i18 = (byteBuffer.get() & 255) << 16;
                                        i19 = (byteBuffer.get() & 255) << 24;
                                    } else if (i20 == 268435456) {
                                        i10 = (byteBuffer.get() & 255) << 24;
                                        i11 = (byteBuffer.get() & 255) << 16;
                                    } else if (i20 == 1342177280) {
                                        i13 = (byteBuffer.get() & 255) << 24;
                                        i14 = (byteBuffer.get() & 255) << 16;
                                        i15 = (byteBuffer.get() & 255) << 8;
                                    } else {
                                        if (i20 != 1610612736) {
                                            throw new IllegalStateException();
                                        }
                                        i16 = (byteBuffer.get() & 255) << 24;
                                        i17 = (byteBuffer.get() & 255) << 16;
                                        i18 = (byteBuffer.get() & 255) << 8;
                                        i19 = byteBuffer.get() & 255;
                                    }
                                    i12 = i16 | i17 | i18 | i19;
                                } else {
                                    i13 = (byteBuffer.get() & 255) << 8;
                                    i14 = (byteBuffer.get() & 255) << 16;
                                    i15 = (byteBuffer.get() & 255) << 24;
                                }
                                i12 = i13 | i14 | i15;
                            } else {
                                float fMax = Math.max(-1.0f, Math.min(byteBuffer.getFloat(), 1.0f));
                                if (fMax < 0.0f) {
                                    fMax = -fMax;
                                    f10 = -2.1474836E9f;
                                } else {
                                    f10 = 2.1474836E9f;
                                }
                                i12 = (int) (fMax * f10);
                            }
                            int i22 = (int) ((((long) i12) * ((long) i21)) / j10);
                            if (i20 != 2) {
                                byteBufferOrder.put((byte) (i22 >> 16));
                                byteBufferOrder.put((byte) (i22 >> 24));
                            } else if (i20 == 3) {
                                byteBufferOrder.put((byte) (i22 >> 24));
                            } else if (i20 != 4) {
                                if (i20 == 21) {
                                    byteBufferOrder.put((byte) (i22 >> 8));
                                    byteBufferOrder.put((byte) (i22 >> 16));
                                    byteBufferOrder.put((byte) (i22 >> 24));
                                } else if (i20 == 22) {
                                    byteBufferOrder.put((byte) i22);
                                    byteBufferOrder.put((byte) (i22 >> 8));
                                    byteBufferOrder.put((byte) (i22 >> 16));
                                    byteBufferOrder.put((byte) (i22 >> 24));
                                } else if (i20 == 268435456) {
                                    byteBufferOrder.put((byte) (i22 >> 24));
                                    byteBufferOrder.put((byte) (i22 >> 16));
                                } else if (i20 == 1342177280) {
                                    byteBufferOrder.put((byte) (i22 >> 24));
                                    byteBufferOrder.put((byte) (i22 >> 16));
                                    byteBufferOrder.put((byte) (i22 >> 8));
                                } else {
                                    if (i20 != 1610612736) {
                                        throw new IllegalStateException();
                                    }
                                    byteBufferOrder.put((byte) (i22 >> 24));
                                    byteBufferOrder.put((byte) (i22 >> 16));
                                    byteBufferOrder.put((byte) (i22 >> 8));
                                    byteBufferOrder.put((byte) i22);
                                }
                            } else if (i22 < 0) {
                                byteBufferOrder.putFloat((-i22) / (-2.1474836E9f));
                            } else {
                                byteBufferOrder.putFloat(i22 / 2.1474836E9f);
                            }
                            if (byteBuffer.position() != lm2Var.f13792d + iPosition) {
                                i21++;
                                iPosition = byteBuffer.position();
                            }
                        } else {
                            i10 = (byteBuffer.get() & 255) << 16;
                            i11 = (byteBuffer.get() & 255) << 24;
                        }
                        i12 = i10 | i11;
                        int i222 = (int) ((((long) i12) * ((long) i21)) / j10);
                        if (i20 != 2) {
                        }
                        if (byteBuffer.position() != lm2Var.f13792d + iPosition) {
                        }
                    }
                    byteBufferOrder.put(byteBuffer);
                    byteBufferOrder.flip();
                    byteBuffer2 = byteBufferOrder;
                } else {
                    byteBuffer2 = byteBuffer;
                }
            }
            this.zzJ = byteBuffer2;
        }
    }

    public final void m(long j10) throws zzri {
        il2 il2Var;
        if (this.zzJ == null) {
            return;
        }
        pm2 pm2Var = this.f15798h;
        if (pm2Var.b()) {
            return;
        }
        int iRemaining = this.zzJ.remaining();
        try {
            boolean zA = ((zl2) this.zzs).a(this.f15816z, this.zzJ);
            this.I = SystemClock.elapsedRealtime();
            pm2Var.c();
            if (((zl2) this.zzs).c() && this.D && (il2Var = this.zzm) != null && !zA) {
            }
            if (this.f15799i.a()) {
                this.f15809s += (long) (iRemaining - this.zzJ.remaining());
            }
            if (zA) {
                if (!this.f15799i.a()) {
                    ih1.b1(this.zzJ == this.zzH);
                    this.f15810t = (((long) this.f15811u) * ((long) this.f15816z)) + this.f15810t;
                }
                this.zzJ = null;
            }
        } catch (zzqb e10) {
            boolean z10 = e10.zzb;
            if (z10) {
                if (t() > 0) {
                    z = true;
                } else if (((zl2) this.zzs).c()) {
                    this.f15799i.getClass();
                    z = true;
                }
            }
            zzri zzriVar = new zzri(e10.zza, this.f15799i.f13789a, z);
            il2 il2Var2 = this.zzm;
            if (il2Var2 != null) {
                vt0.zzf("MediaCodecAudioRenderer", "Audio sink error", zzriVar);
                ((rm2) il2Var2).f16167a.Q0.j(zzriVar);
            }
            if (z10) {
                throw zzriVar;
            }
            pm2Var.a(zzriVar);
        }
    }

    public final void n() {
        if (s()) {
            vk2 vk2Var = this.zzs;
            ((zl2) vk2Var).f19302a.setVolume(this.f15815y);
        }
    }

    public final void o() {
        if (this.f15799i != null) {
            lm2 lm2Var = this.zzn;
            if (lm2Var != null) {
                this.f15799i = lm2Var;
                this.zzn = null;
            }
            try {
                bl2 bl2VarB = ((em2) this.f15801k).b(u(this.f15799i.f13790b));
                lm2 lm2Var2 = this.f15799i;
                this.f15799i = new lm2(lm2Var2.f13789a, lm2Var2.f13790b, lm2Var2.f13791c, lm2Var2.f13792d, bl2VarB, lm2Var2.f13794f);
            } catch (zzqd e10) {
                throw new IllegalStateException(new zzre(e10, this.f15799i.f13789a));
            }
        }
        a();
    }

    public final void p(wd wdVar) {
        om2 om2Var = new om2(wdVar, -9223372036854775807L, -9223372036854775807L);
        if (s()) {
            this.zzu = om2Var;
        } else {
            this.f15804n = om2Var;
        }
    }

    public final void q(long j10) {
        boolean z10;
        wd wdVar;
        boolean z11 = false;
        if (this.f15799i.a()) {
            int i10 = this.f15799i.f13789a.f19350w;
            z10 = true;
        } else {
            z10 = false;
        }
        mm2 mm2Var = this.M;
        if (z10) {
            wdVar = this.f15805o;
            mm2Var.getClass();
            float f10 = wdVar.f18031a;
            fb0 fb0Var = mm2Var.f14294c;
            fb0Var.getClass();
            ih1.x(f10 > 0.0f);
            if (fb0Var.f11155c != f10) {
                fb0Var.f11155c = f10;
                fb0Var.f11161i = true;
            }
            float f11 = wdVar.f18032b;
            ih1.x(f11 > 0.0f);
            if (fb0Var.f11156d != f11) {
                fb0Var.f11156d = f11;
                fb0Var.f11161i = true;
            }
        } else {
            wdVar = wd.f18030d;
        }
        wd wdVar2 = wdVar;
        this.f15805o = wdVar2;
        if (this.f15799i.a()) {
            int i11 = this.f15799i.f13789a.f19350w;
            z11 = this.f15806p;
            mm2Var.f14293b.f17708j = z11;
        }
        this.f15806p = z11;
        this.f15796f.add(new om2(wdVar2, Math.max(0L, j10), bb1.q(this.f15799i.f13793e.f9570b, t())));
        g60 g60Var = this.f15799i.f13794f;
        this.f15800j = g60Var;
        g60Var.b(g70.f11543b);
        il2 il2Var = this.zzm;
        if (il2Var != null) {
            ((rm2) il2Var).f16167a.Q0.i(this.f15806p);
        }
    }

    public final void r(rm2 rm2Var) {
        this.zzm = rm2Var;
    }

    public final boolean s() {
        return this.zzs != null;
    }

    public final long t() {
        if (!this.f15799i.a()) {
            return this.f15810t;
        }
        long j10 = this.f15809s;
        long j11 = this.f15799i.f13792d;
        String str = bb1.f9474a;
        return ((j10 + j11) - 1) / j11;
    }

    public final xk2 u(zn2 zn2Var) {
        wk2 wk2Var = new wk2(zn2Var);
        wk2Var.f18109b = this.f15803m;
        wk2Var.zzb(this.zzR);
        wk2Var.f18110c = this.E;
        wk2Var.f18112e = -1;
        wk2Var.f18111d = this.H;
        return new xk2(wk2Var);
    }

    public final void v() {
        if (this.B) {
            return;
        }
        this.B = true;
        if (((zl2) this.zzs).c()) {
            this.C = false;
        }
        zl2 zl2Var = (zl2) this.zzs;
        if (zl2Var.f19308g) {
            return;
        }
        zl2Var.f19308g = true;
        long jE = zl2Var.e();
        fm2 fm2Var = zl2Var.f19304c;
        fm2Var.f11309v = fm2Var.d();
        ((h71) fm2Var.f11289b).getClass();
        fm2Var.f11307t = bb1.p(SystemClock.elapsedRealtime());
        fm2Var.f11310w = jE;
        zl2Var.f19302a.stop();
    }

    public final long w() {
        ArrayDeque arrayDeque;
        long j10;
        if (!s() || this.f15813w) {
            return Long.MIN_VALUE;
        }
        long jMin = Math.min(((zl2) this.zzs).f19304c.a(), bb1.q(this.f15799i.f13793e.f9570b, t()));
        while (true) {
            arrayDeque = this.f15796f;
            if (arrayDeque.isEmpty() || jMin < ((om2) arrayDeque.getFirst()).f15111c) {
                break;
            }
            this.f15804n = (om2) arrayDeque.remove();
        }
        om2 om2Var = this.f15804n;
        long jD = jMin - om2Var.f15111c;
        long jT = bb1.t(jD, om2Var.f15109a.f18031a);
        boolean zIsEmpty = arrayDeque.isEmpty();
        mm2 mm2Var = this.M;
        if (zIsEmpty) {
            fb0 fb0Var = mm2Var.f14294c;
            if (fb0Var.zzc()) {
                jD = fb0Var.d(jD);
            }
            om2 om2Var2 = this.f15804n;
            j10 = om2Var2.f15110b + jD;
            om2Var2.f15112d = jD - jT;
        } else {
            om2 om2Var3 = this.f15804n;
            j10 = om2Var3.f15110b + jT + om2Var3.f15112d;
        }
        long j11 = mm2Var.f14293b.f17710l;
        long jQ = bb1.q(this.f15799i.f13793e.f9570b, j11) + j10;
        long j12 = this.J;
        if (j11 > j12) {
            long jQ2 = bb1.q(this.f15799i.f13793e.f9570b, j11 - j12);
            this.J = j11;
            this.K += jQ2;
            if (this.L == null) {
                this.L = new Handler(Looper.myLooper());
            }
            this.L.removeCallbacksAndMessages(null);
            this.L.postDelayed(new na1(this, 19), 100L);
        }
        return jQ;
    }

    public final void x() {
        this.D = true;
        if (s()) {
            zl2 zl2Var = (zl2) this.zzs;
            fm2 fm2Var = zl2Var.f19304c;
            if (fm2Var.f11307t != -9223372036854775807L) {
                ((h71) fm2Var.f11289b).getClass();
                fm2Var.f11307t = bb1.p(SystemClock.elapsedRealtime());
            }
            fm2Var.f11297j = bb1.q(fm2Var.f11292e, fm2Var.d());
            fm2Var.f11295h.a(0);
            if (!zl2Var.f19308g || zl2Var.c()) {
                zl2Var.f19302a.play();
            }
        }
    }

    public final boolean y(long j10, int i10, ByteBuffer byteBuffer) throws zzri {
        zl2 zl2VarI;
        boolean z10;
        ByteBuffer byteBuffer2 = this.zzH;
        ih1.x(byteBuffer2 == null || byteBuffer == byteBuffer2);
        if (this.zzn != null) {
            if (!k()) {
                return false;
            }
            lm2 lm2Var = this.zzn;
            lm2 lm2Var2 = this.f15799i;
            lm2Var.getClass();
            if (lm2Var2.f13793e.equals(lm2Var.f13793e)) {
                this.f15799i = this.zzn;
                this.zzn = null;
                vk2 vk2Var = this.zzs;
                if (vk2Var != null && ((zl2) vk2Var).c()) {
                    this.f15799i.getClass();
                }
            } else {
                v();
                if (z()) {
                    return false;
                }
                a();
            }
            q(j10);
        }
        boolean zS = s();
        pm2 pm2Var = this.f15797g;
        if (zS) {
            z10 = false;
        } else {
            try {
                if (pm2Var.b()) {
                    return false;
                }
                try {
                    zl2VarI = i(this.f15799i.f13793e);
                } catch (zzrf e10) {
                    bl2 bl2Var = this.f15799i.f13793e;
                    if (bl2Var.f9572d > 1000000) {
                        al2 al2Var = new al2(bl2Var);
                        al2Var.f8738d = PlaybackException.CUSTOM_ERROR_CODE_BASE;
                        bl2 bl2Var2 = new bl2(al2Var);
                        try {
                            zl2 zl2VarI2 = i(bl2Var2);
                            lm2 lm2Var3 = this.f15799i;
                            this.f15799i = new lm2(lm2Var3.f13789a, lm2Var3.f13790b, lm2Var3.f13791c, lm2Var3.f13792d, bl2Var2, lm2Var3.f13794f);
                            zl2VarI = zl2VarI2;
                        } catch (zzrf e11) {
                            e10.addSuppressed(e11);
                            this.f15799i.getClass();
                            throw e10;
                        }
                    }
                    this.f15799i.getClass();
                    throw e10;
                }
                this.zzs = zl2VarI;
                im2 im2Var = new im2(this, this.f15799i.f13793e);
                this.zzi = im2Var;
                ((zl2) this.zzs).f19307f.a(im2Var);
                if (((zl2) this.zzs).c()) {
                    this.f15799i.getClass();
                }
                mk2 mk2Var = this.zzl;
                if (mk2Var != null) {
                    zl2 zl2Var = (zl2) this.zzs;
                    zl2Var.getClass();
                    if (Build.VERSION.SDK_INT >= 31) {
                        LogSessionId logSessionIdA = mk2Var.a();
                        if (!logSessionIdA.equals(LogSessionId.LOG_SESSION_ID_NONE)) {
                            zl2Var.f19302a.setLogSessionId(logSessionIdA);
                        }
                    }
                }
                n();
                this.G.getClass();
                AudioDeviceInfo audioDeviceInfo = this.zzR;
                if (audioDeviceInfo != null) {
                    ((zl2) this.zzs).zzo(audioDeviceInfo);
                }
                this.f15813w = true;
                int audioSessionId = ((zl2) this.zzs).f19302a.getAudioSessionId();
                int i11 = this.E;
                this.E = audioSessionId;
                il2 il2Var = this.zzm;
                if (il2Var != null) {
                    int i12 = this.f15799i.f13793e.f9569a;
                    ((rm2) il2Var).f16167a.Q0.l(new zj2(0));
                    if (audioSessionId != i11) {
                        this.F = true;
                        lm2 lm2Var4 = this.f15799i;
                        al2 al2Var2 = new al2(lm2Var4.f13793e);
                        al2Var2.f8740f = this.E;
                        this.f15799i = new lm2(lm2Var4.f13789a, lm2Var4.f13790b, lm2Var4.f13791c, lm2Var4.f13792d, new bl2(al2Var2), lm2Var4.f13794f);
                        lm2 lm2Var5 = this.zzn;
                        if (lm2Var5 != null) {
                            al2 al2Var3 = new al2(lm2Var5.f13793e);
                            al2Var3.f8740f = this.E;
                            this.zzn = new lm2(lm2Var5.f13789a, lm2Var5.f13790b, lm2Var5.f13791c, lm2Var5.f13792d, new bl2(al2Var3), lm2Var5.f13794f);
                        }
                        il2 il2Var2 = this.zzm;
                        int i13 = this.E;
                        if (Build.VERSION.SDK_INT >= 35) {
                            tm2 tm2Var = ((rm2) il2Var2).f16167a;
                            if (tm2Var.t0() != null) {
                                tm2Var.t0().a(i13);
                            }
                        }
                        ((rm2) il2Var2).f16167a.Q0.n(i13);
                    }
                }
                z10 = false;
            } catch (zzrf e12) {
                pm2Var.a(e12);
                return false;
            }
        }
        pm2Var.c();
        if (this.f15813w) {
            this.f15814x = Math.max(0L, j10);
            this.f15812v = z10;
            this.f15813w = z10;
            q(j10);
            if (this.D) {
                x();
            }
        }
        if (this.zzH == null) {
            ih1.x(byteBuffer.order() == ByteOrder.LITTLE_ENDIAN);
            if (!byteBuffer.hasRemaining()) {
                return true;
            }
            if (!this.f15799i.a() && this.f15811u == 0) {
                int iC = c(this.f15799i.f13793e.f9569a, byteBuffer);
                this.f15811u = iC;
                if (iC == 0) {
                    return true;
                }
            }
            if (this.zzu != null) {
                if (!k()) {
                    return false;
                }
                q(j10);
                this.zzu = null;
            }
            long j11 = this.f15814x;
            lm2 lm2Var6 = this.f15799i;
            long jQ = bb1.q(lm2Var6.f13789a.f19349v, (lm2Var6.a() ? this.f15807q / ((long) this.f15799i.f13791c) : this.f15808r) - this.f15792b.f18482o) + j11;
            if (!this.f15812v && Math.abs(jQ - j10) > 200000) {
                il2 il2Var3 = this.zzm;
                if (il2Var3 != null) {
                    StringBuilder sb = new StringBuilder(String.valueOf(jQ).length() + 63 + String.valueOf(j10).length());
                    defpackage.e.B(sb, "Unexpected audio track timestamp discontinuity: expected ", jQ, ", got ");
                    sb.append(j10);
                    zzrh zzrhVar = new zzrh(sb.toString());
                    vt0.zzf("MediaCodecAudioRenderer", "Audio sink error", zzrhVar);
                    ((rm2) il2Var3).f16167a.Q0.j(zzrhVar);
                }
                this.f15812v = true;
            }
            if (this.f15812v) {
                if (!k()) {
                    return false;
                }
                long j12 = j10 - jQ;
                this.f15814x += j12;
                this.f15812v = false;
                q(j10);
                il2 il2Var4 = this.zzm;
                if (il2Var4 != null && j12 != 0) {
                    ((rm2) il2Var4).f16167a.Y0 = true;
                }
            }
            if (this.f15799i.a()) {
                this.f15807q += (long) byteBuffer.remaining();
            } else {
                this.f15808r = (((long) this.f15811u) * ((long) i10)) + this.f15808r;
            }
            this.zzH = byteBuffer;
            this.f15816z = i10;
        }
        j(j10);
        if (!this.zzH.hasRemaining()) {
            this.zzH = null;
            this.f15816z = 0;
            return true;
        }
        zl2 zl2Var2 = (zl2) this.zzs;
        long jE = zl2Var2.e();
        fm2 fm2Var = zl2Var2.f19304c;
        if (fm2Var.f11308u == -9223372036854775807L || jE <= 0) {
            return false;
        }
        ((h71) fm2Var.f11289b).getClass();
        if (SystemClock.elapsedRealtime() - fm2Var.f11308u < 200) {
            return false;
        }
        vt0.c("Resetting stalled audio output");
        a();
        return true;
    }

    public final boolean z() {
        if (!s()) {
            return false;
        }
        if (Build.VERSION.SDK_INT >= 29 && ((zl2) this.zzs).c() && this.C) {
            return false;
        }
        long jT = t();
        long jA = ((zl2) this.zzs).f19304c.a();
        vk2 vk2Var = this.zzs;
        vk2Var.getClass();
        return jT > bb1.r(jA, (long) ((zl2) vk2Var).f19302a.getSampleRate(), 1000000L, RoundingMode.UP);
    }

    @Override // com.google.android.gms.internal.ads.jl2
    public final void zzb(@Nullable mk2 mk2Var) {
        this.zzl = mk2Var;
    }

    @Override // com.google.android.gms.internal.ads.jl2
    public final void zzh(zn2 zn2Var, int i10, @Nullable int[] iArr) throws zzre {
        int i11;
        zn2 zn2Var2;
        g60 g60Var;
        int iD;
        nm2 nm2Var = this.f15802l;
        cl2 cl2Var = this.f15801k;
        if (nm2Var == null && this.zzb != null) {
            nm2 nm2Var2 = new nm2(this);
            this.f15802l = nm2Var2;
            em2 em2Var = (em2) cl2Var;
            em2Var.e();
            if (em2Var.f10857a == null) {
                lt0 lt0Var = new lt0(Thread.currentThread());
                em2Var.f10857a = lt0Var;
                lt0Var.f13864g = false;
            }
            em2Var.f10857a.a(nm2Var2);
        }
        if (MimeTypes.AUDIO_RAW.equals(zn2Var.zzo)) {
            int i12 = zn2Var.f19350w;
            ih1.x(bb1.a(i12));
            int iD2 = bb1.d(i12);
            int i13 = zn2Var.f19348u;
            i11 = iD2 * i13;
            as1 as1Var = new as1(4);
            as1Var.b(this.f15795e);
            as1Var.a(this.f15793c);
            i80[] i80VarArr = this.M.f14292a;
            ih1.t(2, i80VarArr);
            as1Var.e(2);
            System.arraycopy(i80VarArr, 0, as1Var.f19374a, as1Var.f19375b, 2);
            as1Var.f19375b += 2;
            g60Var = new g60(as1Var.g());
            if (g60Var.equals(this.f15800j)) {
                g60Var = this.f15800j;
            }
            int i14 = zn2Var.f19351x;
            xm2 xm2Var = this.f15792b;
            xm2Var.f18476i = i14;
            xm2Var.f18477j = zn2Var.f19352y;
            this.f15791a.zzq(iArr);
            try {
                s60 s60VarA = g60Var.a(new s60(zn2Var.f19349v, i13, i12));
                sm2 sm2Var = new sm2(zn2Var);
                int i15 = s60VarA.f16309c;
                sm2Var.f16498u = i15;
                sm2Var.f16497t = s60VarA.f16307a;
                int i16 = s60VarA.f16308b;
                sm2Var.f16496s = i16;
                zn2Var2 = new zn2(sm2Var);
                iD = bb1.d(i15) * i16;
            } catch (zzcn e10) {
                throw new zzre(e10, zn2Var);
            }
        } else {
            i11 = -1;
            zn2Var2 = zn2Var;
            g60Var = new g60(zzgwa.f19786e);
            iD = -1;
        }
        xk2 xk2VarU = u(zn2Var2);
        try {
            bl2 bl2VarB = ((em2) cl2Var).b(xk2VarU);
            int i17 = bl2VarB.f9569a;
            zn2 zn2Var3 = xk2VarU.f18464a;
            if (i17 == 0) {
                StringBuilder sb = new StringBuilder(String.valueOf(false).length() + 36);
                sb.append("Invalid output encoding (isOffload=false)");
                throw new zzre(sb.toString(), zn2Var3);
            }
            if (bl2VarB.f9571c == 0) {
                StringBuilder sb2 = new StringBuilder(String.valueOf(false).length() + 42);
                sb2.append("Invalid output channel config (isOffload=false)");
                throw new zzre(sb2.toString(), zn2Var3);
            }
            lm2 lm2Var = new lm2(zn2Var, zn2Var2, i11, iD, bl2VarB, g60Var);
            if (s()) {
                this.zzn = lm2Var;
            } else {
                this.f15799i = lm2Var;
            }
        } catch (zzqd e11) {
            throw new zzre(e11, zn2Var);
        }
    }

    @Override // com.google.android.gms.internal.ads.jl2
    public final void zzu(@Nullable AudioDeviceInfo audioDeviceInfo) {
        this.zzR = audioDeviceInfo;
        vk2 vk2Var = this.zzs;
        if (vk2Var != null) {
            ((zl2) vk2Var).zzo(audioDeviceInfo);
        }
    }
}
