瀏覽代碼

Add on{Audio/Video}Decode callbacks, meassure perf

phoboslab 6 年之前
父節點
當前提交
7e39678f2a
共有 5 個文件被更改,包括 45 次插入10 次删除
  1. 3 3
      jsmpeg.min.js
  2. 12 3
      src/mp2-wasm.js
  3. 9 1
      src/mp2.js
  4. 12 3
      src/mpeg1-wasm.js
  5. 9 0
      src/mpeg1.js

文件差異過大導致無法顯示
+ 3 - 3
jsmpeg.min.js


+ 12 - 3
src/mp2-wasm.js 查看文件

@@ -6,6 +6,7 @@ JSMpeg.Decoder.MP2AudioWASM = (function(){ "use strict";
6 6
 var MP2WASM = function(options) {
7 7
 	JSMpeg.Decoder.Base.call(this, options);
8 8
 
9
+	this.onDecodeCallback = options.onAudioDecode;
9 10
 	this.module = options.wasmModule;
10 11
 
11 12
 	this.bufferSize = options.audioBufferSize || 128*1024;
@@ -62,10 +63,13 @@ MP2WASM.prototype.bufferWrite = function(buffers) {
62 63
 };
63 64
 
64 65
 MP2WASM.prototype.decode = function() {
65
-	if (!this.decoder) { return; }
66
-	
67
-	var decodedBytes = this.functions._mp2_decoder_decode(this.decoder);
66
+	var startTime = JSMpeg.Now();
67
+
68
+	if (!this.decoder) {
69
+		return false;
70
+	}	
68 71
 
72
+	var decodedBytes = this.functions._mp2_decoder_decode(this.decoder);
69 73
 	if (decodedBytes === 0) {
70 74
 		return false;
71 75
 	}
@@ -89,6 +93,11 @@ MP2WASM.prototype.decode = function() {
89 93
 	}
90 94
 
91 95
 	this.advanceDecodedTime(MP2WASM.SAMPLES_PER_FRAME / this.sampleRate);
96
+
97
+	var elapsedTime = JSMpeg.Now() - startTime;
98
+	if (this.onDecodeCallback) {
99
+		this.onDecodeCallback(this, elapsedTime);
100
+	}
92 101
 	return true;
93 102
 };
94 103
 

+ 9 - 1
src/mp2.js 查看文件

@@ -6,6 +6,8 @@ JSMpeg.Decoder.MP2Audio = (function(){ "use strict";
6 6
 var MP2 = function(options) {
7 7
 	JSMpeg.Decoder.Base.call(this, options);
8 8
 
9
+	this.onDecodeCallback = options.onAudioDecode;
10
+
9 11
 	var bufferSize = options.audioBufferSize || 128*1024;
10 12
 	var bufferMode = options.streaming
11 13
 		? JSMpeg.BitBuffer.MODE.EVICT
@@ -41,6 +43,8 @@ MP2.prototype = Object.create(JSMpeg.Decoder.Base.prototype);
41 43
 MP2.prototype.constructor = MP2;
42 44
 
43 45
 MP2.prototype.decode = function() {
46
+	var startTime = JSMpeg.Now();
47
+
44 48
 	var pos = this.bits.index >> 3;
45 49
 	if (pos >= this.bits.byteLength) {
46 50
 		return false;
@@ -48,7 +52,6 @@ MP2.prototype.decode = function() {
48 52
 
49 53
 	var decoded = this.decodeFrame(this.left, this.right);
50 54
 	this.bits.index = (pos + decoded) << 3;
51
-
52 55
 	if (!decoded) {
53 56
 		return false;
54 57
 	}
@@ -58,6 +61,11 @@ MP2.prototype.decode = function() {
58 61
 	}
59 62
 
60 63
 	this.advanceDecodedTime(this.left.length / this.sampleRate);
64
+
65
+	var elapsedTime = JSMpeg.Now() - startTime;
66
+	if (this.onDecodeCallback) {
67
+		this.onDecodeCallback(this, elapsedTime);
68
+	}
61 69
 	return true;
62 70
 };
63 71
 

+ 12 - 3
src/mpeg1-wasm.js 查看文件

@@ -3,6 +3,7 @@ JSMpeg.Decoder.MPEG1VideoWASM = (function(){ "use strict";
3 3
 var MPEG1WASM = function(options) {
4 4
 	JSMpeg.Decoder.Base.call(this, options);
5 5
 
6
+	this.onDecodeCallback = options.onVideoDecode;
6 7
 	this.module = options.wasmModule;
7 8
 
8 9
 	this.bufferSize = options.videoBufferSize || 512*1024;
@@ -84,10 +85,13 @@ MPEG1WASM.prototype.loadSequnceHeader = function() {
84 85
 };
85 86
 
86 87
 MPEG1WASM.prototype.decode = function() {
87
-	if (!this.decoder) { return; }
88
-	
89
-	var didDecode = this.functions._mpeg1_decoder_decode(this.decoder);
88
+	var startTime = JSMpeg.Now();
90 89
 
90
+	if (!this.decoder) {
91
+		return false;
92
+	}
93
+
94
+	var didDecode = this.functions._mpeg1_decoder_decode(this.decoder);
91 95
 	if (!didDecode) {
92 96
 		return false;
93 97
 	}
@@ -106,6 +110,11 @@ MPEG1WASM.prototype.decode = function() {
106 110
 	}
107 111
 
108 112
 	this.advanceDecodedTime(1/this.frameRate);
113
+
114
+	var elapsedTime = JSMpeg.Now() - startTime;
115
+	if (this.onDecodeCallback) {
116
+		this.onDecodeCallback(this, elapsedTime);
117
+	}
109 118
 	return true;
110 119
 };
111 120
 

+ 9 - 0
src/mpeg1.js 查看文件

@@ -6,6 +6,8 @@ JSMpeg.Decoder.MPEG1Video = (function(){ "use strict";
6 6
 var MPEG1 = function(options) {
7 7
 	JSMpeg.Decoder.Base.call(this, options);
8 8
 
9
+	this.onDecodeCallback = options.onVideoDecode;
10
+
9 11
 	var bufferSize = options.videoBufferSize || 512*1024;
10 12
 	var bufferMode = options.streaming
11 13
 		? JSMpeg.BitBuffer.MODE.EVICT
@@ -40,6 +42,8 @@ MPEG1.prototype.write = function(pts, buffers) {
40 42
 };
41 43
 
42 44
 MPEG1.prototype.decode = function() {
45
+	var startTime = JSMpeg.Now();
46
+	
43 47
 	if (!this.hasSequenceHeader) {
44 48
 		return false;
45 49
 	}
@@ -51,6 +55,11 @@ MPEG1.prototype.decode = function() {
51 55
 
52 56
 	this.decodePicture();
53 57
 	this.advanceDecodedTime(1/this.frameRate);
58
+
59
+	var elapsedTime = JSMpeg.Now() - startTime;
60
+	if (this.onDecodeCallback) {
61
+		this.onDecodeCallback(this, elapsedTime);
62
+	}
54 63
 	return true;
55 64
 };
56 65