Skip to content

Commit

Permalink
Cache WASM Module once instantiated; fix #301
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab committed Dec 1, 2019
1 parent 335b86f commit 9cf21d3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions jsmpeg.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Player = function(url, options) {
this.source.connect(this.demuxer);

if (!options.disableWebAssembly && JSMpeg.WASMModule.IsSupported()) {
this.wasmModule = new JSMpeg.WASMModule();
this.wasmModule = JSMpeg.WASMModule.GetModule();
options.wasmModule = this.wasmModule;
}

Expand Down Expand Up @@ -73,7 +73,10 @@ var Player = function(url, options) {
// loading the source. Otherwise the decoders won't know what to do with
// the source data.
if (this.wasmModule) {
if (JSMpeg.WASM_BINARY_INLINED) {
if (this.wasmModule.ready) {
this.startLoading();
}
else if (JSMpeg.WASM_BINARY_INLINED) {
var wasm = JSMpeg.Base64ToArrayBuffer(JSMpeg.WASM_BINARY_INLINED);
this.wasmModule.loadFromBuffer(wasm, this.startLoading.bind(this));
}
Expand Down
7 changes: 7 additions & 0 deletions src/wasm-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var WASM = function() {
this.stackSize = 5 * 1024 * 1024; // emscripten default
this.pageSize = 64 * 1024; // wasm page size
this.onInitCallback = null;
this.ready = false;
};

WASM.prototype.write = function(buffer) {
Expand Down Expand Up @@ -44,6 +45,7 @@ WASM.prototype.loadFromBuffer = function(buffer, callback) {
this.instance.exports.__post_instantiate();
}
this.createHeapViews();
this.ready = true;
callback && callback(this);
}.bind(this))
};
Expand Down Expand Up @@ -144,6 +146,11 @@ WASM.IsSupported = function() {
return (!!window.WebAssembly);
};

WASM.GetModule = function() {
WASM.CACHED_MODULE = WASM.CACHED_MODULE || new WASM();
return WASM.CACHED_MODULE;
};

return WASM;

})();
Expand Down
1 change: 1 addition & 0 deletions src/wasm/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void bit_buffer_evict(bit_buffer_t *self, unsigned int bytes_needed);

bit_buffer_t *bit_buffer_create(unsigned int initial_byte_capacity, bit_buffer_mode_t mode) {
bit_buffer_t *self = malloc(sizeof(bit_buffer_t));
memset(self, 0, sizeof(bit_buffer_t));
self->mode = mode;
self->bytes = malloc(initial_byte_capacity);
self->byte_capacity = initial_byte_capacity;
Expand Down
1 change: 1 addition & 0 deletions src/wasm/mp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ int decode_frame(mp2_decoder_t *self);

mp2_decoder_t *mp2_decoder_create(unsigned int buffer_size, bit_buffer_mode_t buffer_mode) {
mp2_decoder_t *self = malloc(sizeof(mp2_decoder_t));
memset(self, 0, sizeof(mp2_decoder_t));
self->bits = bit_buffer_create(buffer_size, buffer_mode);

self->sample_rate = 44100;
Expand Down
1 change: 1 addition & 0 deletions src/wasm/mpeg1.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ int read_huffman(bit_buffer_t *bits, const int *code_table);

mpeg1_decoder_t *mpeg1_decoder_create(unsigned int buffer_size, bit_buffer_mode_t buffer_mode) {
mpeg1_decoder_t *self = malloc(sizeof(mpeg1_decoder_t));
memset(self, 0, sizeof(mpeg1_decoder_t));
self->bits = bit_buffer_create(buffer_size, buffer_mode);
return self;
}
Expand Down

0 comments on commit 9cf21d3

Please sign in to comment.