use of com.google.android.exoplayer2.source.MediaSource.MediaPeriodId in project ExoPlayer by google.
the class ExoPlayerTest method sampleStreamMaybeThrowError_isNotThrownUntilPlaybackReachedFailingItem.
@Test
public void sampleStreamMaybeThrowError_isNotThrownUntilPlaybackReachedFailingItem() throws Exception {
ExoPlayer player = new TestExoPlayerBuilder(context).build();
Timeline timeline = new FakeTimeline();
player.addMediaSource(new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT));
player.addMediaSource(new FakeMediaSource(timeline, 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, /* trackDataFactory= */
(format, mediaPeriodId) -> ImmutableList.of(), mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
false) {
@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 {
throw new IOException();
}
};
}
};
}
});
player.prepare();
player.play();
ExoPlaybackException error = TestPlayerRunHelper.runUntilError(player);
Object period1Uid = player.getCurrentTimeline().getPeriod(/* periodIndex= */
1, new Timeline.Period(), /* setIds= */
true).uid;
assertThat(error.mediaPeriodId.periodUid).isEqualTo(period1Uid);
assertThat(player.getCurrentMediaItemIndex()).isEqualTo(1);
}
use of com.google.android.exoplayer2.source.MediaSource.MediaPeriodId 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.MediaPeriodId 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.MediaPeriodId in project ExoPlayer by google.
the class DefaultAnalyticsCollectorTest method adPlayback.
@Test
public void adPlayback() throws Exception {
long contentDurationsUs = 11 * C.MICROS_PER_SECOND;
long windowOffsetInFirstPeriodUs = TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
AtomicReference<AdPlaybackState> adPlaybackState = new AtomicReference<>(FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
1, /* adGroupTimesUs...= */
windowOffsetInFirstPeriodUs, windowOffsetInFirstPeriodUs + 5 * C.MICROS_PER_SECOND, C.TIME_END_OF_SOURCE));
AtomicInteger playedAdCount = new AtomicInteger(0);
Timeline adTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, contentDurationsUs, adPlaybackState.get()));
FakeMediaSource fakeMediaSource = new FakeMediaSource(adTimeline, DrmSessionManager.DRM_UNSUPPORTED, (unusedFormat, mediaPeriodId) -> {
if (mediaPeriodId.isAd()) {
return ImmutableList.of(oneByteSample(/* timeUs= */
0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM);
} else {
// postroll.
return ImmutableList.of(oneByteSample(windowOffsetInFirstPeriodUs + C.MICROS_PER_SECOND, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(windowOffsetInFirstPeriodUs + 6 * C.MICROS_PER_SECOND, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(windowOffsetInFirstPeriodUs + contentDurationsUs, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM);
}
}, ExoPlayerTestRunner.VIDEO_FORMAT);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
player.addListener(new Player.Listener() {
@Override
public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
if (!player.isPlayingAd() && reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
// Finished playing ad. Marked as played.
adPlaybackState.set(adPlaybackState.get().withPlayedAd(/* adGroupIndex= */
playedAdCount.getAndIncrement(), /* adIndexInAdGroup= */
0));
fakeMediaSource.setNewSourceInfo(new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, contentDurationsUs, adPlaybackState.get())), /* sendManifestLoadEvents= */
false);
}
}
});
}
}).pause().waitForIsLoading(true).waitForIsLoading(false).waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
0, /* positionMs= */
3_000).waitForPendingPlayerCommands().playUntilPosition(/* mediaItemIndex= */
0, /* positionMs= */
8_000).waitForPendingPlayerCommands().play().waitForPlaybackState(Player.STATE_ENDED).waitForTimelineChanged().build();
TestAnalyticsListener listener = runAnalyticsTest(fakeMediaSource, actionSchedule);
Object periodUid = listener.lastReportedTimeline.getUidOfPeriod(/* periodIndex= */
0);
EventWindowAndPeriodId prerollAd = new EventWindowAndPeriodId(/* windowIndex= */
0, new MediaPeriodId(periodUid, /* adGroupIndex= */
0, /* adIndexInAdGroup= */
0, /* windowSequenceNumber= */
0));
EventWindowAndPeriodId midrollAd = new EventWindowAndPeriodId(/* windowIndex= */
0, new MediaPeriodId(periodUid, /* adGroupIndex= */
1, /* adIndexInAdGroup= */
0, /* windowSequenceNumber= */
0));
EventWindowAndPeriodId postrollAd = new EventWindowAndPeriodId(/* windowIndex= */
0, new MediaPeriodId(periodUid, /* adGroupIndex= */
2, /* adIndexInAdGroup= */
0, /* windowSequenceNumber= */
0));
EventWindowAndPeriodId contentAfterPreroll = new EventWindowAndPeriodId(/* windowIndex= */
0, new MediaPeriodId(periodUid, /* windowSequenceNumber= */
0, /* nextAdGroupIndex= */
1));
EventWindowAndPeriodId contentAfterMidroll = new EventWindowAndPeriodId(/* windowIndex= */
0, new MediaPeriodId(periodUid, /* windowSequenceNumber= */
0, /* nextAdGroupIndex= */
2));
EventWindowAndPeriodId contentAfterPostroll = new EventWindowAndPeriodId(/* windowIndex= */
0, new MediaPeriodId(periodUid, /* windowSequenceNumber= */
0, /* nextAdGroupIndex= */
C.INDEX_UNSET));
assertThat(listener.getEvents(EVENT_PLAYER_STATE_CHANGED)).containsExactly(WINDOW_0, /* setPlayWhenReady=true */
WINDOW_0, /* setPlayWhenReady=false */
WINDOW_0, /* BUFFERING */
prerollAd, /* READY */
prerollAd, /* setPlayWhenReady=true */
contentAfterPreroll, /* setPlayWhenReady=false */
contentAfterPreroll, /* setPlayWhenReady=true */
contentAfterMidroll, /* setPlayWhenReady=false */
contentAfterMidroll, /* setPlayWhenReady=true */
contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_TIMELINE_CHANGED)).containsExactly(WINDOW_0, /* PLAYLIST_CHANGED */
prerollAd, /* SOURCE_UPDATE (initial) */
contentAfterPreroll, /* SOURCE_UPDATE (played preroll) */
contentAfterMidroll, /* SOURCE_UPDATE (played midroll) */
contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_POSITION_DISCONTINUITY)).containsExactly(contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_IS_LOADING_CHANGED)).containsExactly(prerollAd, prerollAd).inOrder();
assertThat(listener.getEvents(EVENT_TRACKS_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_LOAD_STARTED)).containsExactly(WINDOW_0, /* content manifest */
prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_LOAD_COMPLETED)).containsExactly(WINDOW_0, /* content manifest */
prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_DOWNSTREAM_FORMAT_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_DECODER_ENABLED)).containsExactly(prerollAd);
assertThat(listener.getEvents(EVENT_DECODER_INIT)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_DECODER_FORMAT_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_VIDEO_ENABLED)).containsExactly(prerollAd);
assertThat(listener.getEvents(EVENT_VIDEO_DECODER_INITIALIZED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_VIDEO_INPUT_FORMAT_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_DROPPED_VIDEO_FRAMES)).containsExactly(contentAfterPreroll, contentAfterMidroll, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_VIDEO_SIZE_CHANGED)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_RENDERED_FIRST_FRAME)).containsExactly(prerollAd, contentAfterPreroll, midrollAd, contentAfterMidroll, postrollAd, contentAfterPostroll).inOrder();
assertThat(listener.getEvents(EVENT_VIDEO_FRAME_PROCESSING_OFFSET)).containsExactly(contentAfterPreroll, contentAfterMidroll, contentAfterPostroll).inOrder();
listener.assertNoMoreEvents();
}
use of com.google.android.exoplayer2.source.MediaSource.MediaPeriodId 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();
}
Aggregations