Skip to content

Commit

Permalink
Add player.destroy(); see phoboslab#130, phoboslab#128
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab committed Feb 18, 2017
1 parent be5c0dd commit 4c74e65
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/ajax-progressive.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var AjaxProgressiveSource = function(url, options) {
this.isLoading = false;
this.loadStartTime = 0;
this.throttled = options.throttled !== false;
this.aborted = false;
};

AjaxProgressiveSource.prototype.connect = function(destination) {
Expand Down Expand Up @@ -52,15 +53,16 @@ AjaxProgressiveSource.prototype.resume = function(secondsHeadroom) {
}
};

AjaxProgressiveSource.prototype.abort = function() {
AjaxProgressiveSource.prototype.destroy = function() {
this.request.abort();
this.aborted = true;
};

AjaxProgressiveSource.prototype.loadNextChunk = function() {
var start = this.loadedSize,
end = Math.min(this.loadedSize + this.chunkSize-1, this.fileSize-1);

if (start >= this.fileSize) {
if (start >= this.fileSize || this.aborted) {
this.completed = true;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ AjaxSource.prototype.resume = function(secondsHeadroom) {
// Nothing to do here
};

AjaxSource.prototype.abort = function() {
AjaxSource.prototype.destroy = function() {
this.request.abort();
};

Expand Down
4 changes: 4 additions & 0 deletions src/canvas2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ var CanvasRenderer = function(options) {
this.context = this.canvas.getContext('2d');
};

CanvasRenderer.prototype.destroy = function() {
// Nothing to do here
};

CanvasRenderer.prototype.resize = function(width, height) {
this.width = width|0;
this.height = height|0;
Expand Down
7 changes: 7 additions & 0 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ Player.prototype.stop = function(ev) {
}
};

Player.prototype.destroy = function() {
this.pause();
this.source.destroy();
this.renderer.destroy();
this.audioOut.destroy();
};

Player.prototype.seek = function(time) {
var startOffset = this.audio && this.audio.canPlay
? this.audio.startTime
Expand Down
16 changes: 15 additions & 1 deletion src/webaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ var WebAudioOut = function(options) {
new (window.AudioContext || window.webkitAudioContext)();

this.gain = this.context.createGain();
this.gain.connect(this.context.destination);
this.destination = this.gain;

// Keep track of the number of connections to this AudioContext, so we
// can safely close() it when we're the only one connected to it.
this.gain.connect(this.context.destination);
this.context._connections = (this.context._connections || 0) + 1;

this.startTime = 0;
this.buffer = null;
this.wallclockStartTime = 0;
Expand All @@ -20,6 +24,16 @@ var WebAudioOut = function(options) {
Object.defineProperty(this, 'enqueuedTime', {get: this.getEnqueuedTime});
};

WebAudioOut.prototype.destroy = function() {
this.gain.disconnect();
this.context._connections--;

if (this.context._connections === 0) {
this.context.close();
WebAudioOut.CachedContext = null;
}
};

WebAudioOut.prototype.play = function(sampleRate, left, right) {
if (!this.enabled) {
return;
Expand Down
25 changes: 22 additions & 3 deletions src/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var WebGLRenderer = function(options) {
var vertexAttr = null;

// Init buffers
var vertexBuffer = gl.createBuffer();
this.vertexBuffer = gl.createBuffer();
var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertexCoords, gl.STATIC_DRAW);

// Setup the main YCrCbToRGBA shader
Expand Down Expand Up @@ -57,6 +57,19 @@ var WebGLRenderer = function(options) {
this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
};

WebGLRenderer.prototype.destroy = function() {
var gl = this.gl;

gl.deleteTexture(this.textureY);
gl.deleteTexture(this.textureCb);
gl.deleteTexture(this.textureCr);

gl.deleteProgram(this.program);
gl.deleteProgram(this.loadingProgram);

gl.deleteBuffer(this.vertexBuffer);
};

WebGLRenderer.prototype.resize = function(width, height) {
this.width = width|0;
this.height = height|0;
Expand Down Expand Up @@ -121,8 +134,12 @@ WebGLRenderer.prototype.allowsClampedTextureData = function() {

WebGLRenderer.prototype.renderProgress = function(progress) {
var gl = this.gl;

gl.useProgram(this.loadingProgram);

var loc = gl.getUniformLocation(this.loadingProgram, 'progress');
gl.uniform1f(loc, progress);

gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};

Expand All @@ -144,7 +161,9 @@ WebGLRenderer.prototype.render = function(y, cb, cr) {
y = new Uint8Array(y.buffer),
cb = new Uint8Array(cb.buffer),
cr = new Uint8Array(cr.buffer);
}
}

gl.useProgram(this.program);

this.updateTexture(gl.TEXTURE0, this.textureY, w, h, y);
this.updateTexture(gl.TEXTURE1, this.textureCb, w2, h2, cb);
Expand Down
3 changes: 2 additions & 1 deletion src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ WSSource.prototype.connect = function(destination) {
this.destination = destination;
};

WSSource.prototype.abort = function() {
WSSource.prototype.destroy = function() {
clearTimeout(this.reconnectTimeoutId);
this.shouldAttemptReconnect = false;
this.socket.close();
};
Expand Down

0 comments on commit 4c74e65

Please sign in to comment.