Search in sources :

Example 6 with AdPlaybackState

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

the class ImaAdsLoaderTest method loadAd_withLargeAdCuePoint_updatesAdPlaybackStateWithLoadedAd.

@Test
public void loadAd_withLargeAdCuePoint_updatesAdPlaybackStateWithLoadedAd() {
    // Use a large enough value to test correct truncating of large cue points.
    float midrollTimeSecs = Float.MAX_VALUE;
    List<Float> cuePoints = ImmutableList.of(midrollTimeSecs);
    when(mockAdsManager.getAdCuePoints()).thenReturn(cuePoints);
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    videoAdPlayer.loadAd(TEST_AD_MEDIA_INFO, new AdPodInfo() {

        @Override
        public int getTotalAds() {
            return 1;
        }

        @Override
        public int getAdPosition() {
            return 1;
        }

        @Override
        public boolean isBumper() {
            return false;
        }

        @Override
        public double getMaxDuration() {
            return 0;
        }

        @Override
        public int getPodIndex() {
            return 0;
        }

        @Override
        public double getTimeOffset() {
            return midrollTimeSecs;
        }
    });
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(new AdPlaybackState(TEST_ADS_ID, getAdGroupTimesUsForCuePoints(cuePoints)).withContentDurationUs(CONTENT_PERIOD_DURATION_US).withAdCount(/* adGroupIndex= */
    0, /* adCount= */
    1).withAdUri(/* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, TEST_URI).withAdDurationsUs(new long[][] { { TEST_AD_DURATION_US } }));
}
Also used : AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) AdPodInfo(com.google.ads.interactivemedia.v3.api.AdPodInfo) Test(org.junit.Test)

Example 7 with AdPlaybackState

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

the class ImaAdsLoaderTest method resumePlaybackAfterMidroll_skipsPreroll.

@Test
public void resumePlaybackAfterMidroll_skipsPreroll() {
    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.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 8 with AdPlaybackState

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

the class ImaAdsLoaderTest method playback_withPrerollAd_marksAdAsPlayed.

@Test
public void playback_withPrerollAd_marksAdAsPlayed() {
    // Load the preroll ad.
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    adEventListener.onAdEvent(getAdEvent(AdEventType.LOADED, mockPrerollSingleAd));
    videoAdPlayer.loadAd(TEST_AD_MEDIA_INFO, mockAdPodInfo);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_PAUSE_REQUESTED, mockPrerollSingleAd));
    // Play the preroll ad.
    videoAdPlayer.playAd(TEST_AD_MEDIA_INFO);
    fakePlayer.setPlayingAdPosition(/* periodIndex= */
    0, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* position= */
    0, /* contentPosition= */
    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));
    // Play the content.
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, /* positionMs= */
    0);
    videoAdPlayer.stopAd(TEST_AD_MEDIA_INFO);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */
    null));
    // Verify that the preroll ad has been marked as played.
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(new AdPlaybackState(TEST_ADS_ID, /* adGroupTimesUs...= */
    0).withContentDurationUs(CONTENT_PERIOD_DURATION_US).withAdCount(/* adGroupIndex= */
    0, /* adCount= */
    1).withAdUri(/* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, TEST_URI).withAdDurationsUs(new long[][] { { TEST_AD_DURATION_US } }).withPlayedAd(/* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0).withAdResumePositionUs(/* adResumePositionUs= */
    0));
}
Also used : AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) Test(org.junit.Test)

Example 9 with AdPlaybackState

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

the class ImaAdsLoaderTest method playback_withPostrollFetchError_marksAdAsInErrorState.

@Test
public void playback_withPostrollFetchError_marksAdAsInErrorState() {
    AdEvent mockPostrollFetchErrorAdEvent = mock(AdEvent.class);
    when(mockPostrollFetchErrorAdEvent.getType()).thenReturn(AdEventType.AD_BREAK_FETCH_ERROR);
    when(mockPostrollFetchErrorAdEvent.getAdData()).thenReturn(ImmutableMap.of("adBreakTime", "-1"));
    when(mockAdsManager.getAdCuePoints()).thenReturn(ImmutableList.of(-1f));
    // Simulate loading an empty postroll ad.
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    adEventListener.onAdEvent(mockPostrollFetchErrorAdEvent);
    assertThat(getAdPlaybackState(/* periodIndex= */
    0)).isEqualTo(new AdPlaybackState(TEST_ADS_ID, /* adGroupTimesUs...= */
    C.TIME_END_OF_SOURCE).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(com.google.android.exoplayer2.source.ads.AdPlaybackState) AdEvent(com.google.ads.interactivemedia.v3.api.AdEvent) Test(org.junit.Test)

Example 10 with AdPlaybackState

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

the class ImaAdsLoaderTest method resumePlaybackBeforeSecondMidroll_withoutPlayAdBeforeStartPosition_skipsFirstMidroll.

@Test
public void resumePlaybackBeforeSecondMidroll_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) - 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 = (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)).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)

Aggregations

Test (org.junit.Test)89 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)87 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)51 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)35 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)32 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)27 Pair (android.util.Pair)19 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)17 Timeline (com.google.android.exoplayer2.Timeline)15 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)14 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)14 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)13 DefaultMediaSourceFactory (com.google.android.exoplayer2.source.DefaultMediaSourceFactory)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 Listener (com.google.android.exoplayer2.Player.Listener)10 Context (android.content.Context)9 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)9 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)8 SurfaceTexture (android.graphics.SurfaceTexture)7 Surface (android.view.Surface)7