Search in sources :

Example 1 with MediaChunkIterator

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

the class AdaptiveTrackSelection method updateSelectedTrack.

@Override
public void updateSelectedTrack(long playbackPositionUs, long bufferedDurationUs, long availableDurationUs, List<? extends MediaChunk> queue, MediaChunkIterator[] mediaChunkIterators) {
    long nowMs = clock.elapsedRealtime();
    long chunkDurationUs = getNextChunkDurationUs(mediaChunkIterators, queue);
    // Make initial selection
    if (reason == C.SELECTION_REASON_UNKNOWN) {
        reason = C.SELECTION_REASON_INITIAL;
        selectedIndex = determineIdealSelectedIndex(nowMs, chunkDurationUs);
        return;
    }
    int previousSelectedIndex = selectedIndex;
    @C.SelectionReason int previousReason = reason;
    int formatIndexOfPreviousChunk = queue.isEmpty() ? C.INDEX_UNSET : indexOf(Iterables.getLast(queue).trackFormat);
    if (formatIndexOfPreviousChunk != C.INDEX_UNSET) {
        previousSelectedIndex = formatIndexOfPreviousChunk;
        previousReason = Iterables.getLast(queue).trackSelectionReason;
    }
    int newSelectedIndex = determineIdealSelectedIndex(nowMs, chunkDurationUs);
    if (!isBlacklisted(previousSelectedIndex, nowMs)) {
        // Revert back to the previous selection if conditions are not suitable for switching.
        Format currentFormat = getFormat(previousSelectedIndex);
        Format selectedFormat = getFormat(newSelectedIndex);
        long minDurationForQualityIncreaseUs = minDurationForQualityIncreaseUs(availableDurationUs, chunkDurationUs);
        if (selectedFormat.bitrate > currentFormat.bitrate && bufferedDurationUs < minDurationForQualityIncreaseUs) {
            // The selected track is a higher quality, but we have insufficient buffer to safely switch
            // up. Defer switching up for now.
            newSelectedIndex = previousSelectedIndex;
        } else if (selectedFormat.bitrate < currentFormat.bitrate && bufferedDurationUs >= maxDurationForQualityDecreaseUs) {
            // The selected track is a lower quality, but we have sufficient buffer to defer switching
            // down for now.
            newSelectedIndex = previousSelectedIndex;
        }
    }
    // If we adapted, update the trigger.
    reason = newSelectedIndex == previousSelectedIndex ? previousReason : C.SELECTION_REASON_ADAPTIVE;
    selectedIndex = newSelectedIndex;
}
Also used : Format(androidx.media3.common.Format)

Example 2 with MediaChunkIterator

use of androidx.media3.exoplayer.source.chunk.MediaChunkIterator 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 MediaChunkIterator

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

the class HlsSampleStreamWrapper method selectTracks.

/**
 * Called by the parent {@link HlsMediaPeriod} when a track selection occurs.
 *
 * @param selections The renderer track selections.
 * @param mayRetainStreamFlags Flags indicating whether the existing sample stream can be retained
 *     for each selection. A {@code true} value indicates that the selection is unchanged, and
 *     that the caller does not require that the sample stream be recreated.
 * @param streams The existing sample streams, which will be updated to reflect the provided
 *     selections.
 * @param streamResetFlags Will be updated to indicate new sample streams, and sample streams that
 *     have been retained but with the requirement that the consuming renderer be reset.
 * @param positionUs The current playback position in microseconds.
 * @param forceReset If true then a reset is forced (i.e. a seek will be performed with in-buffer
 *     seeking disabled).
 * @return Whether this wrapper requires the parent {@link HlsMediaPeriod} to perform a seek as
 *     part of the track selection.
 */
