package H;

import H.c;
import a0.y;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import b0.AbstractC0399d;
import b0.D;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import n.z;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import y.g;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static String f887b;

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

    private static abstract class a {

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

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

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

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final List f892d = new LinkedList();

        public a(a aVar, String str, String str2) {
            this.f891c = aVar;
            this.f889a = str;
            this.f890b = str2;
        }

        private a e(a aVar, String str, String str2) {
            if ("QualityLevel".equals(str)) {
                return new f(aVar, str2);
            }
            if ("Protection".equals(str)) {
                return new c(aVar, str2);
            }
            if ("StreamIndex".equals(str)) {
                return new e(aVar, str2);
            }
            return null;
        }

        protected void a(Object obj) {
        }

        protected abstract Object b();

        protected final Object c(String str) {
            for (int i2 = 0; i2 < this.f892d.size(); i2++) {
                Pair pair = (Pair) this.f892d.get(i2);
                if (((String) pair.first).equals(str)) {
                    return pair.second;
                }
            }
            a aVar = this.f891c;
            if (aVar == null) {
                return null;
            }
            return aVar.c(str);
        }

        protected boolean d(String str) {
            return false;
        }

        public final Object f(XmlPullParser xmlPullParser) throws XmlPullParserException, IOException {
            boolean z2 = false;
            int i2 = 0;
            while (true) {
                int eventType = xmlPullParser.getEventType();
                if (eventType == 1) {
                    return null;
                }
                if (eventType == 2) {
                    String name = xmlPullParser.getName();
                    if (this.f890b.equals(name)) {
                        n(xmlPullParser);
                        z2 = true;
                    } else if (z2) {
                        if (i2 > 0) {
                            i2++;
                        } else if (d(name)) {
                            n(xmlPullParser);
                        } else {
                            a aVarE = e(this, name, this.f889a);
                            if (aVarE == null) {
                                i2 = 1;
                            } else {
                                a(aVarE.f(xmlPullParser));
                            }
                        }
                    }
                } else if (eventType != 3) {
                    if (eventType == 4 && z2 && i2 == 0) {
                        o(xmlPullParser);
                    }
                } else if (!z2) {
                    continue;
                } else if (i2 > 0) {
                    i2--;
                } else {
                    String name2 = xmlPullParser.getName();
                    h(xmlPullParser);
                    if (!d(name2)) {
                        return b();
                    }
                }
                xmlPullParser.next();
            }
        }

        protected final boolean g(XmlPullParser xmlPullParser, String str, boolean z2) {
            String attributeValue = xmlPullParser.getAttributeValue(null, str);
            return attributeValue != null ? Boolean.parseBoolean(attributeValue) : z2;
        }

        protected void h(XmlPullParser xmlPullParser) {
        }

        protected final int i(XmlPullParser xmlPullParser, String str, int i2) throws z {
            String attributeValue = xmlPullParser.getAttributeValue(null, str);
            if (attributeValue == null) {
                return i2;
            }
            try {
                return Integer.parseInt(attributeValue);
            } catch (NumberFormatException e2) {
                throw new z(e2);
            }
        }

        protected final long j(XmlPullParser xmlPullParser, String str, long j2) throws z {
            String attributeValue = xmlPullParser.getAttributeValue(null, str);
            if (attributeValue == null) {
                return j2;
            }
            try {
                return Long.parseLong(attributeValue);
            } catch (NumberFormatException e2) {
                throw new z(e2);
            }
        }

        protected final int k(XmlPullParser xmlPullParser, String str) throws z {
            String attributeValue = xmlPullParser.getAttributeValue(null, str);
            if (attributeValue == null) {
                throw new b(str);
            }
            try {
                return Integer.parseInt(attributeValue);
            } catch (NumberFormatException e2) {
                throw new z(e2);
            }
        }

        protected final long l(XmlPullParser xmlPullParser, String str) throws z {
            String attributeValue = xmlPullParser.getAttributeValue(null, str);
            if (attributeValue == null) {
                throw new b(str);
            }
            try {
                return Long.parseLong(attributeValue);
            } catch (NumberFormatException e2) {
                throw new z(e2);
            }
        }

        protected final String m(XmlPullParser xmlPullParser, String str) throws b {
            String attributeValue = xmlPullParser.getAttributeValue(null, str);
            if (attributeValue != null) {
                return attributeValue;
            }
            throw new b(str);
        }

        protected abstract void n(XmlPullParser xmlPullParser);

        protected void o(XmlPullParser xmlPullParser) {
        }

        protected final void p(String str, Object obj) {
            this.f892d.add(Pair.create(str, obj));
        }
    }

    public static class b extends z {
        public b(String str) {
            super("Missing required field: " + str);
        }
    }

    private static class c extends a {

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

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

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

        public c(a aVar, String str) {
            super(aVar, str, "Protection");
        }

        private static String q(String str) {
            return (str.charAt(0) == '{' && str.charAt(str.length() - 1) == '}') ? str.substring(1, str.length() - 1) : str;
        }

        private static String r(String str) {
            return (str.charAt(str.length() + (-1)) == 'L' || str.charAt(str.length() + (-1)) == 'l') ? str.substring(0, str.length() - 1) : str;
        }

        @Override // H.d.a
        public Object b() {
            UUID uuid = this.f894f;
            return new c.a(uuid, g.a(uuid, this.f895g));
        }

        @Override // H.d.a
        public boolean d(String str) {
            return "ProtectionHeader".equals(str);
        }

        @Override // H.d.a
        public void h(XmlPullParser xmlPullParser) {
            if ("ProtectionHeader".equals(xmlPullParser.getName())) {
                this.f893e = false;
            }
        }

        @Override // H.d.a
        public void n(XmlPullParser xmlPullParser) {
            if ("ProtectionHeader".equals(xmlPullParser.getName())) {
                this.f893e = true;
                this.f894f = UUID.fromString(r(q(xmlPullParser.getAttributeValue(null, "SystemID"))));
            }
        }

        @Override // H.d.a
        public void o(XmlPullParser xmlPullParser) {
            if (this.f893e) {
                this.f895g = Base64.decode(xmlPullParser.getText(), 0);
                if (TextUtils.isEmpty(d.f887b)) {
                    return;
                }
                int length = d.f887b.length();
                byte[] bytes = d.f887b.getBytes();
                this.f895g = new byte[length * 2];
                for (int i2 = 0; i2 < length; i2++) {
                    byte[] bArr = this.f895g;
                    int i3 = i2 * 2;
                    bArr[i3] = bytes[i2];
                    bArr[i3 + 1] = 0;
                }
            }
        }
    }

    /* JADX INFO: renamed from: H.d$d, reason: collision with other inner class name */
    private static class C0010d extends a {

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

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

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

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

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

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

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

        /* JADX INFO: renamed from: l, reason: collision with root package name */
        private c.a f903l;

        /* JADX INFO: renamed from: m, reason: collision with root package name */
        private List f904m;

        public C0010d(a aVar, String str) {
            super(aVar, str, "SmoothStreamingMedia");
            this.f901j = -1;
            this.f903l = null;
            this.f904m = new LinkedList();
        }

        @Override // H.d.a
        public void a(Object obj) {
            if (obj instanceof c.b) {
                this.f904m.add((c.b) obj);
            } else if (obj instanceof c.a) {
                AbstractC0399d.e(this.f903l == null);
                this.f903l = (c.a) obj;
            }
        }

        @Override // H.d.a
        public Object b() {
            c.b[] bVarArr = new c.b[this.f904m.size()];
            this.f904m.toArray(bVarArr);
            return new H.c(this.f896e, this.f897f, this.f898g, this.f899h, this.f900i, this.f901j, this.f902k, this.f903l, bVarArr);
        }

        @Override // H.d.a
        public void n(XmlPullParser xmlPullParser) {
            this.f896e = k(xmlPullParser, "MajorVersion");
            this.f897f = k(xmlPullParser, "MinorVersion");
            this.f898g = j(xmlPullParser, "TimeScale", 10000000L);
            this.f899h = l(xmlPullParser, "Duration");
            this.f900i = j(xmlPullParser, "DVRWindowLength", 0L);
            this.f901j = i(xmlPullParser, "LookaheadCount", -1);
            this.f902k = g(xmlPullParser, "IsLive", false);
            p("TimeScale", Long.valueOf(this.f898g));
        }
    }

    private static class e extends a {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        public e(a aVar, String str) {
            super(aVar, str, "StreamIndex");
            this.f905e = str;
            this.f906f = new LinkedList();
        }

        private void q(XmlPullParser xmlPullParser) throws z {
            int iS = s(xmlPullParser);
            this.f907g = iS;
            p("Type", Integer.valueOf(iS));
            if (this.f907g == 2) {
                this.f908h = m(xmlPullParser, "Subtype");
            } else {
                this.f908h = xmlPullParser.getAttributeValue(null, "Subtype");
            }
            this.f910j = xmlPullParser.getAttributeValue(null, "Name");
            this.f911k = i(xmlPullParser, "QualityLevels", -1);
            this.f912l = m(xmlPullParser, "Url");
            this.f913m = i(xmlPullParser, "MaxWidth", -1);
            this.f914n = i(xmlPullParser, "MaxHeight", -1);
            this.f915o = i(xmlPullParser, "DisplayWidth", -1);
            this.f916p = i(xmlPullParser, "DisplayHeight", -1);
            String attributeValue = xmlPullParser.getAttributeValue(null, "Language");
            this.f917q = attributeValue;
            p("Language", attributeValue);
            long jI = i(xmlPullParser, "TimeScale", -1);
            this.f909i = jI;
            if (jI == -1) {
                this.f909i = c("TimeScale") != null ? ((Long) c("TimeScale")).longValue() : 0L;
            }
            this.f918r = new ArrayList();
        }

        private void r(XmlPullParser xmlPullParser) throws z {
            int size = this.f918r.size();
            long j2 = j(xmlPullParser, "t", -1L);
            int i2 = 1;
            if (j2 == -1) {
                if (size == 0) {
                    j2 = 0;
                } else {
                    if (this.f919s == -1) {
                        throw new z("Unable to infer start time");
                    }
                    j2 = this.f919s + ((Long) this.f918r.get(size - 1)).longValue();
                }
            }
            this.f918r.add(Long.valueOf(j2));
            this.f919s = j(xmlPullParser, "d", -1L);
            long j3 = j(xmlPullParser, "r", 1L);
            if (j3 > 1 && this.f919s == -1) {
                throw new z("Repeated chunk with unspecified duration");
            }
            while (true) {
                long j4 = i2;
                if (j4 >= j3) {
                    return;
                }
                this.f918r.add(Long.valueOf((this.f919s * j4) + j2));
                i2++;
            }
        }

        private int s(XmlPullParser xmlPullParser) throws z {
            String attributeValue = xmlPullParser.getAttributeValue(null, "Type");
            if (attributeValue == null) {
                throw new b("Type");
            }
            if ("audio".equalsIgnoreCase(attributeValue)) {
                return 0;
            }
            if ("video".equalsIgnoreCase(attributeValue)) {
                return 1;
            }
            if ("text".equalsIgnoreCase(attributeValue)) {
                return 2;
            }
            throw new z("Invalid key value[" + attributeValue + "]");
        }

        @Override // H.d.a
        public void a(Object obj) {
            if (obj instanceof c.C0009c) {
                this.f906f.add((c.C0009c) obj);
            }
        }

        @Override // H.d.a
        public Object b() {
            c.C0009c[] c0009cArr = new c.C0009c[this.f906f.size()];
            this.f906f.toArray(c0009cArr);
            return new c.b(this.f905e, this.f912l, this.f907g, this.f908h, this.f909i, this.f910j, this.f911k, this.f913m, this.f914n, this.f915o, this.f916p, this.f917q, c0009cArr, this.f918r, this.f919s);
        }

        @Override // H.d.a
        public boolean d(String str) {
            return "c".equals(str);
        }

        @Override // H.d.a
        public void n(XmlPullParser xmlPullParser) throws z {
            if ("c".equals(xmlPullParser.getName())) {
                r(xmlPullParser);
            } else {
                q(xmlPullParser);
            }
        }
    }

    private static class f extends a {

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

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

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

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

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

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

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

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

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

        public f(a aVar, String str) {
            super(aVar, str, "QualityLevel");
            this.f920e = new LinkedList();
        }

        private static String q(String str) {
            if (str.equalsIgnoreCase("H264") || str.equalsIgnoreCase("X264") || str.equalsIgnoreCase("AVC1") || str.equalsIgnoreCase("DAVC")) {
                return "video/avc";
            }
            if (str.equalsIgnoreCase("AAC") || str.equalsIgnoreCase("AACL") || str.equalsIgnoreCase("AACH") || str.equalsIgnoreCase("AACP")) {
                return "audio/mp4a-latm";
            }
            if (str.equalsIgnoreCase("TTML")) {
                return "application/ttml+xml";
            }
            if (str.equalsIgnoreCase("ac-3") || str.equalsIgnoreCase("dac3")) {
                return "audio/ac3";
            }
            if (str.equalsIgnoreCase("ec-3") || str.equalsIgnoreCase("dec3")) {
                return "audio/eac3";
            }
            if (str.equalsIgnoreCase("dtsc")) {
                return "audio/vnd.dts";
            }
            if (str.equalsIgnoreCase("dtsh") || str.equalsIgnoreCase("dtsl")) {
                return "audio/vnd.dts.hd";
            }
            if (str.equalsIgnoreCase("dtse")) {
                return "audio/vnd.dts.hd;profile=lbr";
            }
            if (str.equalsIgnoreCase("opus")) {
                return "audio/opus";
            }
            if (str.equalsIgnoreCase("WVC1")) {
                return "video/wvc1";
            }
            if (str.equalsIgnoreCase("EAC3")) {
                return "audio/eac3";
            }
            if (str.equalsIgnoreCase("WMAP")) {
                return "audio/x-ms-wmapro";
            }
            return null;
        }

        @Override // H.d.a
        public Object b() {
            byte[][] bArr;
            if (this.f920e.isEmpty()) {
                bArr = null;
            } else {
                bArr = new byte[this.f920e.size()][];
                this.f920e.toArray(bArr);
            }
            return new c.C0009c(this.f921f, this.f922g, this.f923h, bArr, this.f924i, this.f925j, this.f926k, this.f927l, this.f928m);
        }

        @Override // H.d.a
        public void n(XmlPullParser xmlPullParser) {
            int iIntValue = c("Type") != null ? ((Integer) c("Type")).intValue() : -1;
            this.f921f = i(xmlPullParser, "Index", -1);
            this.f922g = k(xmlPullParser, "Bitrate");
            this.f928m = (String) c("Language");
            if (iIntValue == 1) {
                this.f925j = k(xmlPullParser, "MaxHeight");
                this.f924i = k(xmlPullParser, "MaxWidth");
                this.f923h = q(m(xmlPullParser, "FourCC"));
            } else {
                this.f925j = -1;
                this.f924i = -1;
                String attributeValue = xmlPullParser.getAttributeValue(null, "FourCC");
                this.f923h = attributeValue != null ? q(attributeValue) : iIntValue == 0 ? "audio/mp4a-latm" : null;
            }
            if (iIntValue == 0) {
                this.f926k = k(xmlPullParser, "SamplingRate");
                this.f927l = k(xmlPullParser, "Channels");
            } else {
                this.f926k = -1;
                this.f927l = -1;
            }
            String attributeValue2 = xmlPullParser.getAttributeValue(null, "CodecPrivateData");
            if (attributeValue2 == null || attributeValue2.length() <= 0) {
                return;
            }
            byte[] bArrO = D.o(attributeValue2);
            byte[][] bArrG = b0.f.g(bArrO);
            if (bArrG == null) {
                this.f920e.add(bArrO);
                return;
            }
            for (byte[] bArr : bArrG) {
                this.f920e.add(bArr);
            }
        }
    }

    public d() {
        try {
            this.f888a = XmlPullParserFactory.newInstance();
        } catch (XmlPullParserException e2) {
            throw new RuntimeException("Couldn't create XmlPullParserFactory instance", e2);
        }
    }

    @Override // a0.y.a
    /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
    public H.c a(String str, InputStream inputStream) throws z {
        try {
            XmlPullParser xmlPullParserNewPullParser = this.f888a.newPullParser();
            xmlPullParserNewPullParser.setInput(inputStream, null);
            return (H.c) new C0010d(null, str).f(xmlPullParserNewPullParser);
        } catch (XmlPullParserException e2) {
            throw new z(e2);
        }
    }

    public final void finalize() {
    }

    public d(String str) {
        this();
        f887b = str;
    }
}
