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(DASH): Add support for location in Content Steering #5914

Merged
merged 3 commits into from
Nov 22, 2023
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
63 changes: 52 additions & 11 deletions lib/dash/dash_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,18 @@ shaka.dash.DashParser = class {
async requestManifest_() {
const requestType = shaka.net.NetworkingEngine.RequestType.MANIFEST;
const type = shaka.net.NetworkingEngine.AdvancedRequestType.MPD;
const getManifestUris = () => {
avelad marked this conversation as resolved.
Show resolved Hide resolved
if (this.manifestUris_.length > 1 && this.contentSteeringManager_) {
const locations = this.contentSteeringManager_.getLocations(
'Location', /* ignoreBaseUrls= */ true);
if (locations.length) {
return locations;
}
}
return this.manifestUris_;
};
const request = shaka.net.NetworkingEngine.makeRequest(
this.manifestUris_, this.config_.retryParameters);
getManifestUris(), this.config_.retryParameters);
const startTime = Date.now();

const response = await this.makeNetworkRequest_(
Expand Down Expand Up @@ -347,27 +357,54 @@ shaka.dash.DashParser = class {
* @private
*/
async processManifest_(mpd, finalManifestUri) {
const Functional = shaka.util.Functional;
const XmlUtils = shaka.util.XmlUtils;

const manifestPreprocessor = this.config_.dash.manifestPreprocessor;
if (manifestPreprocessor) {
manifestPreprocessor(mpd);
}

if (this.contentSteeringManager_) {
this.contentSteeringManager_.clearPreviousLocations();
}

// Get any Location elements. This will update the manifest location and
// the base URI.
/** @type {!Array.<string>} */
let manifestBaseUris = [finalManifestUri];
/** @type {!Array.<string>} */
const locations = XmlUtils.findChildren(mpd, 'Location')
.map(XmlUtils.getContents)
.filter(Functional.isNotNull);
if (locations.length > 0) {
const absoluteLocations = shaka.util.ManifestParserUtils.resolveUris(
manifestBaseUris, locations);
this.manifestUris_ = absoluteLocations;
manifestBaseUris = absoluteLocations;
const locations = [];
/** @type {!Map.<string, string>} */
const locationsMapping = new Map();
const locationsObjs = XmlUtils.findChildren(mpd, 'Location');
for (const locationsObj of locationsObjs) {
const serviceLocation = locationsObj.getAttribute('serviceLocation');
const uri = XmlUtils.getContents(locationsObj);
if (!uri) {
continue;
}
const finalUri = shaka.util.ManifestParserUtils.resolveUris(
manifestBaseUris, [uri])[0];
if (serviceLocation) {
if (this.contentSteeringManager_) {
this.contentSteeringManager_.addLocation(
'Location', serviceLocation, finalUri);
} else {
locationsMapping.set(serviceLocation, finalUri);
}
}
locations.push(finalUri);
}
if (this.contentSteeringManager_) {
const steeringlocations = this.contentSteeringManager_.getLocations(
'Location', /* ignoreBaseUrls= */ true);
if (steeringlocations.length > 0) {
this.manifestUris_ = steeringlocations;
manifestBaseUris = steeringlocations;
}
} else if (locations.length) {
this.manifestUris_ = locations;
manifestBaseUris = locations;
}

let contentSteeringPromise = Promise.resolve();
Expand Down Expand Up @@ -400,13 +437,17 @@ shaka.dash.DashParser = class {
this.contentSteeringManager_.setBaseUris(manifestBaseUris);
this.contentSteeringManager_.setDefaultPathwayId(defaultPathwayId);
}
for (const serviceLocation of locationsMapping.keys()) {
const uri = locationsMapping.get(serviceLocation);
this.contentSteeringManager_.addLocation(
'Location', serviceLocation, uri);
}
}

const uriObjs = XmlUtils.findChildren(mpd, 'BaseURL');
let calculatedBaseUris;
let someLocationValid = false;
if (this.contentSteeringManager_) {
this.contentSteeringManager_.clearPreviousLocations();
for (const uriObj of uriObjs) {
const serviceLocation = uriObj.getAttribute('serviceLocation');
const uri = XmlUtils.getContents(uriObj);
Expand Down
6 changes: 5 additions & 1 deletion lib/util/content_steering_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ shaka.util.ContentSteeringManager = class {
* Get the base locations ordered according the priority.
*
* @param {string|number} streamId
* @param {boolean=} ignoreBaseUrls
* @return {!Array.<string>}
*/
getLocations(streamId) {
getLocations(streamId, ignoreBaseUrls = false) {
const streamLocations = this.locations_.get(streamId) || new Map();
/** @type {!Array.<!{pathwayId: string, location: string}>} */
let locationsPathwayIdMap = [];
Expand Down Expand Up @@ -305,6 +306,9 @@ shaka.util.ContentSteeringManager = class {
locations.push(location);
}
}
if (ignoreBaseUrls) {
return locations;
}
return shaka.util.ManifestParserUtils.resolveUris(
this.baseUris_, locations);
}
Expand Down
62 changes: 62 additions & 0 deletions test/dash/dash_parser_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1539,4 +1539,66 @@ describe('DashParser Live', () => {
shaka.test.ManifestParser.makeReference('s5.mp4', 8, 10, originalUri),
]);
});

it('supports ContentSteering with location change', async () => {
const manifestText = [
'<MPD type="dynamic" availabilityStartTime="1970-01-01T00:00:00Z"',
' suggestedPresentationDelay="PT5S"',
' minimumUpdatePeriod="PT' + updateTime + 'S">',
' <Location serviceLocation="a">http://foobar</Location>',
' <Location serviceLocation="b">http://foobar2</Location>',
' <Location serviceLocation="c">foobar3</Location>',
' <ContentSteering defaultServiceLocation="b" ',
'queryBeforeStart="true">http://contentsteering</ContentSteering>',
' <Period id="1" duration="PT10S">',
' <AdaptationSet mimeType="video/mp4">',
' <Representation id="3" bandwidth="500">',
'<SegmentTemplate startNumber="1" media="s$Number$.mp4" duration="2" />',
' </Representation>',
' </AdaptationSet>',
' </Period>',
'</MPD>',
].join('\n');

const contentSteering = JSON.stringify({
'VERSION': 1,
'TTL': 100,
'RELOAD-URI': 'http://contentsteering/update',
'PATHWAY-PRIORITY': [
'a',
'c',
'b',
],
});

fakeNetEngine
.setResponseText('dummy://foo', manifestText)
.setResponseText('http://contentsteering', contentSteering)
.setMaxUris(3);

const manifestRequest = shaka.net.NetworkingEngine.RequestType.MANIFEST;
const manifestContext = {
type: shaka.net.NetworkingEngine.AdvancedRequestType.MPD,
};

await parser.start('dummy://foo', playerInterface);

fakeNetEngine.request.calls.reset();

// Create a mock so we can verify it gives two URIs.
// The third location is a relative url, and should be resolved as an
// absolute url.
fakeNetEngine.request.and.callFake((type, request, context) => {
expect(type).toBe(manifestRequest);
expect(context).toEqual(manifestContext);
expect(request.uris).toEqual(
['http://foobar', 'dummy://foo/foobar3', 'http://foobar2']);
const data = shaka.util.StringUtils.toUTF8(manifestText);
return shaka.util.AbortableOperation.completed(
{uri: request.uris[0], data: data, headers: {}});
});

await updateManifest();
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
});
});
Loading