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

feat(Ads): Add content resume/pause requested events #6738

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions lib/ads/ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions lib/ads/client_side_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()) {
Expand Down
38 changes: 6 additions & 32 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -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);
});
}
Expand Down
Loading