Skip to content

Commit

Permalink
Defer adsManager.init until the timeline has loaded
Browse files Browse the repository at this point in the history
If the app seeks after we get an ads manager but before the player exposes the
timeline with ads, we would end up expecting to play a preroll even after the
seek request arrived. This caused the player to get stuck.

Wait until a non-empty timeline has been exposed via onTimelineChanged before
initializing IMA (at which point it can start polling the player position). Seek
requests are not handled while an ad is playing.

PiperOrigin-RevId: 265058325
  • Loading branch information
andrewlewis authored and ojw28 committed Sep 2, 2019
1 parent 7cefb56 commit 0085a7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public ImaAdsLoader buildForAdsResponse(String adsResponse) {
private int lastVolumePercentage;

private AdsManager adsManager;
private boolean initializedAdsManager;
private AdLoadException pendingAdLoadError;
private Timeline timeline;
private long contentDurationMs;
Expand Down Expand Up @@ -613,8 +614,8 @@ public void start(EventListener eventListener, AdViewProvider adViewProvider) {
adsManager.resume();
}
} else if (adsManager != null) {
// Ads have loaded but the ads manager is not initialized.
startAdPlayback();
adPlaybackState = new AdPlaybackState(getAdGroupTimesUs(adsManager.getAdCuePoints()));
updateAdPlaybackState();
} else {
// Ads haven't loaded yet, so request them.
requestAds(adViewGroup);
Expand Down Expand Up @@ -688,7 +689,8 @@ public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
if (player != null) {
// If a player is attached already, start playback immediately.
try {
startAdPlayback();
adPlaybackState = new AdPlaybackState(getAdGroupTimesUs(adsManager.getAdCuePoints()));
updateAdPlaybackState();
} catch (Exception e) {
maybeNotifyInternalError("onAdsManagerLoaded", e);
}
Expand Down Expand Up @@ -968,6 +970,10 @@ public void onTimelineChanged(
if (contentDurationUs != C.TIME_UNSET) {
adPlaybackState = adPlaybackState.withContentDurationUs(contentDurationUs);
}
if (!initializedAdsManager && adsManager != null) {
initializedAdsManager = true;
initializeAdsManager();
}
onPositionDiscontinuity(Player.DISCONTINUITY_REASON_INTERNAL);
}

Expand Down Expand Up @@ -1041,7 +1047,7 @@ public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {

// Internal methods.

private void startAdPlayback() {
private void initializeAdsManager() {
AdsRenderingSettings adsRenderingSettings = imaFactory.createAdsRenderingSettings();
adsRenderingSettings.setEnablePreloading(ENABLE_PRELOADING);
adsRenderingSettings.setMimeTypes(supportedMimeTypes);
Expand All @@ -1056,10 +1062,9 @@ private void startAdPlayback() {
adsRenderingSettings.setUiElements(adUiElements);
}

// Set up the ad playback state, skipping ads based on the start position as required.
// Skip ads based on the start position as required.
long[] adGroupTimesUs = getAdGroupTimesUs(adsManager.getAdCuePoints());
adPlaybackState = new AdPlaybackState(adGroupTimesUs);
long contentPositionMs = player.getCurrentPosition();
long contentPositionMs = player.getContentPosition();
int adGroupIndexForPosition =
adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(contentPositionMs));
if (adGroupIndexForPosition > 0 && adGroupIndexForPosition != C.INDEX_UNSET) {
Expand Down Expand Up @@ -1093,7 +1098,6 @@ private void startAdPlayback() {
pendingContentPositionMs = contentPositionMs;
}

// Start ad playback.
adsManager.init(adsRenderingSettings);
updateAdPlaybackState();
if (DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public void testStart_updatesAdPlaybackState() {
assertThat(adsLoaderListener.adPlaybackState)
.isEqualTo(
new AdPlaybackState(/* adGroupTimesUs= */ 0)
.withAdDurationsUs(PREROLL_ADS_DURATIONS_US));
.withAdDurationsUs(PREROLL_ADS_DURATIONS_US)
.withContentDurationUs(CONTENT_DURATION_US));
}

@Test
Expand Down

0 comments on commit 0085a7e

Please sign in to comment.