Search in sources :

Example 36 with Segment

use of com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment in project ExoPlayer by google.

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(com.google.android.exoplayer2.Format) MediaChunkIterator(com.google.android.exoplayer2.source.chunk.MediaChunkIterator) SingleSampleMediaChunk(com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Segment(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)

Example 37 with Segment

use of com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment in project ExoPlayer by google.

the class HlsSampleStreamWrapper method maybeFinishPrepare.

private void maybeFinishPrepare() {
    if (released || trackGroupToSampleQueueIndex != null || !sampleQueuesBuilt) {
        return;
    }
    for (SampleQueue sampleQueue : sampleQueues) {
        if (sampleQueue.getUpstreamFormat() == null) {
            return;
        }
    }
    if (trackGroups != null) {
        // The track groups were created with multivariant playlist information. They only need to be
        // mapped to a sample queue.
        mapSampleQueuesToMatchTrackGroups();
    } else {
        // Tracks are created using media segment information.
        buildTracksFromSampleStreams();
        setIsPrepared();
        callback.onPrepared();
    }
}
Also used : SampleQueue(com.google.android.exoplayer2.source.SampleQueue)

Example 38 with Segment

use of com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment in project ExoPlayer by google.

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(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment) Pair(android.util.Pair)

Example 39 with Segment

use of com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment in project ExoPlayer by google.

the class HlsChunkSource method getSegmentBaseList.

// Package methods.
/**
 * Returns a list with all segment bases in the playlist starting from {@code mediaSequence} and
 * {@code partIndex} in the given playlist. The list may be empty if the starting point is not in
 * the playlist.
 */
@VisibleForTesting
static /* package */
List<HlsMediaPlaylist.SegmentBase> getSegmentBaseList(HlsMediaPlaylist playlist, long mediaSequence, int partIndex) {
    int firstSegmentIndexInPlaylist = (int) (mediaSequence - playlist.mediaSequence);
    if (firstSegmentIndexInPlaylist < 0 || playlist.segments.size() < firstSegmentIndexInPlaylist) {
        // The first media sequence is not in the playlist.
        return ImmutableList.of();
    }
    List<HlsMediaPlaylist.SegmentBase> segmentBases = new ArrayList<>();
    if (firstSegmentIndexInPlaylist < playlist.segments.size()) {
        if (partIndex != C.INDEX_UNSET) {
            // The iterator starts with a part that belongs to a segment.
            Segment firstSegment = playlist.segments.get(firstSegmentIndexInPlaylist);
            if (partIndex == 0) {
                // Use the full segment instead of the first part.
                segmentBases.add(firstSegment);
            } else if (partIndex < firstSegment.parts.size()) {
                // Add the parts from the first requested segment.
                segmentBases.addAll(firstSegment.parts.subList(partIndex, firstSegment.parts.size()));
            }
            firstSegmentIndexInPlaylist++;
        }
        partIndex = 0;
        // Add all remaining segments.
        segmentBases.addAll(playlist.segments.subList(firstSegmentIndexInPlaylist, playlist.segments.size()));
    }
    if (playlist.partTargetDurationUs != C.TIME_UNSET) {
        // That's a low latency playlist.
        partIndex = partIndex == C.INDEX_UNSET ? 0 : partIndex;
        if (partIndex < playlist.trailingParts.size()) {
            segmentBases.addAll(playlist.trailingParts.subList(partIndex, playlist.trailingParts.size()));
        }
    }
    return Collections.unmodifiableList(segmentBases);
}
Also used : ArrayList(java.util.ArrayList) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 40 with Segment

use of com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment in project ExoPlayer by google.

the class HlsChunkSource method getAdjustedSeekPositionUs.

/**
 * Adjusts a seek position given the specified {@link SeekParameters}.
 *
 * @param positionUs The seek position in microseconds.
 * @param seekParameters Parameters that control how the seek is performed.
 * @return The adjusted seek position, in microseconds.
 */
public long getAdjustedSeekPositionUs(long positionUs, SeekParameters seekParameters) {
    int selectedIndex = trackSelection.getSelectedIndex();
    @Nullable HlsMediaPlaylist mediaPlaylist = selectedIndex < playlistUrls.length && selectedIndex != C.INDEX_UNSET ? playlistTracker.getPlaylistSnapshot(playlistUrls[trackSelection.getSelectedIndexInTrackGroup()], /* isForPlayback= */
    true) : null;
    if (mediaPlaylist == null || mediaPlaylist.segments.isEmpty() || !mediaPlaylist.hasIndependentSegments) {
        return positionUs;
    }
    // Segments start with sync samples (i.e., EXT-X-INDEPENDENT-SEGMENTS is set) and the playlist
    // is non-empty, so we can use segment start times as sync points. Note that in the rare case
    // that (a) an adaptive quality switch occurs between the adjustment and the seek being
    // performed, and (b) segment start times are not aligned across variants, it's possible that
    // the adjusted position may not be at a sync point when it was intended to be. However, this is
    // very much an edge case, and getting it wrong is worth it for getting the vast majority of
    // cases right whilst keeping the implementation relatively simple.
    long startOfPlaylistInPeriodUs = mediaPlaylist.startTimeUs - playlistTracker.getInitialStartTimeUs();
    long relativePositionUs = positionUs - startOfPlaylistInPeriodUs;
    int segmentIndex = Util.binarySearchFloor(mediaPlaylist.segments, relativePositionUs, /* inclusive= */
    true, /* stayInBounds= */
    true);
    long firstSyncUs = mediaPlaylist.segments.get(segmentIndex).relativeStartTimeUs;
    long secondSyncUs = firstSyncUs;
    if (segmentIndex != mediaPlaylist.segments.size() - 1) {
        secondSyncUs = mediaPlaylist.segments.get(segmentIndex + 1).relativeStartTimeUs;
    }
    return seekParameters.resolveSeekPositionUs(relativePositionUs, firstSyncUs, secondSyncUs) + startOfPlaylistInPeriodUs;
}
Also used : HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)20 Uri (android.net.Uri)18 Segment (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment)18 Nullable (androidx.annotation.Nullable)16 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)11 ByteArrayInputStream (java.io.ByteArrayInputStream)9 IOException (java.io.IOException)9 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)9 HlsMediaPlaylist (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist)8 Format (com.google.android.exoplayer2.Format)6 SlowMotionData (com.google.android.exoplayer2.metadata.mp4.SlowMotionData)5 SingleSampleMediaChunk (com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk)5 RangedUri (com.google.android.exoplayer2.source.dash.manifest.RangedUri)5 Representation (com.google.android.exoplayer2.source.dash.manifest.Representation)5 BehindLiveWindowException (com.google.android.exoplayer2.source.BehindLiveWindowException)4 ContainerMediaChunk (com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)4 Segment (com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)4 Window (com.google.android.exoplayer2.Timeline.Window)3 Segment (com.google.android.exoplayer2.metadata.mp4.SlowMotionData.Segment)3