forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-progressive.js
137 lines (109 loc) · 3.25 KB
/
ajax-progressive.js
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
JSMpeg.Source.AjaxProgressive = (function(){ "use strict";
var AjaxProgressiveSource = function(url, options) {
this.url = url;
this.destination = null;
this.request = null;
this.streaming = false;
this.completed = false;
this.established = false;
this.progress = 0;
this.fileSize = 0;
this.loadedSize = 0;
this.chunkSize = options.chunkSize || 1024*1024;
this.isLoading = false;
this.loadStartTime = 0;
this.throttled = options.throttled !== false;
this.aborted = false;
this.onEstablishedCallback = options.onSourceEstablished;
this.onCompletedCallback = options.onSourceCompleted;
};
AjaxProgressiveSource.prototype.connect = function(destination) {
this.destination = destination;
};
AjaxProgressiveSource.prototype.start = function() {
this.request = new XMLHttpRequest();
this.request.onreadystatechange = function() {
if (this.request.readyState === this.request.DONE) {
this.fileSize = parseInt(
this.request.getResponseHeader("Content-Length")
);
this.loadNextChunk();
}
}.bind(this);
this.request.onprogress = this.onProgress.bind(this);
this.request.open('HEAD', this.url);
this.request.send();
};
AjaxProgressiveSource.prototype.resume = function(secondsHeadroom) {
if (this.isLoading || !this.throttled) {
return;
}
// Guess the worst case loading time with lots of safety margin. This is
// somewhat arbitrary...
var worstCaseLoadingTime = this.loadTime * 8 + 2;
if (worstCaseLoadingTime > secondsHeadroom) {
this.loadNextChunk();
}
};
AjaxProgressiveSource.prototype.destroy = function() {
this.request.abort();
this.aborted = true;
};
AjaxProgressiveSource.prototype.loadNextChunk = function() {
var start = this.loadedSize,
end = Math.min(this.loadedSize + this.chunkSize-1, this.fileSize-1);
if (start >= this.fileSize || this.aborted) {
this.completed = true;
if (this.onCompletedCallback) {
this.onCompletedCallback(this);
}
return;
}
this.isLoading = true;
this.loadStartTime = JSMpeg.Now();
this.request = new XMLHttpRequest();
this.request.onreadystatechange = function() {
if (
this.request.readyState === this.request.DONE &&
this.request.status >= 200 && this.request.status < 300
) {
this.onChunkLoad(this.request.response);
}
else if (this.request.readyState === this.request.DONE) {
// Retry?
if (this.loadFails++ < 3) {
this.loadNextChunk();
}
}
}.bind(this);
if (start === 0) {
this.request.onprogress = this.onProgress.bind(this);
}
this.request.open('GET', this.url+'?'+start+"-"+end);
this.request.setRequestHeader("Range", "bytes="+start+"-"+end);
this.request.responseType = "arraybuffer";
this.request.send();
};
AjaxProgressiveSource.prototype.onProgress = function(ev) {
this.progress = (ev.loaded / ev.total);
};
AjaxProgressiveSource.prototype.onChunkLoad = function(data) {
var isFirstChunk = !this.established;
this.established = true;
this.progress = 1;
this.loadedSize += data.byteLength;
this.loadFails = 0;
this.isLoading = false;
if (isFirstChunk && this.onEstablishedCallback) {
this.onEstablishedCallback(this);
}
if (this.destination) {
this.destination.write(data);
}
this.loadTime = JSMpeg.Now() - this.loadStartTime;
if (!this.throttled) {
this.loadNextChunk();
}
};
return AjaxProgressiveSource;
})();