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): Parse non-linear VAST ads #7702

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
79 changes: 72 additions & 7 deletions lib/ads/ad_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ shaka.ads.Utils = class {
if (!creatives) {
continue;
}
for (const creative of TXml.findChildren(creatives, 'Creative')) {
const linear = TXml.findChild(creative, 'Linear');
if (!linear) {
continue;
}
const processLinearAd = (linear) => {
let skipOffset = null;
if (linear.attributes['skipoffset']) {
skipOffset = shaka.util.TextParser.parseTime(
Expand All @@ -55,7 +51,7 @@ shaka.ads.Utils = class {
}
const mediaFiles = TXml.findChild(linear, 'MediaFiles');
if (!mediaFiles) {
continue;
return;
}
const medias = TXml.findChildren(mediaFiles, 'MediaFile');
let checkMedias = medias;
Expand All @@ -71,7 +67,10 @@ shaka.ads.Utils = class {
return bHeight - aHeight;
});
for (const media of sortedMedias) {
const adUrl = TXml.getTextContents(media);
if (media.attributes['apiFramework']) {
continue;
}
const adUrl = TXml.getContents(media);
if (!adUrl) {
continue;
}
Expand All @@ -96,6 +95,72 @@ shaka.ads.Utils = class {
});
break;
}
};
const processNonLinearAd = (nonLinear) => {
const staticResource = TXml.findChild(nonLinear, 'StaticResource');
if (!staticResource) {
return;
}
const adUrl = TXml.getContents(staticResource);
if (!adUrl) {
return;
}
const width = TXml.parseAttr(nonLinear, 'width', TXml.parseInt);
const height = TXml.parseAttr(nonLinear, 'height', TXml.parseInt);
if (!width || !height) {
return;
}
let playoutLimit = null;
const minSuggestedDuration =
nonLinear.attributes['minSuggestedDuration'];
if (minSuggestedDuration) {
playoutLimit = shaka.util.TextParser.parseTime(minSuggestedDuration);
}
interstitials.push({
id: null,
startTime: startTime,
endTime: null,
uri: adUrl,
mimeType: staticResource.attributes['creativeType'] || null,
isSkippable: false,
skipOffset: null,
skipFor: null,
canJump: false,
resumeOffset: 0,
playoutLimit,
once: true,
pre: currentTime == null,
post: currentTime == Infinity,
timelineRange: false,
loop: false,
overlay: {
viewport: {
x: 0,
y: 0,
},
topLeft: {
x: 0,
y: 0,
},
size: {
x: width,
y: height,
},
},
});
};
for (const creative of TXml.findChildren(creatives, 'Creative')) {
const linear = TXml.findChild(creative, 'Linear');
if (linear) {
processLinearAd(linear);
}
const nonLinearAds = TXml.findChild(creative, 'NonLinearAds');
if (nonLinearAds) {
const nonLinears = TXml.findChildren(nonLinearAds, 'NonLinear');
for (const nonLinear of nonLinears) {
processNonLinearAd(nonLinear);
}
}
}
}
return interstitials;
Expand Down
34 changes: 25 additions & 9 deletions lib/ads/interstitial_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ shaka.ads.InterstitialAdManager = class {
} else {
const difference = interstitial.startTime - this.lastTime_;
if (difference > 0 && difference <= 10) {
if (!this.preloadManagerInterstitials_.has(interstitial)) {
if (!this.preloadManagerInterstitials_.has(interstitial) &&
this.isPreloadAllowed_(interstitial)) {
this.preloadManagerInterstitials_.set(
interstitial, this.player_.preload(
interstitial.uri,
Expand Down Expand Up @@ -434,7 +435,8 @@ shaka.ads.InterstitialAdManager = class {
}
}
if (shouldPreload) {
if (!this.preloadManagerInterstitials_.has(interstitial)) {
if (!this.preloadManagerInterstitials_.has(interstitial) &&
this.isPreloadAllowed_(interstitial)) {
this.preloadManagerInterstitials_.set(
interstitial, this.player_.preload(
interstitial.uri,
Expand Down Expand Up @@ -697,13 +699,7 @@ shaka.ads.InterstitialAdManager = class {
// interstitial below.
const nextCurrentInterstitial = this.getCurrentInterstitial_(
interstitial.pre, adPosition - oncePlayed);
if (nextCurrentInterstitial) {
this.onEvent_(
new shaka.util.FakeEvent(shaka.ads.Utils.AD_STOPPED));
this.adEventManager_.removeAll();
this.setupAd_(nextCurrentInterstitial, sequenceLength,
++adPosition, initialTime, oncePlayed);
} else {
if (!nextCurrentInterstitial || nextCurrentInterstitial.overlay) {
if (interstitial.post) {
this.lastTime_ = null;
this.lastPlayedAd_ = null;
Expand Down Expand Up @@ -740,6 +736,12 @@ shaka.ads.InterstitialAdManager = class {
this.cuepointsChanged_();
}
this.determineIfUsingBaseVideo_();
} else {
this.onEvent_(
new shaka.util.FakeEvent(shaka.ads.Utils.AD_STOPPED));
this.adEventManager_.removeAll();
this.setupAd_(nextCurrentInterstitial, sequenceLength,
++adPosition, initialTime, oncePlayed);
}
};
const error = async (e) => {
Expand Down Expand Up @@ -1153,6 +1155,20 @@ shaka.ads.InterstitialAdManager = class {
return response.data;
}

/**
* @param {!shaka.extern.AdInterstitial} interstitial
* @return {boolean}
* @private
*/
isPreloadAllowed_(interstitial) {
const interstitialMimeType = interstitial.mimeType;
if (!interstitialMimeType) {
return true;
}
return !interstitialMimeType.startsWith('image/') &&
interstitialMimeType !== 'text/html';
}


/**
* Only for testing
Expand Down
65 changes: 65 additions & 0 deletions test/ads/interstitial_ad_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,71 @@ describe('Interstitial Ad manager', () => {
jasmine.objectContaining(eventValue1));
});

it('supports non-linear ads', async () => {
const vast = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<VAST version="3.0">',
'<Ad id="5925573263">',
'<InLine>',
'<Creatives>',
'<Creative id="138381721867" sequence="1">',
'<NonLinearAds>',
'<NonLinear width="535" height="80" minSuggestedDuration="00:00:05">',
'<StaticResource creativeType="image/png">',
'<![CDATA[test.png]]>',
'</StaticResource>',
'</NonLinear>',
'</NonLinearAds>',
'</Creative>',
'</Creatives>',
'</InLine>',
'</Ad>',
'</VAST>',
].join('');

networkingEngine.setResponseText('test:/vast', vast);

await interstitialAdManager.addAdUrlInterstitial('test:/vast');

expect(onEventSpy).not.toHaveBeenCalled();

const interstitials = interstitialAdManager.getInterstitials();
expect(interstitials.length).toBe(1);
const expectedInterstitial = {
id: null,
startTime: 0,
endTime: null,
uri: 'test.png',
mimeType: 'image/png',
isSkippable: false,
skipOffset: null,
skipFor: null,
canJump: false,
resumeOffset: 0,
playoutLimit: 5,
once: true,
pre: true,
post: false,
timelineRange: false,
loop: false,
overlay: {
viewport: {
x: 0,
y: 0,
},
topLeft: {
x: 0,
y: 0,
},
size: {
x: 535,
y: 80,
},
},
};
expect(interstitials[0]).toEqual(expectedInterstitial);
});

it('ignore empty', async () => {
const vast = [
'<?xml version="1.0" encoding="UTF-8"?>',
Expand Down
Loading