package n;

import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.Arrays;
import n.InterfaceC0634B;
import n.u;

/* JADX INFO: loaded from: classes.dex */
public abstract class C extends G {

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private int[] f5416f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int[] f5417g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private InterfaceC0634B.a f5418h;

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    protected int f5422l;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final String f5421k = "SampleSourceTrackRender";

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    protected long f5423m = -1;

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    protected int f5425o = 1;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private boolean f5426p = false;

    public C(String str, InterfaceC0634B... interfaceC0634BArr) {
        this.f5461c = str;
        this.f5415e = new InterfaceC0634B.a[interfaceC0634BArr.length];
        for (int i2 = 0; i2 < interfaceC0634BArr.length; i2++) {
            this.f5415e[i2] = interfaceC0634BArr[i2].p();
        }
    }

    private long C(long j2) {
        long j3 = this.f5418h.j(this.f5419i);
        if (j3 == Long.MIN_VALUE) {
            return j2;
        }
        if (this.f5418h instanceof G.c) {
            if (this.f5422l > 3) {
                this.f5423m = -1L;
            } else {
                this.f5423m = j2;
            }
            J(j2);
        } else {
            if (this.f5422l > 3) {
                j2 = -1;
            }
            this.f5423m = j2;
            J(j3);
        }
        return j3;
    }

    private void I(InterfaceC0634B.a aVar) throws C0643i {
        try {
            aVar.e();
        } catch (IOException e2) {
            throw new C0643i(e2);
        }
    }

    protected abstract void D(long j2, long j3, boolean z2);

    protected int E(int i2) {
        return this.f5415e[this.f5416f[i2]].c();
    }

    protected abstract boolean F(x xVar);

    protected boolean G() {
        return this.f5426p;
    }

    protected boolean H() {
        return this.f5424n;
    }

    protected abstract void J(long j2);

    protected final int K(long j2, y yVar, C0633A c0633a) {
        return this.f5418h.r(this.f5419i, j2, yVar, c0633a);
    }

    public void L(long j2) {
        this.f5423m = j2;
    }

    protected long M(long j2) {
        return j2;
    }

    @Override // n.G, n.j.a
    public void b(int i2, Object obj) throws C0643i {
        if (i2 == 1002 || i2 == 1012) {
            Bundle bundle = (Bundle) obj;
            if (bundle.containsKey("rendererSpeed")) {
                this.f5422l = bundle.getInt("rendererSpeed");
                return;
            }
            return;
        }
        if (i2 == 1004) {
            if (obj != null) {
                InterfaceC0634B.a[] aVarArr = this.f5415e;
                InterfaceC0634B.a[] aVarArr2 = new InterfaceC0634B.a[aVarArr.length + 1];
                System.arraycopy(aVarArr, 0, aVarArr2, 0, aVarArr.length);
                aVarArr2[this.f5415e.length] = ((InterfaceC0634B) obj).p();
                this.f5415e = aVarArr2;
                d(0L);
                return;
            }
            return;
        }
        if (i2 == 1006) {
            if (obj != null) {
                this.f5424n = ((Boolean) obj).booleanValue();
            }
        } else if (i2 == 1007) {
            this.f5425o = ((Integer) obj).intValue();
        } else if (i2 != 1018) {
            super.b(i2, obj);
        } else if (obj != null) {
            this.f5426p = ((Boolean) obj).booleanValue();
        }
    }

