Search in sources :

Example 11 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource in project ExoPlayer by google.

the class ImaAdsLoaderTest method resumePlaybackBeforeMidroll_withoutPlayAdBeforeStartPosition_skipsPreroll.

@Test
public void resumePlaybackBeforeMidroll_withoutPlayAdBeforeStartPosition_skipsPreroll() {
    imaAdsLoader = new ImaAdsLoader.Builder(getApplicationContext()).setPlayAdBeforeStartPosition(false).setImaFactory(mockImaFactory).setImaSdkSettings(mockImaSdkSettings).build();
    imaAdsLoader.setPlayer(fakePlayer);
    adsMediaSource = new AdsMediaSource(new FakeMediaSource(CONTENT_TIMELINE), TEST_DATA_SPEC, TEST_ADS_ID, new DefaultMediaSourceFactory((Context) getApplicationContext()), imaAdsLoader, adViewProvider);
    long midrollWindowTimeUs = 2 * C.MICROS_PER_SECOND;
    long midrollPeriodTimeUs = midrollWindowTimeUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    ImmutableList<Float> cuePoints = ImmutableList.of(0f, (float) midrollPeriodTimeUs / C.MICROS_PER_SECOND);
    when(mockAdsManager.getAdCuePoints()).thenReturn(cuePoints);
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, Util.usToMs(midrollWindowTimeUs) - 1_000);
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    ArgumentCaptor<Double> playAdsAfterTimeCaptor = ArgumentCaptor.forClass(Double.class);
    verify(mockAdsRenderingSettings).setPlayAdsAfterTime(playAdsAfterTimeCaptor.capture());
    double expectedPlayAdsAfterTimeUs = midrollPeriodTimeUs / 2d;
    assertThat(playAdsAfterTimeCaptor.getValue()).isWithin(0.1d).of(expectedPlayAdsAfterTimeUs / C.MICROS_PER_SECOND);
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(new AdPlaybackState(TEST_ADS_ID, getAdGroupTimesUsForCuePoints(cuePoints)).withSkippedAdGroup(/* adGroupIndex= */
    0).withContentDurationUs(CONTENT_PERIOD_DURATION_US));
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) Test(org.junit.Test)

Example 12 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource in project ExoPlayer by google.

the class ImaAdsLoaderTest method requestAdTagWithDataScheme_requestsWithAdsResponse.

@Test
public void requestAdTagWithDataScheme_requestsWithAdsResponse() throws Exception {
    String adsResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<VAST xmlns:xsi=\"https://www.w3.org/2001/XMLSchema-instance\"" + " xsi:noNamespaceSchemaLocation=\"vast.xsd\" version=\"2.0\">\n" + "  <Ad id=\"17180293\">\n" + "    <InLine></InLine>\n" + "  </Ad>\n" + "</VAST>";
    DataSpec adDataSpec = new DataSpec(Util.getDataUriForString("text/xml", adsResponse));
    imaAdsLoader.start(adsMediaSource, adDataSpec, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    verify(mockAdsRequest).setAdsResponse(adsResponse);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 13 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource in project ExoPlayer by google.

the class ImaAdsLoader method start.

@Override
public void start(AdsMediaSource adsMediaSource, DataSpec adTagDataSpec, Object adsId, AdViewProvider adViewProvider, EventListener eventListener) {
    checkState(wasSetPlayerCalled, "Set player using adsLoader.setPlayer before preparing the player.");
    if (adTagLoaderByAdsMediaSource.isEmpty()) {
        player = nextPlayer;
        @Nullable Player player = this.player;
        if (player == null) {
            return;
        }
        player.addListener(playerListener);
    }
    @Nullable AdTagLoader adTagLoader = adTagLoaderByAdsId.get(adsId);
    if (adTagLoader == null) {
        requestAds(adTagDataSpec, adsId, adViewProvider.getAdViewGroup());
        adTagLoader = adTagLoaderByAdsId.get(adsId);
    }
    adTagLoaderByAdsMediaSource.put(adsMediaSource, checkNotNull(adTagLoader));
    adTagLoader.addListenerWithAdView(eventListener, adViewProvider);
    maybeUpdateCurrentAdTagLoader();
}
Also used : Player(com.google.android.exoplayer2.Player) VideoAdPlayer(com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer) Nullable(androidx.annotation.Nullable)

Example 14 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource in project ExoPlayer by google.

the class ExoPlayerTest method seekTo_whilePlayingAd_doesntBlockFutureUpdates.

// https://github.com/google/ExoPlayer/issues/8349
@Test
public void seekTo_whilePlayingAd_doesntBlockFutureUpdates() throws Exception {
    long contentDurationMs = 10_000;
    long adDurationMs = 4_000;
    AdPlaybackState adPlaybackState = new AdPlaybackState(/* adsId= */
    new Object(), /* adGroupTimesUs...= */
    0).withAdCount(/* adGroupIndex= */
    0, /* adCount= */
    1).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);
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    player.setMediaSource(adsMediaSource);
    player.pause();
    player.prepare();
    runUntilPlaybackState(player, Player.STATE_READY);
    player.seekTo(0, 8000);
    player.play();
    // This times out if playback info updates after the seek are blocked.
    runUntilPlaybackState(player, Player.STATE_ENDED);
}
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) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 15 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource in project ExoPlayer by google.

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(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)19 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)16 AdsMediaSource (com.google.android.exoplayer2.source.ads.AdsMediaSource)15 DefaultMediaSourceFactory (com.google.android.exoplayer2.source.DefaultMediaSourceFactory)12 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)7 MediaItem (com.google.android.exoplayer2.MediaItem)4 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)4 Context (android.content.Context)3 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)3 AdEvent (com.google.ads.interactivemedia.v3.api.AdEvent)3 MediaSource (com.google.android.exoplayer2.source.MediaSource)3 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)3 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)3 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)3 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)3 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)3 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)3 AdViewProvider (com.google.android.exoplayer2.ui.AdViewProvider)3 Uri (android.net.Uri)2