Просмотр исходного кода

benchmark mode that, when enabled, tries to decode frames as fast as possible and prints "frame per second" messages to the JavaScript console

Maik Merten 12 лет назад
Родитель
Сommit
79d4576bc5
1 измененных файлов: 16 добавлений и 1 удалений
  1. 16 1
      jsmpg.js

+ 16 - 1
jsmpg.js Просмотреть файл

@@ -26,6 +26,7 @@ var requestAnimFrame = (function(){
26 26
 		
27 27
 var jsmpeg = window.jsmpeg = function( url, opts ) {
28 28
 	opts = opts || {};
29
+	this.benchmark = !!opts.benchmark;
29 30
 	this.canvas = opts.canvas || document.createElement('canvas');
30 31
 	this.autoplay = !!opts.autoplay;
31 32
 	this.loop = !!opts.loop;
@@ -404,7 +405,21 @@ jsmpeg.prototype.scheduleNextFrame = function() {
404 405
 	var wait = Math.max(0, (1000/this.pictureRate) - this.lateTime);
405 406
 	this.targetTime = Date.now() + wait;
406 407
 
407
-	if( wait < 18 ) {
408
+	if(this.benchmark) {
409
+		var now = Date.now();
410
+		if(!this.benchframe) {
411
+			this.benchstart = now;
412
+			this.benchframe = 0;
413
+		}
414
+		this.benchframe++;
415
+		var timepassed = now - this.benchstart;
416
+		if(this.benchframe >= 100) {
417
+			if(console) console.debug("frames per second: " + (this.benchframe / timepassed) * 1000 );
418
+			this.benchframe = null;
419
+		}
420
+		setTimeout( this.nextFrame.bind(this), 0);
421
+	}
422
+	else if( wait < 18) {
408 423
 		this.scheduleAnimation();
409 424
 	}
410 425
 	else {