Search in sources :

Example 1 with HlsMediaPlaylist

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

the class HlsMediaPlaylistParserTest method testParseMediaPlaylist.

public void testParseMediaPlaylist() {
    Uri playlistUri = Uri.parse("https://example.com/test.m3u8");
    String playlistString = "#EXTM3U\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-PLAYLIST-TYPE:VOD\n" + "#EXT-X-START:TIME-OFFSET=-25" + "#EXT-X-TARGETDURATION:8\n" + "#EXT-X-MEDIA-SEQUENCE:2679\n" + "#EXT-X-DISCONTINUITY-SEQUENCE:4\n" + "#EXT-X-ALLOW-CACHE:YES\n" + "\n" + "#EXTINF:7.975,\n" + "#EXT-X-BYTERANGE:51370@0\n" + "https://priv.example.com/fileSequence2679.ts\n" + "\n" + "#EXT-X-KEY:METHOD=AES-128,URI=\"https://priv.example.com/key.php?r=2680\",IV=0x1566B\n" + "#EXTINF:7.975,\n" + "#EXT-X-BYTERANGE:51501@2147483648\n" + "https://priv.example.com/fileSequence2680.ts\n" + "\n" + "#EXT-X-KEY:METHOD=NONE\n" + "#EXTINF:7.941,\n" + // @2147535149
    "#EXT-X-BYTERANGE:51501\n" + "https://priv.example.com/fileSequence2681.ts\n" + "\n" + "#EXT-X-DISCONTINUITY\n" + "#EXT-X-KEY:METHOD=AES-128,URI=\"https://priv.example.com/key.php?r=2682\"\n" + "#EXTINF:7.975,\n" + // @2147586650
    "#EXT-X-BYTERANGE:51740\n" + "https://priv.example.com/fileSequence2682.ts\n" + "\n" + "#EXTINF:7.975,\n" + "https://priv.example.com/fileSequence2683.ts\n" + "#EXT-X-ENDLIST";
    InputStream inputStream = new ByteArrayInputStream(playlistString.getBytes(Charset.forName(C.UTF8_NAME)));
    try {
        HlsPlaylist playlist = new HlsPlaylistParser().parse(playlistUri, inputStream);
        assertNotNull(playlist);
        assertEquals(HlsPlaylist.TYPE_MEDIA, playlist.type);
        HlsMediaPlaylist mediaPlaylist = (HlsMediaPlaylist) playlist;
        assertEquals(HlsMediaPlaylist.PLAYLIST_TYPE_VOD, mediaPlaylist.playlistType);
        assertEquals(mediaPlaylist.durationUs - 25000000, mediaPlaylist.startOffsetUs);
        assertEquals(2679, mediaPlaylist.mediaSequence);
        assertEquals(3, mediaPlaylist.version);
        assertTrue(mediaPlaylist.hasEndTag);
        List<Segment> segments = mediaPlaylist.segments;
        assertNotNull(segments);
        assertEquals(5, segments.size());
        Segment segment = segments.get(0);
        assertEquals(4, mediaPlaylist.discontinuitySequence + segment.relativeDiscontinuitySequence);
        assertEquals(7975000, segment.durationUs);
        assertFalse(segment.isEncrypted);
        assertEquals(null, segment.encryptionKeyUri);
        assertEquals(null, segment.encryptionIV);
        assertEquals(51370, segment.byterangeLength);
        assertEquals(0, segment.byterangeOffset);
        assertEquals("https://priv.example.com/fileSequence2679.ts", segment.url);
        segment = segments.get(1);
        assertEquals(0, segment.relativeDiscontinuitySequence);
        assertEquals(7975000, segment.durationUs);
        assertTrue(segment.isEncrypted);
        assertEquals("https://priv.example.com/key.php?r=2680", segment.encryptionKeyUri);
        assertEquals("0x1566B", segment.encryptionIV);
        assertEquals(51501, segment.byterangeLength);
        assertEquals(2147483648L, segment.byterangeOffset);
        assertEquals("https://priv.example.com/fileSequence2680.ts", segment.url);
        segment = segments.get(2);
        assertEquals(0, segment.relativeDiscontinuitySequence);
        assertEquals(7941000, segment.durationUs);
        assertFalse(segment.isEncrypted);
        assertEquals(null, segment.encryptionKeyUri);
        assertEquals(null, segment.encryptionIV);
        assertEquals(51501, segment.byterangeLength);
        assertEquals(2147535149L, segment.byterangeOffset);
        assertEquals("https://priv.example.com/fileSequence2681.ts", segment.url);
        segment = segments.get(3);
        assertEquals(1, segment.relativeDiscontinuitySequence);
        assertEquals(7975000, segment.durationUs);
        assertTrue(segment.isEncrypted);
        assertEquals("https://priv.example.com/key.php?r=2682", segment.encryptionKeyUri);
        // 0xA7A == 2682.
        assertNotNull(segment.encryptionIV);
        assertEquals("A7A", segment.encryptionIV.toUpperCase(Locale.getDefault()));
        assertEquals(51740, segment.byterangeLength);
        assertEquals(2147586650L, segment.byterangeOffset);
        assertEquals("https://priv.example.com/fileSequence2682.ts", segment.url);
        segment = segments.get(4);
        assertEquals(1, segment.relativeDiscontinuitySequence);
        assertEquals(7975000, segment.durationUs);
        assertTrue(segment.isEncrypted);
        assertEquals("https://priv.example.com/key.php?r=2682", segment.encryptionKeyUri);
        // 0xA7B == 2683.
        assertNotNull(segment.encryptionIV);
        assertEquals("A7B", segment.encryptionIV.toUpperCase(Locale.getDefault()));
        assertEquals(C.LENGTH_UNSET, segment.byterangeLength);
        assertEquals(0, segment.byterangeOffset);
        assertEquals("https://priv.example.com/fileSequence2683.ts", segment.url);
    } catch (IOException exception) {
        fail(exception.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Uri(android.net.Uri) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment)

Example 2 with HlsMediaPlaylist

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

the class HlsChunkSource method getNextChunk.

/**
   * Returns the next chunk to load.
   * <p>
   * If a chunk is available then {@link HlsChunkHolder#chunk} is set. If the end of the stream has
   * been reached then {@link HlsChunkHolder#endOfStream} is set. If a chunk is not available but
   * the end of the stream has not been reached, {@link HlsChunkHolder#playlist} is set to
   * contain the {@link HlsUrl} that refers to the playlist that needs refreshing.
   *
   * @param previous The most recently loaded media chunk.
   * @param playbackPositionUs The current playback position. If {@code previous} is null then this
   *     parameter is the position from which playback is expected to start (or restart) and hence
   *     should be interpreted as a seek position.
   * @param out A holder to populate.
   */
public void getNextChunk(HlsMediaChunk previous, long playbackPositionUs, HlsChunkHolder out) {
    int oldVariantIndex = previous == null ? C.INDEX_UNSET : trackGroup.indexOf(previous.trackFormat);
    // Use start time of the previous chunk rather than its end time because switching format will
    // require downloading overlapping segments.
    long bufferedDurationUs = previous == null ? 0 : Math.max(0, previous.startTimeUs - playbackPositionUs);
    // Select the variant.
    trackSelection.updateSelectedTrack(bufferedDurationUs);
    int selectedVariantIndex = trackSelection.getSelectedIndexInTrackGroup();
    boolean switchingVariant = oldVariantIndex != selectedVariantIndex;
    HlsUrl selectedUrl = variants[selectedVariantIndex];
    if (!playlistTracker.isSnapshotValid(selectedUrl)) {
        out.playlist = selectedUrl;
        // Retry when playlist is refreshed.
        return;
    }
    HlsMediaPlaylist mediaPlaylist = playlistTracker.getPlaylistSnapshot(selectedUrl);
    // Select the chunk.
    int chunkMediaSequence;
    if (previous == null || switchingVariant) {
        long targetPositionUs = previous == null ? playbackPositionUs : previous.startTimeUs;
        if (!mediaPlaylist.hasEndTag && targetPositionUs > mediaPlaylist.getEndTimeUs()) {
            // If the playlist is too old to contain the chunk, we need to refresh it.
            chunkMediaSequence = mediaPlaylist.mediaSequence + mediaPlaylist.segments.size();
        } else {
            chunkMediaSequence = Util.binarySearchFloor(mediaPlaylist.segments, targetPositionUs - mediaPlaylist.startTimeUs, true, !playlistTracker.isLive() || previous == null) + mediaPlaylist.mediaSequence;
            if (chunkMediaSequence < mediaPlaylist.mediaSequence && previous != null) {
                // We try getting the next chunk without adapting in case that's the reason for falling
                // behind the live window.
                selectedVariantIndex = oldVariantIndex;
                selectedUrl = variants[selectedVariantIndex];
                mediaPlaylist = playlistTracker.getPlaylistSnapshot(selectedUrl);
                chunkMediaSequence = previous.getNextChunkIndex();
            }
        }
    } else {
        chunkMediaSequence = previous.getNextChunkIndex();
    }
    if (chunkMediaSequence < mediaPlaylist.mediaSequence) {
        fatalError = new BehindLiveWindowException();
        return;
    }
    int chunkIndex = chunkMediaSequence - mediaPlaylist.mediaSequence;
    if (chunkIndex >= mediaPlaylist.segments.size()) {
        if (mediaPlaylist.hasEndTag) {
            out.endOfStream = true;
        } else /* Live */
        {
            out.playlist = selectedUrl;
        }
        return;
    }
    // Handle encryption.
    HlsMediaPlaylist.Segment segment = mediaPlaylist.segments.get(chunkIndex);
    // Check if encryption is specified.
    if (segment.isEncrypted) {
        Uri keyUri = UriUtil.resolveToUri(mediaPlaylist.baseUri, segment.encryptionKeyUri);
        if (!keyUri.equals(encryptionKeyUri)) {
            // Encryption is specified and the key has changed.
            out.chunk = newEncryptionKeyChunk(keyUri, segment.encryptionIV, selectedVariantIndex, trackSelection.getSelectionReason(), trackSelection.getSelectionData());
            return;
        }
        if (!Util.areEqual(segment.encryptionIV, encryptionIvString)) {
            setEncryptionData(keyUri, segment.encryptionIV, encryptionKey);
        }
    } else {
        clearEncryptionData();
    }
    DataSpec initDataSpec = null;
    Segment initSegment = mediaPlaylist.initializationSegment;
    if (initSegment != null) {
        Uri initSegmentUri = UriUtil.resolveToUri(mediaPlaylist.baseUri, initSegment.url);
        initDataSpec = new DataSpec(initSegmentUri, initSegment.byterangeOffset, initSegment.byterangeLength, null);
    }
    // Compute start time of the next chunk.
    long startTimeUs = mediaPlaylist.startTimeUs + segment.relativeStartTimeUs;
    int discontinuitySequence = mediaPlaylist.discontinuitySequence + segment.relativeDiscontinuitySequence;
    TimestampAdjuster timestampAdjuster = timestampAdjusterProvider.getAdjuster(discontinuitySequence);
    // Configure the data source and spec for the chunk.
    Uri chunkUri = UriUtil.resolveToUri(mediaPlaylist.baseUri, segment.url);
    DataSpec dataSpec = new DataSpec(chunkUri, segment.byterangeOffset, segment.byterangeLength, null);
    out.chunk = new HlsMediaChunk(mediaDataSource, dataSpec, initDataSpec, selectedUrl, muxedCaptionFormats, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), startTimeUs, startTimeUs + segment.durationUs, chunkMediaSequence, discontinuitySequence, isTimestampMaster, timestampAdjuster, previous, encryptionKey, encryptionIv);
}
Also used : Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment) HlsUrl(com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist.HlsUrl) BehindLiveWindowException(com.google.android.exoplayer2.source.BehindLiveWindowException) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) TimestampAdjuster(com.google.android.exoplayer2.util.TimestampAdjuster) Uri(android.net.Uri) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment)

