Search in sources :

Example 1 with ChunkHolder

use of androidx.media3.exoplayer.source.chunk.ChunkHolder in project media by androidx.

the class DefaultDashChunkSourceTest method getNextChunk_forVodManifest_doesNotSetMayNotLoadAtFullNetworkSpeedFlag.

@Test
public void getNextChunk_forVodManifest_doesNotSetMayNotLoadAtFullNetworkSpeedFlag() throws Exception {
    long nowMs = 2_000_000_000_000L;
    SystemClock.setCurrentTimeMillis(nowMs);
    DashManifest manifest = new DashManifestParser().parse(Uri.parse("https://example.com/test.mpd"), TestUtil.getInputStream(ApplicationProvider.getApplicationContext(), SAMPLE_MPD_VOD));
    DefaultDashChunkSource chunkSource = new DefaultDashChunkSource(BundledChunkExtractor.FACTORY, new LoaderErrorThrower.Dummy(), manifest, new BaseUrlExclusionList(), /* periodIndex= */
    0, /* adaptationSetIndices= */
    new int[] { 0 }, new FixedTrackSelection(new TrackGroup(new Format.Builder().build()), /* track= */
    0), C.TRACK_TYPE_VIDEO, new FakeDataSource(), /* elapsedRealtimeOffsetMs= */
    0, /* maxSegmentsPerLoad= */
    1, /* enableEventMessageTrack= */
    false, /* closedCaptionFormats */
    ImmutableList.of(), /* playerTrackEmsgHandler= */
    null, PlayerId.UNSET);
    ChunkHolder output = new ChunkHolder();
    chunkSource.getNextChunk(/* playbackPositionUs= */
    0, /* loadPositionUs= */
    0, /* queue= */
    ImmutableList.of(), output);
    assertThat(output.chunk.dataSpec.flags & DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED).isEqualTo(0);
}
Also used : DashManifestParser(androidx.media3.exoplayer.dash.manifest.DashManifestParser) FakeDataSource(androidx.media3.test.utils.FakeDataSource) LoaderErrorThrower(androidx.media3.exoplayer.upstream.LoaderErrorThrower) TrackGroup(androidx.media3.common.TrackGroup) ChunkHolder(androidx.media3.exoplayer.source.chunk.ChunkHolder) DashManifest(androidx.media3.exoplayer.dash.manifest.DashManifest) FixedTrackSelection(androidx.media3.exoplayer.trackselection.FixedTrackSelection) Test(org.junit.Test)

Example 2 with ChunkHolder

use of androidx.media3.exoplayer.source.chunk.ChunkHolder in project media by androidx.

the class DefaultDashChunkSource method getNextChunk.

