segment-controller.js
1.89 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
(function(window) {
window.videojs.hls.SegmentController = function() {
var self = this;
self.loadSegment = function(segmentUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
var request = new XMLHttpRequest();
self.url = segmentUrl;
self.onDataCallback = onDataCallback;
self.onErrorCallback = onErrorCallback;
self.onUpdateCallback = onUpdateCallback;
self.requestTimestamp = +new Date();
request.open('GET', segmentUrl, true);
request.responseType = 'arraybuffer';
request.onload = function(response) {
self.onSegmentLoadComplete(new Uint8Array(request.response));
};
request.send(null);
};
self.parseSegment = function(incomingData) {
self.data = {};
self.data.binaryData = incomingData;
self.data.url = self.url;
self.data.isCached = false;
self.data.requestTimestamp = self.requestTimestamp;
self.data.responseTimestamp = self.responseTimestamp;
self.data.byteLength = incomingData.byteLength;
self.data.isCached = parseInt(self.responseTimestamp - self.requestTimestamp) < 75;
self.data.throughput = self.calculateThroughput(self.data.byteLength, self.requestTimestamp ,self.responseTimestamp);
return self.data;
};
self.calculateThroughput = function(dataAmount, startTime, endTime) {
return Math.round(dataAmount / (endTime - startTime) * 1000) * 8;
}
self.onSegmentLoadComplete = function(response) {
var output;
self.responseTimestamp = +new Date();
output = self.parseSegment(response);
if (self.onDataCallback !== undefined) {
self.onDataCallback(output);
}
};
self.onSegmentLoadError = function(error) {
if (error) {
console.log(error.message);
}
if (self.onErrorCallback !== undefined) {
onErrorCallback(error);
}
};
}
})(this);