package I;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import androidx.core.location.LocationRequestCompat;
import b0.AbstractC0399d;
import b0.o;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import n.C;
import n.C0633A;
import n.C0643i;
import n.InterfaceC0634B;
import n.x;
import n.y;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public final class m extends C implements Handler.Callback {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    private final String f985P;

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

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    private String f987R;

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private final l f989r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private final y f990s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private final i[] f991t;

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

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

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

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

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

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

    static {
        ArrayList arrayList = new ArrayList();
        f969S = arrayList;
        try {
            arrayList.add(Z.c.class.asSubclass(i.class));
        } catch (ClassNotFoundException unused) {
        }
        try {
            List list = f969S;
            int i2 = U.a.f1695f;
            list.add(U.a.class.asSubclass(i.class));
        } catch (ClassNotFoundException unused2) {
        }
        try {
            List list2 = f969S;
            int i3 = Z.a.f1906d;
            list2.add(Z.a.class.asSubclass(i.class));
        } catch (ClassNotFoundException unused3) {
        }
        try {
            List list3 = f969S;
            int i4 = T.a.f1661b;
            list3.add(T.a.class.asSubclass(i.class));
        } catch (ClassNotFoundException unused4) {
        }
        try {
            f969S.add(W.a.class.asSubclass(i.class));
        } catch (ClassNotFoundException unused5) {
        }
        try {
            f969S.add(P.a.class.asSubclass(i.class));
        } catch (ClassNotFoundException unused6) {
        }
    }

    public m(InterfaceC0634B interfaceC0634B, l lVar, Looper looper, i... iVarArr) {
        this(HttpUrl.FRAGMENT_ENCODE_SET, new InterfaceC0634B[]{interfaceC0634B}, lVar, looper, iVarArr);
    }

    private void N() {
        R(Collections.emptyList());
    }

    private long O() {
        int i2 = this.f973D;
        return (i2 == -1 || i2 >= this.f994w.d()) ? LocationRequestCompat.PASSIVE_INTERVAL : this.f994w.b(this.f973D);
    }

    private int P(x xVar) {
        int i2 = 0;
        while (true) {
            i[] iVarArr = this.f991t;
            if (i2 >= iVarArr.length) {
                return -1;
            }
            if (iVarArr[i2].a(xVar.f5842g)) {
                return i2;
            }
            i2++;
        }
    }

    private void Q(List list) {
        this.f989r.onCues(list);
    }

    private void R(List list) {
        Handler handler = this.f988q;
        if (handler != null) {
            handler.obtainMessage(0, list).sendToTarget();
        } else {
            Q(list);
        }
    }

    @Override // n.C
    protected void D(long j2, long j3, boolean z2) throws C0643i {
        boolean z3;
        boolean z4;
        long j4;
        List listC;
        long jB;
        boolean z5;
        boolean z6;
        long j5 = this.f986Q;
        long j6 = j2 - j5 > 0 ? j2 - j5 : 0L;
        if (this.f980K) {
            return;
        }
        long j7 = this.f979J;
        if (j7 != -1 && j6 > j7) {
            Log.d(this.f985P, "texttrackRender on targeted pts=" + this.f979J + ", posUs=" + j6);
            this.f980K = true;
            j jVar = this.f971B;
            if (jVar != null) {
                jVar.a();
            }
            N();
            return;
        }
        if (this.f995x == null) {
            try {
                this.f995x = this.f971B.b();
            } catch (IOException e2) {
                throw new C0643i(e2);
            }
        } else if (this.f996y == null && this.f997z) {
            try {
                this.f996y = this.f971B.b();
            } catch (IOException e3) {
                throw new C0643i(e3);
            }
        }
        if (l() != 3) {
            return;
        }
        if (this.f974E && !this.f976G && Math.abs(j6 - this.f975F) > 1000000) {
            this.f973D = 0;
            Log.i(this.f985P, "discontinuity playback, reset index posUs =" + j6 + ", lastPosUs =" + this.f975F);
        }
        this.f975F = j6;
        if (this.f976G) {
            f fVar = this.f995x;
            if (fVar == null || this.f994w != null) {
                z3 = false;
            } else {
                this.f994w = fVar;
                this.f995x = null;
                this.f978I = false;
                this.f973D = fVar.a(j6);
                Log.i(this.f985P, "first sbtl render idx=" + this.f973D + ",posUs=" + j6);
                z3 = true;
            }
            if (this.f994w != null) {
                f fVar2 = this.f995x;
                if (fVar2 != null) {
                    if (!this.f978I) {
                        this.f977H = fVar2.a(j6);
                        this.f978I = true;
                    }
                    int i2 = this.f977H;
                    jB = (i2 == -1 || i2 >= this.f995x.d()) ? Long.MAX_VALUE : this.f995x.b(this.f977H);
                    if (this.f994w.d() == this.f995x.d()) {
                        int i3 = 0;
                        while (i3 < this.f994w.d() && this.f995x.b(i3) == this.f994w.b(i3)) {
                            i3++;
                        }
                        if (i3 == this.f994w.d()) {
                            this.f995x = null;
                            Log.i(this.f985P, "same sample, need get new nextSubtitle.");
                        }
                    }
                } else {
                    jB = Long.MAX_VALUE;
                }
                long jO = O();
                o.d(this.f985P, "subtitle update, curPos=" + j6 + "evTime=" + jO + ",nextNvTime=" + jB, 16);
                if (jO != LocationRequestCompat.PASSIVE_INTERVAL) {
                    f fVar3 = this.f995x;
                    if (fVar3 != null) {
                        if (jO < jB || jB > j6) {
                            z5 = z3;
                            z6 = false;
                        } else {
                            z6 = true;
                            z5 = true;
                        }
                        this.f977H = fVar3.a(j6);
                        z4 = z6;
                        z3 = z5;
                    }
                    while (jO <= j6 && this.f994w != null) {
                        this.f973D++;
                        jO = O();
                        z3 = true;
                    }
                    break;
                }
                f fVar4 = this.f995x;
                this.f994w = fVar4;
                this.f995x = null;
                this.f978I = false;
                if (fVar4 != null) {
                    this.f973D = fVar4.a(j6);
                    jO = O();
                }
                z4 = false;
                while (jO <= j6) {
                    this.f973D++;
                    jO = O();
                    z3 = true;
                }
            } else {
                z4 = false;
            }
        } else if (this.f994w != null) {
            long jO2 = O();
            z3 = false;
            while (jO2 <= j6) {
                this.f973D++;
                jO2 = O();
                z3 = true;
            }
            z4 = false;
        } else {
            z3 = false;
            z4 = false;
        }
        if (this.f995x != null) {
            o.d(this.f985P, "nextSubtitle.startTimeUs =  " + this.f995x.f955a + " positionUs = " + j6, 16);
        }
        f fVar5 = this.f995x;
        if (fVar5 != null) {
            long j8 = fVar5.f955a;
            if (j8 <= j6 && !this.f976G) {
                this.f994w = fVar5;
                if (this.f997z) {
                    f fVar6 = this.f996y;
                    if (fVar6 != null) {
                        if (j8 == fVar6.f955a) {
                            if (fVar6.c(j6).size() > 0) {
                                String string = ((c) this.f996y.c(j6).get(0)).f938a.toString();
                                String string2 = ((c) this.f995x.c(j6).get(0)).f938a.toString();
                                ((c) this.f994w.c(j6).get(0)).a(string2 + " \n" + string);
                            }
                            this.f995x = null;
                            this.f996y = null;
                        } else {
                            this.f995x = fVar6;
                            this.f996y = null;
                        }
                    }
                } else {
                    this.f995x = null;
                    this.f996y = null;
                }
                this.f973D = this.f994w.a(j6);
                z3 = true;
            }
        }
        if (z3) {
            f fVar7 = this.f994w;
            if (fVar7 == null && this.f976G) {
                listC = new ArrayList();
            } else {
                listC = fVar7.c(j6);
                if (this.f981L) {
                    if (listC.isEmpty()) {
                        if (this.f982M == -1) {
                            this.f982M = j6;
                            o.c(this.f985P, 16, "record empty pgs=", Long.valueOf(j6));
                        }
                        this.f983N = true;
                    } else {
                        this.f983N = false;
                    }
                }
            }
            if (z4 && this.f995x != null) {
                for (int i4 = 0; i4 < this.f995x.c(j6).size(); i4++) {
                    listC.add((c) this.f995x.c(j6).get(i4));
                }
            }
            if (this.f970A && listC.isEmpty()) {
                o.d(this.f985P, "Empty Teletext", 16);
                j4 = -1;
            } else {
                long j9 = this.f982M;
                j4 = -1;
                if (j9 == -1) {
                    R(listC);
                } else if (!this.f983N) {
                    o.c(this.f985P, 16, "may pgs flash, wait 100ms, emptyUs=", Long.valueOf(j9));
                    this.f982M = -1L;
                    R(listC);
                }
            }
        } else {
            j4 = -1;
        }
        long j10 = this.f982M;
        if (j10 != j4 && j6 - j10 > 100000 && this.f983N) {
            R(Collections.emptyList());
            this.f982M = j4;
            o.c(this.f985P, 16, "empty pgs expired, show one empt, emptyUs=", Long.valueOf(j4), ", curUs=" + j6);
        }
        if (this.f993v) {
            return;
        }
        if ((this.f995x == null || (this.f996y == null && this.f997z)) && !this.f971B.f()) {
            C0633A c0633aC = this.f971B.c();
            c0633aC.a();
            int iK = K(j6, this.f990s, c0633aC);
            Log.d(this.f985P, "result==" + iK);
            if (iK == -4) {
                this.f971B.g(this.f990s.f5857a);
            } else if (iK == -3) {
                this.f971B.h();
            } else if (iK == -1) {
                this.f993v = true;
            }
        }
    }

    @Override // n.C
    protected boolean F(x xVar) {
        return P(xVar) != -1;
    }

    @Override // n.C
    protected void J(long j2) {
        Log.d(this.f985P, "onDiscontinuity");
        if (this.f974E) {
            this.f973D = 0;
            N();
            return;
        }
        this.f993v = false;
        this.f994w = null;
        this.f995x = null;
        this.f996y = null;
        N();
        this.f982M = -1L;
        j jVar = this.f971B;
        if (jVar != null) {
            jVar.a();
        }
        if (!this.f981L || this.f984O == null) {
            return;
        }
        Log.d(this.f985P, "onDiscontinuity and reset IntPgsParser.");
        ((O.a) this.f984O).g();
    }

    @Override // n.C, n.G, n.j.a
    public void b(int i2, Object obj) throws C0643i {
        o.d(this.f985P, "messageType = " + i2 + " message = " + obj, 16);
        if (i2 == 1003) {
            i iVar = this.f991t[this.f992u];
            if (iVar instanceof e) {
                String str = (String) obj;
                ((e) iVar).d(str);
                this.f987R = str;
                return;
            }
            return;
        }
        if (i2 == 1005) {
            this.f986Q = ((Long) obj).longValue();
            return;
        }
        if (i2 != 1025) {
            super.b(i2, obj);
            return;
        }
        this.f979J = ((Long) obj).longValue();
        this.f980K = false;
        Log.d(this.f985P, "hbbtv set text targeted render pts=" + this.f979J);
    }

    @Override // n.C, n.G
    protected long g() {
        return -3L;
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        if (message.what != 0) {
            return false;
        }
        Q((List) message.obj);
        return true;
    }

    @Override // n.G
    protected boolean n() {
        return this.f993v && (this.f994w == null || O() == LocationRequestCompat.PASSIVE_INTERVAL);
    }

    @Override // n.G
    protected boolean o() {
        return true;
    }

    @Override // n.C, n.G
    protected void q(long j2) {
        this.f994w = null;
        Log.d(this.f985P, "onDisabled");
        this.f995x = null;
        this.f996y = null;
        this.f972C.quit();
        this.f972C = null;
        this.f971B = null;
        this.f974E = false;
        this.f982M = -1L;
        this.f981L = false;
        N();
        super.q(j2);
    }

    @Override // n.C, n.G
    protected void r(int i2, long j2, boolean z2) {
        x xVarJ;
        super.r(i2, j2, z2);
        Log.d(this.f985P, "onEnabled,track== " + i2 + " ,mSubtitleEncodingType == " + this.f987R);
        this.f992u = j(i2) != null ? P(j(i2)) : -1;
        HandlerThread handlerThread = new HandlerThread("textParser");
        this.f972C = handlerThread;
        handlerThread.start();
        int i3 = this.f992u;
        if (i3 == -1) {
            Log.e(this.f985P, "Invalid parserIndex");
            return;
        }
        String str = this.f987R;
        if (str != null) {
            i iVar = this.f991t[i3];
            if (iVar instanceof e) {
                ((e) iVar).d(str);
            }
        }
        if ((this.f991t[this.f992u] instanceof k) && (xVarJ = j(i2)) != null) {
            ((k) this.f991t[this.f992u]).d(xVarJ.r());
        }
        this.f974E = j(i2) != null ? j(i2).f5842g.startsWith("text/ext") : false;
        Log.d(this.f985P, "onEnabled,subtitleParsers[parserIndex] == " + this.f991t[this.f992u] + ", external subtitle=" + this.f974E);
        if (this.f991t[this.f992u] instanceof R.a) {
            this.f997z = true;
        } else {
            this.f997z = false;
        }
        Log.d(this.f985P, "onEnabled,isParseThirdSubtitle == " + this.f997z + ", trackCount=" + m() + ", currentSource trackCount=" + E(i2));
        if (this.f991t[this.f992u] instanceof Y.d) {
            int iM = i2 - (m() - E(i2));
            Log.d(this.f985P, "VobSubParser, index=" + iM);
            ((Y.d) this.f991t[this.f992u]).f(iM >= 0 ? iM : 0);
        }
        if (this.f991t[this.f992u] instanceof V.b) {
            this.f970A = true;
            if (j(i2).r().containsKey("dvb-teletext-page-number")) {
                int integer = j(i2).r().getInteger("dvb-teletext-page-number");
                ((V.b) this.f991t[this.f992u]).d(integer);
                Log.i(this.f985P, "set TeletextParser pageNum:" + integer);
            }
        }
        i iVar2 = this.f991t[this.f992u];
        if (iVar2 instanceof O.a) {
            this.f981L = true;
            ((O.a) iVar2).g();
        }
        if (this.f991t[this.f992u] instanceof S.b) {
            int i4 = j(i2).f5851o0;
            Log.d(this.f985P, "SubParser, frameRate==" + i4);
            if (i4 != -1) {
                ((S.b) this.f991t[this.f992u]).e(i4);
            }
        }
        this.f984O = this.f991t[this.f992u];
        this.f971B = new j(this.f972C.getLooper(), this.f991t[this.f992u]);
        this.f975F = 0L;
    }

    public m(String str, InterfaceC0634B[] interfaceC0634BArr, l lVar, Looper looper, i... iVarArr) {
        super(str, interfaceC0634BArr);
        this.f970A = false;
        this.f974E = false;
        this.f975F = 0L;
        this.f976G = false;
        this.f978I = false;
        this.f979J = -1L;
        this.f980K = false;
        this.f981L = false;
        this.f982M = -1L;
        this.f983N = false;
        this.f986Q = 0L;
        this.f987R = null;
        String str2 = "TextTrackRenderer" + str;
        this.f985P = str2;
        this.f989r = (l) AbstractC0399d.d(lVar);
        this.f988q = looper != null ? new Handler(looper, this) : null;
        Log.d(str2, "TextTrackRenderer");
        if (iVarArr == null || iVarArr.length == 0) {
            int size = f969S.size();
            iVarArr = new i[size];
            for (int i2 = 0; i2 < size; i2++) {
                try {
                    iVarArr[i2] = (i) ((Class) f969S.get(i2)).newInstance();
                } catch (IllegalAccessException e2) {
                    throw new IllegalStateException("Unexpected error creating default parser", e2);
                } catch (InstantiationException e3) {
                    throw new IllegalStateException("Unexpected error creating default parser", e3);
                }
            }
        }
        this.f991t = iVarArr;
        this.f990s = new y();
        this.f974E = false;
    }
}
