package n;

import android.media.AudioManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.util.Pair;
import androidx.core.location.LocationRequestCompat;
import androidx.core.view.PointerIconCompat;
import androidx.media3.session.MediaController;
import b0.AbstractC0399d;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import n.j;

/* JADX INFO: loaded from: classes.dex */
final class l implements Handler.Callback {

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    private volatile long f5553B;

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    private int f5568Q;

    /* JADX INFO: renamed from: T, reason: collision with root package name */
    private AudioManager f5571T;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final String f5578a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Handler f5580b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final HandlerThread f5582c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final Handler f5584d;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final List f5587g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final x[][] f5588h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final int[] f5589i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private long f5590j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private long f5591k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private G[] f5592l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private G f5593m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private o f5594n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private boolean f5595o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private boolean f5596p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private boolean f5597q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private boolean f5598r;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private long f5603w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private long f5604x;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private volatile long f5606z;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private int f5599s = 1;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private int f5601u = 0;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private int f5602v = 0;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    private int f5554C = 1;

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    private int f5555D = -1;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    private boolean f5556E = true;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    private boolean f5557F = true;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    private boolean f5558G = true;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    private boolean f5559H = true;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    private long f5560I = 0;

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    private final String f5561J = "ro.boot.vendor.cf.hardware";

    /* JADX INFO: renamed from: K, reason: collision with root package name */
    private final String f5562K = "vendor.mtk.exoplayer.sub_offset";

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    private final String f5563L = "mt5873";

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    private boolean f5564M = false;

    /* JADX INFO: renamed from: N, reason: collision with root package name */
    private boolean f5565N = false;

    /* JADX INFO: renamed from: O, reason: collision with root package name */
    private boolean f5566O = false;

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    private boolean f5567P = false;

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    private boolean f5569R = false;

    /* JADX INFO: renamed from: S, reason: collision with root package name */
    private int f5570S = 0;

    /* JADX INFO: renamed from: U, reason: collision with root package name */
    private G f5572U = null;

    /* JADX INFO: renamed from: V, reason: collision with root package name */
    private G f5573V = null;

    /* JADX INFO: renamed from: W, reason: collision with root package name */
    private final int f5574W = 500;

    /* JADX INFO: renamed from: X, reason: collision with root package name */
    private final int f5575X = -1;

    /* JADX INFO: renamed from: Y, reason: collision with root package name */
    private final int f5576Y = 0;

    /* JADX INFO: renamed from: Z, reason: collision with root package name */
    private final int f5577Z = 1;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    private int f5579a0 = 0;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    private int f5581b0 = 0;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    private int f5583c0 = -1;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private volatile int f5600t = 1;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private volatile long f5605y = -1;

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    private volatile long f5552A = -1;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final E f5585e = new E();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final AtomicInteger f5586f = new AtomicInteger();

    public l(String str, Handler handler, boolean z2, int[] iArr, int i2, int i3) {
        this.f5578a = "ExoPlayerImplInternal" + str;
        this.f5584d = handler;
        this.f5596p = z2;
        this.f5590j = ((long) i2) * 1000;
        this.f5591k = ((long) i3) * 1000;
        this.f5589i = Arrays.copyOf(iArr, iArr.length);
        this.f5587g = new ArrayList(iArr.length);
        this.f5588h = new x[iArr.length][];
        b0.x xVar = new b0.x("ExoPlayerImplInternal:Handler", -16);
        this.f5582c = xVar;
        xVar.start();
        this.f5580b = new Handler(xVar.getLooper(), this);
        this.f5568Q = -1;
    }

    private void A() {
        C();
        V(1);
        synchronized (this) {
            this.f5595o = true;
            notifyAll();
        }
    }

    private boolean B(G g2) {
        if (g2.n()) {
            return true;
        }
        if (!g2.o()) {
            return false;
        }
        if (this.f5600t == 4) {
            return true;
        }
        long jI = g2.i();
        long jG = g2.g();
        long j2 = this.f5597q ? this.f5591k : this.f5590j;
        if (j2 < 0 || this.f5568Q != -1 || jG == -1 || jG == -3 || jG >= this.f5606z + j2) {
            return true;
        }
        return (jI == -1 || jI == -2 || jG < jI) ? false : true;
    }