@Override
public void getNextChunk(long playbackPositionUs, long loadPositionUs, List<? extends MediaChunk> queue, ChunkHolder out) {
    if (fatalError != null) {
        return;
    }
    long bufferedDurationUs = loadPositionUs - playbackPositionUs;
    long presentationPositionUs = Util.msToUs(manifest.availabilityStartTimeMs) + Util.msToUs(manifest.getPeriod(periodIndex).startMs) + loadPositionUs;
    if (playerTrackEmsgHandler != null && playerTrackEmsgHandler.maybeRefreshManifestBeforeLoadingNextChunk(presentationPositionUs)) {
        return;
    }
    long nowUnixTimeUs = Util.msToUs(Util.getNowUnixTimeMs(elapsedRealtimeOffsetMs));
    long nowPeriodTimeUs = getNowPeriodTimeUs(nowUnixTimeUs);
    MediaChunk previous = queue.isEmpty() ? null : queue.get(queue.size() - 1);
    MediaChunkIterator[] chunkIterators = new MediaChunkIterator[trackSelection.length()];
    for (int i = 0; i < chunkIterators.length; i++) {
        RepresentationHolder representationHolder = representationHolders[i];
        if (representationHolder.segmentIndex == null) {
            chunkIterators[i] = MediaChunkIterator.EMPTY;
        } else {
            long firstAvailableSegmentNum = representationHolder.getFirstAvailableSegmentNum(nowUnixTimeUs);
            long lastAvailableSegmentNum = representationHolder.getLastAvailableSegmentNum(nowUnixTimeUs);
            long segmentNum = getSegmentNum(representationHolder, previous, loadPositionUs, firstAvailableSegmentNum, lastAvailableSegmentNum);
            if (segmentNum < firstAvailableSegmentNum) {
                chunkIterators[i] = MediaChunkIterator.EMPTY;
            } else {
                representationHolder = updateSelectedBaseUrl(/* trackIndex= */
                i);
                chunkIterators[i] = new RepresentationSegmentIterator(representationHolder, segmentNum, lastAvailableSegmentNum, nowPeriodTimeUs);
            }
        }
    }
    long availableLiveDurationUs = getAvailableLiveDurationUs(nowUnixTimeUs, playbackPositionUs);
    trackSelection.updateSelectedTrack(playbackPositionUs, bufferedDurationUs, availableLiveDurationUs, queue, chunkIterators);
    RepresentationHolder representationHolder = updateSelectedBaseUrl(trackSelection.getSelectedIndex());
    if (representationHolder.chunkExtractor != null) {
        Representation selectedRepresentation = representationHolder.representation;
        @Nullable RangedUri pendingInitializationUri = null;
        @Nullable RangedUri pendingIndexUri = null;
        if (representationHolder.chunkExtractor.getSampleFormats() == null) {
            pendingInitializationUri = selectedRepresentation.getInitializationUri();
        }
        if (representationHolder.segmentIndex == null) {
            pendingIndexUri = selectedRepresentation.getIndexUri();
        }
        if (pendingInitializationUri != null || pendingIndexUri != null) {
            // We have initialization and/or index requests to make.
            out.chunk = newInitializationChunk(representationHolder, dataSource, trackSelection.getSelectedFormat(), trackSelection.getSelectionReason(), trackSelection.getSelectionData(), pendingInitializationUri, pendingIndexUri);
            return;
        }
    }
    long periodDurationUs = representationHolder.periodDurationUs;
    boolean periodEnded = periodDurationUs != C.TIME_UNSET;
    if (representationHolder.getSegmentCount() == 0) {
        // The index doesn't define any segments.
        out.endOfStream = periodEnded;
        return;
    }
    long firstAvailableSegmentNum = representationHolder.getFirstAvailableSegmentNum(nowUnixTimeUs);
    long lastAvailableSegmentNum = representationHolder.getLastAvailableSegmentNum(nowUnixTimeUs);
    long segmentNum = getSegmentNum(representationHolder, previous, loadPositionUs, firstAvailableSegmentNum, lastAvailableSegmentNum);
    if (segmentNum < firstAvailableSegmentNum) {
        // This is before the first chunk in the current manifest.
        fatalError = new BehindLiveWindowException();
        return;
    }
    if (segmentNum > lastAvailableSegmentNum || (missingLastSegment && segmentNum >= lastAvailableSegmentNum)) {
        // The segment is beyond the end of the period.
        out.endOfStream = periodEnded;
        return;
    }
    if (periodEnded && representationHolder.getSegmentStartTimeUs(segmentNum) >= periodDurationUs) {
        // The period duration clips the period to a position before the segment.
        out.endOfStream = true;
        return;
    }
    int maxSegmentCount = (int) min(maxSegmentsPerLoad, lastAvailableSegmentNum - segmentNum + 1);
    if (periodDurationUs != C.TIME_UNSET) {
        while (maxSegmentCount > 1 && representationHolder.getSegmentStartTimeUs(segmentNum + maxSegmentCount - 1) >= periodDurationUs) {
            // The period duration clips the period to a position before the last segment in the range
            // [segmentNum, segmentNum + maxSegmentCount - 1]. Reduce maxSegmentCount.
            maxSegmentCount--;
        }
    }
    long seekTimeUs = queue.isEmpty() ? loadPositionUs : C.TIME_UNSET;
    out.chunk = newMediaChunk(representationHolder, dataSource, trackType, trackSelection.getSelectedFormat(), trackSelection.getSelectionReason(), trackSelection.getSelectionData(), segmentNum, maxSegmentCount, seekTimeUs, nowPeriodTimeUs);
}
Also used : BaseMediaChunkIterator(androidx.media3.exoplayer.source.chunk.BaseMediaChunkIterator) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) BehindLiveWindowException(androidx.media3.exoplayer.source.BehindLiveWindowException) SingleSampleMediaChunk(androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk) ContainerMediaChunk(androidx.media3.exoplayer.source.chunk.ContainerMediaChunk) MediaChunk(androidx.media3.exoplayer.source.chunk.MediaChunk) RangedUri(androidx.media3.exoplayer.dash.manifest.RangedUri) Representation(androidx.media3.exoplayer.dash.manifest.Representation) Nullable(androidx.annotation.Nullable)

