From 9716b03f8fa11d855599008572da2f89419043a8 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Wed, 19 Jul 2017 04:54:31 -0700 Subject: [PATCH] Fix isLastPeriod to take into account the played ad count A content period just before a postroll ad group with all ads played was not being marked as the last media period in the timeline period. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=162471919 --- .../exoplayer2/MediaPeriodInfoSequence.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java b/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java index 571f295015..0e9c65421c 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java @@ -323,16 +323,23 @@ import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId; if (adGroupCount == 0) { return true; } + int lastAdGroupIndex = adGroupCount - 1; - boolean periodHasPostrollAd = period.getAdGroupTimeUs(lastAdGroupIndex) == C.TIME_END_OF_SOURCE; - if (!id.isAd()) { - return !periodHasPostrollAd && endPositionUs == C.TIME_END_OF_SOURCE; - } else if (periodHasPostrollAd && id.adGroupIndex == lastAdGroupIndex) { - int adCountInLastAdGroup = period.getAdCountInAdGroup(lastAdGroupIndex); - return adCountInLastAdGroup != C.LENGTH_UNSET - && id.adIndexInAdGroup == adCountInLastAdGroup - 1; + boolean isAd = id.isAd(); + if (period.getAdGroupTimeUs(lastAdGroupIndex) != C.TIME_END_OF_SOURCE) { + // There's no postroll ad. + return !isAd && endPositionUs == C.TIME_END_OF_SOURCE; } - return false; + + int postrollAdCount = period.getAdCountInAdGroup(lastAdGroupIndex); + if (postrollAdCount == C.LENGTH_UNSET) { + // We won't know if this is the last ad until we know how many postroll ads there are. + return false; + } + + boolean isLastAd = isAd && id.adGroupIndex == lastAdGroupIndex + && id.adIndexInAdGroup == postrollAdCount - 1; + return isLastAd || (!isAd && period.getPlayedAdCount(lastAdGroupIndex) == postrollAdCount); } private boolean isLastInTimeline(MediaPeriodId id, boolean isLastMediaPeriodInPeriod) {