Example 3 with HlsMediaPlaylist

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

the class HlsPlaylistTracker method getLoadedPlaylistStartTimeUs.

private long getLoadedPlaylistStartTimeUs(HlsMediaPlaylist oldPlaylist, HlsMediaPlaylist loadedPlaylist) {
    if (loadedPlaylist.hasProgramDateTime) {
        return loadedPlaylist.startTimeUs;
    }
    long primarySnapshotStartTimeUs = primaryUrlSnapshot != null ? primaryUrlSnapshot.startTimeUs : 0;
    if (oldPlaylist == null) {
        return primarySnapshotStartTimeUs;
    }
    int oldPlaylistSize = oldPlaylist.segments.size();
    Segment firstOldOverlappingSegment = getFirstOldOverlappingSegment(oldPlaylist, loadedPlaylist);
    if (firstOldOverlappingSegment != null) {
        return oldPlaylist.startTimeUs + firstOldOverlappingSegment.relativeStartTimeUs;
    } else if (oldPlaylistSize == loadedPlaylist.mediaSequence - oldPlaylist.mediaSequence) {
        return oldPlaylist.getEndTimeUs();
    } else {
        // No segments overlap, we assume the new playlist start coincides with the primary playlist.
        return primarySnapshotStartTimeUs;
    }
}
Also used : Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment)