    private void C() {
        this.f5580b.removeMessages(7);
        this.f5580b.removeMessages(2);
        int i2 = 0;
        this.f5597q = false;
        this.f5585e.g();
        if (this.f5592l == null) {
            return;
        }
        while (true) {
            G[] gArr = this.f5592l;
            if (i2 >= gArr.length) {
                this.f5592l = null;
                this.f5594n = null;
                this.f5593m = null;
                this.f5587g.clear();
                return;
            }
            G g2 = gArr[i2];
            b0(g2);
            z(g2);
            i2++;
        }
    }

    private void D(int i2, long j2, long j3) {
        long jElapsedRealtime = (j2 + j3) - SystemClock.elapsedRealtime();
        if (jElapsedRealtime <= 0) {
            this.f5580b.sendEmptyMessage(i2);
        } else {
            this.f5580b.sendEmptyMessageDelayed(i2, jElapsedRealtime);
        }
    }

    private void F(long j2) {
        try {
            if (j2 == this.f5606z / 1000) {
                V(3);
                Log.i(this.f5578a, "Seek is to the current position. set STATE_READY");
                V(4);
                this.f5564M = false;
            } else {
                this.f5597q = false;
                this.f5606z = 1000 * j2;
                this.f5585e.g();
                this.f5585e.c(this.f5606z);
                if (this.f5600t != 1 && this.f5600t != 2) {
                    for (int i2 = 0; i2 < this.f5587g.size(); i2++) {
                        G g2 = (G) this.f5587g.get(i2);
                        if (g2 instanceof q) {
                            if (b0.n.c() && b0.D.f2965a >= 31 && !b0.n.e() && u() && this.f5568Q == -1) {
                                String str = "hw_sync_id=" + g() + ";ignore_pause=true";
                                Log.i(this.f5578a, "audioManager tunnelingAudioSessionId:" + this.f5555D + ", setParameters: " + str);
                                this.f5571T.setParameters(str);
                            }
                            f(g2);
                        }
                    }
                    for (int i3 = 0; i3 < this.f5587g.size(); i3++) {
                        G g3 = (G) this.f5587g.get(i3);
                        f(g3);
                        g3.x(this.f5606z);
                        if (b0.n.d() && this.f5565N && (g3 instanceof w) && this.f5554C == 1 && this.f5568Q == -1 && u()) {
                            G(g3, PointerIconCompat.TYPE_COPY, Long.valueOf(j2));
                        }
                    }
                    if (this.f5568Q == -1) {
                        this.f5564M = true;
                        Log.d(this.f5578a, "set isSeeking to be true");
                    }
                    this.f5567P = false;
                    V(3);
                    this.f5580b.sendEmptyMessage(7);
                    return;
                }
                Log.i(this.f5578a, "current state is STATE_IDLE or STATE_PREPARING. Do nothing");
            }
        } finally {
            this.f5586f.decrementAndGet();
        }
    }

    private void H(int i2, Object obj) {
        try {
            Pair pair = (Pair) obj;
            ((j.a) pair.first).b(i2, pair.second);
            if (this.f5600t != 1 && this.f5600t != 2) {
                this.f5580b.sendEmptyMessage(7);
            }
            synchronized (this) {
                this.f5602v++;
                notifyAll();
            }
        } catch (Throwable th) {
            synchronized (this) {
                this.f5602v++;
                notifyAll();
                throw th;
            }
        }
    }

    private void M(u.b bVar) {
        for (int i2 = 0; i2 < this.f5587g.size(); i2++) {
            G g2 = (G) this.f5587g.get(i2);
            if (g2 instanceof t) {
                ((t) g2).g1(bVar);
            }
        }
    }

    private void R(boolean z2) {
        try {
            this.f5597q = false;
            this.f5596p = z2;
            if (!z2) {
                if (b0.D.f2965a >= 31 && u()) {
                    String str = "hw_sync_id=" + g() + ";ignore_pause=false";
                    Log.i(this.f5578a, "audioManager tunnelingAudioSessionId:" + this.f5555D + ", setParameters: " + str);
                    this.f5571T.setParameters(str);
                }
                d0();
                f0();
            } else if (this.f5600t == 4) {
                Z();
                this.f5580b.sendEmptyMessage(7);
            } else if (this.f5600t == 3) {
                this.f5580b.sendEmptyMessage(7);
            }
            this.f5584d.obtainMessage(3).sendToTarget();
        } catch (Throwable th) {
            this.f5584d.obtainMessage(3).sendToTarget();
            throw th;
        }
    }

