use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method seekTo_cancelsSourceDiscontinuity_callsOnPositionDiscontinuity.
@Test
public void seekTo_cancelsSourceDiscontinuity_callsOnPositionDiscontinuity() throws Exception {
Timeline timeline1 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
1), new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
2));
final Timeline timeline2 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
1), new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
3));
final FakeMediaSource mediaSource = new FakeMediaSource(timeline1, ExoPlayerTestRunner.VIDEO_FORMAT);
ExoPlayer player = new TestExoPlayerBuilder(context).build();
Player.Listener listener = mock(Player.Listener.class);
player.addListener(listener);
player.setMediaSource(mediaSource);
player.prepare();
TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
1, /* positionMs= */
2000);
player.seekTo(/* mediaItemIndex= */
1, /* positionMs= */
2122);
// This causes a DISCONTINUITY_REASON_REMOVE between pending operations that needs to be
// cancelled by the seek below.
mediaSource.setNewSourceInfo(timeline2);
player.play();
player.seekTo(/* mediaItemIndex= */
0, /* positionMs= */
2222);
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
ArgumentCaptor<Player.PositionInfo> oldPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
ArgumentCaptor<Player.PositionInfo> newPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
InOrder inOrder = inOrder(listener);
inOrder.verify(listener).onPositionDiscontinuity(any(), any(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(listener, times(2)).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_SEEK));
inOrder.verify(listener).onPositionDiscontinuity(any(), any(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(listener, never()).onPositionDiscontinuity(any(), any(), anyInt());
List<Player.PositionInfo> oldPositions = oldPosition.getAllValues();
List<Player.PositionInfo> newPositions = newPosition.getAllValues();
// First seek
assertThat(oldPositions.get(0).mediaItemIndex).isEqualTo(1);
assertThat(oldPositions.get(0).positionMs).isIn(Range.closed(1980L, 2000L));
assertThat(oldPositions.get(0).contentPositionMs).isIn(Range.closed(1980L, 2000L));
assertThat(newPositions.get(0).mediaItemIndex).isEqualTo(1);
assertThat(newPositions.get(0).positionMs).isEqualTo(2122);
assertThat(newPositions.get(0).contentPositionMs).isEqualTo(2122);
// Second seek.
assertThat(oldPositions.get(1).mediaItemIndex).isEqualTo(1);
assertThat(oldPositions.get(1).positionMs).isEqualTo(2122);
assertThat(oldPositions.get(1).contentPositionMs).isEqualTo(2122);
assertThat(newPositions.get(1).mediaItemIndex).isEqualTo(0);
assertThat(newPositions.get(1).positionMs).isEqualTo(2222);
assertThat(newPositions.get(1).contentPositionMs).isEqualTo(2222);
player.release();
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ExoPlayerTest method setShuffleModeEnabled_notifiesAvailableCommandsChanged.
@Test
public void setShuffleModeEnabled_notifiesAvailableCommandsChanged() {
Player.Commands commandsWithSeekToPreviousWindow = createWithDefaultCommands(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM);
Player.Commands commandsWithSeekToNextWindow = createWithDefaultCommands(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, COMMAND_SEEK_TO_NEXT);
Player.Listener mockListener = mock(Player.Listener.class);
ExoPlayer player = new TestExoPlayerBuilder(context).build();
player.addListener(mockListener);
MediaSource mediaSource = new ConcatenatingMediaSource(false, new FakeShuffleOrder(/* length= */
2), new FakeMediaSource(), new FakeMediaSource());
player.addMediaSource(mediaSource);
verify(mockListener).onAvailableCommandsChanged(commandsWithSeekToNextWindow);
// Check that there were no other calls to onAvailableCommandsChanged.
verify(mockListener).onAvailableCommandsChanged(any());
player.setShuffleModeEnabled(true);
verify(mockListener).onAvailableCommandsChanged(commandsWithSeekToPreviousWindow);
verify(mockListener, times(2)).onAvailableCommandsChanged(any());
}
use of com.google.android.exoplayer2.source.MediaSource 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.source.MediaSource in project ExoPlayer by google.
the class SilenceMediaSourceTest method newInstance_setsMediaItem.
@Test
public void newInstance_setsMediaItem() {
SilenceMediaSource mediaSource = new SilenceMediaSource(1_000_000);
MediaItem mediaItem = mediaSource.getMediaItem();
assertThat(mediaItem).isNotNull();
assertThat(mediaItem.mediaId).isEqualTo(SilenceMediaSource.MEDIA_ID);
assertThat(mediaSource.getMediaItem().localConfiguration.uri).isEqualTo(Uri.EMPTY);
assertThat(mediaItem.localConfiguration.mimeType).isEqualTo(MimeTypes.AUDIO_RAW);
}
use of com.google.android.exoplayer2.source.MediaSource in project ExoPlayer by google.
the class ServerSideAdInsertionMediaSourceTest method timeline_containsAdsDefinedInAdPlaybackState.
@Test
public void timeline_containsAdsDefinedInAdPlaybackState() throws Exception {
FakeTimeline wrappedTimeline = new FakeTimeline(new FakeTimeline.TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
true, /* isLive= */
true, /* isPlaceholder= */
false, /* durationUs= */
10_000_000, /* defaultPositionUs= */
3_000_000, /* windowOffsetInFirstPeriodUs= */
42_000_000L, AdPlaybackState.NONE));
ServerSideAdInsertionMediaSource mediaSource = new ServerSideAdInsertionMediaSource(new FakeMediaSource(wrappedTimeline), /* adPlaybackStateUpdater= */
null);
// Test with one ad group before the window, and the window starting within the second ad group.
AdPlaybackState adPlaybackState = new AdPlaybackState(/* adsId= */
new Object(), /* adGroupTimesUs...= */
15_000_000, 41_500_000, 42_200_000).withIsServerSideInserted(/* adGroupIndex= */
0, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
1, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withAdCount(/* adGroupIndex= */
0, /* adCount= */
1).withAdCount(/* adGroupIndex= */
1, /* adCount= */
2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdDurationsUs(/* adGroupIndex= */
0, /* adDurationsUs...= */
500_000).withAdDurationsUs(/* adGroupIndex= */
1, /* adDurationsUs...= */
300_000, 100_000).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
400_000).withContentResumeOffsetUs(/* adGroupIndex= */
0, /* contentResumeOffsetUs= */
100_000).withContentResumeOffsetUs(/* adGroupIndex= */
1, /* contentResumeOffsetUs= */
400_000).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
200_000);
AtomicReference<Timeline> timelineReference = new AtomicReference<>();
mediaSource.setAdPlaybackStates(ImmutableMap.of(new Pair<>(0, 0), adPlaybackState));
mediaSource.prepareSource((source, timeline) -> timelineReference.set(timeline), /* mediaTransferListener= */
null, PlayerId.UNSET);
runMainLooperUntil(() -> timelineReference.get() != null);
Timeline timeline = timelineReference.get();
assertThat(timeline.getPeriodCount()).isEqualTo(1);
Timeline.Period period = timeline.getPeriod(/* periodIndex= */
0, new Timeline.Period());
assertThat(period.getAdGroupCount()).isEqualTo(3);
assertThat(period.getAdCountInAdGroup(/* adGroupIndex= */
0)).isEqualTo(1);
assertThat(period.getAdCountInAdGroup(/* adGroupIndex= */
1)).isEqualTo(2);
assertThat(period.getAdCountInAdGroup(/* adGroupIndex= */
2)).isEqualTo(1);
assertThat(period.getAdGroupTimeUs(/* adGroupIndex= */
0)).isEqualTo(15_000_000);
assertThat(period.getAdGroupTimeUs(/* adGroupIndex= */
1)).isEqualTo(41_500_000);
assertThat(period.getAdGroupTimeUs(/* adGroupIndex= */
2)).isEqualTo(42_200_000);
assertThat(period.getAdDurationUs(/* adGroupIndex= */
0, /* adIndexInAdGroup= */
0)).isEqualTo(500_000);
assertThat(period.getAdDurationUs(/* adGroupIndex= */
1, /* adIndexInAdGroup= */
0)).isEqualTo(300_000);
assertThat(period.getAdDurationUs(/* adGroupIndex= */
1, /* adIndexInAdGroup= */
1)).isEqualTo(100_000);
assertThat(period.getAdDurationUs(/* adGroupIndex= */
2, /* adIndexInAdGroup= */
0)).isEqualTo(400_000);
assertThat(period.getContentResumeOffsetUs(/* adGroupIndex= */
0)).isEqualTo(100_000);
assertThat(period.getContentResumeOffsetUs(/* adGroupIndex= */
1)).isEqualTo(400_000);
assertThat(period.getContentResumeOffsetUs(/* adGroupIndex= */
2)).isEqualTo(200_000);
// windowDurationUs + windowOffsetInFirstPeriodUs - sum(adDurations) + sum(contentResumeOffsets)
assertThat(period.getDurationUs()).isEqualTo(51_400_000);
// positionInWindowUs + sum(adDurationsBeforeWindow) - sum(contentResumeOffsetsBeforeWindow)
assertThat(period.getPositionInWindowUs()).isEqualTo(-41_600_000);
Timeline.Window window = timeline.getWindow(/* windowIndex= */
0, new Timeline.Window());
assertThat(window.positionInFirstPeriodUs).isEqualTo(41_600_000);
// windowDurationUs - sum(adDurationsInWindow) + sum(applicableContentResumeOffsetUs)
assertThat(window.durationUs).isEqualTo(9_800_000);
}
Aggregations