Skip to content

Commit

Permalink
Use high precision timer if available; Benchmark individual decoding …
Browse files Browse the repository at this point in the history
…times to omit scheduling overhead
  • Loading branch information
phoboslab committed Nov 4, 2014
1 parent 42cc895 commit 14806a1
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions jsmpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ jsmpeg.prototype.updateLoaderGL = function( ev ) {
};

jsmpeg.prototype.loadCallback = function(file) {
var time = Date.now();
this.buffer = new BitReader(file);

this.findStartCode(START_SEQUENCE);
Expand All @@ -327,7 +326,7 @@ jsmpeg.prototype.loadCallback = function(file) {

jsmpeg.prototype.play = function(file) {
if( this.playing ) { return; }
this.targetTime = Date.now();
this.targetTime = this.now();
this.playing = true;
this.scheduleNextFrame();
};
Expand Down Expand Up @@ -412,8 +411,20 @@ jsmpeg.prototype.lateTime = 0;
jsmpeg.prototype.firstSequenceHeader = 0;
jsmpeg.prototype.targetTime = 0;

jsmpeg.prototype.benchmark = false;
jsmpeg.prototype.benchFrame = 0;
jsmpeg.prototype.benchDecodeTimes = 0;

jsmpeg.prototype.now = function() {
return window.performance
? window.performance.now()
: Date.now();
}

jsmpeg.prototype.nextFrame = function() {
if( !this.buffer ) { return; }

var frameStart = this.now();
while(true) {
var code = this.buffer.findNextMPEGStartCode();

Expand All @@ -425,6 +436,7 @@ jsmpeg.prototype.nextFrame = function() {
this.scheduleNextFrame();
}
this.decodePicture();
this.benchDecodeTimes += this.now() - frameStart;
return this.canvas;
}
else if( code == BitReader.NOT_FOUND ) {
Expand All @@ -447,24 +459,17 @@ jsmpeg.prototype.nextFrame = function() {
};

jsmpeg.prototype.scheduleNextFrame = function() {
this.lateTime = Date.now() - this.targetTime;
this.lateTime = this.now() - this.targetTime;
var wait = Math.max(0, (1000/this.pictureRate) - this.lateTime);
this.targetTime = Date.now() + wait;
this.targetTime = this.now() + wait;

if( this.benchmark ) {
var now = Date.now();
if(!this.benchframe) {
this.benchstart = now;
this.benchframe = 0;
}
this.benchframe++;
var timepassed = now - this.benchstart;
if( this.benchframe >= 100 ) {
this.benchfps = (this.benchframe / timepassed) * 1000;
if( console ) {
console.log("frames per second: " + this.benchfps);
}
this.benchframe = null;
this.benchFrame++;
if( this.benchFrame >= 120 ) {
var frameTime = this.benchDecodeTimes / this.benchFrame;
console.log("Average time per frame:", frameTime, 'ms');
this.benchFrame = 0;
this.benchDecodeTimes = 0;
}
setTimeout( this.nextFrame.bind(this), 0);
}
Expand Down

0 comments on commit 14806a1

Please sign in to comment.