Search in sources :

Example 11 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class HlsMediaSourceTest method loadOnDemandPlaylist_withNonPreciseStartTime_setsDefaultPosition.

@Test
public void loadOnDemandPlaylist_withNonPreciseStartTime_setsDefaultPosition() throws TimeoutException {
    String playlistUri = "fake://foo.bar/media0/playlist.m3u8";
    String playlist = "#EXTM3U\n" + "#EXT-X-PLAYLIST-TYPE:VOD\n" + "#EXT-X-TARGETDURATION:10\n" + "#EXT-X-VERSION:4\n" + "#EXT-X-START:TIME-OFFSET=15.000" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:10.0,\n" + "fileSequence1.ts\n" + "#EXTINF:10.0,\n" + "fileSequence2.ts\n" + "#EXT-X-ENDLIST";
    HlsMediaSource.Factory factory = createHlsMediaSourceFactory(playlistUri, playlist);
    MediaItem mediaItem = new MediaItem.Builder().setUri(playlistUri).build();
    HlsMediaSource mediaSource = factory.createMediaSource(mediaItem);
    Timeline timeline = prepareAndWaitForTimeline(mediaSource);
    Timeline.Window window = timeline.getWindow(0, new Timeline.Window());
    // The target live offset is not adjusted to the live edge because the list does not have
    // program date time.
    assertThat(window.liveConfiguration).isNull();
    assertThat(window.defaultPositionUs).isEqualTo(10000000);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 12 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class HlsMediaSourceTest method loadOnDemandPlaylist_withStartTimeBeforeTheBeginning_setsDefaultPositionToTheBeginning.

@Test
public void loadOnDemandPlaylist_withStartTimeBeforeTheBeginning_setsDefaultPositionToTheBeginning() throws TimeoutException {
    String playlistUri = "fake://foo.bar/media0/playlist.m3u8";
    String playlist = "#EXTM3U\n" + "#EXT-X-PLAYLIST-TYPE:VOD\n" + "#EXT-X-TARGETDURATION:10\n" + "#EXT-X-VERSION:4\n" + "#EXT-X-START:TIME-OFFSET=-35.000" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:10.0,\n" + "fileSequence1.ts\n" + "#EXTINF:10.0,\n" + "fileSequence2.ts\n" + "#EXT-X-ENDLIST";
    HlsMediaSource.Factory factory = createHlsMediaSourceFactory(playlistUri, playlist);
    MediaItem mediaItem = new MediaItem.Builder().setUri(playlistUri).build();
    HlsMediaSource mediaSource = factory.createMediaSource(mediaItem);
    Timeline timeline = prepareAndWaitForTimeline(mediaSource);
    Timeline.Window window = timeline.getWindow(0, new Timeline.Window());
    assertThat(window.liveConfiguration).isNull();
    assertThat(window.defaultPositionUs).isEqualTo(0);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 13 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory 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 14 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class HlsMediaSourceTest method loadLivePlaylist_noHoldBackInPlaylistAndNoPlaybackSpeedInMediaItem_usesUnitSpeed.

@Test
public void loadLivePlaylist_noHoldBackInPlaylistAndNoPlaybackSpeedInMediaItem_usesUnitSpeed() throws TimeoutException, ParserException {
    String playlistUri = "fake://foo.bar/media0/playlist.m3u8";
    // The playlist has no hold back defined.
    String playlist = "#EXTM3U\n" + "#EXT-X-PROGRAM-DATE-TIME:2020-01-01T00:00:00.0+00:00\n" + "#EXT-X-TARGETDURATION:4\n" + "#EXT-X-VERSION:3\n" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:4.00000,\n" + "fileSequence0.ts";
    // The playlist finishes 1 second before the current time. This should not affect the target
    // live offset set in the media item.
    SystemClock.setCurrentTimeMillis(Util.parseXsDateTime("2020-01-01T00:00:05.0+00:00"));
    HlsMediaSource.Factory factory = createHlsMediaSourceFactory(playlistUri, playlist);
    MediaItem mediaItem = new MediaItem.Builder().setUri(playlistUri).setLiveConfiguration(new MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(1000).build()).build();
    HlsMediaSource mediaSource = factory.createMediaSource(mediaItem);
    Timeline timeline = prepareAndWaitForTimeline(mediaSource);
    Timeline.Window window = timeline.getWindow(0, new Timeline.Window());
    assertThat(window.liveConfiguration.minPlaybackSpeed).isEqualTo(1f);
    assertThat(window.liveConfiguration.maxPlaybackSpeed).isEqualTo(1f);
    assertThat(window.defaultPositionUs).isEqualTo(0);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 15 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class HlsMediaSourceTest method loadOnDemandPlaylist_withPreciseStartTime_setsDefaultPosition.

@Test
public void loadOnDemandPlaylist_withPreciseStartTime_setsDefaultPosition() throws TimeoutException {
    String playlistUri = "fake://foo.bar/media0/playlist.m3u8";
    String playlist = "#EXTM3U\n" + "#EXT-X-PLAYLIST-TYPE:VOD\n" + "#EXT-X-TARGETDURATION:10\n" + "#EXT-X-VERSION:4\n" + "#EXT-X-START:TIME-OFFSET=15.000,PRECISE=YES" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:10.0,\n" + "fileSequence1.ts\n" + "#EXTINF:10.0,\n" + "fileSequence2.ts\n" + "#EXT-X-ENDLIST";
    HlsMediaSource.Factory factory = createHlsMediaSourceFactory(playlistUri, playlist);
    MediaItem mediaItem = new MediaItem.Builder().setUri(playlistUri).build();
    HlsMediaSource mediaSource = factory.createMediaSource(mediaItem);
    Timeline timeline = prepareAndWaitForTimeline(mediaSource);
    Timeline.Window window = timeline.getWindow(0, new Timeline.Window());
    // The target live offset is not adjusted to the live edge because the list does not have
    // program date time.
    assertThat(window.liveConfiguration).isNull();
    assertThat(window.defaultPositionUs).isEqualTo(15000000);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)34 MediaItem (com.google.android.exoplayer2.MediaItem)23 Timeline (com.google.android.exoplayer2.Timeline)21 StreamKey (com.google.android.exoplayer2.offline.StreamKey)6 Format (com.google.android.exoplayer2.Format)5 MediaSource (com.google.android.exoplayer2.source.MediaSource)5 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)5 ArrayList (java.util.ArrayList)5 PositionHolder (com.google.android.exoplayer2.extractor.PositionHolder)4 DefaultDownloaderFactory (com.google.android.exoplayer2.offline.DefaultDownloaderFactory)4 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)4 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)4 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)4 TimestampAdjuster (com.google.android.exoplayer2.util.TimestampAdjuster)4 List (java.util.List)4 Extractor (com.google.android.exoplayer2.extractor.Extractor)3 Downloader (com.google.android.exoplayer2.offline.Downloader)3 DownloaderFactory (com.google.android.exoplayer2.offline.DownloaderFactory)3 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)3 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)3