Example 3 with ChunkHolder

use of androidx.media3.exoplayer.source.chunk.ChunkHolder in project media by androidx.

the class DefaultSsChunkSource method getNextChunk.

@Override
public final void getNextChunk(long playbackPositionUs, long loadPositionUs, List<? extends MediaChunk> queue, ChunkHolder out) {
    if (fatalError != null) {
        return;
    }
    StreamElement streamElement = manifest.streamElements[streamElementIndex];
    if (streamElement.chunkCount == 0) {
        // There aren't any chunks for us to load.
        out.endOfStream = !manifest.isLive;
        return;
    }
    int chunkIndex;
    if (queue.isEmpty()) {
        chunkIndex = streamElement.getChunkIndex(loadPositionUs);
    } else {
        chunkIndex = (int) (queue.get(queue.size() - 1).getNextChunkIndex() - currentManifestChunkOffset);
        if (chunkIndex < 0) {
            // This is before the first chunk in the current manifest.
            fatalError = new BehindLiveWindowException();
            return;
        }
    }
    if (chunkIndex >= streamElement.chunkCount) {
        // This is beyond the last chunk in the current manifest.
        out.endOfStream = !manifest.isLive;
        return;
    }
    long bufferedDurationUs = loadPositionUs - playbackPositionUs;
    long timeToLiveEdgeUs = resolveTimeToLiveEdgeUs(playbackPositionUs);
    MediaChunkIterator[] chunkIterators = new MediaChunkIterator[trackSelection.length()];
    for (int i = 0; i < chunkIterators.length; i++) {
        int trackIndex = trackSelection.getIndexInTrackGroup(i);
        chunkIterators[i] = new StreamElementIterator(streamElement, trackIndex, chunkIndex);
    }
    trackSelection.updateSelectedTrack(playbackPositionUs, bufferedDurationUs, timeToLiveEdgeUs, queue, chunkIterators);
    long chunkStartTimeUs = streamElement.getStartTimeUs(chunkIndex);
    long chunkEndTimeUs = chunkStartTimeUs + streamElement.getChunkDurationUs(chunkIndex);
    long chunkSeekTimeUs = queue.isEmpty() ? loadPositionUs : C.TIME_UNSET;
    int currentAbsoluteChunkIndex = chunkIndex + currentManifestChunkOffset;
    int trackSelectionIndex = trackSelection.getSelectedIndex();
    ChunkExtractor chunkExtractor = chunkExtractors[trackSelectionIndex];
    int manifestTrackIndex = trackSelection.getIndexInTrackGroup(trackSelectionIndex);
    Uri uri = streamElement.buildRequestUri(manifestTrackIndex, chunkIndex);
    out.chunk = newMediaChunk(trackSelection.getSelectedFormat(), dataSource, uri, currentAbsoluteChunkIndex, chunkStartTimeUs, chunkEndTimeUs, chunkSeekTimeUs, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), chunkExtractor);
}
Also used : BaseMediaChunkIterator(androidx.media3.exoplayer.source.chunk.BaseMediaChunkIterator) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) BehindLiveWindowException(androidx.media3.exoplayer.source.BehindLiveWindowException) StreamElement(androidx.media3.exoplayer.smoothstreaming.manifest.SsManifest.StreamElement) ChunkExtractor(androidx.media3.exoplayer.source.chunk.ChunkExtractor) BundledChunkExtractor(androidx.media3.exoplayer.source.chunk.BundledChunkExtractor) Uri(android.net.Uri)

Example 4 with ChunkHolder

use of androidx.media3.exoplayer.source.chunk.ChunkHolder in project media by androidx.

the class FakeChunkSource method getNextChunk.

