Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 3ded9fd

Browse files
committed
clean code
1 parent b3a0052 commit 3ded9fd

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

app/js/streaming/rules/DownloadRatioRule.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ MediaPlayer.rules.DownloadRatioRule = function() {
2626
name: "DownloadRatioRule",
2727

2828
checkIndex: function(current, metrics, data) {
29-
var self = this,
30-
requests = self.metricsExt.getHttpRequests(metrics),
29+
var requests = this.metricsExt.getHttpRequests(metrics),
3130
lastRequest = null,
3231
downloadTime,
3332
totalTime,
@@ -44,13 +43,13 @@ MediaPlayer.rules.DownloadRatioRule = function() {
4443
p = MediaPlayer.rules.SwitchRequest.prototype.DEFAULT;
4544

4645
if (data && data.hasOwnProperty('type')) {
47-
latencyInBandwidth = self.config.getParamFor(data.type, "ABR.latencyInBandwidth", "boolean", true);
48-
switchUpRatioSafetyFactor = self.config.getParamFor(data.type, "ABR.switchUpRatioSafetyFactor", "number", 1.5);
49-
//self.debug.log("Checking download ratio rule...");
50-
self.debug.log("[DownloadRatioRule][" + data.type + "] Checking download ratio rule... (current = " + current + ")");
46+
latencyInBandwidth = this.config.getParamFor(data.type, "ABR.latencyInBandwidth", "boolean", true);
47+
switchUpRatioSafetyFactor = this.config.getParamFor(data.type, "ABR.switchUpRatioSafetyFactor", "number", 1.5);
48+
//this.debug.log("Checking download ratio rule...");
49+
this.debug.log("[DownloadRatioRule][" + data.type + "] Checking download ratio rule... (current = " + current + ")");
5150

5251
if (!metrics) {
53-
self.debug.log("[DownloadRatioRule][" + data.type + "] No metrics, bailing.");
52+
this.debug.log("[DownloadRatioRule][" + data.type + "] No metrics, bailing.");
5453
return new MediaPlayer.rules.SwitchRequest();
5554
}
5655

@@ -64,38 +63,40 @@ MediaPlayer.rules.DownloadRatioRule = function() {
6463
}
6564

6665
if (lastRequest === null) {
67-
self.debug.log("[DownloadRatioRule][" + data.type + "] No valid requests made for this stream yet, bailing.");
66+
this.debug.log("[DownloadRatioRule][" + data.type + "] No valid requests made for this stream yet, bailing.");
6867
return new MediaPlayer.rules.SwitchRequest();
6968
}
7069

7170
totalTime = (lastRequest.tfinish.getTime() - lastRequest.trequest.getTime()) / 1000;
7271
downloadTime = (lastRequest.tfinish.getTime() - lastRequest.tresponse.getTime()) / 1000;
7372

7473
if (totalTime <= 0) {
75-
self.debug.log("[DownloadRatioRule][" + data.type + "] Don't know how long the download of the last fragment took, bailing.");
74+
this.debug.log("[DownloadRatioRule][" + data.type + "] Don't know how long the download of the last fragment took, bailing.");
7675
return new MediaPlayer.rules.SwitchRequest();
7776
}
7877

7978
if (lastRequest.mediaduration === null ||
8079
lastRequest.mediaduration === undefined ||
8180
lastRequest.mediaduration <= 0 ||
8281
isNaN(lastRequest.mediaduration)) {
83-
self.debug.log("[DownloadRatioRule][" + data.type + "] Don't know the duration of the last media fragment, bailing.");
82+
this.debug.log("[DownloadRatioRule][" + data.type + "] Don't know the duration of the last media fragment, bailing.");
8483
return new MediaPlayer.rules.SwitchRequest();
8584
}
8685

87-
self.debug.log("[DownloadRatioRule][" + data.type + "] DL: " + Number(downloadTime.toFixed(3)) + "s, Total: " + Number(totalTime.toFixed(3)) + "s");
88-
8986
totalBytesLength = lastRequest.bytesLength;
9087

88+
this.debug.log("[DownloadRatioRule][" + data.type + "] DL: " + Number(downloadTime.toFixed(3)) + "s, Total: " + Number(totalTime.toFixed(3)) + "s, Length: " + totalBytesLength);
89+
9190
// Take average bandwidth over 3 requests
9291
count = 1;
9392
while (i >= 0 && count < 3) {
9493
if (requests[i].tfinish && requests[i].trequest && requests[i].tresponse && requests[i].bytesLength > 0) {
95-
self.debug.log("[DownloadRatioRule][" + data.type + "] length: " + requests[i].bytesLength + ", time: " + ((requests[i].tfinish.getTime() - requests[i].trequest.getTime()) / 1000));
94+
var _totalTime = (requests[i].tfinish.getTime() - requests[i].trequest.getTime()) / 1000;
95+
var _downloadTime = (requests[i].tfinish.getTime() - requests[i].tresponse.getTime()) / 1000;
96+
this.debug.log("[DownloadRatioRule][" + data.type + "] DL: " + Number(_downloadTime.toFixed(3)) + "s, Total: " + Number(_totalTime.toFixed(3)) + "s, Length: " + requests[i].bytesLength);
97+
totalTime += _totalTime;
98+
downloadTime += _downloadTime;
9699
totalBytesLength += requests[i].bytesLength;
97-
totalTime += (requests[i].tfinish.getTime() - requests[i].trequest.getTime()) / 1000;
98-
downloadTime += (requests[i].tfinish.getTime() - requests[i].tresponse.getTime()) / 1000;
99100
count += 1;
100101
}
101102
i--;
@@ -106,17 +107,17 @@ MediaPlayer.rules.DownloadRatioRule = function() {
106107

107108
calculatedBandwidth = latencyInBandwidth ? (totalBytesLength / totalTime) : (totalBytesLength / downloadTime);
108109

109-
self.debug.log("[DownloadRatioRule][" + data.type + "] BW = " + Math.round(calculatedBandwidth / 1000) + " kb/s");
110+
this.debug.log("[DownloadRatioRule][" + data.type + "] BW = " + Math.round(calculatedBandwidth / 1000) + " kb/s");
110111

111112
if (isNaN(calculatedBandwidth)) {
112113
return new MediaPlayer.rules.SwitchRequest();
113114
}
114115

115-
count = self.manifestExt.getRepresentationCount(data);
116-
currentRepresentation = self.manifestExt.getRepresentationFor(current, data);
117-
currentBandwidth = self.manifestExt.getBandwidth(currentRepresentation);
116+
count = this.manifestExt.getRepresentationCount(data);
117+
currentRepresentation = this.manifestExt.getRepresentationFor(current, data);
118+
currentBandwidth = this.manifestExt.getBandwidth(currentRepresentation);
118119
for (i = 0; i < count; i += 1) {
119-
bandwidths.push(self.manifestExt.getRepresentationBandwidth(data, i));
120+
bandwidths.push(this.manifestExt.getRepresentationBandwidth(data, i));
120121
}
121122
if (calculatedBandwidth <= currentBandwidth) {
122123
for (i = current - 1; i > 0; i -= 1) {
@@ -127,20 +128,20 @@ MediaPlayer.rules.DownloadRatioRule = function() {
127128
q = i;
128129
p = MediaPlayer.rules.SwitchRequest.prototype.WEAK;
129130

130-
self.debug.info("[DownloadRatioRule][" + data.type + "] SwitchRequest: q=" + q + "/" + (count - 1) + " (" + bandwidths[q] + "), p=" + p);
131+
this.debug.info("[DownloadRatioRule][" + data.type + "] SwitchRequest: q=" + q + "/" + (count - 1) + " (" + bandwidths[q] + "), p=" + p);
131132
return new MediaPlayer.rules.SwitchRequest(q, p);
132133
} else {
133134
for (i = count - 1; i > current; i -= 1) {
134135
if (calculatedBandwidth > (bandwidths[i] * switchUpRatioSafetyFactor)) {
135-
//self.debug.log("[DownloadRatioRule][" + data.type + "] bw = " + calculatedBandwidth + " results[i] * switchUpRatioSafetyFactor =" + (bandwidths[i] * switchUpRatioSafetyFactor) + " with i=" + i);
136+
//this.debug.log("[DownloadRatioRule][" + data.type + "] bw = " + calculatedBandwidth + " results[i] * switchUpRatioSafetyFactor =" + (bandwidths[i] * switchUpRatioSafetyFactor) + " with i=" + i);
136137
break;
137138
}
138139
}
139140

140141
q = i;
141142
p = MediaPlayer.rules.SwitchRequest.prototype.STRONG;
142143

143-
self.debug.info("[DownloadRatioRule][" + data.type + "] SwitchRequest: q=" + q + "/" + (count - 1) + " (" + bandwidths[q] + "), p=" + p);
144+
this.debug.info("[DownloadRatioRule][" + data.type + "] SwitchRequest: q=" + q + "/" + (count - 1) + " (" + bandwidths[q] + "), p=" + p);
144145
return new MediaPlayer.rules.SwitchRequest(q, p);
145146
}
146147
} else {

app/js/streaming/rules/InsufficientBufferRule.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ MediaPlayer.rules.InsufficientBufferRule = function() {
2727
name: "InsufficientBufferRule",
2828

2929
checkIndex: function(current, metrics, data, playerState) {
30-
var self = this,
31-
bufferLevel = self.metricsExt.getCurrentBufferLevel(metrics),
30+
var bufferLevel = this.metricsExt.getCurrentBufferLevel(metrics),
3231
minBufferTime,
3332
switchLowerBufferRatio,
3433
switchLowerBufferTime,
@@ -47,36 +46,36 @@ MediaPlayer.rules.InsufficientBufferRule = function() {
4746

4847
// Check if we start buffering the stream. In this case we ignore the rule
4948
if (playerState === 'buffering') {
50-
self.isStartBuffering[data.type] = true;
49+
this.isStartBuffering[data.type] = true;
5150
}
5251

5352
if (bufferLevel === null) {
5453
return new MediaPlayer.rules.SwitchRequest();
5554
}
5655

57-
self.debug.info("[InsufficientBufferRule][" + data.type + "] Checking buffer level ... (current = " + current +
56+
this.debug.info("[InsufficientBufferRule][" + data.type + "] Checking buffer level ... (current = " + current +
5857
", buffer level = " + (Math.round(bufferLevel.level * 100) / 100) +
59-
", buffering = " + self.isStartBuffering[data.type] + ")");
58+
", buffering = " + this.isStartBuffering[data.type] + ")");
6059

6160

62-
mpd = self.manifestExt.getMpd(self.manifestModel.getValue());
61+
mpd = this.manifestExt.getMpd(this.manifestModel.getValue());
6362
if (mpd) {
64-
minBufferTime = self.config.getParamFor(data.type, "BufferController.minBufferTime", "number", mpd.manifest.minBufferTime);
65-
switchLowerBufferRatio = self.config.getParamFor(data.type, "ABR.switchLowerBufferRatio", "number", 0.25);
66-
switchLowerBufferTime = self.config.getParamFor(data.type, "ABR.switchLowerBufferTime", "number", switchLowerBufferRatio * minBufferTime);
67-
switchDownBufferRatio = self.config.getParamFor(data.type, "ABR.switchDownBufferRatio", "number", 0.5);
68-
switchDownBufferTime = self.config.getParamFor(data.type, "ABR.switchDownBufferTime", "number", switchDownBufferRatio * minBufferTime);
69-
switchUpBufferRatio = self.config.getParamFor(data.type, "ABR.switchUpBufferRatio", "number", 0.75);
70-
switchUpBufferTime = self.config.getParamFor(data.type, "ABR.switchUpBufferTime", "number", switchUpBufferRatio * minBufferTime);
71-
72-
if ((bufferLevel.level < switchDownBufferTime) && (self.isStartBuffering[data.type])) {
63+
minBufferTime = this.config.getParamFor(data.type, "BufferController.minBufferTime", "number", mpd.manifest.minBufferTime);
64+
switchLowerBufferRatio = this.config.getParamFor(data.type, "ABR.switchLowerBufferRatio", "number", 0.25);
65+
switchLowerBufferTime = this.config.getParamFor(data.type, "ABR.switchLowerBufferTime", "number", switchLowerBufferRatio * minBufferTime);
66+
switchDownBufferRatio = this.config.getParamFor(data.type, "ABR.switchDownBufferRatio", "number", 0.5);
67+
switchDownBufferTime = this.config.getParamFor(data.type, "ABR.switchDownBufferTime", "number", switchDownBufferRatio * minBufferTime);
68+
switchUpBufferRatio = this.config.getParamFor(data.type, "ABR.switchUpBufferRatio", "number", 0.75);
69+
switchUpBufferTime = this.config.getParamFor(data.type, "ABR.switchUpBufferTime", "number", switchUpBufferRatio * minBufferTime);
70+
71+
if ((bufferLevel.level < switchDownBufferTime) && (this.isStartBuffering[data.type])) {
7372
return new MediaPlayer.rules.SwitchRequest();
7473
} else {
7574
if (bufferLevel.level >= switchDownBufferTime) {
76-
self.isStartBuffering[data.type] = false;
75+
this.isStartBuffering[data.type] = false;
7776
}
7877

79-
var max = self.manifestExt.getRepresentationCount(data);
78+
var max = this.manifestExt.getRepresentationCount(data);
8079

8180
max -= 1; // 0 based
8281

@@ -88,12 +87,12 @@ MediaPlayer.rules.InsufficientBufferRule = function() {
8887
p = MediaPlayer.rules.SwitchRequest.prototype.DEFAULT;
8988
}
9089

91-
self.debug.info("[InsufficientBufferRule][" + data.type + "] SwitchRequest: q=" + q + ", p=" + p);
90+
this.debug.info("[InsufficientBufferRule][" + data.type + "] SwitchRequest: q=" + q + ", p=" + p);
9291
return new MediaPlayer.rules.SwitchRequest(q, p);
9392

9493
}
9594
} else {
96-
self.debug.log("[InsufficientBufferRule][" + data.type + "] Manifest not present yet");
95+
this.debug.log("[InsufficientBufferRule][" + data.type + "] Manifest not present yet");
9796
return new MediaPlayer.rules.SwitchRequest();
9897
}
9998
}

0 commit comments

Comments
 (0)