Skip to content

Commit

Permalink
feat(CMCD): Add support to dl, nrr and nor parameters (#6171)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jan 29, 2024
1 parent 5b141eb commit 8a9f17d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
95 changes: 94 additions & 1 deletion lib/util/cmcd_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ goog.provide('shaka.util.CmcdManager');
goog.require('goog.Uri');
goog.require('shaka.log');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.ArrayUtils');

/**
* @summary
Expand Down Expand Up @@ -193,11 +194,45 @@ shaka.util.CmcdManager = class {
if (stream) {
if (isMedia) {
data.bl = this.getBufferLength_(stream.type);
if (data.ot !== ObjectType.TIMED_TEXT) {
const remainingBufferLength =
this.getRemainingBufferLength_(stream.type);
const playbackRate = this.playerInterface_.getPlaybackRate();
if (playbackRate) {
data.dl = remainingBufferLength / Math.abs(playbackRate);
} else {
data.dl = remainingBufferLength;
}
}
}

if (stream.bandwidth) {
data.br = stream.bandwidth / 1000;
}

if (stream.segmentIndex && segment) {
const iterator = stream.segmentIndex.getIteratorForTime(
segment.endTime, /* allowNonIndepedent= */ true);
if (iterator) {
const nextSegment = iterator.next().value;
if (nextSegment && nextSegment != segment) {
if (!shaka.util.ArrayUtils.equal(
segment.getUris(), nextSegment.getUris())) {
data.nor = this.urlToRelativePath_(
nextSegment.getUris()[0], request.uris[0]);
}
if ((nextSegment.startByte || nextSegment.endByte) &&
(segment.startByte != nextSegment.startByte ||
segment.endByte != nextSegment.endByte)) {
let range = nextSegment.startByte + '-';
if (nextSegment.endByte) {
range += nextSegment.endByte;
}
data.nrr = range;
}
}
}
}
}

if (isMedia && data.ot !== ObjectType.TIMED_TEXT) {
Expand Down Expand Up @@ -335,7 +370,7 @@ shaka.util.CmcdManager = class {
data.su = this.buffering_;
}

// TODO: Implement rtp, nrr, nor, dl
// TODO: Implement rtp

if (useHeaders) {
const headers = shaka.util.CmcdManager.toHeaders(data);
Expand Down Expand Up @@ -458,6 +493,64 @@ shaka.util.CmcdManager = class {
return (range.end - start) * 1000;
}

/**
* Get the remaining buffer length for a media type in milliseconds
*
* @param {string} type
* @return {number}
* @private
*/
getRemainingBufferLength_(type) {
const ranges = this.playerInterface_.getBufferedInfo()[type];

if (!ranges.length) {
return 0;
}

const start = this.playerInterface_.getCurrentTime();
const range = ranges.find((r) => r.start <= start && r.end >= start);

if (!range) {
return 0;
}

return (range.end - start) * 1000;
}

/**
* Constructs a relative path from a URL
*
* @param {string} url
* @param {string} base
* @return {string}
* @private
*/
urlToRelativePath_(url, base) {
const to = new URL(url);
const from = new URL(base);

if (to.origin !== from.origin) {
return url;
}

const toPath = to.pathname.split('/').slice(1);
const fromPath = from.pathname.split('/').slice(1, -1);

// remove common parents
while (toPath[0] === fromPath[0]) {
toPath.shift();
fromPath.shift();
}

// add back paths
while (fromPath.length) {
fromPath.shift();
toPath.unshift('..');
}

return toPath.join('/');
}

/**
* Get the stream format
*
Expand Down
10 changes: 5 additions & 5 deletions test/util/cmcd_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ describe('CmcdManager', () => {
it('modifies segment request uris', () => {
const r = ObjectUtils.cloneObject(request);
cmcdManager.applySegmentData(r, segmentInfo);
const uri = 'https://test.com/test.mpd?CMCD=bl%3D21200%2Cbr%3D5234%2Ccid%3D%22' +
'testing%22%2Cd%3D3330%2Cmtp%3D10000%2Cot%3Dv%2Csf%3Dd%2C' +
'sid%3D%222ed2d1cd-970b-48f2-bfb3-50a79e87cfa3%22%2Cst%3Dv%2Csu%2C' +
'tb%3D4000';
const uri = 'https://test.com/test.mpd?CMCD=bl%3D21200%2Cbr%3D5234%' +
'2Ccid%3D%22testing%22%2Cd%3D3330%2Cdl%3D21200%2Cmtp%3D10000%2Cot%' +
'3Dv%2Csf%3Dd%2Csid%3D%222ed2d1cd-970b-48f2-bfb3-50a79e87cfa3%22%2' +
'Cst%3Dv%2Csu%2Ctb%3D4000';
expect(r.uris[0]).toBe(uri);
});

Expand Down Expand Up @@ -250,7 +250,7 @@ describe('CmcdManager', () => {
expect(r.headers).toEqual({
'testing': '1234',
'CMCD-Object': 'br=5234,d=3330,ot=v,tb=4000',
'CMCD-Request': 'bl=21200,mtp=10000,su',
'CMCD-Request': 'bl=21200,dl=21200,mtp=10000,su',
'CMCD-Session': 'cid="testing",sf=d,' +
'sid="2ed2d1cd-970b-48f2-bfb3-50a79e87cfa3",st=v',
});
Expand Down

0 comments on commit 8a9f17d

Please sign in to comment.