From 95f298f5a49e90d6fe80c10690281ecf73a91bd7 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Wed, 14 Feb 2018 07:03:40 -0800 Subject: [PATCH] Fix handling of ad tags with only preroll and postroll ads Content progress is only polled if there are midroll ad groups. If the ad tag had only preroll/postroll ads, the pending content position was not provided to IMA, which meant that the postroll ad could never play. Only set the pending content position if there are midroll ads. Issue: #3715 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=185680803 --- RELEASENOTES.md | 2 ++ .../exoplayer2/ext/ima/ImaAdsLoader.java | 22 ++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index a1ab55ebb0..7d15a0c70d 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -97,6 +97,8 @@ ([#3676](https://github.com/google/ExoPlayer/issues/3676)). * Fix handling of ad tags where ad groups are out of order ([#3716](https://github.com/google/ExoPlayer/issues/3716)). + * Fix handling of ad tags with only preroll/postroll ad groups + ([#3715](https://github.com/google/ExoPlayer/issues/3715)). * Propagate ad media preparation errors to IMA so that the ads can be skipped. * `EventLogger` moved from the demo app into the core library. diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java index d11bc920e1..0a79acb617 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java @@ -851,15 +851,14 @@ public final class ImaAdsLoader extends Player.DefaultEventListener implements A adsRenderingSettings.setMimeTypes(supportedMimeTypes); // Set up the ad playback state, skipping ads based on the start position as required. - pendingContentPositionMs = player.getCurrentPosition(); long[] adGroupTimesUs = getAdGroupTimesUs(adsManager.getAdCuePoints()); adPlaybackState = new AdPlaybackState(adGroupTimesUs); + long contentPositionMs = player.getCurrentPosition(); int adGroupIndexForPosition = - adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(pendingContentPositionMs)); + adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(contentPositionMs)); if (adGroupIndexForPosition == 0) { podIndexOffset = 0; } else if (adGroupIndexForPosition == C.INDEX_UNSET) { - pendingContentPositionMs = C.TIME_UNSET; // There is no preroll and midroll pod indices start at 1. podIndexOffset = -1; } else /* adGroupIndexForPosition > 0 */ { @@ -879,6 +878,11 @@ public final class ImaAdsLoader extends Player.DefaultEventListener implements A podIndexOffset = adGroupIndexForPosition - 1; } + if (hasMidrollAdGroups(adGroupTimesUs)) { + // IMA will poll the content position, so provide the player's initial position like a seek. + pendingContentPositionMs = contentPositionMs; + } + // Start ad playback. adsManager.init(adsRenderingSettings); updateAdPlaybackState(); @@ -1051,4 +1055,16 @@ public final class ImaAdsLoader extends Player.DefaultEventListener implements A // a single ad, ad group or the whole timeline. return adError.getErrorCode() == AdErrorCode.VAST_LINEAR_ASSET_MISMATCH; } + + private static boolean hasMidrollAdGroups(long[] adGroupTimesUs) { + int count = adGroupTimesUs.length; + if (count == 1) { + return adGroupTimesUs[0] != 0 && adGroupTimesUs[0] != C.TIME_END_OF_SOURCE; + } else if (count == 2) { + return adGroupTimesUs[0] != 0 || adGroupTimesUs[1] != C.TIME_END_OF_SOURCE; + } else { + // There's at least one midroll ad group, as adGroupTimesUs is never empty. + return true; + } + } }