Search in sources :

Example 91 with MediaSource

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();
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Allocator(com.google.android.exoplayer2.upstream.Allocator) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) FakeRenderer(com.google.android.exoplayer2.testutil.FakeRenderer) FakeMediaPeriod(com.google.android.exoplayer2.testutil.FakeMediaPeriod) ServerSideAdInsertionMediaSource(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource) MaskingMediaSource(com.google.android.exoplayer2.source.MaskingMediaSource) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) CompositeMediaSource(com.google.android.exoplayer2.source.CompositeMediaSource) ClippingMediaSource(com.google.android.exoplayer2.source.ClippingMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 92 with MediaSource

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));
}
Also used : ShuffleOrder(com.google.android.exoplayer2.source.ShuffleOrder) FakeShuffleOrder(com.google.android.exoplayer2.testutil.FakeShuffleOrder) MediaSource(com.google.android.exoplayer2.source.MediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Test(org.junit.Test)

Example 93 with MediaSource

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();
}
Also used : ShuffleOrder(com.google.android.exoplayer2.source.ShuffleOrder) FakeShuffleOrder(com.google.android.exoplayer2.testutil.FakeShuffleOrder) MediaSource(com.google.android.exoplayer2.source.MediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Test(org.junit.Test)

Example 94 with MediaSource

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);
}
Also used : NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ServerSideAdInsertionMediaSource(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource) MaskingMediaSource(com.google.android.exoplayer2.source.MaskingMediaSource) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) CompositeMediaSource(com.google.android.exoplayer2.source.CompositeMediaSource) ClippingMediaSource(com.google.android.exoplayer2.source.ClippingMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 95 with MediaSource

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);
}
Also used : NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)182 MediaSource (com.google.android.exoplayer2.source.MediaSource)122 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)117 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)74 MediaItem (com.google.android.exoplayer2.MediaItem)72 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)70 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)70 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)60 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)52 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)51 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)51 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)50 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)50 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)45 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)39 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)39 Timeline (com.google.android.exoplayer2.Timeline)35 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)34 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)28 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)25