Explorar el Código

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

Dominic Szablewski hace 9 años
padre
commit
4c74e65dee
Se han modificado 7 ficheros con 55 adiciones y 8 borrados
  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 Ver fichero

@@ -16,6 +16,7 @@ var AjaxProgressiveSource = function(url, options) {
16 16
 	this.isLoading = false;
17 17
 	this.loadStartTime = 0;
18 18
 	this.throttled = options.throttled !== false;
19
+	this.aborted = false;
19 20
 };
20 21
 
21 22
 AjaxProgressiveSource.prototype.connect = function(destination) {
@@ -52,15 +53,16 @@ AjaxProgressiveSource.prototype.resume = function(secondsHeadroom) {
52 53
 	}
53 54
 };
54 55
 
55
-AjaxProgressiveSource.prototype.abort = function() {
56
+AjaxProgressiveSource.prototype.destroy = function() {
56 57
 	this.request.abort();
58
+	this.aborted = true;
57 59
 };
58 60
 
59 61
 AjaxProgressiveSource.prototype.loadNextChunk = function() {
60 62
 	var start = this.loadedSize,
61 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 66
 		this.completed = true;
65 67
 		return;
66 68
 	}

+ 1 - 1
src/ajax.js Ver fichero

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

+ 4 - 0
src/canvas2d.js Ver fichero

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

+ 7 - 0
src/player.js Ver fichero

@@ -110,6 +110,13 @@ Player.prototype.stop = function(ev) {
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 120
 Player.prototype.seek = function(time) {
114 121
 	var startOffset = this.audio && this.audio.canPlay
115 122
 		? this.audio.startTime

+ 15 - 1
src/webaudio.js Ver fichero

@@ -6,9 +6,13 @@ var WebAudioOut = function(options) {
6 6
 		new (window.AudioContext || window.webkitAudioContext)();
7 7
 
8 8
 	this.gain = this.context.createGain();
9
-	this.gain.connect(this.context.destination);
10 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 16
 	this.startTime = 0;
13 17
 	this.buffer = null;
14 18
 	this.wallclockStartTime = 0;
@@ -20,6 +24,16 @@ var WebAudioOut = function(options) {
20 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 37
 WebAudioOut.prototype.play = function(sampleRate, left, right) {
24 38
 	if (!this.enabled) {
25 39
 		return;

+ 22 - 3
src/webgl.js Ver fichero

@@ -26,9 +26,9 @@ var WebGLRenderer = function(options) {
26 26
 	var vertexAttr = null;
27 27
 
28 28
 	// Init buffers
29
-	var vertexBuffer = gl.createBuffer();
29
+	this.vertexBuffer = gl.createBuffer();
30 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 32
 	gl.bufferData(gl.ARRAY_BUFFER, vertexCoords, gl.STATIC_DRAW);
33 33
 
34 34
 	// Setup the main YCrCbToRGBA shader
@@ -57,6 +57,19 @@ var WebGLRenderer = function(options) {
57 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 73
 WebGLRenderer.prototype.resize = function(width, height) {
61 74
 	this.width = width|0;
62 75
 	this.height = height|0;
@@ -121,8 +134,12 @@ WebGLRenderer.prototype.allowsClampedTextureData = function() {
121 134
 
122 135
 WebGLRenderer.prototype.renderProgress = function(progress) {
123 136
 	var gl = this.gl;
137
+
138
+	gl.useProgram(this.loadingProgram);
139
+
124 140
 	var loc = gl.getUniformLocation(this.loadingProgram, 'progress');
125 141
 	gl.uniform1f(loc, progress);
142
+	
126 143
 	gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
127 144
 };
128 145
 
@@ -144,7 +161,9 @@ WebGLRenderer.prototype.render = function(y, cb, cr) {
144 161
 		y = new Uint8Array(y.buffer),
145 162
 		cb = new Uint8Array(cb.buffer),
146 163
 		cr = new Uint8Array(cr.buffer);	
147
-	}	
164
+	}
165
+
166
+	gl.useProgram(this.program);
148 167
 
149 168
 	this.updateTexture(gl.TEXTURE0, this.textureY, w, h, y);
150 169
 	this.updateTexture(gl.TEXTURE1, this.textureCb, w2, h2, cb);

+ 2 - 1
src/websocket.js Ver fichero

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