package tv.danmaku.ijk.media.player;

import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.BaseDataSource;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.TransferListener;
import java.io.File;
import java.io.IOException;

/* JADX INFO: loaded from: classes3.dex */
@UnstableApi
public final class RandomMediaExoDataSource extends BaseDataSource {
    private long bytesRemaining;

    @Nullable
    private RandomMediaDataSource mediaDataSource;
    private boolean opened;

    @Nullable
    private Uri openedUri;
    private final PasswordProvider passwordProvider;
    private long readPosition;

    @UnstableApi
    public static final class Factory implements DataSource.Factory {
        private final PasswordProvider passwordProvider;

        @Nullable
        private TransferListener transferListener;

        public Factory(PasswordProvider passwordProvider) {
            this.passwordProvider = passwordProvider;
        }

        @Override // androidx.media3.datasource.DataSource.Factory
        @NonNull
        public DataSource createDataSource() {
            RandomMediaExoDataSource randomMediaExoDataSource = new RandomMediaExoDataSource(this.passwordProvider);
            TransferListener transferListener = this.transferListener;
            if (transferListener != null) {
                randomMediaExoDataSource.addTransferListener(transferListener);
            }
            return randomMediaExoDataSource;
        }

        public Factory setTransferListener(@Nullable TransferListener transferListener) {
            this.transferListener = transferListener;
            return this;
        }
    }

    public interface PasswordProvider {
        String getPassword(Uri uri);
    }

    public RandomMediaExoDataSource(PasswordProvider passwordProvider) {
        super(false);
        this.passwordProvider = passwordProvider;
    }

    private static File resolveFile(Uri uri) {
        if (!"file".equalsIgnoreCase(uri.getScheme())) {
            return new File(uri.toString());
        }
        String path = uri.getPath();
        if (path == null) {
            path = "";
        }
        return new File(path);
    }

    @Override // androidx.media3.datasource.DataSource
    public void close() throws IOException {
        this.openedUri = null;
        this.readPosition = 0L;
        this.bytesRemaining = 0L;
        RandomMediaDataSource randomMediaDataSource = this.mediaDataSource;
        if (randomMediaDataSource != null) {
            randomMediaDataSource.close();
            this.mediaDataSource = null;
        }
        if (this.opened) {
            this.opened = false;
            transferEnded();
        }
    }

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

    @Override // androidx.media3.datasource.DataSource
    public long open(@NonNull DataSpec dataSpec) throws IOException {
        transferInitializing(dataSpec);
        Uri uri = dataSpec.uri;
        String password = this.passwordProvider.getPassword(uri);
        if (password == null || password.isEmpty()) {
            throw new IOException("Missing password for uri: " + uri);
        }
        File fileResolveFile = resolveFile(uri);
        if (!fileResolveFile.exists() || !fileResolveFile.isFile()) {
            throw new IOException("File not found: " + fileResolveFile.getAbsolutePath());
        }
        try {
            RandomMediaDataSource randomMediaDataSource = new RandomMediaDataSource(fileResolveFile, password);
            this.mediaDataSource = randomMediaDataSource;
            long size = randomMediaDataSource.getSize();
            long j10 = dataSpec.position;
            this.readPosition = j10;
            if (j10 < 0 || j10 > size) {
                throw new IOException("Invalid start position: " + this.readPosition + ", fileSize=" + size);
            }
            long j11 = dataSpec.length;
            this.bytesRemaining = j11 == -1 ? size - j10 : Math.min(j11, size - j10);
            this.openedUri = uri;
            this.opened = true;
            transferStarted(dataSpec);
            long j12 = dataSpec.length;
            return j12 == -1 ? this.bytesRemaining : j12;
        } catch (Exception e10) {
            close();
            if (e10 instanceof IOException) {
                throw ((IOException) e10);
            }
            throw new IOException("Failed to open RandomMediaDataSource", e10);
        }
    }

    @Override // androidx.media3.common.DataReader
    public int read(@NonNull byte[] bArr, int i10, int i11) throws IOException {
        if (i11 == 0) {
            return 0;
        }
        long j10 = this.bytesRemaining;
        if (j10 == 0) {
            return -1;
        }
        if (this.mediaDataSource == null) {
            throw new IOException("DataSource is not opened");
        }
        try {
            int at = this.mediaDataSource.readAt(this.readPosition, bArr, i10, (int) Math.min(i11, j10));
            if (at <= 0) {
                return -1;
            }
            long j11 = at;
            this.readPosition += j11;
            this.bytesRemaining -= j11;
            bytesTransferred(at);
            return at;
        } catch (Exception e10) {
            throw new IOException("Failed to read encrypted media", e10);
        }
    }
}
