package com.google.android.gms.internal.ads;

import android.text.TextUtils;
import androidx.media3.common.C;
import androidx.media3.datasource.RawResourceDataSource;
import androidx.media3.exoplayer.DefaultLoadControl;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes2.dex */
public final class zg2 implements fi2 {

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public static final zzguf f19265p;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final tj f19266a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final gj f19267b;

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

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

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

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

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

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

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

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

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

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

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

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

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

    static {
        bs1 bs1Var = zzguf.f19771b;
        Object[] objArr = {"file", "content", "data", "android.resource", RawResourceDataSource.RAW_RESOURCE_SCHEME, "asset"};
        ih1.t(6, objArr);
        f19265p = zzguf.u(6, objArr);
    }

    public zg2() {
        r rVar = new r();
        zzgui zzguiVar = zzgwf.f19797g;
        j(1000, 0, "bufferForPlaybackMs", SessionDescription.SUPPORTED_SDP_VERSION);
        j(1000, 0, "bufferForPlaybackForLocalPlaybackMs", SessionDescription.SUPPORTED_SDP_VERSION);
        j(2000, 0, "bufferForPlaybackAfterRebufferMs", SessionDescription.SUPPORTED_SDP_VERSION);
        j(1000, 0, "bufferForPlaybackAfterRebufferForLocalPlaybackMs", SessionDescription.SUPPORTED_SDP_VERSION);
        j(50000, 1000, "minBufferMs", "bufferForPlaybackMs");
        j(1000, 1000, "minBufferForLocalPlaybackMs", "bufferForPlaybackForLocalPlaybackMs");
        j(50000, 2000, "minBufferMs", "bufferForPlaybackAfterRebufferMs");
        j(1000, 1000, "minBufferForLocalPlaybackMs", "bufferForPlaybackAfterRebufferForLocalPlaybackMs");
        j(50000, 50000, "maxBufferMs", "minBufferMs");
        j(50000, 1000, "maxBufferForLocalPlaybackMs", "minBufferForLocalPlaybackMs");
        j(0, 0, "backBufferDurationMs", SessionDescription.SUPPORTED_SDP_VERSION);
        this.f19266a = new tj();
        this.f19267b = new gj();
        this.f19268c = rVar;
        this.f19269d = bb1.p(50000L);
        this.f19270e = bb1.p(1000L);
        this.f19271f = bb1.p(50000L);
        this.f19272g = bb1.p(50000L);
        this.f19273h = bb1.p(1000L);
        this.f19274i = bb1.p(1000L);
        this.f19275j = bb1.p(ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MS);
        this.f19276k = bb1.p(1000L);
        this.f19277l = bb1.p(0L);
        this.f19279n = new ConcurrentHashMap();
        this.f19278m = zzgui.a(zzguiVar);
        this.f19280o = -1L;
    }

