Search in sources :

Example 16 with AdsMediaSource

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

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(com.google.android.exoplayer2.upstream.Allocator) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Example 17 with AdsMediaSource

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

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(com.google.android.exoplayer2.testutil.FakeMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) MediaItem(com.google.android.exoplayer2.MediaItem) AdViewProvider(com.google.android.exoplayer2.ui.AdViewProvider) EventListener(com.google.android.exoplayer2.source.ads.AdsLoader.EventListener) EventListener(com.google.android.exoplayer2.source.ads.AdsLoader.EventListener) Before(org.junit.Before)

Example 18 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource in project prebid-mobile-android by prebid.

the class XandrInstreamVideoGamActivity method initializePlayer.

private void initializePlayer() {
    SimpleExoPlayer.Builder playerBuilder = new SimpleExoPlayer.Builder(this);
    player = playerBuilder.build();
    playerView.setPlayer(player);
    adsLoader.setPlayer(player);
    Uri uri = Uri.parse(getString(R.string.content_url));
    MediaItem mediaItem = MediaItem.fromUri(uri);
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, getString(R.string.app_name));
    ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
    MediaSource mediaSource = mediaSourceFactory.createMediaSource(mediaItem);
    DataSpec dataSpec = new DataSpec(adsUri);
    AdsMediaSource adsMediaSource = new AdsMediaSource(mediaSource, dataSpec, "ad", mediaSourceFactory, adsLoader, playerView);
    player.setMediaSource(adsMediaSource);
    player.setPlayWhenReady(true);
    player.prepare();
}
Also used : SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) Uri(android.net.Uri) DataSource(com.google.android.exoplayer2.upstream.DataSource) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) MediaItem(com.google.android.exoplayer2.MediaItem) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec)

Example 19 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource 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 20 with AdsMediaSource

use of com.google.android.exoplayer2.source.ads.AdsMediaSource 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)

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