    @Override // n.G
    protected final boolean d(long j2) throws C0643i {
        InterfaceC0634B.a[] aVarArr;
        boolean z2;
        int i2;
        int i3;
        int i4;
        int i5 = 0;
        int i6 = 0;
        boolean zS = true;
        while (true) {
            InterfaceC0634B.a[] aVarArr2 = this.f5415e;
            if (i6 >= aVarArr2.length) {
                break;
            }
            zS &= aVarArr2[i6].s(j2);
            i6++;
        }
        if (!zS) {
            return false;
        }
        int i7 = 0;
        int iC = 0;
        while (true) {
            aVarArr = this.f5415e;
            if (i7 >= aVarArr.length) {
                break;
            }
            iC += aVarArr[i7].c();
            i7++;
        }
        int[] iArr = new int[iC];
        int[] iArr2 = new int[iC];
        int length = aVarArr.length;
        long jMax = 0;
        int i8 = 0;
        int i9 = 0;
        int i10 = 0;
        int i11 = 0;
        while (i8 < length) {
            InterfaceC0634B.a aVar = this.f5415e[i8];
            int iC2 = aVar.c();
            int i12 = i5;
            while (i12 < iC2) {
                x xVarA = aVar.a(i12);
                if (xVarA == null) {
                    Log.e("SampleSourceTrackRender", "getFormat is null!");
                    i2 = length;
                } else {
                    i2 = length;
                    if (xVarA.f5842g.contains("video")) {
                        i9++;
                    }
                    if (xVarA.f5842g.contains("audio")) {
                        i10++;
                    }
                    try {
                        if (F(xVarA)) {
                            iArr[i11] = i8;
                            iArr2[i11] = i12;
                            if (!xVarA.f5842g.equals("video/x-unknown") && !xVarA.f5842g.equals("audio/x-unknown")) {
                                i11++;
                            }
                            if (jMax == -1) {
                                i3 = i9;
                                i4 = i10;
                            } else {
                                i3 = i9;
                                i4 = i10;
                                long j3 = xVarA.f5856y;
                                if (j3 == -1) {
                                    i9 = i3;
                                    i10 = i4;
                                    jMax = -1;
                                } else if (j3 != -2) {
                                    jMax = Math.max(jMax, j3);
                                }
                            }
                            i9 = i3;
                            i10 = i4;
                        }
                    } catch (u.b e2) {
                        throw new C0643i(e2);
                    }
                }
                i12++;
                length = i2;
            }
            i8++;
            i5 = 0;
        }
        Log.i("SampleSourceTrackRender", "videoTrackCount " + i9 + ", audioTrackCount " + i10 + ", handledTrackCount " + i11);
        if ((this instanceof w) && i11 == 0 && i9 == 1 && i10 > 0) {
            throw new C0643i(new Exception("no available video track."), 0);
        }
        if (this instanceof q) {
            z2 = true;
            if (i10 == 1 && i9 > 0 && i11 == 0) {
                throw new C0643i(new Exception("no available audio track"), 1);
            }
        } else {
            z2 = true;
        }
        this.f5420j = jMax;
        this.f5416f = Arrays.copyOf(iArr, i11);
        this.f5417g = Arrays.copyOf(iArr2, i11);
        return z2;
    }

    @Override // n.G
    protected final void e(long j2, long j3) {
        long jM = M(j2);
        D(C(jM), j3, this.f5418h.f(this.f5419i, jM));
    }

    @Override // n.G
    protected long g() {
        return this.f5418h.t();
    }

    @Override // n.G
    public int h() {
        InterfaceC0634B.a aVar = this.f5418h;
        if (aVar != null) {
            return aVar.h();
        }
        return 0;
    }

    @Override // n.G
    protected long i() {
        return this.f5420j;
    }

    @Override // n.G
    protected final x j(int i2) {
        return this.f5415e[this.f5416f[i2]].a(this.f5417g[i2]);
    }

    @Override // n.G
    protected final int m() {
        return this.f5417g.length;
    }

    @Override // n.G
    protected void p() throws C0643i {
        InterfaceC0634B.a aVar = this.f5418h;
        if (aVar != null) {
            I(aVar);
            return;
        }
        int length = this.f5415e.length;
        for (int i2 = 0; i2 < length; i2++) {
            I(this.f5415e[i2]);
        }
    }

    @Override // n.G
    protected void q(long j2) {
        InterfaceC0634B.a aVar = this.f5418h;
        if (aVar instanceof G.c) {
            ((G.c) aVar).b(this.f5419i, j2);
        } else {
            aVar.k(this.f5419i);
        }
        this.f5418h = null;
    }

    @Override // n.G
    protected void r(int i2, long j2, boolean z2) {
        long jM = M(j2);
        InterfaceC0634B.a aVar = this.f5415e[this.f5416f[i2]];
        this.f5418h = aVar;
        int i3 = this.f5417g[i2];
        this.f5419i = i3;
        aVar.d(i3, jM);
        String str = j(i2) == null ? null : j(i2).f5842g;
        if (!(this.f5418h instanceof G.c) || (str != null && b0.k.g(str))) {
            this.f5423m = jM;
        }
        J(jM);
    }

    @Override // n.G
    protected void s() {
        int length = this.f5415e.length;
        for (int i2 = 0; i2 < length; i2++) {
            this.f5415e[i2].release();
        }
    }

    @Override // n.G
    protected void x(long j2) {
        this.f5423m = this.f5422l > 3 ? -1L : j2;
        long jM = M(j2);
        this.f5418h.o(jM);
        C(jM);
    }

    @Override // n.G
    public void y(int i2) {
        super.y(i2);
        int i3 = 0;
        while (true) {
            InterfaceC0634B.a[] aVarArr = this.f5415e;
            if (i3 >= aVarArr.length) {
                return;
            }
            InterfaceC0634B.a aVar = aVarArr[i3];
            if (aVar instanceof G.c) {
                ((G.c) aVar).i(i2);
            }
            i3++;
        }
    }
}
