Search in sources :

Example 1 with MediaSourceCaller

use of com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller 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 2 with MediaSourceCaller

use of com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller in project ExoPlayer by google.

the class BaseMediaSource method prepareSource.

@Override
public final void prepareSource(MediaSourceCaller caller, @Nullable TransferListener mediaTransferListener, PlayerId playerId) {
    Looper looper = Looper.myLooper();
    Assertions.checkArgument(this.looper == null || this.looper == looper);
    this.playerId = playerId;
    @Nullable Timeline timeline = this.timeline;
    mediaSourceCallers.add(caller);
    if (this.looper == null) {
        this.looper = looper;
        enabledMediaSourceCallers.add(caller);
        prepareSourceInternal(mediaTransferListener);
    } else if (timeline != null) {
        enable(caller);
        caller.onSourceInfoRefreshed(/* source= */
        this, timeline);
    }
}
Also used : Looper(android.os.Looper) Timeline(com.google.android.exoplayer2.Timeline) Nullable(androidx.annotation.Nullable)

Example 3 with MediaSourceCaller

use of com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller in project ExoPlayer by google.

the class DashMediaSourceTest method prepareAndWaitForTimelineRefresh.

private static Window prepareAndWaitForTimelineRefresh(MediaSource mediaSource) throws InterruptedException {
    AtomicReference<Window> windowReference = new AtomicReference<>();
    CountDownLatch countDownLatch = new CountDownLatch(/* count= */
    1);
    MediaSourceCaller caller = (MediaSource source, Timeline timeline) -> {
        if (windowReference.get() == null) {
            windowReference.set(timeline.getWindow(0, new Timeline.Window()));
            countDownLatch.countDown();
        }
    };
    mediaSource.prepareSource(caller, /* mediaTransferListener= */
    null, PlayerId.UNSET);
    while (!countDownLatch.await(/* timeout= */
    10, MILLISECONDS)) {
        ShadowLooper.idleMainLooper();
    }
    return windowReference.get();
}
Also used : Window(com.google.android.exoplayer2.Timeline.Window) MediaSourceCaller(com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller) Timeline(com.google.android.exoplayer2.Timeline) Window(com.google.android.exoplayer2.Timeline.Window) MediaSource(com.google.android.exoplayer2.source.MediaSource) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with MediaSourceCaller

use of com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller in project ExoPlayer by google.

the class ConcatenatingMediaSourceTest method customCallbackIsCalledAfterRelease.

@Test
public void customCallbackIsCalledAfterRelease() throws Exception {
    DummyMainThread testThread = new DummyMainThread();
    CountDownLatch callbackCalledCondition = new CountDownLatch(1);
    try {
        testThread.runOnMainThread(() -> {
            MediaSourceCaller caller = mock(MediaSourceCaller.class);
            mediaSource.addMediaSources(Arrays.asList(createMediaSources(2)));
            mediaSource.prepareSource(caller, /* mediaTransferListener= */
            null, PlayerId.UNSET);
            mediaSource.moveMediaSource(/* currentIndex= */
            0, /* newIndex= */
            1, Util.createHandlerForCurrentLooper(), callbackCalledCondition::countDown);
            mediaSource.releaseSource(caller);
        });
        assertThat(callbackCalledCondition.await(MediaSourceTestRunner.TIMEOUT_MS, MILLISECONDS)).isTrue();
    } finally {
        testThread.release();
    }
}
Also used : MediaSourceCaller(com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller) DummyMainThread(com.google.android.exoplayer2.testutil.DummyMainThread) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Timeline (com.google.android.exoplayer2.Timeline)3 MediaSource (com.google.android.exoplayer2.source.MediaSource)2 MediaSourceCaller (com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 Uri (android.net.Uri)1 Looper (android.os.Looper)1 SystemClock (android.os.SystemClock)1 Nullable (androidx.annotation.Nullable)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1 C (com.google.android.exoplayer2.C)1 MediaItem (com.google.android.exoplayer2.MediaItem)1 ParserException (com.google.android.exoplayer2.ParserException)1 Window (com.google.android.exoplayer2.Timeline.Window)1 PlayerId (com.google.android.exoplayer2.analytics.PlayerId)1 RobolectricUtil.runMainLooperUntil (com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil)1 HlsMediaPlaylist (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist)1 HlsPlaylistParser (com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser)1 DummyMainThread (com.google.android.exoplayer2.testutil.DummyMainThread)1