Search in sources :

Example 16 with AdViewProvider

use of com.google.android.exoplayer2.ui.AdViewProvider in project ExoPlayer by google.

the class ImaAdsLoaderTest method resumePlaybackAtSecondMidroll_skipsFirstMidroll.

@Test
public void resumePlaybackAtSecondMidroll_skipsFirstMidroll() {
    long firstMidrollWindowTimeUs = 2 * C.MICROS_PER_SECOND;
    long firstMidrollPeriodTimeUs = firstMidrollWindowTimeUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    long secondMidrollWindowTimeUs = 4 * C.MICROS_PER_SECOND;
    long secondMidrollPeriodTimeUs = secondMidrollWindowTimeUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    ImmutableList<Float> cuePoints = ImmutableList.of((float) firstMidrollPeriodTimeUs / C.MICROS_PER_SECOND, (float) secondMidrollPeriodTimeUs / C.MICROS_PER_SECOND);
    when(mockAdsManager.getAdCuePoints()).thenReturn(cuePoints);
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, Util.usToMs(secondMidrollWindowTimeUs));
    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 = (firstMidrollPeriodTimeUs + secondMidrollPeriodTimeUs) / 2d;
    assertThat(playAdsAfterTimeCaptor.getValue()).isWithin(0.1).of(expectedPlayAdsAfterTimeUs / C.MICROS_PER_SECOND);
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(new AdPlaybackState(TEST_ADS_ID, getAdGroupTimesUsForCuePoints(cuePoints)).withContentDurationUs(CONTENT_PERIOD_DURATION_US).withSkippedAdGroup(/* adGroupIndex= */
    0));
}
Also used : AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) Test(org.junit.Test)

Example 17 with AdViewProvider

use of com.google.android.exoplayer2.ui.AdViewProvider in project ExoPlayer by google.

the class AdTagLoader method addListenerWithAdView.

/**
 * Starts passing events from this instance (including any pending ad playback state) and
 * registers obstructions.
 */
public void addListenerWithAdView(EventListener eventListener, AdViewProvider adViewProvider) {
    boolean isStarted = !eventListeners.isEmpty();
    eventListeners.add(eventListener);
    if (isStarted) {
        if (!AdPlaybackState.NONE.equals(adPlaybackState)) {
            // Pass the existing ad playback state to the new listener.
            eventListener.onAdPlaybackState(adPlaybackState);
        }
        return;
    }
    lastVolumePercent = 0;
    lastAdProgress = VideoProgressUpdate.VIDEO_TIME_NOT_READY;
    lastContentProgress = VideoProgressUpdate.VIDEO_TIME_NOT_READY;
    maybeNotifyPendingAdLoadError();
    if (!AdPlaybackState.NONE.equals(adPlaybackState)) {
        // Pass the ad playback state to the player, and resume ads if necessary.
        eventListener.onAdPlaybackState(adPlaybackState);
    } else if (adsManager != null) {
        adPlaybackState = new AdPlaybackState(adsId, getAdGroupTimesUsForCuePoints(adsManager.getAdCuePoints()));
        updateAdPlaybackState();
    }
    for (AdOverlayInfo overlayInfo : adViewProvider.getAdOverlayInfos()) {
        adDisplayContainer.registerFriendlyObstruction(imaFactory.createFriendlyObstruction(overlayInfo.view, ImaUtil.getFriendlyObstructionPurpose(overlayInfo.purpose), overlayInfo.reasonDetail));
    }
}
Also used : AdOverlayInfo(com.google.android.exoplayer2.ui.AdOverlayInfo) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState)

Example 18 with AdViewProvider

use of com.google.android.exoplayer2.ui.AdViewProvider in project ExoPlayer by google.

the class ImaAdsLoaderTest method resumePlaybackAtSecondMidroll_withoutPlayAdBeforeStartPosition_skipsFirstMidroll.

@Test
public void resumePlaybackAtSecondMidroll_withoutPlayAdBeforeStartPosition_skipsFirstMidroll() {
    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 firstMidrollWindowTimeUs = 2 * C.MICROS_PER_SECOND;
    long firstMidrollPeriodTimeUs = firstMidrollWindowTimeUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    long secondMidrollWindowTimeUs = 4 * C.MICROS_PER_SECOND;
    long secondMidrollPeriodTimeUs = secondMidrollWindowTimeUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    ImmutableList<Float> cuePoints = ImmutableList.of((float) firstMidrollPeriodTimeUs / C.MICROS_PER_SECOND, (float) secondMidrollPeriodTimeUs / C.MICROS_PER_SECOND);
    when(mockAdsManager.getAdCuePoints()).thenReturn(cuePoints);
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, Util.usToMs(secondMidrollWindowTimeUs));
    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 = (firstMidrollPeriodTimeUs + secondMidrollPeriodTimeUs) / 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)).withContentDurationUs(CONTENT_PERIOD_DURATION_US).withSkippedAdGroup(/* adGroupIndex= */
    0));
}
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 19 with AdViewProvider

