Skip to content

Commit

Permalink
perf(Ads): Reduce latency for interstitial to start playing (shaka-pr…
Browse files Browse the repository at this point in the history
…oject#7528)

Use requestVideoFrameCallback the achieve better performance
  • Loading branch information
avelad authored Oct 31, 2024
1 parent 075713f commit 6303924
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/ads/interstitial_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ shaka.ads.InterstitialAdManager = class {
}
});

/** @private {shaka.util.Timer} */
this.timeUpdateTimer_ = new shaka.util.Timer(() => {
const checkForInterstitials = () => {
if (this.playingAd_ || !this.lastTime_ ||
this.basePlayer_.isRemotePlayback()) {
return;
Expand All @@ -125,7 +124,22 @@ shaka.ads.InterstitialAdManager = class {
this.setupAd_(currentInterstitial, /* sequenceLength= */ 1,
/* adPosition= */ 1, /* initialTime= */ Date.now());
}
}).tickEvery(/* seconds= */ 0.025);
};

/** @private {shaka.util.Timer} */
this.timeUpdateTimer_ = new shaka.util.Timer(checkForInterstitials);

if ('requestVideoFrameCallback' in this.baseVideo_ &&
!shaka.util.Platform.isSmartTV()) {
const baseVideo = /** @type {!HTMLVideoElement} */ (this.baseVideo_);
const videoFrameCallback = () => {
checkForInterstitials();
baseVideo.requestVideoFrameCallback(videoFrameCallback);
};
baseVideo.requestVideoFrameCallback(videoFrameCallback);
} else {
this.timeUpdateTimer_.tickEvery(/* seconds= */ 0.025);
}

/** @private {shaka.util.Timer} */
this.pollTimer_ = new shaka.util.Timer(async () => {
Expand Down

0 comments on commit 6303924

Please sign in to comment.