Pārlūkot izejas kodu

Add player.destroy(); see #130, #128

Dominic Szablewski 9 gadus atpakaļ
vecāks
revīzija
4c74e65dee
7 mainītis faili ar 55 papildinājumiem un 8 dzēšanām
  1. 4 2
      src/ajax-progressive.js
  2. 1 1
      src/ajax.js
  3. 4 0
      src/canvas2d.js
  4. 7 0
      src/player.js
  5. 15 1
      src/webaudio.js
  6. 22 3
      src/webgl.js
  7. 2 1
      src/websocket.js

+ 4 - 2
src/ajax-progressive.js Parādīt failu

16
 	this.isLoading = false;
16
 	this.isLoading = false;
17
 	this.loadStartTime = 0;
17
 	this.loadStartTime = 0;
18
 	this.throttled = options.throttled !== false;
18
 	this.throttled = options.throttled !== false;
19
+	this.aborted = false;
19
 };
20
 };
20
 
21
 
21
 AjaxProgressiveSource.prototype.connect = function(destination) {
22
 AjaxProgressiveSource.prototype.connect = function(destination) {
52
 	}
53
 	}
53
 };
54
 };
54
 
55
 
55
-AjaxProgressiveSource.prototype.abort = function() {
56
+AjaxProgressiveSource.prototype.destroy = function() {
56
 	this.request.abort();
57
 	this.request.abort();
58
+	this.aborted = true;
57
 };
59
 };
58
 
60
 
59
 AjaxProgressiveSource.prototype.loadNextChunk = function() {
61
 AjaxProgressiveSource.prototype.loadNextChunk = function() {
60
 	var start = this.loadedSize,
62
 	var start = this.loadedSize,
61
 		end = Math.min(this.loadedSize + this.chunkSize-1, this.fileSize-1);
63
 		end = Math.min(this.loadedSize + this.chunkSize-1, this.fileSize-1);
62
 	
64
 	
63
-	if (start >= this.fileSize) {
65
+	if (start >= this.fileSize || this.aborted) {
64
 		this.completed = true;
66
 		this.completed = true;
65
 		return;
67
 		return;
66
 	}
68
 	}

+ 1 - 1
src/ajax.js Parādīt failu

36
 	// Nothing to do here
36
 	// Nothing to do here
37
 };
37
 };
38
 
38
 
39
-AjaxSource.prototype.abort = function() {
39
+AjaxSource.prototype.destroy = function() {
40
 	this.request.abort();
40
 	this.request.abort();
41
 };
41
 };
42
 
42
 

+ 4 - 0
src/canvas2d.js Parādīt failu

9
 	this.context = this.canvas.getContext('2d');
9
 	this.context = this.canvas.getContext('2d');
10
 };
10
 };
11
 
11
 
12
+CanvasRenderer.prototype.destroy = function() {
13
+	// Nothing to do here
14
+};
15
+
12
 CanvasRenderer.prototype.resize = function(width, height) {
16
 CanvasRenderer.prototype.resize = function(width, height) {
13
 	this.width = width|0;
17
 	this.width = width|0;
14
 	this.height = height|0;
18
 	this.height = height|0;

+ 7 - 0
src/player.js Parādīt failu

110
 	}
110
 	}
111
 };
111
 };
112
 
112
 
113
+Player.prototype.destroy = function() {
114
+	this.pause();
115
+	this.source.destroy();
116
+	this.renderer.destroy();
117
+	this.audioOut.destroy();
118
+};
119
+
113
 Player.prototype.seek = function(time) {
120
 Player.prototype.seek = function(time) {
114
 	var startOffset = this.audio && this.audio.canPlay
121
 	var startOffset = this.audio && this.audio.canPlay
115
 		? this.audio.startTime
122
 		? this.audio.startTime

+ 15 - 1
src/webaudio.js Parādīt failu

6
 		new (window.AudioContext || window.webkitAudioContext)();
6
 		new (window.AudioContext || window.webkitAudioContext)();
7
 
7
 
8
 	this.gain = this.context.createGain();
8
 	this.gain = this.context.createGain();
9
-	this.gain.connect(this.context.destination);
10
 	this.destination = this.gain;
9
 	this.destination = this.gain;
11
 
10
 
11
+	// Keep track of the number of connections to this AudioContext, so we
12
+	// can safely close() it when we're the only one connected to it.
13
+	this.gain.connect(this.context.destination);
14
+	this.context._connections = (this.context._connections || 0) + 1;
15
+	
12
 	this.startTime = 0;
16
 	this.startTime = 0;
13
 	this.buffer = null;
17
 	this.buffer = null;
14
 	this.wallclockStartTime = 0;
18
 	this.wallclockStartTime = 0;
20
 	Object.defineProperty(this, 'enqueuedTime', {get: this.getEnqueuedTime});
24
 	Object.defineProperty(this, 'enqueuedTime', {get: this.getEnqueuedTime});
21
 };
25
 };
