use of com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US in project ExoPlayer by google.
the class FakeTimeline method createMultiPeriodAdTimeline.
/**
* Creates a multi-period timeline with ad and content periods specified by the flags passed as
* var-arg arguments.
*
* <p>Period uid end up being a {@code new Pair<>(windowId, periodIndex)}.
*
* @param windowId The window ID.
* @param numberOfPlayedAds The number of ads that should be marked as played.
* @param isAdPeriodFlags A value of true indicates an ad period. A value of false indicated a
* content period.
* @return A timeline with a single window with as many periods as var-arg arguments.
*/
public static FakeTimeline createMultiPeriodAdTimeline(Object windowId, int numberOfPlayedAds, boolean... isAdPeriodFlags) {
long periodDurationUs = DEFAULT_WINDOW_DURATION_US / isAdPeriodFlags.length;
AdPlaybackState contentPeriodState = new AdPlaybackState(/* adsId= */
"adsId");
AdPlaybackState firstAdPeriodState = contentPeriodState.withNewAdGroup(/* adGroupIndex= */
0, /* adGroupTimesUs */
0).withAdCount(/* adGroupIndex= */
0, 1).withAdDurationsUs(/* adGroupIndex= */
0, DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US + periodDurationUs).withIsServerSideInserted(/* adGroupIndex= */
0, true);
AdPlaybackState commonAdPeriodState = firstAdPeriodState.withAdDurationsUs(0, periodDurationUs);
List<AdPlaybackState> adPlaybackStates = new ArrayList<>();
int playedAdsCounter = 0;
for (boolean isAd : isAdPeriodFlags) {
AdPlaybackState periodAdPlaybackState = isAd ? (adPlaybackStates.isEmpty() ? firstAdPeriodState : commonAdPeriodState) : contentPeriodState;
if (isAd && playedAdsCounter < numberOfPlayedAds) {
periodAdPlaybackState = periodAdPlaybackState.withPlayedAd(/* adGroupIndex= */
0, /* adIndexInAdGroup= */
0);
playedAdsCounter++;
}
adPlaybackStates.add(periodAdPlaybackState);
}
return new FakeTimeline(new FakeTimeline.TimelineWindowDefinition(isAdPeriodFlags.length, windowId, /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
DEFAULT_WINDOW_DURATION_US, /* defaultPositionUs= */
0, /* windowOffsetInFirstPeriodUs= */
DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, /* adPlaybackStates= */
adPlaybackStates, MediaItem.EMPTY));
}
use of com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US in project ExoPlayer by google.
the class ExoPlayerTest method play_playedSSAIPreMidPostRollsSinglePeriodWindow_noDiscontinuities.
@Test
public void play_playedSSAIPreMidPostRollsSinglePeriodWindow_noDiscontinuities() throws Exception {
AdPlaybackState adPlaybackState = addAdGroupToAdPlaybackState(new AdPlaybackState("adsId"), /* fromPositionUs= */
DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
C.MICROS_PER_SECOND);
adPlaybackState = addAdGroupToAdPlaybackState(adPlaybackState, /* fromPositionUs= */
DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US + (3 * C.MICROS_PER_SECOND), /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
C.MICROS_PER_SECOND);
adPlaybackState = addAdGroupToAdPlaybackState(adPlaybackState, /* fromPositionUs= */
DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US + (5 * C.MICROS_PER_SECOND), /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
C.MICROS_PER_SECOND);
adPlaybackState = addAdGroupToAdPlaybackState(adPlaybackState, /* fromPositionUs= */
DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US + (9 * C.MICROS_PER_SECOND), /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
C.MICROS_PER_SECOND);
adPlaybackState = adPlaybackState.withPlayedAd(/* adGroupIndex= */
0, /* adIndexInAdGroup+ */
0);
adPlaybackState = adPlaybackState.withPlayedAd(/* adGroupIndex= */
1, /* adIndexInAdGroup+ */
0);
adPlaybackState = adPlaybackState.withPlayedAd(/* adGroupIndex= */
2, /* adIndexInAdGroup+ */
0);
adPlaybackState = adPlaybackState.withPlayedAd(/* adGroupIndex= */
3, /* adIndexInAdGroup+ */
0);
FakeTimeline adTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, "windowId", /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
DEFAULT_WINDOW_DURATION_US, /* defaultPositionUs= */
0, /* windowOffsetInFirstPeriodUs= */
DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, /* adPlaybackStates= */
ImmutableList.of(adPlaybackState), MediaItem.EMPTY));
Listener listener = mock(Listener.class);
ExoPlayer player = new TestExoPlayerBuilder(context).build();
player.addListener(listener);
AtomicReference<ServerSideAdInsertionMediaSource> sourceReference = new AtomicReference<>();
sourceReference.set(new ServerSideAdInsertionMediaSource(new FakeMediaSource(adTimeline, ExoPlayerTestRunner.AUDIO_FORMAT), contentTimeline -> {
sourceReference.get().setAdPlaybackStates(adTimeline.getAdPlaybackStates(/* windowIndex= */
0));
return true;
}));
player.setMediaSource(sourceReference.get());
player.prepare();
player.play();
runUntilPlaybackState(player, Player.STATE_ENDED);
long finalPositionMs = player.getCurrentPosition();
player.release();
assertThat(finalPositionMs).isEqualTo(6000);
verify(listener, never()).onPositionDiscontinuity(any(), any(), anyInt());
ArgumentCaptor<Integer> playbackStateCaptor = ArgumentCaptor.forClass(Integer.class);
verify(listener, times(3)).onPlaybackStateChanged(playbackStateCaptor.capture());
assertThat(playbackStateCaptor.getAllValues()).containsExactly(2, 3, 4).inOrder();
}
Aggregations