Example 4 with HlsMediaPlaylist

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

the class HlsMediaSourceTest method refreshPlaylist_targetLiveOffsetRemainsInWindow.

@Test
public void refreshPlaylist_targetLiveOffsetRemainsInWindow() throws TimeoutException, IOException {
    String playlistUri1 = "fake://foo.bar/media0/playlist1.m3u8";
    // The playlist has a duration of 16 seconds and a hold back of 12 seconds.
    String playlist1 = "#EXTM3U\n" + "#EXT-X-TARGETDURATION:4\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:4.00000,\n" + "fileSequence0.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence1.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence2.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence3.ts\n" + "#EXT-X-SERVER-CONTROL:HOLD-BACK:12";
    // The second playlist defines a different hold back.
    String playlistUri2 = "fake://foo.bar/media0/playlist2.m3u8";
    String playlist2 = "#EXTM3U\n" + "#EXT-X-TARGETDURATION:4\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-MEDIA-SEQUENCE:4\n" + "#EXTINF:4.00000,\n" + "fileSequence4.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence5.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence6.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence7.ts\n" + "#EXT-X-SERVER-CONTROL:HOLD-BACK:14";
    // The third playlist has a duration of 8 seconds.
    String playlistUri3 = "fake://foo.bar/media0/playlist3.m3u8";
    String playlist3 = "#EXTM3U\n" + "#EXT-X-TARGETDURATION:4\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-MEDIA-SEQUENCE:4\n" + "#EXTINF:4.00000,\n" + "fileSequence8.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence9.ts\n" + "#EXTINF:4.00000,\n" + "#EXT-X-SERVER-CONTROL:HOLD-BACK:12";
    // The third playlist has a duration of 16 seconds but the target live offset should remain at
    // 8 seconds.
    String playlistUri4 = "fake://foo.bar/media0/playlist4.m3u8";
    String playlist4 = "#EXTM3U\n" + "#EXT-X-TARGETDURATION:4\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-MEDIA-SEQUENCE:4\n" + "#EXTINF:4.00000,\n" + "fileSequence10.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence11.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence12.ts\n" + "#EXTINF:4.00000,\n" + "fileSequence13.ts\n" + "#EXTINF:4.00000,\n" + "#EXT-X-SERVER-CONTROL:HOLD-BACK:12";
    HlsMediaSource.Factory factory = createHlsMediaSourceFactory(playlistUri1, playlist1);
    MediaItem mediaItem = new MediaItem.Builder().setUri(playlistUri1).build();
    HlsMediaSource mediaSource = factory.createMediaSource(mediaItem);
    HlsMediaPlaylist secondPlaylist = parseHlsMediaPlaylist(playlistUri2, playlist2);
    HlsMediaPlaylist thirdPlaylist = parseHlsMediaPlaylist(playlistUri3, playlist3);
    HlsMediaPlaylist fourthPlaylist = parseHlsMediaPlaylist(playlistUri4, playlist4);
    List<Timeline> timelines = new ArrayList<>();
    MediaSource.MediaSourceCaller mediaSourceCaller = (source, timeline) -> timelines.add(timeline);
    mediaSource.prepareSource(mediaSourceCaller, /* mediaTransferListener= */
    null, PlayerId.UNSET);
    runMainLooperUntil(() -> timelines.size() == 1);
    mediaSource.onPrimaryPlaylistRefreshed(secondPlaylist);
    runMainLooperUntil(() -> timelines.size() == 2);
    mediaSource.onPrimaryPlaylistRefreshed(thirdPlaylist);
    runMainLooperUntil(() -> timelines.size() == 3);
    mediaSource.onPrimaryPlaylistRefreshed(fourthPlaylist);
    runMainLooperUntil(() -> timelines.size() == 4);
    Timeline.Window window = new Timeline.Window();
    assertThat(timelines.get(0).getWindow(0, window).liveConfiguration.targetOffsetMs).isEqualTo(12000);
    assertThat(timelines.get(1).getWindow(0, window).liveConfiguration.targetOffsetMs).isEqualTo(12000);
    assertThat(timelines.get(2).getWindow(0, window).liveConfiguration.targetOffsetMs).isEqualTo(8000);
    assertThat(timelines.get(3).getWindow(0, window).liveConfiguration.targetOffsetMs).isEqualTo(8000);
}
Also used : RobolectricUtil.runMainLooperUntil(com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil) Util(com.google.android.exoplayer2.util.Util) MediaItem(com.google.android.exoplayer2.MediaItem) HlsPlaylistParser(com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser) ParserException(com.google.android.exoplayer2.ParserException) Uri(android.net.Uri) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) SystemClock(android.os.SystemClock) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AtomicReference(java.util.concurrent.atomic.AtomicReference) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) ArrayList(java.util.ArrayList) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) List(java.util.List) Timeline(com.google.android.exoplayer2.Timeline) PlayerId(com.google.android.exoplayer2.analytics.PlayerId) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaSource(com.google.android.exoplayer2.source.MediaSource) C(com.google.android.exoplayer2.C) ArrayList(java.util.ArrayList) Timeline(com.google.android.exoplayer2.Timeline) MediaSource(com.google.android.exoplayer2.source.MediaSource) MediaItem(com.google.android.exoplayer2.MediaItem) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) Test(org.junit.Test)

