package d3;

import android.net.Uri;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.TransferListener;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: renamed from: d3.w, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1933w implements DataSource {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final DataSource f16131a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final byte[] f16132b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile int f16133c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile int f16134d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public volatile int f16135e;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public Thread f16139i;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final AtomicBoolean f16136f = new AtomicBoolean(false);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final AtomicBoolean f16137g = new AtomicBoolean(false);

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Object f16138h = new Object();

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public long f16140j = -1;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public long f16141k = System.currentTimeMillis();

    public C1933w(DataSource dataSource, int i6) {
        this.f16131a = dataSource;
        this.f16132b = new byte[i6];
    }

    @Override // androidx.media3.datasource.DataSource
    public final void addTransferListener(TransferListener listener) {
        kotlin.jvm.internal.o.h(listener, "listener");
        this.f16131a.addTransferListener(listener);
    }

    @Override // androidx.media3.datasource.DataSource
    public final void close() {
        this.f16136f.set(true);
        synchronized (this.f16138h) {
            this.f16138h.notifyAll();
        }
        try {
            this.f16131a.close();
        } catch (Exception unused) {
        }
        try {
            Thread thread = this.f16139i;
            if (thread != null) {
                thread.interrupt();
            }
            Thread thread2 = this.f16139i;
            if (thread2 != null) {
                thread2.join(2000L);
            }
            this.f16139i = null;
        } catch (InterruptedException unused2) {
        }
    }

    @Override // androidx.media3.datasource.DataSource
    public final Uri getUri() {
        return this.f16131a.getUri();
    }

    @Override // androidx.media3.datasource.DataSource
    public final long open(DataSpec dataSpec) {
        kotlin.jvm.internal.o.h(dataSpec, "dataSpec");
        this.f16136f.set(false);
        this.f16137g.set(false);
        this.f16133c = 0;
        this.f16134d = 0;
        this.f16135e = 0;
        this.f16140j = this.f16131a.open(dataSpec);
        Thread thread = new Thread(new F1.r(this, 18));
        this.f16139i = thread;
        thread.setName("PrefetchNetwork");
        Thread thread2 = this.f16139i;
        kotlin.jvm.internal.o.e(thread2);
        thread2.setPriority(6);
        Thread thread3 = this.f16139i;
        kotlin.jvm.internal.o.e(thread3);
        thread3.start();
        return this.f16140j;
    }

    @Override // androidx.media3.common.DataReader
    public final int read(byte[] target, int i6, int i7) {
        kotlin.jvm.internal.o.h(target, "target");
        synchronized (this.f16138h) {
            while (this.f16135e == 0 && !this.f16137g.get() && !this.f16136f.get()) {
                long jCurrentTimeMillis = System.currentTimeMillis() - this.f16141k;
                if (jCurrentTimeMillis > 120000) {
                    throw new IOException("Network stalled for " + jCurrentTimeMillis + " ms");
                }
                try {
                    this.f16138h.wait(50L);
                } catch (InterruptedException e6) {
                    Thread.currentThread().interrupt();
                    throw new IOException("Read interrupted", e6);
                }
            }
            if (this.f16135e == 0 && this.f16137g.get()) {
                return -1;
            }
            int iMin = Math.min(i7, this.f16135e);
            int iMin2 = Math.min(iMin, this.f16132b.length - this.f16133c);
            System.arraycopy(this.f16132b, this.f16133c, target, i6, iMin2);
            int i8 = iMin - iMin2;
            if (i8 > 0) {
                System.arraycopy(this.f16132b, 0, target, i6 + iMin2, i8);
            }
            this.f16133c = (this.f16133c + iMin) % this.f16132b.length;
            this.f16135e -= iMin;
            this.f16138h.notifyAll();
            return iMin;
        }
    }
}
