Search in sources :

Example 61 with FakeMediaSource

use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.

the class ExoPlayerTest method mediaSourceMaybeThrowSourceInfoRefreshError_isNotThrownUntilPlaybackReachedFailingItem.

@Test
public void mediaSourceMaybeThrowSourceInfoRefreshError_isNotThrownUntilPlaybackReachedFailingItem() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    player.addMediaSource(new FakeMediaSource());
    player.addMediaSource(new FakeMediaSource(/* timeline= */
    null) {

        @Override
        public void maybeThrowSourceInfoRefreshError() throws IOException {
            throw new IOException();
        }
    });
    player.prepare();
    player.play();
    ExoPlaybackException error = TestPlayerRunHelper.runUntilError(player);
    Object period1Uid = player.getCurrentTimeline().getPeriod(/* periodIndex= */
    1, new Timeline.Period(), /* setIds= */
    true).uid;
    assertThat(error.mediaPeriodId.periodUid).isEqualTo(period1Uid);
    assertThat(player.getCurrentMediaItemIndex()).isEqualTo(1);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod) FakeMediaPeriod(com.google.android.exoplayer2.testutil.FakeMediaPeriod) IOException(java.io.IOException) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 62 with FakeMediaSource

use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.

the class ExoPlayerTest method setMediaSources_whenEmpty_invalidInitialSeek_correctMasking.

@Test
public void setMediaSources_whenEmpty_invalidInitialSeek_correctMasking() throws Exception {
    final int[] currentMediaItemIndices = { C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET };
    final long[] currentPositions = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    final long[] bufferedPositions = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPositionDiscontinuity().waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
            currentPositions[0] = player.getCurrentPosition();
            bufferedPositions[0] = player.getBufferedPosition();
            // Increase current media item index.
            player.addMediaSource(/* index= */
            0, new FakeMediaSource());
            currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
            currentPositions[1] = player.getCurrentPosition();
            bufferedPositions[1] = player.getBufferedPosition();
        }
    }).prepare().waitForTimelineChanged().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[2] = player.getCurrentMediaItemIndex();
            currentPositions[2] = player.getCurrentPosition();
            bufferedPositions[2] = player.getBufferedPosition();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).build();
    new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
    1, 2000).setMediaSources(new FakeMediaSource()).setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertArrayEquals(new int[] { 0, 1, 1 }, currentMediaItemIndices);
    assertArrayEquals(new long[] { 0, 0, 0 }, currentPositions);
    assertArrayEquals(new long[] { 0, 0, 0 }, bufferedPositions);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 63 with FakeMediaSource

use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.

the class ExoPlayerTest method timelineUpdateDropsPrebufferedPeriods.

@Test
public void timelineUpdateDropsPrebufferedPeriods() throws Exception {
    Timeline timeline1 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    1), new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    2));
    final Timeline timeline2 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    1), new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    3));
    final FakeMediaSource mediaSource = new FakeMediaSource(timeline1, ExoPlayerTestRunner.VIDEO_FORMAT);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
    0, /* positionMs= */
    Util.usToMs(TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US)).executeRunnable(() -> mediaSource.setNewSourceInfo(timeline2)).waitForTimelineChanged(timeline2, /* expectedReason */
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE).play().build();
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    testRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
    testRunner.assertPlayedPeriodIndices(0, 1);
    // Assert that the second period was re-created from the new timeline.
    assertThat(mediaSource.getCreatedMediaPeriods()).hasSize(3);
    assertThat(mediaSource.getCreatedMediaPeriods().get(0).periodUid).isEqualTo(timeline1.getUidOfPeriod(/* periodIndex= */
    0));
    assertThat(mediaSource.getCreatedMediaPeriods().get(1).periodUid).isEqualTo(timeline1.getUidOfPeriod(/* periodIndex= */
    1));
    assertThat(mediaSource.getCreatedMediaPeriods().get(2).periodUid).isEqualTo(timeline2.getUidOfPeriod(/* periodIndex= */
    1));
    assertThat(mediaSource.getCreatedMediaPeriods().get(1).windowSequenceNumber).isGreaterThan(mediaSource.getCreatedMediaPeriods().get(0).windowSequenceNumber);
    assertThat(mediaSource.getCreatedMediaPeriods().get(2).windowSequenceNumber).isGreaterThan(mediaSource.getCreatedMediaPeriods().get(1).windowSequenceNumber);
}
Also used : NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 64 with FakeMediaSource

