Search in sources :

Example 36 with ExoPlayer

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

the class ExoPlayerTest method mediaItemOfSources_correctInTimelineWindows.

@Test
public void mediaItemOfSources_correctInTimelineWindows() throws Exception {
    TimelineWindowDefinition window1 = new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    1, /* isSeekable= */
    true, /* isDynamic= */
    false, /* isLive= */
    false, /* isPlaceholder= */
    false, /* durationUs = */
    100_000, /* defaultPositionUs = */
    0, /* windowOffsetInFirstPeriodUs= */
    0, ImmutableList.of(AdPlaybackState.NONE), MediaItem.fromUri("http://foo.bar/fake1"));
    FakeMediaSource fakeMediaSource1 = new FakeMediaSource(new FakeTimeline(window1));
    TimelineWindowDefinition window2 = new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    2, /* isSeekable= */
    true, /* isDynamic= */
    false, /* isLive= */
    false, /* isPlaceholder= */
    false, /* durationUs = */
    100_000, /* defaultPositionUs = */
    0, /* windowOffsetInFirstPeriodUs= */
    0, ImmutableList.of(AdPlaybackState.NONE), MediaItem.fromUri("http://foo.bar/fake2"));
    FakeMediaSource fakeMediaSource2 = new FakeMediaSource(new FakeTimeline(window2));
    TimelineWindowDefinition window3 = new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    3, /* isSeekable= */
    true, /* isDynamic= */
    false, /* isLive= */
    false, /* isPlaceholder= */
    false, /* durationUs = */
    100_000, /* defaultPositionUs = */
    0, /* windowOffsetInFirstPeriodUs= */
    0, ImmutableList.of(AdPlaybackState.NONE), MediaItem.fromUri("http://foo.bar/fake3"));
    FakeMediaSource fakeMediaSource3 = new FakeMediaSource(new FakeTimeline(window3));
    final Player[] playerHolder = { null };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerHolder[0] = player;
        }
    }).waitForPlaybackState(Player.STATE_READY).seek(/* positionMs= */
    0).play().build();
    List<MediaItem> currentMediaItems = new ArrayList<>();
    List<MediaItem> mediaItemsInTimeline = new ArrayList<>();
    Player.Listener playerListener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            if (reason != Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED) {
                return;
            }
            Window window = new Window();
            for (int i = 0; i < timeline.getWindowCount(); i++) {
                mediaItemsInTimeline.add(timeline.getWindow(i, window).mediaItem);
            }
        }

        @Override
        public void onPositionDiscontinuity(int reason) {
            if (reason == Player.DISCONTINUITY_REASON_SEEK || reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
                currentMediaItems.add(playerHolder[0].getCurrentMediaItem());
            }
        }
    };
    new ExoPlayerTestRunner.Builder(context).setPlayerListener(playerListener).setActionSchedule(actionSchedule).setMediaSources(fakeMediaSource1, fakeMediaSource2, fakeMediaSource3).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertThat(currentMediaItems.get(0).localConfiguration.uri.toString()).isEqualTo("http://foo.bar/fake1");
    assertThat(currentMediaItems.get(1).localConfiguration.uri.toString()).isEqualTo("http://foo.bar/fake2");
    assertThat(currentMediaItems.get(2).localConfiguration.uri.toString()).isEqualTo("http://foo.bar/fake3");
    assertThat(mediaItemsInTimeline).containsExactlyElementsIn(currentMediaItems);
}
Also used : Window(com.google.android.exoplayer2.Timeline.Window) 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) 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) ArrayList(java.util.ArrayList) Listener(com.google.android.exoplayer2.Player.Listener) 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) TestPlayerRunHelper.playUntilStartOfMediaItem(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 37 with ExoPlayer

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

the class ExoPlayerTest method setShuffleMode_correctPositionMasking.

@Test
public void setShuffleMode_correctPositionMasking() throws Exception {
    long[] currentPositionMs = new long[] { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    long[] bufferedPositionMs = new long[] { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).playUntilPosition(0, 5000).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentPositionMs[0] = player.getCurrentPosition();
            bufferedPositionMs[0] = player.getBufferedPosition();
            player.setShuffleModeEnabled(true);
            currentPositionMs[1] = player.getCurrentPosition();
            bufferedPositionMs[1] = player.getBufferedPosition();
            player.setShuffleModeEnabled(false);
            currentPositionMs[2] = player.getCurrentPosition();
            bufferedPositionMs[2] = player.getBufferedPosition();
        }
    }).play().build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(currentPositionMs[0]).isAtLeast(5000);
    assertThat(currentPositionMs[1]).isEqualTo(currentPositionMs[0]);
    assertThat(currentPositionMs[2]).isEqualTo(currentPositionMs[0]);
    assertThat(bufferedPositionMs[0]).isGreaterThan(currentPositionMs[0]);
    assertThat(bufferedPositionMs[1]).isEqualTo(bufferedPositionMs[0]);
    assertThat(bufferedPositionMs[2]).isEqualTo(bufferedPositionMs[0]);
}
Also used : ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 38 with ExoPlayer

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

