Search in sources :

Example 31 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ExoPlayerTest method seekAfterPlaybackError.

@Test
public void seekAfterPlaybackError() throws Exception {
    final Timeline timeline = new FakeTimeline(/* windowCount= */
    2);
    final long[] positionHolder = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    final int[] mediaItemIndexHolder = { C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET };
    final FakeMediaSource firstMediaSource = new FakeMediaSource(timeline);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
    1, /* positionMs= */
    500).throwPlaybackException(ExoPlaybackException.createForSource(new IOException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED)).waitForPlaybackState(Player.STATE_IDLE).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Position while in error state
            positionHolder[0] = player.getCurrentPosition();
            mediaItemIndexHolder[0] = player.getCurrentMediaItemIndex();
        }
    }).seek(/* mediaItemIndex= */
    0, /* positionMs= */
    C.TIME_UNSET).waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Position while in error state
            positionHolder[1] = player.getCurrentPosition();
            mediaItemIndexHolder[1] = player.getCurrentMediaItemIndex();
        }
    }).prepare().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Position after prepare.
            positionHolder[2] = player.getCurrentPosition();
            mediaItemIndexHolder[2] = player.getCurrentMediaItemIndex();
        }
    }).play().build();
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(firstMediaSource).setActionSchedule(actionSchedule).build();
    assertThrows(ExoPlaybackException.class, () -> testRunner.start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS));
    assertThat(positionHolder[0]).isAtLeast(500L);
    assertThat(positionHolder[1]).isEqualTo(0L);
    assertThat(positionHolder[2]).isEqualTo(0L);
    assertThat(mediaItemIndexHolder[0]).isEqualTo(1);
    assertThat(mediaItemIndexHolder[1]).isEqualTo(0);
    assertThat(mediaItemIndexHolder[2]).isEqualTo(0);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) IOException(java.io.IOException) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 32 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ExoPlayerTest method setMediaSources_whenPrepared_correctMaskingPlaybackState.

@Test
public void setMediaSources_whenPrepared_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).pause().waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set empty media item with an implicit seek to current position.
            player.setMediaSource(new ConcatenatingMediaSource(), /* resetPosition= */
            false);
            // Expect masking state is ended,
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).setMediaSources(/* mediaItemIndex= */
    0, /* positionMs= */
    C.TIME_UNSET, firstMediaSource).waitForPlaybackState(Player.STATE_READY).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);
            // Expect masking state is ended,
            maskingPlaybackStates[1] = player.getPlaybackState();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).setMediaSources(/* mediaItemIndex= */
    0, /* positionMs= */
    C.TIME_UNSET, firstMediaSource).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an explicit seek.
            player.setMediaSource(secondMediaSource, /* startPositionMs= */
            C.TIME_UNSET);
            // Expect masking state is buffering,
            maskingPlaybackStates[2] = player.getPlaybackState();
        }
    }).waitForPlaybackState(Player.STATE_READY).setMediaSources(/* mediaItemIndex= */
    0, /* positionMs= */
    C.TIME_UNSET, firstMediaSource).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an implicit seek to the current position.
            player.setMediaSource(secondMediaSource, /* resetPosition= */
            false);
            // Expect masking state is buffering,
            maskingPlaybackStates[3] = player.getPlaybackState();
        }
    }).play().waitForPlaybackState(Player.STATE_READY).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).setExpectedPlayerEndedCount(/* expectedPlayerEndedCount= */
    3).setMediaSources(firstMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    // Expect reset of masking to first media item.
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_BUFFERING, // Ready after initial prepare.
    Player.STATE_READY, // Ended after setting empty source without seek.
    Player.STATE_ENDED, Player.STATE_BUFFERING, // Ready again after re-setting source.
    Player.STATE_READY, // Ended after setting empty source with seek.
    Player.STATE_ENDED, Player.STATE_BUFFERING, // Ready again after re-setting source.
    Player.STATE_READY, Player.STATE_BUFFERING, // Ready after setting media item with seek.
    Player.STATE_READY, Player.STATE_BUFFERING, // Ready again after re-setting source.
    Player.STATE_READY, // Play.
    Player.STATE_BUFFERING, // Ready after setting media item without seek.
    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, // Initial source.
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, // Empty source.
    Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Reset source.
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, // Empty source.
    Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Reset source.
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Set source with seek.
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Reset source.
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Set source without seek.
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
}
Also used : 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) 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) 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)

Example 33 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ExoPlayerTest method shortAdFollowedByUnpreparedAd_playbackDoesNotGetStuck.