Example 5 with HlsMediaPlaylist

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

the class HlsPlaylistParser method parseMediaPlaylist.

private static HlsMediaPlaylist parseMediaPlaylist(HlsMultivariantPlaylist multivariantPlaylist, @Nullable HlsMediaPlaylist previousMediaPlaylist, LineIterator iterator, String baseUri) throws IOException {
    @HlsMediaPlaylist.PlaylistType int playlistType = HlsMediaPlaylist.PLAYLIST_TYPE_UNKNOWN;
    long startOffsetUs = C.TIME_UNSET;
    long mediaSequence = 0;
    // Default version == 1.
    int version = 1;
    long targetDurationUs = C.TIME_UNSET;
    long partTargetDurationUs = C.TIME_UNSET;
    boolean hasIndependentSegmentsTag = multivariantPlaylist.hasIndependentSegments;
    boolean hasEndTag = false;
    @Nullable Segment initializationSegment = null;
    HashMap<String, String> variableDefinitions = new HashMap<>();
    HashMap<String, Segment> urlToInferredInitSegment = new HashMap<>();
    List<Segment> segments = new ArrayList<>();
    List<Part> trailingParts = new ArrayList<>();
    @Nullable Part preloadPart = null;
    List<RenditionReport> renditionReports = new ArrayList<>();
    List<String> tags = new ArrayList<>();
    long segmentDurationUs = 0;
    String segmentTitle = "";
    boolean hasDiscontinuitySequence = false;
    int playlistDiscontinuitySequence = 0;
    int relativeDiscontinuitySequence = 0;
    long playlistStartTimeUs = 0;
    long segmentStartTimeUs = 0;
    boolean preciseStart = false;
    long segmentByteRangeOffset = 0;
    long segmentByteRangeLength = C.LENGTH_UNSET;
    long partStartTimeUs = 0;
    long partByteRangeOffset = 0;
    boolean isIFrameOnly = false;
    long segmentMediaSequence = 0;
    boolean hasGapTag = false;
    HlsMediaPlaylist.ServerControl serverControl = new HlsMediaPlaylist.ServerControl(/* skipUntilUs= */
    C.TIME_UNSET, /* canSkipDateRanges= */
    false, /* holdBackUs= */
    C.TIME_UNSET, /* partHoldBackUs= */
    C.TIME_UNSET, /* canBlockReload= */
    false);
    @Nullable DrmInitData playlistProtectionSchemes = null;
    @Nullable String fullSegmentEncryptionKeyUri = null;
    @Nullable String fullSegmentEncryptionIV = null;
    TreeMap<String, SchemeData> currentSchemeDatas = new TreeMap<>();
    @Nullable String encryptionScheme = null;
    @Nullable DrmInitData cachedDrmInitData = null;
    String line;
    while (iterator.hasNext()) {
        line = iterator.next();
        if (line.startsWith(TAG_PREFIX)) {
            // We expose all tags through the playlist.
            tags.add(line);
        }
        if (line.startsWith(TAG_PLAYLIST_TYPE)) {
            String playlistTypeString = parseStringAttr(line, REGEX_PLAYLIST_TYPE, variableDefinitions);
            if ("VOD".equals(playlistTypeString)) {
                playlistType = HlsMediaPlaylist.PLAYLIST_TYPE_VOD;
            } else if ("EVENT".equals(playlistTypeString)) {
                playlistType = HlsMediaPlaylist.PLAYLIST_TYPE_EVENT;
            }
        } else if (line.equals(TAG_IFRAME)) {
            isIFrameOnly = true;
        } else if (line.startsWith(TAG_START)) {
            startOffsetUs = (long) (parseDoubleAttr(line, REGEX_TIME_OFFSET) * C.MICROS_PER_SECOND);
            preciseStart = parseOptionalBooleanAttribute(line, REGEX_PRECISE, /* defaultValue= */
            false);
        } else if (line.startsWith(TAG_SERVER_CONTROL)) {
            serverControl = parseServerControl(line);
        } else if (line.startsWith(TAG_PART_INF)) {
            double partTargetDurationSeconds = parseDoubleAttr(line, REGEX_PART_TARGET_DURATION);
            partTargetDurationUs = (long) (partTargetDurationSeconds * C.MICROS_PER_SECOND);
        } else if (line.startsWith(TAG_INIT_SEGMENT)) {
            String uri = parseStringAttr(line, REGEX_URI, variableDefinitions);
            String byteRange = parseOptionalStringAttr(line, REGEX_ATTR_BYTERANGE, variableDefinitions);
            if (byteRange != null) {
                String[] splitByteRange = Util.split(byteRange, "@");
                segmentByteRangeLength = Long.parseLong(splitByteRange[0]);
                if (splitByteRange.length > 1) {
                    segmentByteRangeOffset = Long.parseLong(splitByteRange[1]);
                }
            }
            if (segmentByteRangeLength == C.LENGTH_UNSET) {
                // The segment has no byte range defined.
                segmentByteRangeOffset = 0;
            }
            if (fullSegmentEncryptionKeyUri != null && fullSegmentEncryptionIV == null) {
                // See RFC 8216, Section 4.3.2.5.
                throw ParserException.createForMalformedManifest("The encryption IV attribute must be present when an initialization segment is" + " encrypted with METHOD=AES-128.", /* cause= */
                null);
            }
            initializationSegment = new Segment(uri, segmentByteRangeOffset, segmentByteRangeLength, fullSegmentEncryptionKeyUri, fullSegmentEncryptionIV);
            if (segmentByteRangeLength != C.LENGTH_UNSET) {
                segmentByteRangeOffset += segmentByteRangeLength;
            }
            segmentByteRangeLength = C.LENGTH_UNSET;
        } else if (line.startsWith(TAG_TARGET_DURATION)) {
            targetDurationUs = parseIntAttr(line, REGEX_TARGET_DURATION) * C.MICROS_PER_SECOND;
        } else if (line.startsWith(TAG_MEDIA_SEQUENCE)) {
            mediaSequence = parseLongAttr(line, REGEX_MEDIA_SEQUENCE);
            segmentMediaSequence = mediaSequence;
        } else if (line.startsWith(TAG_VERSION)) {
            version = parseIntAttr(line, REGEX_VERSION);
        } else if (line.startsWith(TAG_DEFINE)) {
            String importName = parseOptionalStringAttr(line, REGEX_IMPORT, variableDefinitions);
            if (importName != null) {
                String value = multivariantPlaylist.variableDefinitions.get(importName);
                if (value != null) {
                    variableDefinitions.put(importName, value);
                } else {
                // The multivariant playlist does not declare the imported variable. Ignore.
                }
            } else {
                variableDefinitions.put(parseStringAttr(line, REGEX_NAME, variableDefinitions), parseStringAttr(line, REGEX_VALUE, variableDefinitions));
            }
        } else if (line.startsWith(TAG_MEDIA_DURATION)) {
            segmentDurationUs = parseTimeSecondsToUs(line, REGEX_MEDIA_DURATION);
            segmentTitle = parseOptionalStringAttr(line, REGEX_MEDIA_TITLE, "", variableDefinitions);
        } else if (line.startsWith(TAG_SKIP)) {
            int skippedSegmentCount = parseIntAttr(line, REGEX_SKIPPED_SEGMENTS);
            checkState(previousMediaPlaylist != null && segments.isEmpty());
            int startIndex = (int) (mediaSequence - castNonNull(previousMediaPlaylist).mediaSequence);
            int endIndex = startIndex + skippedSegmentCount;
            if (startIndex < 0 || endIndex > previousMediaPlaylist.segments.size()) {
                // Throw to force a reload if not all segments are available in the previous playlist.
                throw new DeltaUpdateException();
            }
            for (int i = startIndex; i < endIndex; i++) {
                Segment segment = previousMediaPlaylist.segments.get(i);
                if (mediaSequence != previousMediaPlaylist.mediaSequence) {
                    // If the media sequences of the playlists are not the same, we need to recreate the
                    // object with the updated relative start time and the relative discontinuity
                    // sequence. With identical playlist media sequences these values do not change.
                    int newRelativeDiscontinuitySequence = previousMediaPlaylist.discontinuitySequence - playlistDiscontinuitySequence + segment.relativeDiscontinuitySequence;
                    segment = segment.copyWith(segmentStartTimeUs, newRelativeDiscontinuitySequence);
                }
                segments.add(segment);
                segmentStartTimeUs += segment.durationUs;
                partStartTimeUs = segmentStartTimeUs;
                if (segment.byteRangeLength != C.LENGTH_UNSET) {
                    segmentByteRangeOffset = segment.byteRangeOffset + segment.byteRangeLength;
                }
                relativeDiscontinuitySequence = segment.relativeDiscontinuitySequence;
                initializationSegment = segment.initializationSegment;
                cachedDrmInitData = segment.drmInitData;
                fullSegmentEncryptionKeyUri = segment.fullSegmentEncryptionKeyUri;
                if (segment.encryptionIV == null || !segment.encryptionIV.equals(Long.toHexString(segmentMediaSequence))) {
                    fullSegmentEncryptionIV = segment.encryptionIV;
                }
                segmentMediaSequence++;
            }
        } else if (line.startsWith(TAG_KEY)) {
            String method = parseStringAttr(line, REGEX_METHOD, variableDefinitions);
            String keyFormat = parseOptionalStringAttr(line, REGEX_KEYFORMAT, KEYFORMAT_IDENTITY, variableDefinitions);
            fullSegmentEncryptionKeyUri = null;
            fullSegmentEncryptionIV = null;
            if (METHOD_NONE.equals(method)) {
                currentSchemeDatas.clear();
                cachedDrmInitData = null;
            } else /* !METHOD_NONE.equals(method) */
            {
                fullSegmentEncryptionIV = parseOptionalStringAttr(line, REGEX_IV, variableDefinitions);
                if (KEYFORMAT_IDENTITY.equals(keyFormat)) {
                    if (METHOD_AES_128.equals(method)) {
                        // The segment is fully encrypted using an identity key.
                        fullSegmentEncryptionKeyUri = parseStringAttr(line, REGEX_URI, variableDefinitions);
                    } else {
                    // Do nothing. Samples are encrypted using an identity key, but this is not supported.
                    // Hopefully, a traditional DRM alternative is also provided.
                    }
                } else {
                    if (encryptionScheme == null) {
                        encryptionScheme = parseEncryptionScheme(method);
                    }
                    SchemeData schemeData = parseDrmSchemeData(line, keyFormat, variableDefinitions);
                    if (schemeData != null) {
                        cachedDrmInitData = null;
                        currentSchemeDatas.put(keyFormat, schemeData);
                    }
                }
            }
        } else if (line.startsWith(TAG_BYTERANGE)) {
            String byteRange = parseStringAttr(line, REGEX_BYTERANGE, variableDefinitions);
            String[] splitByteRange = Util.split(byteRange, "@");
            segmentByteRangeLength = Long.parseLong(splitByteRange[0]);
            if (splitByteRange.length > 1) {
                segmentByteRangeOffset = Long.parseLong(splitByteRange[1]);
            }
        } else if (line.startsWith(TAG_DISCONTINUITY_SEQUENCE)) {
            hasDiscontinuitySequence = true;
            playlistDiscontinuitySequence = Integer.parseInt(line.substring(line.indexOf(':') + 1));
        } else if (line.equals(TAG_DISCONTINUITY)) {
            relativeDiscontinuitySequence++;
        } else if (line.startsWith(TAG_PROGRAM_DATE_TIME)) {
            if (playlistStartTimeUs == 0) {
                long programDatetimeUs = Util.msToUs(Util.parseXsDateTime(line.substring(line.indexOf(':') + 1)));
                playlistStartTimeUs = programDatetimeUs - segmentStartTimeUs;
            }
        } else if (line.equals(TAG_GAP)) {
            hasGapTag = true;
        } else if (line.equals(TAG_INDEPENDENT_SEGMENTS)) {
            hasIndependentSegmentsTag = true;
        } else if (line.equals(TAG_ENDLIST)) {
            hasEndTag = true;
        } else if (line.startsWith(TAG_RENDITION_REPORT)) {
            long lastMediaSequence = parseOptionalLongAttr(line, REGEX_LAST_MSN, C.INDEX_UNSET);
            int lastPartIndex = parseOptionalIntAttr(line, REGEX_LAST_PART, C.INDEX_UNSET);
            String uri = parseStringAttr(line, REGEX_URI, variableDefinitions);
            Uri playlistUri = Uri.parse(UriUtil.resolve(baseUri, uri));
            renditionReports.add(new RenditionReport(playlistUri, lastMediaSequence, lastPartIndex));
        } else if (line.startsWith(TAG_PRELOAD_HINT)) {
            if (preloadPart != null) {
                continue;
            }
            String type = parseStringAttr(line, REGEX_PRELOAD_HINT_TYPE, variableDefinitions);
            if (!TYPE_PART.equals(type)) {
                continue;
            }
            String url = parseStringAttr(line, REGEX_URI, variableDefinitions);
            long byteRangeStart = parseOptionalLongAttr(line, REGEX_BYTERANGE_START, /* defaultValue= */
            C.LENGTH_UNSET);
            long byteRangeLength = parseOptionalLongAttr(line, REGEX_BYTERANGE_LENGTH, /* defaultValue= */
            C.LENGTH_UNSET);
            @Nullable String segmentEncryptionIV = getSegmentEncryptionIV(segmentMediaSequence, fullSegmentEncryptionKeyUri, fullSegmentEncryptionIV);
            if (cachedDrmInitData == null && !currentSchemeDatas.isEmpty()) {
                SchemeData[] schemeDatas = currentSchemeDatas.values().toArray(new SchemeData[0]);
                cachedDrmInitData = new DrmInitData(encryptionScheme, schemeDatas);
                if (playlistProtectionSchemes == null) {
                    playlistProtectionSchemes = getPlaylistProtectionSchemes(encryptionScheme, schemeDatas);
                }
            }
            if (byteRangeStart == C.LENGTH_UNSET || byteRangeLength != C.LENGTH_UNSET) {
                // Skip preload part if it is an unbounded range request.
                preloadPart = new Part(url, initializationSegment, /* durationUs= */
                0, relativeDiscontinuitySequence, partStartTimeUs, cachedDrmInitData, fullSegmentEncryptionKeyUri, segmentEncryptionIV, byteRangeStart != C.LENGTH_UNSET ? byteRangeStart : 0, byteRangeLength, /* hasGapTag= */
                false, /* isIndependent= */
                false, /* isPreload= */
                true);
            }
        } else if (line.startsWith(TAG_PART)) {
            @Nullable String segmentEncryptionIV = getSegmentEncryptionIV(segmentMediaSequence, fullSegmentEncryptionKeyUri, fullSegmentEncryptionIV);
            String url = parseStringAttr(line, REGEX_URI, variableDefinitions);
            long partDurationUs = (long) (parseDoubleAttr(line, REGEX_ATTR_DURATION) * C.MICROS_PER_SECOND);
            boolean isIndependent = parseOptionalBooleanAttribute(line, REGEX_INDEPENDENT, /* defaultValue= */
            false);
            // The first part of a segment is always independent if the segments are independent.
            isIndependent |= hasIndependentSegmentsTag && trailingParts.isEmpty();
            boolean isGap = parseOptionalBooleanAttribute(line, REGEX_GAP, /* defaultValue= */
            false);
            @Nullable String byteRange = parseOptionalStringAttr(line, REGEX_ATTR_BYTERANGE, variableDefinitions);
            long partByteRangeLength = C.LENGTH_UNSET;
            if (byteRange != null) {
                String[] splitByteRange = Util.split(byteRange, "@");
                partByteRangeLength = Long.parseLong(splitByteRange[0]);
                if (splitByteRange.length > 1) {
                    partByteRangeOffset = Long.parseLong(splitByteRange[1]);
                }
            }
            if (partByteRangeLength == C.LENGTH_UNSET) {
                partByteRangeOffset = 0;
            }
            if (cachedDrmInitData == null && !currentSchemeDatas.isEmpty()) {
                SchemeData[] schemeDatas = currentSchemeDatas.values().toArray(new SchemeData[0]);
                cachedDrmInitData = new DrmInitData(encryptionScheme, schemeDatas);
                if (playlistProtectionSchemes == null) {
                    playlistProtectionSchemes = getPlaylistProtectionSchemes(encryptionScheme, schemeDatas);
                }
            }
            trailingParts.add(new Part(url, initializationSegment, partDurationUs, relativeDiscontinuitySequence, partStartTimeUs, cachedDrmInitData, fullSegmentEncryptionKeyUri, segmentEncryptionIV, partByteRangeOffset, partByteRangeLength, isGap, isIndependent, /* isPreload= */
            false));
            partStartTimeUs += partDurationUs;
            if (partByteRangeLength != C.LENGTH_UNSET) {
                partByteRangeOffset += partByteRangeLength;
            }
        } else if (!line.startsWith("#")) {
            @Nullable String segmentEncryptionIV = getSegmentEncryptionIV(segmentMediaSequence, fullSegmentEncryptionKeyUri, fullSegmentEncryptionIV);
            segmentMediaSequence++;
            String segmentUri = replaceVariableReferences(line, variableDefinitions);
            @Nullable Segment inferredInitSegment = urlToInferredInitSegment.get(segmentUri);
            if (segmentByteRangeLength == C.LENGTH_UNSET) {
                // The segment has no byte range defined.
                segmentByteRangeOffset = 0;
            } else if (isIFrameOnly && initializationSegment == null && inferredInitSegment == null) {
                // The segment is a resource byte range without an initialization segment.
                // As per RFC 8216, Section 4.3.3.6, we assume the initialization section exists in the
                // bytes preceding the first segment in this segment's URL.
                // We assume the implicit initialization segment is unencrypted, since there's no way for
                // the playlist to provide an initialization vector for it.
                inferredInitSegment = new Segment(segmentUri, /* byteRangeOffset= */
                0, segmentByteRangeOffset, /* fullSegmentEncryptionKeyUri= */
                null, /* encryptionIV= */
                null);
                urlToInferredInitSegment.put(segmentUri, inferredInitSegment);
            }
            if (cachedDrmInitData == null && !currentSchemeDatas.isEmpty()) {
                SchemeData[] schemeDatas = currentSchemeDatas.values().toArray(new SchemeData[0]);
                cachedDrmInitData = new DrmInitData(encryptionScheme, schemeDatas);
                if (playlistProtectionSchemes == null) {
                    playlistProtectionSchemes = getPlaylistProtectionSchemes(encryptionScheme, schemeDatas);
                }
            }
            segments.add(new Segment(segmentUri, initializationSegment != null ? initializationSegment : inferredInitSegment, segmentTitle, segmentDurationUs, relativeDiscontinuitySequence, segmentStartTimeUs, cachedDrmInitData, fullSegmentEncryptionKeyUri, segmentEncryptionIV, segmentByteRangeOffset, segmentByteRangeLength, hasGapTag, trailingParts));
            segmentStartTimeUs += segmentDurationUs;
            partStartTimeUs = segmentStartTimeUs;
            segmentDurationUs = 0;
            segmentTitle = "";
            trailingParts = new ArrayList<>();
            if (segmentByteRangeLength != C.LENGTH_UNSET) {
                segmentByteRangeOffset += segmentByteRangeLength;
            }
            segmentByteRangeLength = C.LENGTH_UNSET;
            hasGapTag = false;
        }
    }
    Map<Uri, RenditionReport> renditionReportMap = new HashMap<>();
    for (int i = 0; i < renditionReports.size(); i++) {
        RenditionReport renditionReport = renditionReports.get(i);
        long lastMediaSequence = renditionReport.lastMediaSequence;
        if (lastMediaSequence == C.INDEX_UNSET) {
            lastMediaSequence = mediaSequence + segments.size() - (trailingParts.isEmpty() ? 1 : 0);
        }
        int lastPartIndex = renditionReport.lastPartIndex;
        if (lastPartIndex == C.INDEX_UNSET && partTargetDurationUs != C.TIME_UNSET) {
            List<Part> lastParts = trailingParts.isEmpty() ? Iterables.getLast(segments).parts : trailingParts;
            lastPartIndex = lastParts.size() - 1;
        }
        renditionReportMap.put(renditionReport.playlistUri, new RenditionReport(renditionReport.playlistUri, lastMediaSequence, lastPartIndex));
    }
    if (preloadPart != null) {
        trailingParts.add(preloadPart);
    }
    return new HlsMediaPlaylist(playlistType, baseUri, tags, startOffsetUs, preciseStart, playlistStartTimeUs, hasDiscontinuitySequence, playlistDiscontinuitySequence, mediaSequence, version, targetDurationUs, partTargetDurationUs, hasIndependentSegmentsTag, hasEndTag, /* hasProgramDateTime= */
    playlistStartTimeUs != 0, playlistProtectionSchemes, segments, trailingParts, serverControl, renditionReportMap);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RenditionReport(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.RenditionReport) SchemeData(com.google.android.exoplayer2.drm.DrmInitData.SchemeData) Uri(android.net.Uri) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment) DrmInitData(com.google.android.exoplayer2.drm.DrmInitData) TreeMap(java.util.TreeMap) Part(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Part) Nullable(androidx.annotation.Nullable)

Aggregations

HlsMediaPlaylist (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist)20 Uri (android.net.Uri)18 Segment (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment)18 Test (org.junit.Test)18 InputStream (java.io.InputStream)11 ArrayList (java.util.ArrayList)11 Nullable (androidx.annotation.Nullable)9 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 HlsPlaylistParser (com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser)4 IOException (java.io.IOException)3 ParserException (com.google.android.exoplayer2.ParserException)2 BehindLiveWindowException (com.google.android.exoplayer2.source.BehindLiveWindowException)2 BaseMediaChunkIterator (com.google.android.exoplayer2.source.chunk.BaseMediaChunkIterator)2 MediaChunkIterator (com.google.android.exoplayer2.source.chunk.MediaChunkIterator)2 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)2 SystemClock (android.os.SystemClock)1 Pair (android.util.Pair)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1