diff --git a/lib/ads/interstitial_ad_manager.js b/lib/ads/interstitial_ad_manager.js index d2c66a7c9b..5e0e2dd172 100644 --- a/lib/ads/interstitial_ad_manager.js +++ b/lib/ads/interstitial_ad_manager.js @@ -102,7 +102,7 @@ shaka.ads.InterstitialAdManager = class { } this.lastTime_ = this.baseVideo_.currentTime; const currentInterstitial = this.getCurrentInterstitial_( - /* needPreRoll= */ true, /* numberToSkip= */ 0, this.lastPlayedAd_); + /* needPreRoll= */ true); if (currentInterstitial) { this.setupAd_(currentInterstitial, /* sequenceLength= */ 1, /* adPosition= */ 1, /* initialTime= */ Date.now()); @@ -121,8 +121,7 @@ shaka.ads.InterstitialAdManager = class { this.lastTime_ < this.lastPlayedAd_.startTime) { this.lastPlayedAd_ = null; } - const currentInterstitial = this.getCurrentInterstitial_( - /* needPreRoll= */ false, /* numberToSkip= */ 0, this.lastPlayedAd_); + const currentInterstitial = this.getCurrentInterstitial_(); if (currentInterstitial) { this.setupAd_(currentInterstitial, /* sequenceLength= */ 1, /* adPosition= */ 1, /* initialTime= */ Date.now()); @@ -456,13 +455,12 @@ shaka.ads.InterstitialAdManager = class { /** - * @param {boolean} needPreRoll - * @param {number} numberToSkip - * @param {?shaka.extern.AdInterstitial=} lastPlayedAd + * @param {boolean=} needPreRoll + * @param {?number=} numberToSkip * @return {?shaka.extern.AdInterstitial} * @private */ - getCurrentInterstitial_(needPreRoll, numberToSkip, lastPlayedAd) { + getCurrentInterstitial_(needPreRoll = false, numberToSkip = null) { let skipped = 0; let currentInterstitial = null; if (this.interstitials_.size && this.lastTime_ != null) { @@ -473,7 +471,15 @@ shaka.ads.InterstitialAdManager = class { const roundDecimals = (number) => { return Math.round(number * 1000) / 1000; }; - for (const interstitial of interstitials) { + let interstitialsToCheck = interstitials; + if (needPreRoll) { + interstitialsToCheck = interstitials.filter((i) => i.pre); + } else if (isEnded) { + interstitialsToCheck = interstitials.filter((i) => i.post); + } else { + interstitialsToCheck = interstitials.filter((i) => !i.pre && !i.post); + } + for (const interstitial of interstitialsToCheck) { let isValid = false; if (needPreRoll) { isValid = interstitial.pre; @@ -484,18 +490,28 @@ shaka.ads.InterstitialAdManager = class { this.lastTime_ - roundDecimals(interstitial.startTime); if (difference > 0 && (difference <= 1 || !interstitial.canJump)) { - if (lastPlayedAd && !lastPlayedAd.pre && !lastPlayedAd.post && - lastPlayedAd.startTime >= interstitial.startTime) { + if (numberToSkip == null && this.lastPlayedAd_ && + !this.lastPlayedAd_.pre && !this.lastPlayedAd_.post && + this.lastPlayedAd_.startTime >= interstitial.startTime) { isValid = false; } else { isValid = true; } } } - if (isValid) { - if (skipped == numberToSkip) { + if (isValid && (!this.lastPlayedAd_ || + interstitial.startTime >= this.lastPlayedAd_.startTime)) { + if (skipped == (numberToSkip || 0)) { currentInterstitial = interstitial; - break; + } else if (currentInterstitial && !interstitial.canJump) { + const currentStartTime = + roundDecimals(currentInterstitial.startTime); + const newStartTime = + roundDecimals(interstitial.startTime); + if (newStartTime - currentStartTime > 0.001) { + currentInterstitial = interstitial; + skipped = 0; + } } skipped++; } @@ -540,7 +556,7 @@ shaka.ads.InterstitialAdManager = class { }).length; } - if (interstitial.once || interstitial.pre) { + if (interstitial.once) { oncePlayed++; this.interstitials_.delete(interstitial); this.cuepointsChanged_(); @@ -630,6 +646,10 @@ shaka.ads.InterstitialAdManager = class { this.setupAd_(nextCurrentInterstitial, sequenceLength, ++adPosition, initialTime, oncePlayed); } else { + if (interstitial.post) { + this.lastTime_ = null; + this.lastPlayedAd_ = null; + } if (this.usingBaseVideo_) { await this.player_.detach(); } else {