From 9af439782b501136700c05261240975f356b699e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Wed, 23 Oct 2024 21:31:51 +0200 Subject: [PATCH] fix(Ads): Limit interstitial duration to actual duration if available (#7480) Currently, if the interstitial lasts 30 and we have a stream that lasts 31 seconds, we would go back to live after the interstitial, but with 1 extra second of latency. This PR solves this by limiting the play range to 30. --- lib/ads/interstitial_ad_manager.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/ads/interstitial_ad_manager.js b/lib/ads/interstitial_ad_manager.js index 0af70ce9e3..6f90befd0c 100644 --- a/lib/ads/interstitial_ad_manager.js +++ b/lib/ads/interstitial_ad_manager.js @@ -529,11 +529,26 @@ shaka.ads.InterstitialAdManager = class { }); try { this.updatePlayerConfig_(); + // playRangeEnd in src= causes the ended event not to be fired when that + // position is reached. So we don't use it because we would never go back + // to the main stream. + const loadMode = this.basePlayer_.getLoadMode(); + if (loadMode == shaka.Player.LoadMode.MEDIA_SOURCE && + interstitial.startTime && interstitial.endTime && + interstitial.endTime != Infinity && + interstitial.startTime != interstitial.endTime) { + const duration = interstitial.endTime - interstitial.startTime; + if (duration > 0) { + this.player_.configure('playRangeEnd', duration); + } + } if (interstitial.playoutLimit) { playoutLimitTimer = new shaka.util.Timer(() => { ad.skip(); }).tickAfter(interstitial.playoutLimit); - this.player_.configure('playRangeEnd', interstitial.playoutLimit); + if (loadMode == shaka.Player.LoadMode.MEDIA_SOURCE) { + this.player_.configure('playRangeEnd', interstitial.playoutLimit); + } } await this.player_.attach(this.video_); if (this.preloadManagerInterstitials_.has(interstitial)) {