Skip to content

Commit 47eeb46

Browse files
authored
chore(Demo): Improve ad behavior (#8285)
Allow preload adTagUri to improve the behavior with pre-roll Include the AD feature when using a custom asset
1 parent fad638e commit 47eeb46

File tree

3 files changed

+97
-50
lines changed

3 files changed

+97
-50
lines changed

demo/common/asset.js

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,37 @@ const ShakaDemoAssetInfo = class {
161161
return this;
162162
}
163163

164+
/**
165+
* @param {shakaAssets.Feature} feature
166+
* @return {!ShakaDemoAssetInfo}
167+
*/
168+
removeFeature(feature) {
169+
this.features = this.features.filter((f) => f != feature);
170+
// Sort the features list, so that features are in a predictable order.
171+
this.features.sort(ShakaDemoAssetInfo.caseLessAlphaComparator_);
172+
return this;
173+
}
174+
175+
/**
176+
* @private
177+
*/
178+
checkAdFeature_() {
179+
let isAd = false;
180+
if (this.adTagUri || (this.imaVideoId && this.imaContentSrcId) ||
181+
this.imaAssetKey || this.mediaTailorUrl) {
182+
isAd = true;
183+
}
184+
if (isAd) {
185+
if (!this.features.includes(shakaAssets.Feature.ADS)) {
186+
this.addFeature(shakaAssets.Feature.ADS);
187+
}
188+
} else {
189+
if (this.features.includes(shakaAssets.Feature.ADS)) {
190+
this.removeFeature(shakaAssets.Feature.ADS);
191+
}
192+
}
193+
}
194+
164195
/**
165196
* @param {shakaAssets.KeySystem} keySystem
166197
* @return {!ShakaDemoAssetInfo}
@@ -263,8 +294,8 @@ const ShakaDemoAssetInfo = class {
263294
* @return {!ShakaDemoAssetInfo}
264295
*/
265296
setAdTagUri(uri) {
266-
this.adTagUri = uri;
267-
this.addFeature(shakaAssets.Feature.ADS);
297+
this.adTagUri = uri.trim();
298+
this.checkAdFeature_();
268299
return this;
269300
}
270301

@@ -273,10 +304,8 @@ const ShakaDemoAssetInfo = class {
273304
* @return {!ShakaDemoAssetInfo}
274305
*/
275306
setIMAContentSourceId(id) {
276-
this.imaContentSrcId = id;
277-
if (!this.features.includes(shakaAssets.Feature.ADS)) {
278-
this.addFeature(shakaAssets.Feature.ADS);
279-
}
307+
this.imaContentSrcId = id.trim();
308+
this.checkAdFeature_();
280309

281310
return this;
282311
}
@@ -286,10 +315,8 @@ const ShakaDemoAssetInfo = class {
286315
* @return {!ShakaDemoAssetInfo}
287316
*/
288317
setIMAVideoId(id) {
289-
this.imaVideoId = id;
290-
if (!this.features.includes(shakaAssets.Feature.ADS)) {
291-
this.addFeature(shakaAssets.Feature.ADS);
292-
}
318+
this.imaVideoId = id.trim();
319+
this.checkAdFeature_();
293320

294321
return this;
295322
}
@@ -299,10 +326,8 @@ const ShakaDemoAssetInfo = class {
299326
* @return {!ShakaDemoAssetInfo}
300327
*/
301328
setIMAAssetKey(key) {
302-
this.imaAssetKey = key;
303-
if (!this.features.includes(shakaAssets.Feature.ADS)) {
304-
this.addFeature(shakaAssets.Feature.ADS);
305-
}
329+
this.imaAssetKey = key.trim();
330+
this.checkAdFeature_();
306331

307332
return this;
308333
}
@@ -312,10 +337,8 @@ const ShakaDemoAssetInfo = class {
312337
* @return {!ShakaDemoAssetInfo}
313338
*/
314339
setIMAManifestType(type) {
315-
this.imaManifestType = type;
316-
if (!this.features.includes(shakaAssets.Feature.ADS)) {
317-
this.addFeature(shakaAssets.Feature.ADS);
318-
}
340+
this.imaManifestType = type.trim();
341+
this.checkAdFeature_();
319342

320343
return this;
321344
}
@@ -326,15 +349,20 @@ const ShakaDemoAssetInfo = class {
326349
* @return {!ShakaDemoAssetInfo}
327350
*/
328351
setMediaTailor(url, adsParams=null) {
329-
this.mediaTailorUrl = url;
352+
this.mediaTailorUrl = url.trim();
330353
this.mediaTailorAdsParams = adsParams;
331-
if (!this.features.includes(shakaAssets.Feature.ADS)) {
332-
this.addFeature(shakaAssets.Feature.ADS);
333-
}
354+
this.checkAdFeature_();
334355

335356
return this;
336357
}
337358

359+
/**
360+
* @return {boolean}
361+
*/
362+
hasAds() {
363+
return this.features.includes(shakaAssets.Feature.ADS);
364+
}
365+
338366
/**
339367
* @param {string} headerName
340368
* @param {string} headerValue

demo/custom.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ shakaDemo.Custom = class {
288288
}
289289
};
290290
const adTagOnChange = (input) => {
291-
assetInProgress.adTagUri = input.value;
291+
assetInProgress.setAdTagUri(input.value);
292292
};
293293
this.makeField_(container, 'Ad Tag URL', adTagSetup, adTagOnChange);
294294

@@ -302,7 +302,7 @@ shakaDemo.Custom = class {
302302
this.checkManifestRequired_(assetInProgress);
303303
};
304304
const contentSrcIdOnChange = (input) => {
305-
assetInProgress.imaContentSrcId = input.value;
305+
assetInProgress.setIMAContentSourceId(input.value);
306306
this.manifestField_.required =
307307
this.checkManifestRequired_(assetInProgress);
308308
};
@@ -320,7 +320,7 @@ shakaDemo.Custom = class {
320320
this.checkManifestRequired_(assetInProgress);
321321
};
322322
const videoIdOnChange = (input) => {
323-
assetInProgress.imaVideoId = input.value;
323+
assetInProgress.setIMAVideoId(input.value);
324324
this.manifestField_.required =
325325
this.checkManifestRequired_(assetInProgress);
326326
};
@@ -337,7 +337,7 @@ shakaDemo.Custom = class {
337337
this.checkManifestRequired_(assetInProgress);
338338
};
339339
const assetKeyChange = (input) => {
340-
assetInProgress.imaAssetKey = input.value;
340+
assetInProgress.setIMAAssetKey(input.value);
341341
this.manifestField_.required =
342342
this.checkManifestRequired_(assetInProgress);
343343
};
@@ -354,7 +354,7 @@ shakaDemo.Custom = class {
354354
this.checkManifestRequired_(assetInProgress);
355355
};
356356
const manifestTypeChange = (input) => {
357-
assetInProgress.imaManifestType = input.value;
357+
assetInProgress.setIMAManifestType(input.value);
358358
this.manifestField_.required =
359359
this.checkManifestRequired_(assetInProgress);
360360
};

demo/main.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,48 @@ shakaDemo.Main = class {
14261426
ui.configure(uiConfig);
14271427
}
14281428

1429+
if (asset.hasAds()) {
1430+
// The player internally, if another stream is loaded, calls
1431+
// adManager.onAssetUnload and this would prevent the initial preloading
1432+
// of the ad, so we unload the player first to prevent the player
1433+
// from being unloaded the new ad.
1434+
const loadMode = this.player_.getLoadMode();
1435+
if (loadMode == shaka.Player.LoadMode.MEDIA_SOURCE ||
1436+
loadMode == shaka.Player.LoadMode.SRC_EQUALS) {
1437+
await this.player_.unload();
1438+
}
1439+
}
1440+
1441+
// If the asset has an ad tag attached to it, load the ads
1442+
const adManager = this.player_.getAdManager();
1443+
if (adManager && asset.adTagUri) {
1444+
const adTagUri = asset.adTagUri + Date.now();
1445+
if (asset.useIMA) {
1446+
try {
1447+
// If IMA is blocked by an AdBlocker, init() will throw.
1448+
// If that happens, just proceed to load.
1449+
goog.asserts.assert(
1450+
this.video_ != null, 'this.video should exist!');
1451+
adManager.initClientSide(
1452+
this.controls_.getClientSideAdContainer(), this.video_,
1453+
/** adsRenderingSettings= */ null);
1454+
const adRequest = new google.ima.AdsRequest();
1455+
adRequest.adTagUrl = adTagUri;
1456+
adManager.requestClientSideAds(adRequest);
1457+
} catch (error) {
1458+
console.log(error);
1459+
console.warn('Ads code has been prevented from running. ' +
1460+
'Proceeding without ads.');
1461+
}
1462+
} else {
1463+
try {
1464+
await adManager.addAdUrlInterstitial(adTagUri);
1465+
} catch (error) {
1466+
console.log(error);
1467+
}
1468+
}
1469+
}
1470+
14291471
// Finally, the asset can be loaded.
14301472
if (asset.preloadManager) {
14311473
const preloadManager = asset.preloadManager;
@@ -1474,29 +1516,6 @@ shakaDemo.Main = class {
14741516
}
14751517
}
14761518

1477-
// If the asset has an ad tag attached to it, load the ads
1478-
const adManager = this.player_.getAdManager();
1479-
if (adManager && asset.adTagUri && asset.useIMA) {
1480-
try {
1481-
// If IMA is blocked by an AdBlocker, init() will throw.
1482-
// If that happens, just proceed to load.
1483-
goog.asserts.assert(this.video_ != null, 'this.video should exist!');
1484-
adManager.initClientSide(
1485-
this.controls_.getClientSideAdContainer(), this.video_,
1486-
/** adsRenderingSettings= */ null);
1487-
const adRequest = new google.ima.AdsRequest();
1488-
adRequest.adTagUrl = asset.adTagUri;
1489-
adManager.requestClientSideAds(adRequest);
1490-
} catch (error) {
1491-
console.log(error);
1492-
console.warn('Ads code has been prevented from running. ' +
1493-
'Proceeding without ads.');
1494-
}
1495-
}
1496-
if (adManager && asset.adTagUri && !asset.useIMA) {
1497-
adManager.addAdUrlInterstitial(asset.adTagUri);
1498-
}
1499-
15001519
// Set media session title, but only if the browser supports that API.
15011520
if (navigator.mediaSession) {
15021521
const icon = asset.iconUri || shakaDemo.Main.logo_;

0 commit comments

Comments
 (0)