package com.applovin.impl; import java.util.ArrayDeque; /* loaded from: classes.dex */ public abstract class bk implements m5 { private final Thread a; private final Object b = new Object(); private final ArrayDeque c = new ArrayDeque(); private final ArrayDeque d = new ArrayDeque(); private final p5[] e; private final zg[] f; private int g; private int h; private p5 i; private o5 j; private boolean k; private boolean l; private int m; public abstract o5 a(p5 p5Var, zg zgVar, boolean z); public abstract o5 a(Throwable th); public abstract p5 f(); public abstract zg g(); public bk(p5[] p5VarArr, zg[] zgVarArr) { this.e = p5VarArr; this.g = p5VarArr.length; for (int i = 0; i < this.g; i++) { this.e[i] = f(); } this.f = zgVarArr; this.h = zgVarArr.length; for (int i2 = 0; i2 < this.h; i2++) { this.f[i2] = g(); } a aVar = new a("ExoPlayer:SimpleDecoder"); this.a = aVar; aVar.start(); } public class a extends Thread { public a(String str) { super(str); } @Override // java.lang.Thread, java.lang.Runnable public void run() { bk.this.m(); } } @Override // com.applovin.impl.m5 /* renamed from: i, reason: merged with bridge method [inline-methods] */ public final p5 d() { p5 p5Var; synchronized (this.b) { l(); b1.b(this.i == null); int i = this.g; if (i == 0) { p5Var = null; } else { p5[] p5VarArr = this.e; int i2 = i - 1; this.g = i2; p5Var = p5VarArr[i2]; } this.i = p5Var; } return p5Var; } @Override // com.applovin.impl.m5 /* renamed from: j, reason: merged with bridge method [inline-methods] */ public final zg c() { synchronized (this.b) { try { l(); if (this.d.isEmpty()) { return null; } return (zg) this.d.removeFirst(); } catch (Throwable th) { throw th; } } } @Override // com.applovin.impl.m5 public final void a(p5 p5Var) { synchronized (this.b) { l(); b1.a(p5Var == this.i); this.c.addLast(p5Var); k(); this.i = null; } } @Override // com.applovin.impl.m5 public final void b() { synchronized (this.b) { try { this.k = true; this.m = 0; p5 p5Var = this.i; if (p5Var != null) { b(p5Var); this.i = null; } while (!this.c.isEmpty()) { b((p5) this.c.removeFirst()); } while (!this.d.isEmpty()) { ((zg) this.d.removeFirst()).g(); } } catch (Throwable th) { throw th; } } } private void l() { o5 o5Var = this.j; if (o5Var != null) { throw o5Var; } } private void k() { if (e()) { this.b.notify(); } } /* JADX INFO: Access modifiers changed from: private */ public void m() { do { try { } catch (InterruptedException e) { throw new IllegalStateException(e); } } while (h()); } private boolean h() { o5 a2; synchronized (this.b) { while (!this.l && !e()) { try { this.b.wait(); } finally { } } if (this.l) { return false; } p5 p5Var = (p5) this.c.removeFirst(); zg[] zgVarArr = this.f; int i = this.h - 1; this.h = i; zg zgVar = zgVarArr[i]; boolean z = this.k; this.k = false; if (p5Var.e()) { zgVar.b(4); } else { if (p5Var.d()) { zgVar.b(Integer.MIN_VALUE); } try { a2 = a(p5Var, zgVar, z); } catch (OutOfMemoryError e) { a2 = a((Throwable) e); } catch (RuntimeException e2) { a2 = a((Throwable) e2); } if (a2 != null) { synchronized (this.b) { this.j = a2; } return false; } } synchronized (this.b) { try { if (this.k) { zgVar.g(); } else if (zgVar.d()) { this.m++; zgVar.g(); } else { zgVar.c = this.m; this.m = 0; this.d.addLast(zgVar); } b(p5Var); } finally { } } return true; } } private boolean e() { return !this.c.isEmpty() && this.h > 0; } @Override // com.applovin.impl.m5 public void a() { synchronized (this.b) { this.l = true; this.b.notify(); } try { this.a.join(); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } } private void b(p5 p5Var) { p5Var.b(); p5[] p5VarArr = this.e; int i = this.g; this.g = i + 1; p5VarArr[i] = p5Var; } public void a(zg zgVar) { synchronized (this.b) { b(zgVar); k(); } } public final void a(int i) { b1.b(this.g == this.e.length); for (p5 p5Var : this.e) { p5Var.g(i); } } private void b(zg zgVar) { zgVar.b(); zg[] zgVarArr = this.f; int i = this.h; this.h = i + 1; zgVarArr[i] = zgVar; } }