package v;

import A.p;
import a0.InterfaceC0147b;
import a0.r;
import android.net.Uri;
import android.os.Handler;
import android.os.SystemClock;
import android.util.SparseArray;
import b0.AbstractC0399d;
import b0.D;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import n.C0633A;
import n.InterfaceC0634B;
import n.x;
import n.y;
import n.z;
import u.InterfaceC0722a;
import w.AbstractC0730a;
import x.C0734c;
import y.C0743e;
import z.AbstractC0749a;

/* JADX INFO: loaded from: classes.dex */
public final class h implements InterfaceC0634B, InterfaceC0634B.a, v.g, r.a {

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    private static final List f6578J;

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

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    private r f6580B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    private d f6581C;

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    private IOException f6582D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    private int f6583E;

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

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    private boolean f6585G;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    private int f6586H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    private int f6587I;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final e f6588a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final InterfaceC0147b f6589b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final int f6590c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final SparseArray f6591d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Uri f6593f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final a0.f f6594g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final Handler f6595h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final c f6596i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final int f6597j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private volatile boolean f6598k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private volatile m f6599l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private volatile InterfaceC0722a f6600m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private boolean f6601n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private int f6602o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private x[] f6603p;

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private boolean[] f6605r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private boolean[] f6606s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private boolean[] f6607t;

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

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

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

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

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private boolean f6612y;

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

    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            h.this.f6588a.a();
        }
    }

    class b implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ IOException f6615b;

        b(IOException iOException) {
            this.f6615b = iOException;
        }

        @Override // java.lang.Runnable
        public void run() {
            h.this.f6596i.a(h.this.f6597j, this.f6615b);
        }
    }

    public interface c {
        void a(int i2, IOException iOException);
    }

    private static class d implements r.c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final Uri f6617a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final a0.f f6618b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final e f6619c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final InterfaceC0147b f6620d;

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final j f6622f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private volatile boolean f6623g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        private boolean f6624h;

        public d(Uri uri, a0.f fVar, e eVar, InterfaceC0147b interfaceC0147b, int i2, long j2) {
            this.f6617a = (Uri) AbstractC0399d.d(uri);
            this.f6618b = (a0.f) AbstractC0399d.d(fVar);
            this.f6619c = (e) AbstractC0399d.d(eVar);
            this.f6620d = (InterfaceC0147b) AbstractC0399d.d(interfaceC0147b);
            this.f6621e = i2;
            j jVar = new j();
            this.f6622f = jVar;
            jVar.f6632a = j2;
            this.f6624h = true;
        }

        @Override // a0.r.c
        public boolean m() {
            return this.f6623g;
        }

        @Override // a0.r.c
        public void q() throws Throwable {
            int iA = 0;
            while (iA == 0 && !this.f6623g) {
                C0726b c0726b = null;
                try {
                    long j2 = this.f6622f.f6632a;
                    long jA = this.f6618b.a(new a0.h(this.f6617a, j2, -1L, null));
                    if (jA != -1) {
                        jA += j2;
                    }
                    C0726b c0726b2 = new C0726b(this.f6618b, j2, jA);
                    try {
                        v.e eVarB = this.f6619c.b(c0726b2);
                        if (this.f6624h) {
                            eVarB.f();
                            this.f6624h = false;
                        }
                        while (iA == 0 && !this.f6623g) {
                            this.f6620d.a(this.f6621e);
                            iA = eVarB.a(c0726b2, this.f6622f);
                        }
                        if (iA == 1) {
                            iA = 0;
                        } else {
                            this.f6622f.f6632a = c0726b2.j();
                        }
                        D.g(this.f6618b);
                    } catch (Throwable th) {
                        th = th;
                        c0726b = c0726b2;
                        if (iA != 1 && c0726b != null) {
                            this.f6622f.f6632a = c0726b.j();
                        }
                        D.g(this.f6618b);
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                }
            }
        }

        @Override // a0.r.c
        public void u() {
            this.f6623g = true;
        }
    }

    private static final class e {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final v.e[] f6625a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final v.g f6626b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private v.e f6627c;

        public e(v.e[] eVarArr, v.g gVar) {
            this.f6625a = eVarArr;
            this.f6626b = gVar;
        }

        public void a() {
            v.e eVar = this.f6627c;
            if (eVar != null) {
                eVar.release();
                this.f6627c = null;
            }
        }

        public v.e b(v.f fVar) throws g {
            v.e eVar = this.f6627c;
            if (eVar != null) {
                return eVar;
            }
            if (fVar == null) {
                return null;
            }
            v.e[] eVarArr = this.f6625a;
            int length = eVarArr.length;
            int i2 = 0;
            while (true) {
                if (i2 >= length) {
                    break;
                }
                v.e eVar2 = eVarArr[i2];
                try {
                    if (eVar2.g(fVar)) {
                        this.f6627c = eVar2;
                        fVar.g();
                        break;
                    }
                } catch (EOFException unused) {
                } catch (Throwable th) {
                    fVar.g();
                    throw th;
                }
                fVar.g();
                i2++;
            }
            v.e eVar3 = this.f6627c;
            if (eVar3 == null) {
                throw new g(this.f6625a);
            }
            eVar3.e(this.f6626b);
            return this.f6627c;
        }
    }

    private class f extends C0727c {
        public f(InterfaceC0147b interfaceC0147b) {
            super(interfaceC0147b);
        }

        @Override // v.C0727c, v.o
        public void e(long j2, int i2, int i3, int i4, byte[] bArr) {
            super.e(j2, i2, i3, i4, bArr);
            h.this.f6586H++;
        }
    }

    public static final class g extends z {
        public g(v.e[] eVarArr) {
            super("None of the available extractors (" + D.p(eVarArr) + ") could read the stream.");
        }
    }

    static {
        ArrayList arrayList = new ArrayList();
        f6578J = arrayList;
        try {
            int i2 = C.g.f209d0;
            arrayList.add(C.g.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused) {
        }
        try {
            List list = f6578J;
            int i3 = C0743e.f6967D;
            list.add(C0743e.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused2) {
        }
        try {
            List list2 = f6578J;
            int i4 = y.f.f7001d;
            list2.add(y.f.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused3) {
        }
        try {
            List list3 = f6578J;
            int i5 = C0734c.f6746p;
            list3.add(C0734c.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused4) {
        }
        try {
            List list4 = f6578J;
            int i6 = A.b.f11g;
            list4.add(A.b.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused5) {
        }
        try {
            List list5 = f6578J;
            int i7 = p.f151q;
            list5.add(p.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused6) {
        }
        try {
            List list6 = f6578J;
            int i8 = AbstractC0730a.f6729c;
            list6.add(AbstractC0730a.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused7) {
        }
        try {
            f6578J.add(AbstractC0749a.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused8) {
        }
        try {
            f6578J.add(A.m.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused9) {
        }
        try {
            f6578J.add(B.a.class.asSubclass(v.e.class));
        } catch (ClassNotFoundException unused10) {
        }
        try {
            f6578J.add(Class.forName("com.google.android.exoplayer.ext.flac.FlacExtractor").asSubclass(v.e.class));
        } catch (ClassNotFoundException unused11) {
        }
    }

    public h(Uri uri, a0.f fVar, InterfaceC0147b interfaceC0147b, int i2, Handler handler, c cVar, int i3, v.e... eVarArr) {
        this(uri, fVar, interfaceC0147b, i2, -1, handler, cVar, i3, eVarArr);
    }

    private d A(long j2) {
        return new d(this.f6593f, this.f6594g, this.f6588a, this.f6589b, this.f6590c, this.f6599l.c(j2));
    }

    private d B() {
        return new d(this.f6593f, this.f6594g, this.f6588a, this.f6589b, this.f6590c, 0L);
    }

    private void C(long j2) {
        int i2 = 0;
        while (true) {
            boolean[] zArr = this.f6607t;
            if (i2 >= zArr.length) {
                return;
            }
            if (!zArr[i2]) {
                ((f) this.f6591d.valueAt(i2)).j(j2);
            }
            i2++;
        }
    }

    private long D(long j2) {
        return Math.min((j2 - 1) * 1000, 5000L);
    }

    private boolean E() {
        for (int i2 = 0; i2 < this.f6591d.size(); i2++) {
            if (!((f) this.f6591d.valueAt(i2)).r()) {
                return false;
            }
        }
        return true;
    }

    private boolean F() {
        return this.f6582D instanceof g;
    }

    private boolean G() {
        return this.f6611x != Long.MIN_VALUE;
    }

    private void H() {
        if (this.f6585G || this.f6580B.d()) {
            return;
        }
        int i2 = 0;
        if (this.f6582D == null) {
            this.f6579A = 0L;
            this.f6612y = false;
            if (this.f6601n) {
                AbstractC0399d.e(G());
                long j2 = this.f6604q;
                if (j2 != -1 && this.f6611x >= j2) {
                    this.f6585G = true;
                    this.f6611x = Long.MIN_VALUE;
                    return;
                } else {
                    this.f6581C = A(this.f6611x);
                    this.f6611x = Long.MIN_VALUE;
                }
            } else {
                this.f6581C = B();
            }
            this.f6587I = this.f6586H;
            this.f6580B.g(this.f6581C, this);
            return;
        }
        if (F()) {
            return;
        }
        AbstractC0399d.e(this.f6581C != null);
        if (SystemClock.elapsedRealtime() - this.f6584F >= D(this.f6583E)) {
            this.f6582D = null;
            if (!this.f6601n) {
                while (i2 < this.f6591d.size()) {
                    ((f) this.f6591d.valueAt(i2)).h();
                    i2++;
                }
                this.f6581C = B();
            } else if (!this.f6599l.b() && this.f6604q == -1) {
                while (i2 < this.f6591d.size()) {
                    ((f) this.f6591d.valueAt(i2)).h();
                    i2++;
                }
                this.f6581C = B();
                this.f6613z = this.f6609v;
                this.f6612y = true;
            }
            this.f6587I = this.f6586H;
            this.f6580B.g(this.f6581C, this);
        }
    }

    private void I(IOException iOException) {
        Handler handler = this.f6595h;
        if (handler == null || this.f6596i == null) {
            return;
        }
        handler.post(new b(iOException));
    }

    private void J(long j2) {
        this.f6611x = j2;
        this.f6585G = false;
        if (this.f6580B.d()) {
            this.f6580B.c();
        } else {
            z();
            H();
        }
    }

    private void z() {
        for (int i2 = 0; i2 < this.f6591d.size(); i2++) {
            ((f) this.f6591d.valueAt(i2)).h();
        }
        this.f6581C = null;
        this.f6582D = null;
        this.f6583E = 0;
    }

    @Override // n.InterfaceC0634B.a
    public x a(int i2) {
        AbstractC0399d.e(this.f6601n);
        return this.f6603p[i2];
    }

    @Override // v.g
    public void b(m mVar) {
        this.f6599l = mVar;
    }

    @Override // n.InterfaceC0634B.a
    public int c() {
        return this.f6591d.size();
    }

    @Override // n.InterfaceC0634B.a
    public void d(int i2, long j2) {
        AbstractC0399d.e(this.f6601n);
        AbstractC0399d.e(!this.f6607t[i2]);
        int i3 = this.f6602o + 1;
        this.f6602o = i3;
        this.f6607t[i2] = true;
        this.f6605r[i2] = true;
        this.f6606s[i2] = false;
        if (i3 == 1) {
            if (!this.f6599l.b()) {
                j2 = 0;
            }
            this.f6609v = j2;
            this.f6610w = j2;
            J(j2);
        }
    }

    @Override // n.InterfaceC0634B.a
    public void e() throws IOException {
        if (this.f6582D == null) {
            return;
        }
        if (F()) {
            throw this.f6582D;
        }
        int i2 = this.f6592e;
        if (i2 == -1) {
            i2 = (this.f6599l == null || this.f6599l.b()) ? 3 : 6;
        }
        if (this.f6583E > i2) {
            throw this.f6582D;
        }
    }

    @Override // n.InterfaceC0634B.a
    public boolean f(int i2, long j2) {
        AbstractC0399d.e(this.f6601n);
        AbstractC0399d.e(this.f6607t[i2]);
        this.f6609v = j2;
        C(j2);
        if (this.f6585G) {
            return true;
        }
        H();
        if (G()) {
            return false;
        }
        return !((f) this.f6591d.valueAt(i2)).s();
    }

    @Override // v.g
    public void g(InterfaceC0722a interfaceC0722a) {
        this.f6600m = interfaceC0722a;
    }

    @Override // n.InterfaceC0634B.a
    public int h() {
        v.e eVarB = null;
        try {
            eVarB = this.f6588a.b(null);
        } catch (IOException | InterruptedException unused) {
        }
        if (eVarB == null || !(eVarB instanceof n)) {
            return (this.f6599l == null || !this.f6599l.b()) ? 0 : 8;
        }
        return 16;
    }

    @Override // v.g
    public o i(int i2) {
        f fVar = (f) this.f6591d.get(i2);
        if (fVar != null) {
            return fVar;
        }
        f fVar2 = new f(this.f6589b);
        this.f6591d.put(i2, fVar2);
        return fVar2;
    }

    @Override // n.InterfaceC0634B.a
    public long j(int i2) {
        boolean[] zArr = this.f6606s;
        if (!zArr[i2]) {
            return Long.MIN_VALUE;
        }
        zArr[i2] = false;
        return this.f6610w;
    }

    @Override // n.InterfaceC0634B.a
    public void k(int i2) {
        AbstractC0399d.e(this.f6601n);
        AbstractC0399d.e(this.f6607t[i2]);
        int i3 = this.f6602o - 1;
        this.f6602o = i3;
        this.f6607t[i2] = false;
        if (i3 == 0) {
            this.f6609v = Long.MIN_VALUE;
            if (this.f6580B.d()) {
                this.f6580B.c();
            } else {
                z();
                this.f6589b.f(0);
            }
        }
    }

    @Override // a0.r.a
    public void l(r.c cVar) {
        this.f6585G = true;
    }

    @Override // v.g
    public void m() {
        this.f6598k = true;
    }

    @Override // a0.r.a
    public void n(r.c cVar, IOException iOException) {
        this.f6582D = iOException;
        this.f6583E = this.f6586H <= this.f6587I ? 1 + this.f6583E : 1;
        this.f6584F = SystemClock.elapsedRealtime();
        I(iOException);
        H();
    }

    @Override // n.InterfaceC0634B.a
    public void o(long j2) {
        AbstractC0399d.e(this.f6601n);
        int i2 = 0;
        AbstractC0399d.e(this.f6602o > 0);
        if (!this.f6599l.b()) {
            j2 = 0;
        }
        long j3 = G() ? this.f6611x : this.f6609v;
        this.f6609v = j2;
        this.f6610w = j2;
        if (j3 == j2) {
            return;
        }
        boolean zU = !G();
        for (int i3 = 0; zU && i3 < this.f6591d.size(); i3++) {
            zU &= ((f) this.f6591d.valueAt(i3)).u(j2);
        }
        if (!zU) {
            J(j2);
        }
        while (true) {
            boolean[] zArr = this.f6606s;
            if (i2 >= zArr.length) {
                return;
            }
            zArr[i2] = true;
            i2++;
        }
    }

    @Override // n.InterfaceC0634B
    public InterfaceC0634B.a p() {
        this.f6608u++;
        return this;
    }

    @Override // n.InterfaceC0634B.a
    public int r(int i2, long j2, y yVar, C0633A c0633a) {
        this.f6609v = j2;
        if (!this.f6606s[i2] && !G()) {
            f fVar = (f) this.f6591d.valueAt(i2);
            if (this.f6605r[i2]) {
                yVar.f5857a = fVar.m();
                yVar.f5858b = this.f6600m;
                this.f6605r[i2] = false;
                return -4;
            }
            if (c0633a != null && fVar.p(c0633a)) {
                long j3 = c0633a.f5412e;
                boolean z2 = j3 < this.f6610w;
                c0633a.f5411d = (z2 ? 134217728 : 0) | c0633a.f5411d;
                if (this.f6612y) {
                    this.f6579A = this.f6613z - j3;
                    this.f6612y = false;
                }
                c0633a.f5412e = j3 + this.f6579A;
                return -3;
            }
            if (this.f6585G) {
                return -1;
            }
        }
        return -2;
    }

    @Override // n.InterfaceC0634B.a
    public void release() {
        r rVar;
        AbstractC0399d.e(this.f6608u > 0);
        int i2 = this.f6608u - 1;
        this.f6608u = i2;
        if (i2 != 0 || (rVar = this.f6580B) == null) {
            return;
        }
        rVar.f(new a());
        this.f6580B = null;
    }

    @Override // n.InterfaceC0634B.a
    public boolean s(long j2) {
        if (this.f6601n) {
            return true;
        }
        if (this.f6580B == null) {
            this.f6580B = new r("Loader:ExtractorSampleSource");
        }
        H();
        if (this.f6599l == null || !this.f6598k || !E()) {
            return false;
        }
        int size = this.f6591d.size();
        this.f6607t = new boolean[size];
        this.f6606s = new boolean[size];
        this.f6605r = new boolean[size];
        this.f6603p = new x[size];
        this.f6604q = -1L;
        for (int i2 = 0; i2 < size; i2++) {
            x xVarM = ((f) this.f6591d.valueAt(i2)).m();
            this.f6603p[i2] = xVarM;
            long j3 = xVarM.f5856y;
            if (j3 != -1 && j3 > this.f6604q) {
                this.f6604q = j3;
            }
        }
        this.f6601n = true;
        return true;
    }

    @Override // n.InterfaceC0634B.a
    public long t() {
        if (this.f6585G) {
            return -3L;
        }
        if (G()) {
            return this.f6611x;
        }
        long jMax = Long.MIN_VALUE;
        for (int i2 = 0; i2 < this.f6591d.size(); i2++) {
            jMax = Math.max(jMax, ((f) this.f6591d.valueAt(i2)).n());
        }
        return jMax == Long.MIN_VALUE ? this.f6609v : jMax;
    }

    @Override // a0.r.a
    public void v(r.c cVar) {
        if (this.f6602o > 0) {
            J(this.f6611x);
        } else {
            z();
            this.f6589b.f(0);
        }
    }

    public h(Uri uri, a0.f fVar, InterfaceC0147b interfaceC0147b, int i2, int i3, Handler handler, c cVar, int i4, v.e... eVarArr) {
        this.f6593f = uri;
        this.f6594g = fVar;
        this.f6596i = cVar;
        this.f6595h = handler;
        this.f6597j = i4;
        this.f6589b = interfaceC0147b;
        this.f6590c = i2;
        this.f6592e = i3;
        if (eVarArr == null || eVarArr.length == 0) {
            int size = f6578J.size();
            eVarArr = new v.e[size];
            for (int i5 = 0; i5 < size; i5++) {
                try {
                    eVarArr[i5] = (v.e) ((Class) f6578J.get(i5)).newInstance();
                } catch (IllegalAccessException e2) {
                    throw new IllegalStateException("Unexpected error creating default extractor", e2);
                } catch (InstantiationException e3) {
                    throw new IllegalStateException("Unexpected error creating default extractor", e3);
                }
            }
        }
        this.f6588a = new e(eVarArr, this);
        this.f6591d = new SparseArray();
        this.f6611x = Long.MIN_VALUE;
    }
}
