package androidx.media3.exoplayer.dash.offline;

import F3.u0;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.RunnableFutureTask;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.cache.CacheDataSource;
import androidx.media3.exoplayer.dash.BaseUrlExclusionList;
import androidx.media3.exoplayer.dash.DashSegmentIndex;
import androidx.media3.exoplayer.dash.DashUtil;
import androidx.media3.exoplayer.dash.DashWrappingSegmentIndex;
import androidx.media3.exoplayer.dash.manifest.AdaptationSet;
import androidx.media3.exoplayer.dash.manifest.BaseUrl;
import androidx.media3.exoplayer.dash.manifest.DashManifest;
import androidx.media3.exoplayer.dash.manifest.DashManifestParser;
import androidx.media3.exoplayer.dash.manifest.Period;
import androidx.media3.exoplayer.dash.manifest.RangedUri;
import androidx.media3.exoplayer.dash.manifest.Representation;
import androidx.media3.exoplayer.offline.DownloadException;
import androidx.media3.exoplayer.offline.SegmentDownloader;
import androidx.media3.exoplayer.upstream.ParsingLoadable;
import androidx.media3.extractor.ChunkIndex;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class DashDownloader extends SegmentDownloader<DashManifest> {
    private final BaseUrlExclusionList baseUrlExclusionList;

    public DashDownloader(MediaItem mediaItem, CacheDataSource.Factory factory) {
        this(mediaItem, factory, new a());
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x00ae A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:48:0x00a8 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private void addSegmentsForAdaptationSet(DataSource dataSource, AdaptationSet adaptationSet, long j4, long j6, boolean z6, ArrayList<SegmentDownloader.Segment> arrayList) throws IOException {
        DashSegmentIndex segmentIndex;
        DashDownloader dashDownloader = this;
        int i6 = 0;
        while (i6 < adaptationSet.representations.size()) {
            Representation representation = adaptationSet.representations.get(i6);
            try {
            } catch (IOException e6) {
                e = e6;
            }
            try {
                segmentIndex = dashDownloader.getSegmentIndex(dataSource, adaptationSet.type, representation, z6);
            } catch (IOException e7) {
                e = e7;
                if (z6) {
                    throw e;
                }
                i6++;
                dashDownloader = this;
            }
            if (segmentIndex != null) {
                long segmentCount = segmentIndex.getSegmentCount(j6);
                if (segmentCount == -1) {
                    throw new DownloadException("Unbounded segment index");
                }
                String str = ((BaseUrl) Util.castNonNull(dashDownloader.baseUrlExclusionList.selectBaseUrl(representation.baseUrls))).url;
                RangedUri initializationUri = representation.getInitializationUri();
                if (initializationUri != null) {
                    arrayList.add(dashDownloader.createSegment(representation, str, j4, initializationUri));
                }
                RangedUri indexUri = representation.getIndexUri();
                if (indexUri != null) {
                    arrayList.add(createSegment(representation, str, j4, indexUri));
                }
                long firstSegmentNum = segmentIndex.getFirstSegmentNum();
                long j7 = (segmentCount + firstSegmentNum) - 1;
                while (firstSegmentNum <= j7) {
                    arrayList.add(createSegment(representation, str, segmentIndex.getTimeUs(firstSegmentNum) + j4, segmentIndex.getSegmentUrl(firstSegmentNum)));
                    firstSegmentNum++;
                }
                i6++;
                dashDownloader = this;
            } else {
                try {
                    throw new DownloadException("Missing segment index");
                } catch (IOException e8) {
                    e = e8;
                    if (z6) {
                    }
                }
            }
        }
    }

    private SegmentDownloader.Segment createSegment(Representation representation, String str, long j4, RangedUri rangedUri) {
        return new SegmentDownloader.Segment(j4, DashUtil.buildDataSpec(representation, str, rangedUri, 0, u0.f1605j));
    }

    private DashSegmentIndex getSegmentIndex(final DataSource dataSource, final int i6, final Representation representation, boolean z6) {
        DashSegmentIndex index = representation.getIndex();
        if (index != null) {
            return index;
        }
        ChunkIndex chunkIndex = (ChunkIndex) execute(new RunnableFutureTask<ChunkIndex, IOException>() { // from class: androidx.media3.exoplayer.dash.offline.DashDownloader.1
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // androidx.media3.common.util.RunnableFutureTask
            public ChunkIndex doWork() {
                return DashUtil.loadChunkIndex(dataSource, i6, representation);
            }
        }, z6);
        if (chunkIndex == null) {
            return null;
        }
        return new DashWrappingSegmentIndex(chunkIndex, representation.presentationTimeOffsetUs);
    }

    public DashDownloader(MediaItem mediaItem, CacheDataSource.Factory factory, Executor executor) {
        this(mediaItem, new DashManifestParser(), factory, executor, 20000L);
    }

    @Override // androidx.media3.exoplayer.offline.SegmentDownloader
    public List<SegmentDownloader.Segment> getSegments(DataSource dataSource, DashManifest dashManifest, boolean z6) throws IOException {
        ArrayList<SegmentDownloader.Segment> arrayList = new ArrayList<>();
        for (int i6 = 0; i6 < dashManifest.getPeriodCount(); i6++) {
            Period period = dashManifest.getPeriod(i6);
            long jMsToUs = Util.msToUs(period.startMs);
            long periodDurationUs = dashManifest.getPeriodDurationUs(i6);
            List<AdaptationSet> list = period.adaptationSets;
            for (int i7 = 0; i7 < list.size(); i7++) {
                addSegmentsForAdaptationSet(dataSource, list.get(i7), jMsToUs, periodDurationUs, z6, arrayList);
            }
        }
        return arrayList;
    }

    @Deprecated
    public DashDownloader(MediaItem mediaItem, ParsingLoadable.Parser<DashManifest> parser, CacheDataSource.Factory factory, Executor executor) {
        this(mediaItem, parser, factory, executor, 20000L);
    }

    public DashDownloader(MediaItem mediaItem, ParsingLoadable.Parser<DashManifest> parser, CacheDataSource.Factory factory, Executor executor, long j4) {
        super(mediaItem, parser, factory, executor, j4);
        this.baseUrlExclusionList = new BaseUrlExclusionList();
    }
}
