Skip to content

Commit

Permalink
Remove try/catch in InsufficientBufferRule.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dsilhavy committed Jul 14, 2021
1 parent cda97d6 commit 499008f
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions src/streaming/rules/abr/InsufficientBufferRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,48 +78,44 @@ function InsufficientBufferRule(config) {
function getMaxIndex(rulesContext) {
const switchRequest = SwitchRequest(context).create();

try {
if (!rulesContext || !rulesContext.hasOwnProperty('getMediaType')) {
return switchRequest;
}

if (!rulesContext || !rulesContext.hasOwnProperty('getMediaType')) {
return switchRequest;
}
checkConfig();

checkConfig();
const mediaType = rulesContext.getMediaType();
const currentBufferState = dashMetrics.getCurrentBufferState(mediaType);
const representationInfo = rulesContext.getRepresentationInfo();
const fragmentDuration = representationInfo.fragmentDuration;
const streamInfo = rulesContext.getStreamInfo();
const streamId = streamInfo ? streamInfo.id : null;

const mediaType = rulesContext.getMediaType();
const currentBufferState = dashMetrics.getCurrentBufferState(mediaType);
const representationInfo = rulesContext.getRepresentationInfo();
const fragmentDuration = representationInfo.fragmentDuration;
const streamInfo = rulesContext.getStreamInfo();
const streamId = streamInfo ? streamInfo.id : null;
// Don't ask for a bitrate change if there is not info about buffer state or if fragmentDuration is not defined
if (shouldIgnore(mediaType) || !fragmentDuration) {
return switchRequest;
}

// Don't ask for a bitrate change if there is not info about buffer state or if fragmentDuration is not defined
if (shouldIgnore(mediaType) || !fragmentDuration) {
return switchRequest;
}
if (currentBufferState && currentBufferState.state === MetricsConstants.BUFFER_EMPTY) {
logger.debug('[' + mediaType + '] Switch to index 0; buffer is empty.');
switchRequest.quality = 0;
switchRequest.reason = 'InsufficientBufferRule: Buffer is empty';
} else {
const mediaInfo = rulesContext.getMediaInfo();
const abrController = rulesContext.getAbrController();
const throughputHistory = abrController.getThroughputHistory();

const bufferLevel = dashMetrics.getCurrentBufferLevel(mediaType);
const throughput = throughputHistory.getAverageThroughput(mediaType);
const latency = throughputHistory.getAverageLatency(mediaType);
const bitrate = throughput * (bufferLevel / fragmentDuration) * INSUFFICIENT_BUFFER_SAFETY_FACTOR;

switchRequest.quality = abrController.getQualityForBitrate(mediaInfo, bitrate, streamId, latency);
switchRequest.reason = 'InsufficientBufferRule: being conservative to avoid immediate rebuffering';
}

if (currentBufferState && currentBufferState.state === MetricsConstants.BUFFER_EMPTY) {
logger.debug('[' + mediaType + '] Switch to index 0; buffer is empty.');
switchRequest.quality = 0;
switchRequest.reason = 'InsufficientBufferRule: Buffer is empty';
} else {
const mediaInfo = rulesContext.getMediaInfo();
const abrController = rulesContext.getAbrController();
const throughputHistory = abrController.getThroughputHistory();

const bufferLevel = dashMetrics.getCurrentBufferLevel(mediaType);
const throughput = throughputHistory.getAverageThroughput(mediaType);
const latency = throughputHistory.getAverageLatency(mediaType);
const bitrate = throughput * (bufferLevel / fragmentDuration) * INSUFFICIENT_BUFFER_SAFETY_FACTOR;

switchRequest.quality = abrController.getQualityForBitrate(mediaInfo, bitrate, streamId, latency);
switchRequest.reason = 'InsufficientBufferRule: being conservative to avoid immediate rebuffering';
}
return switchRequest;

return switchRequest;
} catch (e) {
return switchRequest;
}
}

function shouldIgnore(mediaType) {
Expand Down

0 comments on commit 499008f

Please sign in to comment.