package z0;

import a0.n;
import a0.r;
import android.os.Looper;
import android.util.Log;
import b0.AbstractC0399d;
import b0.D;
import b0.o;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import k1.Y;

/* JADX INFO: renamed from: z0.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0752c extends n implements r.a {

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Looper f7130f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private InputStream f7132h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final n.b f7133i;

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private final boolean f7136l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private byte[] f7137m;

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

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private volatile int f7140p;

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private byte[] f7142r;

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

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private final Lock f7145u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private final Condition f7146v;

    public C0752c(n.b bVar, A0.a aVar) {
        super(bVar);
        this.f7127c = 2147483647L;
        this.f7141q = 3;
        this.f7142r = null;
        this.f7143s = 262144;
        this.f7144t = 0L;
        ReentrantLock reentrantLock = new ReentrantLock();
        this.f7145u = reentrantLock;
        this.f7146v = reentrantLock.newCondition();
        this.f7131g = false;
        this.f7136l = false;
        this.f7140p = 0;
        Log.i("DLNAFrameworkDataSource", "isPcmAudio = false");
        this.f7126b = bVar.b();
        Log.i("DLNAFrameworkDataSource", "fileSize = " + this.f7126b);
        if (this.f7126b == 0) {
            this.f7126b = this.f7127c;
            Log.i("DLNAFrameworkDataSource", "fileSize Change to MAX: " + this.f7126b);
        }
        this.f7129e = new r("Loader: Input Stream");
        this.f7130f = Looper.myLooper();
        InputStream inputStreamC = bVar.c();
        this.f7132h = inputStreamC;
        if (inputStreamC instanceof Y) {
            Log.i("DLNAFrameworkDataSource", "it is Samba case");
            this.f7135k = true;
            this.f7128d = false;
        } else {
            Log.i("DLNAFrameworkDataSource", "it is default case");
            this.f7135k = true;
            this.f7128d = false;
        }
    }

    private void i() {
        r rVar = this.f7129e;
        if (rVar != null && rVar.d() && this.f7134j != null) {
            this.f7129e.c();
            this.f7134j = null;
            this.f7129e = null;
        }
        InputStream inputStream = this.f7132h;
        if (inputStream != null) {
            try {
                inputStream.close();
                this.f7132h = null;
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

    private byte[] j(long j2) {
        return new byte[]{(byte) (j2 >>> 56), (byte) (j2 >>> 48), (byte) (j2 >>> 40), (byte) (j2 >>> 32), (byte) (j2 >>> 24), (byte) (j2 >>> 16), (byte) (j2 >>> 8), (byte) j2};
    }

    private int m(long j2, byte[] bArr, int i2) {
        if (this.f7132h == null) {
            InputStream inputStreamC = this.f7133i.c();
            this.f7132h = inputStreamC;
            AbstractC0399d.b(inputStreamC != null, "newInputStream fail!");
        }
        if (this.f7129e == null) {
            this.f7129e = new r("Loader: Input Stream");
        }
        if (this.f7134j == null) {
            this.f7134j = new C0753d(this.f7132h, this.f7133i, this.f7126b, this.f7139o);
            this.f7139o = 0L;
            this.f7129e.h(this.f7130f, this.f7134j, this);
        }
        if (j2 >= this.f7126b) {
            Log.i("DLNAFrameworkDataSource", "offset:" + j2);
            Log.i("DLNAFrameworkDataSource", "fileSize:" + this.f7126b);
            Log.i("DLNAFrameworkDataSource", "offset >= mFileSize return 001");
            return -1;
        }
        if (this.f7131g) {
            InputStream inputStream = this.f7132h;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
            this.f7132h = this.f7133i.c();
            try {
                if (this.f7134j != null) {
                    this.f7134j = new C0753d(this.f7132h, this.f7133i, this.f7126b, this.f7139o);
                    this.f7139o = 0L;
                }
                this.f7129e.h(this.f7130f, this.f7134j, this);
                this.f7131g = false;
                D.h(this.f7132h);
            } catch (Throwable th) {
                D.h(this.f7132h);
                throw th;
            }
        }
        if (!this.f7136l) {
            return this.f7134j.a(j2, bArr, i2);
        }
        if (j2 >= 44) {
            return this.f7134j.a(j2 - 44, bArr, i2);
        }
        if (i2 <= 44 - j2) {
            System.arraycopy(this.f7137m, (int) j2, bArr, 0, i2);
            return i2;
        }
        int i3 = (int) j2;
        int i4 = 44 - i3;
        int i5 = i2 - i4;
        byte[] bArr2 = new byte[i5];
        int iA = this.f7134j.a(0L, bArr2, i5);
        if (iA < 0) {
            return iA;
        }
        System.arraycopy(this.f7137m, i3, bArr, 0, i4);
        System.arraycopy(bArr2, 0, bArr, i4, iA);
        return i4 + iA;
    }

    @Override // a0.n
    protected void a() {
        Log.i("DLNAFrameworkDataSource", "close");
        i();
    }

    @Override // a0.n
    protected long c() {
        Log.i("DLNAFrameworkDataSource", "call getSize(), size = " + this.f7126b);
        if (this.f7126b == 0) {
            this.f7126b = this.f7127c;
            Log.i("DLNAFrameworkDataSource", "fileSize Change to MAX: " + this.f7126b);
        }
        return this.f7126b;
    }

    @Override // a0.n
    protected int e(long j2, byte[] bArr, int i2, int i3) {
        int i4;
        synchronized (this) {
            try {
                if (this.f7140p == -1) {
                    o.d("DLNAFrameworkDataSource", "readAtV23, return -1, currentStatus = " + this.f7140p, 64);
                    return -1;
                }
                if (j2 == this.f7126b - 1 && i3 >= 16) {
                    Log.d("DLNAFrameworkDataSource", "position == fileSize - 1 && size >= 16, get duration");
                    if (!this.f7128d) {
                        return 0;
                    }
                    if (this.f7138n == 0 && ((i4 = this.f7141q) == 1 || i4 == 3)) {
                        return 0;
                    }
                    System.arraycopy(new byte[]{100, 117, 114, 97, 116, 105, 111, 110}, 0, bArr, 0, 8);
                    System.arraycopy(j(((long) this.f7138n) * 1000), 0, bArr, 8, 8);
                    return 16;
                }
                byte[] bArr2 = new byte[i3];
                long jCurrentTimeMillis = System.currentTimeMillis();
                o.d("DLNAFrameworkDataSource", "+readAtV23, position=" + j2 + ", offset=" + i2 + ", size=" + i3, 64);
                int iM = m(j2, bArr2, i3);
                o.d("DLNAFrameworkDataSource", "-readAtV23, position=" + j2 + ", offset=" + i2 + ", size=" + i3 + ", cost(ms)=" + (System.currentTimeMillis() - jCurrentTimeMillis), 64);
                if (iM > 0) {
                    System.arraycopy(bArr2, 0, bArr, i2, iM);
                }
                return iM;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // a0.n
    public void f(long j2) {
        Log.i("DLNAFrameworkDataSource", "seekTo: " + j2 + "(Us) for DLNA");
        if (this.f7132h == null) {
            InputStream inputStreamC = this.f7133i.c();
            this.f7132h = inputStreamC;
            AbstractC0399d.b(inputStreamC != null, "newInputStream fail!");
        }
        if (this.f7129e == null) {
            this.f7129e = new r("Loader: Input Stream");
        }
        if (this.f7134j == null) {
            try {
                C0753d c0753d = new C0753d(this.f7132h, this.f7133i, this.f7126b, j2);
                this.f7134j = c0753d;
                this.f7129e.h(this.f7130f, c0753d, this);
            } finally {
                D.h(this.f7132h);
            }
        }
        this.f7140p = 0;
        this.f7134j.b(j2);
    }

    @Override // a0.n
    public void g() {
        Log.i("DLNAFrameworkDataSource", "begin call tryDisconnect");
        if (this.f7135k) {
            k();
        } else {
            this.f7140p = -1;
            i();
        }
        Log.i("DLNAFrameworkDataSource", "end call tryDisconnect, currentStatus = " + this.f7140p);
    }

    @Override // a0.n
    public void h(long j2) {
        Log.i("DLNAFrameworkDataSource", "call tryResume, positionUs = " + j2);
        this.f7139o = j2;
        if (this.f7132h == null) {
            InputStream inputStreamC = this.f7133i.c();
            this.f7132h = inputStreamC;
            AbstractC0399d.b(inputStreamC != null, "newInputStream fail!");
        } else {
            o();
        }
        if (this.f7129e == null) {
            this.f7129e = new r("Loader: Input Stream");
        }
        if (this.f7134j == null) {
            try {
                this.f7134j = new C0753d(this.f7132h, this.f7133i, this.f7126b, this.f7139o);
                this.f7139o = 0L;
                this.f7129e.h(this.f7130f, this.f7134j, this);
            } finally {
                D.h(this.f7132h);
            }
        }
        this.f7140p = 0;
    }

    protected void k() {
        r rVar = this.f7129e;
        if (rVar != null) {
            rVar.d();
        }
    }

    @Override // a0.r.a
    public void l(r.c cVar) {
        Log.i("DLNAFrameworkDataSource", "onLoadCompleted");
        this.f7131g = true;
    }

    @Override // a0.r.a
    public void n(r.c cVar, IOException iOException) {
    }

    protected void o() {
        r rVar = this.f7129e;
        if (rVar != null) {
            rVar.d();
        }
    }

    @Override // a0.r.a
    public void v(r.c cVar) {
        Log.i("DLNAFrameworkDataSource", "onLoadCanceled");
    }
}