    public static void j(int i10, int i11, String str, String str2) {
        if (!(i10 >= i11)) {
            throw new IllegalArgumentException(ih1.B0("%s cannot be less than %s", str, str2));
        }
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final void a(mk2 mk2Var) {
        long id2 = Thread.currentThread().getId();
        long j10 = this.f19280o;
        ih1.f1("Players that share the same LoadControl must share the same playback thread. See ExoPlayer.Builder.setPlaybackLooper(Looper).", j10 == -1 || j10 == id2);
        this.f19280o = id2;
        ConcurrentHashMap concurrentHashMap = this.f19279n;
        yg2 yg2Var = (yg2) concurrentHashMap.get(mk2Var);
        if (yg2Var == null) {
            yg2 yg2Var2 = new yg2();
            yg2Var2.f18836a = 1;
            concurrentHashMap.put(mk2Var, yg2Var2);
        } else {
            yg2Var.f18836a++;
        }
        yg2 yg2Var3 = (yg2) concurrentHashMap.get(mk2Var);
        yg2Var3.getClass();
        Integer num = (Integer) this.f19278m.get(mk2Var.f14244a);
        int iIntValue = (num == null || num.intValue() == -1) ? -1 : num.intValue();
        if (iIntValue == -1) {
            iIntValue = 13107200;
        }
        yg2Var3.f18838c = iIntValue;
        yg2Var3.f18837b = false;
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final void b(mk2 mk2Var) {
        ConcurrentHashMap concurrentHashMap = this.f19279n;
        yg2 yg2Var = (yg2) concurrentHashMap.get(mk2Var);
        if (yg2Var != null) {
            int i10 = yg2Var.f18836a - 1;
            yg2Var.f18836a = i10;
            if (i10 == 0) {
                concurrentHashMap.remove(mk2Var);
                h();
            }
        }
        if (concurrentHashMap.isEmpty()) {
            this.f19280o = -1L;
        }
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final boolean c(ei2 ei2Var) {
        long jMin;
        boolean z10;
        int i10;
        boolean z11 = ei2Var.f10808f;
        boolean zI = i(ei2Var);
        long jRound = ei2Var.f10806d;
        float f10 = ei2Var.f10807e;
        String str = bb1.f9474a;
        if (f10 != 1.0f) {
            jRound = Math.round(jRound / ((double) f10));
        }
        if (z11) {
            if (zI) {
                jMin = this.f19276k;
                z10 = true;
            } else {
                jMin = this.f19275j;
                z10 = false;
            }
        } else if (zI) {
            jMin = this.f19274i;
            z10 = true;
        } else {
            jMin = this.f19273h;
            z10 = false;
        }
        long j10 = ei2Var.f10809g;
        if (j10 != -9223372036854775807L) {
            jMin = Math.min(j10 / 2, jMin);
        }
        if (jMin > 0 && jRound < jMin) {
            if (z10) {
                return false;
            }
            mk2 mk2Var = ei2Var.f10803a;
            yg2 yg2Var = (yg2) this.f19279n.get(mk2Var);
            yg2Var.getClass();
            synchronized (yg2Var) {
                i10 = yg2Var.f18839d;
            }
            int i11 = i10 * C.DEFAULT_BUFFER_SEGMENT_SIZE;
            yg2 yg2Var2 = (yg2) this.f19279n.get(mk2Var);
            yg2Var2.getClass();
            if (i11 < yg2Var2.f18838c) {
                return false;
            }
        }
        return true;
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final void d(mk2 mk2Var) {
        ConcurrentHashMap concurrentHashMap = this.f19279n;
        yg2 yg2Var = (yg2) concurrentHashMap.get(mk2Var);
        if (yg2Var != null) {
            int i10 = yg2Var.f18836a - 1;
            yg2Var.f18836a = i10;
            if (i10 == 0) {
                concurrentHashMap.remove(mk2Var);
                h();
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final boolean e(ei2 ei2Var) {
        int i10;
        ConcurrentHashMap concurrentHashMap = this.f19279n;
        mk2 mk2Var = ei2Var.f10803a;
        yg2 yg2Var = (yg2) concurrentHashMap.get(mk2Var);
        yg2Var.getClass();
        yg2 yg2Var2 = (yg2) this.f19279n.get(mk2Var);
        yg2Var2.getClass();
        synchronized (yg2Var2) {
            i10 = yg2Var2.f18839d;
        }
        int i11 = i10 * C.DEFAULT_BUFFER_SEGMENT_SIZE;
        yg2 yg2Var3 = (yg2) this.f19279n.get(mk2Var);
        yg2Var3.getClass();
        int i12 = yg2Var3.f18838c;
        if (mk2Var.equals(mk2.f14243b)) {
            return i11 < i12;
        }
        boolean zI = i(ei2Var);
        long jMin = zI ? this.f19270e : this.f19269d;
        long j10 = zI ? this.f19272g : this.f19271f;
        float f10 = ei2Var.f10807e;
        if (f10 > 1.0f) {
            jMin = Math.min(bb1.t(jMin, f10), j10);
        }
        long jMax = Math.max(jMin, 500000L);
        long j11 = ei2Var.f10806d;
        if (j11 < jMax) {
            boolean z10 = zI || i11 < i12;
            yg2Var.f18837b = z10;
            if (!z10 && j11 < 500000) {
                vt0.c("Target buffer size reached with less than 500ms of buffered media data.");
            }
        } else if (j11 >= j10 || i11 >= i12) {
            yg2Var.f18837b = false;
        }
        return yg2Var.f18837b;
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final void f(ei2 ei2Var, d[] dVarArr) {
        ConcurrentHashMap concurrentHashMap = this.f19279n;
        mk2 mk2Var = ei2Var.f10803a;
        Integer num = (Integer) this.f19278m.get(mk2Var.f14244a);
        int iIntValue = (num == null || num.intValue() == -1) ? -1 : num.intValue();
        yg2 yg2Var = (yg2) concurrentHashMap.get(mk2Var);
        yg2Var.getClass();
        if (iIntValue == -1) {
            boolean zI = i(ei2Var);
            int length = dVarArr.length;
            int i10 = 0;
            int i11 = 0;
            while (true) {
                int i12 = 13107200;
                if (i10 >= length) {
                    break;
                }
                d dVar = dVarArr[i10];
                if (dVar != null) {
                    int i13 = dVar.zza().f11275c;
                    if (i13 != -1) {
                        if (i13 == 0) {
                            i12 = DefaultLoadControl.DEFAULT_MUXED_BUFFER_SIZE;
                        } else if (i13 != 1) {
                            i12 = i13 != 2 ? i13 != 4 ? 131072 : DefaultLoadControl.DEFAULT_IMAGE_BUFFER_SIZE : zI ? 19660800 : DefaultLoadControl.DEFAULT_VIDEO_BUFFER_SIZE;
                        }
                    }
                    i11 += i12;
                }
                i10++;
            }
            iIntValue = Math.max(13107200, i11);
        }
        yg2Var.f18838c = iIntValue;
        h();
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final n g(mk2 mk2Var) {
        return new xg2(this, mk2Var);
    }

    public final void h() {
        ConcurrentHashMap concurrentHashMap = this.f19279n;
        boolean zIsEmpty = concurrentHashMap.isEmpty();
        r rVar = this.f19268c;
        int i10 = 0;
        if (zIsEmpty) {
            synchronized (rVar) {
                rVar.b(0);
            }
        } else {
            Iterator it = concurrentHashMap.values().iterator();
            while (it.hasNext()) {
                i10 += ((yg2) it.next()).f18838c;
            }
            rVar.b(i10);
        }
    }

    public final boolean i(ei2 ei2Var) {
        Object obj = ei2Var.f10805c.f10025a;
        gj gjVar = this.f19267b;
        mk mkVar = ei2Var.f10804b;
        m3 m3Var = mkVar.b(mkVar.n(obj, gjVar).f11762a, this.f19266a, 0L).f16911b.zzb;
        if (m3Var == null) {
            return false;
        }
        String scheme = m3Var.f13937a.getScheme();
        return TextUtils.isEmpty(scheme) || f19265p.contains(scheme);
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final long zzf() {
        return this.f19277l;
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final void zzg() {
    }

    @Override // com.google.android.gms.internal.ads.fi2
    public final boolean zzj() {
        Iterator it = this.f19279n.values().iterator();
        while (it.hasNext()) {
            if (((yg2) it.next()).f18837b) {
                return false;
            }
        }
        return true;
    }
}
