use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method nextLoadPositionExceedingLoadControlMaxBuffer_whileCurrentLoadInProgress_doesNotThrowException.
@Test
public void nextLoadPositionExceedingLoadControlMaxBuffer_whileCurrentLoadInProgress_doesNotThrowException() throws Exception {
long maxBufferUs = 2 * C.MICROS_PER_SECOND;
LoadControl loadControlWithMaxBufferUs = new DefaultLoadControl() {
@Override
public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) {
return bufferedDurationUs < maxBufferUs;
}
@Override
public boolean shouldStartPlayback(long bufferedDurationUs, float playbackSpeed, boolean rebuffering, long targetLiveOffsetUs) {
return true;
}
};
MediaSource mediaSourceWithLoadInProgress = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT) {
@Override
protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
return new FakeMediaPeriod(trackGroupArray, allocator, TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, mediaSourceEventDispatcher) {
@Override
public long getBufferedPositionUs() {
// Pretend not to have buffered data yet.
return 0;
}
@Override
public long getNextLoadPositionUs() {
// Set next load position beyond the maxBufferUs configured in the LoadControl.
return Long.MAX_VALUE;
}
@Override
public boolean isLoading() {
return true;
}
};
}
};
FakeRenderer rendererWaitingForData = new FakeRenderer(C.TRACK_TYPE_VIDEO) {
@Override
public boolean isReady() {
return false;
}
};
ExoPlayer player = new TestExoPlayerBuilder(context).setRenderers(rendererWaitingForData).setLoadControl(loadControlWithMaxBufferUs).build();
player.setMediaSource(mediaSourceWithLoadInProgress);
player.prepare();
// Wait until the MediaSource is prepared, i.e. returned its timeline, and at least one
// iteration of doSomeWork after this was run.
TestPlayerRunHelper.runUntilTimelineChanged(player);
runUntilPendingCommandsAreFullyHandled(player);
assertThat(player.getPlayerError()).isNull();
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class MediaSourceListTest method removeMediaSources_whenPrepared_expectRelease.
@Test
public void removeMediaSources_whenPrepared_expectRelease() {
MediaSource mockMediaSource1 = mock(MediaSource.class);
when(mockMediaSource1.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
MediaSource mockMediaSource2 = mock(MediaSource.class);
when(mockMediaSource2.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
MediaSource mockMediaSource3 = mock(MediaSource.class);
when(mockMediaSource3.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
MediaSource mockMediaSource4 = mock(MediaSource.class);
when(mockMediaSource4.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
ShuffleOrder.DefaultShuffleOrder shuffleOrder = new ShuffleOrder.DefaultShuffleOrder(/* length= */
4);
List<MediaSourceList.MediaSourceHolder> holders = createFakeHoldersWithSources(/* useLazyPreparation= */
false, mockMediaSource1, mockMediaSource2, mockMediaSource3, mockMediaSource4);
mediaSourceList.prepare(/* mediaTransferListener */
null);
mediaSourceList.addMediaSources(/* index= */
0, holders, shuffleOrder);
mediaSourceList.removeMediaSourceRange(/* fromIndex= */
1, /* toIndex= */
3, shuffleOrder);
assertThat(mediaSourceList.getSize()).isEqualTo(2);
holders.remove(2);
holders.remove(1);
assertDefaultFirstWindowInChildIndexOrder(holders);
verify(mockMediaSource1, times(0)).releaseSource(any(MediaSource.MediaSourceCaller.class));
verify(mockMediaSource2, times(1)).releaseSource(any(MediaSource.MediaSourceCaller.class));
verify(mockMediaSource3, times(1)).releaseSource(any(MediaSource.MediaSourceCaller.class));
verify(mockMediaSource4, times(0)).releaseSource(any(MediaSource.MediaSourceCaller.class));
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class MediaSourceListTest method setMediaSources_mediaSourceListUnprepared_notUsingLazyPreparation.
@Test
public void setMediaSources_mediaSourceListUnprepared_notUsingLazyPreparation() {
ShuffleOrder.DefaultShuffleOrder shuffleOrder = new ShuffleOrder.DefaultShuffleOrder(/* length= */
2);
MediaSource mockMediaSource1 = mock(MediaSource.class);
when(mockMediaSource1.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
MediaSource mockMediaSource2 = mock(MediaSource.class);
when(mockMediaSource2.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
List<MediaSourceList.MediaSourceHolder> mediaSources = createFakeHoldersWithSources(/* useLazyPreparation= */
false, mockMediaSource1, mockMediaSource2);
Timeline timeline = mediaSourceList.setMediaSources(mediaSources, shuffleOrder);
assertThat(timeline.getWindowCount()).isEqualTo(2);
assertThat(mediaSourceList.getSize()).isEqualTo(2);
// Assert holder offsets have been set properly
for (int i = 0; i < mediaSources.size(); i++) {
MediaSourceList.MediaSourceHolder mediaSourceHolder = mediaSources.get(i);
assertThat(mediaSourceHolder.isRemoved).isFalse();
assertThat(mediaSourceHolder.firstWindowIndexInChild).isEqualTo(i);
}
// Set media items again. The second holder is re-used.
MediaSource mockMediaSource3 = mock(MediaSource.class);
when(mockMediaSource3.getMediaItem()).thenReturn(MINIMAL_MEDIA_ITEM);
List<MediaSourceList.MediaSourceHolder> moreMediaSources = createFakeHoldersWithSources(/* useLazyPreparation= */
false, mockMediaSource3);
moreMediaSources.add(mediaSources.get(1));
timeline = mediaSourceList.setMediaSources(moreMediaSources, shuffleOrder);
assertThat(mediaSourceList.getSize()).isEqualTo(2);
assertThat(timeline.getWindowCount()).isEqualTo(2);
for (int i = 0; i < moreMediaSources.size(); i++) {
MediaSourceList.MediaSourceHolder mediaSourceHolder = moreMediaSources.get(i);
assertThat(mediaSourceHolder.isRemoved).isFalse();
assertThat(mediaSourceHolder.firstWindowIndexInChild).isEqualTo(i);
}
// Expect removed holders and sources to be removed without releasing.
verify(mockMediaSource1, times(0)).releaseSource(any(MediaSource.MediaSourceCaller.class));
assertThat(mediaSources.get(0).isRemoved).isTrue();
// Expect re-used holder and source not to be removed.
verify(mockMediaSource2, times(0)).releaseSource(any(MediaSource.MediaSourceCaller.class));
assertThat(mediaSources.get(1).isRemoved).isFalse();
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method moveMediaItems_correctMaskingMediaItemIndex.
@Test
public void moveMediaItems_correctMaskingMediaItemIndex() throws Exception {
Timeline timeline = new FakeTimeline();
MediaSource firstMediaSource = new FakeMediaSource(timeline);
MediaSource secondMediaSource = new FakeMediaSource(timeline);
MediaSource thirdMediaSource = new FakeMediaSource(timeline);
final int[] currentMediaItemIndices = { C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET };
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Move the current item down in the playlist.
player.moveMediaItems(/* fromIndex= */
0, /* toIndex= */
2, /* newIndex= */
1);
currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
}
}).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Move the current item up in the playlist.
player.moveMediaItems(/* fromIndex= */
1, /* toIndex= */
3, /* newIndex= */
0);
currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
}
}).seek(/* mediaItemIndex= */
2, C.TIME_UNSET).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Move items from before to behind the current item.
player.moveMediaItems(/* fromIndex= */
0, /* toIndex= */
2, /* newIndex= */
1);
currentMediaItemIndices[2] = player.getCurrentMediaItemIndex();
}
}).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Move items from behind to before the current item.
player.moveMediaItems(/* fromIndex= */
1, /* toIndex= */
3, /* newIndex= */
0);
currentMediaItemIndices[3] = player.getCurrentMediaItemIndex();
}
}).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Move items from before to before the current item.
// No change in currentMediaItemIndex.
player.moveMediaItems(/* fromIndex= */
0, /* toIndex= */
1, /* newIndex= */
1);
currentMediaItemIndices[4] = player.getCurrentMediaItemIndex();
}
}).seek(/* mediaItemIndex= */
0, C.TIME_UNSET).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Move items from behind to behind the current item.
// No change in currentMediaItemIndex.
player.moveMediaItems(/* fromIndex= */
1, /* toIndex= */
2, /* newIndex= */
2);
currentMediaItemIndices[5] = player.getCurrentMediaItemIndex();
}
}).build();
new ExoPlayerTestRunner.Builder(context).setMediaSources(firstMediaSource, secondMediaSource, thirdMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
assertArrayEquals(new int[] { 1, 0, 0, 2, 2, 0 }, currentMediaItemIndices);
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method sendMessagesMoveCurrentMediaItemIndex.
@Test
public void sendMessagesMoveCurrentMediaItemIndex() throws Exception {
Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0));
final Timeline secondTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
1), new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0));
final FakeMediaSource mediaSource = new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT);
PositionGrabbingMessageTarget target = new PositionGrabbingMessageTarget();
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForTimelineChanged(timeline, /* expectedReason */
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE).sendMessage(target, /* positionMs= */
50).executeRunnable(() -> mediaSource.setNewSourceInfo(secondTimeline)).waitForTimelineChanged(secondTimeline, /* expectedReason */
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE).play().build();
new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
assertThat(target.positionMs).isAtLeast(50L);
assertThat(target.mediaItemIndex).isEqualTo(1);
}
Aggregations