Search in sources :

Example 16 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState in project ExoPlayer by google.

the class ExoPlayerTest method seekForward_pastDuration_seeksToDuration.

@Test
public void seekForward_pastDuration_seeksToDuration() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Timeline fakeTimeline = new FakeTimeline(new TimelineWindowDefinition(/* isSeekable= */
    true, /* isDynamic= */
    true, /* durationUs= */
    Util.msToUs(C.DEFAULT_SEEK_FORWARD_INCREMENT_MS / 2)));
    player.setMediaSource(new FakeMediaSource(fakeTimeline));
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    player.seekForward();
    assertThat(player.getCurrentPosition()).isEqualTo(C.DEFAULT_SEEK_FORWARD_INCREMENT_MS / 2 - 1);
    player.release();
}
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) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 17 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState in project ExoPlayer by google.

the class DefaultAnalyticsCollectorTest method release_withCallbacksArrivingAfterRelease_onPlayerReleasedForwardedLast.

@Test
public void release_withCallbacksArrivingAfterRelease_onPlayerReleasedForwardedLast() throws Exception {
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0, /* isAutoAdvancing= */
    true);
    ExoPlayer exoPlayer = new TestExoPlayerBuilder(ApplicationProvider.getApplicationContext()).setClock(fakeClock).build();
    AnalyticsListener analyticsListener = spy(new AnalyticsListener() {

        @Override
        public void onVideoDisabled(EventTime eventTime, DecoderCounters decoderCounters) {
            // Add delay in callback to test whether event timestamp and release timestamp are
            // in the correct order.
            fakeClock.advanceTime(1);
        }
    });
    exoPlayer.addAnalyticsListener(analyticsListener);
    // Prepare with media to ensure video renderer is enabled.
    exoPlayer.setMediaSource(new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT));
    exoPlayer.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(exoPlayer, Player.STATE_READY);
    // Release and add delay on releasing thread to verify timestamps of events.
    exoPlayer.release();
    long releaseTimeMs = fakeClock.currentTimeMillis();
    fakeClock.advanceTime(1);
    ShadowLooper.idleMainLooper();
    // Verify video disable events and release events arrived in order.
    ArgumentCaptor<AnalyticsListener.EventTime> videoDisabledEventTime = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    ArgumentCaptor<AnalyticsListener.EventTime> releasedEventTime = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    InOrder inOrder = inOrder(analyticsListener);
    inOrder.verify(analyticsListener).onVideoDisabled(videoDisabledEventTime.capture(), any());
    inOrder.verify(analyticsListener).onEvents(same(exoPlayer), argThat(events -> events.contains(EVENT_VIDEO_DISABLED)));
    inOrder.verify(analyticsListener).onPlayerReleased(releasedEventTime.capture());
    inOrder.verify(analyticsListener).onEvents(same(exoPlayer), argThat(events -> events.contains(EVENT_PLAYER_RELEASED)));
    // Verify order of timestamps of these events.
    // This verification is needed as a regression test against [internal ref: b/195396384]. The
    // root cause of the regression was an onPlayerReleased timestamp that was less than the
    // previously reported timestamps for other events triggered as part of the release.
    long videoDisableTimeMs = videoDisabledEventTime.getValue().realtimeMs;
    assertThat(videoDisableTimeMs).isGreaterThan(releaseTimeMs);
    assertThat(releasedEventTime.getValue().realtimeMs).isGreaterThan(videoDisableTimeMs);
}
Also used : ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) LoadEventInfo(com.google.android.exoplayer2.source.LoadEventInfo) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) EVENT_TIMELINE_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_TIMELINE_CHANGED) ExoMediaDrm(com.google.android.exoplayer2.drm.ExoMediaDrm) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) EVENT_RENDERED_FIRST_FRAME(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_RENDERED_FIRST_FRAME) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) MimeTypes(com.google.android.exoplayer2.util.MimeTypes) Looper(android.os.Looper) EVENT_AUDIO_DECODER_INITIALIZED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_DECODER_INITIALIZED) C(com.google.android.exoplayer2.C) EVENT_LOAD_STARTED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_LOAD_STARTED) DefaultDrmSessionManager(com.google.android.exoplayer2.drm.DefaultDrmSessionManager) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) Surface(android.view.Surface) EVENT_AUDIO_ENABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_ENABLED) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) FakeAudioRenderer(com.google.android.exoplayer2.testutil.FakeAudioRenderer) Nullable(androidx.annotation.Nullable) Timeline(com.google.android.exoplayer2.Timeline) EVENT_PLAYER_ERROR(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYER_ERROR) RobolectricUtil(com.google.android.exoplayer2.robolectric.RobolectricUtil) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) EVENT_POSITION_DISCONTINUITY(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_POSITION_DISCONTINUITY) EVENT_VIDEO_INPUT_FORMAT_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_INPUT_FORMAT_CHANGED) EVENT_IS_PLAYING_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_IS_PLAYING_CHANGED) Mockito.mock(org.mockito.Mockito.mock) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) RunWith(org.junit.runner.RunWith) PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) EVENT_PLAYBACK_PARAMETERS_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYBACK_PARAMETERS_CHANGED) TestUtil(com.google.android.exoplayer2.testutil.TestUtil) EVENT_VIDEO_SIZE_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_SIZE_CHANGED) ArrayList(java.util.ArrayList) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) EVENT_VIDEO_FRAME_PROCESSING_OFFSET(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_FRAME_PROCESSING_OFFSET) MediaDrmCallbackException(com.google.android.exoplayer2.drm.MediaDrmCallbackException) EVENT_DRM_SESSION_ACQUIRED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_SESSION_ACQUIRED) Clock(com.google.android.exoplayer2.util.Clock) ShadowLooper(org.robolectric.shadows.ShadowLooper) IOException(java.io.IOException) Test(org.junit.Test) EVENT_VIDEO_DISABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_DISABLED) TestPlayerRunHelper(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper) TracksInfo(com.google.android.exoplayer2.TracksInfo) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) DrmInitData(com.google.android.exoplayer2.drm.DrmInitData) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Util(com.google.android.exoplayer2.util.Util) EVENT_VIDEO_ENABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_ENABLED) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) Player(com.google.android.exoplayer2.Player) EVENT_VIDEO_DECODER_INITIALIZED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_VIDEO_DECODER_INITIALIZED) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) ApplicationProvider(androidx.test.core.app.ApplicationProvider) Format(com.google.android.exoplayer2.Format) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EVENT_IS_LOADING_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_IS_LOADING_CHANGED) Method(java.lang.reflect.Method) MediaSource(com.google.android.exoplayer2.source.MediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) EVENT_PLAYER_RELEASED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYER_RELEASED) RenderersFactory(com.google.android.exoplayer2.RenderersFactory) EVENT_PLAYBACK_STATE_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAYBACK_STATE_CHANGED) MediaItem(com.google.android.exoplayer2.MediaItem) EVENT_DRM_KEYS_LOADED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_KEYS_LOADED) END_OF_STREAM_ITEM(com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) EVENT_TRACKS_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_TRACKS_CHANGED) UUID(java.util.UUID) EVENT_DRM_SESSION_MANAGER_ERROR(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_SESSION_MANAGER_ERROR) SparseArray(android.util.SparseArray) FakeSampleStreamItem.oneByteSample(com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.oneByteSample) DrmSession(com.google.android.exoplayer2.drm.DrmSession) List(java.util.List) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Mockito.inOrder(org.mockito.Mockito.inOrder) EVENT_DRM_SESSION_RELEASED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DRM_SESSION_RELEASED) PlaybackException(com.google.android.exoplayer2.PlaybackException) MediaLoadData(com.google.android.exoplayer2.source.MediaLoadData) FakeVideoRenderer(com.google.android.exoplayer2.testutil.FakeVideoRenderer) EVENT_AUDIO_POSITION_ADVANCING(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_POSITION_ADVANCING) Metadata(com.google.android.exoplayer2.metadata.Metadata) EVENT_LOAD_COMPLETED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_LOAD_COMPLETED) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DecoderCounters(com.google.android.exoplayer2.decoder.DecoderCounters) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) AtomicReference(java.util.concurrent.atomic.AtomicReference) EVENT_LOAD_ERROR(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_LOAD_ERROR) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) FakeRenderer(com.google.android.exoplayer2.testutil.FakeRenderer) ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) VideoSize(com.google.android.exoplayer2.video.VideoSize) SurfaceTexture(android.graphics.SurfaceTexture) Window(com.google.android.exoplayer2.Timeline.Window) EVENT_MEDIA_ITEM_TRANSITION(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_MEDIA_ITEM_TRANSITION) MediaDrmCallback(com.google.android.exoplayer2.drm.MediaDrmCallback) InOrder(org.mockito.InOrder) Iterator(java.util.Iterator) EVENT_AUDIO_DISABLED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_DISABLED) EVENT_DOWNSTREAM_FORMAT_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DOWNSTREAM_FORMAT_CHANGED) Truth.assertThat(com.google.common.truth.Truth.assertThat) EVENT_DROPPED_VIDEO_FRAMES(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_DROPPED_VIDEO_FRAMES) Mockito.verify(org.mockito.Mockito.verify) EVENT_PLAY_WHEN_READY_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_PLAY_WHEN_READY_CHANGED) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeExoMediaDrm(com.google.android.exoplayer2.testutil.FakeExoMediaDrm) EVENT_AUDIO_INPUT_FORMAT_CHANGED(com.google.android.exoplayer2.analytics.AnalyticsListener.EVENT_AUDIO_INPUT_FORMAT_CHANGED) Renderer(com.google.android.exoplayer2.Renderer) InOrder(org.mockito.InOrder) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) DecoderCounters(com.google.android.exoplayer2.decoder.DecoderCounters) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 18 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState in project ExoPlayer by google.