public boolean selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs, boolean forceReset) {
    assertIsPrepared();
    int oldEnabledTrackGroupCount = enabledTrackGroupCount;
    // Deselect old tracks.
    for (int i = 0; i < selections.length; i++) {
        HlsSampleStream stream = (HlsSampleStream) streams[i];
        if (stream != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
            enabledTrackGroupCount--;
            stream.unbindSampleQueue();
            streams[i] = null;
        }
    }
    // We'll always need to seek if we're being forced to reset, or if this is a first selection to
    // a position other than the one we started preparing with, or if we're making a selection
    // having previously disabled all tracks.
    boolean seekRequired = forceReset || (seenFirstTrackSelection ? oldEnabledTrackGroupCount == 0 : positionUs != lastSeekPositionUs);
    // Get the old (i.e. current before the loop below executes) primary track selection. The new
    // primary selection will equal the old one unless it's changed in the loop.
    ExoTrackSelection oldPrimaryTrackSelection = chunkSource.getTrackSelection();
    ExoTrackSelection primaryTrackSelection = oldPrimaryTrackSelection;
    // Select new tracks.
    for (int i = 0; i < selections.length; i++) {
        ExoTrackSelection selection = selections[i];
        if (selection == null) {
            continue;
        }
        int trackGroupIndex = trackGroups.indexOf(selection.getTrackGroup());
        if (trackGroupIndex == primaryTrackGroupIndex) {
            primaryTrackSelection = selection;
            chunkSource.setTrackSelection(selection);
        }
        if (streams[i] == null) {
            enabledTrackGroupCount++;
            streams[i] = new HlsSampleStream(this, trackGroupIndex);
            streamResetFlags[i] = true;
            if (trackGroupToSampleQueueIndex != null) {
                ((HlsSampleStream) streams[i]).bindSampleQueue();
                // If there's still a chance of avoiding a seek, try and seek within the sample queue.
                if (!seekRequired) {
                    SampleQueue sampleQueue = sampleQueues[trackGroupToSampleQueueIndex[trackGroupIndex]];
                    // A seek can be avoided if we're able to seek to the current playback position in
                    // the sample queue, or if we haven't read anything from the queue since the previous
                    // seek (this case is common for sparse tracks such as metadata tracks). In all other
                    // cases a seek is required.
                    seekRequired = !sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */
                    true) && sampleQueue.getReadIndex() != 0;
                }
            }
        }
    }
    if (enabledTrackGroupCount == 0) {
        chunkSource.reset();
        downstreamTrackFormat = null;
        pendingResetUpstreamFormats = true;
        mediaChunks.clear();
        if (loader.isLoading()) {
            if (sampleQueuesBuilt) {
                // Discard as much as we can synchronously.
                for (SampleQueue sampleQueue : sampleQueues) {
                    sampleQueue.discardToEnd();
                }
            }
            loader.cancelLoading();
        } else {
            resetSampleQueues();
        }
    } else {
        if (!mediaChunks.isEmpty() && !Util.areEqual(primaryTrackSelection, oldPrimaryTrackSelection)) {
            // The primary track selection has changed and we have buffered media. The buffered media
            // may need to be discarded.
            boolean primarySampleQueueDirty = false;
            if (!seenFirstTrackSelection) {
                long bufferedDurationUs = positionUs < 0 ? -positionUs : 0;
                HlsMediaChunk lastMediaChunk = getLastMediaChunk();
                MediaChunkIterator[] mediaChunkIterators = chunkSource.createMediaChunkIterators(lastMediaChunk, positionUs);
                primaryTrackSelection.updateSelectedTrack(positionUs, bufferedDurationUs, C.TIME_UNSET, readOnlyMediaChunks, mediaChunkIterators);
                int chunkIndex = chunkSource.getTrackGroup().indexOf(lastMediaChunk.trackFormat);
                if (primaryTrackSelection.getSelectedIndexInTrackGroup() != chunkIndex) {
                    // This is the first selection and the chunk loaded during preparation does not match
                    // the initially selected format.
                    primarySampleQueueDirty = true;
                }
            } else {
                // The primary sample queue contains media buffered for the old primary track selection.
                primarySampleQueueDirty = true;
            }
            if (primarySampleQueueDirty) {
                forceReset = true;
                seekRequired = true;
                pendingResetUpstreamFormats = true;
            }
        }
        if (seekRequired) {
            seekToUs(positionUs, forceReset);
            // We'll need to reset renderers consuming from all streams due to the seek.
            for (int i = 0; i < streams.length; i++) {
                if (streams[i] != null) {
                    streamResetFlags[i] = true;
                }
            }
        }
    }
    updateSampleStreams(streams);
    seenFirstTrackSelection = true;
    return seekRequired;
}
Also used : SampleQueue(androidx.media3.exoplayer.source.SampleQueue) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection)

