Przeglądaj źródła

Cache WASM Module once instantiated; fix #301

phoboslab 5 lat temu
rodzic
commit
9cf21d34ec
6 zmienionych plików z 18 dodań i 5 usunięć
  1. 3 3
      jsmpeg.min.js
  2. 5 2
      src/player.js
  3. 7 0
      src/wasm-module.js
  4. 1 0
      src/wasm/buffer.c
  5. 1 0
      src/wasm/mp2.c
  6. 1 0
      src/wasm/mpeg1.c

Plik diff jest za duży
+ 3 - 3
jsmpeg.min.js


+ 5 - 2
src/player.js Wyświetl plik

@@ -28,7 +28,7 @@ var Player = function(url, options) {
28 28
 	this.source.connect(this.demuxer);
29 29
 
30 30
 	if (!options.disableWebAssembly && JSMpeg.WASMModule.IsSupported()) {
31
-		this.wasmModule = new JSMpeg.WASMModule();
31
+		this.wasmModule = JSMpeg.WASMModule.GetModule();
32 32
 		options.wasmModule = this.wasmModule;
33 33
 	}
34 34
 
@@ -73,7 +73,10 @@ var Player = function(url, options) {
73 73
 	// loading the source. Otherwise the decoders won't know what to do with 
74 74
 	// the source data.
75 75
 	if (this.wasmModule) {
76
-		if (JSMpeg.WASM_BINARY_INLINED) {
76
+		if (this.wasmModule.ready) {
77
+			this.startLoading();
78
+		}
79
+		else if (JSMpeg.WASM_BINARY_INLINED) {
77 80
 			var wasm = JSMpeg.Base64ToArrayBuffer(JSMpeg.WASM_BINARY_INLINED);
78 81
 			this.wasmModule.loadFromBuffer(wasm, this.startLoading.bind(this));
79 82
 		}

+ 7 - 0
src/wasm-module.js Wyświetl plik

@@ -4,6 +4,7 @@ var WASM = function() {
4 4
 	this.stackSize = 5 * 1024 * 1024; // emscripten default
5 5
 	this.pageSize = 64 * 1024; // wasm page size
6 6
 	this.onInitCallback = null;
7
+	this.ready = false;
7 8
 };
8 9
 
9 10
 WASM.prototype.write = function(buffer) {
@@ -44,6 +45,7 @@ WASM.prototype.loadFromBuffer = function(buffer, callback) {
44 45
 			this.instance.exports.__post_instantiate();
45 46
 		}
46 47
 		this.createHeapViews();
48
+		this.ready = true;
47 49
 		callback && callback(this);
48 50
 	}.bind(this))
49 51
 };
@@ -144,6 +146,11 @@ WASM.IsSupported = function() {
144 146
 	return (!!window.WebAssembly);
145 147
 };
146 148
 
149
+WASM.GetModule = function() {
150
+	WASM.CACHED_MODULE = WASM.CACHED_MODULE || new WASM();
151
+	return WASM.CACHED_MODULE;
152
+};
153
+
147 154
 return WASM;
148 155
 
149 156
 })();

+ 1 - 0
src/wasm/buffer.c Wyświetl plik

@@ -19,6 +19,7 @@ void bit_buffer_evict(bit_buffer_t *self, unsigned int bytes_needed);
19 19
 
20 20
 bit_buffer_t *bit_buffer_create(unsigned int initial_byte_capacity, bit_buffer_mode_t mode) {
21 21
 	bit_buffer_t *self = malloc(sizeof(bit_buffer_t));
22
+	memset(self, 0, sizeof(bit_buffer_t));
22 23
 	self->mode = mode;
23 24
 	self->bytes = malloc(initial_byte_capacity);
24 25
 	self->byte_capacity = initial_byte_capacity;

+ 1 - 0
src/wasm/mp2.c Wyświetl plik

@@ -233,6 +233,7 @@ int decode_frame(mp2_decoder_t *self);
233 233
 
234 234
 mp2_decoder_t *mp2_decoder_create(unsigned int buffer_size, bit_buffer_mode_t buffer_mode) {
235 235
 	mp2_decoder_t *self = malloc(sizeof(mp2_decoder_t));
236
+	memset(self, 0, sizeof(mp2_decoder_t));
236 237
 	self->bits = bit_buffer_create(buffer_size, buffer_mode);
237 238
 
238 239
 	self->sample_rate = 44100;

+ 1 - 0
src/wasm/mpeg1.c Wyświetl plik

@@ -776,6 +776,7 @@ int read_huffman(bit_buffer_t *bits, const int *code_table);
776 776
 
777 777
 mpeg1_decoder_t *mpeg1_decoder_create(unsigned int buffer_size, bit_buffer_mode_t buffer_mode) {
778 778
 	mpeg1_decoder_t *self = malloc(sizeof(mpeg1_decoder_t));
779
+	memset(self, 0, sizeof(mpeg1_decoder_t));
779 780
 	self->bits = bit_buffer_create(buffer_size, buffer_mode);
780 781
 	return self;
781 782
 }