package O0;

import C3.AbstractC0055q;
import C3.D;
import C3.G;
import android.os.SystemClock;
import java.util.ArrayList;
import java.util.List;
import u0.C1579o;
import u0.T;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class b extends c {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final P0.d f4799g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final long f4800h;
    public final long i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final long f4801j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final int f4802k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final int f4803l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final float f4804m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final float f4805n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final G f4806o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final x0.q f4807p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public float f4808q;

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f4810s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public long f4811t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public M0.b f4812u;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public b(T t6, int[] iArr, P0.d dVar, long j8, long j9, long j10, G g8) {
        super(t6, iArr);
        x0.q qVar = x0.q.f18545a;
        if (j10 < j8) {
            AbstractC1709a.B("AdaptiveTrackSelection", "Adjusting minDurationToRetainAfterDiscardMs to be at least minDurationForQualityIncreaseMs");
            j10 = j8;
        }
        this.f4799g = dVar;
        this.f4800h = j8 * 1000;
        this.i = j9 * 1000;
        this.f4801j = j10 * 1000;
        this.f4802k = 1279;
        this.f4803l = 719;
        this.f4804m = 0.7f;
        this.f4805n = 0.75f;
        this.f4806o = G.k(g8);
        this.f4807p = qVar;
        this.f4808q = 1.0f;
        this.f4810s = 0;
        this.f4811t = -9223372036854775807L;
    }

    public static void u(ArrayList arrayList, long[] jArr) {
        long j8 = 0;
        for (long j9 : jArr) {
            j8 += j9;
        }
        for (int i = 0; i < arrayList.size(); i++) {
            D d8 = (D) arrayList.get(i);
            if (d8 != null) {
                d8.a(new a(j8, jArr[i]));
            }
        }
    }

    public static long w(List list) {
        if (!list.isEmpty()) {
            M0.b bVar = (M0.b) AbstractC0055q.h(list);
            long j8 = bVar.f4146g;
            if (j8 != -9223372036854775807L) {
                long j9 = bVar.f4147h;
                if (j9 != -9223372036854775807L) {
                    return j9 - j8;
                }
            }
        }
        return -9223372036854775807L;
    }

    @Override // O0.s
    public final int f() {
        return this.f4810s;
    }

    @Override // O0.s
    public final int g() {
        return this.f4809r;
    }

    @Override // O0.c, O0.s
    public final void i() {
        this.f4811t = -9223372036854775807L;
        this.f4812u = null;
    }

    @Override // O0.c, O0.s
    public final void j(float f3) {
        this.f4808q = f3;
    }

    @Override // O0.s
    public final Object l() {
        return null;
    }

    @Override // O0.c, O0.s
    public final int n(long j8, List list) {
        int i;
        int i5;
        this.f4807p.getClass();
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long j9 = this.f4811t;
        if (j9 != -9223372036854775807L && jElapsedRealtime - j9 < 1000 && (list.isEmpty() || ((M0.b) AbstractC0055q.h(list)).equals(this.f4812u))) {
            return list.size();
        }
        this.f4811t = jElapsedRealtime;
        this.f4812u = list.isEmpty() ? null : (M0.b) AbstractC0055q.h(list);
        if (list.isEmpty()) {
            return 0;
        }
        int size = list.size();
        long jZ = x0.v.z(((M0.b) list.get(size - 1)).f4146g - j8, this.f4808q);
        long j10 = this.f4801j;
        if (jZ >= j10) {
            w(list);
            C1579o c1579o = this.f4816d[v(jElapsedRealtime)];
            for (int i8 = 0; i8 < size; i8++) {
                M0.b bVar = (M0.b) list.get(i8);
                C1579o c1579o2 = bVar.f4143d;
                if (x0.v.z(bVar.f4146g - j8, this.f4808q) >= j10 && c1579o2.f17354j < c1579o.f17354j && (i = c1579o2.f17366v) != -1 && i <= this.f4803l && (i5 = c1579o2.f17365u) != -1 && i5 <= this.f4802k && i < c1579o.f17366v) {
                    return i8;
                }
            }
        }
        return size;
    }

    @Override // O0.c, O0.s
    public final void q() {
        this.f4812u = null;
    }

    @Override // O0.s
    public final void s(long j8, long j9, long j10, List list, M0.c[] cVarArr) {
        long jW;
        this.f4807p.getClass();
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        int i = this.f4809r;
        int i5 = 0;
        if (i >= cVarArr.length || !cVarArr[i].next()) {
            int length = cVarArr.length;
            int i8 = 0;
            while (true) {
                if (i8 >= length) {
                    jW = w(list);
                    break;
                }
                M0.c cVar = cVarArr[i8];
                if (cVar.next()) {
                    jW = cVar.b() - cVar.j();
                    break;
                }
                i8++;
            }
        } else {
            M0.c cVar2 = cVarArr[this.f4809r];
            jW = cVar2.b() - cVar2.j();
        }
        int i9 = this.f4810s;
        if (i9 == 0) {
            this.f4810s = 1;
            this.f4809r = v(jElapsedRealtime);
            return;
        }
        int i10 = this.f4809r;
        boolean zIsEmpty = list.isEmpty();
        C1579o[] c1579oArr = this.f4816d;
        if (zIsEmpty) {
            i5 = -1;
        } else {
            C1579o c1579o = ((M0.b) AbstractC0055q.h(list)).f4143d;
            while (i5 < this.f4814b) {
                if (c1579oArr[i5] == c1579o) {
                    break;
                } else {
                    i5++;
                }
            }
            i5 = -1;
        }
        if (i5 != -1) {
            i9 = ((M0.b) AbstractC0055q.h(list)).f4144e;
            i10 = i5;
        }
        int iV = v(jElapsedRealtime);
        if (iV != i10 && !o(jElapsedRealtime, i10)) {
            C1579o c1579o2 = c1579oArr[i10];
            C1579o c1579o3 = c1579oArr[iV];
            long jMin = this.f4800h;
            if (j10 != -9223372036854775807L) {
                jMin = Math.min((long) ((jW != -9223372036854775807L ? j10 - jW : j10) * this.f4805n), jMin);
            }
            int i11 = c1579o3.f17354j;
            int i12 = c1579o2.f17354j;
            if ((i11 > i12 && j9 < jMin) || (i11 < i12 && j9 >= this.i)) {
                iV = i10;
            }
        }
        if (iV != i10) {
            i9 = 3;
        }
        this.f4810s = i9;
        this.f4809r = iV;
    }

    public final int v(long j8) {
        long j9;
        P0.g gVar = (P0.g) this.f4799g;
        synchronized (gVar) {
            j9 = gVar.f5287l;
        }
        long j10 = (long) (j9 * this.f4804m);
        this.f4799g.getClass();
        long j11 = (long) (j10 / this.f4808q);
        if (!this.f4806o.isEmpty()) {
            int i = 1;
            while (i < this.f4806o.size() - 1 && ((a) this.f4806o.get(i)).f4797a < j11) {
                i++;
            }
            a aVar = (a) this.f4806o.get(i - 1);
            a aVar2 = (a) this.f4806o.get(i);
            long j12 = aVar.f4797a;
            long j13 = aVar.f4798b;
            j11 = j13 + ((long) (((j11 - j12) / (aVar2.f4797a - j12)) * (aVar2.f4798b - j13)));
        }
        int i5 = 0;
        for (int i8 = 0; i8 < this.f4814b; i8++) {
            if (j8 == Long.MIN_VALUE || !o(j8, i8)) {
                if (this.f4816d[i8].f17354j <= j11) {
                    return i8;
                }
                i5 = i8;
            }
        }
        return i5;
    }
}