22
 
26
 
27
+WebAudioOut.prototype.destroy = function() {
28
+	this.gain.disconnect();
29
+	this.context._connections--;
30
+
31
+	if (this.context._connections === 0) {
32
+		this.context.close();
33
+		WebAudioOut.CachedContext = null;
34
+	}
35
+};
36
+
23
 WebAudioOut.prototype.play = function(sampleRate, left, right) {
37
 WebAudioOut.prototype.play = function(sampleRate, left, right) {
24
 	if (!this.enabled) {
38
 	if (!this.enabled) {
25
 		return;
39
 		return;

+ 22 - 3
src/webgl.js Parādīt failu

26
 	var vertexAttr = null;
26
 	var vertexAttr = null;
27
 
27
 
28
 	// Init buffers
28
 	// Init buffers
29
-	var vertexBuffer = gl.createBuffer();
29
+	this.vertexBuffer = gl.createBuffer();
30
 	var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
30
 	var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
31
-	gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
31
+	gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
32
 	gl.bufferData(gl.ARRAY_BUFFER, vertexCoords, gl.STATIC_DRAW);
32
 	gl.bufferData(gl.ARRAY_BUFFER, vertexCoords, gl.STATIC_DRAW);
33
 
33
 
34
 	// Setup the main YCrCbToRGBA shader
34
 	// Setup the main YCrCbToRGBA shader
57
 	this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
57
 	this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
58
 };
58
 };
59
 
59
 
60
+WebGLRenderer.prototype.destroy = function() {
61
+	var gl = this.gl;
62
+	
63
+	gl.deleteTexture(this.textureY);
64
+	gl.deleteTexture(this.textureCb);
65
+	gl.deleteTexture(this.textureCr);
66
+
67
+	gl.deleteProgram(this.program);
68
+	gl.deleteProgram(this.loadingProgram);
69
+
70
+	gl.deleteBuffer(this.vertexBuffer);
71
+};
72
+
60
 WebGLRenderer.prototype.resize = function(width, height) {
73
 WebGLRenderer.prototype.resize = function(width, height) {
61
 	this.width = width|0;
74
 	this.width = width|0;
62
 	this.height = height|0;
75
 	this.height = height|0;
121
 
134
 
122
 WebGLRenderer.prototype.renderProgress = function(progress) {
135
 WebGLRenderer.prototype.renderProgress = function(progress) {
123
 	var gl = this.gl;
136
 	var gl = this.gl;
137
+
138
+	gl.useProgram(this.loadingProgram);
139
+
124
 	var loc = gl.getUniformLocation(this.loadingProgram, 'progress');
140
 	var loc = gl.getUniformLocation(this.loadingProgram, 'progress');
125
 	gl.uniform1f(loc, progress);
141
 	gl.uniform1f(loc, progress);
142
+	
126
 	gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
143
 	gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
127
 };
144
 };
128
 
145
 
144
 		y = new Uint8Array(y.buffer),
161
 		y = new Uint8Array(y.buffer),
145
 		cb = new Uint8Array(cb.buffer),
162
 		cb = new Uint8Array(cb.buffer),
146
 		cr = new Uint8Array(cr.buffer);	
163
 		cr = new Uint8Array(cr.buffer);	
147
-	}	
164
+	}
165
+
166
+	gl.useProgram(this.program);
148
 
167
 
149
 	this.updateTexture(gl.TEXTURE0, this.textureY, w, h, y);
168
 	this.updateTexture(gl.TEXTURE0, this.textureY, w, h, y);
150
 	this.updateTexture(gl.TEXTURE1, this.textureCb, w2, h2, cb);
169
 	this.updateTexture(gl.TEXTURE1, this.textureCb, w2, h2, cb);

+ 2 - 1
src/websocket.js Parādīt failu

23
 	this.destination = destination;
23
 	this.destination = destination;
24
 };
24
 };
25
 
25
 
26
-WSSource.prototype.abort = function() {
26
+WSSource.prototype.destroy = function() {
27
+	clearTimeout(this.reconnectTimeoutId);
27
 	this.shouldAttemptReconnect = false;
28
 	this.shouldAttemptReconnect = false;
28
 	this.socket.close();
29
 	this.socket.close();
29
 };
30
 };