package G;

import a0.n;
import a0.r;
import android.content.Context;
import android.media.MediaExtractor;
import android.net.Uri;
import android.util.Log;
import b0.AbstractC0399d;
import b0.D;
import b0.k;
import b0.o;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import n.C0633A;
import n.InterfaceC0634B;
import n.x;
import n.y;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public class c implements InterfaceC0634B, InterfaceC0634B.a, r.a {

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private static Method f725y;

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

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Map f730e;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private boolean[] f739n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private int[] f740o;

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

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

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

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

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

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

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private Lock f747v;

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

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

    static {
        Class<?> cls;
        try {
            if (D.f2965a >= 23) {
                try {
                    cls = Class.forName("android.media.MediaDataSource");
                } catch (ClassNotFoundException unused) {
                    o.d("NonBlockingFSS", "android.media.MediaDataSource(API 23) not found", 8);
                    cls = null;
                }
                if (cls != null) {
                    f725y = MediaExtractor.class.getMethod("setDataSource", cls);
                }
                if (f725y != null) {
                    o.d("NonBlockingFSS", "setDataSource(MediaDataSource dataSource)(API 23) found", 8);
                }
            }
        } catch (NoSuchMethodException unused2) {
            o.d("NonBlockingFSS", "setDataSource method(API 22/23) not found", 8);
        }
    }

    public c(Context context, Uri uri, Map map, String... strArr) {
        this(context, uri, map, null, true, strArr);
    }

    private boolean g() throws Throwable {
        long length;
        long j2;
        RandomAccessFile randomAccessFile;
        Uri uri = this.f729d;
        if (uri == null || "http".equals(uri.getScheme()) || "https".equals(this.f729d.getScheme())) {
            return true;
        }
        RandomAccessFile randomAccessFile2 = null;
        try {
            try {
                randomAccessFile = new RandomAccessFile(this.f729d.getPath(), "r");
            } catch (IOException unused) {
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            length = randomAccessFile.length();
            try {
                randomAccessFile.close();
            } catch (IOException unused2) {
                o.d(this.f726a, "file close fail uri: " + this.f729d.getPath(), 8);
            }
        } catch (IOException unused3) {
            randomAccessFile2 = randomAccessFile;
            o.d(this.f726a, "get file length fail uri: " + this.f729d.getPath(), 8);
            if (randomAccessFile2 != null) {
                try {
                    randomAccessFile2.close();
                } catch (IOException unused4) {
                    o.d(this.f726a, "file close fail uri: " + this.f729d.getPath(), 8);
                }
            }
            length = 0;
        } catch (Throwable th2) {
            th = th2;
            randomAccessFile2 = randomAccessFile;
            if (randomAccessFile2 != null) {
                try {
                    randomAccessFile2.close();
                } catch (IOException unused5) {
                    o.d(this.f726a, "file close fail uri: " + this.f729d.getPath(), 8);
                }
            }
            throw th;
        }
        int i2 = 0;
        while (true) {
            j2 = -1;
            if (i2 < c()) {
                x xVarA = a(i2);
                if (xVarA != null && xVarA.f5856y != -1 && k.g(xVarA.f5842g)) {
                    j2 = xVarA.f5856y;
                    break;
                }
                i2++;
            } else {
                break;
            }
        }
        if (j2 > 0 && j2 >= 1000000) {
            this.f741p.j((length / (j2 / 1000000)) * 8);
        }
        return true;
    }

    @Override // n.InterfaceC0634B.a
    public x a(int i2) {
        return this.f737l[i2];
    }

    public void b(int i2, long j2) {
        o.d(this.f726a, "disable track = " + i2 + ", positionUs = " + j2, 8);
        this.f740o[i2] = 0;
        this.f741p.b(i2, j2);
    }

    @Override // n.InterfaceC0634B.a
    public int c() {
        return this.f740o.length;
    }

    @Override // n.InterfaceC0634B.a
    public void d(int i2, long j2) {
        o.d(this.f726a, "enable track = " + i2, 8);
        this.f740o[i2] = 1;
        x xVarA = a(i2);
        if (xVarA == null) {
            Log.e(this.f726a, "get Format is null");
            return;
        }
        if (this.f746u != null && k.g(xVarA.f5842g) && j2 != 0) {
            this.f746u.f(j2);
        }
        this.f741p.c(i2, j2);
        if (!k.g(xVarA.f5842g) || j2 == 0) {
            return;
        }
        this.f738m[i2] = true;
        this.f739n[i2] = true;
        this.f743r = j2 * (-1);
    }

    @Override // n.InterfaceC0634B.a
    public void e() throws IOException {
        IOException iOException = this.f732g;
        if (iOException != null) {
            throw iOException;
        }
    }

    @Override // n.InterfaceC0634B.a
    public boolean f(int i2, long j2) {
        AbstractC0399d.e(this.f733h);
        AbstractC0399d.e(this.f741p != null);
        long j3 = this.f736k[i2];
        if (j3 <= 0 || j3 >= Math.abs(this.f743r)) {
            return this.f741p.a(i2, j2);
        }
        return true;
    }

    @Override // n.InterfaceC0634B.a
    public int h() {
        return 8;
    }

    public void i(int i2) {
        if (this.f749x != i2) {
            o.d(this.f726a, "set focus mode " + i2, 8);
            this.f749x = i2;
            if (this.f733h) {
                this.f741p.k(i2);
            }
        }
    }

    @Override // n.InterfaceC0634B.a
    public long j(int i2) {
        boolean[] zArr = this.f738m;
        if (!zArr[i2]) {
            return Long.MIN_VALUE;
        }
        zArr[i2] = false;
        long j2 = this.f744s;
        return j2 > 0 ? j2 : this.f743r;
    }

    @Override // n.InterfaceC0634B.a
    public void k(int i2) {
        b(i2, -1L);
    }

    @Override // a0.r.a
    public void l(r.c cVar) {
    }

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

    @Override // n.InterfaceC0634B.a
    public void o(long j2) {
        o.d(this.f726a, "seekToUs position = " + j2 + ", lastSeekPositionUs=" + this.f743r + ", readAnySample=" + this.f734i, 8);
        if (this.f734i || this.f743r != j2) {
            this.f747v.lock();
            this.f743r = j2;
            this.f744s = -1L;
            this.f734i = false;
            this.f735j = false;
            n nVar = this.f746u;
            if (nVar != null) {
                nVar.f(j2);
            }
            this.f744s = this.f741p.i(j2);
            for (int i2 = 0; i2 < c(); i2++) {
                if (this.f740o[i2] != 0) {
                    this.f738m[i2] = true;
                    this.f739n[i2] = true;
                }
            }
            this.f747v.unlock();
        }
    }

    @Override // n.InterfaceC0634B
    public InterfaceC0634B.a p() {
        this.f742q++;
        return this;
    }

    @Override // n.InterfaceC0634B.a
    public int r(int i2, long j2, y yVar, C0633A c0633a) {
        if (this.f738m[i2] || !this.f747v.tryLock()) {
            return -2;
        }
        if (this.f740o[i2] != 2) {
            yVar.f5857a = a(i2);
            yVar.f5858b = null;
            this.f740o[i2] = 2;
            this.f747v.unlock();
            return -4;
        }
        if (c0633a == null) {
            this.f747v.unlock();
            return -2;
        }
        if (c0633a.f5409b == null) {
            c0633a.c(6291456);
        }
        if (!this.f741p.e(i2)) {
            long j3 = this.f736k[i2];
            if (j3 <= 0 || j3 >= Math.abs(this.f743r)) {
                try {
                    int iG = this.f741p.g(i2, c0633a);
                    if (iG == -3) {
                        int i3 = (c0633a.f5412e < this.f743r ? 134217728 : 0) | c0633a.f5411d;
                        c0633a.f5411d = i3;
                        boolean[] zArr = this.f739n;
                        if (zArr[i2]) {
                            zArr[i2] = false;
                            c0633a.f5411d = 67108864 | i3;
                            if ("ESExtractor".equals(this.f748w)) {
                                c0633a.f5412e = this.f743r;
                            }
                        }
                        this.f734i = true;
                    }
                    this.f747v.unlock();
                    return iG;
                } catch (Throwable th) {
                    this.f747v.unlock();
                    throw th;
                }
            }
        }
        o.d(this.f726a, "track[" + i2 + "] reach EOS. durationUs:" + this.f736k[i2] + ", lastSeekPositionUs:" + this.f743r + ", isEndOfStream=" + this.f741p.e(i2), 8);
        this.f747v.unlock();
        return -1;
    }

    @Override // n.InterfaceC0634B.a
    public void release() {
        b bVar;
        Log.i(this.f726a, "release remainingReleaseCount = " + this.f742q);
        int i2 = this.f742q + (-1);
        this.f742q = i2;
        if (i2 != 0 || (bVar = this.f741p) == null) {
            return;
        }
        bVar.h();
    }

    /* JADX WARN: Removed duplicated region for block: B:47:0x0153  */
    /* JADX WARN: Removed duplicated region for block: B:60:0x019c  */
    @Override // n.InterfaceC0634B.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public boolean s(long r13) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 529
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: G.c.s(long):boolean");
    }

    @Override // n.InterfaceC0634B.a
    public long t() {
        return this.f741p.d();
    }

    @Override // a0.r.a
    public void v(r.c cVar) {
    }

    public c(Context context, n nVar, String... strArr) {
        this(context, null, null, nVar, true, strArr);
    }

    private c(Context context, Uri uri, Map map, n nVar, boolean z2, String... strArr) {
        this.f749x = 0;
        String str = (strArr == null || strArr.length == 0) ? HttpUrl.FRAGMENT_ENCODE_SET : strArr[0];
        this.f728c = str;
        this.f726a = "NonBlockingFSS" + str;
        this.f727b = context;
        this.f729d = uri;
        this.f730e = map;
        this.f746u = nVar;
        this.f731f = z2;
        if (nVar != null) {
            this.f745t = nVar.b();
        }
        this.f747v = new ReentrantLock();
        this.f743r = -1L;
        this.f744s = -1L;
    }
}
