package D0;

import C3.AbstractC0055q;
import C3.W;
import T0.AbstractC0302b;
import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.PlaybackParams;
import android.media.metrics.LogSessionId;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Pair;
import d3.R0;
import d5.AbstractC0844a;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayDeque;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import u0.C1567c;
import u0.C1568d;
import u0.C1579o;
import v0.AbstractC1664c;
import v0.C1665d;
import v0.C1666e;
import v0.C1667f;
import v0.InterfaceC1668g;
import x0.AbstractC1709a;

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

    /* JADX INFO: renamed from: j0, reason: collision with root package name */
    public static final Object f1435j0 = new Object();

    /* JADX INFO: renamed from: k0, reason: collision with root package name */
    public static ScheduledExecutorService f1436k0;

    /* JADX INFO: renamed from: l0, reason: collision with root package name */
    public static int f1437l0;

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

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

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public u0.H f1440C;
    public boolean D;

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    public ByteBuffer f1451P;

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

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    public ByteBuffer f1453R;

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

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

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

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

    /* JADX INFO: renamed from: W, reason: collision with root package name */
    public boolean f1458W;
    public int X;

    /* JADX INFO: renamed from: Y, reason: collision with root package name */
    public C1568d f1459Y;

    /* JADX INFO: renamed from: Z, reason: collision with root package name */
    public K4.c f1460Z;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final A1.y f1463b;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    public Looper f1472f0;

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

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

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

    /* JADX INFO: renamed from: h0, reason: collision with root package name */
    public long f1476h0;
    public final boolean i;

    /* JADX INFO: renamed from: i0, reason: collision with root package name */
    public Handler f1477i0;

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public A1.y f1479k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final C f1480l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final C f1481m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final G f1482n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final R0 f1483o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final G f1484p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public C0.l f1485q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public K4.c f1486r;

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

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

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

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

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

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public A1.y f1493y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public C1567c f1494z = C1567c.f17266b;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public C0067d f1491w = null;

    public F(y yVar) {
        this.f1461a = (Context) yVar.f1635r;
        this.f1463b = (A1.y) yVar.f1636s;
        int i = x0.v.f18553a;
        this.i = false;
        this.f1478j = 0;
        this.f1482n = (G) yVar.f1637t;
        R0 r02 = (R0) yVar.f1639v;
        r02.getClass();
        this.f1483o = r02;
        this.f1473g = new u(new K4.c(5, this));
        v vVar = new v();
        this.f1465c = vVar;
        L l8 = new L();
        l8.f1530m = x0.v.f18555c;
        this.f1467d = l8;
        v0.k kVar = new v0.k();
        C3.E e8 = C3.G.f1197r;
        Object[] objArr = {kVar, vVar, l8};
        AbstractC0055q.a(3, objArr);
        this.f1469e = C3.G.i(3, objArr);
        Object[] objArr2 = {new K(), vVar, l8};
        AbstractC0055q.a(3, objArr2);
        this.f1471f = C3.G.i(3, objArr2);
        this.f1450O = 1.0f;
        this.X = 0;
        this.f1459Y = new C1568d();
        u0.H h8 = u0.H.f17158d;
        this.f1439B = new A(h8, 0L, 0L);
        this.f1440C = h8;
        this.D = false;
        this.f1475h = new ArrayDeque();
        this.f1480l = new C();
        this.f1481m = new C();
        this.f1484p = (G) yVar.f1638u;
    }

    public static boolean p(AudioTrack audioTrack) {
        return x0.v.f18553a >= 29 && audioTrack.isOffloadedPlayback();
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x0052  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(long j8) {
        u0.H h8;
        boolean zX = x();
        boolean z8 = false;
        A1.y yVar = this.f1463b;
        if (zX) {
            h8 = u0.H.f17158d;
        } else if (!this.f1462a0) {
            z zVar = this.f1488t;
            if (zVar.f1642c == 0) {
                int i = zVar.f1640a.f17338F;
                h8 = this.f1440C;
                yVar.getClass();
                float f3 = h8.f17159a;
                v0.j jVar = (v0.j) yVar.f365t;
                jVar.getClass();
                AbstractC1709a.d(f3 > 0.0f);
                if (jVar.f18051c != f3) {
                    jVar.f18051c = f3;
                    jVar.i = true;
                }
                float f7 = h8.f17160b;
                AbstractC1709a.d(f7 > 0.0f);
                if (jVar.f18052d != f7) {
                    jVar.f18052d = f7;
                    jVar.i = true;
                }
            } else {
                h8 = u0.H.f17158d;
            }
            this.f1440C = h8;
        }
        u0.H h9 = h8;
        if (!this.f1462a0) {
            z zVar2 = this.f1488t;
            if (zVar2.f1642c == 0) {
                int i5 = zVar2.f1640a.f17338F;
                z8 = this.D;
                ((J) yVar.f364s).f1519o = z8;
            }
        }
        this.D = z8;
        this.f1475h.add(new A(h9, Math.max(0L, j8), x0.v.Q(k(), this.f1488t.f1644e)));
        C1665d c1665d = this.f1488t.i;
        this.f1489u = c1665d;
        c1665d.b();
        K4.c cVar = this.f1486r;
        if (cVar != null) {
            final boolean z9 = this.D;
            final m mVar = ((H) cVar.f3604r).f1498T0;
            Handler handler = mVar.f1569b;
            if (handler != null) {
                handler.post(new Runnable() { // from class: D0.l
                    @Override // java.lang.Runnable
                    public final void run() {
                        m mVar2 = mVar;
                        mVar2.getClass();
                        int i8 = x0.v.f18553a;
                        B0.I i9 = mVar2.f1570c.f523q;
                        boolean z10 = i9.f568p0;
                        boolean z11 = z9;
                        if (z10 == z11) {
                            return;
                        }
                        i9.f568p0 = z11;
                        i9.f532C.e(23, new B0.B(1, z11));
                    }
                });
            }
        }
    }

    public final AudioTrack b(n nVar, C1567c c1567c, int i, C1579o c1579o) throws p {
        try {
            try {
                AudioTrack audioTrackA = this.f1484p.a(nVar, c1567c, i);
                int state = audioTrackA.getState();
                if (state == 1) {
                    return audioTrackA;
                }
                try {
                    audioTrackA.release();
                } catch (Exception unused) {
                }
                throw new p(state, nVar.f1573c, nVar.f1575e, nVar.f1572b, c1579o, nVar.f1574d, null);
            } catch (IllegalArgumentException e8) {
                e = e8;
                RuntimeException runtimeException = e;
                throw new p(0, nVar.f1573c, nVar.f1575e, nVar.f1572b, c1579o, nVar.f1574d, runtimeException);
            } catch (UnsupportedOperationException e9) {
                e = e9;
                RuntimeException runtimeException2 = e;
                throw new p(0, nVar.f1573c, nVar.f1575e, nVar.f1572b, c1579o, nVar.f1574d, runtimeException2);
            }
        } catch (IllegalArgumentException e10) {
            e = e10;
        } catch (UnsupportedOperationException e11) {
            e = e11;
        }
    }

    public final AudioTrack c(z zVar) throws p {
        try {
            return b(zVar.a(), this.f1494z, this.X, zVar.f1640a);
        } catch (p e8) {
            K4.c cVar = this.f1486r;
            if (cVar != null) {
                cVar.G(e8);
            }
            throw e8;
        }
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Removed duplicated region for block: B:57:0x0131  */
    /* JADX WARN: Removed duplicated region for block: B:72:0x0171  */
    /* JADX WARN: Removed duplicated region for block: B:77:0x0182  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void d(C1579o c1579o, int[] iArr) throws o {
        int iIntValue;
        int iS;
        int i;
        C1665d c1665d;
        boolean z8;
        int i5;
        boolean z9;
        int i8;
        int i9;
        int i10;
        int i11;
        int i12;
        int i13;
        int iC;
        q();
        boolean zEquals = "audio/raw".equals(c1579o.f17358n);
        boolean z10 = this.i;
        String str = c1579o.f17358n;
        int i14 = c1579o.f17337E;
        int i15 = c1579o.D;
        if (zEquals) {
            int i16 = c1579o.f17338F;
            AbstractC1709a.d(x0.v.H(i16));
            int iS2 = x0.v.s(i16) * i15;
            C3.D d8 = new C3.D(4);
            d8.d(this.f1469e);
            d8.b((InterfaceC1668g[]) this.f1463b.f363r);
            C1665d c1665d2 = new C1665d(d8.g());
            if (c1665d2.equals(this.f1489u)) {
                c1665d2 = this.f1489u;
            }
            int i17 = c1579o.f17339G;
            L l8 = this.f1467d;
            l8.i = i17;
            l8.f1527j = c1579o.H;
            this.f1465c.i = iArr;
            try {
                C1666e c1666eA = c1665d2.a(new C1666e(i14, i15, i16));
                int i18 = c1666eA.f18017b;
                iIntValue = x0.v.r(i18);
                int i19 = c1666eA.f18018c;
                iS = x0.v.s(i19) * i18;
                i14 = c1666eA.f18016a;
                i = i19;
                i5 = 0;
                z9 = z10;
                i8 = iS2;
                c1665d = c1665d2;
                z8 = false;
            } catch (C1667f e8) {
                throw new o(e8, c1579o);
            }
        } else {
            C1665d c1665d3 = new C1665d(W.f1225u);
            C0073j c0073jH = this.f1478j != 0 ? h(c1579o) : C0073j.f1560d;
            if (this.f1478j == 0 || !c0073jH.f1561a) {
                Pair pairD = this.f1491w.d(c1579o, this.f1494z);
                if (pairD == null) {
                    throw new o("Unable to configure passthrough for: " + c1579o, c1579o);
                }
                int iIntValue2 = ((Integer) pairD.first).intValue();
                iIntValue = ((Integer) pairD.second).intValue();
                iS = -1;
                i = iIntValue2;
                c1665d = c1665d3;
                z8 = false;
                i5 = 2;
                z9 = z10;
                i8 = -1;
            } else {
                str.getClass();
                int iC2 = u0.E.c(str, c1579o.f17355k);
                iIntValue = x0.v.r(i15);
                i5 = 1;
                i8 = -1;
                iS = -1;
                c1665d = c1665d3;
                z8 = c0073jH.f1562b;
                i = iC2;
                z9 = true;
            }
        }
        if (i == 0) {
            throw new o("Invalid output encoding (mode=" + i5 + ") for: " + c1579o, c1579o);
        }
        if (iIntValue == 0) {
            throw new o("Invalid output channel config (mode=" + i5 + ") for: " + c1579o, c1579o);
        }
        boolean zEquals2 = "audio/vnd.dts.hd;profile=lbr".equals(str);
        int i20 = c1579o.f17354j;
        if (zEquals2 && i20 == -1) {
            i20 = 768000;
        }
        int minBufferSize = AudioTrack.getMinBufferSize(i14, iIntValue, i);
        AbstractC1709a.j(minBufferSize != -2);
        int i21 = iS != -1 ? iS : 1;
        double d9 = z9 ? 8.0d : 1.0d;
        this.f1482n.getClass();
        if (i5 != 0) {
            if (i5 == 1) {
                i9 = i8;
                i11 = W3.b.h((((long) 50000000) * ((long) G.c(i))) / 1000000);
            } else {
                if (i5 != 2) {
                    throw new IllegalArgumentException();
                }
                if (i == 5) {
                    i13 = 500000;
                } else if (i == 8) {
                    i13 = 1000000;
                } else {
                    i12 = 8;
                    i13 = 250000;
                    if (i20 == -1) {
                        RoundingMode roundingMode = RoundingMode.CEILING;
                        roundingMode.getClass();
                        iC = i20 / 8;
                        int i22 = i20 - (i12 * iC);
                        if (i22 != 0) {
                            int i23 = ((i20 ^ 8) >> 31) | 1;
                            switch (E3.c.f2179a[roundingMode.ordinal()]) {
                                case 1:
                                    if (i22 != 0) {
                                        throw new ArithmeticException("mode was UNNECESSARY, but rounding was necessary");
                                    }
                                    break;
                                case 2:
                                    break;
                                case 3:
                                    if (i23 < 0) {
                                        iC += i23;
                                    }
                                    break;
                                case 4:
                                    break;
                                case 5:
                                    if (i23 > 0) {
                                    }
                                    break;
                                case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                                case Z.g.DOUBLE_FIELD_NUMBER /* 7 */:
                                case Z.g.BYTES_FIELD_NUMBER /* 8 */:
                                    int iAbs = Math.abs(i22);
                                    int iAbs2 = iAbs - (Math.abs(i12) - iAbs);
                                    if (iAbs2 == 0) {
                                        RoundingMode roundingMode2 = RoundingMode.HALF_UP;
                                        RoundingMode roundingMode3 = RoundingMode.HALF_EVEN;
                                        break;
                                    } else if (iAbs2 > 0) {
                                    }
                                    break;
                                default:
                                    throw new AssertionError();
                            }
                        }
                    } else {
                        iC = G.c(i);
                    }
                    i9 = i8;
                    i11 = W3.b.h((((long) i13) * ((long) iC)) / 1000000);
                }
                i12 = 8;
                if (i20 == -1) {
                }
                i9 = i8;
                i11 = W3.b.h((((long) i13) * ((long) iC)) / 1000000);
            }
            i10 = i;
        } else {
            i9 = i8;
            long j8 = i14;
            long j9 = ((long) 250000) * j8;
            long j10 = i21;
            i10 = i;
            i11 = x0.v.i(minBufferSize * 4, W3.b.h((j9 * j10) / 1000000), W3.b.h(((((long) 750000) * j8) * j10) / 1000000));
        }
        this.f1468d0 = false;
        z zVar = new z(c1579o, i9, i5, iS, i14, iIntValue, i10, (((Math.max(minBufferSize, (int) (((double) i11) * d9)) + i21) - 1) / i21) * i21, c1665d, z9, z8, this.f1462a0);
        if (o()) {
            this.f1487s = zVar;
        } else {
            this.f1488t = zVar;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x00ab  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void e(long j8) throws r {
        int iWrite;
        K4.c cVar;
        B0.L l8;
        boolean z8;
        if (this.f1453R == null) {
            return;
        }
        C c3 = this.f1481m;
        if (c3.f1430a != null) {
            synchronized (f1435j0) {
                z8 = f1437l0 > 0;
            }
            if (z8 || SystemClock.elapsedRealtime() < c3.f1432c) {
                return;
            }
        }
        int iRemaining = this.f1453R.remaining();
        if (this.f1462a0) {
            AbstractC1709a.j(j8 != -9223372036854775807L);
            if (j8 == Long.MIN_VALUE) {
                j8 = this.f1464b0;
            } else {
                this.f1464b0 = j8;
            }
            AudioTrack audioTrack = this.f1490v;
            ByteBuffer byteBuffer = this.f1453R;
            if (x0.v.f18553a >= 26) {
                iWrite = audioTrack.write(byteBuffer, iRemaining, 1, 1000 * j8);
            } else {
                if (this.f1441E == null) {
                    ByteBuffer byteBufferAllocate = ByteBuffer.allocate(16);
                    this.f1441E = byteBufferAllocate;
                    byteBufferAllocate.order(ByteOrder.BIG_ENDIAN);
                    this.f1441E.putInt(1431633921);
                }
                if (this.f1442F == 0) {
                    this.f1441E.putInt(4, iRemaining);
                    this.f1441E.putLong(8, j8 * 1000);
                    this.f1441E.position(0);
                    this.f1442F = iRemaining;
                }
                int iRemaining2 = this.f1441E.remaining();
                if (iRemaining2 <= 0) {
                    iWrite = audioTrack.write(byteBuffer, iRemaining, 1);
                    if (iWrite < 0) {
                        this.f1442F = 0;
                    } else {
                        this.f1442F -= iWrite;
                    }
                } else {
                    int iWrite2 = audioTrack.write(this.f1441E, iRemaining2, 1);
                    if (iWrite2 < 0) {
                        this.f1442F = 0;
                        iWrite = iWrite2;
                    } else if (iWrite2 < iRemaining2) {
                        iWrite = 0;
                    }
                }
            }
        } else {
            iWrite = this.f1490v.write(this.f1453R, iRemaining, 1);
        }
        this.f1466c0 = SystemClock.elapsedRealtime();
        if (iWrite < 0) {
            if ((x0.v.f18553a >= 24 && iWrite == -6) || iWrite == -32) {
                if (k() > 0) {
                    z = true;
                } else if (p(this.f1490v)) {
                    if (this.f1488t.f1642c == 1) {
                        this.f1468d0 = true;
                    }
                    z = true;
                }
            }
            r rVar = new r(iWrite, this.f1488t.f1640a, z);
            K4.c cVar2 = this.f1486r;
            if (cVar2 != null) {
                cVar2.G(rVar);
            }
            if (rVar.f1581r) {
                this.f1491w = C0067d.f1537c;
                throw rVar;
            }
            c3.a(rVar);
            return;
        }
        c3.f1430a = null;
        c3.f1431b = -9223372036854775807L;
        c3.f1432c = -9223372036854775807L;
        if (p(this.f1490v)) {
            if (this.f1445J > 0) {
                this.f1470e0 = false;
            }
            if (this.f1457V && (cVar = this.f1486r) != null && iWrite < iRemaining && !this.f1470e0 && (l8 = ((H) cVar.f3604r).f3281V) != null) {
                l8.f591a.f630c0 = true;
            }
        }
        int i = this.f1488t.f1642c;
        if (i == 0) {
            this.f1444I += (long) iWrite;
        }
        if (iWrite == iRemaining) {
            if (i != 0) {
                AbstractC1709a.j(this.f1453R == this.f1451P);
                this.f1445J = (((long) this.f1446K) * ((long) this.f1452Q)) + this.f1445J;
            }
            this.f1453R = null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x0043 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:20:0x0044 A[RETURN] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean f() throws r {
        ByteBuffer byteBuffer;
        if (!this.f1489u.e()) {
            e(Long.MIN_VALUE);
            return this.f1453R == null;
        }
        C1665d c1665d = this.f1489u;
        if (c1665d.e() && !c1665d.f18014d) {
            c1665d.f18014d = true;
            ((InterfaceC1668g) c1665d.f18012b.get(0)).c();
        }
        t(Long.MIN_VALUE);
        if (!this.f1489u.d() || ((byteBuffer = this.f1453R) != null && byteBuffer.hasRemaining())) {
        }
    }

    public final void g() {
        A1.y yVar;
        if (o()) {
            this.f1443G = 0L;
            this.H = 0L;
            this.f1444I = 0L;
            this.f1445J = 0L;
            this.f1470e0 = false;
            this.f1446K = 0;
            this.f1439B = new A(this.f1440C, 0L, 0L);
            this.f1449N = 0L;
            this.f1438A = null;
            this.f1475h.clear();
            this.f1451P = null;
            this.f1452Q = 0;
            this.f1453R = null;
            this.f1455T = false;
            this.f1454S = false;
            this.f1456U = false;
            this.f1441E = null;
            this.f1442F = 0;
            this.f1467d.f1532o = 0L;
            C1665d c1665d = this.f1488t.i;
            this.f1489u = c1665d;
            c1665d.b();
            AudioTrack audioTrack = this.f1473g.f1605c;
            audioTrack.getClass();
            if (audioTrack.getPlayState() == 3) {
                this.f1490v.pause();
            }
            if (p(this.f1490v)) {
                A1.y yVar2 = this.f1479k;
                yVar2.getClass();
                this.f1490v.unregisterStreamEventCallback((E) yVar2.f364s);
                ((Handler) yVar2.f363r).removeCallbacksAndMessages(null);
            }
            n nVarA = this.f1488t.a();
            z zVar = this.f1487s;
            if (zVar != null) {
                this.f1488t = zVar;
                this.f1487s = null;
            }
            u uVar = this.f1473g;
            uVar.e();
            uVar.f1605c = null;
            uVar.f1607e = null;
            if (x0.v.f18553a >= 24 && (yVar = this.f1493y) != null) {
                B b8 = (B) yVar.f365t;
                b8.getClass();
                ((AudioTrack) yVar.f363r).removeOnRoutingChangedListener(b8);
                yVar.f365t = null;
                this.f1493y = null;
            }
            AudioTrack audioTrack2 = this.f1490v;
            K4.c cVar = this.f1486r;
            Handler handler = new Handler(Looper.myLooper());
            synchronized (f1435j0) {
                try {
                    if (f1436k0 == null) {
                        f1436k0 = Executors.newSingleThreadScheduledExecutor(new x0.u());
                    }
                    f1437l0++;
                    f1436k0.schedule(new x(audioTrack2, cVar, handler, nVarA, 0), 20L, TimeUnit.MILLISECONDS);
                } catch (Throwable th) {
                    throw th;
                }
            }
            this.f1490v = null;
        }
        C c3 = this.f1481m;
        c3.f1430a = null;
        c3.f1431b = -9223372036854775807L;
        c3.f1432c = -9223372036854775807L;
        C c8 = this.f1480l;
        c8.f1430a = null;
        c8.f1431b = -9223372036854775807L;
        c8.f1432c = -9223372036854775807L;
        this.f1474g0 = 0L;
        this.f1476h0 = 0L;
        Handler handler2 = this.f1477i0;
        if (handler2 != null) {
            handler2.removeCallbacksAndMessages(null);
        }
    }

    public final C0073j h(C1579o c1579o) {
        int i;
        boolean zBooleanValue;
        if (this.f1468d0) {
            return C0073j.f1560d;
        }
        C1567c c1567c = this.f1494z;
        R0 r02 = this.f1483o;
        r02.getClass();
        c1579o.getClass();
        c1567c.getClass();
        int i5 = x0.v.f18553a;
        if (i5 < 29 || (i = c1579o.f17337E) == -1) {
            return C0073j.f1560d;
        }
        Boolean bool = (Boolean) r02.f11693s;
        boolean z8 = false;
        if (bool != null) {
            zBooleanValue = bool.booleanValue();
        } else {
            String parameters = AbstractC1664c.q((Context) r02.f11692r).getParameters("offloadVariableRateSupported");
            Boolean boolValueOf = Boolean.valueOf(parameters != null && parameters.equals("offloadVariableRateSupported=1"));
            r02.f11693s = boolValueOf;
            zBooleanValue = boolValueOf.booleanValue();
        }
        String str = c1579o.f17358n;
        str.getClass();
        int iC = u0.E.c(str, c1579o.f17355k);
        if (iC == 0 || i5 < x0.v.p(iC)) {
            return C0073j.f1560d;
        }
        int iR = x0.v.r(c1579o.D);
        if (iR == 0) {
            return C0073j.f1560d;
        }
        try {
            AudioFormat audioFormatQ = x0.v.q(i, iR, iC);
            if (i5 < 31) {
                if (!AudioManager.isOffloadedPlaybackSupported(audioFormatQ, (AudioAttributes) c1567c.a().f14409q)) {
                    return C0073j.f1560d;
                }
                C0072i c0072i = new C0072i();
                c0072i.f1557a = true;
                c0072i.f1559c = zBooleanValue;
                return c0072i.a();
            }
            int playbackOffloadSupport = AudioManager.getPlaybackOffloadSupport(audioFormatQ, (AudioAttributes) c1567c.a().f14409q);
            if (playbackOffloadSupport == 0) {
                return C0073j.f1560d;
            }
            C0072i c0072i2 = new C0072i();
            if (i5 > 32 && playbackOffloadSupport == 2) {
                z8 = true;
            }
            c0072i2.f1557a = true;
            c0072i2.f1558b = z8;
            c0072i2.f1559c = zBooleanValue;
            return c0072i2.a();
        } catch (IllegalArgumentException unused) {
            return C0073j.f1560d;
        }
    }

    public final int i(C1579o c1579o) {
        q();
        if ("audio/raw".equals(c1579o.f17358n)) {
            int i = c1579o.f17338F;
            if (!x0.v.H(i)) {
                AbstractC0844a.s(i, "Invalid PCM encoding: ", "DefaultAudioSink");
                return 0;
            }
            if (i != 2) {
                return 1;
            }
        } else if (this.f1491w.d(c1579o, this.f1494z) == null) {
            return 0;
        }
        return 2;
    }

    public final long j() {
        z zVar = this.f1488t;
        return zVar.f1642c == 0 ? this.f1443G / ((long) zVar.f1641b) : this.H;
    }

    public final long k() {
        z zVar = this.f1488t;
        if (zVar.f1642c != 0) {
            return this.f1445J;
        }
        long j8 = this.f1444I;
        long j9 = zVar.f1643d;
        int i = x0.v.f18553a;
        return ((j8 + j9) - 1) / j9;
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    /* JADX WARN: Code restructure failed: missing block: B:210:0x0399, code lost:
    
        if (r5 == 0) goto L211;
     */
    /* JADX WARN: Code restructure failed: missing block: B:72:0x0113, code lost:
    
        if (r10.b() == 0) goto L12;
     */
    /* JADX WARN: Removed duplicated region for block: B:140:0x024d  */
    /* JADX WARN: Removed duplicated region for block: B:162:0x027f  */
    /* JADX WARN: Removed duplicated region for block: B:164:0x0285  */
    /* JADX WARN: Removed duplicated region for block: B:165:0x0287  */
    /* JADX WARN: Removed duplicated region for block: B:224:0x03da  */
    /* JADX WARN: Removed duplicated region for block: B:228:0x03fe  */
    /* JADX WARN: Removed duplicated region for block: B:238:0x0426  */
    /* JADX WARN: Removed duplicated region for block: B:239:0x0431  */
    /* JADX WARN: Removed duplicated region for block: B:243:0x044a  */
    /* JADX WARN: Removed duplicated region for block: B:245:0x0451  */
    /* JADX WARN: Removed duplicated region for block: B:254:0x00b0 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:57:0x00c2  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean l(ByteBuffer byteBuffer, long j8, int i) throws p, r {
        boolean zO;
        K4.c cVar;
        int i5;
        int iJ;
        int i8;
        byte b8;
        int i9;
        byte b9;
        int i10;
        int i11;
        int i12;
        int i13;
        s sVar;
        ByteBuffer byteBuffer2 = this.f1451P;
        AbstractC1709a.d(byteBuffer2 == null || byteBuffer == byteBuffer2);
        z zVar = this.f1487s;
        u uVar = this.f1473g;
        if (zVar == null) {
            zO = o();
            C c3 = this.f1480l;
            if (zO) {
                try {
                    if (n()) {
                        c3.f1430a = null;
                        c3.f1431b = -9223372036854775807L;
                        c3.f1432c = -9223372036854775807L;
                        if (this.f1448M) {
                            this.f1449N = Math.max(0L, j8);
                            this.f1447L = false;
                            this.f1448M = false;
                            if (x()) {
                                v();
                            }
                            a(j8);
                            if (this.f1457V) {
                                r();
                            }
                        }
                        long jK = k();
                        AudioTrack audioTrack = uVar.f1605c;
                        audioTrack.getClass();
                        int playState = audioTrack.getPlayState();
                        if (uVar.f1609g) {
                            if (playState == 2) {
                                uVar.f1616o = false;
                                return false;
                            }
                            if (playState == 1) {
                            }
                        }
                        boolean z8 = uVar.f1616o;
                        boolean zD = uVar.d(jK);
                        uVar.f1616o = zD;
                        if (z8 && !zD && playState != 1) {
                            int i14 = uVar.f1606d;
                            long jW = x0.v.W(uVar.f1610h);
                            F f3 = (F) uVar.f1603a.f3604r;
                            if (f3.f1486r != null) {
                                long jElapsedRealtime = SystemClock.elapsedRealtime() - f3.f1466c0;
                                m mVar = ((H) f3.f1486r.f3604r).f1498T0;
                                Handler handler = mVar.f1569b;
                                if (handler != null) {
                                    handler.post(new RunnableC0074k(mVar, i14, jW, jElapsedRealtime));
                                }
                            }
                        }
                        if (this.f1451P == null) {
                            AbstractC1709a.d(byteBuffer.order() == ByteOrder.LITTLE_ENDIAN);
                            if (byteBuffer.hasRemaining()) {
                                z zVar2 = this.f1488t;
                                if (zVar2.f1642c != 0 && this.f1446K == 0) {
                                    int i15 = zVar2.f1646g;
                                    if (i15 == 20) {
                                        if ((byteBuffer.get(5) & 2) == 0) {
                                            i5 = 0;
                                        } else {
                                            byte b10 = byteBuffer.get(26);
                                            int i16 = 28;
                                            int i17 = 28;
                                            for (int i18 = 0; i18 < b10; i18++) {
                                                i17 += byteBuffer.get(i18 + 27);
                                            }
                                            byte b11 = byteBuffer.get(i17 + 26);
                                            for (int i19 = 0; i19 < b11; i19++) {
                                                i16 += byteBuffer.get(i17 + 27 + i19);
                                            }
                                            i5 = i17 + i16;
                                        }
                                        int i20 = byteBuffer.get(i5 + 26) + 27 + i5;
                                        iJ = (int) ((AbstractC0302b.j(byteBuffer.get(i20), byteBuffer.limit() - i20 > 1 ? byteBuffer.get(i20 + 1) : (byte) 0) * 48000) / 1000000);
                                    } else if (i15 != 30) {
                                        switch (i15) {
                                            case 5:
                                            case Z.g.STRING_SET_FIELD_NUMBER /* 6 */:
                                                i10 = ((byteBuffer.get(byteBuffer.position() + 5) & 248) >> 3) > 10 ? AbstractC0302b.f6368c[((byteBuffer.get(byteBuffer.position() + 4) & 192) >> 6) != 3 ? (byteBuffer.get(byteBuffer.position() + 4) & 48) >> 4 : 3] * 256 : 1536;
                                                iJ = i10;
                                                break;
                                            case Z.g.DOUBLE_FIELD_NUMBER /* 7 */:
                                            case Z.g.BYTES_FIELD_NUMBER /* 8 */:
                                                if (byteBuffer.getInt(0) == -233094848 || byteBuffer.getInt(0) == -398277519) {
                                                    iJ = 1024;
                                                } else if (byteBuffer.getInt(0) == 622876772) {
                                                    iJ = 4096;
                                                } else {
                                                    int iPosition = byteBuffer.position();
                                                    byte b12 = byteBuffer.get(iPosition);
                                                    if (b12 != -2) {
                                                        if (b12 == -1) {
                                                            i8 = (byteBuffer.get(iPosition + 4) & 7) << 4;
                                                            b9 = byteBuffer.get(iPosition + 7);
                                                        } else if (b12 != 31) {
                                                            i8 = (byteBuffer.get(iPosition + 4) & 1) << 6;
                                                            b8 = byteBuffer.get(iPosition + 5);
                                                        } else {
                                                            i8 = (byteBuffer.get(iPosition + 5) & 7) << 4;
                                                            b9 = byteBuffer.get(iPosition + 6);
                                                        }
                                                        i9 = b9 & 60;
                                                        iJ = (((i9 >> 2) | i8) + 1) * 32;
                                                    } else {
                                                        i8 = (byteBuffer.get(iPosition + 5) & 1) << 6;
                                                        b8 = byteBuffer.get(iPosition + 4);
                                                    }
                                                    i9 = b8 & 252;
                                                    iJ = (((i9 >> 2) | i8) + 1) * 32;
                                                }
                                                break;
                                            case 9:
                                                int iReverseBytes = byteBuffer.getInt(byteBuffer.position());
                                                if (byteBuffer.order() != ByteOrder.BIG_ENDIAN) {
                                                    iReverseBytes = Integer.reverseBytes(iReverseBytes);
                                                }
                                                if ((iReverseBytes & (-2097152)) != -2097152 || (i11 = (iReverseBytes >>> 19) & 3) == 1 || (i12 = (iReverseBytes >>> 17) & 3) == 0) {
                                                    i10 = -1;
                                                    if (i10 == -1) {
                                                        throw new IllegalArgumentException();
                                                    }
                                                    iJ = i10;
                                                } else {
                                                    int i21 = (iReverseBytes >>> 12) & 15;
                                                    int i22 = (iReverseBytes >>> 10) & 3;
                                                    if (i21 != 0 && i21 != 15 && i22 != 3) {
                                                        int i23 = 1152;
                                                        if (i12 != 1) {
                                                            if (i12 != 2) {
                                                                if (i12 != 3) {
                                                                    throw new IllegalArgumentException();
                                                                }
                                                                i23 = 384;
                                                            }
                                                        } else if (i11 != 3) {
                                                            i23 = 576;
                                                        }
                                                        i10 = i23;
                                                    }
                                                    if (i10 == -1) {
                                                    }
                                                    iJ = i10;
                                                }
                                                break;
                                            case 10:
                                                iJ = 1024;
                                                break;
                                            case 11:
                                            case 12:
                                                iJ = 2048;
                                                break;
                                            default:
                                                char c8 = 16;
                                                switch (i15) {
                                                    case 14:
                                                        int iPosition2 = byteBuffer.position();
                                                        int iLimit = byteBuffer.limit() - 10;
                                                        int i24 = iPosition2;
                                                        while (true) {
                                                            if (i24 <= iLimit) {
                                                                int iReverseBytes2 = byteBuffer.getInt(i24 + 4);
                                                                char c9 = c8;
                                                                if (byteBuffer.order() != ByteOrder.BIG_ENDIAN) {
                                                                    iReverseBytes2 = Integer.reverseBytes(iReverseBytes2);
                                                                }
                                                                if ((iReverseBytes2 & (-2)) == -126718022) {
                                                                    i13 = i24 - iPosition2;
                                                                } else {
                                                                    i24++;
                                                                    c8 = c9;
                                                                }
                                                            } else {
                                                                i13 = -1;
                                                            }
                                                        }
                                                        if (i13 == -1) {
                                                            iJ = 0;
                                                        } else {
                                                            iJ = (40 << ((byteBuffer.get((byteBuffer.position() + i13) + ((byteBuffer.get((byteBuffer.position() + i13) + 7) & 255) == 187 ? 9 : 8)) >> 4) & 7)) * 16;
                                                        }
                                                        break;
                                                    case 15:
                                                        iJ = 512;
                                                        break;
                                                    case 16:
                                                        iJ = 1024;
                                                        break;
                                                    case 17:
                                                        byte[] bArr = new byte[16];
                                                        int iPosition3 = byteBuffer.position();
                                                        byteBuffer.get(bArr);
                                                        byteBuffer.position(iPosition3);
                                                        iJ = AbstractC0302b.m(new T0.H(16, bArr)).f5898c;
                                                        break;
                                                    case 18:
                                                        break;
                                                    default:
                                                        throw new IllegalStateException(AbstractC0844a.j("Unexpected audio encoding: ", i15));
                                                }
                                                break;
                                        }
                                    }
                                    this.f1446K = iJ;
                                }
                                if (this.f1438A == null) {
                                    long jQ = x0.v.Q(j() - this.f1467d.f1532o, this.f1488t.f1640a.f17337E) + this.f1449N;
                                    if (!this.f1447L && Math.abs(jQ - j8) > 200000) {
                                        cVar = this.f1486r;
                                        if (cVar != null) {
                                            cVar.G(new q("Unexpected audio track timestamp discontinuity: expected " + jQ + ", got " + j8));
                                        }
                                        this.f1447L = true;
                                    }
                                    if (this.f1447L) {
                                        if (f()) {
                                            long j9 = j8 - jQ;
                                            this.f1449N += j9;
                                            this.f1447L = false;
                                            a(j8);
                                            K4.c cVar2 = this.f1486r;
                                            if (cVar2 != null && j9 != 0) {
                                                ((H) cVar2.f3604r).f1505c1 = true;
                                            }
                                        }
                                    }
                                    if (this.f1488t.f1642c != 0) {
                                        this.f1443G += (long) byteBuffer.remaining();
                                    } else {
                                        this.H = (((long) this.f1446K) * ((long) i)) + this.H;
                                    }
                                    this.f1451P = byteBuffer;
                                    this.f1452Q = i;
                                    t(j8);
                                    if (!this.f1451P.hasRemaining()) {
                                    }
                                } else if (f()) {
                                    a(j8);
                                    this.f1438A = null;
                                    long jQ2 = x0.v.Q(j() - this.f1467d.f1532o, this.f1488t.f1640a.f17337E) + this.f1449N;
                                    if (!this.f1447L) {
                                        cVar = this.f1486r;
                                        if (cVar != null) {
                                        }
                                        this.f1447L = true;
                                    }
                                    if (this.f1447L) {
                                    }
                                    if (this.f1488t.f1642c != 0) {
                                    }
                                    this.f1451P = byteBuffer;
                                    this.f1452Q = i;
                                    t(j8);
                                    if (!this.f1451P.hasRemaining()) {
                                    }
                                }
                            }
                            return true;
                        }
                        t(j8);
                        if (!this.f1451P.hasRemaining()) {
                            this.f1451P = null;
                            this.f1452Q = 0;
                            return true;
                        }
                        long jK2 = k();
                        if (uVar.f1626y != -9223372036854775807L && jK2 > 0) {
                            uVar.f1602I.getClass();
                            if (SystemClock.elapsedRealtime() - uVar.f1626y >= 200) {
                                AbstractC1709a.B("DefaultAudioSink", "Resetting stalled audio track");
                                g();
                                return true;
                            }
                        }
                        return false;
                    }
                } catch (p e8) {
                    if (e8.f1579r) {
                        throw e8;
                    }
                    c3.a(e8);
                    return false;
                }
            }
        } else if (f()) {
            z zVar3 = this.f1487s;
            z zVar4 = this.f1488t;
            zVar3.getClass();
            if (zVar4.f1642c == zVar3.f1642c && zVar4.f1646g == zVar3.f1646g && zVar4.f1644e == zVar3.f1644e && zVar4.f1645f == zVar3.f1645f && zVar4.f1643d == zVar3.f1643d && zVar4.f1648j == zVar3.f1648j && zVar4.f1649k == zVar3.f1649k) {
                this.f1488t = this.f1487s;
                this.f1487s = null;
                AudioTrack audioTrack2 = this.f1490v;
                if (audioTrack2 != null && p(audioTrack2) && this.f1488t.f1649k) {
                    if (this.f1490v.getPlayState() == 3) {
                        this.f1490v.setOffloadEndOfStream();
                        uVar.f1601G = true;
                        t tVar = uVar.f1607e;
                        if (tVar != null && (sVar = tVar.f1590a) != null) {
                            sVar.f1588f = true;
                        }
                    }
                    AudioTrack audioTrack3 = this.f1490v;
                    C1579o c1579o = this.f1488t.f1640a;
                    audioTrack3.setOffloadDelayPadding(c1579o.f17339G, c1579o.H);
                    this.f1470e0 = true;
                }
            } else {
                s();
                if (!m()) {
                    g();
                }
            }
            a(j8);
            zO = o();
            C c32 = this.f1480l;
            if (zO) {
            }
        }
        return false;
    }

    public final boolean m() {
        if (o()) {
            return !(x0.v.f18553a >= 29 && this.f1490v.isOffloadedPlayback() && this.f1456U) && this.f1473g.d(k());
        }
        return false;
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0025  */
    /* JADX WARN: Removed duplicated region for block: B:83:0x019b  */
    /* JADX WARN: Removed duplicated region for block: B:94:? A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean n() throws p {
        boolean z8;
        AudioTrack audioTrackC;
        C0071h c0071h;
        C0.l lVar;
        boolean z9;
        C c3 = this.f1480l;
        if (c3.f1430a == null) {
            z8 = false;
        } else {
            synchronized (f1435j0) {
                z9 = f1437l0 > 0;
            }
            if (z9 || SystemClock.elapsedRealtime() < c3.f1432c) {
                z8 = true;
            }
        }
        if (z8) {
            return false;
        }
        try {
            z zVar = this.f1488t;
            zVar.getClass();
            audioTrackC = c(zVar);
        } catch (p e8) {
            z zVar2 = this.f1488t;
            if (zVar2.f1647h > 1000000) {
                z zVar3 = new z(zVar2.f1640a, zVar2.f1641b, zVar2.f1642c, zVar2.f1643d, zVar2.f1644e, zVar2.f1645f, zVar2.f1646g, 1000000, zVar2.i, zVar2.f1648j, zVar2.f1649k, zVar2.f1650l);
                try {
                    audioTrackC = c(zVar3);
                    this.f1488t = zVar3;
                } catch (p e9) {
                    e8.addSuppressed(e9);
                    if (this.f1488t.f1642c == 1) {
                        throw e8;
                    }
                    this.f1468d0 = true;
                    throw e8;
                }
            }
            if (this.f1488t.f1642c == 1) {
            }
        }
        this.f1490v = audioTrackC;
        if (p(audioTrackC)) {
            AudioTrack audioTrack = this.f1490v;
            if (this.f1479k == null) {
                this.f1479k = new A1.y(this);
            }
            A1.y yVar = this.f1479k;
            Handler handler = (Handler) yVar.f363r;
            Objects.requireNonNull(handler);
            audioTrack.registerStreamEventCallback(new D(handler), (E) yVar.f364s);
            z zVar4 = this.f1488t;
            if (zVar4.f1649k) {
                AudioTrack audioTrack2 = this.f1490v;
                C1579o c1579o = zVar4.f1640a;
                audioTrack2.setOffloadDelayPadding(c1579o.f17339G, c1579o.H);
            }
        }
        int i = x0.v.f18553a;
        if (i >= 31 && (lVar = this.f1485q) != null) {
            AudioTrack audioTrack3 = this.f1490v;
            LogSessionId logSessionIdA = lVar.a();
            LogSessionId unused = LogSessionId.LOG_SESSION_ID_NONE;
            if (!logSessionIdA.equals(LogSessionId.LOG_SESSION_ID_NONE)) {
                audioTrack3.setLogSessionId(logSessionIdA);
            }
        }
        this.X = this.f1490v.getAudioSessionId();
        AudioTrack audioTrack4 = this.f1490v;
        z zVar5 = this.f1488t;
        boolean z10 = zVar5.f1642c == 2;
        int i5 = zVar5.f1646g;
        int i8 = zVar5.f1643d;
        int i9 = zVar5.f1647h;
        u uVar = this.f1473g;
        uVar.f1605c = audioTrack4;
        uVar.f1606d = i9;
        uVar.f1607e = new t(audioTrack4);
        uVar.f1608f = audioTrack4.getSampleRate();
        uVar.f1609g = z10 && i < 23 && (i5 == 5 || i5 == 6);
        boolean zH = x0.v.H(i5);
        uVar.f1617p = zH;
        uVar.f1610h = zH ? x0.v.Q(i9 / i8, uVar.f1608f) : -9223372036854775807L;
        uVar.f1620s = 0L;
        uVar.f1621t = 0L;
        uVar.f1601G = false;
        uVar.H = 0L;
        uVar.f1622u = 0L;
        uVar.f1616o = false;
        uVar.f1625x = -9223372036854775807L;
        uVar.f1626y = -9223372036854775807L;
        uVar.f1618q = 0L;
        uVar.f1615n = 0L;
        uVar.i = 1.0f;
        if (o()) {
            this.f1490v.setVolume(this.f1450O);
        }
        this.f1459Y.getClass();
        K4.c cVar = this.f1460Z;
        if (cVar != null && i >= 23) {
            this.f1490v.setPreferredDevice((AudioDeviceInfo) cVar.f3604r);
            C0071h c0071h2 = this.f1492x;
            if (c0071h2 != null) {
                c0071h2.b((AudioDeviceInfo) this.f1460Z.f3604r);
            }
        }
        if (i >= 24 && (c0071h = this.f1492x) != null) {
            this.f1493y = new A1.y(this.f1490v, c0071h);
        }
        this.f1448M = true;
        K4.c cVar2 = this.f1486r;
        if (cVar2 != null) {
            n nVarA = this.f1488t.a();
            m mVar = ((H) cVar2.f3604r).f1498T0;
            Handler handler2 = mVar.f1569b;
            if (handler2 != null) {
                handler2.post(new RunnableC0074k(mVar, nVarA, 1));
            }
        }
        return true;
    }

    public final boolean o() {
        return this.f1490v != null;
    }

    public final void q() {
        C0067d c0067dB;
        C0068e c0068e;
        if (this.f1492x == null) {
            Context context = this.f1461a;
            this.f1472f0 = Looper.myLooper();
            C0071h c0071h = new C0071h(context, new A1.G(11, this), this.f1494z, this.f1460Z);
            this.f1492x = c0071h;
            if (c0071h.f1556j) {
                c0067dB = c0071h.f1554g;
                c0067dB.getClass();
            } else {
                c0071h.f1556j = true;
                C0069f c0069f = c0071h.f1553f;
                if (c0069f != null) {
                    c0069f.f1543a.registerContentObserver(c0069f.f1544b, false, c0069f);
                }
                int i = x0.v.f18553a;
                Handler handler = c0071h.f1550c;
                Context context2 = c0071h.f1548a;
                if (i >= 23 && (c0068e = c0071h.f1551d) != null) {
                    AbstractC1664c.q(context2).registerAudioDeviceCallback(c0068e, handler);
                }
                c0067dB = C0067d.b(context2, context2.registerReceiver(c0071h.f1552e, new IntentFilter("android.media.action.HDMI_AUDIO_PLUG"), null, handler), c0071h.i, c0071h.f1555h);
                c0071h.f1554g = c0067dB;
            }
            this.f1491w = c0067dB;
        }
        this.f1491w.getClass();
    }

    public final void r() {
        this.f1457V = true;
        if (o()) {
            u uVar = this.f1473g;
            if (uVar.f1625x != -9223372036854775807L) {
                uVar.f1602I.getClass();
                uVar.f1625x = x0.v.L(SystemClock.elapsedRealtime());
            }
            t tVar = uVar.f1607e;
            tVar.getClass();
            tVar.a();
            this.f1490v.play();
        }
    }

    public final void s() {
        if (this.f1455T) {
            return;
        }
        this.f1455T = true;
        long jK = k();
        u uVar = this.f1473g;
        uVar.f1627z = uVar.b();
        uVar.f1602I.getClass();
        uVar.f1625x = x0.v.L(SystemClock.elapsedRealtime());
        uVar.f1596A = jK;
        if (p(this.f1490v)) {
            this.f1456U = false;
        }
        this.f1490v.stop();
        this.f1442F = 0;
    }

    public final void t(long j8) throws r {
        ByteBuffer byteBuffer;
        e(j8);
        if (this.f1453R != null) {
            return;
        }
        if (!this.f1489u.e()) {
            ByteBuffer byteBuffer2 = this.f1451P;
            if (byteBuffer2 != null) {
                w(byteBuffer2);
                e(j8);
                return;
            }
            return;
        }
        while (!this.f1489u.d()) {
            do {
                C1665d c1665d = this.f1489u;
                if (c1665d.e()) {
                    ByteBuffer byteBuffer3 = c1665d.f18013c[c1665d.c()];
                    if (byteBuffer3.hasRemaining()) {
                        byteBuffer = byteBuffer3;
                    } else {
                        c1665d.f(InterfaceC1668g.f18020a);
                        byteBuffer = c1665d.f18013c[c1665d.c()];
                    }
                } else {
                    byteBuffer = InterfaceC1668g.f18020a;
                }
                if (byteBuffer.hasRemaining()) {
                    w(byteBuffer);
                    e(j8);
                } else {
                    ByteBuffer byteBuffer4 = this.f1451P;
                    if (byteBuffer4 == null || !byteBuffer4.hasRemaining()) {
                        return;
                    }
                    C1665d c1665d2 = this.f1489u;
                    ByteBuffer byteBuffer5 = this.f1451P;
                    if (c1665d2.e() && !c1665d2.f18014d) {
                        c1665d2.f(byteBuffer5);
                    }
                }
            } while (this.f1453R == null);
            return;
        }
    }

    public final void u() {
        g();
        C3.E eListIterator = this.f1469e.listIterator(0);
        while (eListIterator.hasNext()) {
            ((InterfaceC1668g) eListIterator.next()).reset();
        }
        C3.E eListIterator2 = this.f1471f.listIterator(0);
        while (eListIterator2.hasNext()) {
            ((InterfaceC1668g) eListIterator2.next()).reset();
        }
        C1665d c1665d = this.f1489u;
        if (c1665d != null) {
            int i = 0;
            while (true) {
                W w4 = c1665d.f18011a;
                if (i >= w4.f1227t) {
                    break;
                }
                InterfaceC1668g interfaceC1668g = (InterfaceC1668g) w4.get(i);
                interfaceC1668g.flush();
                interfaceC1668g.reset();
                i++;
            }
            c1665d.f18013c = new ByteBuffer[0];
            C1666e c1666e = C1666e.f18015e;
            c1665d.f18014d = false;
        }
        this.f1457V = false;
        this.f1468d0 = false;
    }

    public final void v() {
        if (o()) {
            try {
                this.f1490v.setPlaybackParams(new PlaybackParams().allowDefaults().setSpeed(this.f1440C.f17159a).setPitch(this.f1440C.f17160b).setAudioFallbackMode(2));
            } catch (IllegalArgumentException e8) {
                AbstractC1709a.C("DefaultAudioSink", "Failed to set playback params", e8);
            }
            u0.H h8 = new u0.H(this.f1490v.getPlaybackParams().getSpeed(), this.f1490v.getPlaybackParams().getPitch());
            this.f1440C = h8;
            u uVar = this.f1473g;
            uVar.i = h8.f17159a;
            t tVar = uVar.f1607e;
            if (tVar != null) {
                tVar.a();
            }
            uVar.e();
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0038  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x013f  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x01d7  */
    /* JADX WARN: Removed duplicated region for block: B:82:0x01ec A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:84:0x0055 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void w(ByteBuffer byteBuffer) {
        ByteBuffer byteBufferOrder;
        int i;
        byte b8;
        int i5;
        int i8;
        AbstractC1709a.j(this.f1453R == null);
        if (byteBuffer.hasRemaining()) {
            if (this.f1488t.f1642c != 0) {
                byteBufferOrder = byteBuffer;
            } else {
                int iS = (int) x0.v.S(x0.v.L(20L), this.f1488t.f1644e, 1000000L, RoundingMode.UP);
                long jK = k();
                long j8 = iS;
                if (jK < j8) {
                    z zVar = this.f1488t;
                    int i9 = zVar.f1646g;
                    int i10 = (int) jK;
                    byteBufferOrder = ByteBuffer.allocateDirect(byteBuffer.remaining()).order(ByteOrder.nativeOrder());
                    int iPosition = byteBuffer.position();
                    while (byteBuffer.hasRemaining() && i10 < iS) {
                        if (i9 != 2) {
                            if (i9 == 3) {
                                i8 = (byteBuffer.get() & 255) << 24;
                            } else if (i9 == 4) {
                                float fH = x0.v.h(byteBuffer.getFloat(), -1.0f, 1.0f);
                                i8 = (int) (fH < 0.0f ? (-fH) * (-2.1474836E9f) : fH * 2.1474836E9f);
                            } else if (i9 == 21) {
                                i = ((byteBuffer.get() & 255) << 8) | ((byteBuffer.get() & 255) << 16);
                                b8 = byteBuffer.get();
                            } else if (i9 != 22) {
                                if (i9 == 268435456) {
                                    i = (byteBuffer.get() & 255) << 24;
                                    i5 = (byteBuffer.get() & 255) << 16;
                                } else if (i9 == 1342177280) {
                                    i = ((byteBuffer.get() & 255) << 24) | ((byteBuffer.get() & 255) << 16);
                                    i5 = (byteBuffer.get() & 255) << 8;
                                } else {
                                    if (i9 != 1610612736) {
                                        throw new IllegalStateException();
                                    }
                                    i = ((byteBuffer.get() & 255) << 24) | ((byteBuffer.get() & 255) << 16) | ((byteBuffer.get() & 255) << 8);
                                    i5 = byteBuffer.get() & 255;
                                }
                                i8 = i | i5;
                            } else {
                                i = (byteBuffer.get() & 255) | ((byteBuffer.get() & 255) << 8) | ((byteBuffer.get() & 255) << 16);
                                b8 = byteBuffer.get();
                            }
                            int i11 = (int) ((((long) i8) * ((long) i10)) / j8);
                            if (i9 != 2) {
                                byteBufferOrder.put((byte) (i11 >> 16));
                                byteBufferOrder.put((byte) (i11 >> 24));
                            } else if (i9 == 3) {
                                byteBufferOrder.put((byte) (i11 >> 24));
                            } else if (i9 != 4) {
                                if (i9 == 21) {
                                    byteBufferOrder.put((byte) (i11 >> 8));
                                    byteBufferOrder.put((byte) (i11 >> 16));
                                    byteBufferOrder.put((byte) (i11 >> 24));
                                } else if (i9 == 22) {
                                    byteBufferOrder.put((byte) i11);
                                    byteBufferOrder.put((byte) (i11 >> 8));
                                    byteBufferOrder.put((byte) (i11 >> 16));
                                    byteBufferOrder.put((byte) (i11 >> 24));
                                } else if (i9 == 268435456) {
                                    byteBufferOrder.put((byte) (i11 >> 24));
                                    byteBufferOrder.put((byte) (i11 >> 16));
                                } else if (i9 == 1342177280) {
                                    byteBufferOrder.put((byte) (i11 >> 24));
                                    byteBufferOrder.put((byte) (i11 >> 16));
                                    byteBufferOrder.put((byte) (i11 >> 8));
                                } else {
                                    if (i9 != 1610612736) {
                                        throw new IllegalStateException();
                                    }
                                    byteBufferOrder.put((byte) (i11 >> 24));
                                    byteBufferOrder.put((byte) (i11 >> 16));
                                    byteBufferOrder.put((byte) (i11 >> 8));
                                    byteBufferOrder.put((byte) i11);
                                }
                            } else if (i11 < 0) {
                                byteBufferOrder.putFloat((-i11) / (-2.1474836E9f));
                            } else {
                                byteBufferOrder.putFloat(i11 / 2.1474836E9f);
                            }
                            if (byteBuffer.position() != zVar.f1643d + iPosition) {
                                i10++;
                                iPosition = byteBuffer.position();
                            }
                        } else {
                            i = (byteBuffer.get() & 255) << 16;
                            b8 = byteBuffer.get();
                        }
                        i5 = (b8 & 255) << 24;
                        i8 = i | i5;
                        int i112 = (int) ((((long) i8) * ((long) i10)) / j8);
                        if (i9 != 2) {
                        }
                        if (byteBuffer.position() != zVar.f1643d + iPosition) {
                        }
                    }
                    byteBufferOrder.put(byteBuffer);
                    byteBufferOrder.flip();
                }
            }
            this.f1453R = byteBufferOrder;
        }
    }

    public final boolean x() {
        z zVar = this.f1488t;
        return zVar != null && zVar.f1648j && x0.v.f18553a >= 23;
    }
}
