Skip to content

Commit

Permalink
feat(CMCD): Add support to rtp parameter (shaka-project#6184)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored and Vasanthavanan-Devarajan committed Feb 20, 2024
1 parent 5ff5161 commit 120c1c8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions demo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ shakaDemo.Config = class {
.addBoolInput_('Enabled', 'cmcd.enabled')
.addTextInput_('Session ID', 'cmcd.sessionId')
.addTextInput_('Content ID', 'cmcd.contentId')
.addNumberInput_('RTP safety Factor', 'cmcd.rtpSafetyFactor',
/* canBeDecimal= */ true)
.addBoolInput_('Use Headers', 'cmcd.useHeaders');
}

Expand Down
6 changes: 5 additions & 1 deletion externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,8 @@ shaka.extern.AdvancedAbrConfiguration;
* enabled: boolean,
* useHeaders: boolean,
* sessionId: string,
* contentId: string
* contentId: string,
* rtpSafetyFactor: number
* }}
*
* @description
Expand All @@ -1538,6 +1539,9 @@ shaka.extern.AdvancedAbrConfiguration;
* characters. This value is consistent across multiple different sessions and
* devices and is defined and updated at the discretion of the service
* provider.
* @property {number} rtpSafetyFactor
* RTP safety factor.
* Defaults to <code>5</code>.
* @exportDoc
*/
shaka.extern.CmcdConfiguration;
Expand Down
35 changes: 33 additions & 2 deletions lib/util/cmcd_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ goog.require('shaka.log');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.ArrayUtils');

goog.requireType('shaka.media.SegmentReference');

/**
* @summary
* A CmcdManager maintains CMCD state as well as a collection of utility
Expand Down Expand Up @@ -235,6 +237,10 @@ shaka.util.CmcdManager = class {
}
}
}
const rtp = this.calculateRtp_(stream, segment);
if (!isNaN(rtp)) {
data.rtp = rtp;
}
}
}

Expand Down Expand Up @@ -373,8 +379,6 @@ shaka.util.CmcdManager = class {
data.su = this.buffering_;
}

// TODO: Implement rtp

if (useHeaders) {
const headers = shaka.util.CmcdManager.toHeaders(data);
if (!Object.keys(headers).length) {
Expand Down Expand Up @@ -554,6 +558,33 @@ shaka.util.CmcdManager = class {
return toPath.join('/');
}

/**
* Calculate requested maximun throughput
*
* @param {shaka.extern.Stream} stream
* @param {shaka.media.SegmentReference} segment
* @return {number}
* @private
*/
calculateRtp_(stream, segment) {
const playbackRate = this.playerInterface_.getPlaybackRate() || 1;
const currentBufferLevel =
this.getRemainingBufferLength_(stream.type) || 500;
const bandwidth = stream.bandwidth;
if (!bandwidth) {
return NaN;
}
const segmentDuration = segment.endTime - segment.startTime;
// Calculate file size in kilobits
const segmentSize = bandwidth * segmentDuration / 1000;
// Calculate time available to load file in seconds
const timeToLoad = (currentBufferLevel / playbackRate) / 1000;
// Calculate the exact bandwidth required
const minBandwidth = segmentSize / timeToLoad;
// Include a safety buffer
return minBandwidth * this.config_.rtpSafetyFactor;
}

/**
* Get the stream format
*
Expand Down
1 change: 1 addition & 0 deletions lib/util/player_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ shaka.util.PlayerConfiguration = class {
enabled: false,
sessionId: '',
contentId: '',
rtpSafetyFactor: 5,
useHeaders: false,
};

Expand Down
1 change: 1 addition & 0 deletions test/util/cmcd_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe('CmcdManager', () => {
enabled: false,
sessionId: '',
contentId: 'testing',
rtpSafetyFactor: 5,
useHeaders: false,
};

Expand Down

0 comments on commit 120c1c8

Please sign in to comment.