Search in sources :

Example 1 with Segment

use of androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment in project media by androidx.

the class SegmentDownloader method mergeSegments.

private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFactory) {
    HashMap<String, Integer> lastIndexByCacheKey = new HashMap<>();
    int nextOutIndex = 0;
    for (int i = 0; i < segments.size(); i++) {
        Segment segment = segments.get(i);
        String cacheKey = keyFactory.buildCacheKey(segment.dataSpec);
        @Nullable Integer lastIndex = lastIndexByCacheKey.get(cacheKey);
        @Nullable Segment lastSegment = lastIndex == null ? null : segments.get(lastIndex);
        if (lastSegment == null || segment.startTimeUs > lastSegment.startTimeUs + MAX_MERGED_SEGMENT_START_TIME_DIFF_US || !canMergeSegments(lastSegment.dataSpec, segment.dataSpec)) {
            lastIndexByCacheKey.put(cacheKey, nextOutIndex);
            segments.set(nextOutIndex, segment);
            nextOutIndex++;
        } else {
            long mergedLength = segment.dataSpec.length == C.LENGTH_UNSET ? C.LENGTH_UNSET : lastSegment.dataSpec.length + segment.dataSpec.length;
            DataSpec mergedDataSpec = lastSegment.dataSpec.subrange(/* offset= */
            0, mergedLength);
            segments.set(Assertions.checkNotNull(lastIndex), new Segment(lastSegment.startTimeUs, mergedDataSpec));
        }
    }
    Util.removeRange(segments, /* fromIndex= */
    nextOutIndex, /* toIndex= */
    segments.size());
}
Also used : HashMap(java.util.HashMap) DataSpec(androidx.media3.datasource.DataSpec) Nullable(androidx.annotation.Nullable)

Example 2 with Segment

use of androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment in project media by androidx.

the class HlsChunkSource method getNextMediaSequenceAndPartIndex.

// Private methods.
/**
 * Returns the media sequence number and part index to load next in the {@code mediaPlaylist}.
 *
 * @param previous The last (at least partially) loaded segment.
 * @param switchingTrack Whether the segment to load is not preceded by a segment in the same
 *     track.
 * @param mediaPlaylist The media playlist to which the segment to load belongs.
 * @param startOfPlaylistInPeriodUs The start of {@code mediaPlaylist} relative to the period
 *     start in microseconds.
 * @param loadPositionUs The current load position relative to the period start in microseconds.
 * @return The media sequence and part index to load.
 */
private Pair<Long, Integer> getNextMediaSequenceAndPartIndex(@Nullable HlsMediaChunk previous, boolean switchingTrack, HlsMediaPlaylist mediaPlaylist, long startOfPlaylistInPeriodUs, long loadPositionUs) {
    if (previous == null || switchingTrack) {
        long endOfPlaylistInPeriodUs = startOfPlaylistInPeriodUs + mediaPlaylist.durationUs;
        long targetPositionInPeriodUs = (previous == null || independentSegments) ? loadPositionUs : previous.startTimeUs;
        if (!mediaPlaylist.hasEndTag && targetPositionInPeriodUs >= endOfPlaylistInPeriodUs) {
            // If the playlist is too old to contain the chunk, we need to refresh it.
            return new Pair<>(mediaPlaylist.mediaSequence + mediaPlaylist.segments.size(), /* partIndex */
            C.INDEX_UNSET);
        }
        long targetPositionInPlaylistUs = targetPositionInPeriodUs - startOfPlaylistInPeriodUs;
        int segmentIndexInPlaylist = Util.binarySearchFloor(mediaPlaylist.segments, /* value= */
        targetPositionInPlaylistUs, /* inclusive= */
        true, /* stayInBounds= */
        !playlistTracker.isLive() || previous == null);
        long mediaSequence = segmentIndexInPlaylist + mediaPlaylist.mediaSequence;
        int partIndex = C.INDEX_UNSET;
        if (segmentIndexInPlaylist >= 0) {
            // In case we are inside the live window, we try to pick a part if available.
            Segment segment = mediaPlaylist.segments.get(segmentIndexInPlaylist);
            List<HlsMediaPlaylist.Part> parts = targetPositionInPlaylistUs < segment.relativeStartTimeUs + segment.durationUs ? segment.parts : mediaPlaylist.trailingParts;
            for (int i = 0; i < parts.size(); i++) {
                HlsMediaPlaylist.Part part = parts.get(i);
                if (targetPositionInPlaylistUs < part.relativeStartTimeUs + part.durationUs) {
                    if (part.isIndependent) {
                        partIndex = i;
                        // Increase media sequence by one if the part is a trailing part.
                        mediaSequence += parts == mediaPlaylist.trailingParts ? 1 : 0;
                    }
                    break;
                }
            }
        }
        return new Pair<>(mediaSequence, partIndex);
    }
    // If loading has not completed, we return the previous chunk again.
    return (previous.isLoadCompleted() ? new Pair<>(previous.partIndex == C.INDEX_UNSET ? previous.getNextChunkIndex() : previous.chunkIndex, previous.partIndex == C.INDEX_UNSET ? C.INDEX_UNSET : previous.partIndex + 1) : new Pair<>(previous.chunkIndex, previous.partIndex));
}
Also used : HlsMediaPlaylist(androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist) Segment(androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment) Pair(android.util.Pair)

Example 3 with Segment

use of androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment in project media by androidx.