@Test
public void shortAdFollowedByUnpreparedAd_playbackDoesNotGetStuck() throws Exception {
    AdPlaybackState adPlaybackState = FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
    2, /* adGroupTimesUs...= */
    0);
    long shortAdDurationMs = 1_000;
    adPlaybackState = adPlaybackState.withAdDurationsUs(new long[][] { { shortAdDurationMs, shortAdDurationMs } });
    Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    Util.msToUs(10000), adPlaybackState));
    // Simulate the second ad not being prepared.
    FakeMediaSource mediaSource = new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT) {

        @Override
        protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
            return new FakeMediaPeriod(trackGroupArray, allocator, FakeMediaPeriod.TrackDataFactory.singleSampleWithTimeUs(0), mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
            id.adIndexInAdGroup == 1);
        }
    };
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    player.setMediaSource(mediaSource);
    player.prepare();
    player.play();
    // The player is not stuck in the buffering state.
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Allocator(com.google.android.exoplayer2.upstream.Allocator) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) FakeMediaPeriod(com.google.android.exoplayer2.testutil.FakeMediaPeriod) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 34 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ExoPlayerTest method modifyPlaylistUnprepared_remainsInIdle_needsPrepareForBuffering.

@Test
public void modifyPlaylistUnprepared_remainsInIdle_needsPrepareForBuffering() throws Exception {
    int[] playbackStates = new int[4];
    int[] timelineWindowCounts = new int[4];
    int[] maskingPlaybackState = { C.INDEX_UNSET };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForTimelineChanged(placeholderTimeline, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED).executeRunnable(new PlaybackStateCollector(/* index= */
    0, playbackStates, timelineWindowCounts)).clearMediaItems().executeRunnable(new PlaybackStateCollector(/* index= */
    1, playbackStates, timelineWindowCounts)).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            player.setMediaSource(new FakeMediaSource(), /* startPositionMs= */
            1000);
            maskingPlaybackState[0] = player.getPlaybackState();
        }
    }).executeRunnable(new PlaybackStateCollector(/* index= */
    2, playbackStates, timelineWindowCounts)).addMediaSources(new FakeMediaSource()).executeRunnable(new PlaybackStateCollector(/* index= */
    3, playbackStates, timelineWindowCounts)).seek(/* mediaItemIndex= */
    1, /* positionMs= */
    2000).prepare().waitForPlaybackState(Player.STATE_BUFFERING).waitForPlaybackState(Player.STATE_READY).waitForPlaybackState(Player.STATE_ENDED).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(new FakeMediaSource()).setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertArrayEquals(new int[] { Player.STATE_IDLE, Player.STATE_IDLE, Player.STATE_IDLE, Player.STATE_IDLE }, playbackStates);
    assertArrayEquals(new int[] { 1, 0, 1, 2 }, timelineWindowCounts);
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_BUFFERING, /* first buffering state after prepare */
    Player.STATE_READY, Player.STATE_ENDED);
    exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, /* initial setMediaSources */
    Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, /* clear */
    Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, /* set media items */
    Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, /* add media items */
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
    Timeline expectedSecondPlaceholderTimeline = new FakeTimeline(TimelineWindowDefinition.createPlaceholder(/* tag= */
    0), TimelineWindowDefinition.createPlaceholder(/* tag= */
    0));
    Timeline expectedSecondRealTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    10_000_000), new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    10_000_000));
    exoPlayerTestRunner.assertTimelinesSame(placeholderTimeline, Timeline.EMPTY, placeholderTimeline, expectedSecondPlaceholderTimeline, expectedSecondRealTimeline);
    assertArrayEquals(new int[] { Player.STATE_IDLE }, maskingPlaybackState);
}
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) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 35 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class SubtitleExtractor method init.

@Override
public void init(ExtractorOutput output) {
    checkState(state == STATE_CREATED);
    extractorOutput = output;
    trackOutput = extractorOutput.track(/* id= */
    0, C.TRACK_TYPE_TEXT);
    extractorOutput.endTracks();
    extractorOutput.seekMap(new IndexSeekMap(/* positions= */
    new long[] { 0 }, /* timesUs= */
    new long[] { 0 }, /* durationUs= */
    C.TIME_UNSET));
    trackOutput.format(format);
    state = STATE_INITIALIZED;
}
Also used : IndexSeekMap(com.google.android.exoplayer2.extractor.IndexSeekMap)

Aggregations

Test (org.junit.Test)26 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)10 Nullable (androidx.annotation.Nullable)9 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)9 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)9 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)8 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)8 SuppressLint (android.annotation.SuppressLint)7 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)7 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)7 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)7 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)6 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)6 ArrayList (java.util.ArrayList)6 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)5 Listener (com.google.android.exoplayer2.Player.Listener)4 MediaSource (com.google.android.exoplayer2.source.MediaSource)4 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)4 IOException (java.io.IOException)4 Pair (android.util.Pair)3