Search in sources :

Example 1 with ChunkHolder

use of com.google.android.exoplayer2.source.chunk.ChunkHolder in project ExoPlayer by google.

the class DefaultDashChunkSource method getNextChunk.

@Override
public final void getNextChunk(MediaChunk previous, long playbackPositionUs, ChunkHolder out) {
    if (fatalError != null) {
        return;
    }
    long bufferedDurationUs = previous != null ? (previous.endTimeUs - playbackPositionUs) : 0;
    trackSelection.updateSelectedTrack(bufferedDurationUs);
    RepresentationHolder representationHolder = representationHolders[trackSelection.getSelectedIndex()];
    Representation selectedRepresentation = representationHolder.representation;
    DashSegmentIndex segmentIndex = representationHolder.segmentIndex;
    RangedUri pendingInitializationUri = null;
    RangedUri pendingIndexUri = null;
    if (representationHolder.extractorWrapper.getSampleFormats() == null) {
        pendingInitializationUri = selectedRepresentation.getInitializationUri();
    }
    if (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 nowUs = getNowUnixTimeUs();
    int availableSegmentCount = representationHolder.getSegmentCount();
    if (availableSegmentCount == 0) {
        // The index doesn't define any segments.
        out.endOfStream = !manifest.dynamic || (periodIndex < manifest.getPeriodCount() - 1);
        return;
    }
    int firstAvailableSegmentNum = representationHolder.getFirstSegmentNum();
    int lastAvailableSegmentNum;
    if (availableSegmentCount == DashSegmentIndex.INDEX_UNBOUNDED) {
        // The index is itself unbounded. We need to use the current time to calculate the range of
        // available segments.
        long liveEdgeTimeUs = nowUs - manifest.availabilityStartTime * 1000;
        long periodStartUs = manifest.getPeriod(periodIndex).startMs * 1000;
        long liveEdgeTimeInPeriodUs = liveEdgeTimeUs - periodStartUs;
        if (manifest.timeShiftBufferDepth != C.TIME_UNSET) {
            long bufferDepthUs = manifest.timeShiftBufferDepth * 1000;
            firstAvailableSegmentNum = Math.max(firstAvailableSegmentNum, representationHolder.getSegmentNum(liveEdgeTimeInPeriodUs - bufferDepthUs));
        }
        // getSegmentNum(liveEdgeTimestampUs) will not be completed yet, so subtract one to get the
        // index of the last completed segment.
        lastAvailableSegmentNum = representationHolder.getSegmentNum(liveEdgeTimeInPeriodUs) - 1;
    } else {
        lastAvailableSegmentNum = firstAvailableSegmentNum + availableSegmentCount - 1;
    }
    int segmentNum;
    if (previous == null) {
        segmentNum = Util.constrainValue(representationHolder.getSegmentNum(playbackPositionUs), firstAvailableSegmentNum, lastAvailableSegmentNum);
    } else {
        segmentNum = previous.getNextChunkIndex();
        if (segmentNum < firstAvailableSegmentNum) {
            // This is before the first chunk in the current manifest.
            fatalError = new BehindLiveWindowException();
            return;
        }
    }
    if (segmentNum > lastAvailableSegmentNum || (missingLastSegment && segmentNum >= lastAvailableSegmentNum)) {
        // This is beyond the last chunk in the current manifest.
        out.endOfStream = !manifest.dynamic || (periodIndex < manifest.getPeriodCount() - 1);
        return;
    }
    int maxSegmentCount = Math.min(maxSegmentsPerLoad, lastAvailableSegmentNum - segmentNum + 1);
    out.chunk = newMediaChunk(representationHolder, dataSource, trackSelection.getSelectedFormat(), trackSelection.getSelectionReason(), trackSelection.getSelectionData(), segmentNum, maxSegmentCount);
}
Also used : BehindLiveWindowException(com.google.android.exoplayer2.source.BehindLiveWindowException) RangedUri(com.google.android.exoplayer2.source.dash.manifest.RangedUri) Representation(com.google.android.exoplayer2.source.dash.manifest.Representation)

Example 2 with ChunkHolder

use of com.google.android.exoplayer2.source.chunk.ChunkHolder in project ExoPlayer by google.

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(com.google.android.exoplayer2.source.chunk.BaseMediaChunkIterator) MediaChunkIterator(com.google.android.exoplayer2.source.chunk.MediaChunkIterator) BehindLiveWindowException(com.google.android.exoplayer2.source.BehindLiveWindowException) ContainerMediaChunk(com.google.android.exoplayer2.source.chunk.ContainerMediaChunk) MediaChunk(com.google.android.exoplayer2.source.chunk.MediaChunk) SingleSampleMediaChunk(com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk) RangedUri(com.google.android.exoplayer2.source.dash.manifest.RangedUri) Representation(com.google.android.exoplayer2.source.dash.manifest.Representation) Nullable(androidx.annotation.Nullable)

Example 3 with ChunkHolder

use of com.google.android.exoplayer2.source.chunk.ChunkHolder in project ExoPlayer by google.

the class DefaultDashChunkSourceTest method getNextChunk_onChunkLoadErrorExclusionDisabled_neverRequestReplacementChunk.

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

        @Override
        @Nullable
        public FallbackSelection getFallbackSelectionFor(FallbackOptions fallbackOptions, LoadErrorInfo loadErrorInfo) {
            // Never exclude, neither tracks nor locations.
            return null;
        }
    };
    DashChunkSource chunkSource = createDashChunkSource(/* numberOfTracks= */
    2);
    ChunkHolder output = new ChunkHolder();
    chunkSource.getNextChunk(/* playbackPositionUs= */
    0, /* loadPositionUs= */
    0, /* queue= */
    ImmutableList.of(), output);
    boolean requestReplacementChunk = chunkSource.onChunkLoadError(checkNotNull(output.chunk), /* cancelable= */
    true, createFakeLoadErrorInfo(output.chunk.dataSpec, /* httpResponseCode= */
    404, /* errorCount= */
    1), loadErrorHandlingPolicy);
    assertThat(requestReplacementChunk).isFalse();
}
Also used : DefaultLoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy) ChunkHolder(com.google.android.exoplayer2.source.chunk.ChunkHolder) Test(org.junit.Test)