use of com.google.android.exoplayer2.ui.AdViewProvider in project ExoPlayer by google.

the class ImaAdsLoaderTest method playbackWithTwoAdsMediaSourcesAndMatchingAdsIds_hasMatchingAdPlaybackState.

@Test
public void playbackWithTwoAdsMediaSourcesAndMatchingAdsIds_hasMatchingAdPlaybackState() {
    AdsMediaSource secondAdsMediaSource = new AdsMediaSource(new FakeMediaSource(CONTENT_TIMELINE), TEST_DATA_SPEC, TEST_ADS_ID, new DefaultMediaSourceFactory((Context) getApplicationContext()), imaAdsLoader, adViewProvider);
    timelineWindowDefinitions = new TimelineWindowDefinition[] { getInitialTimelineWindowDefinition(TEST_ADS_ID), getInitialTimelineWindowDefinition(TEST_ADS_ID) };
    TestAdsLoaderListener secondAdsLoaderListener = new TestAdsLoaderListener(/* periodIndex= */
    1);
    // Load and play the preroll ad then content.
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    adEventListener.onAdEvent(getAdEvent(AdEventType.LOADED, mockPrerollSingleAd));
    videoAdPlayer.loadAd(TEST_AD_MEDIA_INFO, mockAdPodInfo);
    imaAdsLoader.start(secondAdsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, secondAdsLoaderListener);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_PAUSE_REQUESTED, mockPrerollSingleAd));
    videoAdPlayer.playAd(TEST_AD_MEDIA_INFO);
    fakePlayer.setPlayingAdPosition(/* periodIndex= */
    0, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* positionMs= */
    0, /* contentPositionMs= */
    0);
    fakePlayer.setState(Player.STATE_READY, /* playWhenReady= */
    true);
    adEventListener.onAdEvent(getAdEvent(AdEventType.STARTED, mockPrerollSingleAd));
    adEventListener.onAdEvent(getAdEvent(AdEventType.FIRST_QUARTILE, mockPrerollSingleAd));
    adEventListener.onAdEvent(getAdEvent(AdEventType.MIDPOINT, mockPrerollSingleAd));
    adEventListener.onAdEvent(getAdEvent(AdEventType.THIRD_QUARTILE, mockPrerollSingleAd));
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, /* positionMs= */
    0);
    videoAdPlayer.stopAd(TEST_AD_MEDIA_INFO);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */
    null));
    // Verify that the ad playback states for the two periods match.
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(getAdPlaybackState(/* periodIndex= */
    1));
}
Also used : ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) Context(android.content.Context) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) Test(org.junit.Test)

Example 20 with AdViewProvider

use of com.google.android.exoplayer2.ui.AdViewProvider in project ExoPlayer by google.

the class ImaAdsLoaderTest method buildWithEnableContinuousPlayback_setsAdsRequestProperty.

@Test
public void buildWithEnableContinuousPlayback_setsAdsRequestProperty() {
    imaAdsLoader = new ImaAdsLoader.Builder(getApplicationContext()).setEnableContinuousPlayback(true).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);
    when(mockAdsManager.getAdCuePoints()).thenReturn(PREROLL_CUE_POINTS_SECONDS);
    imaAdsLoader.setSupportedContentTypes(C.TYPE_OTHER);
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    verify(mockAdsRequest).setContinuousPlayback(true);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) DefaultMediaSourceFactory(com.google.android.exoplayer2.source.DefaultMediaSourceFactory) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)17 AdsMediaSource (com.google.android.exoplayer2.source.ads.AdsMediaSource)13 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)13 DefaultMediaSourceFactory (com.google.android.exoplayer2.source.DefaultMediaSourceFactory)12 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)7 Context (android.content.Context)3 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)3 AdEvent (com.google.ads.interactivemedia.v3.api.AdEvent)3 AdOverlayInfo (com.google.android.exoplayer2.ui.AdOverlayInfo)3 AdViewProvider (com.google.android.exoplayer2.ui.AdViewProvider)3 Nullable (androidx.annotation.Nullable)2 MediaItem (com.google.android.exoplayer2.MediaItem)2 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)2 Before (org.junit.Before)2 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 FrameLayout (android.widget.FrameLayout)1 AdPodInfo (com.google.ads.interactivemedia.v3.api.AdPodInfo)1 CuePoint (com.google.ads.interactivemedia.v3.api.CuePoint)1