package com.applovin.impl; import android.net.Uri; import android.os.Handler; import androidx.work.WorkRequest; import com.applovin.impl.a7; import com.applovin.impl.bj; import com.applovin.impl.ce; import com.applovin.impl.f9; import com.applovin.impl.ij; import com.applovin.impl.l5; import com.applovin.impl.mc; import com.applovin.impl.oc; import com.applovin.impl.ta; import com.applovin.impl.wd; import com.mbridge.msdk.playercommon.exoplayer2.C; import java.io.IOException; import java.io.InterruptedIOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; /* loaded from: classes.dex */ final class ai implements wd, m8, oc.b, oc.f, bj.d { private static final Map N = l(); private static final f9 O = new f9.b().c("icy").f("application/x-icy").a(); private boolean B; private boolean D; private boolean E; private int F; private long H; private boolean J; private int K; private boolean L; private boolean M; private final Uri a; private final i5 b; private final b7 c; private final mc d; private final ce.a f; private final a7.a g; private final b h; private final n0 i; private final String j; private final long k; private final zh m; private wd.a r; private va s; private boolean v; private boolean w; private boolean x; private e y; private ij z; private final oc l = new oc("ProgressiveMediaPeriod"); private final c4 n = new c4(); private final Runnable o = new Runnable() { // from class: com.applovin.impl.ai$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { ai.this.r(); } }; private final Runnable p = new Runnable() { // from class: com.applovin.impl.ai$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { ai.this.q(); } }; private final Handler q = xp.a(); private d[] u = new d[0]; private bj[] t = new bj[0]; private long I = C.TIME_UNSET; private long G = -1; private long A = C.TIME_UNSET; private int C = 1; public interface b { void a(long j, boolean z, boolean z2); } private static Map l() { HashMap hashMap = new HashMap(); hashMap.put("Icy-MetaData", "1"); return Collections.unmodifiableMap(hashMap); } private boolean p() { return this.I != C.TIME_UNSET; } private void u() { a aVar = new a(this.a, this.b, this.m, this, this.n); if (this.w) { b1.b(p()); long j = this.A; if (j != C.TIME_UNSET && this.I > j) { this.L = true; this.I = C.TIME_UNSET; return; } aVar.a(((ij) b1.a(this.z)).b(this.I).a.b, this.I); for (bj bjVar : this.t) { bjVar.c(this.I); } this.I = C.TIME_UNSET; } this.K = m(); this.f.c(new nc(aVar.a, aVar.k, this.l.a(aVar, this, this.d.a(this.C))), 1, -1, null, 0, null, aVar.j, this.A); } @Override // com.applovin.impl.wd public void c(long j) { } public ai(Uri uri, i5 i5Var, zh zhVar, b7 b7Var, a7.a aVar, mc mcVar, ce.a aVar2, b bVar, n0 n0Var, String str, int i) { this.a = uri; this.b = i5Var; this.c = b7Var; this.g = aVar; this.d = mcVar; this.f = aVar2; this.h = bVar; this.i = n0Var; this.j = str; this.k = i; this.m = zhVar; } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void q() { if (this.M) { return; } ((wd.a) b1.a(this.r)).a((pj) this); } public void t() { if (this.w) { for (bj bjVar : this.t) { bjVar.k(); } } this.l.a(this); this.q.removeCallbacksAndMessages(null); this.r = null; this.M = true; } @Override // com.applovin.impl.wd public void f() { s(); if (this.L && !this.w) { throw dh.a("Loading finished before preparation is complete.", null); } } @Override // com.applovin.impl.wd public long g() { if (this.F == 0) { return Long.MIN_VALUE; } return e(); } @Override // com.applovin.impl.wd public long h() { if (!this.E) { return C.TIME_UNSET; } if (!this.L && m() <= this.K) { return C.TIME_UNSET; } this.E = false; return this.H; } @Override // com.applovin.impl.wd public boolean b(long j) { if (this.L || this.l.c() || this.J) { return false; } if (this.w && this.F == 0) { return false; } boolean e2 = this.n.e(); if (this.l.d()) { return e2; } u(); return true; } @Override // com.applovin.impl.wd public long e() { long j; k(); boolean[] zArr = this.y.b; if (this.L) { return Long.MIN_VALUE; } if (p()) { return this.I; } if (this.x) { int length = this.t.length; j = Long.MAX_VALUE; for (int i = 0; i < length; i++) { if (zArr[i] && !this.t[i].i()) { j = Math.min(j, this.t[i].c()); } } } else { j = Long.MAX_VALUE; } if (j == Long.MAX_VALUE) { j = n(); } return j == Long.MIN_VALUE ? this.H : j; } public void s() { this.l.a(this.d.a(this.C)); } public void d(int i) { this.t[i].j(); s(); } private boolean v() { return this.E || p(); } @Override // com.applovin.impl.wd public po b() { k(); return this.y.a; } public qo o() { return a(new d(0, true)); } /* JADX INFO: Access modifiers changed from: private */ public void r() { bf a2; if (this.M || this.w || !this.v || this.z == null) { return; } for (bj bjVar : this.t) { if (bjVar.f() == null) { return; } } this.n.c(); int length = this.t.length; oo[] ooVarArr = new oo[length]; boolean[] zArr = new boolean[length]; for (int i = 0; i < length; i++) { f9 f9Var = (f9) b1.a(this.t[i].f()); String str = f9Var.m; boolean g = Cif.g(str); boolean z = g || Cif.i(str); zArr[i] = z; this.x = z | this.x; va vaVar = this.s; if (vaVar != null) { if (g || this.u[i].b) { bf bfVar = f9Var.k; if (bfVar == null) { a2 = new bf(vaVar); } else { a2 = bfVar.a(vaVar); } f9Var = f9Var.a().a(a2).a(); } if (g && f9Var.g == -1 && f9Var.h == -1 && vaVar.a != -1) { f9Var = f9Var.a().b(vaVar.a).a(); } } ooVarArr[i] = new oo(f9Var.a(this.c.a(f9Var))); } this.y = new e(new po(ooVarArr), zArr); this.w = true; ((wd.a) b1.a(this.r)).a((wd) this); } @Override // com.applovin.impl.m8 public void c() { this.v = true; this.q.post(this.o); } @Override // com.applovin.impl.oc.f public void d() { for (bj bjVar : this.t) { bjVar.l(); } this.m.a(); } private int m() { int i = 0; for (bj bjVar : this.t) { i += bjVar.g(); } return i; } /* JADX INFO: Access modifiers changed from: private */ public long n() { long j = Long.MIN_VALUE; for (bj bjVar : this.t) { j = Math.max(j, bjVar.c()); } return j; } private void k() { b1.b(this.w); b1.a(this.y); b1.a(this.z); } public final class c implements cj { private final int a; public c(int i) { this.a = i; } @Override // com.applovin.impl.cj public boolean d() { return ai.this.a(this.a); } @Override // com.applovin.impl.cj public void a() { ai.this.d(this.a); } @Override // com.applovin.impl.cj public int a(g9 g9Var, p5 p5Var, int i) { return ai.this.a(this.a, g9Var, p5Var, i); } @Override // com.applovin.impl.cj public int a(long j) { return ai.this.a(this.a, j); } } public final class a implements oc.e, ta.a { private final Uri b; private final fl c; private final zh d; private final m8 e; private final c4 f; private volatile boolean h; private long j; private qo m; private boolean n; private final th g = new th(); private boolean i = true; private long l = -1; private final long a = nc.a(); private l5 k = a(0); @Override // com.applovin.impl.oc.e public void b() { this.h = true; } public a(Uri uri, i5 i5Var, zh zhVar, m8 m8Var, c4 c4Var) { this.b = uri; this.c = new fl(i5Var); this.d = zhVar; this.e = m8Var; this.f = c4Var; } @Override // com.applovin.impl.oc.e public void a() { int i = 0; while (i == 0 && !this.h) { try { long j = this.g.a; l5 a = a(j); this.k = a; long a2 = this.c.a(a); this.l = a2; if (a2 != -1) { this.l = a2 + j; } ai.this.s = va.a(this.c.e()); g5 g5Var = this.c; if (ai.this.s != null && ai.this.s.g != -1) { g5Var = new ta(this.c, ai.this.s.g, this); qo o = ai.this.o(); this.m = o; o.a(ai.O); } long j2 = j; this.d.a(g5Var, this.b, this.c.e(), j, this.l, this.e); if (ai.this.s != null) { this.d.c(); } if (this.i) { this.d.a(j2, this.j); this.i = false; } while (true) { long j3 = j2; while (i == 0 && !this.h) { try { this.f.a(); i = this.d.a(this.g); j2 = this.d.b(); if (j2 > ai.this.k + j3) { break; } } catch (InterruptedException unused) { throw new InterruptedIOException(); } } this.f.c(); ai.this.q.post(ai.this.p); } if (i == 1) { i = 0; } else if (this.d.b() != -1) { this.g.a = this.d.b(); } xp.a((i5) this.c); } catch (Throwable th) { if (i != 1 && this.d.b() != -1) { this.g.a = this.d.b(); } xp.a((i5) this.c); throw th; } } } @Override // com.applovin.impl.ta.a public void a(bh bhVar) { long max = !this.n ? this.j : Math.max(ai.this.n(), this.j); int a = bhVar.a(); qo qoVar = (qo) b1.a(this.m); qoVar.a(bhVar, a); qoVar.a(max, 1, a, 0, null); this.n = true; } /* JADX INFO: Access modifiers changed from: private */ public void a(long j, long j2) { this.g.a = j; this.j = j2; this.i = true; this.n = false; } private l5 a(long j) { return new l5.b().a(this.b).a(j).a(ai.this.j).a(6).a(ai.N).a(); } } private boolean a(a aVar, int i) { ij ijVar; if (this.G != -1 || ((ijVar = this.z) != null && ijVar.d() != C.TIME_UNSET)) { this.K = i; return true; } if (this.w && !v()) { this.J = true; return false; } this.E = this.w; this.H = 0L; this.K = 0; for (bj bjVar : this.t) { bjVar.n(); } aVar.a(0L, 0L); return true; } public static final class e { public final po a; public final boolean[] b; public final boolean[] c; public final boolean[] d; public e(po poVar, boolean[] zArr) { this.a = poVar; this.b = zArr; int i = poVar.a; this.c = new boolean[i]; this.d = new boolean[i]; } } public static final class d { public final int a; public final boolean b; public int hashCode() { return (this.a * 31) + (this.b ? 1 : 0); } public d(int i, boolean z) { this.a = i; this.b = z; } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || d.class != obj.getClass()) { return false; } d dVar = (d) obj; return this.a == dVar.a && this.b == dVar.b; } } private void c(int i) { k(); boolean[] zArr = this.y.b; if (this.J && zArr[i]) { if (this.t[i].a(false)) { return; } this.I = 0L; this.J = false; this.E = true; this.H = 0L; this.K = 0; for (bj bjVar : this.t) { bjVar.n(); } ((wd.a) b1.a(this.r)).a((pj) this); } } private void a(a aVar) { if (this.G == -1) { this.G = aVar.l; } } private void b(int i) { k(); e eVar = this.y; boolean[] zArr = eVar.d; if (zArr[i]) { return; } f9 a2 = eVar.a.a(i).a(0); this.f.a(Cif.e(a2.m), a2, 0, (Object) null, this.H); zArr[i] = true; } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: c, reason: merged with bridge method [inline-methods] */ public void b(ij ijVar) { this.z = this.s == null ? ijVar : new ij.b(C.TIME_UNSET); this.A = ijVar.d(); boolean z = this.G == -1 && ijVar.d() == C.TIME_UNSET; this.B = z; this.C = z ? 7 : 1; this.h.a(this.A, ijVar.b(), this.B); if (this.w) { return; } r(); } @Override // com.applovin.impl.wd public void a(long j, boolean z) { k(); if (p()) { return; } boolean[] zArr = this.y.c; int length = this.t.length; for (int i = 0; i < length; i++) { this.t[i].b(j, z, zArr[i]); } } @Override // com.applovin.impl.wd public long a(long j, jj jjVar) { k(); if (!this.z.b()) { return 0L; } ij.a b2 = this.z.b(j); return jjVar.a(j, b2.a.a, b2.b.a); } @Override // com.applovin.impl.wd public boolean a() { return this.l.d() && this.n.d(); } public boolean a(int i) { return !v() && this.t[i].a(this.L); } @Override // com.applovin.impl.oc.b public void a(a aVar, long j, long j2, boolean z) { fl flVar = aVar.c; nc ncVar = new nc(aVar.a, aVar.k, flVar.h(), flVar.i(), j, j2, flVar.g()); this.d.a(aVar.a); this.f.a(ncVar, 1, -1, null, 0, null, aVar.j, this.A); if (z) { return; } a(aVar); for (bj bjVar : this.t) { bjVar.n(); } if (this.F > 0) { ((wd.a) b1.a(this.r)).a((pj) this); } } @Override // com.applovin.impl.oc.b public void a(a aVar, long j, long j2) { ij ijVar; if (this.A == C.TIME_UNSET && (ijVar = this.z) != null) { boolean b2 = ijVar.b(); long n = n(); long j3 = n == Long.MIN_VALUE ? 0L : n + WorkRequest.MIN_BACKOFF_MILLIS; this.A = j3; this.h.a(j3, b2, this.B); } fl flVar = aVar.c; nc ncVar = new nc(aVar.a, aVar.k, flVar.h(), flVar.i(), j, j2, flVar.g()); this.d.a(aVar.a); this.f.b(ncVar, 1, -1, null, 0, null, aVar.j, this.A); a(aVar); this.L = true; ((wd.a) b1.a(this.r)).a((pj) this); } @Override // com.applovin.impl.oc.b public oc.c a(a aVar, long j, long j2, IOException iOException, int i) { boolean z; a aVar2; oc.c cVar; a(aVar); fl flVar = aVar.c; nc ncVar = new nc(aVar.a, aVar.k, flVar.h(), flVar.i(), j, j2, flVar.g()); long a2 = this.d.a(new mc.a(ncVar, new ud(1, -1, null, 0, null, t2.b(aVar.j), t2.b(this.A)), iOException, i)); if (a2 == C.TIME_UNSET) { cVar = oc.g; } else { int m = m(); if (m > this.K) { aVar2 = aVar; z = true; } else { z = false; aVar2 = aVar; } if (a(aVar2, m)) { cVar = oc.a(z, a2); } else { cVar = oc.f; } } boolean z2 = !cVar.a(); this.f.a(ncVar, 1, -1, null, 0, null, aVar.j, this.A, iOException, z2); if (z2) { this.d.a(aVar.a); } return cVar; } @Override // com.applovin.impl.bj.d public void a(f9 f9Var) { this.q.post(this.o); } @Override // com.applovin.impl.wd public void a(wd.a aVar, long j) { this.r = aVar; this.n.e(); u(); } private qo a(d dVar) { int length = this.t.length; for (int i = 0; i < length; i++) { if (dVar.equals(this.u[i])) { return this.t[i]; } } bj a2 = bj.a(this.i, this.q.getLooper(), this.c, this.g); a2.a(this); int i2 = length + 1; d[] dVarArr = (d[]) Arrays.copyOf(this.u, i2); dVarArr[length] = dVar; this.u = (d[]) xp.a((Object[]) dVarArr); bj[] bjVarArr = (bj[]) Arrays.copyOf(this.t, i2); bjVarArr[length] = a2; this.t = (bj[]) xp.a((Object[]) bjVarArr); return a2; } public int a(int i, g9 g9Var, p5 p5Var, int i2) { if (v()) { return -3; } b(i); int a2 = this.t[i].a(g9Var, p5Var, i2, this.L); if (a2 == -3) { c(i); } return a2; } private boolean a(boolean[] zArr, long j) { int length = this.t.length; for (int i = 0; i < length; i++) { if (!this.t[i].b(j, false) && (zArr[i] || !this.x)) { return false; } } return true; } @Override // com.applovin.impl.m8 public void a(final ij ijVar) { this.q.post(new Runnable() { // from class: com.applovin.impl.ai$$ExternalSyntheticLambda2 @Override // java.lang.Runnable public final void run() { ai.this.b(ijVar); } }); } @Override // com.applovin.impl.wd public long a(long j) { k(); boolean[] zArr = this.y.b; if (!this.z.b()) { j = 0; } int i = 0; this.E = false; this.H = j; if (p()) { this.I = j; return j; } if (this.C != 7 && a(zArr, j)) { return j; } this.J = false; this.I = j; this.L = false; if (this.l.d()) { bj[] bjVarArr = this.t; int length = bjVarArr.length; while (i < length) { bjVarArr[i].b(); i++; } this.l.a(); } else { this.l.b(); bj[] bjVarArr2 = this.t; int length2 = bjVarArr2.length; while (i < length2) { bjVarArr2[i].n(); i++; } } return j; } @Override // com.applovin.impl.wd public long a(h8[] h8VarArr, boolean[] zArr, cj[] cjVarArr, boolean[] zArr2, long j) { h8 h8Var; k(); e eVar = this.y; po poVar = eVar.a; boolean[] zArr3 = eVar.c; int i = this.F; int i2 = 0; for (int i3 = 0; i3 < h8VarArr.length; i3++) { cj cjVar = cjVarArr[i3]; if (cjVar != null && (h8VarArr[i3] == null || !zArr[i3])) { int i4 = ((c) cjVar).a; b1.b(zArr3[i4]); this.F--; zArr3[i4] = false; cjVarArr[i3] = null; } } boolean z = !this.D ? j == 0 : i != 0; for (int i5 = 0; i5 < h8VarArr.length; i5++) { if (cjVarArr[i5] == null && (h8Var = h8VarArr[i5]) != null) { b1.b(h8Var.b() == 1); b1.b(h8Var.b(0) == 0); int a2 = poVar.a(h8Var.a()); b1.b(!zArr3[a2]); this.F++; zArr3[a2] = true; cjVarArr[i5] = new c(a2); zArr2[i5] = true; if (!z) { bj bjVar = this.t[a2]; z = (bjVar.b(j, true) || bjVar.e() == 0) ? false : true; } } } if (this.F == 0) { this.J = false; this.E = false; if (this.l.d()) { bj[] bjVarArr = this.t; int length = bjVarArr.length; while (i2 < length) { bjVarArr[i2].b(); i2++; } this.l.a(); } else { bj[] bjVarArr2 = this.t; int length2 = bjVarArr2.length; while (i2 < length2) { bjVarArr2[i2].n(); i2++; } } } else if (z) { j = a(j); while (i2 < cjVarArr.length) { if (cjVarArr[i2] != null) { zArr2[i2] = true; } i2++; } } this.D = true; return j; } public int a(int i, long j) { if (v()) { return 0; } b(i); bj bjVar = this.t[i]; int a2 = bjVar.a(j, this.L); bjVar.f(a2); if (a2 == 0) { c(i); } return a2; } @Override // com.applovin.impl.m8 public qo a(int i, int i2) { return a(new d(i, false)); } }