the class ExoPlayerTest method play_withPreMidAndPostRollAd_callsOnDiscontinuityCorrectly.

@Test
public void play_withPreMidAndPostRollAd_callsOnDiscontinuityCorrectly() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    player.addListener(listener);
    AdPlaybackState adPlaybackState = FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
    2, /* adGroupTimesUs...= */
    0, 7 * C.MICROS_PER_SECOND, C.TIME_END_OF_SOURCE);
    TimelineWindowDefinition adTimeline = new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    false, /* isLive= */
    false, /* isPlaceholder= */
    false, /* durationUs= */
    10 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    0, /* windowOffsetInFirstPeriodUs= */
    0, adPlaybackState);
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(adTimeline)));
    player.prepare();
    player.play();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
    ArgumentCaptor<Player.PositionInfo> oldPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
    ArgumentCaptor<Player.PositionInfo> newPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
    verify(listener, never()).onPositionDiscontinuity(any(), any(), not(eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION)));
    verify(listener, times(8)).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    // first ad group (pre-roll)
    // starts with ad to ad transition
    List<Player.PositionInfo> oldPositions = oldPosition.getAllValues();
    List<Player.PositionInfo> newPositions = newPosition.getAllValues();
    assertThat(oldPositions.get(0).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(0).positionMs).isEqualTo(5000);
    assertThat(oldPositions.get(0).contentPositionMs).isEqualTo(0);
    assertThat(oldPositions.get(0).adGroupIndex).isEqualTo(0);
    assertThat(oldPositions.get(0).adIndexInAdGroup).isEqualTo(0);
    assertThat(newPositions.get(0).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(0).positionMs).isEqualTo(0);
    assertThat(newPositions.get(0).contentPositionMs).isEqualTo(0);
    assertThat(newPositions.get(0).adGroupIndex).isEqualTo(0);
    assertThat(newPositions.get(0).adIndexInAdGroup).isEqualTo(1);
    // ad to content transition
    assertThat(oldPositions.get(1).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(1).positionMs).isEqualTo(5000);
    assertThat(oldPositions.get(1).contentPositionMs).isEqualTo(0);
    assertThat(oldPositions.get(1).adGroupIndex).isEqualTo(0);
    assertThat(oldPositions.get(1).adIndexInAdGroup).isEqualTo(1);
    assertThat(newPositions.get(1).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(1).positionMs).isEqualTo(0);
    assertThat(newPositions.get(1).contentPositionMs).isEqualTo(0);
    assertThat(newPositions.get(1).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(1).adIndexInAdGroup).isEqualTo(-1);
    // second add group (mid-roll)
    assertThat(oldPositions.get(2).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(2).positionMs).isEqualTo(7000);
    assertThat(oldPositions.get(2).contentPositionMs).isEqualTo(7000);
    assertThat(oldPositions.get(2).adGroupIndex).isEqualTo(-1);
    assertThat(oldPositions.get(2).adIndexInAdGroup).isEqualTo(-1);
    assertThat(newPositions.get(2).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(2).positionMs).isEqualTo(0);
    assertThat(newPositions.get(2).contentPositionMs).isEqualTo(7000);
    assertThat(newPositions.get(2).adGroupIndex).isEqualTo(1);
    assertThat(newPositions.get(2).adIndexInAdGroup).isEqualTo(0);
    // ad to ad transition
    assertThat(oldPositions.get(3).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(3).positionMs).isEqualTo(5000);
    assertThat(oldPositions.get(3).contentPositionMs).isEqualTo(7000);
    assertThat(oldPositions.get(3).adGroupIndex).isEqualTo(1);
    assertThat(oldPositions.get(3).adIndexInAdGroup).isEqualTo(0);
    assertThat(newPositions.get(3).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(3).positionMs).isEqualTo(0);
    assertThat(newPositions.get(3).contentPositionMs).isEqualTo(7000);
    assertThat(newPositions.get(3).adGroupIndex).isEqualTo(1);
    assertThat(newPositions.get(3).adIndexInAdGroup).isEqualTo(1);
    // ad to content transition
    assertThat(oldPositions.get(4).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(4).positionMs).isEqualTo(5000);
    assertThat(oldPositions.get(4).contentPositionMs).isEqualTo(7000);
    assertThat(oldPositions.get(4).adGroupIndex).isEqualTo(1);
    assertThat(oldPositions.get(4).adIndexInAdGroup).isEqualTo(1);
    assertThat(newPositions.get(4).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(4).positionMs).isEqualTo(7000);
    assertThat(newPositions.get(4).contentPositionMs).isEqualTo(7000);
    assertThat(newPositions.get(4).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(4).adIndexInAdGroup).isEqualTo(-1);
    // third add group (post-roll)
    assertThat(oldPositions.get(5).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(5).positionMs).isEqualTo(10000);
    assertThat(oldPositions.get(5).contentPositionMs).isEqualTo(10000);
    assertThat(oldPositions.get(5).adGroupIndex).isEqualTo(-1);
    assertThat(oldPositions.get(5).adIndexInAdGroup).isEqualTo(-1);
    assertThat(newPositions.get(5).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(5).positionMs).isEqualTo(0);
    assertThat(newPositions.get(5).contentPositionMs).isEqualTo(10000);
    assertThat(newPositions.get(5).adGroupIndex).isEqualTo(2);
    assertThat(newPositions.get(5).adIndexInAdGroup).isEqualTo(0);
    // ad to ad transition
    assertThat(oldPositions.get(6).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(6).positionMs).isEqualTo(5000);
    assertThat(oldPositions.get(6).contentPositionMs).isEqualTo(10000);
    assertThat(oldPositions.get(6).adGroupIndex).isEqualTo(2);
    assertThat(oldPositions.get(6).adIndexInAdGroup).isEqualTo(0);
    assertThat(newPositions.get(6).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(6).positionMs).isEqualTo(0);
    assertThat(newPositions.get(6).contentPositionMs).isEqualTo(10000);
    assertThat(newPositions.get(6).adGroupIndex).isEqualTo(2);
    assertThat(newPositions.get(6).adIndexInAdGroup).isEqualTo(1);
    // post roll ad to end of content transition
    assertThat(oldPositions.get(7).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(7).positionMs).isEqualTo(5000);
    assertThat(oldPositions.get(7).contentPositionMs).isEqualTo(10000);
    assertThat(oldPositions.get(7).adGroupIndex).isEqualTo(2);
    assertThat(oldPositions.get(7).adIndexInAdGroup).isEqualTo(1);
    assertThat(newPositions.get(7).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(7).positionMs).isEqualTo(9999);
    assertThat(newPositions.get(7).contentPositionMs).isEqualTo(9999);
    assertThat(newPositions.get(7).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(7).adIndexInAdGroup).isEqualTo(-1);
    player.release();
}
Also used : Listener(com.google.android.exoplayer2.Player.Listener) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) 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) PositionInfo(com.google.android.exoplayer2.Player.PositionInfo) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 19 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState in project ExoPlayer by google.

