use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.
the class ExoPlayerTest method seekTo_whilePlayingAd_correctMasking.
@Test
public void seekTo_whilePlayingAd_correctMasking() throws Exception {
long contentDurationMs = 10_000;
long adDurationMs = 4_000;
AdPlaybackState adPlaybackState = new AdPlaybackState(/* adsId= */
new Object(), /* adGroupTimesUs...= */
0);
adPlaybackState = adPlaybackState.withAdCount(/* adGroupIndex= */
0, /* adCount= */
1);
adPlaybackState = adPlaybackState.withAdUri(/* adGroupIndex= */
0, /* adIndexInAdGroup= */
0, Uri.EMPTY);
long[][] durationsUs = new long[1][];
durationsUs[0] = new long[] { Util.msToUs(adDurationMs) };
adPlaybackState = adPlaybackState.withAdDurationsUs(durationsUs);
Timeline adTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* durationUs= */
Util.msToUs(contentDurationMs), adPlaybackState));
FakeMediaSource adsMediaSource = new FakeMediaSource(adTimeline);
int[] mediaItemIndex = new int[] { C.INDEX_UNSET, C.INDEX_UNSET };
long[] positionMs = new long[] { C.TIME_UNSET, C.TIME_UNSET };
long[] bufferedPositionMs = new long[] { C.TIME_UNSET, C.TIME_UNSET };
long[] totalBufferedDurationMs = new long[] { C.TIME_UNSET, C.TIME_UNSET };
boolean[] isPlayingAd = new boolean[2];
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).waitForIsLoading(true).waitForIsLoading(false).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
player.seekTo(/* mediaItemIndex= */
0, /* positionMs= */
8000);
mediaItemIndex[0] = player.getCurrentMediaItemIndex();
isPlayingAd[0] = player.isPlayingAd();
positionMs[0] = player.getCurrentPosition();
bufferedPositionMs[0] = player.getBufferedPosition();
totalBufferedDurationMs[0] = player.getTotalBufferedDuration();
}
}).waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
mediaItemIndex[1] = player.getCurrentMediaItemIndex();
isPlayingAd[1] = player.isPlayingAd();
positionMs[1] = player.getCurrentPosition();
bufferedPositionMs[1] = player.getBufferedPosition();
totalBufferedDurationMs[1] = player.getTotalBufferedDuration();
}
}).stop().build();
new ExoPlayerTestRunner.Builder(context).setMediaSources(adsMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
assertThat(mediaItemIndex[0]).isEqualTo(0);
assertThat(isPlayingAd[0]).isTrue();
assertThat(positionMs[0]).isEqualTo(0);
assertThat(bufferedPositionMs[0]).isEqualTo(adDurationMs);
assertThat(totalBufferedDurationMs[0]).isEqualTo(adDurationMs);
assertThat(mediaItemIndex[1]).isEqualTo(0);
assertThat(isPlayingAd[1]).isTrue();
assertThat(positionMs[1]).isEqualTo(0);
assertThat(bufferedPositionMs[1]).isEqualTo(adDurationMs);
assertThat(totalBufferedDurationMs[1]).isEqualTo(adDurationMs);
}
use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.
the class ExoPlayerTest method timelineRefresh_withModifiedMediaItem_doesNotNotifyMediaItemTransition.
@Test
public void timelineRefresh_withModifiedMediaItem_doesNotNotifyMediaItemTransition() throws Exception {
List<MediaItem> reportedMediaItems = new ArrayList<>();
List<Integer> reportedTransitionReasons = new ArrayList<>();
List<Timeline> reportedTimelines = new ArrayList<>();
MediaItem initialMediaItem = FakeTimeline.FAKE_MEDIA_ITEM.buildUpon().setTag(0).build();
TimelineWindowDefinition initialWindow = new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
10_000_000, /* defaultPositionUs= */
0, /* windowOffsetInFirstPeriodUs= */
0, ImmutableList.of(AdPlaybackState.NONE), initialMediaItem);
TimelineWindowDefinition secondWindow = new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
10_000_000, /* defaultPositionUs= */
0, /* windowOffsetInFirstPeriodUs= */
0, ImmutableList.of(AdPlaybackState.NONE), initialMediaItem.buildUpon().setTag(1).build());
FakeTimeline timeline = new FakeTimeline(initialWindow);
FakeTimeline newTimeline = new FakeTimeline(secondWindow);
FakeMediaSource mediaSource = new FakeMediaSource(timeline);
ExoPlayer player = new TestExoPlayerBuilder(context).build();
player.addListener(new Listener() {
@Override
public void onTimelineChanged(Timeline timeline, int reason) {
reportedTimelines.add(timeline);
}
@Override
public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
reportedMediaItems.add(mediaItem);
reportedTransitionReasons.add(reason);
}
});
player.setMediaSource(mediaSource);
player.prepare();
player.play();
runUntilPlaybackState(player, Player.STATE_READY);
mediaSource.setNewSourceInfo(newTimeline);
runUntilPlaybackState(player, Player.STATE_ENDED);
assertTimelinesSame(reportedTimelines, ImmutableList.of(placeholderTimeline, timeline, newTimeline));
assertThat(reportedMediaItems).containsExactly(initialMediaItem).inOrder();
assertThat(reportedTransitionReasons).containsExactly(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED).inOrder();
player.release();
}
use of com.google.android.exoplayer2.Timeline 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);
}
use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.
the class SinglePeriodTimelineTest method getWindow_setsMediaItemAndTag.
// Tests backward compatibility.
@SuppressWarnings("deprecation")
@Test
public void getWindow_setsMediaItemAndTag() {
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.EMPTY).setTag(new Object()).build();
SinglePeriodTimeline timeline = new SinglePeriodTimeline(/* durationUs= */
C.TIME_UNSET, /* isSeekable= */
false, /* isDynamic= */
false, /* isLive= */
false, /* manifest= */
null, mediaItem);
Window window = timeline.getWindow(/* windowIndex= */
0, this.window);
assertThat(window.mediaItem).isEqualTo(mediaItem);
assertThat(window.tag).isEqualTo(mediaItem.localConfiguration.tag);
}
use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.
the class AdsMediaSourceTest method setUp.
@Before
public void setUp() {
// Set up content and ad media sources, passing a null timeline so tests can simulate setting it
// later.
contentMediaSource = new FakeMediaSource(/* timeline= */
null);
prerollAdMediaSource = new FakeMediaSource(/* timeline= */
null);
MediaSource.Factory adMediaSourceFactory = mock(MediaSource.Factory.class);
when(adMediaSourceFactory.createMediaSource(any(MediaItem.class))).thenReturn(prerollAdMediaSource);
// Prepare the AdsMediaSource and capture its ads loader listener.
AdsLoader mockAdsLoader = mock(AdsLoader.class);
AdViewProvider mockAdViewProvider = mock(AdViewProvider.class);
ArgumentCaptor<EventListener> eventListenerArgumentCaptor = ArgumentCaptor.forClass(AdsLoader.EventListener.class);
adsMediaSource = new AdsMediaSource(contentMediaSource, TEST_ADS_DATA_SPEC, TEST_ADS_ID, adMediaSourceFactory, mockAdsLoader, mockAdViewProvider);
adsMediaSource.prepareSource(mockMediaSourceCaller, /* mediaTransferListener= */
null, PlayerId.UNSET);
shadowOf(Looper.getMainLooper()).idle();
verify(mockAdsLoader).start(eq(adsMediaSource), eq(TEST_ADS_DATA_SPEC), eq(TEST_ADS_ID), eq(mockAdViewProvider), eventListenerArgumentCaptor.capture());
// Simulate loading a preroll ad.
AdsLoader.EventListener adsLoaderEventListener = eventListenerArgumentCaptor.getValue();
adsLoaderEventListener.onAdPlaybackState(AD_PLAYBACK_STATE);
shadowOf(Looper.getMainLooper()).idle();
}
Aggregations