Преглед на файлове

Add fetch() streaming source, close #119, #300

phoboslab преди 6 години
родител
ревизия
3672355038
променени са 6 файла, в които са добавени 83 реда и са изтрити 5 реда
  1. 1 0
      build.sh
  2. 3 4
      jsmpeg.min.js
  3. 1 0
      src/ajax-progressive.js
  4. 1 0
      src/ajax.js
  5. 75 0
      src/fetch.js
  6. 2 1
      src/websocket.js

+ 1 - 0
build.sh Целия файл

@@ -63,6 +63,7 @@ cat \
63 63
 	src/player.js \
64 64
 	src/buffer.js \
65 65
 	src/ajax.js \
66
+	src/fetch.js \
66 67
 	src/ajax-progressive.js \
67 68
 	src/websocket.js \
68 69
 	src/ts.js \

Файловите разлики са ограничени, защото са твърде много
+ 3 - 4
jsmpeg.min.js


+ 1 - 0
src/ajax-progressive.js Целия файл

@@ -4,6 +4,7 @@ var AjaxProgressiveSource = function(url, options) {
4 4
 	this.url = url;
5 5
 	this.destination = null;
6 6
 	this.request = null;
7
+	this.streaming = false;
7 8
 
8 9
 	this.completed = false;
9 10
 	this.established = false;

+ 1 - 0
src/ajax.js Целия файл

@@ -4,6 +4,7 @@ var AjaxSource = function(url, options) {
4 4
 	this.url = url;
5 5
 	this.destination = null;
6 6
 	this.request = null;
7
+	this.streaming = false;
7 8
 
8 9
 	this.completed = false;
9 10
 	this.established = false;

+ 75 - 0
src/fetch.js Целия файл

@@ -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
+})();

+ 2 - 1
src/websocket.js Целия файл

@@ -3,7 +3,8 @@ JSMpeg.Source.WebSocket = (function(){ "use strict";
3 3
 var WSSource = function(url, options) {
4 4
 	this.url = url;
5 5
 	this.options = options;
6
-	this.socket = null;	
6
+	this.socket = null;
7
+	this.streaming = true;
7 8
 
8 9
 	this.callbacks = {connect: [], data: []};
9 10
 	this.destination = null;