the class ExoPlayerTest method targetLiveOffsetInMedia_afterAutomaticPeriodTransition_adjustsLiveOffsetToTargetOffset.

@Test
public void targetLiveOffsetInMedia_afterAutomaticPeriodTransition_adjustsLiveOffsetToTargetOffset() throws Exception {
    long windowStartUnixTimeMs = 987_654_321_000L;
    long nowUnixTimeMs = windowStartUnixTimeMs + 10_000;
    ExoPlayer player = new TestExoPlayerBuilder(context).setClock(new FakeClock(/* initialTimeMs= */
    nowUnixTimeMs, /* isAutoAdvancing= */
    true)).build();
    Timeline nonLiveTimeline = new FakeTimeline();
    Timeline liveTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    true, /* isLive= */
    true, /* isPlaceholder= */
    false, /* durationUs= */
    1000 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    8 * C.MICROS_PER_SECOND, /* windowOffsetInFirstPeriodUs= */
    Util.msToUs(windowStartUnixTimeMs), ImmutableList.of(AdPlaybackState.NONE), new MediaItem.Builder().setUri(Uri.EMPTY).setLiveConfiguration(new MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(9_000).build()).build()));
    player.pause();
    player.addMediaSource(new FakeMediaSource(nonLiveTimeline));
    player.addMediaSource(new FakeMediaSource(liveTimeline));
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    // Play until close to the end of the available live window.
    TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
    1, /* positionMs= */
    999_000);
    long liveOffsetAtEnd = player.getCurrentLiveOffset();
    player.release();
    // Assert that player adjusted live offset to the media value.
    assertThat(liveOffsetAtEnd).isIn(Range.closed(8_900L, 9_100L));
}
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) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestPlayerRunHelper.playUntilStartOfMediaItem(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 20 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState in project ExoPlayer by google.

the class ExoPlayerTest method play_multiItemPlaylistWidthAds_callsOnDiscontinuityCorrectly.

@Test
public void play_multiItemPlaylistWidthAds_callsOnDiscontinuityCorrectly() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    player.addListener(listener);
    AdPlaybackState postRollAdPlaybackState = FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
    1, /* adGroupTimesUs...= */
    C.TIME_END_OF_SOURCE);
    TimelineWindowDefinition postRollWindow = new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    "id-2", /* isSeekable= */
    true, /* isDynamic= */
    false, /* isLive= */
    false, /* isPlaceholder= */
    false, /* durationUs= */
    20 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    0, /* windowOffsetInFirstPeriodUs= */
    0, postRollAdPlaybackState);
    AdPlaybackState preRollAdPlaybackState = FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
    1, /* adGroupTimesUs...= */
    0);
    TimelineWindowDefinition preRollWindow = new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    "id-3", /* isSeekable= */
    true, /* isDynamic= */
    false, /* isLive= */
    false, /* isPlaceholder= */
    false, /* durationUs= */
    25 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    0, /* windowOffsetInFirstPeriodUs= */
    0, preRollAdPlaybackState);
    player.setMediaSources(ImmutableList.of(createFakeMediaSource(/* id= */
    "id-0"), new FakeMediaSource(new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    "id-1", /* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    15 * C.MICROS_PER_SECOND))), new FakeMediaSource(new FakeTimeline(postRollWindow)), new FakeMediaSource(new FakeTimeline(preRollWindow))));
    player.prepare();
    player.play();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
    ArgumentCaptor<Player.PositionInfo> oldPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
    ArgumentCaptor<Player.PositionInfo> newPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
    Window window = new Window();
    InOrder inOrder = Mockito.inOrder(listener);
    // from first to second media item
    inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    inOrder.verify(listener).onMediaItemTransition(any(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_AUTO));
    assertThat(oldPosition.getValue().windowUid).isEqualTo(player.getCurrentTimeline().getWindow(0, window).uid);
    assertThat(oldPosition.getValue().mediaItemIndex).isEqualTo(0);
    assertThat(oldPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-0");
    assertThat(oldPosition.getValue().positionMs).isEqualTo(10_000);
    assertThat(oldPosition.getValue().contentPositionMs).isEqualTo(10_000);
    assertThat(oldPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(oldPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    assertThat(newPosition.getValue().windowUid).isEqualTo(player.getCurrentTimeline().getWindow(1, window).uid);
    assertThat(newPosition.getValue().mediaItemIndex).isEqualTo(1);
    assertThat(newPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-1");
    assertThat(newPosition.getValue().positionMs).isEqualTo(0);
    assertThat(newPosition.getValue().contentPositionMs).isEqualTo(0);
    assertThat(newPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(newPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    // from second media item to third
    inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    inOrder.verify(listener).onMediaItemTransition(any(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_AUTO));
    assertThat(oldPosition.getValue().windowUid).isEqualTo(player.getCurrentTimeline().getWindow(1, window).uid);
    assertThat(newPosition.getValue().windowUid).isEqualTo(player.getCurrentTimeline().getWindow(2, window).uid);
    assertThat(oldPosition.getValue().mediaItemIndex).isEqualTo(1);
    assertThat(oldPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-1");
    assertThat(oldPosition.getValue().positionMs).isEqualTo(15_000);
    assertThat(oldPosition.getValue().contentPositionMs).isEqualTo(15_000);
    assertThat(oldPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(oldPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    assertThat(newPosition.getValue().mediaItemIndex).isEqualTo(2);
    assertThat(newPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-2");
    assertThat(newPosition.getValue().positionMs).isEqualTo(0);
    assertThat(newPosition.getValue().contentPositionMs).isEqualTo(0);
    assertThat(newPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(newPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    // from third media item content to post roll ad
    @Nullable Object lastNewWindowUid = newPosition.getValue().windowUid;
    inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    assertThat(oldPosition.getValue().mediaItemIndex).isEqualTo(2);
    assertThat(oldPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-2");
    assertThat(oldPosition.getValue().windowUid).isEqualTo(lastNewWindowUid);
    assertThat(oldPosition.getValue().positionMs).isEqualTo(20_000);
    assertThat(oldPosition.getValue().contentPositionMs).isEqualTo(20_000);
    assertThat(oldPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(oldPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    assertThat(newPosition.getValue().mediaItemIndex).isEqualTo(2);
    assertThat(newPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-2");
    assertThat(newPosition.getValue().positionMs).isEqualTo(0);
    assertThat(newPosition.getValue().contentPositionMs).isEqualTo(20_000);
    assertThat(newPosition.getValue().adGroupIndex).isEqualTo(0);
    assertThat(newPosition.getValue().adIndexInAdGroup).isEqualTo(0);
    // from third media item post roll to third media item content end
    lastNewWindowUid = newPosition.getValue().windowUid;
    inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    assertThat(oldPosition.getValue().windowUid).isEqualTo(lastNewWindowUid);
    assertThat(oldPosition.getValue().mediaItemIndex).isEqualTo(2);
    assertThat(oldPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-2");
    assertThat(oldPosition.getValue().positionMs).isEqualTo(5_000);
    assertThat(oldPosition.getValue().contentPositionMs).isEqualTo(20_000);
    assertThat(oldPosition.getValue().adGroupIndex).isEqualTo(0);
    assertThat(oldPosition.getValue().adIndexInAdGroup).isEqualTo(0);
    assertThat(newPosition.getValue().windowUid).isEqualTo(oldPosition.getValue().windowUid);
    assertThat(newPosition.getValue().mediaItemIndex).isEqualTo(2);
    assertThat(newPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-2");
    assertThat(newPosition.getValue().positionMs).isEqualTo(19_999);
    assertThat(newPosition.getValue().contentPositionMs).isEqualTo(19_999);
    assertThat(newPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(newPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    // from third media item content end to fourth media item pre roll ad
    lastNewWindowUid = newPosition.getValue().windowUid;
    inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    inOrder.verify(listener).onMediaItemTransition(any(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_AUTO));
    assertThat(oldPosition.getValue().windowUid).isEqualTo(lastNewWindowUid);
    assertThat(oldPosition.getValue().mediaItemIndex).isEqualTo(2);
    assertThat(oldPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-2");
    assertThat(oldPosition.getValue().positionMs).isEqualTo(20_000);
    assertThat(oldPosition.getValue().contentPositionMs).isEqualTo(20_000);
    assertThat(oldPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(oldPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    assertThat(newPosition.getValue().windowUid).isNotEqualTo(oldPosition.getValue().windowUid);
    assertThat(newPosition.getValue().mediaItemIndex).isEqualTo(3);
    assertThat(newPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-3");
    assertThat(newPosition.getValue().positionMs).isEqualTo(0);
    assertThat(newPosition.getValue().contentPositionMs).isEqualTo(0);
    assertThat(newPosition.getValue().adGroupIndex).isEqualTo(0);
    assertThat(newPosition.getValue().adIndexInAdGroup).isEqualTo(0);
    // from fourth media item pre roll ad to fourth media item content
    lastNewWindowUid = newPosition.getValue().windowUid;
    inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    assertThat(oldPosition.getValue().windowUid).isEqualTo(lastNewWindowUid);
    assertThat(oldPosition.getValue().mediaItemIndex).isEqualTo(3);
    assertThat(oldPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-3");
    assertThat(oldPosition.getValue().positionMs).isEqualTo(5_000);
    assertThat(oldPosition.getValue().contentPositionMs).isEqualTo(0);
    assertThat(oldPosition.getValue().adGroupIndex).isEqualTo(0);
    assertThat(oldPosition.getValue().adIndexInAdGroup).isEqualTo(0);
    assertThat(newPosition.getValue().windowUid).isEqualTo(oldPosition.getValue().windowUid);
    assertThat(newPosition.getValue().mediaItemIndex).isEqualTo(3);
    assertThat(newPosition.getValue().mediaItem.localConfiguration.tag).isEqualTo("id-3");
    assertThat(newPosition.getValue().positionMs).isEqualTo(0);
    assertThat(newPosition.getValue().contentPositionMs).isEqualTo(0);
    assertThat(newPosition.getValue().adGroupIndex).isEqualTo(-1);
    assertThat(newPosition.getValue().adIndexInAdGroup).isEqualTo(-1);
    inOrder.verify(listener, never()).onPositionDiscontinuity(any(), any(), not(eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION)));
    inOrder.verify(listener, never()).onMediaItemTransition(any(), not(eq(Player.MEDIA_ITEM_TRANSITION_REASON_AUTO)));
    player.release();
}
Also used : Window(com.google.android.exoplayer2.Timeline.Window) InOrder(org.mockito.InOrder) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Listener(com.google.android.exoplayer2.Player.Listener) 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) PositionInfo(com.google.android.exoplayer2.Player.PositionInfo) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)53 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)32 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)31 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)31 FakeClock (com.google.android.exoplayer2.testutil.FakeClock)29 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)23 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)23 PlaybackOutput (com.google.android.exoplayer2.robolectric.PlaybackOutput)18 Context (android.content.Context)17 Listener (com.google.android.exoplayer2.Player.Listener)17 CapturingRenderersFactory (com.google.android.exoplayer2.testutil.CapturingRenderersFactory)17 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)16 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)16 PositionInfo (com.google.android.exoplayer2.Player.PositionInfo)13 TestPlayerRunHelper.playUntilStartOfMediaItem (com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem)11 SurfaceTexture (android.graphics.SurfaceTexture)10 Surface (android.view.Surface)10 InOrder (org.mockito.InOrder)10 Nullable (androidx.annotation.Nullable)7 MediaSource (com.google.android.exoplayer2.source.MediaSource)5