|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+JSMpeg.Source.Fetch = (function(){ "use strict";
|
|
|
2
|
+
|
|
|
3
|
+var FetchSource = function(url, options) {
|
|
|
4
|
+ this.url = url;
|
|
|
5
|
+ this.destination = null;
|
|
|
6
|
+ this.request = null;
|
|
|
7
|
+ this.streaming = true;
|
|
|
8
|
+
|
|
|
9
|
+ this.completed = false;
|
|
|
10
|
+ this.established = false;
|
|
|
11
|
+ this.progress = 0;
|
|
|
12
|
+ this.aborted = false;
|
|
|
13
|
+
|
|
|
14
|
+ this.onEstablishedCallback = options.onSourceEstablished;
|
|
|
15
|
+ this.onCompletedCallback = options.onSourceCompleted;
|
|
|
16
|
+};
|
|
|
17
|
+
|
|
|
18
|
+FetchSource.prototype.connect = function(destination) {
|
|
|
19
|
+ this.destination = destination;
|
|
|
20
|
+};
|
|
|
21
|
+
|
|
|
22
|
+FetchSource.prototype.start = function() {
|
|
|
23
|
+ var params = {
|
|
|
24
|
+ method: 'GET',
|
|
|
25
|
+ headers: new Headers(),
|
|
|
26
|
+ cache: 'default'
|
|
|
27
|
+ };
|
|
|
28
|
+
|
|
|
29
|
+ self.fetch(this.url, params).then(function(res) {
|
|
|
30
|
+ console.log(res.value);
|
|
|
31
|
+ if (res.ok && (res.status >= 200 && res.status <= 299)) {
|
|
|
32
|
+ this.progress = 1;
|
|
|
33
|
+ this.established = true;
|
|
|
34
|
+ return this.pump(res.body.getReader());
|
|
|
35
|
+ }
|
|
|
36
|
+ else {
|
|
|
37
|
+ //error
|
|
|
38
|
+ }
|
|
|
39
|
+ }.bind(this)).catch(function(err) {
|
|
|
40
|
+ throw(err);
|
|
|
41
|
+ });
|
|
|
42
|
+};
|
|
|
43
|
+
|
|
|
44
|
+FetchSource.prototype.pump = function(reader) {
|
|
|
45
|
+ return reader.read().then(function(result) {
|
|
|
46
|
+ if (result.done) {
|
|
|
47
|
+ this.completed = true;
|
|
|
48
|
+ }
|
|
|
49
|
+ else {
|
|
|
50
|
+ if (this.aborted) {
|
|
|
51
|
+ return reader.cancel();
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ if (this.destination) {
|
|
|
55
|
+ this.destination.write(result.value.buffer);
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ return this.pump(reader);
|
|
|
59
|
+ }
|
|
|
60
|
+ }.bind(this)).catch(function(err) {
|
|
|
61
|
+ throw(err);
|
|
|
62
|
+ });
|
|
|
63
|
+};
|
|
|
64
|
+
|
|
|
65
|
+FetchSource.prototype.resume = function(secondsHeadroom) {
|
|
|
66
|
+ // Nothing to do here
|
|
|
67
|
+};
|
|
|
68
|
+
|
|
|
69
|
+FetchSource.prototype.abort = function() {
|
|
|
70
|
+ this.aborted = true;
|
|
|
71
|
+};
|
|
|
72
|
+
|
|
|
73
|
+return FetchSource;
|
|
|
74
|
+
|
|
|
75
|
+})();
|