Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Ads): Fix behavior when user JUMPS across more than one interstitial #7667

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading