Skip to content

Commit

Permalink
feat(DASH): MPD Alternate support
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Jul 17, 2024
1 parent c59922b commit 297a3a6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions externs/shaka/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ shaka.extern.IAdManager = class extends EventTarget {
*/
onHLSInterstitialMetadata(basePlayer, baseVideo, interstitial) {}

/**
* @param {!shaka.Player} basePlayer
* @param {!HTMLMediaElement} baseVideo
* @param {shaka.extern.TimelineRegionInfo} region
*/
onDASHInterstitialMetadata(basePlayer, baseVideo, region) {}

/**
* @param {shaka.extern.AdInterstitial} interstitial
*/
Expand Down
16 changes: 16 additions & 0 deletions lib/ads/ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,22 @@ shaka.ads.AdManager = class extends shaka.util.FakeEventTarget {
}
}

/**
* @override
* @export
*/
onDASHInterstitialMetadata(basePlayer, baseVideo, region) {
if (region.schemeIdUri != 'urn:mpeg:dash:event:alternativeMPD:2022') {
return;
}
if (!this.interstitialAdManager_) {
this.initInterstitial(/* adContainer= */ null, basePlayer, baseVideo);
}
if (this.interstitialAdManager_) {
this.interstitialAdManager_.addRegion(region);
}
}

/**
* @override
* @export
Expand Down
45 changes: 45 additions & 0 deletions lib/ads/interstitial_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,51 @@ shaka.ads.InterstitialAdManager = class {
}
}

/**
* @param {shaka.extern.TimelineRegionInfo} region
*/
addRegion(region) {
let alternativeMPDUri;
let alternativeMPDmode;
for (const node of region.eventNode.children) {
if (node.tagName == 'AlternativeMPD') {
const uri = node.attributes['uri'];
const mode = node.attributes['mode'];
if (uri) {
alternativeMPDUri = uri;
alternativeMPDmode = mode;
break;
}
}
}
if (!alternativeMPDUri) {
shaka.log.alwaysWarn('Unsupported MPD alternate', region);
return;
}
const isReplace = alternativeMPDmode == 'replace';
const isInsert = alternativeMPDmode == 'insert';
if (!isReplace && !isInsert) {
shaka.log.warning('Unsupported MPD alternate', region);
return;
}
const interstitial = {
id: null,
startTime: region.startTime,
endTime: isReplace ? region.endTime : null,
uri: alternativeMPDUri,
isSkippable: false,
skipOffset: null,
canJump: true,
resumeOffset: isInsert ? 0 : null,
playoutLimit: null,
once: false,
pre: false,
post: false,
timelineRange: isReplace && !isInsert,
};
this.addInterstitials([interstitial]);
}

/**
* @param {string} url
* @return {!Promise}
Expand Down
3 changes: 3 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,9 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
preloadManager.addQueuedOperation(false, () => {
if (this.adManager_) {
this.adManager_.onDashTimedMetadata(region);
goog.asserts.assert(this.video_, 'Must have video');
this.adManager_.onDASHInterstitialMetadata(
this, this.video_, region);
}
});
});
Expand Down
3 changes: 3 additions & 0 deletions test/test/util/fake_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ shaka.test.FakeAdManager = class extends shaka.util.FakeEventTarget {
/** @override */
onHLSInterstitialMetadata(basePlayer, baseVideo, interstitial) {}

/** @override */
onDASHInterstitialMetadata(basePlayer, baseVideo, region) {}

/** @override */
addCustomInterstitial(interstitial) {}

Expand Down

0 comments on commit 297a3a6

Please sign in to comment.