Search in sources :

Example 56 with MediaItem

use of com.google.android.exoplayer2.MediaItem 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 57 with MediaItem

use of com.google.android.exoplayer2.MediaItem 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 58 with MediaItem

use of com.google.android.exoplayer2.MediaItem 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)

Example 59 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class HlsMediaSourceTest method loadLivePlaylist_holdBackInPlaylist_targetLiveOffsetFromHoldBack.

@Test
public void loadLivePlaylist_holdBackInPlaylist_targetLiveOffsetFromHoldBack() throws TimeoutException, ParserException {
    String playlistUri = "fake://foo.bar/media0/playlist.m3u8";
    // The playlist has a duration of 16 seconds and a hold back of 12 seconds.
    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\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 playlist finishes 1 second before the current time, therefore there's a live edge
    // offset of 1 second.
    SystemClock.setCurrentTimeMillis(Util.parseXsDateTime("2020-01-01T00:00:17.0+00:00"));
    HlsMediaSource.Factory factory = createHlsMediaSourceFactory(playlistUri, playlist);
    MediaItem mediaItem = MediaItem.fromUri(playlistUri);
    HlsMediaSource mediaSource = factory.createMediaSource(mediaItem);
    Timeline timeline = prepareAndWaitForTimeline(mediaSource);
    Timeline.Window window = timeline.getWindow(0, new Timeline.Window());
    // The target live offset is picked from hold back and then expressed in relation to the live
    // edge (+1 seconds).
    assertThat(window.liveConfiguration.targetOffsetMs).isEqualTo(13000);
    assertThat(window.liveConfiguration.minPlaybackSpeed).isEqualTo(C.RATE_UNSET);
    assertThat(window.liveConfiguration.maxPlaybackSpeed).isEqualTo(C.RATE_UNSET);
    assertThat(window.defaultPositionUs).isEqualTo(4000000);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 60 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class HlsMediaSourceTest method loadOnDemandPlaylist_withStartTimeAfterTheNed_setsDefaultPositionToTheEnd.

@Test
public void loadOnDemandPlaylist_withStartTimeAfterTheNed_setsDefaultPositionToTheEnd() 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(20000000);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Aggregations

MediaItem (com.google.android.exoplayer2.MediaItem)160 Test (org.junit.Test)151 MediaSource (com.google.android.exoplayer2.source.MediaSource)31 ArrayList (java.util.ArrayList)29 Timeline (com.google.android.exoplayer2.Timeline)25 InOrder (org.mockito.InOrder)20 Nullable (androidx.annotation.Nullable)19 AdsMediaSource (com.google.android.exoplayer2.source.ads.AdsMediaSource)19 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)19 Player (com.google.android.exoplayer2.Player)17 Uri (android.net.Uri)15 Listener (com.google.android.exoplayer2.Player.Listener)15 TestPlayerRunHelper.playUntilStartOfMediaItem (com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem)15 DefaultMediaSourceFactory (com.google.android.exoplayer2.source.DefaultMediaSourceFactory)15 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)13 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)13 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)12 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)12