Search in sources :

Example 1 with ImaAdsLoader

use of androidx.media3.exoplayer.ima.ImaAdsLoader in project media by androidx.

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(androidx.media3.test.utils.FakeMediaSource) DefaultMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory) AdPlaybackState(androidx.media3.common.AdPlaybackState) AdsMediaSource(androidx.media3.exoplayer.source.ads.AdsMediaSource) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) Test(org.junit.Test)

Example 2 with ImaAdsLoader

use of androidx.media3.exoplayer.ima.ImaAdsLoader in project media by androidx.

the class ImaAdsLoaderTest method playbackWithTwoAdsMediaSources_preloadsSecondAdTagWithBackgroundResume.

@Test
public void playbackWithTwoAdsMediaSources_preloadsSecondAdTagWithBackgroundResume() {
    Object secondAdsId = new Object();
    AdsMediaSource secondAdsMediaSource = new AdsMediaSource(new FakeMediaSource(CONTENT_TIMELINE), TEST_DATA_SPEC, secondAdsId, new DefaultMediaSourceFactory((Context) getApplicationContext()), imaAdsLoader, adViewProvider);
    timelineWindowDefinitions = new TimelineWindowDefinition[] { getInitialTimelineWindowDefinition(TEST_ADS_ID), getInitialTimelineWindowDefinition(secondAdsId) };
    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);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_PAUSE_REQUESTED, mockPrerollSingleAd));
    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));
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, /* positionMs= */
    0);
    videoAdPlayer.stopAd(TEST_AD_MEDIA_INFO);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */
    null));
    // Simulate starting to buffer the second ads media source.
    imaAdsLoader.start(secondAdsMediaSource, TEST_DATA_SPEC, secondAdsId, adViewProvider, secondAdsLoaderListener);
    // Simulate backgrounding/resuming.
    imaAdsLoader.stop(adsMediaSource, adsLoaderListener);
    imaAdsLoader.stop(secondAdsMediaSource, secondAdsLoaderListener);
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    imaAdsLoader.start(secondAdsMediaSource, TEST_DATA_SPEC, secondAdsId, adViewProvider, secondAdsLoaderListener);
    // 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));
    // Verify that the second source's ad cue points have preloaded.
    assertThat(getAdPlaybackState(/* periodIndex= */
    1)).isEqualTo(new AdPlaybackState(secondAdsId, /* adGroupTimesUs...= */
    0));
}
Also used : ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) Context(android.content.Context) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) DefaultMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory) AdPlaybackState(androidx.media3.common.AdPlaybackState) AdsMediaSource(androidx.media3.exoplayer.source.ads.AdsMediaSource) Test(org.junit.Test)

Example 3 with ImaAdsLoader

use of androidx.media3.exoplayer.ima.ImaAdsLoader in project media by androidx.

the class ImaAdsLoaderTest method playbackWithTwoAdsMediaSources_preloadsSecondAdTag.

@Test
public void playbackWithTwoAdsMediaSources_preloadsSecondAdTag() {
    Object secondAdsId = new Object();
    AdsMediaSource secondAdsMediaSource = new AdsMediaSource(new FakeMediaSource(CONTENT_TIMELINE), TEST_DATA_SPEC, secondAdsId, new DefaultMediaSourceFactory((Context) getApplicationContext()), imaAdsLoader, adViewProvider);
    timelineWindowDefinitions = new TimelineWindowDefinition[] { getInitialTimelineWindowDefinition(TEST_ADS_ID), getInitialTimelineWindowDefinition(secondAdsId) };
    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);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_PAUSE_REQUESTED, mockPrerollSingleAd));
    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));
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, /* positionMs= */
    0);
    videoAdPlayer.stopAd(TEST_AD_MEDIA_INFO);
    adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */
    null));
    // Simulate starting to buffer the second ads media source.
    imaAdsLoader.start(secondAdsMediaSource, TEST_DATA_SPEC, secondAdsId, adViewProvider, secondAdsLoaderListener);
    // 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));
    // Verify that the second source's ad cue points have preloaded.
    assertThat(getAdPlaybackState(/* periodIndex= */
    1)).isEqualTo(new AdPlaybackState(secondAdsId, /* adGroupTimesUs...= */
    0));
}
Also used : ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) Context(android.content.Context) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) DefaultMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory) AdPlaybackState(androidx.media3.common.AdPlaybackState) AdsMediaSource(androidx.media3.exoplayer.source.ads.AdsMediaSource) Test(org.junit.Test)