use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.

the class ExoPlayerTest method seekTo_mediaItemIndexIsReset_deprecated.

@SuppressWarnings("deprecation")
@Test
public void seekTo_mediaItemIndexIsReset_deprecated() throws Exception {
    FakeTimeline fakeTimeline = new FakeTimeline();
    FakeMediaSource mediaSource = new FakeMediaSource(fakeTimeline);
    final int[] mediaItemIndex = { C.INDEX_UNSET };
    final long[] positionMs = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    final long[] bufferedPositions = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().seek(/* mediaItemIndex= */
    1, /* positionMs= */
    C.TIME_UNSET).playUntilPosition(/* mediaItemIndex= */
    1, /* positionMs= */
    3000).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            positionMs[0] = player.getCurrentPosition();
            bufferedPositions[0] = player.getBufferedPosition();
            // noinspection deprecation
            player.prepare(mediaSource);
            player.seekTo(/* positionMs= */
            7000);
            positionMs[1] = player.getCurrentPosition();
            bufferedPositions[1] = player.getBufferedPosition();
        }
    }).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            mediaItemIndex[0] = player.getCurrentMediaItemIndex();
            positionMs[2] = player.getCurrentPosition();
            bufferedPositions[2] = player.getBufferedPosition();
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource, mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS);
    assertThat(mediaItemIndex[0]).isEqualTo(0);
    assertThat(positionMs[0]).isAtLeast(3000L);
    assertThat(positionMs[1]).isEqualTo(7000L);
    assertThat(positionMs[2]).isEqualTo(7000L);
    assertThat(bufferedPositions[0]).isAtLeast(3000L);
    assertThat(bufferedPositions[1]).isEqualTo(7000L);
    assertThat(bufferedPositions[2]).isEqualTo(fakeTimeline.getWindow(0, new Window()).getDurationMs());
}
Also used : Window(com.google.android.exoplayer2.Timeline.Window) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 65 with FakeMediaSource

use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.

the class ExoPlayerTest method setMediaSources_whenIdle_correctMaskingPlaybackState.

@Test
public void setMediaSources_whenIdle_correctMaskingPlaybackState() throws Exception {
    final int[] maskingPlaybackStates = new int[4];
    Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set empty media item with no seek.
            player.setMediaSource(new ConcatenatingMediaSource());
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an implicit seek to the current position.
            player.setMediaSource(new FakeMediaSource());
            maskingPlaybackStates[1] = player.getPlaybackState();
        }
    }).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an explicit seek.
            player.setMediaSource(new FakeMediaSource(), /* startPositionMs= */
            C.TIME_UNSET);
            maskingPlaybackStates[2] = player.getPlaybackState();
        }
    }).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set empty media item with an explicit seek.
            player.setMediaSource(new ConcatenatingMediaSource(), /* startPositionMs= */
            C.TIME_UNSET);
            maskingPlaybackStates[3] = player.getPlaybackState();
        }
    }).prepare().build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).skipSettingMediaSources().setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    // Expect reset of masking to first media item.
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_ENDED);
    assertArrayEquals(new int[] { Player.STATE_IDLE, Player.STATE_IDLE, Player.STATE_IDLE, Player.STATE_IDLE }, maskingPlaybackStates);
    exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Aggregations

FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)262 Test (org.junit.Test)252 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)177 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)169 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)113 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)111 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)109 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)94 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)93 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)89 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)84 MediaSource (com.google.android.exoplayer2.source.MediaSource)76 Listener (com.google.android.exoplayer2.Player.Listener)64 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)53 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)52 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)52 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)52 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)52 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)45 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)44