@Override
public void getNextChunk(long playbackPositionUs, long loadPositionUs, List<? extends MediaChunk> queue, ChunkHolder out) {
    long bufferedDurationUs = loadPositionUs - playbackPositionUs;
    int chunkIndex = queue.isEmpty() ? dataSet.getChunkIndexByPosition(playbackPositionUs) : (int) queue.get(queue.size() - 1).getNextChunkIndex();
    MediaChunkIterator[] chunkIterators = new MediaChunkIterator[trackSelection.length()];
    for (int i = 0; i < chunkIterators.length; i++) {
        int trackGroupIndex = trackSelection.getIndexInTrackGroup(i);
        chunkIterators[i] = new FakeAdaptiveDataSet.Iterator(dataSet, trackGroupIndex, chunkIndex);
    }
    trackSelection.updateSelectedTrack(playbackPositionUs, bufferedDurationUs, C.TIME_UNSET, queue, chunkIterators);
    if (chunkIndex >= dataSet.getChunkCount()) {
        out.endOfStream = true;
    } else {
        Format selectedFormat = trackSelection.getSelectedFormat();
        long startTimeUs = dataSet.getStartTime(chunkIndex);
        long endTimeUs = startTimeUs + dataSet.getChunkDuration(chunkIndex);
        int trackGroupIndex = trackSelection.getIndexInTrackGroup(trackSelection.getSelectedIndex());
        String uri = dataSet.getUri(trackGroupIndex);
        Segment fakeDataChunk = Assertions.checkStateNotNull(dataSet.getData(uri)).getSegments().get(chunkIndex);
        DataSpec dataSpec = new DataSpec(Uri.parse(uri), fakeDataChunk.byteOffset, fakeDataChunk.length);
        int trackType = MimeTypes.getTrackType(selectedFormat.sampleMimeType);
        out.chunk = new SingleSampleMediaChunk(dataSource, dataSpec, selectedFormat, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), startTimeUs, endTimeUs, chunkIndex, trackType, selectedFormat);
    }
}
Also used : Format(androidx.media3.common.Format) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) SingleSampleMediaChunk(androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk) DataSpec(androidx.media3.datasource.DataSpec) Segment(androidx.media3.test.utils.FakeDataSet.FakeData.Segment)

Example 5 with ChunkHolder

use of androidx.media3.exoplayer.source.chunk.ChunkHolder in project media by androidx.

the class DefaultDashChunkSourceTest method getNextChunk_onChunkLoadErrorTrackExclusionEnabled_correctFallbackBehavior.

