package androidx.media3.exoplayer.dash.offline;

import androidx.annotation.Nullable;
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.o;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import m.a;

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

    public static final class Factory extends SegmentDownloader.BaseFactory<DashManifest> {
        public Factory(CacheDataSource.Factory factory) {
            super(factory, new DashManifestParser());
        }

        @CanIgnoreReturnValue
        public Factory setManifestParser(DashManifestParser dashManifestParser) {
            this.manifestParser = dashManifestParser;
            return this;
        }

        @Override // androidx.media3.exoplayer.offline.SegmentDownloaderFactory
        public DashDownloader create(MediaItem mediaItem) {
            return new DashDownloader(mediaItem, this.manifestParser, this.cacheDataSourceFactory, this.executor, this.maxMergedSegmentStartTimeDiffMs, this.startPositionUs, this.durationUs);
        }

        @Override // androidx.media3.exoplayer.offline.SegmentDownloader.BaseFactory, androidx.media3.exoplayer.offline.SegmentDownloaderFactory
        @CanIgnoreReturnValue
        public Factory setDurationUs(long j10) {
            super.setDurationUs(j10);
            return this;
        }

        @Override // androidx.media3.exoplayer.offline.SegmentDownloader.BaseFactory, androidx.media3.exoplayer.offline.SegmentDownloaderFactory
        @CanIgnoreReturnValue
        public Factory setExecutor(Executor executor) {
            super.setExecutor(executor);
            return this;
        }

        @Override // androidx.media3.exoplayer.offline.SegmentDownloader.BaseFactory, androidx.media3.exoplayer.offline.SegmentDownloaderFactory
        @CanIgnoreReturnValue
        public Factory setMaxMergedSegmentStartTimeDiffMs(long j10) {
            super.setMaxMergedSegmentStartTimeDiffMs(j10);
            return this;
        }

        @Override // androidx.media3.exoplayer.offline.SegmentDownloader.BaseFactory, androidx.media3.exoplayer.offline.SegmentDownloaderFactory
        @CanIgnoreReturnValue
        public Factory setStartPositionUs(long j10) {
            super.setStartPositionUs(j10);
            return this;
        }
    }

    private void addSegmentsForAdaptationSet(DataSource dataSource, AdaptationSet adaptationSet, long j10, long j11, boolean z10, ArrayList<SegmentDownloader.Segment> arrayList) throws InterruptedException, IOException {
        String str;
        for (int i10 = 0; i10 < adaptationSet.representations.size(); i10++) {
            Representation representation = adaptationSet.representations.get(i10);
            try {
            } catch (IOException e10) {
                e = e10;
            }
            try {
                DashSegmentIndex segmentIndex = getSegmentIndex(dataSource, adaptationSet.type, representation, z10);
                if (segmentIndex == null) {
                    throw new DownloadException("Missing segment index");
                }
                long segmentCount = segmentIndex.getSegmentCount(j11);
                if (segmentCount == -1) {
                    throw new DownloadException("Unbounded segment index");
                }
                String str2 = ((BaseUrl) Util.castNonNull(this.baseUrlExclusionList.selectBaseUrl(representation.baseUrls))).url;
                RangedUri initializationUri = representation.getInitializationUri();
                if (initializationUri != null) {
                    str = str2;
                    arrayList.add(createSegment(representation, str2, j10, initializationUri));
                } else {
                    str = str2;
                }
                RangedUri indexUri = representation.getIndexUri();
                if (indexUri != null) {
                    arrayList.add(createSegment(representation, str, j10, indexUri));
                }
                long j12 = this.startPositionUs - j10;
                long j13 = this.durationUs;
                long j14 = j13 != -9223372036854775807L ? j13 + j12 : -9223372036854775807L;
                long firstSegmentNum = (z10 || j12 <= 0) ? segmentIndex.getFirstSegmentNum() : segmentIndex.getSegmentNum(j12, j11);
                long firstSegmentNum2 = (j14 == -9223372036854775807L || z10 || j14 >= j10 + j11) ? (segmentIndex.getFirstSegmentNum() + segmentCount) - 1 : segmentIndex.getSegmentNum(j14, j11);
                for (long j15 = firstSegmentNum; j15 <= firstSegmentNum2; j15++) {
                    arrayList.add(createSegment(representation, str, segmentIndex.getTimeUs(j15) + j10, segmentIndex.getSegmentUrl(j15)));
                }
            } catch (IOException e11) {
                e = e11;
                if (!z10) {
                    throw e;
                }
            }
        }
    }

    private SegmentDownloader.Segment createSegment(Representation representation, String str, long j10, RangedUri rangedUri) {
        return new SegmentDownloader.Segment(j10, DashUtil.buildDataSpec(representation, str, rangedUri, 0, ImmutableMap.g()));
    }

    @Nullable
    private DashSegmentIndex getSegmentIndex(final DataSource dataSource, final int i10, final Representation representation, boolean z10) throws InterruptedException, IOException {
        DashSegmentIndex index = representation.getIndex();
        if (index != null) {
            return index;
        }
        o oVar = (o) execute(new RunnableFutureTask<o, 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 o doWork() throws IOException {
                return DashUtil.loadChunkIndex(dataSource, i10, representation);
            }
        }, z10);
        if (oVar == null) {
            return null;
        }
        return new DashWrappingSegmentIndex(oVar, representation.presentationTimeOffsetUs);
    }

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

    @Override // androidx.media3.exoplayer.offline.SegmentDownloader
    public List<SegmentDownloader.Segment> getSegments(DataSource dataSource, DashManifest dashManifest, boolean z10) throws InterruptedException, IOException {
        ArrayList<SegmentDownloader.Segment> arrayList = new ArrayList<>();
        for (int i10 = 0; i10 < dashManifest.getPeriodCount(); i10++) {
            Period period = dashManifest.getPeriod(i10);
            long jMsToUs = Util.msToUs(period.startMs);
            long periodDurationUs = dashManifest.getPeriodDurationUs(i10);
            if (periodDurationUs == -9223372036854775807L || jMsToUs + periodDurationUs > this.startPositionUs) {
                long j10 = this.durationUs;
                if (j10 != -9223372036854775807L && jMsToUs >= this.startPositionUs + j10) {
                    break;
                }
                int i11 = 0;
                for (List<AdaptationSet> list = period.adaptationSets; i11 < list.size(); list = list) {
                    addSegmentsForAdaptationSet(dataSource, list.get(i11), jMsToUs, periodDurationUs, z10, arrayList);
                    i11++;
                }
            }
        }
        return arrayList;
    }

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

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