    private void T(int i2, int i3) {
        G g2;
        int iL;
        G g3;
        int[] iArr = this.f5589i;
        if (iArr[i2] == i3) {
            return;
        }
        iArr[i2] = i3;
        if (this.f5600t == 1 || this.f5600t == 2 || (iL = (g2 = this.f5592l[i2]).l()) == 0 || iL == -1 || g2.m() == 0) {
            return;
        }
        boolean z2 = iL == 2 || iL == 3;
        boolean z3 = i3 >= 0 && i3 < this.f5588h[i2].length;
        if (z2) {
            if (!z3 && g2 == this.f5593m) {
                this.f5585e.c(this.f5594n.a());
            }
            if (b0.D.f2965a >= 31 && (g2 instanceof q) && u()) {
                String str = "hw_sync_id=" + g() + ";ignore_pause=true";
                Log.i(this.f5578a, "audioManager tunnelingAudioSessionId:" + this.f5555D + ", setParameters: " + str);
                this.f5571T.setParameters(str);
            }
            e(g2);
            this.f5587g.remove(g2);
        }
        if (z3) {
            boolean z4 = this.f5596p && this.f5600t == 4;
            d(g2, i3, !z2 && z4);
            if (z4) {
                g2.A();
            }
            if ((g2 instanceof q) && z2 && (g3 = this.f5592l[0]) != null && this.f5587g.contains(g3)) {
                w wVar = (w) this.f5592l[0];
                wVar.L(wVar.a());
            }
            this.f5580b.sendEmptyMessage(7);
        }
    }

    private void V(int i2) {
        if (this.f5600t != i2 || (b0.n.e() && this.f5586f.get() > 0)) {
            this.f5600t = i2;
            this.f5584d.obtainMessage(2, i2, 0).sendToTarget();
            for (int i3 = 0; i3 < this.f5587g.size(); i3++) {
                G((j.a) this.f5587g.get(i3), 1007, Integer.valueOf(i2));
            }
        }
    }

    private void W() {
        G[] gArr = this.f5592l;
        if (gArr == null) {
            return;
        }
        for (G g2 : gArr) {
            if (g2 != null) {
                g2.z(this.f5554C);
                Log.d(this.f5578a, "setTrackRenderSeekMode() seekMode=" + this.f5554C + "-renderer=" + g2);
            }
        }
    }

    private void Y(int i2, int i3, int i4) {
        G g2 = this.f5592l[0];
        if (g2 != null && (g2 instanceof w)) {
            ((w) g2).Q1(i4, i2);
        }
        if ((this.f5568Q != i4 || i4 == 1 || i4 == 4 || i4 == -1) && this.f5600t != 1) {
            this.f5580b.removeMessages(7);
            this.f5567P = false;
            int i5 = this.f5568Q;
            if (i5 != -1 || i4 != -1) {
                this.f5569R = false;
            } else if (i2 == 1) {
                this.f5569R = true;
            }
            switch (i2) {
                case 1:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    break;
                case 2:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    break;
                case 3:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    G g3 = this.f5592l[0];
                    if (g3 != null && ((g3.l() == 2 || this.f5592l[0].l() == 3) && (this.f5592l[0] instanceof o) && !u())) {
                        this.f5606z = ((o) this.f5592l[0]).a();
                    }
                    break;
                case 4:
                    Object obj = this.f5592l[0];
                    if (obj != null) {
                        this.f5585e.c(((o) obj).a());
                    }
                    if (!u()) {
                        this.f5585e.d(o(i2));
                        this.f5585e.f();
                    } else {
                        this.f5585e.d(o(3));
                        this.f5585e.f();
                    }
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    break;
                case 9:
                case 10:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    break;
                case 11:
                case 12:
                case 13:
                case 14:
                default:
                    Log.i(this.f5578a, "speed is not handled in here !");
                    break;
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    break;
                case 20:
                case 21:
                case 22:
                case 23:
                case 24:
                    if (i5 == 3) {
                        this.f5585e.h();
                    }
                    break;
            }
            if (this.f5568Q == 0 && this.f5565N) {
                this.f5598r = true;
            }
            this.f5568Q = i4;
            D(7, SystemClock.elapsedRealtime(), 100L);
        }
    }

