-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_wish_bitmovin.html
More file actions
365 lines (300 loc) · 10.6 KB
/
Copy pathindex_wish_bitmovin.html
File metadata and controls
365 lines (300 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bitmovin SDK - Getting Started WEB SDK</title>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script type="text/javascript" src="https://cdn.bitmovin.com/player/web/8.43.0/bitmovinplayer.js"></script>
</head>
<body>
<!-- STEP 2 - Provide a player container element-->
<div id="my-player"></div>
<p> Select user preferences.</p>
<form >
<label for="preferences">Select a mode:</label>
<select name="preferences" id="preferences">
<option value="save_data">Reduce Data Usage</option>
<option value="balance" selected>Balanced</option>
<option value="high_quality">Highest Quality</option>
</select>
<br><br>
<label for="quality_function">Select a quality function:</label>
<select name="quality_function" id="quality_function">
<option value="linear"select>Linear</option>
<option value="logarithm">Logarithm</option>
</select>
<br><br>
<button type="button" onclick="getUserConfiguration()"> Submit</button>
</form>
<script>
var qualitySwitches = 0
const buffer_size = 40
var throughputHistory = [];
var currentQuality = null;
var sameRepCount = 0;
var MIN_BUFFER_RATIO = 0.2;
var alpha = 0;
var beta = 0;
var gamma = 0;
var denominator_exp = 0;
var smoothThroughputKbps = 0;
var num_downloaded_segments = 0;
var selected_quality_index_array = [];
var availableVideoQualities;
var last_selected_quality = 0;
var next_selected_quality = 0;
var bitratesKbps = [];
var SD = 4;
var currentBufferS;
var lastBufferS;
var downloadedData
var low_buff_thresS = 0;
var lastThroughputKbps = 0;
var qualityFunction = null;
var multiplier = 1;
var maxResolution = 0;
var videoThroughputKbps = 0;
var audioThroughputKbps = 0;
//<!-- STEP 3 - Configure and Initialize the player-->
var playerConfig = {
"key": "",
"playback": {
"muted": true,
"autoplay": true
},
style: {
width: '640px',
height: '480px'
},
tweaks: {
file_protocol: true,
app_id: 'ANY_STRING_ID',
max_buffer_level: buffer_size
},
adaptation: {
onVideoAdaptation: wishmmsp
},
}
var container = document.getElementById('my-player');
var player = new bitmovin.player.Player(container, playerConfig);
var sourceConfig = {
// dash: "http://127.0.0.1:81/tos_h264_4s/manifest.mpd",
dash: "https://dash.akamaized.net/akamai/bbb_30fps/bbb_30fps.mpd"
}
function getUserConfiguration() {
let preference = document.getElementById('preferences')
switch(preference.value) {
case "save_data":
m_xi = 1;
m_delta = 1;
maxResolution = 720;
break;
case "balance":
m_xi = 0.75;
m_delta = 0.75;
maxResolution = 1080;
break;
case "high_quality":
m_xi = 0.5;
m_delta = 0.5;
maxResolution = 2160;
break;
}
player.load(sourceConfig).then(function() {
console.log('Successfully loaded Source Config!');
}).catch(function(reason) {
console.log('Error while loading source:', reason);
}
);
}
///////////////////////////////////////////////////////////////////////
function wishmmsp() {
availableVideoQualities = player.getAvailableVideoQualities()
next_selected_quality = 0;
availableVideoQualities.sort(function (a, b) {
return a.bitrate - b.bitrate;
});
bitratesKbps = []
for (var i = 0; i < availableVideoQualities.length; i++) {
if (availableVideoQualities[i].height <= maxResolution)
bitratesKbps.push(availableVideoQualities[i].bitrate/1000);
}
currentBufferS = player.getVideoBufferLength();
if (!player) {
return;
}
if (player.getConfig().hasOwnProperty('tweaks') && player.getConfig().tweaks.hasOwnProperty('max_buffer_level')) {
maxBufferLevel = player.getConfig().tweaks.max_buffer_level;
} else {
maxBufferLevel = 20;
}
var init = function () {
player.on(bitmovin.player.PlayerEvent.DownloadFinished, onDownloadFinished);
player.on(bitmovin.player.PlayerEvent.SegmentPlayback, function(data) {
SD = data.duration
})
};
if (player.getSource()) {
init();
} else {
player.on(bitmovin.player.PlayerEvent.SourceLoaded, init);
}
var nextQuality = onVideoAdaptation();
return nextQuality.id;
}
function resetBuffer() {
console.log(" resetBuffer ")
throughputHistory = [];
}
function onDownloadFinished(event) {
const instantThroughputKbps = (event.size > 2000) ? (event.size * 8/1000) / event.downloadTime : lastThroughputKbps;
if (event.downloadType === 'media/video') {
videoThroughputKbps = instantThroughputKbps;
lastThroughputKbps = Math.max(videoThroughputKbps, audioThroughputKbps)
} else {
audioThroughputKbps = instantThroughputKbps;
lastThroughputKbps = Math.max(videoThroughputKbps, audioThroughputKbps)
}
}
function getSmoothThroughput(margin, lastThroughputKbps, smoothThroughputKbps) {
if (lastThroughputKbps > 0) {
if (smoothThroughputKbps === 0) {
smoothThroughputKbps = lastThroughputKbps;
}
else {
smoothThroughputKbps = (1 - margin) * smoothThroughputKbps + margin * lastThroughputKbps;
}
}
else {
smoothThroughputKbps = 5000; // finetune
}
return smoothThroughputKbps;
}
function getQualityFunction(bitrates, functionType) {
let qualityLevelList = [];
let length = bitrates.length;
if (functionType === "linear"){
for (let i = 0; i < length; i ++) {
qualityLevelList.push(1.0*bitrates[i]/bitrates[length-1]);
}
} else {
for (let i = 0; i < length; i ++) {
qualityLevelList.push(Math.log(bitrates[i]/bitrates[0]));
}
}
return qualityLevelList;
}
function setWeightsLinear(m_xi, m_delta, qualityLevelList, segment_duration) {
let num_considered_bitrate=bitratesKbps.length
let R_max_Mbps = bitratesKbps[num_considered_bitrate-1];
let R_o_Mbps = bitratesKbps[num_considered_bitrate-1];
let thrp_optimal = bitratesKbps[num_considered_bitrate-1];
let last_quality_1_Mbps = bitratesKbps[num_considered_bitrate-2];
let optimal_delta_buffer_S = buffer_size*(m_xi - MIN_BUFFER_RATIO);
let temp_beta_alpha = optimal_delta_buffer_S/segment_duration;
let temp_a = 2.0*Math.exp(1+ last_quality_1_Mbps/R_max_Mbps-2.0*R_o_Mbps/R_max_Mbps);
let temp_b = (1 + temp_beta_alpha*SD/(optimal_delta_buffer_S))/thrp_optimal;
denominator_exp = Math.exp(2*qualityLevelList[num_considered_bitrate-1]-2*qualityLevelList[0]);
alpha = 1.0/(1 + temp_beta_alpha + R_max_Mbps*temp_b*denominator_exp/temp_a);
beta = temp_beta_alpha*alpha;
gamma = 1 - alpha - beta;
}
function setWeightsLogarit(m_xi, m_delta, qualityLevelList, segment_duration,) {
let num_considered_bitrate=bitratesKbps.length
let R_i = bitratesKbps[num_considered_bitrate-1];
let R_min = bitratesKbps[0];
let Q_k = bitratesKbps[num_considered_bitrate-2];
let delta_B = m_xi*buffer_size - low_buff_thresS;
denominator_exp = Math.exp(2*qualityLevelList[num_considered_bitrate-1]-2*qualityLevelList[0]);
alpha = 1 / (1 +
delta_B/segment_duration +
Math.pow(R_i,3)/(m_delta*Math.pow(R_min,2)*Q_k));
beta = alpha*delta_B/segment_duration;
gamma = 1 - alpha - beta;
}
function getTotalCost_v3(bitrates, qualityIndex,
estimated_throghputKbps, currentbufferS) {
let totalCost;
let bandwidthCost;
let bufferCost;
let qualityCost;
let current_quality_level = qualityLevelList[qualityIndex];
let temp = bitrates[qualityIndex]*1.0/estimated_throghputKbps; // bitrate is in bps
let average_quality = 0;
let slice_window = 10;
let length_quality = qualityLevelList.length;
let num_downloaded_segments = selected_quality_index_array.length;
let quality_window = Math.min(slice_window, num_downloaded_segments);
for (let i = 1; i <= quality_window; i++){
let m_qualityIndex = getQualityIndexFromBitrate(selected_quality_index_array[num_downloaded_segments-i], length_quality);
average_quality += qualityLevelList[m_qualityIndex];
}
average_quality = average_quality/quality_window;
bandwidthCost = temp;
bufferCost = temp*(SD/(currentbufferS - low_buff_thresS));
qualityCost = Math.exp(qualityLevelList[length_quality-1] + average_quality - 2*current_quality_level)/denominator_exp;
totalCost = alpha*bandwidthCost + beta*bufferCost + gamma*qualityCost;
return totalCost;
}
function onVideoAdaptation() {
const length = bitratesKbps.length;
low_buff_thresS = SD;
if (downloadedData == null || currentBufferS < SD){
downloadedData = availableVideoQualities[0]
return downloadedData;
}
selected_quality_index_array.push(downloadedData.bitrate/1000)
num_downloaded_segments = selected_quality_index_array.length
let lowest_cost = Number.MAX_SAFE_INTEGER;
let max_quality = 0;
smoothThroughputKbps = getSmoothThroughput(0.125, lastThroughputKbps, smoothThroughputKbps);
var estimated_throghputKbps = Math.min(smoothThroughputKbps, lastThroughputKbps);
qualityLevelList = getQualityFunction(bitratesKbps, document.getElementById('quality_function').value);
for (let i = length-1; i >= 0; i--){
if (bitratesKbps[i] < lastThroughputKbps * (1+0.1)) { // MMSP
// if (bitratesKbps[i] < smoothThroughputKbps * (1+0.1)) {
max_quality = i;
break;
}
}
if (max_quality === 0) {
next_selected_quality = max_quality;
}
if (document.getElementById("quality_function").value == "linear") {
multiplier = 100;
setWeightsLinear(m_xi, m_delta, qualityLevelList, SD);
} else {
multiplier = 10000;
setWeightsLogarit(m_xi, m_delta, qualityLevelList, SD);
}
for (let i = 1; i <= max_quality; i++) {
let currentTotalCost = Math.round(multiplier*getTotalCost_v3(bitratesKbps, i, estimated_throghputKbps, currentBufferS));
if (currentTotalCost <= lowest_cost) {
next_selected_quality = i;
lowest_cost = currentTotalCost;
}
}
// check if it's suitable to decrease the quality
const threshold_ = 0.2
if (lastBufferS - currentBufferS < lastBufferS*threshold_ && next_selected_quality < last_selected_quality) {
next_selected_quality = last_selected_quality;
}
last_selected_quality = next_selected_quality;
var finalDecision = availableVideoQualities[next_selected_quality]
downloadedData = finalDecision;
lastBufferS = currentBufferS;
return finalDecision
}
function getQualityIndexFromBitrate(bitrate, bitrates_length) {
for (let i = 0; i < bitrates_length; i ++) {
if (bitrate == bitratesKbps[i]) {
return i;
}
}
return 0;
}
</script>
</body>
</html>