Search in sources :

Example 26 with ActionSchedule

use of androidx.media3.test.utils.ActionSchedule in project media by androidx.

the class ExoPlayerTest method recursivePlayerChangesAreReportedInCorrectOrder.

@Test
public void recursivePlayerChangesAreReportedInCorrectOrder() throws Exception {
    // The listener stops the player as soon as it's ready (which should report a timeline and state
    // change) and sets playWhenReady to false when the timeline callback is received.
    final AtomicReference<Player> playerReference = new AtomicReference<>();
    final List<Boolean> playerListenerPlayWhenReady = new ArrayList<>();
    final List<Integer> playerListenerStates = new ArrayList<>();
    List<Integer> sequence = new ArrayList<>();
    final Player.Listener playerListener = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListenerStates.add(playbackState);
            if (playbackState == Player.STATE_READY) {
                playerReference.get().stop(/* reset= */
                true);
                sequence.add(0);
            }
        }

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            if (timeline.isEmpty()) {
                playerReference.get().pause();
                sequence.add(1);
            }
        }

        @Override
        public void onPlayWhenReadyChanged(boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
            playerListenerPlayWhenReady.add(playWhenReady);
            sequence.add(2);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerReference.set(player);
            player.addListener(playerListener);
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(playerListenerStates).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
    assertThat(playerListenerPlayWhenReady).containsExactly(false).inOrder();
    assertThat(sequence).containsExactly(0, 1, 2).inOrder();
}
Also used : Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Listener(androidx.media3.common.Player.Listener) Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 27 with ActionSchedule

use of androidx.media3.test.utils.ActionSchedule in project media by androidx.

the class ExoPlayerTest method clippedLoopedPeriodsArePlayedFully.

@Test
public void clippedLoopedPeriodsArePlayedFully() throws Exception {
    long startPositionUs = 300_000;
    long expectedDurationUs = 700_000;
    MediaSource mediaSource = new ClippingMediaSource(new FakeMediaSource(), startPositionUs, startPositionUs + expectedDurationUs);
    Clock clock = new FakeClock(/* isAutoAdvancing= */
    true);
    AtomicReference<Player> playerReference = new AtomicReference<>();
    AtomicLong positionAtDiscontinuityMs = new AtomicLong(C.TIME_UNSET);
    AtomicLong clockAtStartMs = new AtomicLong(C.TIME_UNSET);
    AtomicLong clockAtDiscontinuityMs = new AtomicLong(C.TIME_UNSET);
    Player.Listener playerListener = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            if (playbackState == Player.STATE_READY && clockAtStartMs.get() == C.TIME_UNSET) {
                clockAtStartMs.set(clock.elapsedRealtime());
            }
        }

        @Override
        public void onPositionDiscontinuity(@DiscontinuityReason int reason) {
            if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
                positionAtDiscontinuityMs.set(playerReference.get().getCurrentPosition());
                clockAtDiscontinuityMs.set(clock.elapsedRealtime());
            }
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerReference.set(player);
            player.addListener(playerListener);
        }
    }).pause().setRepeatMode(Player.REPEAT_MODE_ALL).waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
    0, /* positionMs= */
    1).playUntilStartOfMediaItem(/* mediaItemIndex= */
    0).setRepeatMode(Player.REPEAT_MODE_OFF).play().build();
    new ExoPlayerTestRunner.Builder(context).setClock(clock).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(positionAtDiscontinuityMs.get()).isAtLeast(0L);
    assertThat(clockAtDiscontinuityMs.get() - clockAtStartMs.get()).isAtLeast(Util.usToMs(expectedDurationUs));
}
Also used : Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) FakeClock(androidx.media3.test.utils.FakeClock) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) FakeClock(androidx.media3.test.utils.FakeClock) Clock(androidx.media3.common.util.Clock) Listener(androidx.media3.common.Player.Listener) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) Test(org.junit.Test)

Example 28 with ActionSchedule

use of androidx.media3.test.utils.ActionSchedule in project media by androidx.

the class ExoPlayerTest method pauseAtEndOfMediaItems_pausesPlaybackWhenEnded.

