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

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

Dominic Szablewski 11 лет назад
Родитель
Сommit
14806a151b
1 измененных файлов: 22 добавлений и 17 удалений
  1. 22 17
      jsmpg.js

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

306
 };
306
 };
307
 	
307
 	
308
 jsmpeg.prototype.loadCallback = function(file) {
308
 jsmpeg.prototype.loadCallback = function(file) {
309
-	var time = Date.now();
310
 	this.buffer = new BitReader(file);
309
 	this.buffer = new BitReader(file);
311
 	
310
 	
312
 	this.findStartCode(START_SEQUENCE);
311
 	this.findStartCode(START_SEQUENCE);
327
 
326
 
328
 jsmpeg.prototype.play = function(file) {
327
 jsmpeg.prototype.play = function(file) {
329
 	if( this.playing ) { return; }
328
 	if( this.playing ) { return; }
330
-	this.targetTime = Date.now();
329
+	this.targetTime = this.now();
331
 	this.playing = true;
330
 	this.playing = true;
332
 	this.scheduleNextFrame();
331
 	this.scheduleNextFrame();
333
 };
332
 };
412
 jsmpeg.prototype.firstSequenceHeader = 0;
411
 jsmpeg.prototype.firstSequenceHeader = 0;
413
 jsmpeg.prototype.targetTime = 0;
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
 jsmpeg.prototype.nextFrame = function() {
424
 jsmpeg.prototype.nextFrame = function() {
416
 	if( !this.buffer ) { return; }
425
 	if( !this.buffer ) { return; }
426
+
427
+	var frameStart = this.now();
417
 	while(true) {
428
 	while(true) {
418
 		var code = this.buffer.findNextMPEGStartCode();
429
 		var code = this.buffer.findNextMPEGStartCode();
419
 		
430
 		
425
 				this.scheduleNextFrame();
436
 				this.scheduleNextFrame();
426
 			}
437
 			}
427
 			this.decodePicture();
438
 			this.decodePicture();
439
+			this.benchDecodeTimes += this.now() - frameStart;
428
 			return this.canvas;
440
 			return this.canvas;
429
 		}
441
 		}
430
 		else if( code == BitReader.NOT_FOUND ) {
442
 		else if( code == BitReader.NOT_FOUND ) {
447
 };
459
 };
448
 
460
 
449
 jsmpeg.prototype.scheduleNextFrame = function() {
461
 jsmpeg.prototype.scheduleNextFrame = function() {
450
-	this.lateTime = Date.now() - this.targetTime;
462
+	this.lateTime = this.now() - this.targetTime;
451
 	var wait = Math.max(0, (1000/this.pictureRate) - this.lateTime);
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
 	if( this.benchmark ) {
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
 		setTimeout( this.nextFrame.bind(this), 0);
474
 		setTimeout( this.nextFrame.bind(this), 0);
470
 	}
475
 	}