Skip to content

Commit

Permalink
Merge pull request #48 from cycjimmy/update-from-origin-jsmpeg
Browse files Browse the repository at this point in the history
Update from origin jsmpeg
  • Loading branch information
cycjimmy authored Mar 15, 2022
2 parents d5a6e65 + 490801a commit 180b0b0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/lib/mpeg1-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export default class MPEG1WASM extends BaseDecoder {
!this.hasSequenceHeader &&
this.functions._mpeg1_decoder_has_sequence_header(this.decoder)
) {
this.loadSequnceHeader();
this.loadSequenceHeader();
}
}

loadSequnceHeader() {
loadSequenceHeader() {
this.hasSequenceHeader = true;
this.frameRate = this.functions._mpeg1_decoder_get_frame_rate(this.decoder);
this.codedSize = this.functions._mpeg1_decoder_get_coded_size(this.decoder);
Expand Down
38 changes: 34 additions & 4 deletions src/lib/wasm-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,53 @@ export default class WASM {
constructor() {
this.stackSize = 5 * 1024 * 1024; // emscripten default
this.pageSize = 64 * 1024; // wasm page size
this.onInitCallback = null;
this.onInitCallbacks = [];
this.ready = false;
this.loadingFromFileStarted = false;
this.loadingFromBufferStarted = false;
}

write(buffer) {
this.loadFromBuffer(buffer, this.onInitCallback);
this.loadFromBuffer(buffer);
}

loadFromFile(url, callback) {
if (callback) {
this.onInitCallbacks.push(callback);
}

// Make sure this WASM Module is only instantiated once. If loadFromFile()
// was already called, bail out here. On instantiation all pending
// onInitCallbacks will be called.
if (this.loadingFromFileStarted) {
return;
}
this.loadingFromFileStarted = true;

this.onInitCallback = callback;
const ajax = new AjaxSource(url, {});
ajax.connect(this);
ajax.start();
}

loadFromBuffer(buffer, callback) {
if (callback) {
this.onInitCallbacks.push(callback);
}

// Make sure this WASM Module is only instantiated once. If loadFromBuffer()
// was already called, bail out here. On instantiation all pending
// onInitCallbacks will be called.
if (this.loadingFromBufferStarted) {
return;
}
this.loadingFromBufferStarted = true;

this.moduleInfo = this.readDylinkSection(buffer);
if (!this.moduleInfo) {
this.callback && this.callback(null);
for (let i = 0; i < this.onInitCallbacks.length; i++) {
this.onInitCallbacks[i](null);
}
return;
}

Expand All @@ -47,7 +75,9 @@ export default class WASM {
}
this.createHeapViews();
this.ready = true;
callback && callback(this);
for (let i = 0; i < this.onInitCallbacks.length; i++) {
this.onInitCallbacks[i](this);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/wasm/WASM_BINARY.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions src/lib/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class WebGLRenderer {
this.width = this.canvas.width;
this.height = this.canvas.height;
this.enabled = true;
this.contextLost = false;

this.hasTextureData = {};

Expand All @@ -24,6 +25,19 @@ class WebGLRenderer {
throw new Error('Failed to get WebGL Context');
}

this.canvas.addEventListener('webglcontextlost', this.handleContextLost.bind(this), false);
this.canvas.addEventListener(
'webglcontextrestored',
this.handleContextRestored.bind(this),
false
);

this.initGL();
}

initGL() {
this.hasTextureData = {};

const { gl } = this;
let vertexAttr = null;

Expand Down Expand Up @@ -60,7 +74,22 @@ class WebGLRenderer {
this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
}

handleContextLost(ev) {
ev.preventDefault();
this.contextLost = true;
}

handleContextRestored() {
this.initGL();
this.contextLost = false;
}

destroy() {
if (this.contextLost) {
// Nothing to do here
return;
}

const { gl } = this;

this.deleteTexture(gl.TEXTURE0, this.textureY);
Expand All @@ -76,6 +105,7 @@ class WebGLRenderer {

gl.getExtension('WEBGL_lose_context').loseContext();
this.canvas.remove();
this.contextLost = true;
}

resize(width, height) {
Expand Down

0 comments on commit 180b0b0

Please sign in to comment.