Example 4 with ChunkHolder

use of com.google.android.exoplayer2.source.chunk.ChunkHolder in project ExoPlayer by google.

the class DefaultDashChunkSourceTest method getNextChunk_onChunkLoadErrorLocationExclusionEnabled_correctFallbackBehavior.

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

        @Override
        public FallbackSelection getFallbackSelectionFor(FallbackOptions fallbackOptions, LoadErrorInfo loadErrorInfo) {
            return new FallbackSelection(FALLBACK_TYPE_LOCATION, DEFAULT_LOCATION_EXCLUSION_MS);
        }
    };
    List<Chunk> chunks = new ArrayList<>();
    DashChunkSource chunkSource = createDashChunkSource(/* numberOfTracks= */
    1);
    ChunkHolder output = new ChunkHolder();
    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_1300000.m4s", "http://video.com/baseUrl/b/video/video_0_1300000.m4s", "http://video.com/baseUrl/d/video/video_0_1300000.m4s").inOrder();
    // Assert expiration of exclusions.
    ShadowSystemClock.advanceBy(Duration.ofMillis(DEFAULT_LOCATION_EXCLUSION_MS));
    chunkSource.onChunkLoadError(checkNotNull(output.chunk), /* cancelable= */
    true, createFakeLoadErrorInfo(output.chunk.dataSpec, /* httpResponseCode= */
    404, /* errorCount= */
    1), loadErrorHandlingPolicy);
    chunkSource.getNextChunk(/* playbackPositionUs= */
    0, /* loadPositionUs= */
    0, /* queue= */
    ImmutableList.of(), output);
    assertThat(output.chunk.dataSpec.uri.toString()).isEqualTo("http://video.com/baseUrl/a/video/video_0_1300000.m4s");
}
Also used : Util(com.google.android.exoplayer2.util.Util) LoadEventInfo(com.google.android.exoplayer2.source.LoadEventInfo) Uri(android.net.Uri) RunWith(org.junit.runner.RunWith) SystemClock(android.os.SystemClock) Random(java.util.Random) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtil(com.google.android.exoplayer2.testutil.TestUtil) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Format(com.google.android.exoplayer2.Format) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) PlayerId(com.google.android.exoplayer2.analytics.PlayerId) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DashManifestParser(com.google.android.exoplayer2.source.dash.manifest.DashManifestParser) Duration(java.time.Duration) AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) C(com.google.android.exoplayer2.C) BundledChunkExtractor(com.google.android.exoplayer2.source.chunk.BundledChunkExtractor) DefaultLoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy) DEFAULT_LOCATION_EXCLUSION_MS(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy.DEFAULT_LOCATION_EXCLUSION_MS) ImmutableMap(com.google.common.collect.ImmutableMap) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) ChunkHolder(com.google.android.exoplayer2.source.chunk.ChunkHolder) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) DefaultBandwidthMeter(com.google.android.exoplayer2.upstream.DefaultBandwidthMeter) List(java.util.List) Nullable(androidx.annotation.Nullable) Chunk(com.google.android.exoplayer2.source.chunk.Chunk) DashManifest(com.google.android.exoplayer2.source.dash.manifest.DashManifest) LoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy) ShadowSystemClock(org.robolectric.shadows.ShadowSystemClock) MediaLoadData(com.google.android.exoplayer2.source.MediaLoadData) Assertions.checkNotNull(com.google.android.exoplayer2.util.Assertions.checkNotNull) Assertions(com.google.android.exoplayer2.util.Assertions) LoaderErrorThrower(com.google.android.exoplayer2.upstream.LoaderErrorThrower) DefaultLoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy) ChunkHolder(com.google.android.exoplayer2.source.chunk.ChunkHolder) ArrayList(java.util.ArrayList) Chunk(com.google.android.exoplayer2.source.chunk.Chunk) Test(org.junit.Test)

