Skip to content

Commit

Permalink
Fix reporting of CMCD for SegmentBase range requests (#4189)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsilhavy committed May 18, 2023
1 parent 0982920 commit fb5fb74
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/streaming/models/CmcdModel.js
Expand Up @@ -289,7 +289,9 @@ function CmcdModel() {
if (!rtp) {
rtp = _calculateRtp(request);
}
data.rtp = rtp;
if (!isNaN(rtp)) {
data.rtp = rtp;
}

if (nextRequest) {
if (request.url !== nextRequest.url) {
Expand Down Expand Up @@ -429,7 +431,7 @@ function CmcdModel() {

function _getObjectDurationByRequest(request) {
try {
return !isNaN(request.duration) ? Math.round(request.duration * 1000) : null;
return !isNaN(request.duration) ? Math.round(request.duration * 1000) : NaN;
} catch (e) {
return null;
}
Expand Down Expand Up @@ -567,24 +569,32 @@ function CmcdModel() {
}

function _calculateRtp(request) {
// Get the values we need
let playbackRate = playbackController.getPlaybackRate();
if (!playbackRate) playbackRate = 1;
let { quality, mediaType, mediaInfo, duration } = request;
let currentBufferLevel = _getBufferLevelByType(mediaType);
if (currentBufferLevel === 0) currentBufferLevel = 500;
let bitrate = mediaInfo.bitrateList[quality].bandwidth;

// Calculate RTP
let segmentSize = (bitrate * duration) / 1000; // Calculate file size in kilobits
let timeToLoad = (currentBufferLevel / playbackRate) / 1000; // Calculate time available to load file in seconds
let minBandwidth = segmentSize / timeToLoad; // Calculate the exact bandwidth required
let rtpSafetyFactor = settings.get().streaming.cmcd.rtpSafetyFactor && !isNaN(settings.get().streaming.cmcd.rtpSafetyFactor) ? settings.get().streaming.cmcd.rtpSafetyFactor : RTP_SAFETY_FACTOR;
let maxBandwidth = minBandwidth * rtpSafetyFactor; // Include a safety buffer

let rtp = (parseInt(maxBandwidth / 100) + 1) * 100; // Round to the next multiple of 100

return rtp;
try {
// Get the values we need
let playbackRate = playbackController.getPlaybackRate();
if (!playbackRate) playbackRate = 1;
let { quality, mediaType, mediaInfo, duration } = request;

if (!mediaInfo) {
return NaN;
}
let currentBufferLevel = _getBufferLevelByType(mediaType);
if (currentBufferLevel === 0) currentBufferLevel = 500;
let bitrate = mediaInfo.bitrateList[quality].bandwidth;

// Calculate RTP
let segmentSize = (bitrate * duration) / 1000; // Calculate file size in kilobits
let timeToLoad = (currentBufferLevel / playbackRate) / 1000; // Calculate time available to load file in seconds
let minBandwidth = segmentSize / timeToLoad; // Calculate the exact bandwidth required
let rtpSafetyFactor = settings.get().streaming.cmcd.rtpSafetyFactor && !isNaN(settings.get().streaming.cmcd.rtpSafetyFactor) ? settings.get().streaming.cmcd.rtpSafetyFactor : RTP_SAFETY_FACTOR;
let maxBandwidth = minBandwidth * rtpSafetyFactor; // Include a safety buffer


// Round to the next multiple of 100
return (parseInt(maxBandwidth / 100) + 1) * 100;
} catch (e) {
return NaN;
}
}

function reset() {
Expand Down

0 comments on commit fb5fb74

Please sign in to comment.