Skip to content

Commit

Permalink
Add on{Audio/Video}Decode callbacks, meassure perf
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab committed Oct 9, 2018
1 parent 47daa9d commit 7e39678
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
6 changes: 3 additions & 3 deletions jsmpeg.min.js

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions src/mp2-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ JSMpeg.Decoder.MP2AudioWASM = (function(){ "use strict";
var MP2WASM = function(options) {
JSMpeg.Decoder.Base.call(this, options);

this.onDecodeCallback = options.onAudioDecode;
this.module = options.wasmModule;

this.bufferSize = options.audioBufferSize || 128*1024;
Expand Down Expand Up @@ -62,10 +63,13 @@ MP2WASM.prototype.bufferWrite = function(buffers) {
};

MP2WASM.prototype.decode = function() {
if (!this.decoder) { return; }

var decodedBytes = this.functions._mp2_decoder_decode(this.decoder);
var startTime = JSMpeg.Now();

if (!this.decoder) {
return false;
}

var decodedBytes = this.functions._mp2_decoder_decode(this.decoder);
if (decodedBytes === 0) {
return false;
}
Expand All @@ -89,6 +93,11 @@ MP2WASM.prototype.decode = function() {
}

this.advanceDecodedTime(MP2WASM.SAMPLES_PER_FRAME / this.sampleRate);

var elapsedTime = JSMpeg.Now() - startTime;
if (this.onDecodeCallback) {
this.onDecodeCallback(this, elapsedTime);
}
return true;
};

Expand Down
10 changes: 9 additions & 1 deletion src/mp2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ JSMpeg.Decoder.MP2Audio = (function(){ "use strict";
var MP2 = function(options) {
JSMpeg.Decoder.Base.call(this, options);

this.onDecodeCallback = options.onAudioDecode;

var bufferSize = options.audioBufferSize || 128*1024;
var bufferMode = options.streaming
? JSMpeg.BitBuffer.MODE.EVICT
Expand Down Expand Up @@ -41,14 +43,15 @@ MP2.prototype = Object.create(JSMpeg.Decoder.Base.prototype);
MP2.prototype.constructor = MP2;

MP2.prototype.decode = function() {
var startTime = JSMpeg.Now();

var pos = this.bits.index >> 3;
if (pos >= this.bits.byteLength) {
return false;
}

var decoded = this.decodeFrame(this.left, this.right);
this.bits.index = (pos + decoded) << 3;

if (!decoded) {
return false;
}
Expand All @@ -58,6 +61,11 @@ MP2.prototype.decode = function() {
}

this.advanceDecodedTime(this.left.length / this.sampleRate);

var elapsedTime = JSMpeg.Now() - startTime;
if (this.onDecodeCallback) {
this.onDecodeCallback(this, elapsedTime);
}
return true;
};

Expand Down
15 changes: 12 additions & 3 deletions src/mpeg1-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ JSMpeg.Decoder.MPEG1VideoWASM = (function(){ "use strict";
var MPEG1WASM = function(options) {
JSMpeg.Decoder.Base.call(this, options);

this.onDecodeCallback = options.onVideoDecode;
this.module = options.wasmModule;

this.bufferSize = options.videoBufferSize || 512*1024;
Expand Down Expand Up @@ -84,10 +85,13 @@ MPEG1WASM.prototype.loadSequnceHeader = function() {
};

MPEG1WASM.prototype.decode = function() {
if (!this.decoder) { return; }

var didDecode = this.functions._mpeg1_decoder_decode(this.decoder);
var startTime = JSMpeg.Now();

if (!this.decoder) {
return false;
}

var didDecode = this.functions._mpeg1_decoder_decode(this.decoder);
if (!didDecode) {
return false;
}
Expand All @@ -106,6 +110,11 @@ MPEG1WASM.prototype.decode = function() {
}

this.advanceDecodedTime(1/this.frameRate);

var elapsedTime = JSMpeg.Now() - startTime;
if (this.onDecodeCallback) {
this.onDecodeCallback(this, elapsedTime);
}
return true;
};

Expand Down
9 changes: 9 additions & 0 deletions src/mpeg1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ JSMpeg.Decoder.MPEG1Video = (function(){ "use strict";
var MPEG1 = function(options) {
JSMpeg.Decoder.Base.call(this, options);

this.onDecodeCallback = options.onVideoDecode;

var bufferSize = options.videoBufferSize || 512*1024;
var bufferMode = options.streaming
? JSMpeg.BitBuffer.MODE.EVICT
Expand Down Expand Up @@ -40,6 +42,8 @@ MPEG1.prototype.write = function(pts, buffers) {
};

MPEG1.prototype.decode = function() {
var startTime = JSMpeg.Now();

if (!this.hasSequenceHeader) {
return false;
}
Expand All @@ -51,6 +55,11 @@ MPEG1.prototype.decode = function() {

this.decodePicture();
this.advanceDecodedTime(1/this.frameRate);

var elapsedTime = JSMpeg.Now() - startTime;
if (this.onDecodeCallback) {
this.onDecodeCallback(this, elapsedTime);
}
return true;
};

Expand Down

0 comments on commit 7e39678

Please sign in to comment.