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): Allow use a custom playhead tracker in CS #5543

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions demo/common/message_ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ shakaDemo.MessageIds = {
/* Config panel. */
ADAPTATION_RESTRICTIONS_SECTION_HEADER: 'DEMO_ADAPTATION_RESTRICTIONS_SECTION_HEADER',
ADAPTATION_SECTION_HEADER: 'DEMO_ADAPTATION_SECTION_HEADER',
ADS_SECTION_HEADER: 'DEMO_ADS_SECTION_HEADER',
ALWAYS_STREAM_TEXT: 'DEMO_ALWAYS_STREAM_TEXT',
ALWAYS_STREAM_TEXT_WARNING: 'DEMO_ALWAYS_STREAM_TEXT_WARNING',
AUDIO_CHANNEL_COUNT: 'DEMO_AUDIO_CHANNEL_COUNT',
Expand All @@ -179,6 +180,7 @@ shakaDemo.MessageIds = {
CMCD_SECTION_HEADER: 'DEMO_CMCD_SECTION_HEADER',
CONNECTION_TIMEOUT: 'DEMO_CONNECTION_TIMEOUT',
CONTENT_ID: 'DEMO_CONTENT_ID',
CUSTOM_PLAYHEAD_TRACKER: 'DEMO_CUSTOM_PLAYHEAD_TRACKER',
DASH_SEQUENCE_MODE: 'DEMO_DASH_SEQUENCE_MODE',
DEFAULT_AUDIO_CODEC: 'DEMO_DEFAULT_AUDIO_CODEC',
DEFAULT_PRESENTATION_DELAY: 'DEMO_DEFAULT_PRESENTATION_DELAY',
Expand Down
10 changes: 10 additions & 0 deletions demo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ shakaDemo.Config = class {
shakaDemo.MessageIds.RESTRICTIONS_SECTION_HEADER);
this.addCmcdSection_();
this.addLcevcSection_();
this.addAdsSection_();
}

/**
Expand Down Expand Up @@ -328,6 +329,15 @@ shakaDemo.Config = class {
.addBoolInput_(MessageIds.LCEVC_DRAW_LOGO, 'lcevc.drawLogo');
}

/** @private */
addAdsSection_() {
const MessageIds = shakaDemo.MessageIds;
const docLink = this.resolveExternLink_('.AdsConfiguration');
this.addSection_(MessageIds.ADS_SECTION_HEADER, docLink)
.addBoolInput_(MessageIds.CUSTOM_PLAYHEAD_TRACKER,
'ads.customPlayheadTracker');
}

/**
* @param {string} category
* @param {!shakaDemo.MessageIds} sectionName
Expand Down
2 changes: 2 additions & 0 deletions demo/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"DEMO_AD_SEARCH": "Filters for assets that have advertisements.",
"DEMO_AD_TAG_URL": "Ad Tag URL",
"DEMO_ADS_TAB": "Ads",
"DEMO_ADS_SECTION_HEADER": "Ads",
"DEMO_ALL_CONTENT": "ALL CONTENT",
"DEMO_ALWAYS_STREAM_TEXT": "Always Stream Text",
"DEMO_ALWAYS_STREAM_TEXT_WARNING": "Text must always be streamed while native controls are enabled, for captions to work.",
Expand Down Expand Up @@ -53,6 +54,7 @@
"DEMO_CUSTOM_INTRO_ONE": "Try Shaka Player with your own content!",
"DEMO_CUSTOM_INTRO_THREE": "Custom assets will remain even after reloading the page.",
"DEMO_CUSTOM_INTRO_TWO": "Press the button below to add a custom asset.",
"DEMO_CUSTOM_PLAYHEAD_TRACKER": "Custom playhead tracker",
"DEMO_DASH": "DASH",
"DEMO_DASH_IF": "DASH-IF",
"DEMO_DASH_SEQUENCE_MODE": "Enable DASH sequence mode",
Expand Down
8 changes: 8 additions & 0 deletions demo/locales/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"description": "The header for a tab within the custom asset creation dialog.",
"message": "Ads"
},
"DEMO_ADS_SECTION_HEADER": {
"description": "The header for a section of configuration values.",
"message": "Ads"
},
"DEMO_AD_SEARCH": {
"description": "A tooltip for an optional search term.",
"message": "Filters for assets that have advertisements."
Expand Down Expand Up @@ -215,6 +219,10 @@
"description": "The second part of a message instructing users on how to add custom content.",
"message": "Press the button below to add a custom asset."
},
"DEMO_CUSTOM_PLAYHEAD_TRACKER": {
"description": "The name of a configuration value.",
"message": "Custom playhead tracker"
},
"DEMO_DASH": {
"description": "Text that describes an asset that is packaged in a DASH manifest.",
"message": "[PROPER_NAME:DASH]"
Expand Down
2 changes: 1 addition & 1 deletion externs/ima.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ google.ima.AdsManager = class {
/** @const */
google.ima.AdsManagerLoadedEvent = class extends Event {
/**
* @param {!HTMLElement} video
* @param {!(HTMLElement|{currentTime: number})} video
* @param {!google.ima.AdsRenderingSettings=} adsRenderingSettings
* @return {!google.ima.AdsManager}
*/
Expand Down
8 changes: 7 additions & 1 deletion externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1246,11 +1246,17 @@ shaka.extern.MediaSourceConfiguration;


/**
* @typedef {Object}
* @typedef {{
* customPlayheadTracker: boolean
* }}
*
* @description
* Ads configuration.
*
* @property {boolean} customPlayheadTracker
* If this is <code>true</code>, we create a custom playhead tracker for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to have some details here on why/when you might want this. In the PR description, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have to admit I'm not sure what this has to do with having multiple video elements?
I tried the change out and it doesn't seem to result in the ad container having a different DOM structure than the regular method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure is the same, but the playhead tracker is used to get the position of the original video. If you can only have one video, if you delete it to leave the video to the ad, you stop having the position of the original video, so you can no longer track the position to launch the ads (vmap case)

* Client Side. This value defaults to <code>false</code>.
*
* @exportDoc
*/
shaka.extern.AdsConfiguration;
Expand Down
33 changes: 31 additions & 2 deletions lib/ads/client_side_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,37 @@ shaka.ads.ClientSideAdManager = class {
this.onEvent_(new shaka.util.FakeEvent(shaka.ads.AdManager.ADS_LOADED,
(new Map()).set('loadTime', loadTime)));

this.imaAdsManager_ = e.getAdsManager(this.video_,
this.adsRenderingSettings_);
if (!this.config_.customPlayheadTracker) {
this.imaAdsManager_ = e.getAdsManager(this.video_,
this.adsRenderingSettings_);
} else {
const videoPlayHead = {
currentTime: this.video_.currentTime,
};

this.imaAdsManager_ = e.getAdsManager(videoPlayHead,
this.adsRenderingSettings_);

if (this.video_.muted) {
this.imaAdsManager_.setVolume(0);
} else {
this.imaAdsManager_.setVolume(this.video_.volume);
}

this.eventManager_.listen(this.video_, 'timeupdate', () => {
if (!this.video_.duration) {
return;
}
videoPlayHead.currentTime = this.video_.currentTime;
});
this.eventManager_.listen(this.video_, 'volumechange', () => {
if (this.video_.muted) {
this.imaAdsManager_.setVolume(0);
} else {
this.imaAdsManager_.setVolume(this.video_.volume);
}
});
}

this.onEvent_(new shaka.util.FakeEvent(
shaka.ads.AdManager.IMA_AD_MANAGER_LOADED,
Expand Down
4 changes: 3 additions & 1 deletion lib/util/player_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ shaka.util.PlayerConfiguration = class {
forceTransmux: false,
};

const ads = {};
const ads = {
customPlayheadTracker: false,
};

const AutoShowText = shaka.config.AutoShowText;

Expand Down