    private void Z() {
        this.f5597q = false;
        if (this.f5565N && this.f5569R) {
            this.f5598r = false;
            this.f5569R = false;
        } else {
            this.f5598r = true;
        }
        this.f5599s = 1;
        this.f5585e.e();
        for (int i2 = 0; i2 < this.f5587g.size(); i2++) {
            if (((G) this.f5587g.get(i2)).l() != 3) {
                ((G) this.f5587g.get(i2)).A();
            }
        }
    }

    private void b() {
        x[] xVarArr;
        x[] xVarArr2;
        x[] xVarArr3;
        if (this.f5570S == 0 || this.f5571T == null) {
            return;
        }
        G[] gArr = this.f5592l;
        G g2 = gArr[0];
        G g3 = gArr[1];
        if ("0".equals(b0.D.y("mtk.exoplayer.AudioOnlyTunnel", "0")) && (g2 == null || (g2 instanceof C0642h) || (xVarArr3 = this.f5588h[0]) == null || xVarArr3.length == 0)) {
            return;
        }
        if (g2 != null && !(g2 instanceof C0642h) && (xVarArr2 = this.f5588h[0]) != null && xVarArr2.length > 0) {
            boolean zE0 = ((t) g2).E0(xVarArr2[0]);
            Log.i(this.f5578a, "video tunneled judgement: mimeType[" + this.f5588h[0][0].f5842g + "] tunneled=" + zE0);
            if (!zE0) {
                return;
            }
        }
        if (g3 != null && !(g3 instanceof C0642h) && (xVarArr = this.f5588h[1]) != null && xVarArr.length > 0) {
            boolean zE02 = ((t) g3).E0(xVarArr[0]);
            Log.i(this.f5578a, "audio tunneled judgement: mimeType[" + this.f5588h[1][0].f5842g + "] tunneled=" + zE02);
            if (!zE02) {
                return;
            }
        }
        x[] xVarArr4 = this.f5588h[1];
        if (xVarArr4 == null || xVarArr4.length == 0) {
            this.f5555D = 0;
        } else {
            int i2 = this.f5579a0;
            if ((i2 != 0 && this.f5555D == -1) || i2 == 0) {
                this.f5555D = this.f5571T.generateAudioSessionId();
                int iG = g();
                Log.i(this.f5578a, "tunnelingAudioSessionId:" + this.f5555D + ", hw_sync_id:" + iG);
            }
        }
        try {
            if (this.f5570S == 1) {
                if (g2 != null && (g2 instanceof w)) {
                    ((t) g2).l1(this.f5555D);
                }
                if (g3 == null || !(g3 instanceof q)) {
                    return;
                }
                ((t) g3).l1(this.f5555D);
            }
        } catch (IllegalArgumentException e2) {
            e2.printStackTrace();
        }
    }

    private void b0(G g2) {
        try {
            e(g2);
        } catch (RuntimeException e2) {
            Log.e(this.f5578a, "Stop failed.", e2);
        } catch (C0643i e3) {
            Log.e(this.f5578a, "Stop failed.", e3);
        }
    }

