use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.
the class ExoPlayerTest method sendMessages_withMediaRemoval_triggersCorrectMessagesAndDoesNotThrow.
@Test
public void sendMessages_withMediaRemoval_triggersCorrectMessagesAndDoesNotThrow() throws Exception {
ExoPlayer player = new TestExoPlayerBuilder(context).build();
player.addMediaSources(ImmutableList.of(new FakeMediaSource(), new FakeMediaSource()));
player.createMessage((messageType, payload) -> {
}).setPosition(/* mediaItemIndex= */
0, /* positionMs= */
0).setDeleteAfterDelivery(false).send();
PlayerMessage.Target secondMediaItemTarget = mock(PlayerMessage.Target.class);
player.createMessage(secondMediaItemTarget).setPosition(/* mediaItemIndex= */
1, /* positionMs= */
0).setDeleteAfterDelivery(false).send();
// Play through media once to trigger all messages. This ensures any internally saved message
// indices are non-zero.
player.prepare();
player.play();
runUntilPlaybackState(player, Player.STATE_ENDED);
verify(secondMediaItemTarget).handleMessage(anyInt(), any());
// Remove first item and play second item again to check if message is triggered again.
// After removal, any internally saved message indices are invalid and will throw
// IndexOutOfBoundsException if used without updating.
// See https://github.com/google/ExoPlayer/issues/7278.
player.removeMediaItem(/* index= */
0);
player.seekTo(/* positionMs= */
0);
runUntilPlaybackState(player, Player.STATE_ENDED);
assertThat(player.getPlayerError()).isNull();
verify(secondMediaItemTarget, times(2)).handleMessage(anyInt(), any());
}
use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.
the class ExoPlayerTest method removeMediaItems_currentItemRemovedThatIsTheLast_correctMasking.
@Test
public void removeMediaItems_currentItemRemovedThatIsTheLast_correctMasking() 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);
Timeline thirdTimeline = new FakeTimeline(/* windowCount= */
1, 3L);
MediaSource thirdMediaSource = new FakeMediaSource(thirdTimeline);
Timeline fourthTimeline = new FakeTimeline(/* windowCount= */
1, 3L);
MediaSource fourthMediaSource = new FakeMediaSource(fourthTimeline);
final int[] currentMediaItemIndices = new int[9];
Arrays.fill(currentMediaItemIndices, C.INDEX_UNSET);
final int[] maskingPlaybackStates = new int[4];
Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
final long[] currentPositions = new long[3];
Arrays.fill(currentPositions, C.TIME_UNSET);
final long[] bufferedPositions = new long[3];
Arrays.fill(bufferedPositions, C.TIME_UNSET);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_READY).waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Expect the current media item index to be 2 after seek.
currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
currentPositions[0] = player.getCurrentPosition();
bufferedPositions[0] = player.getBufferedPosition();
player.removeMediaItem(/* index= */
2);
// Expect the current media item index to be 0
// (default position of timeline after not finding subsequent period).
currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
// Transition to ENDED.
maskingPlaybackStates[0] = player.getPlaybackState();
currentPositions[1] = player.getCurrentPosition();
bufferedPositions[1] = player.getBufferedPosition();
}
}).waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Expects the current media item index still on 0.
currentMediaItemIndices[2] = player.getCurrentMediaItemIndex();
// Insert an item at begin when the playlist is not empty.
player.addMediaSource(/* index= */
0, thirdMediaSource);
// Expects the current media item index to be (0 + 1) after insertion at begin.
currentMediaItemIndices[3] = player.getCurrentMediaItemIndex();
// Remains in ENDED.
maskingPlaybackStates[1] = player.getPlaybackState();
currentPositions[2] = player.getCurrentPosition();
bufferedPositions[2] = player.getBufferedPosition();
}
}).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
currentMediaItemIndices[4] = player.getCurrentMediaItemIndex();
// Implicit seek to the current media item index, which is out of bounds in new
// timeline.
player.setMediaSource(fourthMediaSource, /* resetPosition= */
false);
// 0 after reset.
currentMediaItemIndices[5] = player.getCurrentMediaItemIndex();
// Invalid seek, so we remain in ENDED.
maskingPlaybackStates[2] = player.getPlaybackState();
}
}).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
currentMediaItemIndices[6] = player.getCurrentMediaItemIndex();
// Explicit seek to (0, C.TIME_UNSET). Player transitions to BUFFERING.
player.setMediaSource(fourthMediaSource, /* startPositionMs= */
5000);
// 0 after explicit seek.
currentMediaItemIndices[7] = player.getCurrentMediaItemIndex();
// Transitions from ENDED to BUFFERING after explicit seek.
maskingPlaybackStates[3] = player.getPlaybackState();
}
}).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Check whether actual media item index is equal masking index from above.
currentMediaItemIndices[8] = player.getCurrentMediaItemIndex();
}
}).build();
ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
2, /* positionMs= */
C.TIME_UNSET).setExpectedPlayerEndedCount(2).setMediaSources(firstMediaSource, secondMediaSource, thirdMediaSource).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 removing current media item index
Player.STATE_ENDED, // buffers after set items with seek
Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_ENDED);
assertArrayEquals(new int[] { // ended after removing current media item index
Player.STATE_ENDED, // adding items does not change state
Player.STATE_ENDED, // set items with seek to current position.
Player.STATE_ENDED, Player.STATE_BUFFERING }, // buffers after set items with seek
maskingPlaybackStates);
assertArrayEquals(new int[] { 2, 0, 0, 1, 1, 0, 0, 0, 0 }, currentMediaItemIndices);
assertThat(currentPositions[0]).isEqualTo(0);
assertThat(currentPositions[1]).isEqualTo(0);
assertThat(currentPositions[2]).isEqualTo(0);
assertThat(bufferedPositions[0]).isGreaterThan(0);
assertThat(bufferedPositions[1]).isEqualTo(0);
assertThat(bufferedPositions[2]).isEqualTo(0);
}
use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.
the class ExoPlayerTest method moveMediaItem_behindLoadingPeriod_correctMaskingPosition.
@Test
public void moveMediaItem_behindLoadingPeriod_correctMaskingPosition() throws Exception {
final int[] mediaItemIndex = { C.INDEX_UNSET, C.INDEX_UNSET };
final long[] positionMs = { C.INDEX_UNSET, C.INDEX_UNSET };
final long[] bufferedPositions = { C.INDEX_UNSET, C.INDEX_UNSET };
final long[] totalBufferedDuration = { C.INDEX_UNSET, C.INDEX_UNSET };
runPositionMaskingCapturingActionSchedule(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
player.moveMediaItem(/* currentIndex= */
1, /* newIndex= */
2);
}
}, /* pauseMediaItemIndex= */
0, mediaItemIndex, positionMs, bufferedPositions, totalBufferedDuration, new FakeMediaSource(), new FakeMediaSource(), createPartiallyBufferedMediaSource(/* maxBufferedPositionMs= */
4000));
assertThat(mediaItemIndex[0]).isEqualTo(0);
assertThat(positionMs[0]).isAtLeast(8000);
assertThat(bufferedPositions[0]).isEqualTo(10_000);
assertThat(totalBufferedDuration[0]).isEqualTo(10_000 - positionMs[0]);
assertThat(mediaItemIndex[1]).isEqualTo(mediaItemIndex[0]);
assertThat(positionMs[1]).isEqualTo(positionMs[0]);
assertThat(bufferedPositions[1]).isEqualTo(10_000);
assertThat(totalBufferedDuration[1]).isEqualTo(10_000 - positionMs[1]);
}
use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.
the class ExoPlayerTest method clearMediaItems_correctMaskingPosition.
@Test
public void clearMediaItems_correctMaskingPosition() throws Exception {
final int[] mediaItemIndex = { C.INDEX_UNSET, C.INDEX_UNSET };
final long[] positionMs = { C.INDEX_UNSET, C.INDEX_UNSET };
final long[] bufferedPositions = { C.INDEX_UNSET, C.INDEX_UNSET };
final long[] totalBufferedDuration = { C.INDEX_UNSET, C.INDEX_UNSET };
runPositionMaskingCapturingActionSchedule(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
player.clearMediaItems();
}
}, /* pauseMediaItemIndex= */
0, mediaItemIndex, positionMs, bufferedPositions, totalBufferedDuration, new FakeMediaSource(), new FakeMediaSource(), createPartiallyBufferedMediaSource(/* maxBufferedPositionMs= */
4000));
assertThat(mediaItemIndex[0]).isEqualTo(0);
assertThat(positionMs[0]).isEqualTo(0);
assertThat(bufferedPositions[0]).isEqualTo(0);
assertThat(totalBufferedDuration[0]).isEqualTo(0);
assertThat(mediaItemIndex[1]).isEqualTo(mediaItemIndex[0]);
assertThat(positionMs[1]).isEqualTo(positionMs[0]);
assertThat(bufferedPositions[1]).isEqualTo(bufferedPositions[0]);
assertThat(totalBufferedDuration[1]).isEqualTo(totalBufferedDuration[0]);
}
use of com.google.android.exoplayer2.testutil.FakeMediaSource in project ExoPlayer by google.
the class ExoPlayerTest method adGroupWithLoadError_noFurtherAdGroup_isSkipped.
@Test
public void adGroupWithLoadError_noFurtherAdGroup_isSkipped() throws Exception {
AdPlaybackState initialAdPlaybackState = FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
1, /* adGroupTimesUs...= */
TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US + 5 * C.MICROS_PER_SECOND);
Timeline fakeTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
10 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
0, TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, initialAdPlaybackState));
AdPlaybackState errorAdPlaybackState = initialAdPlaybackState.withAdLoadError(/* adGroupIndex= */
0, /* adIndexInAdGroup= */
0);
final Timeline adErrorTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
10 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
0, TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, errorAdPlaybackState));
final FakeMediaSource fakeMediaSource = new FakeMediaSource(fakeTimeline, ExoPlayerTestRunner.VIDEO_FORMAT);
ExoPlayer player = new TestExoPlayerBuilder(context).build();
Player.Listener mockListener = mock(Player.Listener.class);
player.addListener(mockListener);
player.setMediaSource(fakeMediaSource);
player.prepare();
runUntilPlaybackState(player, Player.STATE_READY);
fakeMediaSource.setNewSourceInfo(adErrorTimeline);
player.play();
runUntilPlaybackState(player, Player.STATE_ENDED);
Timeline.Window window = player.getCurrentTimeline().getWindow(/* windowIndex= */
0, new Timeline.Window());
Timeline.Period period = player.getCurrentTimeline().getPeriod(/* periodIndex= */
0, new Timeline.Period(), /* setIds= */
true);
player.release();
// Content to content transition is ignored.
verify(mockListener, never()).onPositionDiscontinuity(any(), any(), anyInt());
}
Aggregations