소스 검색

Use high precision timer if available; Benchmark individual decoding times to omit scheduling overhead

Dominic Szablewski 11 년 전
부모
커밋
14806a151b
1개의 변경된 파일22개의 추가작업 그리고 17개의 파일을 삭제
  1. 22 17
      jsmpg.js

+ 22 - 17
jsmpg.js 파일 보기

@@ -306,7 +306,6 @@ jsmpeg.prototype.updateLoaderGL = function( ev ) {
306 306
 };
307 307
 	
308 308
 jsmpeg.prototype.loadCallback = function(file) {
309
-	var time = Date.now();
310 309
 	this.buffer = new BitReader(file);
311 310
 	
312 311
 	this.findStartCode(START_SEQUENCE);
@@ -327,7 +326,7 @@ jsmpeg.prototype.loadCallback = function(file) {
327 326
 
328 327
 jsmpeg.prototype.play = function(file) {
329 328
 	if( this.playing ) { return; }
330
-	this.targetTime = Date.now();
329
+	this.targetTime = this.now();
331 330
 	this.playing = true;
332 331
 	this.scheduleNextFrame();
333 332
 };
@@ -412,8 +411,20 @@ jsmpeg.prototype.lateTime = 0;
412 411
 jsmpeg.prototype.firstSequenceHeader = 0;
413 412
 jsmpeg.prototype.targetTime = 0;
414 413
 
414
+jsmpeg.prototype.benchmark = false;
415
+jsmpeg.prototype.benchFrame = 0;
416
+jsmpeg.prototype.benchDecodeTimes = 0;
417
+
418
+jsmpeg.prototype.now = function() {
419
+	return window.performance 
420
+		? window.performance.now() 
421
+		: Date.now();
422
+}
423
+
415 424
 jsmpeg.prototype.nextFrame = function() {
416 425
 	if( !this.buffer ) { return; }
426
+
427
+	var frameStart = this.now();
417 428
 	while(true) {
418 429
 		var code = this.buffer.findNextMPEGStartCode();
419 430
 		
@@ -425,6 +436,7 @@ jsmpeg.prototype.nextFrame = function() {
425 436
 				this.scheduleNextFrame();
426 437
 			}
427 438
 			this.decodePicture();
439
+			this.benchDecodeTimes += this.now() - frameStart;
428 440
 			return this.canvas;
429 441
 		}
430 442
 		else if( code == BitReader.NOT_FOUND ) {
@@ -447,24 +459,17 @@ jsmpeg.prototype.nextFrame = function() {
447 459
 };
448 460
 
449 461
 jsmpeg.prototype.scheduleNextFrame = function() {
450
-	this.lateTime = Date.now() - this.targetTime;
462
+	this.lateTime = this.now() - this.targetTime;
451 463
 	var wait = Math.max(0, (1000/this.pictureRate) - this.lateTime);
452
-	this.targetTime = Date.now() + wait;
464
+	this.targetTime = this.now() + wait;
453 465
 
454 466
 	if( this.benchmark ) {
455
-		var now = Date.now();
456
-		if(!this.benchframe) {
457
-			this.benchstart = now;
458
-			this.benchframe = 0;
459
-		}
460
-		this.benchframe++;
461
-		var timepassed = now - this.benchstart;
462
-		if( this.benchframe >= 100 ) {
463
-			this.benchfps = (this.benchframe / timepassed) * 1000;
464
-			if( console ) {
465
-				console.log("frames per second: " + this.benchfps);
466
-			}
467
-			this.benchframe = null;
467
+		this.benchFrame++;
468
+		if( this.benchFrame >= 120 ) {
469
+			var frameTime = this.benchDecodeTimes / this.benchFrame;
470
+			console.log("Average time per frame:", frameTime, 'ms');
471
+			this.benchFrame = 0;
472
+			this.benchDecodeTimes = 0;
468 473
 		}
469 474
 		setTimeout( this.nextFrame.bind(this), 0);
470 475
 	}