the class ExoPlayerTest method setMediaSources_whenEnded_noSeek_correctMaskingPlaybackState.

@Test
public void setMediaSources_whenEnded_noSeek_correctMaskingPlaybackState() throws Exception {
    final int[] maskingPlaybackStates = new int[1];
    Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_READY).clearMediaItems().waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with no seek (keep current position).
            player.setMediaSource(new FakeMediaSource(), /* resetPosition= */
            false);
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).waitForTimelineChanged().waitForPlaybackState(Player.STATE_ENDED).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().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_ENDED }, 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) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 39 with ExoPlayer

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

the class ExoPlayerTest method clearMediaItems_unprepared_correctMaskingMediaItemIndex_notEnded.

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

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
            currentStates[0] = player.getPlaybackState();
            player.clearMediaItems();
            currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
            currentStates[1] = player.getPlaybackState();
        }
    }).prepare().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Transitions to ended when prepared with zero media items.
            currentStates[2] = player.getPlaybackState();
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
    1, /* positionMs= */
    C.TIME_UNSET).setMediaSources(new FakeMediaSource(), 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_ENDED }, currentStates);
    assertArrayEquals(new int[] { 1, 0 }, currentMediaItemIndices);
}
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 40 with ExoPlayer

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

the class ServerSideAdInsertionMediaSourceTest method playbackWithNewlyInsertedAds_playsSuccessfulWithoutRendererResets.

