package defpackage;

import android.media.DeniedByServerException;
import android.media.MediaDrm;
import android.media.MediaDrmResetException;
import android.media.NotProvisionedException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Pair;
import androidx.media3.exoplayer.drm.DefaultDrmSessionManager$MissingSchemeDataException;
import androidx.media3.exoplayer.drm.DrmSession$DrmSessionException;
import androidx.media3.exoplayer.drm.KeysExpiredException;
import androidx.media3.exoplayer.drm.UnsupportedDrmException;
import j$.util.DesugarCollections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/* JADX INFO: renamed from: ᵗʩʨ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C2478 implements InterfaceC4417 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final InterfaceC0695 f9618;

    /* JADX INFO: renamed from: ˉᵲ, reason: contains not printable characters */
    public HandlerC4324 f9619;

    /* JADX INFO: renamed from: ˊᵙᵾᵲ, reason: contains not printable characters */
    public HandlerThread f9620;

    /* JADX INFO: renamed from: ˊᵨᵧ, reason: contains not printable characters */
    public DrmSession$DrmSessionException f9621;

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final C5523 f9622;

    /* JADX INFO: renamed from: ˋˈ, reason: contains not printable characters */
    public final HandlerC4211 f9623;

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public byte[] f9624;

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public C2639 f9625;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final C0767 f9626;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public C2641 f9627;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public byte[] f9628;

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public final Looper f9629;

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public int f9630;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final boolean f9631;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public ˈᵷᵵᵞ f9632;

    /* JADX INFO: renamed from: ᵪᵹᵥᵻ, reason: contains not printable characters */
    public final Object f9633;

    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    public final UUID f9634;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public C2538 f9635;

    /* JADX INFO: renamed from: ᵱʡˏ, reason: contains not printable characters */
    public int f9636;

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public final C2136 f9637;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final List f9638;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final HashMap f9639;

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public final InterfaceC2338 f9640;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final C1014 f9641;

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final C1013 f9642;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final boolean f9643;

    public C2478(UUID uuid, InterfaceC0695 interfaceC0695, C0767 c0767, C1014 c1014, List list, boolean z, boolean z2, byte[] bArr, HashMap map, InterfaceC2338 interfaceC2338, Looper looper, C1013 c1013, C2136 c2136) {
        this.f9634 = uuid;
        this.f9626 = c0767;
        this.f9641 = c1014;
        this.f9618 = interfaceC0695;
        this.f9631 = z;
        this.f9643 = z2;
        if (bArr != null) {
            this.f9628 = bArr;
            this.f9638 = null;
        } else {
            list.getClass();
            this.f9638 = DesugarCollections.unmodifiableList(list);
        }
        this.f9639 = map;
        this.f9640 = interfaceC2338;
        this.f9622 = new C5523();
        this.f9642 = c1013;
        this.f9637 = c2136;
        this.f9630 = 2;
        this.f9629 = looper;
        this.f9623 = new HandlerC4211(this, looper, 2);
        this.f9633 = new Object();
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ʽˈˋ */
    public final boolean mo4233() {
        m6219();
        return this.f9631;
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ˊᵫᵗ */
    public final C2639 mo4234() {
        m6219();
        return this.f9625;
    }

    /* JADX WARN: Removed duplicated region for block: B:24:0x0053  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x0059  */
    /* JADX INFO: renamed from: ˋˈ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean m6217() {
        Set set;
        if (m6223()) {
            return true;
        }
        try {
            byte[] bArrMo2490 = this.f9618.mo2490();
            this.f9624 = bArrMo2490;
            this.f9618.mo2489(bArrMo2490, this.f9637);
            this.f9625 = this.f9618.mo2493(this.f9624);
            this.f9630 = 3;
            C5523 c5523 = this.f9622;
            synchronized (c5523.f20846) {
                set = c5523.f20847;
            }
            Iterator it = set.iterator();
            while (it.hasNext()) {
                ((C3317) it.next()).m7593(3);
            }
            this.f9624.getClass();
            return true;
        } catch (NotProvisionedException unused) {
            this.f9626.m2764(this);
            return false;
        } catch (Exception e) {
            e = e;
            if (ᵸᵺᵜ.ᵜᵪʾ(e)) {
                m6221(1, e);
                return false;
            }
            this.f9626.m2764(this);
            return false;
        } catch (NoSuchMethodError e2) {
            e = e2;
            if (ᵸᵺᵜ.ᵜᵪʾ(e)) {
            }
        }
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ːˏᵟᵯ */
    public final void mo4235(C3317 c3317) {
        m6219();
        int i = this.f9636;
        if (i <= 0) {
            ˋˀᵧ.ᵝᵶ("release() called on a session that's already fully released.");
            return;
        }
        int i2 = i - 1;
        this.f9636 = i2;
        if (i2 == 0) {
            this.f9630 = 0;
            HandlerC4211 handlerC4211 = this.f9623;
            String str = AbstractC0956.f4222;
            handlerC4211.removeCallbacksAndMessages(null);
            this.f9619.m9308();
            this.f9619 = null;
            this.f9620.quit();
            this.f9620 = null;
            this.f9625 = null;
            this.f9621 = null;
            this.f9627 = null;
            synchronized (this.f9633) {
                this.f9632 = null;
            }
            this.f9635 = null;
            byte[] bArr = this.f9624;
            if (bArr != null) {
                this.f9618.mo2492(bArr);
                this.f9624 = null;
            }
        }
        if (c3317 != null) {
            this.f9622.m11150(c3317);
            if (this.f9622.m11149(c3317) == 0) {
                c3317.m7594();
            }
        }
        C1014 c1014 = this.f9641;
        int i3 = this.f9636;
        C2076 c2076 = (C2076) c1014.f4404;
        if (i3 == 1 && c2076.f8235 > 0 && c2076.f8243 != -9223372036854775807L) {
            c2076.f8237.add(this);
            Handler handler = c2076.f8250;
            handler.getClass();
            handler.postAtTime(new RunnableC2846(15, this), this, SystemClock.uptimeMillis() + c2076.f8243);
        } else if (i3 == 0) {
            c2076.f8233.remove(this);
            if (c2076.f8239 == this) {
                c2076.f8239 = null;
            }
            if (c2076.f8238 == this) {
                c2076.f8238 = null;
            }
            C0767 c0767 = c2076.f8236;
            HashSet hashSet = (HashSet) c0767.f3433;
            hashSet.remove(this);
            if (((C2478) c0767.f3432) == this) {
                c0767.f3432 = null;
                if (!hashSet.isEmpty()) {
                    C2478 c2478 = (C2478) hashSet.iterator().next();
                    c0767.f3432 = c2478;
                    C2538 c2538Mo2496 = c2478.f9618.mo2496();
                    c2478.f9635 = c2538Mo2496;
                    HandlerC4324 handlerC4324 = c2478.f9619;
                    String str2 = AbstractC0956.f4222;
                    c2538Mo2496.getClass();
                    handlerC4324.getClass();
                    handlerC4324.obtainMessage(1, new C4426(C3622.f13742.getAndIncrement(), true, SystemClock.elapsedRealtime(), c2538Mo2496)).sendToTarget();
                }
            }
            if (c2076.f8243 != -9223372036854775807L) {
                Handler handler2 = c2076.f8250;
                handler2.getClass();
                handler2.removeCallbacksAndMessages(this);
                c2076.f8237.remove(this);
            }
        }
        c2076.m5538();
    }

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public final void m6218(Throwable th, boolean z) {
        if ((th instanceof NotProvisionedException) || ᵸᵺᵜ.ᵜᵪʾ(th)) {
            this.f9626.m2764(this);
        } else {
            m6221(z ? 1 : 2, th);
        }
    }

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public final void m6219() {
        Thread threadCurrentThread = Thread.currentThread();
        Looper looper = this.f9629;
        if (threadCurrentThread != looper.getThread()) {
            ˋˀᵧ.ᵠˏˏ("DefaultDrmSession accessed on the wrong thread.\nCurrent thread: " + Thread.currentThread().getName() + "\nExpected thread: " + looper.getThread().getName(), new IllegalStateException());
        }
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ᵨᵣ */
    public final int mo4236() {
        m6219();
        return this.f9630;
    }

    /* JADX INFO: renamed from: ᵪᵹᵥᵻ, reason: contains not printable characters */
    public final void m6220(int i, boolean z, byte[] bArr) {
        try {
            synchronized (this.f9633) {
                try {
                    this.f9632 = new ˈᵷᵵᵞ((byte) 0, 0);
                    List list = this.f9638;
                    if (list != null) {
                        AbstractC4509.m9559(list);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            C2641 c2641Mo2483 = this.f9618.mo2483(bArr, this.f9638, i, this.f9639);
            this.f9627 = c2641Mo2483;
            HandlerC4324 handlerC4324 = this.f9619;
            String str = AbstractC0956.f4222;
            c2641Mo2483.getClass();
            handlerC4324.getClass();
            handlerC4324.obtainMessage(2, new C4426(C3622.f13742.getAndIncrement(), z, SystemClock.elapsedRealtime(), c2641Mo2483)).sendToTarget();
        } catch (Exception | NoSuchMethodError e) {
            m6218(e, true);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x001d  */
    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void m6221(int i, Throwable th) {
        int iM3219;
        Set set;
        if (th instanceof MediaDrm.MediaDrmStateException) {
            iM3219 = AbstractC0956.m3219(AbstractC0956.m3234(((MediaDrm.MediaDrmStateException) th).getDiagnosticInfo()));
        } else if (!(th instanceof MediaDrmResetException)) {
            if ((th instanceof NotProvisionedException) || ᵸᵺᵜ.ᵜᵪʾ(th)) {
                iM3219 = 6002;
            } else if (th instanceof DeniedByServerException) {
                iM3219 = 6007;
            } else if (th instanceof UnsupportedDrmException) {
                iM3219 = 6001;
            } else if (th instanceof DefaultDrmSessionManager$MissingSchemeDataException) {
                iM3219 = 6003;
            } else if (th instanceof KeysExpiredException) {
                iM3219 = 6008;
            } else if (i == 1) {
                iM3219 = 6006;
            } else if (i == 2) {
                iM3219 = 6004;
            } else {
                if (i != 3) {
                    ᵷᵝˎ.ᵷᵺʾᵖ();
                    return;
                }
                iM3219 = 6002;
            }
        }
        this.f9621 = new DrmSession$DrmSessionException(iM3219, th);
        ˋˀᵧ.ˋˈ("DRM session error", th);
        if (th instanceof Exception) {
            C5523 c5523 = this.f9622;
            synchronized (c5523.f20846) {
                set = c5523.f20847;
            }
            Iterator it = set.iterator();
            while (it.hasNext()) {
                ((C3317) it.next()).m7596((Exception) th);
            }
        } else if (!(th instanceof Error)) {
            C4935.m10239("Unexpected Throwable subclass", th);
            return;
        } else if (!ᵸᵺᵜ.ᵔᵖᵹᵫ(th) && !ᵸᵺᵜ.ᵜᵪʾ(th)) {
            throw ((Error) th);
        }
        if (this.f9630 != 4) {
            this.f9630 = 1;
        }
    }

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public final void m6222(boolean z) {
        long jMin;
        String str;
        Set set;
        if (this.f9643) {
            return;
        }
        byte[] bArr = this.f9624;
        String str2 = AbstractC0956.f4222;
        boolean z2 = true;
        if (this.f9628 == null) {
            m6220(1, z, bArr);
            return;
        }
        if (this.f9630 != 4) {
            try {
                this.f9618.mo2486(this.f9624, this.f9628);
            } catch (Exception | NoSuchMethodError e) {
                m6221(1, e);
                z2 = false;
            }
            if (!z2) {
                return;
            }
        }
        if (AbstractC3361.f12881.equals(this.f9634)) {
            m6219();
            byte[] bArr2 = this.f9624;
            Pair pair = null;
            Map mapMo2485 = bArr2 == null ? null : this.f9618.mo2485(bArr2);
            if (mapMo2485 != null) {
                long j = -9223372036854775807L;
                try {
                    str = (String) mapMo2485.get("LicenseDurationRemaining");
                } catch (NumberFormatException unused) {
                }
                long j2 = str != null ? Long.parseLong(str) : -9223372036854775807L;
                Long lValueOf = Long.valueOf(j2);
                try {
                    String str3 = (String) mapMo2485.get("PlaybackDurationRemaining");
                    if (str3 != null) {
                        j = Long.parseLong(str3);
                    }
                } catch (NumberFormatException unused2) {
                }
                pair = new Pair(lValueOf, Long.valueOf(j));
            }
            pair.getClass();
            jMin = Math.min(((Long) pair.first).longValue(), ((Long) pair.second).longValue());
        } else {
            jMin = Long.MAX_VALUE;
        }
        if (jMin <= 60) {
            ˋˀᵧ.ᵶᵥᵝ("Offline license has expired or will expire soon. Remaining seconds: " + jMin);
            m6220(2, z, bArr);
            return;
        }
        if (jMin <= 0) {
            m6221(2, new KeysExpiredException());
            return;
        }
        this.f9630 = 4;
        C5523 c5523 = this.f9622;
        synchronized (c5523.f20846) {
            set = c5523.f20847;
        }
        Iterator it = set.iterator();
        while (it.hasNext()) {
            ((C3317) it.next()).m7592();
        }
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ᵲᵶᵳˈ */
    public final UUID mo4237() {
        m6219();
        return this.f9634;
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ᵵᵽ */
    public final DrmSession$DrmSessionException mo4238() {
        m6219();
        if (this.f9630 == 1) {
            return this.f9621;
        }
        return null;
    }

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public final boolean m6223() {
        int i = this.f9630;
        return i == 3 || i == 4;
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ᵷᵺʾᵖ */
    public final void mo4239(C3317 c3317) {
        m6219();
        if (this.f9636 < 0) {
            ˋˀᵧ.ᵝᵶ("Session reference count less than zero: " + this.f9636);
            this.f9636 = 0;
        }
        if (c3317 != null) {
            C5523 c5523 = this.f9622;
            synchronized (c5523.f20846) {
                try {
                    ArrayList arrayList = new ArrayList(c5523.f20849);
                    arrayList.add(c3317);
                    c5523.f20849 = DesugarCollections.unmodifiableList(arrayList);
                    Integer num = (Integer) c5523.f20848.get(c3317);
                    if (num == null) {
                        HashSet hashSet = new HashSet(c5523.f20847);
                        hashSet.add(c3317);
                        c5523.f20847 = DesugarCollections.unmodifiableSet(hashSet);
                    }
                    c5523.f20848.put(c3317, Integer.valueOf(num != null ? num.intValue() + 1 : 1));
                } finally {
                }
            }
        }
        int i = this.f9636 + 1;
        this.f9636 = i;
        if (i == 1) {
            ᵻᵪ.ˌᴵ(this.f9630 == 2);
            HandlerThread handlerThread = new HandlerThread("ExoPlayer:DrmRequestHandler");
            this.f9620 = handlerThread;
            handlerThread.start();
            this.f9619 = new HandlerC4324(this, this.f9620.getLooper());
            if (m6217()) {
                m6222(true);
            }
        } else if (c3317 != null && m6223() && this.f9622.m11149(c3317) == 1) {
            c3317.m7593(this.f9630);
        }
        C2076 c2076 = (C2076) this.f9641.f4404;
        if (c2076.f8243 != -9223372036854775807L) {
            c2076.f8237.remove(this);
            Handler handler = c2076.f8250;
            handler.getClass();
            handler.removeCallbacksAndMessages(this);
        }
    }

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final void m6224(InterfaceC3498 interfaceC3498) {
        Set set;
        C5523 c5523 = this.f9622;
        synchronized (c5523.f20846) {
            set = c5523.f20847;
        }
        Iterator it = set.iterator();
        while (it.hasNext()) {
            interfaceC3498.accept((C3317) it.next());
        }
    }

    @Override // defpackage.InterfaceC4417
    /* JADX INFO: renamed from: ᵻˈᵦʼ */
    public final boolean mo4240(String str) {
        m6219();
        byte[] bArr = this.f9624;
        bArr.getClass();
        return this.f9618.mo2488(str, bArr);
    }
}
