Skip to content

Commit

Permalink
fix(Ads): Fix JUMP implementation to avoid loop the same ad group in …
Browse files Browse the repository at this point in the history
…Interstitials (#7329)
  • Loading branch information
avelad authored Sep 18, 2024
1 parent 3a146c2 commit 524014e
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lib/ads/interstitial_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,23 @@ shaka.ads.InterstitialAdManager = class {
/** @private {?number} */
this.lastTime_ = null;

/** @private {?shaka.extern.AdInterstitial} */
this.lastPlayedAd_ = null;

this.eventManager_.listen(this.baseVideo_, 'timeupdate', () => {
if (this.playingAd_) {
return;
}
const needPreRoll = this.lastTime_ == null || this.lastTime_ == 0;
this.lastTime_ = this.baseVideo_.currentTime;
const currentInterstitial = this.getCurrentInterstitial_(needPreRoll);
// Remove last played add when the new time is before to the ad time.
if (this.lastPlayedAd_ &&
!this.lastPlayedAd_.pre && !this.lastPlayedAd_.post &&
this.lastTime_ < this.lastPlayedAd_.startTime) {
this.lastPlayedAd_ = null;
}
const currentInterstitial = this.getCurrentInterstitial_(
needPreRoll, /* numberToSkip= */ 0, this.lastPlayedAd_);
if (currentInterstitial) {
this.setupAd_(currentInterstitial, /* sequenceLength= */ 1,
/* adPosition= */ 1, /* initialTime= */ Date.now());
Expand Down Expand Up @@ -187,6 +197,7 @@ shaka.ads.InterstitialAdManager = class {
this.player_.detach();
this.playingAd_ = false;
this.lastTime_ = null;
this.lastPlayedAd_ = null;
}

/** @override */
Expand Down Expand Up @@ -374,11 +385,12 @@ shaka.ads.InterstitialAdManager = class {

/**
* @param {boolean} needPreRoll
* @param {number=} numberToSkip
* @param {number} numberToSkip
* @param {?shaka.extern.AdInterstitial=} lastPlayedAd
* @return {?shaka.extern.AdInterstitial}
* @private
*/
getCurrentInterstitial_(needPreRoll, numberToSkip = 0) {
getCurrentInterstitial_(needPreRoll, numberToSkip, lastPlayedAd) {
let skipped = 0;
let currentInterstitial = null;
if (this.interstitials_.size && this.lastTime_ != null) {
Expand All @@ -398,8 +410,16 @@ shaka.ads.InterstitialAdManager = class {
} else if (!interstitial.pre && !interstitial.post) {
const difference =
this.lastTime_ - roundDecimals(interstitial.startTime);
isValid = difference > 0 &&
(difference <= 1 || !interstitial.canJump);
if (difference > 0 &&
(difference <= 1 || !interstitial.canJump)) {
if (difference > 1 && lastPlayedAd &&
!lastPlayedAd.pre && !lastPlayedAd.post &&
lastPlayedAd.startTime >= interstitial.startTime) {
isValid = false;
} else {
isValid = true;
}
}
}
if (isValid) {
if (skipped == numberToSkip) {
Expand All @@ -426,6 +446,8 @@ shaka.ads.InterstitialAdManager = class {
oncePlayed = 0) {
goog.asserts.assert(this.video_, 'Must have video');

this.lastPlayedAd_ = interstitial;

const startTime = Date.now();

if (!this.video_.parentElement && this.adContainer_) {
Expand Down

0 comments on commit 524014e

Please sign in to comment.