Skip to content

Commit

Permalink
fix(Ads): Fix behavior when user JUMPS across more than one interstit…
Browse files Browse the repository at this point in the history
…ial (#7667)

Fixes #7665
  • Loading branch information
avelad authored Nov 26, 2024
1 parent ff7c465 commit 4aa9306
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions lib/ads/interstitial_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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++;
}
Expand Down Expand Up @@ -540,7 +556,7 @@ shaka.ads.InterstitialAdManager = class {
}).length;
}

if (interstitial.once || interstitial.pre) {
if (interstitial.once) {
oncePlayed++;
this.interstitials_.delete(interstitial);
this.cuepointsChanged_();
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4aa9306

Please sign in to comment.