    private void c() {
        G g2;
        long j2;
        b0.B.a("doSomeWork");
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long jMin = this.f5605y != -1 ? this.f5605y : LocationRequestCompat.PASSIVE_INTERVAL;
        f0();
        int i2 = 0;
        boolean z2 = true;
        boolean z3 = true;
        boolean z4 = false;
        while (i2 < this.f5587g.size()) {
            G g3 = (G) this.f5587g.get(i2);
            int i3 = i2;
            g3.e(this.f5606z, this.f5604x);
            if (!(g3 instanceof I.m) || b0.n.a() == 2) {
                z2 = z2 && g3.n();
            }
            boolean z5 = g3 instanceof q;
            if (z5 && ((t) g3).y0() != -1 && g3.n()) {
                g2 = g3;
                if ((this.f5605y / 1000) - this.f5603w >= 0 && (this.f5605y / 1000) - this.f5603w < 1000) {
                    Log.d(this.f5578a, "Last seek near EOS and audio ends. Quit playback.");
                    V(5);
                    d0();
                    this.f5580b.removeMessages(7);
                    return;
                }
            } else {
                g2 = g3;
            }
            if (b0.D.f2965a >= 31 && z5 && ((t) g2).y0() != -1 && this.f5596p && g2.n() && !this.f5567P) {
                String str = "hw_sync_id=" + g() + ";end_of_stream=true";
                Log.i(this.f5578a, "audioManager setParameters: " + str);
                this.f5571T.setParameters(str);
                this.f5567P = true;
            }
            if (z5 && this.f5566O && b0.n.e() && g2.n()) {
                Log.d(this.f5578a, "audio only playback end, update state.");
                V(5);
                d0();
                this.f5580b.removeMessages(7);
                return;
            }
            G g4 = g2;
            boolean zB = B(g4);
            if (z5) {
                this.f5572U = g4;
                z4 = zB;
            } else if (g4 instanceof w) {
                this.f5573V = g4;
            }
            if (!zB || ((g4 instanceof w) && g4.n())) {
                g4.p();
            }
            if (z3 && zB) {
                j2 = -1;
                z3 = true;
            } else {
                j2 = -1;
                z3 = false;
            }
            if (jMin != j2) {
                long jI = g4.i();
                long jG = g4.g();
                if (jG == j2) {
                    jMin = j2;
                } else if (jG != -3 && (jI == j2 || jI == -2 || jG < jI)) {
                    if (b0.n.e() && jMin < jG) {
                        jMin = jG;
                    }
                    jMin = Math.min(jMin, jG);
                }
            }
            i2 = i3 + 1;
        }
        this.f5552A = jMin;
        if (z2) {
            V(5);
            d0();
        } else if (this.f5600t == 3) {
            b0.o.d(this.f5578a, "state == ExoPlayer.STATE_BUFFERING", 1);
            if (b0.n.c()) {
                for (int i4 = 0; i4 < this.f5587g.size(); i4++) {
                    if (this.f5587g.get(i4) instanceof w) {
                    }
                }
            }
            if (b0.n.d() && !this.f5596p && u() && this.f5568Q == -1) {
                w wVar = null;
                for (int i5 = 0; i5 < this.f5587g.size(); i5++) {
                    if (this.f5587g.get(i5) instanceof w) {
                        wVar = (w) this.f5587g.get(i5);
                    }
                }
                if (wVar != null && !this.f5580b.hasMessages(3)) {
                    if (z3) {
                        if (this.f5565N && !this.f5564M) {
                            G(wVar, 1008, 1);
                        }
                    } else if (this.f5564M) {
                        G(wVar, PointerIconCompat.TYPE_ALIAS, null);
                        this.f5564M = false;
                    }
                }
            }
            if (z3) {
                b0.o.d(this.f5578a, "allRenderersReadyOrEnded isSeeking:" + this.f5564M + ", set STATE_READY, startRenders", 1);
                if (this.f5564M && b0.n.d()) {
                    this.f5564M = false;
                }
                V(4);
                if (this.f5596p) {
                    Z();
                }
            } else if (z4 && u() && this.f5596p && this.f5572U.l() != 3) {
                b0.o.d(this.f5578a, "audioRenders Ready, start it at first in TunnelingPlayback", 1);
                this.f5572U.A();
            }
        } else if (this.f5600t == 4 && !z3) {
            this.f5597q = this.f5596p;
            V(3);
            d0();
        }
        this.f5580b.removeMessages(7);
        if ((this.f5596p && this.f5600t == 4) || this.f5600t == 3) {
            if (this.f5570S != 0 && this.f5579a0 == 2) {
                D(7, jElapsedRealtime, 500L);
            } else if (this.f5555D == -1 || this.f5587g.contains(this.f5592l[2])) {
                D(7, jElapsedRealtime, 10L);
            } else {
                D(7, jElapsedRealtime, 20L);
            }
        } else if (!this.f5587g.isEmpty()) {
            D(7, jElapsedRealtime, 1000L);
        }
        b0.B.c();
    }

    private void c0() {
        C();
        V(1);
    }

    private void d(G g2, int i2, boolean z2) {
        boolean z3 = g2 instanceof q;
        if (z3 && 1 == this.f5579a0 && 1 == this.f5583c0 && this.f5570S == 0) {
            this.f5556E = true;
            this.f5557F = true;
            this.f5559H = true;
            this.f5560I = 0L;
            g2.f(i2, this.f5606z + 450000, z2);
        } else if (z3 && 1 == this.f5579a0 && 1 == this.f5583c0 && 1 == this.f5570S) {
            g2.f(i2, 0L, z2);
        } else {
            long j2 = this.f5606z;
            if (z3 && this.f5606z != 0) {
                j2 = this.f5606z + 450000;
                Log.i(this.f5578a, "Add delay for audio renderer, positionUs[" + this.f5606z + "], tmpPostionUs[" + j2 + "]");
            }
            g2.f(i2, j2, z2);
        }
        this.f5587g.add(g2);
        o oVarK = g2.k();
        if (oVarK != null) {
            AbstractC0399d.e(this.f5594n == null);
            this.f5594n = oVarK;
            this.f5593m = g2;
        }
    }

