package D;

import D.f;
import a0.y;
import b0.D;
import com.jsyn.unitgen.u;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Pattern;
import n.z;

/* JADX INFO: loaded from: classes.dex */
public final class i implements y.a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static final Pattern f373a = Pattern.compile("BANDWIDTH=(\\d+)\\b");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final Pattern f374b = Pattern.compile("CODECS=\"(.+?)\"");

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static final Pattern f375c = Pattern.compile("RESOLUTION=(\\d+x\\d+)");

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static final Pattern f376d = Pattern.compile("#EXTINF:([\\d.]+)\\b");

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static final Pattern f377e = Pattern.compile("#EXT-X-MEDIA-SEQUENCE:(\\d+)\\b");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final Pattern f378f = Pattern.compile("#EXT-X-TARGETDURATION:(\\d+)\\b");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static final Pattern f379g = Pattern.compile("#EXT-X-VERSION:(\\d+)\\b");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static final Pattern f380h = Pattern.compile("#EXT-X-BYTERANGE:(\\d+(?:@\\d+)?)\\b");

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static final Pattern f381i = Pattern.compile("METHOD=(NONE|AES-128)");

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final Pattern f382j = Pattern.compile("URI=\"(.+?)\"");

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private static final Pattern f383k = Pattern.compile("IV=([^,.*]+)");

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static final Pattern f384l = Pattern.compile("TYPE=(AUDIO|VIDEO|SUBTITLES|CLOSED-CAPTIONS)");

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private static final Pattern f385m = Pattern.compile("LANGUAGE=\"(.+?)\"");

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private static final Pattern f386n = Pattern.compile("NAME=\"(.+?)\"");

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private static final Pattern f387o = Pattern.compile("INSTREAM-ID=\"(.+?)\"");

    private static class a {

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

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

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

        public a(Queue queue, BufferedReader bufferedReader) {
            this.f389b = queue;
            this.f388a = bufferedReader;
        }

        public boolean a() throws IOException {
            String strTrim;
            if (this.f390c != null) {
                return true;
            }
            if (!this.f389b.isEmpty()) {
                this.f390c = (String) this.f389b.poll();
                return true;
            }
            do {
                String line = this.f388a.readLine();
                this.f390c = line;
                if (line == null) {
                    return false;
                }
                strTrim = line.trim();
                this.f390c = strTrim;
            } while (strTrim.isEmpty());
            return true;
        }

        public String b() {
            if (!a()) {
                return null;
            }
            String str = this.f390c;
            this.f390c = null;
            return str;
        }
    }

    private static e c(a aVar, String str) throws z {
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        boolean z2 = false;
        int iC = 0;
        String str2 = null;
        String strD = null;
        while (true) {
            String strD2 = null;
            int i2 = -1;
            int i3 = -1;
            String strD3 = null;
            while (aVar.a()) {
                String strB = aVar.b();
                b0.o.d("HlsPlaylistParser", strB.toString(), 4);
                if (strB.startsWith("#EXT-X-MEDIA")) {
                    String strE = g.e(strB, f384l, "TYPE");
                    if ("CLOSED-CAPTIONS".equals(strE)) {
                        if ("CC1".equals(g.e(strB, f387o, "INSTREAM-ID"))) {
                            strD = g.d(strB, f385m);
                        }
                    } else if ("SUBTITLES".equals(strE)) {
                        arrayList3.add(new n(g.e(strB, f382j, "URI"), new p.j(g.e(strB, f386n, "NAME"), "application/x-mpegURL", -1, -1, -1.0f, -1, -1, -1, g.d(strB, f385m), strD2)));
                    } else if ("AUDIO".equals(strE)) {
                        String strD4 = g.d(strB, f385m);
                        String strD5 = g.d(strB, f382j);
                        if (strD5 != null) {
                            arrayList2.add(new n(strD5, new p.j(g.e(strB, f386n, "NAME"), "application/x-mpegURL", -1, -1, -1.0f, -1, -1, -1, strD4, strD2)));
                        } else {
                            str2 = strD4;
                        }
                    }
                } else if (strB.startsWith("#EXT-X-STREAM-INF")) {
                    iC = g.c(strB, f373a, "BANDWIDTH");
                    strD2 = g.d(strB, f374b);
                    strD3 = g.d(strB, f386n);
                    String strD6 = g.d(strB, f375c);
                    z2 = true;
                    if (strD6 != null) {
                        String[] strArrSplit = strD6.split("x");
                        int i4 = Integer.parseInt(strArrSplit[0]);
                        if (i4 <= 0) {
                            i4 = -1;
                        }
                        int i5 = Integer.parseInt(strArrSplit[1]);
                        if (i5 <= 0) {
                            i5 = -1;
                        }
                        i3 = i5;
                        i2 = i4;
                    } else {
                        i2 = -1;
                        i3 = -1;
                    }
                } else if (!strB.startsWith("#") && z2) {
                    arrayList.add(new n(strB, new p.j(strD3 == null ? Integer.toString(arrayList.size()) : strD3, "application/x-mpegURL", i2, i3, -1.0f, -1, -1, iC, null, strD2)));
                    z2 = false;
                    iC = 0;
                }
            }
            return new e(str, arrayList, arrayList2, arrayList3, str2, strD);
        }
    }

    private static f d(a aVar, String str) throws z {
        ArrayList arrayList = new ArrayList();
        char c2 = 0;
        int i2 = 1;
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        int iC = 0;
        boolean zEquals = false;
        int iC2 = 0;
        int iC3 = 1;
        boolean z2 = true;
        long j2 = -1;
        String strD = null;
        String str2 = null;
        long jA = 0;
        long j3 = 0;
        double dB = u.FALSE;
        long j4 = 0;
        while (aVar.a()) {
            String strB = aVar.b();
            b0.o.d("HlsPlaylistParser", strB.toString(), 4);
            if (strB.startsWith("#EXT-X-TARGETDURATION")) {
                iC = g.c(strB, f378f, "#EXT-X-TARGETDURATION");
            } else if (strB.startsWith("#EXT-X-MEDIA-SEQUENCE")) {
                if (i5 == 0) {
                    iC2 = g.c(strB, f377e, "#EXT-X-MEDIA-SEQUENCE");
                    i5 = iC2;
                } else {
                    c2 = 0;
                    i2 = 1;
                }
            } else if (strB.startsWith("#EXT-X-VERSION")) {
                iC3 = g.c(strB, f379g, "#EXT-X-VERSION");
            } else if (strB.startsWith("#EXTINF")) {
                dB = g.b(strB, f376d, "#EXTINF");
                if (iC == 0 || iC < ((int) (dB / 1000000.0d))) {
                    iC = (int) (dB / 1000000.0d);
                }
            } else if (strB.startsWith("#EXT-X-KEY")) {
                zEquals = "AES-128".equals(g.e(strB, f381i, "METHOD"));
                if (zEquals) {
                    String strE = g.e(strB, f382j, "URI");
                    strD = g.d(strB, f383k);
                    str2 = strE;
                } else {
                    strD = null;
                    str2 = null;
                }
            } else if (strB.startsWith("#EXT-X-BYTERANGE")) {
                String[] strArrSplit = g.e(strB, f380h, "#EXT-X-BYTERANGE").split("@");
                j2 = Long.parseLong(strArrSplit[c2]);
                if (strArrSplit.length > i2) {
                    j3 = Long.parseLong(strArrSplit[i2]);
                }
            } else if (strB.startsWith("#EXT-X-DISCONTINUITY-SEQUENCE")) {
                i3 = Integer.parseInt(strB.substring(strB.indexOf(58) + i2));
            } else if (strB.equals("#EXT-X-DISCONTINUITY")) {
                i3++;
            } else if (strB.startsWith("#EXT-X-PROGRAM-DATE-TIME")) {
                if (i4 == 0) {
                    jA = g.a(strB, "#EXTINF");
                    i4 = i2;
                } else {
                    c2 = 0;
                    i2 = 1;
                }
            } else if (!strB.startsWith("#")) {
                String hexString = !zEquals ? null : strD != null ? strD : Integer.toHexString(iC2);
                long j5 = j2 == -1 ? 0L : j3;
                arrayList.add(new f.a(strB, dB, i3, j4, zEquals, str2, hexString, j5, j2, iC2));
                j4 += (long) (dB * 1000000.0d);
                if (j2 != -1) {
                    j5 += j2;
                }
                j3 = j5;
                iC2++;
                j2 = -1;
                c2 = 0;
                i2 = 1;
                dB = u.FALSE;
            } else if (strB.equals("#EXT-X-ENDLIST")) {
                c2 = 0;
                i2 = 1;
                z2 = false;
            } else {
                c2 = 0;
                i2 = 1;
            }
        }
        return new f(str, i5, iC, iC3, z2, Collections.unmodifiableList(arrayList), jA);
    }

    @Override // a0.y.a
    /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
    public h a(String str, InputStream inputStream) throws z {
        String strTrim;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        LinkedList linkedList = new LinkedList();
        while (true) {
            try {
                String line = bufferedReader.readLine();
                if (line == null) {
                    D.h(bufferedReader);
                    throw new z("Failed to parse the playlist, could not identify any tags.");
                }
                strTrim = line.trim();
                if (!strTrim.isEmpty()) {
                    if (!strTrim.startsWith("#EXT-X-STREAM-INF")) {
                        if (strTrim.startsWith("#EXT-X-TARGETDURATION") || strTrim.startsWith("#EXT-X-MEDIA-SEQUENCE") || strTrim.startsWith("#EXTINF") || strTrim.startsWith("#EXT-X-KEY") || strTrim.startsWith("#EXT-X-BYTERANGE") || strTrim.equals("#EXT-X-DISCONTINUITY") || strTrim.equals("#EXT-X-DISCONTINUITY-SEQUENCE") || strTrim.equals("#EXT-X-ENDLIST")) {
                            break;
                        }
                        linkedList.add(strTrim);
                    } else {
                        linkedList.add(strTrim);
                        e eVarC = c(new a(linkedList, bufferedReader), str);
                        D.h(bufferedReader);
                        return eVarC;
                    }
                }
            } finally {
                D.h(bufferedReader);
            }
        }
        linkedList.add(strTrim);
        f fVarD = d(new a(linkedList, bufferedReader), str);
        D.h(bufferedReader);
        return fVarD;
    }
}