@Test
public void pauseAtEndOfMediaItems_pausesPlaybackWhenEnded() throws Exception {
    TimelineWindowDefinition timelineWindowDefinition = new TimelineWindowDefinition(/* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    10 * C.MICROS_PER_SECOND);
    MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(timelineWindowDefinition));
    AtomicInteger playbackStateAfterPause = new AtomicInteger(C.INDEX_UNSET);
    AtomicLong positionAfterPause = new AtomicLong(C.TIME_UNSET);
    AtomicInteger mediaItemIndexAfterPause = new AtomicInteger(C.INDEX_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlayWhenReady(true).waitForPlayWhenReady(false).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playbackStateAfterPause.set(player.getPlaybackState());
            mediaItemIndexAfterPause.set(player.getCurrentMediaItemIndex());
            positionAfterPause.set(player.getContentPosition());
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setPauseAtEndOfMediaItems(true).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertThat(playbackStateAfterPause.get()).isEqualTo(Player.STATE_ENDED);
    assertThat(mediaItemIndexAfterPause.get()).isEqualTo(0);
    assertThat(positionAfterPause.get()).isEqualTo(10_000);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Test(org.junit.Test)

Example 29 with ActionSchedule

use of androidx.media3.test.utils.ActionSchedule in project media by androidx.

the class ExoPlayerTest method setMediaSources_whenIdle_invalidSeek_correctMaskingPlaybackState.

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

        @Override
        public void run(ExoPlayer player) {
            // Set a media item with an implicit seek to the current position which is
            // invalid in the new timeline.
            player.setMediaSource(new FakeMediaSource(new FakeTimeline(/* windowCount= */
            1, 1L)));
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).prepare().waitForPlaybackState(Player.STATE_READY).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
    1, /* positionMs= */
    C.TIME_UNSET).setMediaSources(new ConcatenatingMediaSource()).setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    // Expect reset of masking to first media item.
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_ENDED);
    assertArrayEquals(new int[] { Player.STATE_IDLE }, maskingPlaybackStates);
    exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Test(org.junit.Test)

Example 30 with ActionSchedule

use of androidx.media3.test.utils.ActionSchedule in project media by androidx.

the class ExoPlayerTest method seekToCurrentPosition_inEndedState_switchesToBufferingStateAndContinuesPlayback.

@Test
public void seekToCurrentPosition_inEndedState_switchesToBufferingStateAndContinuesPlayback() throws Exception {
    MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(/* windowCount = */
    1));
    AtomicInteger mediaItemIndexAfterFinalEndedState = new AtomicInteger();
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_ENDED).addMediaSources(mediaSource).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            player.seekTo(player.getCurrentPosition());
        }
    }).waitForPlaybackState(Player.STATE_READY).waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            mediaItemIndexAfterFinalEndedState.set(player.getCurrentMediaItemIndex());
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertThat(mediaItemIndexAfterFinalEndedState.get()).isEqualTo(1);
}
Also used : CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Aggregations

ActionSchedule (androidx.media3.test.utils.ActionSchedule)143 Test (org.junit.Test)142 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)103 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)97 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)96 FakeTimeline (androidx.media3.test.utils.FakeTimeline)84 PlayerRunnable (androidx.media3.test.utils.ActionSchedule.PlayerRunnable)79 Timeline (androidx.media3.common.Timeline)64 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)59 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)59 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)55 MediaSource (androidx.media3.exoplayer.source.MediaSource)40 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)36 ClippingMediaSource (androidx.media3.exoplayer.source.ClippingMediaSource)30 CompositeMediaSource (androidx.media3.exoplayer.source.CompositeMediaSource)30 MaskingMediaSource (androidx.media3.exoplayer.source.MaskingMediaSource)30 ServerSideAdInsertionMediaSource (androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource)30 FakeAdaptiveMediaSource (androidx.media3.test.utils.FakeAdaptiveMediaSource)30 TransferListener (androidx.media3.datasource.TransferListener)19 Player (androidx.media3.common.Player)17