Search in sources :

Example 11 with AdsMediaSource

use of androidx.media3.exoplayer.source.ads.AdsMediaSource in project media by androidx.

the class ImaAdsLoaderTest method playback_withAdNotPreloadingAfterTimeout_hasErrorAdGroup.

@Test
public void playback_withAdNotPreloadingAfterTimeout_hasErrorAdGroup() {
    // Simulate an ad at 2 seconds.
    long adGroupPositionInWindowUs = 2 * C.MICROS_PER_SECOND;
    long adGroupTimeUs = adGroupPositionInWindowUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    ImmutableList<Float> cuePoints = ImmutableList.of((float) adGroupTimeUs / C.MICROS_PER_SECOND);
    when(mockAdsManager.getAdCuePoints()).thenReturn(cuePoints);
    // Advance playback to just before the midroll and simulate buffering.
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, Util.usToMs(adGroupPositionInWindowUs));
    fakePlayer.setState(Player.STATE_BUFFERING, /* playWhenReady= */
    true);
    // Advance past the timeout and simulate polling content progress.
    ShadowSystemClock.advanceBy(Duration.ofSeconds(5));
    contentProgressProvider.getContentProgress();
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(new AdPlaybackState(TEST_ADS_ID, getAdGroupTimesUsForCuePoints(cuePoints)).withContentDurationUs(CONTENT_PERIOD_DURATION_US).withAdDurationsUs(new long[][] { { TEST_AD_DURATION_US } }).withAdCount(/* adGroupIndex= */
    0, /* adCount= */
    1).withAdLoadError(/* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0));
}
Also used : AdPlaybackState(androidx.media3.common.AdPlaybackState) Test(org.junit.Test)

Example 12 with AdsMediaSource

use of androidx.media3.exoplayer.source.ads.AdsMediaSource in project media by androidx.

the class ExoPlayerTest method seekTo_whilePlayingAd_correctMasking.

@Test
public void seekTo_whilePlayingAd_correctMasking() throws Exception {
    long contentDurationMs = 10_000;
    long adDurationMs = 4_000;
    AdPlaybackState adPlaybackState = new AdPlaybackState(/* adsId= */
    new Object(), /* adGroupTimesUs...= */
    0);
    adPlaybackState = adPlaybackState.withAdCount(/* adGroupIndex= */
    0, /* adCount= */
    1);
    adPlaybackState = adPlaybackState.withAdUri(/* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, Uri.EMPTY);
    long[][] durationsUs = new long[1][];
    durationsUs[0] = new long[] { Util.msToUs(adDurationMs) };
    adPlaybackState = adPlaybackState.withAdDurationsUs(durationsUs);
    Timeline adTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    Util.msToUs(contentDurationMs), adPlaybackState));
    FakeMediaSource adsMediaSource = new FakeMediaSource(adTimeline);
    int[] mediaItemIndex = new int[] { C.INDEX_UNSET, C.INDEX_UNSET };
    long[] positionMs = new long[] { C.TIME_UNSET, C.TIME_UNSET };
    long[] bufferedPositionMs = new long[] { C.TIME_UNSET, C.TIME_UNSET };
    long[] totalBufferedDurationMs = new long[] { C.TIME_UNSET, C.TIME_UNSET };
    boolean[] isPlayingAd = new boolean[2];
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).waitForIsLoading(true).waitForIsLoading(false).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            player.seekTo(/* mediaItemIndex= */
            0, /* positionMs= */
            8000);
            mediaItemIndex[0] = player.getCurrentMediaItemIndex();
            isPlayingAd[0] = player.isPlayingAd();
            positionMs[0] = player.getCurrentPosition();
            bufferedPositionMs[0] = player.getBufferedPosition();
            totalBufferedDurationMs[0] = player.getTotalBufferedDuration();
        }
    }).waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            mediaItemIndex[1] = player.getCurrentMediaItemIndex();
            isPlayingAd[1] = player.isPlayingAd();
            positionMs[1] = player.getCurrentPosition();
            bufferedPositionMs[1] = player.getBufferedPosition();
            totalBufferedDurationMs[1] = player.getTotalBufferedDuration();
        }
    }).stop().build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(adsMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertThat(mediaItemIndex[0]).isEqualTo(0);
    assertThat(isPlayingAd[0]).isTrue();
    assertThat(positionMs[0]).isEqualTo(0);
    assertThat(bufferedPositionMs[0]).isEqualTo(adDurationMs);
    assertThat(totalBufferedDurationMs[0]).isEqualTo(adDurationMs);
    assertThat(mediaItemIndex[1]).isEqualTo(0);
    assertThat(isPlayingAd[1]).isTrue();
    assertThat(positionMs[1]).isEqualTo(0);
    assertThat(bufferedPositionMs[1]).isEqualTo(adDurationMs);
    assertThat(totalBufferedDurationMs[1]).isEqualTo(adDurationMs);
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) AdPlaybackState(androidx.media3.common.AdPlaybackState) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) Test(org.junit.Test)

Example 13 with AdsMediaSource

use of androidx.media3.exoplayer.source.ads.AdsMediaSource in project media by androidx.

the class AdsMediaSourceTest method createPeriod_preparesChildAdMediaSourceAndRefreshesSourceInfoWithAdMediaSourceInfo.

