Search in sources :

Example 1 with HlsPlaylistParser

use of com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser 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 HlsPlaylistParser

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

the class HlsMediaPlaylistParserTest method encryptedMapTagWithNoIvFails.

@Test
public void encryptedMapTagWithNoIvFails() throws IOException {
    Uri playlistUri = Uri.parse("https://example.com/test3.m3u8");
    String playlistString = "#EXTM3U\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-TARGETDURATION:5\n" + "#EXT-X-MEDIA-SEQUENCE:10\n" + "#EXT-X-KEY:METHOD=AES-128," + "URI=\"https://priv.example.com/key.php?r=2680\"\n" + "#EXT-X-MAP:URI=\"init1.ts\"" + "#EXTINF:5.005,\n" + "02/00/32.ts\n";
    InputStream inputStream = new ByteArrayInputStream(Util.getUtf8Bytes(playlistString));
    try {
        new HlsPlaylistParser().parse(playlistUri, inputStream);
        fail();
    } catch (ParserException e) {
    // Expected because the initialization segment does not have a defined initialization vector,
    // although it is affected by an EXT-X-KEY tag.
    }
}
Also used : ParserException(com.google.android.exoplayer2.ParserException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Uri(android.net.Uri) Test(org.junit.Test)

Example 3 with HlsPlaylistParser

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

the class HlsMediaPlaylistParserTest method parseMediaPlaylist_withByteRanges.

@Test
public void parseMediaPlaylist_withByteRanges() throws Exception {
    Uri playlistUri = Uri.parse("https://example.com/test.m3u8");
    String playlistString = "#EXTM3U\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-TARGETDURATION:5\n" + "\n" + "#EXT-X-BYTERANGE:200@100\n" + "#EXT-X-MAP:URI=\"stream.mp4\"\n" + "#EXTINF:5,\n" + "#EXT-X-BYTERANGE:400\n" + "stream.mp4\n" + "#EXTINF:5,\n" + "#EXT-X-BYTERANGE:500\n" + "stream.mp4\n" + "#EXT-X-DISCONTINUITY\n" + "#EXT-X-MAP:URI=\"init.mp4\"\n" + "#EXTINF:5,\n" + "segment.mp4\n";
    InputStream inputStream = new ByteArrayInputStream(Util.getUtf8Bytes(playlistString));
    HlsPlaylist playlist = new HlsPlaylistParser().parse(playlistUri, inputStream);
    HlsMediaPlaylist mediaPlaylist = (HlsMediaPlaylist) playlist;
    List<Segment> segments = mediaPlaylist.segments;
    assertThat(segments).isNotNull();
    assertThat(segments).hasSize(3);
    Segment segment = segments.get(0);
    assertThat(segment.initializationSegment.byteRangeOffset).isEqualTo(100);
    assertThat(segment.initializationSegment.byteRangeLength).isEqualTo(200);
    assertThat(segment.byteRangeOffset).isEqualTo(300);
    assertThat(segment.byteRangeLength).isEqualTo(400);
    segment = segments.get(1);
    assertThat(segment.byteRangeOffset).isEqualTo(700);
    assertThat(segment.byteRangeLength).isEqualTo(500);
    segment = segments.get(2);
    assertThat(segment.initializationSegment.byteRangeOffset).isEqualTo(0);
    assertThat(segment.initializationSegment.byteRangeLength).isEqualTo(C.LENGTH_UNSET);
    assertThat(segment.byteRangeOffset).isEqualTo(0);
    assertThat(segment.byteRangeLength).isEqualTo(C.LENGTH_UNSET);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Uri(android.net.Uri) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment) Test(org.junit.Test)

Example 4 with HlsPlaylistParser

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

the class HlsMediaPlaylistParserTest method iframeOnly_withExplicitInitSegment_hasCorrectByteRange.

@Test
public void iframeOnly_withExplicitInitSegment_hasCorrectByteRange() throws IOException {
    Uri playlistUri = Uri.parse("https://example.com/test3.m3u8");
    String playlistString = "#EXTM3U\n" + "#EXT-X-VERSION:6\n" + "#EXT-X-MEDIA-SEQUENCE:1616630672\n" + "#EXT-X-TARGETDURATION:7\n" + "#EXT-X-DISCONTINUITY-SEQUENCE:491 \n" + "#EXT-X-MAP:URI=\"iframe0.tsv\",BYTERANGE=\"564@0\"\n" + "\n" + "#EXT-X-I-FRAMES-ONLY\n" + "#EXT-X-PROGRAM-DATE-TIME:2021-04-12T17:08:22.000Z\n" + "#EXTINF:1.001000,\n" + "#EXT-X-BYTERANGE:121260@1128\n" + "iframe0.tsv";
    InputStream inputStream = new ByteArrayInputStream(Util.getUtf8Bytes(playlistString));
    HlsMediaPlaylist standalonePlaylist = (HlsMediaPlaylist) new HlsPlaylistParser().parse(playlistUri, inputStream);
    @Nullable Segment initSegment = standalonePlaylist.segments.get(0).initializationSegment;
    assertThat(standalonePlaylist.segments).hasSize(1);
    assertThat(initSegment.byteRangeLength).isEqualTo(564);
    assertThat(initSegment.byteRangeOffset).isEqualTo(0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable) Segment(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment) Test(org.junit.Test)

Example 5 with HlsPlaylistParser

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

the class HlsChunkSourceTest method getAdjustedSeekPositionUs_noIndependentSegments.

@Test
public void getAdjustedSeekPositionUs_noIndependentSegments() throws IOException {
    InputStream inputStream = TestUtil.getInputStream(ApplicationProvider.getApplicationContext(), PLAYLIST);
    HlsMediaPlaylist playlist = (HlsMediaPlaylist) new HlsPlaylistParser().parse(PLAYLIST_URI, inputStream);
    when(mockPlaylistTracker.getPlaylistSnapshot(eq(PLAYLIST_URI), anyBoolean())).thenReturn(playlist);
    long adjustedPositionUs = testChunkSource.getAdjustedSeekPositionUs(playlistTimeToPeriodTimeUs(100_000_000), SeekParameters.EXACT);
    assertThat(periodTimeToPlaylistTimeUs(adjustedPositionUs)).isEqualTo(100_000_000);
}
Also used : InputStream(java.io.InputStream) HlsPlaylistParser(com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser) HlsMediaPlaylist(com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)12 Uri (android.net.Uri)10 Test (org.junit.Test)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Segment (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist.Segment)8 HlsMediaPlaylist (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist)3 HlsPlaylistParser (com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser)3 Nullable (androidx.annotation.Nullable)2 Format (com.google.android.exoplayer2.Format)1 ParserException (com.google.android.exoplayer2.ParserException)1 HlsPlaylistTracker (com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistTracker)1 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)1 IOException (java.io.IOException)1 Before (org.junit.Before)1