Search in sources :

Example 66 with PlayerRunnable

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

the class ExoPlayerTest method recursivePlayerChangesReportConsistentValuesForAllListeners.

@Test
public void recursivePlayerChangesReportConsistentValuesForAllListeners() throws Exception {
    // We add two listeners to the player. The first stops the player as soon as it's ready and both
    // record the state change events they receive.
    final AtomicReference<Player> playerReference = new AtomicReference<>();
    final List<Integer> playerListener1States = new ArrayList<>();
    final List<Integer> playerListener2States = new ArrayList<>();
    final Player.Listener playerListener1 = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListener1States.add(playbackState);
            if (playbackState == Player.STATE_READY) {
                playerReference.get().stop(/* reset= */
                true);
            }
        }
    };
    final Player.Listener playerListener2 = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListener2States.add(playbackState);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerReference.set(player);
            player.addListener(playerListener1);
            player.addListener(playerListener2);
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(playerListener1States).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
    assertThat(playerListener2States).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Listener(com.google.android.exoplayer2.Player.Listener) MediaSourceEventListener(com.google.android.exoplayer2.source.MediaSourceEventListener) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Listener(com.google.android.exoplayer2.Player.Listener) Test(org.junit.Test)

Example 67 with PlayerRunnable

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

the class ExoPlayerTest method addMediaSources_skipSettingMediaItems_validInitialSeek_correctMasking.

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

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
            // If the timeline is empty masking variables are used.
            currentPositions[0] = player.getCurrentPosition();
            bufferedPositions[0] = player.getBufferedPosition();
            player.addMediaSource(/* index= */
            0, new ConcatenatingMediaSource());
            currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
            player.addMediaSource(/* index= */
            0, new FakeMediaSource(new FakeTimeline(/* windowCount= */
            2)));
            currentMediaItemIndices[2] = player.getCurrentMediaItemIndex();
            player.addMediaSource(/* index= */
            0, new FakeMediaSource());
            currentMediaItemIndices[3] = player.getCurrentMediaItemIndex();
            // With a non-empty timeline, we mask the periodId in the playback info.
            currentPositions[1] = player.getCurrentPosition();
            bufferedPositions[1] = player.getBufferedPosition();
        }
    }).prepare().waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[4] = player.getCurrentMediaItemIndex();
            // Finally original playbackInfo coming from EPII is used.
            currentPositions[2] = player.getCurrentPosition();
            bufferedPositions[2] = player.getBufferedPosition();
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).skipSettingMediaSources().initialSeek(/* mediaItemIndex= */
    1, 2000).setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertArrayEquals(new int[] { 1, 1, 1, 2, 2 }, currentMediaItemIndices);
    assertThat(currentPositions[0]).isEqualTo(2000);
    assertThat(currentPositions[1]).isEqualTo(2000);
    assertThat(currentPositions[2]).isAtLeast(2000);
    assertThat(bufferedPositions[0]).isEqualTo(2000);
    assertThat(bufferedPositions[1]).isEqualTo(2000);
    assertThat(bufferedPositions[2]).isAtLeast(2000);
}
Also used : 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) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 68 with PlayerRunnable

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

the class ExoPlayerTest method seekToUnpreparedWindowWithMultiplePeriodsInConcatenationStartsAtCorrectPeriod.

