package G0;

import L0.C0164t;
import android.net.Uri;
import android.os.Handler;
import android.os.SystemClock;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import u0.C1578n;
import u0.C1579o;
import u0.E;
import u0.F;
import x0.v;

/* JADX INFO: loaded from: classes.dex */
public final class c implements P0.h {

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public static final C0.c f2754E = new C0.c(23);

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public Uri f2755A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public k f2756B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public boolean f2757C;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final K4.c f2758q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final r f2759r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final C4.f f2760s;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public E0.e f2763v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public P0.m f2764w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public Handler f2765x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public F0.l f2766y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public n f2767z;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final CopyOnWriteArrayList f2762u = new CopyOnWriteArrayList();

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final HashMap f2761t = new HashMap();
    public long D = -9223372036854775807L;

    public c(K4.c cVar, C4.f fVar, r rVar) {
        this.f2758q = cVar;
        this.f2759r = rVar;
        this.f2760s = fVar;
    }

    public final k a(Uri uri, boolean z8) {
        HashMap map = this.f2761t;
        k kVar = ((b) map.get(uri)).f2747t;
        if (kVar != null && z8) {
            if (!uri.equals(this.f2755A)) {
                List list = this.f2767z.f2838e;
                int i = 0;
                while (true) {
                    if (i >= list.size()) {
                        break;
                    }
                    if (uri.equals(((m) list.get(i)).f2830a)) {
                        k kVar2 = this.f2756B;
                        if (kVar2 == null || !kVar2.f2818o) {
                            this.f2755A = uri;
                            b bVar = (b) map.get(uri);
                            k kVar3 = bVar.f2747t;
                            if (kVar3 == null || !kVar3.f2818o) {
                                bVar.f(b(uri));
                            } else {
                                this.f2756B = kVar3;
                                this.f2766y.t(kVar3);
                            }
                        }
                    } else {
                        i++;
                    }
                }
            }
            b bVar2 = (b) map.get(uri);
            k kVar4 = bVar2.f2747t;
            if (!bVar2.f2742A) {
                bVar2.f2742A = true;
                if (kVar4 != null && !kVar4.f2818o) {
                    bVar2.c(true);
                }
            }
        }
        return kVar;
    }

    public final Uri b(Uri uri) {
        g gVar;
        k kVar = this.f2756B;
        if (kVar == null || !kVar.f2825v.f2807e || (gVar = (g) kVar.f2823t.get(uri)) == null) {
            return uri;
        }
        Uri.Builder builderBuildUpon = uri.buildUpon();
        builderBuildUpon.appendQueryParameter("_HLS_msn", String.valueOf(gVar.f2788b));
        int i = gVar.f2789c;
        if (i != -1) {
            builderBuildUpon.appendQueryParameter("_HLS_part", String.valueOf(i));
        }
        return builderBuildUpon.build();
    }

    public final boolean c(Uri uri) {
        int i;
        b bVar = (b) this.f2761t.get(uri);
        if (bVar.f2747t == null) {
            return false;
        }
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long jMax = Math.max(30000L, v.W(bVar.f2747t.f2824u));
        k kVar = bVar.f2747t;
        return kVar.f2818o || (i = kVar.f2808d) == 2 || i == 1 || bVar.f2748u + jMax > jElapsedRealtime;
    }

    @Override // P0.h
    public final void e(P0.j jVar, long j8, long j9, int i) {
        C0164t c0164t;
        P0.o oVar = (P0.o) jVar;
        if (i == 0) {
            long j10 = oVar.f5307a;
            c0164t = new C0164t(oVar.f5308b);
        } else {
            long j11 = oVar.f5307a;
            Uri uri = oVar.f5310d.f19183s;
            c0164t = new C0164t(j9);
        }
        this.f2763v.e(c0164t, oVar.f5309c, -1, null, 0, null, -9223372036854775807L, -9223372036854775807L, i);
    }

    @Override // P0.h
    public final void g(P0.j jVar, long j8, long j9) {
        n nVar;
        P0.o oVar = (P0.o) jVar;
        o oVar2 = (o) oVar.f5312f;
        boolean z8 = oVar2 instanceof k;
        if (z8) {
            String str = oVar2.f2844a;
            n nVar2 = n.f2836l;
            Uri uri = Uri.parse(str);
            C1578n c1578n = new C1578n();
            c1578n.f17309a = "0";
            c1578n.f17319l = E.m("application/x-mpegURL");
            List listSingletonList = Collections.singletonList(new m(uri, new C1579o(c1578n), null, null, null, null));
            List list = Collections.EMPTY_LIST;
            nVar = new n("", list, listSingletonList, list, list, list, list, null, null, false, Collections.EMPTY_MAP, list);
        } else {
            nVar = (n) oVar2;
        }
        this.f2767z = nVar;
        this.f2755A = ((m) nVar.f2838e.get(0)).f2830a;
        this.f2762u.add(new a(this));
        List list2 = nVar.f2837d;
        int size = list2.size();
        for (int i = 0; i < size; i++) {
            Uri uri2 = (Uri) list2.get(i);
            this.f2761t.put(uri2, new b(this, uri2));
        }
        Uri uri3 = oVar.f5310d.f19183s;
        C0164t c0164t = new C0164t(j9);
        b bVar = (b) this.f2761t.get(this.f2755A);
        if (z8) {
            bVar.h((k) oVar2, c0164t);
        } else {
            bVar.c(false);
        }
        this.f2760s.getClass();
        this.f2763v.c(c0164t, 4, -1, null, 0, null, -9223372036854775807L, -9223372036854775807L);
    }

    @Override // P0.h
    public final B1.f n(P0.j jVar, long j8, long j9, IOException iOException, int i) {
        long jMin;
        P0.o oVar = (P0.o) jVar;
        long j10 = oVar.f5307a;
        Uri uri = oVar.f5310d.f19183s;
        C0164t c0164t = new C0164t(j9);
        this.f2760s.getClass();
        if ((iOException instanceof F) || (iOException instanceof FileNotFoundException) || (iOException instanceof z0.q) || (iOException instanceof P0.l)) {
            jMin = -9223372036854775807L;
            break;
        }
        int i5 = z0.i.f19130r;
        for (Throwable cause = iOException; cause != null; cause = cause.getCause()) {
            if ((cause instanceof z0.i) && ((z0.i) cause).f19131q == 2008) {
                jMin = -9223372036854775807L;
                break;
            }
        }
        jMin = Math.min((i - 1) * 1000, 5000);
        boolean z8 = jMin == -9223372036854775807L;
        long j11 = jMin;
        this.f2763v.d(c0164t, oVar.f5309c, -1, null, 0, null, -9223372036854775807L, -9223372036854775807L, iOException, z8);
        return z8 ? P0.m.f5303f : new B1.f(0, j11, false);
    }

    @Override // P0.h
    public final void s(P0.j jVar, long j8, long j9, boolean z8) {
        P0.o oVar = (P0.o) jVar;
        long j10 = oVar.f5307a;
        Uri uri = oVar.f5310d.f19183s;
        C0164t c0164t = new C0164t(j9);
        this.f2760s.getClass();
        this.f2763v.b(c0164t, 4, -1, null, 0, null, -9223372036854775807L, -9223372036854775807L);
    }
}