Example 4 with ImaAdsLoader

use of androidx.media3.exoplayer.ima.ImaAdsLoader in project media by androidx.

the class ImaAdsLoaderTest method resumePlaybackAtMidroll_withoutPlayAdBeforeStartPosition_skipsPreroll.

@Test
public void resumePlaybackAtMidroll_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));
    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)).withContentDurationUs(CONTENT_PERIOD_DURATION_US).withSkippedAdGroup(/* adGroupIndex= */
    0));
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) DefaultMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory) AdPlaybackState(androidx.media3.common.AdPlaybackState) AdsMediaSource(androidx.media3.exoplayer.source.ads.AdsMediaSource) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) Test(org.junit.Test)

Example 5 with ImaAdsLoader

use of androidx.media3.exoplayer.ima.ImaAdsLoader in project media by androidx.

the class ImaAdsLoaderTest method setUp.

@Before
public void setUp() {
    setupMocks();
    fakePlayer = new FakeExoPlayer();
    adViewGroup = new FrameLayout(getApplicationContext());
    View adOverlayView = new View(getApplicationContext());
    adViewProvider = new AdViewProvider() {

        @Override
        public ViewGroup getAdViewGroup() {
            return adViewGroup;
        }

        @Override
        public ImmutableList<AdOverlayInfo> getAdOverlayInfos() {
            return ImmutableList.of(new AdOverlayInfo(adOverlayView, AdOverlayInfo.PURPOSE_CLOSE_AD));
        }
    };
    audioAdsAdViewProvider = () -> null;
    imaAdsLoader = new ImaAdsLoader.Builder(getApplicationContext()).setImaFactory(mockImaFactory).setImaSdkSettings(mockImaSdkSettings).setVideoAdPlayerCallback(mockVideoAdPlayerCallback).build();
    imaAdsLoader.setPlayer(fakePlayer);
    adsMediaSource = 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) };
    adsLoaderListener = new TestAdsLoaderListener(/* periodIndex= */
    0);
    when(mockAdsManager.getAdCuePoints()).thenReturn(PREROLL_CUE_POINTS_SECONDS);
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) ViewGroup(android.view.ViewGroup) ImmutableList(com.google.common.collect.ImmutableList) View(android.view.View) AdOverlayInfo(androidx.media3.common.AdOverlayInfo) DefaultMediaSourceFactory(androidx.media3.exoplayer.source.DefaultMediaSourceFactory) FrameLayout(android.widget.FrameLayout) AdViewProvider(androidx.media3.common.AdViewProvider) AdsMediaSource(androidx.media3.exoplayer.source.ads.AdsMediaSource) Before(org.junit.Before)

Aggregations

DefaultMediaSourceFactory (androidx.media3.exoplayer.source.DefaultMediaSourceFactory)12 AdsMediaSource (androidx.media3.exoplayer.source.ads.AdsMediaSource)12 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)12 Test (org.junit.Test)11 AdPlaybackState (androidx.media3.common.AdPlaybackState)7 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)4 Context (android.content.Context)3 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)3 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 FrameLayout (android.widget.FrameLayout)1 AdOverlayInfo (androidx.media3.common.AdOverlayInfo)1 AdViewProvider (androidx.media3.common.AdViewProvider)1 ImmutableList (com.google.common.collect.ImmutableList)1 Before (org.junit.Before)1