package defpackage;

import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.media3.session.legacy.PlaybackStateCompat;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import kotlin.Metadata;
import okhttp3.OkHttpClient;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\u0018\u00002\u00020\u0001:\u0002\u0002\u0003¨\u0006\u0004"}, d2 = {"Lg17;", "", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "b", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
public final class g17 {
    public final String a;
    public final OkHttpClient b;
    public final ll7 c;
    public volatile long d;
    public volatile b e;
    public volatile ExecutorService f;
    public final ConcurrentLinkedQueue g;
    public final AtomicBoolean h;
    public q59 i;
    public final AtomicInteger j;
    public final AtomicInteger k;
    public final AtomicInteger l;
    public final AtomicInteger m;
    public final ConcurrentHashMap n;
    public volatile long o;
    public volatile long p;
    public volatile boolean q;

    @Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0007¨\u0006\b"}, d2 = {"Lg17$a;", "", "", "TAG", "Ljava/lang/String;", "", "MAX_CHUNK_SESSION_RETRIES", "I", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
    /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0010\n\u0000\b\u0086\u0081\u0002\u0018\u00002\b\u0012\u0004\u0012\u00020\u00000\u0001¨\u0006\u0002"}, d2 = {"Lg17$b;", "", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class b {
        public static final b a;
        public static final b b;
        public static final b c;
        public static final b d;
        public static final /* synthetic */ b[] e;
        public static final /* synthetic */ hn2 f;

        static {
            b bVar = new b("PARALLEL", 0);
            a = bVar;
            b bVar2 = new b("SINGLE_RANGE", 1);
            b = bVar2;
            b bVar3 = new b("SINGLE_GET", 2);
            c = bVar3;
            b bVar4 = new b("FAILED", 3);
            d = bVar4;
            b[] bVarArr = {bVar, bVar2, bVar3, bVar4};
            e = bVarArr;
            f = jn2.a(bVarArr);
        }

        public static b valueOf(String str) {
            return (b) Enum.valueOf(b.class, str);
        }