    private void d0() {
        this.f5585e.g();
        for (int i2 = 0; i2 < this.f5587g.size(); i2++) {
            f((G) this.f5587g.get(i2));
        }
    }

    private void e(G g2) {
        f(g2);
        if (g2.l() == 2) {
            g2.c(this.f5606z);
            if (g2 == this.f5593m) {
                this.f5594n = null;
                this.f5593m = null;
            }
        }
        if (g2 instanceof q) {
            int i2 = this.f5579a0;
            if (1 == i2 || 3 == i2) {
                this.f5556E = false;
                this.f5557F = false;
                this.f5559H = false;
                this.f5560I = 0L;
            }
        }
    }

    private void f(G g2) {
        if (g2.l() == 3) {
            g2.B();
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:67:0x010a  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x0135  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private void f0() {
        /*
            Method dump skipped, instruction units count: 346
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: n.l.f0():void");
    }

    private int g() {
        if (b0.D.f2965a < 31) {
            return 0;
        }
        try {
            Method declaredMethod = Class.forName("android.media.AudioManager").getDeclaredMethod("getAudioHwSyncForSession", Integer.TYPE);
            if (declaredMethod != null) {
                return ((Integer) declaredMethod.invoke(this.f5571T, Integer.valueOf(this.f5555D))).intValue();
            }
            return 0;
        } catch (Exception unused) {
            Log.e(this.f5578a, "Android sdk version dose not support getAudioHwSyncForSession().");
            return 0;
        }
    }

    private void g0() {
        long jA = this.f5594n.a();
        if (((q) this.f5593m).w1() && this.f5559H) {
            this.f5559H = false;
            this.f5560I = jA;
            if (jA > this.f5606z) {
                if (this.f5556E) {
                    Log.d(this.f5578a, "Audio pause.");
                    ((q) this.f5593m).D1();
                    this.f5556E = false;
                }
                this.f5606z = this.f5585e.a();
                return;
            }
            return;
        }
        if (((q) this.f5593m).w1()) {
            this.f5560I = jA;
            this.f5606z = jA;
            this.f5585e.c(jA);
            return;
        }
        long j2 = this.f5560I - this.f5606z;
        if (j2 >= -30000 && j2 <= MediaController.RELEASE_UNBIND_TIMEOUT_MS && this.f5557F) {
            if (1 == this.f5579a0) {
                ((q) this.f5593m).H1(true);
            }
            if (3 == this.f5579a0) {
                ((q) this.f5593m).H1(false);
            }
            Log.d(this.f5578a, "Audio play.");
            ((q) this.f5593m).E1();
            this.f5557F = false;
        }
        this.f5606z = this.f5585e.a();
    }

    static double o(int i2) {
        switch (i2) {
            case 4:
                return 2.0d;
            case 5:
                return 4.0d;
            case 6:
                return 8.0d;
            case 7:
                return 16.0d;
            case 8:
                return 32.0d;
            case 9:
            default:
                return 1.0d;
            case 10:
                return 2.0d;
            case 11:
                return 4.0d;
            case 12:
                return 8.0d;
            case 13:
                return 16.0d;
            case 14:
                return 32.0d;
            case 15:
            case 20:
                return 0.5d;
            case 16:
            case 21:
                return 0.25d;
            case 17:
            case 22:
                return 0.125d;
            case 18:
            case 23:
                return 0.0625d;
            case 19:
            case 24:
                return 0.03125d;
        }
    }

    private void r() {
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        if (this.f5592l[2].l() == 0 && this.f5592l[2].v(this.f5606z) == 0) {
            this.f5592l[2].p();
            D(2, jElapsedRealtime, 10L);
            return;
        }
        G g2 = this.f5592l[2];
        int iM = g2.m();
        x[] xVarArr = new x[iM];
        for (int i2 = 0; i2 < iM; i2++) {
            xVarArr[i2] = g2.j(i2);
            b0.o.d(this.f5578a, "[incrementalPrepareExternalText] rendererTrackFormats[" + i2 + "]: " + xVarArr[i2].f5842g, 16);
        }
        this.f5588h[2] = xVarArr;
        this.f5584d.obtainMessage(1, this.f5600t, 0, this.f5588h).sendToTarget();
    }

    /* JADX WARN: Removed duplicated region for block: B:77:0x0176 A[PHI: r9 r10
      0x0176: PHI (r9v3 boolean) = (r9v1 boolean), (r9v1 boolean), (r9v5 boolean), (r9v5 boolean) binds: [B:44:0x0102, B:45:0x0104, B:73:0x0169, B:75:0x016d] A[DONT_GENERATE, DONT_INLINE]
      0x0176: PHI (r10v3 boolean) = (r10v1 boolean), (r10v1 boolean), (r10v5 boolean), (r10v5 boolean) binds: [B:44:0x0102, B:45:0x0104, B:73:0x0169, B:75:0x016d] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private void s() {
        /*
            Method dump skipped, instruction units count: 679
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: n.l.s():void");
    }

    private boolean v() {
        String strY = b0.D.y("ro.boot.vendor.cf.hardware", "0");
        strY.hashCode();
        return strY.equals("mt5873");
    }

    private void x(G[] gArr) {
        C();
        this.f5592l = gArr;
        Arrays.fill(this.f5588h, (Object) null);
        V(2);
        s();
    }

    private void z(G g2) {
        try {
            g2.w();
        } catch (RuntimeException e2) {
            Log.e(this.f5578a, "Release failed.", e2);
        } catch (C0643i e3) {
            Log.e(this.f5578a, "Release failed.", e3);
        }
    }

    public void E(long j2) {
        this.f5603w = j2;
        this.f5586f.incrementAndGet();
        this.f5580b.obtainMessage(6, b0.D.B(j2), b0.D.n(j2)).sendToTarget();
    }

    public void G(j.a aVar, int i2, Object obj) {
        this.f5601u++;
        this.f5580b.obtainMessage(9, i2, 0, Pair.create(aVar, obj)).sendToTarget();
    }

    public void I(long j2) {
        G g2 = this.f5572U;
        if (g2 != null) {
            G(g2, 1025, Long.valueOf(j2));
            Log.d(this.f5578a, "set audio target mute when a/v length not match, pts=" + j2);
        }
    }

    public void J(int i2) {
        this.f5580b.obtainMessage(11, Integer.valueOf(i2)).sendToTarget();
    }

    public void K(int i2) {
        b0.o.d(this.f5578a, "set audioSessionId " + i2, 1);
        this.f5555D = i2;
    }

    public void L(u.b bVar) {
        this.f5580b.obtainMessage(1002, bVar).sendToTarget();
    }

    public void N(int i2) {
        this.f5580b.obtainMessage(10, Integer.valueOf(i2)).sendToTarget();
    }

    public void O(int i2) {
        if (this.f5579a0 != i2) {
            b0.o.d(this.f5578a, "set focus mode " + i2, 1);
            this.f5579a0 = i2;
            int i3 = this.f5581b0;
            if (2 == i3 && 1 == i2) {
                this.f5556E = true;
                this.f5557F = true;
                this.f5559H = true;
                this.f5560I = 0L;
                this.f5583c0 = 1;
            } else if (i3 == 0 && 1 == i2) {
                this.f5583c0 = 0;
            }
            this.f5581b0 = i2;
            G[] gArr = this.f5592l;
            if (gArr != null) {
                for (G g2 : gArr) {
                    if (g2 != null) {
                        g2.y(i2);
                        if (g2 instanceof q) {
                            if (1 == i2) {
                                ((q) g2).H1(true);
                            }
                            if (3 == i2) {
                                ((q) g2).H1(false);
                            }
                        }
                    }
                }
            }
        }
    }

    public void P(int i2, AudioManager audioManager) {
        this.f5570S = i2;
        this.f5571T = audioManager;
        b0.o.d(this.f5578a, "tunneling is " + i2, 1);
    }

    public void Q(boolean z2) {
        this.f5580b.obtainMessage(3, z2 ? 1 : 0, 0).sendToTarget();
    }

    public void S(int i2, int i3) {
        this.f5580b.obtainMessage(8, i2, i3).sendToTarget();
    }

    public void U(int i2) {
        this.f5554C = i2;
    }

    public void X(int i2, int i3, int i4) {
        this.f5580b.obtainMessage(1001, i2, i3, Integer.valueOf(i4)).sendToTarget();
    }

    public synchronized void a(j.a aVar, int i2, Object obj) {
        if (this.f5595o) {
            Log.w(this.f5578a, "Sent message(" + i2 + ") after release. Message ignored.");
            return;
        }
        int i3 = this.f5601u;
        this.f5601u = i3 + 1;
        this.f5580b.obtainMessage(9, i2, 0, Pair.create(aVar, obj)).sendToTarget();
        while (this.f5602v <= i3 && !this.f5595o) {
            try {
                wait();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        }
    }

    public void a0() {
        this.f5580b.sendEmptyMessage(4);
    }

    public void e0(C0643i c0643i) {
        this.f5580b.obtainMessage(1004, c0643i).sendToTarget();
    }

    public long h() {
        if (this.f5552A == -1) {
            return -1L;
        }
        return this.f5552A / 1000;
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        try {
            int i2 = message.what;
            switch (i2) {
                case 1:
                    x((G[]) message.obj);
                    return true;
                case 2:
                    s();
                    return true;
                case 3:
                    R(message.arg1 != 0);
                    return true;
                case 4:
                    c0();
                    return true;
                case 5:
                    A();
                    return true;
                case 6:
                    F(b0.D.v(message.arg1, message.arg2));
                    return true;
                case 7:
                    c();
                    return true;
                case 8:
                    T(message.arg1, message.arg2);
                    return true;
                case 9:
                    H(message.arg1, message.obj);
                    return true;
                case 10:
                    O(((Integer) message.obj).intValue());
                    return true;
                case 11:
                    K(((Integer) message.obj).intValue());
                    return true;
                default:
                    switch (i2) {
                        case 1001:
                            Y(message.arg1, message.arg2, ((Integer) message.obj).intValue());
                            return true;
                        case 1002:
                            M((u.b) message.obj);
                            return true;
                        case 1003:
                            r();
                            return true;
                        case 1004:
                            if (this.f5600t == 1) {
                                return true;
                            }
                            throw ((C0643i) message.obj);
                        default:
                            return false;
                    }
            }
        } catch (RuntimeException e2) {
            Log.e(this.f5578a, "Internal runtime error.", e2);
            this.f5584d.obtainMessage(4, new C0643i((Throwable) e2, true)).sendToTarget();
            c0();
            return true;
        } catch (C0643i e3) {
            Log.e(this.f5578a, "Internal track renderer error.", e3);
            this.f5584d.obtainMessage(4, e3).sendToTarget();
            c0();
            return true;
        }
    }

    public long i() {
        return this.f5604x;
    }

    public long j() {
        return this.f5586f.get() > 0 ? this.f5603w : this.f5606z / 1000;
    }

    public long k() {
        return this.f5606z;
    }

    public long l() {
        if (this.f5605y == -1) {
            return -1L;
        }
        return this.f5605y / 1000;
    }

    public int m() {
        return this.f5579a0;
    }

    public Looper n() {
        return this.f5582c.getLooper();
    }

    public int p() {
        return this.f5600t;
    }

    public int q() {
        return this.f5555D;
    }

    public boolean t() {
        List list = this.f5587g;
        if (list == null || list.size() <= 0) {
            return false;
        }
        boolean zN = false;
        for (int i2 = 0; i2 < this.f5587g.size(); i2++) {
            G g2 = (G) this.f5587g.get(i2);
            if (g2 instanceof q) {
                zN = g2.n();
            }
        }
        return zN;
    }

    public boolean u() {
        x[] xVarArr;
        if (this.f5600t != 1 && this.f5600t != 2 && this.f5600t != 5) {
            G g2 = this.f5592l[0];
            return (g2 == null || (g2 instanceof C0642h) || (xVarArr = this.f5588h[0]) == null || xVarArr.length == 0 || ((t) g2).y0() == -1) ? false : true;
        }
        Log.w(this.f5578a, "Check tunneling playback in wrong state:" + this.f5600t);
        return false;
    }

    public void w(G... gArr) {
        this.f5580b.obtainMessage(1, gArr).sendToTarget();
    }

    public synchronized void y() {
        if (this.f5595o) {
            return;
        }
        this.f5580b.sendEmptyMessage(5);
        while (!this.f5595o) {
            try {
                wait();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        }
        this.f5582c.quit();
    }
}