the class DashMediaSourceTest method prepare_targetLiveOffsetInWindow_manifestTargetOffsetAndAlignedWindowStartPosition.

@Test
public void prepare_targetLiveOffsetInWindow_manifestTargetOffsetAndAlignedWindowStartPosition() throws InterruptedException {
    DashMediaSource mediaSource = new DashMediaSource.Factory(() -> createSampleMpdDataSource(SAMPLE_MPD_LIVE_WITH_OFFSET_INSIDE_WINDOW)).createMediaSource(MediaItem.fromUri(Uri.EMPTY));
    Window window = prepareAndWaitForTimelineRefresh(mediaSource);
    // Expect the target live offset as defined in the manifest.
    assertThat(window.liveConfiguration.targetOffsetMs).isEqualTo(3000);
    // Expect the default position at the first segment start before the live edge.
    assertThat(window.getDefaultPositionMs()).isEqualTo(2_000);
}
Also used : Window(androidx.media3.common.Timeline.Window) Test(org.junit.Test)

Example 4 with Segment

use of androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment 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 5 with Segment

use of androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment in project media by androidx.

the class DefaultDashChunkSource method newMediaChunk.

protected Chunk newMediaChunk(RepresentationHolder representationHolder, DataSource dataSource, @C.TrackType int trackType, Format trackFormat, @C.SelectionReason int trackSelectionReason, Object trackSelectionData, long firstSegmentNum, int maxSegmentCount, long seekTimeUs, long nowPeriodTimeUs) {
    Representation representation = representationHolder.representation;
    long startTimeUs = representationHolder.getSegmentStartTimeUs(firstSegmentNum);
    RangedUri segmentUri = representationHolder.getSegmentUrl(firstSegmentNum);
    if (representationHolder.chunkExtractor == null) {
        long endTimeUs = representationHolder.getSegmentEndTimeUs(firstSegmentNum);
        int flags = representationHolder.isSegmentAvailableAtFullNetworkSpeed(firstSegmentNum, nowPeriodTimeUs) ? 0 : DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED;
        DataSpec dataSpec = DashUtil.buildDataSpec(representation, representationHolder.selectedBaseUrl.url, segmentUri, flags);
        return new SingleSampleMediaChunk(dataSource, dataSpec, trackFormat, trackSelectionReason, trackSelectionData, startTimeUs, endTimeUs, firstSegmentNum, trackType, trackFormat);
    } else {
        int segmentCount = 1;
        for (int i = 1; i < maxSegmentCount; i++) {
            RangedUri nextSegmentUri = representationHolder.getSegmentUrl(firstSegmentNum + i);
            @Nullable RangedUri mergedSegmentUri = segmentUri.attemptMerge(nextSegmentUri, representationHolder.selectedBaseUrl.url);
            if (mergedSegmentUri == null) {
                // Unable to merge segment fetches because the URIs do not merge.
                break;
            }
            segmentUri = mergedSegmentUri;
            segmentCount++;
        }
        long segmentNum = firstSegmentNum + segmentCount - 1;
        long endTimeUs = representationHolder.getSegmentEndTimeUs(segmentNum);
        long periodDurationUs = representationHolder.periodDurationUs;
        long clippedEndTimeUs = periodDurationUs != C.TIME_UNSET && periodDurationUs <= endTimeUs ? periodDurationUs : C.TIME_UNSET;
        int flags = representationHolder.isSegmentAvailableAtFullNetworkSpeed(segmentNum, nowPeriodTimeUs) ? 0 : DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED;
        DataSpec dataSpec = DashUtil.buildDataSpec(representation, representationHolder.selectedBaseUrl.url, segmentUri, flags);
        long sampleOffsetUs = -representation.presentationTimeOffsetUs;
        return new ContainerMediaChunk(dataSource, dataSpec, trackFormat, trackSelectionReason, trackSelectionData, startTimeUs, endTimeUs, seekTimeUs, clippedEndTimeUs, firstSegmentNum, segmentCount, sampleOffsetUs, representationHolder.chunkExtractor);
    }
}
Also used : SingleSampleMediaChunk(androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk) RangedUri(androidx.media3.exoplayer.dash.manifest.RangedUri) DataSpec(androidx.media3.datasource.DataSpec) Representation(androidx.media3.exoplayer.dash.manifest.Representation) ContainerMediaChunk(androidx.media3.exoplayer.source.chunk.ContainerMediaChunk) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)20 Uri (android.net.Uri)16 Nullable (androidx.annotation.Nullable)16 Segment (androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist.Segment)13 DataSpec (androidx.media3.datasource.DataSpec)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 ArrayList (java.util.ArrayList)8 HlsMediaPlaylist (androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist)7 Format (androidx.media3.common.Format)6 SlowMotionData (androidx.media3.extractor.metadata.mp4.SlowMotionData)5 Segment (androidx.media3.test.utils.FakeDataSet.FakeData.Segment)5 SingleSampleMediaChunk (androidx.media3.exoplayer.source.chunk.SingleSampleMediaChunk)4 Window (androidx.media3.common.Timeline.Window)3 RangedUri (androidx.media3.exoplayer.dash.manifest.RangedUri)3 Representation (androidx.media3.exoplayer.dash.manifest.Representation)3 ContainerMediaChunk (androidx.media3.exoplayer.source.chunk.ContainerMediaChunk)3 MediaChunkIterator (androidx.media3.exoplayer.source.chunk.MediaChunkIterator)3 Segment (androidx.media3.extractor.metadata.mp4.SlowMotionData.Segment)3