forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
74 lines (61 loc) · 1.44 KB
/
fetch.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
JSMpeg.Source.Fetch = (function(){ "use strict";
var FetchSource = function(url, options) {
this.url = url;
this.destination = null;
this.request = null;
this.streaming = true;
this.completed = false;
this.established = false;
this.progress = 0;
this.aborted = false;
this.onEstablishedCallback = options.onSourceEstablished;
this.onCompletedCallback = options.onSourceCompleted;
};
FetchSource.prototype.connect = function(destination) {
this.destination = destination;
};
FetchSource.prototype.start = function() {
var params = {
method: 'GET',
headers: new Headers(),
cache: 'default'
};
self.fetch(this.url, params).then(function(res) {
if (res.ok && (res.status >= 200 && res.status <= 299)) {
this.progress = 1;
this.established = true;
return this.pump(res.body.getReader());
}
else {
//error
}
}.bind(this)).catch(function(err) {
throw(err);
});
};
FetchSource.prototype.pump = function(reader) {
return reader.read().then(function(result) {
if (result.done) {
this.completed = true;
}
else {
if (this.aborted) {
return reader.cancel();
}
if (this.destination) {
this.destination.write(result.value.buffer);
}
return this.pump(reader);
}
}.bind(this)).catch(function(err) {
throw(err);
});
};
FetchSource.prototype.resume = function(secondsHeadroom) {
// Nothing to do here
};
FetchSource.prototype.abort = function() {
this.aborted = true;
};
return FetchSource;
})();