@Test
public void seekToUnpreparedWindowWithMultiplePeriodsInConcatenationStartsAtCorrectPeriod() throws Exception {
    long periodDurationMs = 5000;
    Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    2, /* id= */
    new Object(), /* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    2 * periodDurationMs * 1000));
    FakeMediaSource mediaSource = new FakeMediaSource(/* timeline= */
    null);
    MediaSource concatenatedMediaSource = new ConcatenatingMediaSource(mediaSource);
    AtomicInteger periodIndexWhenReady = new AtomicInteger();
    AtomicLong positionWhenReady = new AtomicLong();
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_BUFFERING).seek(/* positionMs= */
    periodDurationMs + 10).executeRunnable(() -> mediaSource.setNewSourceInfo(timeline)).waitForTimelineChanged().waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            periodIndexWhenReady.set(player.getCurrentPeriodIndex());
            positionWhenReady.set(player.getContentPosition());
        }
    }).play().build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(concatenatedMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(periodIndexWhenReady.get()).isEqualTo(1);
    assertThat(positionWhenReady.get()).isEqualTo(periodDurationMs + 10);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) AtomicLong(java.util.concurrent.atomic.AtomicLong) ServerSideAdInsertionMediaSource(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource) MaskingMediaSource(com.google.android.exoplayer2.source.MaskingMediaSource) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) CompositeMediaSource(com.google.android.exoplayer2.source.CompositeMediaSource) ClippingMediaSource(com.google.android.exoplayer2.source.ClippingMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 69 with PlayerRunnable

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

the class ExoPlayerTest method setMediaSources_whenEnded_correctMaskingPlaybackState.

@Test
public void setMediaSources_whenEnded_correctMaskingPlaybackState() throws Exception {
    Timeline firstTimeline = new FakeTimeline(/* windowCount= */
    1, 1L);
    MediaSource firstMediaSource = new FakeMediaSource(firstTimeline);
    Timeline secondTimeline = new FakeTimeline(/* windowCount= */
    1, 2L);
    MediaSource secondMediaSource = new FakeMediaSource(secondTimeline);
    final int[] maskingPlaybackStates = new int[4];
    Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set empty media item with an implicit seek to the current position.
            player.setMediaSource(new ConcatenatingMediaSource());
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).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[1] = player.getPlaybackState();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {

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

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an explicit seek.
            player.setMediaSource(secondMediaSource, /* startPositionMs= */
            C.TIME_UNSET);
            maskingPlaybackStates[3] = player.getPlaybackState();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).setExpectedPlayerEndedCount(/* expectedPlayerEndedCount= */
    3).setMediaSources(new ConcatenatingMediaSource()).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    // Expect reset of masking to first media item.
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_ENDED, Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_ENDED, Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_ENDED);
    assertArrayEquals(new int[] { Player.STATE_ENDED, Player.STATE_ENDED, Player.STATE_BUFFERING, Player.STATE_BUFFERING }, maskingPlaybackStates);
    exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ServerSideAdInsertionMediaSource(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource) MaskingMediaSource(com.google.android.exoplayer2.source.MaskingMediaSource) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) CompositeMediaSource(com.google.android.exoplayer2.source.CompositeMediaSource) ClippingMediaSource(com.google.android.exoplayer2.source.ClippingMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 70 with PlayerRunnable

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

the class ExoPlayerTest method seekTo_toLoadingPeriod_correctMaskingPosition.

@Test
public void seekTo_toLoadingPeriod_correctMaskingPosition() throws Exception {
    final int[] mediaItemIndex = { C.INDEX_UNSET, C.INDEX_UNSET };
    final long[] positionMs = { C.INDEX_UNSET, C.INDEX_UNSET };
    final long[] bufferedPositions = { C.INDEX_UNSET, C.INDEX_UNSET };
    final long[] totalBufferedDuration = { C.INDEX_UNSET, C.INDEX_UNSET };
    runPositionMaskingCapturingActionSchedule(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            player.seekTo(1, 1000);
        }
    }, /* pauseMediaItemIndex= */
    0, mediaItemIndex, positionMs, bufferedPositions, totalBufferedDuration, new FakeMediaSource(), new FakeMediaSource());
    assertThat(mediaItemIndex[0]).isEqualTo(1);
    assertThat(positionMs[0]).isEqualTo(1000);
    // TODO(b/160450903): Verify masking of buffering properties when behaviour in EPII is fully
    // covered.
    // assertThat(bufferedPositions[0]).isEqualTo(10_000);
    // assertThat(totalBufferedDuration[0]).isEqualTo(10_000 - positionMs[0]);
    assertThat(mediaItemIndex[1]).isEqualTo(mediaItemIndex[0]);
    assertThat(positionMs[1]).isEqualTo(positionMs[0]);
    assertThat(bufferedPositions[1]).isEqualTo(10_000);
    assertThat(totalBufferedDuration[1]).isEqualTo(10_000 - positionMs[1]);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) Test(org.junit.Test)

Aggregations

PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)93 Test (org.junit.Test)91 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)76 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)74 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)56 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)51 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)43 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)34 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)28 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)28 MediaSource (com.google.android.exoplayer2.source.MediaSource)21 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)20 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)20 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)20 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)20 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)20 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)18 AtomicLong (java.util.concurrent.atomic.AtomicLong)15 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11