use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method loadControlNeverWantsToLoad_throwsIllegalStateException.
@Test
public void loadControlNeverWantsToLoad_throwsIllegalStateException() {
LoadControl neverLoadingLoadControl = new DefaultLoadControl() {
@Override
public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) {
return false;
}
@Override
public boolean shouldStartPlayback(long bufferedDurationUs, float playbackSpeed, boolean rebuffering, long targetLiveOffsetUs) {
return true;
}
};
// Use chunked data to ensure the player actually needs to continue loading and playing.
FakeAdaptiveDataSet.Factory dataSetFactory = new FakeAdaptiveDataSet.Factory(/* chunkDurationUs= */
500_000, /* bitratePercentStdDev= */
10.0, new Random(0));
MediaSource chunkedMediaSource = new FakeAdaptiveMediaSource(new FakeTimeline(), new TrackGroupArray(new TrackGroup(ExoPlayerTestRunner.VIDEO_FORMAT)), new FakeChunkSource.Factory(dataSetFactory, new FakeDataSource.Factory()));
ExoPlaybackException exception = assertThrows(ExoPlaybackException.class, () -> new ExoPlayerTestRunner.Builder(context).setLoadControl(neverLoadingLoadControl).setMediaSources(chunkedMediaSource).build().start().blockUntilEnded(TIMEOUT_MS));
assertThat(exception.type).isEqualTo(ExoPlaybackException.TYPE_UNEXPECTED);
assertThat(exception.getUnexpectedException()).isInstanceOf(IllegalStateException.class);
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method loading_withLargeAllocationCausingOom_playsRemainingMediaAndThenThrows.
@Test
public void loading_withLargeAllocationCausingOom_playsRemainingMediaAndThenThrows() {
Loader.Loadable loadable = new Loader.Loadable() {
@SuppressWarnings("UnusedVariable")
@Override
public void load() throws IOException {
// This test needs the allocation to cause an OOM.
@SuppressWarnings("unused") byte[] largeBuffer = new byte[Integer.MAX_VALUE];
}
@Override
public void cancelLoad() {
}
};
// Create 3 samples without end of stream signal to test that all 3 samples are
// still played before the sample stream exception is thrown.
FakeSampleStreamItem sample = oneByteSample(TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, C.BUFFER_FLAG_KEY_FRAME);
FakeMediaPeriod.TrackDataFactory threeSamplesWithoutEos = (format, mediaPeriodId) -> ImmutableList.of(sample, sample, sample);
MediaSource largeBufferAllocatingMediaSource = 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, threeSamplesWithoutEos, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
false) {
private final Loader loader = new Loader("ExoPlayerTest");
@Override
public boolean continueLoading(long positionUs) {
super.continueLoading(positionUs);
if (!loader.isLoading()) {
loader.startLoading(loadable, new FakeLoaderCallback(), /* defaultMinRetryCount= */
1);
}
return true;
}
@Override
protected FakeSampleStream createSampleStream(Allocator allocator, @Nullable MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, Format initialFormat, List<FakeSampleStreamItem> fakeSampleStreamItems) {
return new FakeSampleStream(allocator, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, initialFormat, fakeSampleStreamItems) {
@Override
public void maybeThrowError() throws IOException {
loader.maybeThrowError();
}
};
}
};
}
};
FakeRenderer renderer = new FakeRenderer(C.TRACK_TYPE_VIDEO);
ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(largeBufferAllocatingMediaSource).setRenderers(renderer).build();
ExoPlaybackException exception = assertThrows(ExoPlaybackException.class, () -> testRunner.start().blockUntilEnded(TIMEOUT_MS));
assertThat(exception.type).isEqualTo(ExoPlaybackException.TYPE_SOURCE);
assertThat(exception.getSourceException()).isInstanceOf(Loader.UnexpectedLoaderException.class);
assertThat(exception.getSourceException().getCause()).isInstanceOf(OutOfMemoryError.class);
assertThat(renderer.sampleBufferReadCount).isEqualTo(3);
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method repeatedSeeksToUnpreparedPeriodInSameWindowKeepsWindowSequenceNumber.
@Test
public void repeatedSeeksToUnpreparedPeriodInSameWindowKeepsWindowSequenceNumber() throws Exception {
Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
2, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* durationUs= */
10 * C.MICROS_PER_SECOND));
FakeMediaSource mediaSource = new FakeMediaSource(timeline);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).seek(/* mediaItemIndex= */
0, /* positionMs= */
9999).waitForPendingPlayerCommands().seek(/* mediaItemIndex= */
0, /* positionMs= */
1).waitForPendingPlayerCommands().seek(/* mediaItemIndex= */
0, /* positionMs= */
9999).waitForPendingPlayerCommands().play().build();
ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
testRunner.assertPlayedPeriodIndices(0, 1, 0, 1);
assertThat(mediaSource.getCreatedMediaPeriods()).containsAtLeast(new MediaPeriodId(timeline.getUidOfPeriod(/* periodIndex= */
0), /* windowSequenceNumber= */
0), new MediaPeriodId(timeline.getUidOfPeriod(/* periodIndex= */
1), /* windowSequenceNumber= */
0));
assertThat(mediaSource.getCreatedMediaPeriods()).doesNotContain(new MediaPeriodId(timeline.getUidOfPeriod(/* periodIndex= */
1), /* windowSequenceNumber= */
1));
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method setMediaSources_whenPrepared_correctMaskingPlaybackState.
@Test
public void setMediaSources_whenPrepared_correctMaskingPlaybackState() 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);
final int[] maskingPlaybackStates = new int[4];
Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Set empty media item with an implicit seek to current position.
player.setMediaSource(new ConcatenatingMediaSource(), /* resetPosition= */
false);
// Expect masking state is ended,
maskingPlaybackStates[0] = player.getPlaybackState();
}
}).waitForPlaybackState(Player.STATE_ENDED).setMediaSources(/* mediaItemIndex= */
0, /* positionMs= */
C.TIME_UNSET, firstMediaSource).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Set empty media item with an explicit seek.
player.setMediaSource(new ConcatenatingMediaSource(), /* startPositionMs= */
C.TIME_UNSET);
// Expect masking state is ended,
maskingPlaybackStates[1] = player.getPlaybackState();
}
}).waitForPlaybackState(Player.STATE_ENDED).setMediaSources(/* mediaItemIndex= */
0, /* positionMs= */
C.TIME_UNSET, firstMediaSource).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Set media item with an explicit seek.
player.setMediaSource(secondMediaSource, /* startPositionMs= */
C.TIME_UNSET);
// Expect masking state is buffering,
maskingPlaybackStates[2] = player.getPlaybackState();
}
}).waitForPlaybackState(Player.STATE_READY).setMediaSources(/* mediaItemIndex= */
0, /* positionMs= */
C.TIME_UNSET, firstMediaSource).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Set media item with an implicit seek to the current position.
player.setMediaSource(secondMediaSource, /* resetPosition= */
false);
// Expect masking state is buffering,
maskingPlaybackStates[3] = player.getPlaybackState();
}
}).play().waitForPlaybackState(Player.STATE_READY).build();
ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).setExpectedPlayerEndedCount(/* expectedPlayerEndedCount= */
3).setMediaSources(firstMediaSource).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 setting empty source without seek.
Player.STATE_ENDED, Player.STATE_BUFFERING, // Ready again after re-setting source.
Player.STATE_READY, // Ended after setting empty source with seek.
Player.STATE_ENDED, Player.STATE_BUFFERING, // Ready again after re-setting source.
Player.STATE_READY, Player.STATE_BUFFERING, // Ready after setting media item with seek.
Player.STATE_READY, Player.STATE_BUFFERING, // Ready again after re-setting source.
Player.STATE_READY, // Play.
Player.STATE_BUFFERING, // Ready after setting media item without seek.
Player.STATE_READY, Player.STATE_ENDED);
assertArrayEquals(new int[] { Player.STATE_ENDED, Player.STATE_ENDED, Player.STATE_BUFFERING, Player.STATE_BUFFERING }, maskingPlaybackStates);
exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Initial source.
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, // Empty source.
Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Reset source.
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, // Empty source.
Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Reset source.
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Set source with seek.
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Reset source.
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, // Set source without seek.
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method stop_withoutReset_releasesMediaSource.
@Test
public void stop_withoutReset_releasesMediaSource() throws Exception {
Timeline timeline = new FakeTimeline();
final FakeMediaSource mediaSource = new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_READY).stop(/* reset= */
false).build();
new ExoPlayerTestRunner.Builder(context).setTimeline(timeline).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
mediaSource.assertReleased();
}
Aggregations