        public static b[] values() {
            return (b[]) e.clone();
        }
    }

    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public /* synthetic */ class c {
        static {
            int[] iArr = new int[b.values().length];
            try {
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                b bVar = b.a;
                iArr[1] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                b bVar2 = b.a;
                iArr[2] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                b bVar3 = b.a;
                iArr[3] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    public g17(String str, OkHttpClient okHttpClient, ll7 ll7Var) {
        cr4.g(okHttpClient, "client");
        cr4.g(ll7Var, "config");
        this.a = str;
        this.b = okHttpClient;
        this.c = ll7Var;
        this.d = -1L;
        this.e = b.a;
        this.g = new ConcurrentLinkedQueue();
        this.h = new AtomicBoolean(false);
        this.j = new AtomicInteger(0);
        this.k = new AtomicInteger(0);
        this.l = new AtomicInteger(0);
        this.m = new AtomicInteger(0);
        this.n = new ConcurrentHashMap();
    }

    public final void a() {
        this.h.set(true);
        b();
        q59 q59Var = this.i;
        if (q59Var != null) {
            ReentrantLock reentrantLock = q59Var.b;
            reentrantLock.lock();
            try {
                q59Var.f = true;
                q59Var.a.clear();
                q59Var.e = 0L;
                q59Var.c.signalAll();
            } finally {
                reentrantLock.unlock();
            }
        }
        ExecutorService executorService = this.f;
        if (executorService != null) {
            executorService.shutdownNow();
        }
        this.f = null;
    }

    public final void b() {
        while (true) {
            ConcurrentLinkedQueue concurrentLinkedQueue = this.g;
            if (concurrentLinkedQueue.isEmpty()) {
                return;
            }
            Future future = (Future) concurrentLinkedQueue.poll();
            if (future != null) {
                future.cancel(true);
            }
        }
    }

    public final synchronized void c(int i) {
        Throwable th;
        try {
            try {
                if (this.m.get() != i) {
                    return;
                }
                b bVar = this.e;
                b bVar2 = b.c;
                if (bVar == bVar2 || this.e == b.d) {
                    return;
                }
                this.e = bVar2;
                Log.w("StreamSession", "Mode → SINGLE_GET [gen=" + i + "]");
                b();
                q59 q59Var = this.i;
                if (q59Var != null) {
                    try {
                        q59Var.c(this.o);
                    } catch (Throwable th2) {
                        th = th2;
                    }
                }
                if (!k(new fia(this, i, 2, this.o)) && this.m.get() == i) {
                    f();
                }
                return;
            } catch (Throwable th3) {
                th = th3;
                th = th;
            }
        } catch (Throwable th4) {
            th = th4;
        }
        throw th;
    }

    public final synchronized void d(int i) throws Throwable {
        Throwable th;
        try {
            try {
                if (this.m.get() != i) {
                    return;
                }
                b bVar = this.e;
                b bVar2 = b.b;
                if (bVar == bVar2 || this.e == b.c || this.e == b.d) {
                    return;
                }
                this.e = bVar2;
                Log.w("StreamSession", "Mode → SINGLE_RANGE [gen=" + i + "]");
                b();
                q59 q59Var = this.i;
                if (q59Var != null) {
                    try {
                        q59Var.c(this.o);
                    } catch (Throwable th2) {
                        th = th2;
                    }
                }
                i(i, this.o, this.p);
                return;
            } catch (Throwable th3) {
                th = th3;
            }
        } catch (Throwable th4) {
            th = th4;
        }
        th = th;
        throw th;
    }

    public final synchronized void e(int i, long j) throws Throwable {
        try {
            try {
                if (this.m.get() != i) {
                    return;
                }
                b bVar = this.e;
                b bVar2 = b.b;
                if (bVar == bVar2 || this.e == b.c || this.e == b.d) {
                    return;
                }
                this.e = bVar2;
                Log.w("StreamSession", "Mode → SINGLE_RANGE from " + j + " [gen=" + i + "]");
                b();
                i(i, j, this.p);
            } catch (Throwable th) {
                th = th;
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public final void f() {
        this.e = b.d;
        this.q = true;
        q59 q59Var = this.i;
        if (q59Var != null) {
            ReentrantLock reentrantLock = q59Var.b;
            reentrantLock.lock();
            try {
                q59Var.g = true;
                q59Var.c.signalAll();
            } finally {
                reentrantLock.unlock();
            }
        }
    }

    public final void g(int i, long j, long j2) {
        AtomicInteger atomicInteger = this.m;
        if (atomicInteger.get() != i) {
            return;
        }
        int iIncrementAndGet = this.k.incrementAndGet();
        AtomicInteger atomicInteger2 = this.l;
        int i2 = atomicInteger2.get();
        if (this.j.get() == 0 && iIncrementAndGet >= i2 && !this.h.get() && atomicInteger.get() == i) {
            Log.w("StreamSession", n41.l("All ", iIncrementAndGet, " chunks failed (0 succeeded), degrading to SINGLE_RANGE [gen=", "]", i));
            d(i);
            return;
        }
        int iIncrementAndGet2 = ((AtomicInteger) this.n.computeIfAbsent(Long.valueOf(j), new gb1(new l24(27), 3))).incrementAndGet();
        if (iIncrementAndGet2 > 2) {
            StringBuilder sbP = vw2.p(j, "Chunk ", "-");
            sbP.append(j2);
            sbP.append(" exhausted retries, degrading to SINGLE_RANGE from ");
            sbP.append(j);
            sbP.append(" [gen=");
            sbP.append(i);
            sbP.append("]");
            Log.w("StreamSession", sbP.toString());
            e(i, j);
            return;
        }
        StringBuilder sbP2 = vw2.p(j, "Chunk ", "-");
        sbP2.append(j2);
        sbP2.append(" failed, session retry #");
        sbP2.append(iIncrementAndGet2);
        sbP2.append(" [gen=");
        sbP2.append(i);
        sbP2.append("]");
        Log.w("StreamSession", sbP2.toString());
        atomicInteger2.incrementAndGet();
        if (k(new f17(this, i, j, j2, 1)) || atomicInteger.get() != i) {
            return;
        }
        e(i, j);
    }

    public final void h(int i, long j, long j2) throws Throwable {
        q59 q59Var;
        if (this.m.get() == i && (q59Var = this.i) != null) {
            this.c.getClass();
            long j3 = q59Var.d;
            if (j >= j3) {
                j3 = j;
            }
            int i2 = 0;
            while (j3 <= j2) {
                this.c.getClass();
                if (i2 >= ((int) 10) || this.h.get() || this.m.get() != i) {
                    return;
                }
                long jMin = Math.min(2097151 + j3, j2);
                this.l.incrementAndGet();
                if (!k(new f17(this, i, j3, jMin, 0))) {
                    if (this.m.get() == i) {
                        d(i);
                        return;
                    }
                    return;
                } else {
                    if (this.m.get() == i) {
                        long j4 = jMin + 1;
                        if (j4 > q59Var.d) {
                            q59Var.d = j4;
                        }
                    }
                    j3 = 1 + jMin;
                    i2++;
                }
            }
        }
    }

    public final void i(int i, long j, long j2) {
        if (k(new f17(this, i, j, j2, 2)) || this.m.get() != i) {
            return;
        }
        c(i);
    }

    public final void j(InputStream inputStream, long j, long j2, int i) throws IOException {
        int i2;
        byte[] bArrCopyOf;
        long j3;
        AtomicInteger atomicInteger = this.m;
        if (inputStream == null) {
            if (atomicInteger.get() == i) {
                f();
                return;
            }
            return;
        }
        this.c.getClass();
        byte[] bArr = new byte[(int) PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE];
        if (j2 <= 0) {
            j2 = -1;
        }
        while (!this.h.get() && atomicInteger.get() == i && (i2 = inputStream.read(bArr)) > 0) {
            try {
                if (j2 > 0) {
                    long j4 = ((long) i2) + j;
                    if (j4 <= j2) {
                        j = j4;
                    }
                }
                if (j2 <= 0 || j >= j2) {
                    bArrCopyOf = Arrays.copyOf(bArr, i2);
                    cr4.f(bArrCopyOf, "copyOf(...)");
                    j3 = j;
                } else {
                    bArrCopyOf = st.p(bArr, (int) (j2 - j), i2);
                    j3 = j2;
                }
                if (atomicInteger.get() != i) {
                    break;
                }
                q59 q59Var = this.i;
                if (q59Var != null) {
                    q59Var.b(j3, bArrCopyOf);
                }
                j += (long) i2;
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    f21.a(inputStream, th);
                    throw th2;
                }
            }
        }
        inputStream.close();
    }

    public final boolean k(Runnable runnable) {
        ExecutorService executorService = this.f;
        if (executorService == null) {
            return false;
        }
        try {
            this.g.add(executorService.submit(runnable));
            return true;
        } catch (RejectedExecutionException unused) {
            Log.w("StreamSession", "Executor rejected task");
            return false;
        }
    }
}