@Test
public void playbackWithNewlyInsertedAds_playsSuccessfulWithoutRendererResets() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    AtomicReference<Object> periodUid = new AtomicReference<>();
    CapturingRenderersFactory renderersFactory = new CapturingRenderersFactory(context);
    ExoPlayer player = new ExoPlayer.Builder(context, renderersFactory).setClock(new FakeClock(/* isAutoAdvancing= */
    true)).build();
    player.setVideoSurface(new Surface(new SurfaceTexture(/* texName= */
    1)));
    PlaybackOutput playbackOutput = PlaybackOutput.register(player, renderersFactory);
    AdPlaybackState firstAdPlaybackState = addAdGroupToAdPlaybackState(new AdPlaybackState(/* adsId= */
    new Object()), /* fromPositionUs= */
    900_000, /* contentResumeOffsetUs= */
    0, /* adDurationsUs...= */
    100_000);
    AtomicReference<ServerSideAdInsertionMediaSource> mediaSourceRef = new AtomicReference<>();
    mediaSourceRef.set(new ServerSideAdInsertionMediaSource(new DefaultMediaSourceFactory(context).createMediaSource(MediaItem.fromUri(TEST_ASSET)), /* adPlaybackStateUpdater= */
    contentTimeline -> {
        periodUid.set(checkNotNull(contentTimeline.getPeriod(/* periodIndex= */
        0, new Timeline.Period(), /* setIds= */
        true).uid));
        mediaSourceRef.get().setAdPlaybackStates(ImmutableMap.of(periodUid.get(), firstAdPlaybackState));
        return true;
    }));
    AnalyticsListener listener = mock(AnalyticsListener.class);
    player.addAnalyticsListener(listener);
    player.setMediaSource(mediaSourceRef.get());
    player.prepare();
    // Add ad at the current playback position during playback.
    runUntilPlaybackState(player, Player.STATE_READY);
    AdPlaybackState secondAdPlaybackState = addAdGroupToAdPlaybackState(firstAdPlaybackState, /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    0, /* adDurationsUs...= */
    500_000);
    mediaSourceRef.get().setAdPlaybackStates(ImmutableMap.of(periodUid.get(), secondAdPlaybackState));
    runUntilPendingCommandsAreFullyHandled(player);
    player.play();
    runUntilPlaybackState(player, Player.STATE_ENDED);
    player.release();
    // Assert all samples have been played.
    DumpFileAsserts.assertOutput(context, playbackOutput, TEST_ASSET_DUMP);
    // Assert playback has been reported with ads: [content][ad0][content][ad1][content]
    // 5*2(audio+video) format changes, 4 discontinuities between parts.
    verify(listener, times(4)).onPositionDiscontinuity(any(), any(), any(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    verify(listener, times(10)).onDownstreamFormatChanged(any(), any());
    // Assert renderers played through without reset (=decoders have been enabled only once).
    verify(listener).onVideoEnabled(any(), any());
    verify(listener).onAudioEnabled(any(), any());
    // Assert playback progression was smooth (=no unexpected delays that cause audio to underrun)
    verify(listener, never()).onAudioUnderrun(any(), anyInt(), anyLong(), anyLong());
}
Also used : Context(android.content.Context) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) Context(android.content.Context) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Pair(android.util.Pair) RunWith(org.junit.runner.RunWith) Player(com.google.android.exoplayer2.Player) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) TestPlayerRunHelper.playUntilPosition(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) PlayerId(com.google.android.exoplayer2.analytics.PlayerId) ShadowMediaCodecConfig(com.google.android.exoplayer2.robolectric.ShadowMediaCodecConfig) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) RobolectricUtil.runMainLooperUntil(com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil) SurfaceTexture(android.graphics.SurfaceTexture) MediaItem(com.google.android.exoplayer2.MediaItem) TestPlayerRunHelper.runUntilPlaybackState(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState) Surface(android.view.Surface) ImmutableMap(com.google.common.collect.ImmutableMap) CapturingRenderersFactory(com.google.android.exoplayer2.testutil.CapturingRenderersFactory) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Mockito.never(org.mockito.Mockito.never) Timeline(com.google.android.exoplayer2.Timeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Rule(org.junit.Rule) DumpFileAsserts(com.google.android.exoplayer2.testutil.DumpFileAsserts) PlaybackOutput(com.google.android.exoplayer2.robolectric.PlaybackOutput) Assert(org.junit.Assert) Assertions.checkNotNull(com.google.android.exoplayer2.util.Assertions.checkNotNull) Mockito.mock(org.mockito.Mockito.mock) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) CapturingRenderersFactory(com.google.android.exoplayer2.testutil.CapturingRenderersFactory) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Surface(android.view.Surface) PlaybackOutput(com.google.android.exoplayer2.robolectric.PlaybackOutput) Timeline(com.google.android.exoplayer2.Timeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) SurfaceTexture(android.graphics.SurfaceTexture) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)248 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)179 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)172 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)121 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)108 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)92 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)89 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)89 Listener (com.google.android.exoplayer2.Player.Listener)85 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)73 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)67 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)65 MediaSource (com.google.android.exoplayer2.source.MediaSource)55 FakeClock (com.google.android.exoplayer2.testutil.FakeClock)48 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)47 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)47 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)47 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)47 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)47 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)44