@Test
public void createPeriod_preparesChildAdMediaSourceAndRefreshesSourceInfoWithAdMediaSourceInfo() {
    contentMediaSource.setNewSourceInfo(CONTENT_TIMELINE);
    adsMediaSource.createPeriod(new MediaPeriodId(CONTENT_PERIOD_UID, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    0), mock(Allocator.class), /* startPositionUs= */
    0);
    prerollAdMediaSource.setNewSourceInfo(PREROLL_AD_TIMELINE);
    shadowOf(Looper.getMainLooper()).idle();
    verify(mockMediaSourceCaller).onSourceInfoRefreshed(adsMediaSource, new SinglePeriodAdTimeline(CONTENT_TIMELINE, AD_PLAYBACK_STATE.withAdDurationsUs(new long[][] { { PREROLL_AD_DURATION_US } })));
}
Also used : Allocator(androidx.media3.exoplayer.upstream.Allocator) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Example 14 with AdsMediaSource

use of androidx.media3.exoplayer.source.ads.AdsMediaSource in project media by androidx.

the class AdsMediaSourceTest method createPeriod_preparesChildAdMediaSourceAndRefreshesSourceInfo.

@Test
public void createPeriod_preparesChildAdMediaSourceAndRefreshesSourceInfo() {
    contentMediaSource.setNewSourceInfo(CONTENT_TIMELINE);
    adsMediaSource.createPeriod(new MediaPeriodId(CONTENT_PERIOD_UID, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    0), mock(Allocator.class), /* startPositionUs= */
    0);
    shadowOf(Looper.getMainLooper()).idle();
    assertThat(prerollAdMediaSource.isPrepared()).isTrue();
    verify(mockMediaSourceCaller).onSourceInfoRefreshed(adsMediaSource, new SinglePeriodAdTimeline(CONTENT_TIMELINE, AD_PLAYBACK_STATE));
}
Also used : Allocator(androidx.media3.exoplayer.upstream.Allocator) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Example 15 with AdsMediaSource

use of androidx.media3.exoplayer.source.ads.AdsMediaSource in project media by androidx.

the class AdsMediaSourceTest method setUp.

@Before
public void setUp() {
    // Set up content and ad media sources, passing a null timeline so tests can simulate setting it
    // later.
    contentMediaSource = new FakeMediaSource(/* timeline= */
    null);
    prerollAdMediaSource = new FakeMediaSource(/* timeline= */
    null);
    MediaSource.Factory adMediaSourceFactory = mock(MediaSource.Factory.class);
    when(adMediaSourceFactory.createMediaSource(any(MediaItem.class))).thenReturn(prerollAdMediaSource);
    // Prepare the AdsMediaSource and capture its ads loader listener.
    AdsLoader mockAdsLoader = mock(AdsLoader.class);
    AdViewProvider mockAdViewProvider = mock(AdViewProvider.class);
    ArgumentCaptor<EventListener> eventListenerArgumentCaptor = ArgumentCaptor.forClass(AdsLoader.EventListener.class);
    adsMediaSource = new AdsMediaSource(contentMediaSource, TEST_ADS_DATA_SPEC, TEST_ADS_ID, adMediaSourceFactory, mockAdsLoader, mockAdViewProvider);
    adsMediaSource.prepareSource(mockMediaSourceCaller, /* mediaTransferListener= */
    null, PlayerId.UNSET);
    shadowOf(Looper.getMainLooper()).idle();
    verify(mockAdsLoader).start(eq(adsMediaSource), eq(TEST_ADS_DATA_SPEC), eq(TEST_ADS_ID), eq(mockAdViewProvider), eventListenerArgumentCaptor.capture());
    // Simulate loading a preroll ad.
    AdsLoader.EventListener adsLoaderEventListener = eventListenerArgumentCaptor.getValue();
    adsLoaderEventListener.onAdPlaybackState(AD_PLAYBACK_STATE);
    shadowOf(Looper.getMainLooper()).idle();
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaItem(androidx.media3.common.MediaItem) AdViewProvider(androidx.media3.common.AdViewProvider) EventListener(androidx.media3.exoplayer.source.ads.AdsLoader.EventListener) EventListener(androidx.media3.exoplayer.source.ads.AdsLoader.EventListener) Before(org.junit.Before)

Aggregations

Test (org.junit.Test)28 AdPlaybackState (androidx.media3.common.AdPlaybackState)19 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)16 AdsMediaSource (androidx.media3.exoplayer.source.ads.AdsMediaSource)13 DefaultMediaSourceFactory (androidx.media3.exoplayer.source.DefaultMediaSourceFactory)12 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)7 Context (android.content.Context)3 AdViewProvider (androidx.media3.common.AdViewProvider)3 Timeline (androidx.media3.common.Timeline)3 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)3 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)3 FakeTimeline (androidx.media3.test.utils.FakeTimeline)3 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)3 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)3 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)3 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)3 AdEvent (com.google.ads.interactivemedia.v3.api.AdEvent)3 Nullable (androidx.annotation.Nullable)2 MediaItem (androidx.media3.common.MediaItem)2 DataSpec (androidx.media3.datasource.DataSpec)2