diff --git a/lib/ads/ad_manager.js b/lib/ads/ad_manager.js index 2b534eb58c..b141ed3378 100644 --- a/lib/ads/ad_manager.js +++ b/lib/ads/ad_manager.js @@ -370,6 +370,26 @@ goog.require('shaka.util.IReleasable'); */ +/** + * @event shaka.ads.AdManager.AdContentPauseRequestedEvent + * @description Fired when the ad requires the main content to be paused. + * Fired when the platform does not support multiple media elements. + * @property {string} type + * 'ad-content-pause-requested' + * @exportDoc + */ + + +/** + * @event shaka.ads.AdManager.AdContentResumeRequestedEvent + * @description Fired when the ad requires the main content to be resumed. + * Fired when the platform does not support multiple media elements. + * @property {string} type + * 'ad-content-resume-requested' + * @exportDoc + */ + + /** * A class responsible for ad-related interactions. * @implements {shaka.extern.IAdManager} @@ -1052,6 +1072,26 @@ shaka.ads.AdManager.AD_BREAK_READY = 'ad-break-ready'; shaka.ads.AdManager.AD_INTERACTION = 'ad-interaction'; +/** + * The name of the event for when an ad requires the main content to be paused. + * Fired when the platform does not support multiple media elements. + * + * @const {string} + * @export + */ +shaka.ads.AdManager.AD_CONTENT_PAUSE_REQUESTED = 'ad-content-pause-requested'; + + +/** + * The name of the event for when an ad requires the main content to be resumed. + * Fired when the platform does not support multiple media elements. + * + * @const {string} + * @export + */ +shaka.ads.AdManager.AD_CONTENT_RESUME_REQUESTED = 'ad-content-resume-requested'; + + /** * Set this is a default ad manager for the player. * Apps can also set their own ad manager, if they'd like. diff --git a/lib/ads/client_side_ad_manager.js b/lib/ads/client_side_ad_manager.js index 7dbef48a73..5b5479de44 100644 --- a/lib/ads/client_side_ad_manager.js +++ b/lib/ads/client_side_ad_manager.js @@ -527,6 +527,11 @@ shaka.ads.ClientSideAdManager = class { this.ad_ = new shaka.ads.ClientSideAd(imaAd, this.imaAdsManager_, this.video_); + if (e.type == google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED && + !this.config_.supportsMultipleMediaElements ) { + this.onEvent_(new shaka.util.FakeEvent( + shaka.ads.AdManager.AD_CONTENT_PAUSE_REQUESTED)); + } const data = new Map() .set('ad', this.ad_) .set('sdkAdObject', imaAd) @@ -551,6 +556,11 @@ shaka.ads.ClientSideAdManager = class { * @private */ onAdComplete_(e) { + if (e.type == google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED && + !this.config_.supportsMultipleMediaElements) { + this.onEvent_(new shaka.util.FakeEvent( + shaka.ads.AdManager.AD_CONTENT_RESUME_REQUESTED)); + } this.onEvent_(new shaka.util.FakeEvent(shaka.ads.AdManager.AD_STOPPED, (new Map()).set('originalEvent', e))); if (this.ad_ && this.ad_.isLinear()) { diff --git a/lib/player.js b/lib/player.js index 95bc490d71..4e5ea46a34 100644 --- a/lib/player.js +++ b/lib/player.js @@ -792,23 +792,10 @@ shaka.Player = class extends shaka.util.FakeEventTarget { this.adManager_ = shaka.Player.adManagerFactory_(); this.adManager_.configure(this.config_.ads); - // Note: we don't use shaka.ads.AdManager.AD_STARTED to avoid add a - // optional module in the player. + // Note: we don't use shaka.ads.AdManager.AD_CONTENT_PAUSE_REQUESTED to + // avoid add a optional module in the player. this.adManagerEventManager_.listen( - this.adManager_, 'ad-started', async (e) => { - if (this.config_.ads.supportsMultipleMediaElements) { - return; - } - const event = /** @type {?google.ima.AdEvent} */ - (e['originalEvent']); - if (!event) { - return; - } - const contentPauseRequested = - google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED; - if (event.type != contentPauseRequested) { - return; - } + this.adManager_, 'ad-content-pause-requested', async () => { this.preloadDueAdManagerTimer_.stop(); if (!this.preloadDueAdManager_) { this.preloadDueAdManagerVideo_ = this.video_; @@ -817,23 +804,10 @@ shaka.Player = class extends shaka.util.FakeEventTarget { } }); - // Note: we don't use shaka.ads.AdManager.AD_STOPPED to avoid add a - // optional module in the player. + // Note: we don't use shaka.ads.AdManager.AD_CONTENT_RESUME_REQUESTED to + // avoid add a optional module in the player. this.adManagerEventManager_.listen( - this.adManager_, 'ad-stopped', (e) => { - if (this.config_.ads.supportsMultipleMediaElements) { - return; - } - const event = /** @type {?google.ima.AdEvent} */ - (e['originalEvent']); - if (!event) { - return; - } - const contentResumeRequested = - google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED; - if (event.type != contentResumeRequested) { - return; - } + this.adManager_, 'ad-content-resume-requested', () => { this.preloadDueAdManagerTimer_.tickAfter(0.1); }); }