@Test
public void getNextChunk_onChunkLoadErrorTrackExclusionEnabled_correctFallbackBehavior() throws Exception {
    DefaultLoadErrorHandlingPolicy loadErrorHandlingPolicy = new DefaultLoadErrorHandlingPolicy() {

        @Override
        public FallbackSelection getFallbackSelectionFor(FallbackOptions fallbackOptions, LoadErrorInfo loadErrorInfo) {
            // Exclude tracks only.
            return new FallbackSelection(FALLBACK_TYPE_TRACK, DefaultLoadErrorHandlingPolicy.DEFAULT_TRACK_EXCLUSION_MS);
        }
    };
    DashChunkSource chunkSource = createDashChunkSource(/* numberOfTracks= */
    4);
    ChunkHolder output = new ChunkHolder();
    List<Chunk> chunks = new ArrayList<>();
    boolean requestReplacementChunk = true;
    while (requestReplacementChunk) {
        chunkSource.getNextChunk(/* playbackPositionUs= */
        0, /* loadPositionUs= */
        0, /* queue= */
        ImmutableList.of(), output);
        chunks.add(output.chunk);
        requestReplacementChunk = chunkSource.onChunkLoadError(checkNotNull(output.chunk), /* cancelable= */
        true, createFakeLoadErrorInfo(output.chunk.dataSpec, /* httpResponseCode= */
        404, /* errorCount= */
        1), loadErrorHandlingPolicy);
    }
    assertThat(Lists.transform(chunks, (chunk) -> chunk.dataSpec.uri.toString())).containsExactly("http://video.com/baseUrl/a/video/video_0_700000.m4s", "http://video.com/baseUrl/a/video/video_0_452000.m4s", "http://video.com/baseUrl/a/video/video_0_250000.m4s", "http://video.com/baseUrl/a/video/video_0_1300000.m4s").inOrder();
}
Also used : TrackGroup(androidx.media3.common.TrackGroup) LoaderErrorThrower(androidx.media3.exoplayer.upstream.LoaderErrorThrower) Assertions.checkNotNull(androidx.media3.common.util.Assertions.checkNotNull) LoadEventInfo(androidx.media3.exoplayer.source.LoadEventInfo) Uri(android.net.Uri) LoadErrorHandlingPolicy(androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy) RunWith(org.junit.runner.RunWith) DEFAULT_LOCATION_EXCLUSION_MS(androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy.DEFAULT_LOCATION_EXCLUSION_MS) SystemClock(android.os.SystemClock) FixedTrackSelection(androidx.media3.exoplayer.trackselection.FixedTrackSelection) Random(java.util.Random) DefaultLoadErrorHandlingPolicy(androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) HttpDataSource(androidx.media3.datasource.HttpDataSource) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) DashManifestParser(androidx.media3.exoplayer.dash.manifest.DashManifestParser) FakeDataSource(androidx.media3.test.utils.FakeDataSource) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) BundledChunkExtractor(androidx.media3.exoplayer.source.chunk.BundledChunkExtractor) TestUtil(androidx.media3.test.utils.TestUtil) Duration(java.time.Duration) AdaptiveTrackSelection(androidx.media3.exoplayer.trackselection.AdaptiveTrackSelection) Assertions(androidx.media3.common.util.Assertions) ImmutableMap(com.google.common.collect.ImmutableMap) DefaultBandwidthMeter(androidx.media3.exoplayer.upstream.DefaultBandwidthMeter) MediaLoadData(androidx.media3.exoplayer.source.MediaLoadData) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Util(androidx.media3.common.util.Util) Format(androidx.media3.common.Format) C(androidx.media3.common.C) List(java.util.List) Nullable(androidx.annotation.Nullable) PlayerId(androidx.media3.exoplayer.analytics.PlayerId) DashManifest(androidx.media3.exoplayer.dash.manifest.DashManifest) Chunk(androidx.media3.exoplayer.source.chunk.Chunk) ShadowSystemClock(org.robolectric.shadows.ShadowSystemClock) ChunkHolder(androidx.media3.exoplayer.source.chunk.ChunkHolder) DataSpec(androidx.media3.datasource.DataSpec) DefaultLoadErrorHandlingPolicy(androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy) ChunkHolder(androidx.media3.exoplayer.source.chunk.ChunkHolder) ArrayList(java.util.ArrayList) Chunk(androidx.media3.exoplayer.source.chunk.Chunk) Test(org.junit.Test)

Aggregations

ChunkHolder (androidx.media3.exoplayer.source.chunk.ChunkHolder)5 Test (org.junit.Test)5 TrackGroup (androidx.media3.common.TrackGroup)4 DashManifest (androidx.media3.exoplayer.dash.manifest.DashManifest)4 DashManifestParser (androidx.media3.exoplayer.dash.manifest.DashManifestParser)4 FixedTrackSelection (androidx.media3.exoplayer.trackselection.FixedTrackSelection)4 LoaderErrorThrower (androidx.media3.exoplayer.upstream.LoaderErrorThrower)4 FakeDataSource (androidx.media3.test.utils.FakeDataSource)4 Uri (android.net.Uri)3 Nullable (androidx.annotation.Nullable)3 Format (androidx.media3.common.Format)3 DataSpec (androidx.media3.datasource.DataSpec)3 BundledChunkExtractor (androidx.media3.exoplayer.source.chunk.BundledChunkExtractor)3 MediaChunkIterator (androidx.media3.exoplayer.source.chunk.MediaChunkIterator)3 DefaultLoadErrorHandlingPolicy (androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy)3 SystemClock (android.os.SystemClock)2 C (androidx.media3.common.C)2 Assertions (androidx.media3.common.util.Assertions)2 Assertions.checkNotNull (androidx.media3.common.util.Assertions.checkNotNull)2 Util (androidx.media3.common.util.Util)2