Example 4 with MediaChunkIterator

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

the class HlsChunkSource method createMediaChunkIterators.

/**
 * Returns an array of {@link MediaChunkIterator}s for upcoming media chunks.
 *
 * @param previous The previous media chunk. May be null.
 * @param loadPositionUs The position at which the iterators will start.
 * @return Array of {@link MediaChunkIterator}s for each track.
 */
public MediaChunkIterator[] createMediaChunkIterators(@Nullable HlsMediaChunk previous, long loadPositionUs) {
    int oldTrackIndex = previous == null ? C.INDEX_UNSET : trackGroup.indexOf(previous.trackFormat);
    MediaChunkIterator[] chunkIterators = new MediaChunkIterator[trackSelection.length()];
    for (int i = 0; i < chunkIterators.length; i++) {
        int trackIndex = trackSelection.getIndexInTrackGroup(i);
        Uri playlistUrl = playlistUrls[trackIndex];
        if (!playlistTracker.isSnapshotValid(playlistUrl)) {
            chunkIterators[i] = MediaChunkIterator.EMPTY;
            continue;
        }
        @Nullable HlsMediaPlaylist playlist = playlistTracker.getPlaylistSnapshot(playlistUrl, /* isForPlayback= */
        false);
        // Playlist snapshot is valid (checked by if() above) so playlist must be non-null.
        checkNotNull(playlist);
        long startOfPlaylistInPeriodUs = playlist.startTimeUs - playlistTracker.getInitialStartTimeUs();
        boolean switchingTrack = trackIndex != oldTrackIndex;
        Pair<Long, Integer> chunkMediaSequenceAndPartIndex = getNextMediaSequenceAndPartIndex(previous, switchingTrack, playlist, startOfPlaylistInPeriodUs, loadPositionUs);
        long chunkMediaSequence = chunkMediaSequenceAndPartIndex.first;
        int partIndex = chunkMediaSequenceAndPartIndex.second;
        chunkIterators[i] = new HlsMediaPlaylistSegmentIterator(playlist.baseUri, startOfPlaylistInPeriodUs, getSegmentBaseList(playlist, chunkMediaSequence, partIndex));
    }
    return chunkIterators;
}
Also used : BaseMediaChunkIterator(androidx.media3.exoplayer.source.chunk.BaseMediaChunkIterator) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) HlsMediaPlaylist(androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable)

Example 5 with MediaChunkIterator

use of androidx.media3.exoplayer.source.chunk.MediaChunkIterator 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)

Aggregations

MediaChunkIterator (androidx.media3.exoplayer.source.chunk.MediaChunkIterator)6 BaseMediaChunkIterator (androidx.media3.exoplayer.source.chunk.BaseMediaChunkIterator)4 Uri (android.net.Uri)3 Nullable (androidx.annotation.Nullable)3 BehindLiveWindowException (androidx.media3.exoplayer.source.BehindLiveWindowException)3 Format (androidx.media3.common.Format)2 HlsMediaPlaylist (androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist)2 SingleSampleMediaChunk (androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk)2 DataSpec (androidx.media3.datasource.DataSpec)1 RangedUri (androidx.media3.exoplayer.dash.manifest.RangedUri)1 Representation (androidx.media3.exoplayer.dash.manifest.Representation)1 StreamElement (androidx.media3.exoplayer.smoothstreaming.manifest.SsManifest.StreamElement)1 SampleQueue (androidx.media3.exoplayer.source.SampleQueue)1 BundledChunkExtractor (androidx.media3.exoplayer.source.chunk.BundledChunkExtractor)1 ChunkExtractor (androidx.media3.exoplayer.source.chunk.ChunkExtractor)1 ContainerMediaChunk (androidx.media3.exoplayer.source.chunk.ContainerMediaChunk)1 MediaChunk (androidx.media3.exoplayer.source.chunk.MediaChunk)1 ExoTrackSelection (androidx.media3.exoplayer.trackselection.ExoTrackSelection)1 Segment (androidx.media3.test.utils.FakeDataSet.FakeData.Segment)1