Example 5 with ChunkHolder

use of com.google.android.exoplayer2.source.chunk.ChunkHolder in project ExoPlayer by google.

the class DefaultSsChunkSource method getNextChunk.

@Override
public final void getNextChunk(MediaChunk previous, long playbackPositionUs, ChunkHolder out) {
    if (fatalError != null) {
        return;
    }
    long bufferedDurationUs = previous != null ? (previous.endTimeUs - playbackPositionUs) : 0;
    trackSelection.updateSelectedTrack(bufferedDurationUs);
    StreamElement streamElement = manifest.streamElements[elementIndex];
    if (streamElement.chunkCount == 0) {
        // There aren't any chunks for us to load.
        out.endOfStream = !manifest.isLive;
        return;
    }
    int chunkIndex;
    if (previous == null) {
        chunkIndex = streamElement.getChunkIndex(playbackPositionUs);
    } else {
        chunkIndex = previous.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 chunkStartTimeUs = streamElement.getStartTimeUs(chunkIndex);
    long chunkEndTimeUs = chunkStartTimeUs + streamElement.getChunkDurationUs(chunkIndex);
    int currentAbsoluteChunkIndex = chunkIndex + currentManifestChunkOffset;
    int trackSelectionIndex = trackSelection.getSelectedIndex();
    ChunkExtractorWrapper extractorWrapper = extractorWrappers[trackSelectionIndex];
    int manifestTrackIndex = trackSelection.getIndexInTrackGroup(trackSelectionIndex);
    Uri uri = streamElement.buildRequestUri(manifestTrackIndex, chunkIndex);
    out.chunk = newMediaChunk(trackSelection.getSelectedFormat(), dataSource, uri, null, currentAbsoluteChunkIndex, chunkStartTimeUs, chunkEndTimeUs, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), extractorWrapper);
}
Also used : BehindLiveWindowException(com.google.android.exoplayer2.source.BehindLiveWindowException) ChunkExtractorWrapper(com.google.android.exoplayer2.source.chunk.ChunkExtractorWrapper) StreamElement(com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest.StreamElement) Uri(android.net.Uri)

Aggregations

ChunkHolder (com.google.android.exoplayer2.source.chunk.ChunkHolder)5 Test (org.junit.Test)5 Uri (android.net.Uri)4 BehindLiveWindowException (com.google.android.exoplayer2.source.BehindLiveWindowException)4 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)4 DashManifest (com.google.android.exoplayer2.source.dash.manifest.DashManifest)4 DashManifestParser (com.google.android.exoplayer2.source.dash.manifest.DashManifestParser)4 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)4 FixedTrackSelection (com.google.android.exoplayer2.trackselection.FixedTrackSelection)4 LoaderErrorThrower (com.google.android.exoplayer2.upstream.LoaderErrorThrower)4 Nullable (androidx.annotation.Nullable)3 Format (com.google.android.exoplayer2.Format)3 BundledChunkExtractor (com.google.android.exoplayer2.source.chunk.BundledChunkExtractor)3 MediaChunkIterator (com.google.android.exoplayer2.source.chunk.MediaChunkIterator)3 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)3 DefaultLoadErrorHandlingPolicy (com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy)3 SystemClock (android.os.SystemClock)2 ApplicationProvider (androidx.test.core.app.ApplicationProvider)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 C (com.google.android.exoplayer2.C)2