-
Notifications
You must be signed in to change notification settings - Fork 378
/
decoder.js
258 lines (230 loc) · 7 KB
/
decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
self.Module = {
onRuntimeInitialized: function () {
onWasmLoaded();
}
};
self.importScripts("common.js");
self.importScripts("libffmpeg.js");
function Decoder() {
this.logger = new Logger("Decoder");
this.coreLogLevel = 1;
this.accurateSeek = true;
this.wasmLoaded = false;
this.tmpReqQue = [];
this.cacheBuffer = null;
this.decodeTimer = null;
this.videoCallback = null;
this.audioCallback = null;
this.requestCallback = null;
}
Decoder.prototype.initDecoder = function (fileSize, chunkSize) {
var ret = Module._initDecoder(fileSize, this.coreLogLevel);
this.logger.logInfo("initDecoder return " + ret + ".");
if (0 == ret) {
this.cacheBuffer = Module._malloc(chunkSize);
}
var objData = {
t: kInitDecoderRsp,
e: ret
};
self.postMessage(objData);
};
Decoder.prototype.uninitDecoder = function () {
var ret = Module._uninitDecoder();
this.logger.logInfo("Uninit ffmpeg decoder return " + ret + ".");
if (this.cacheBuffer != null) {
Module._free(this.cacheBuffer);
this.cacheBuffer = null;
}
};
Decoder.prototype.openDecoder = function () {
var paramCount = 7, paramSize = 4;
var paramByteBuffer = Module._malloc(paramCount * paramSize);
var ret = Module._openDecoder(paramByteBuffer, paramCount, this.videoCallback, this.audioCallback, this.requestCallback);
this.logger.logInfo("openDecoder return " + ret);
if (ret == 0) {
var paramIntBuff = paramByteBuffer >> 2;
var paramArray = Module.HEAP32.subarray(paramIntBuff, paramIntBuff + paramCount);
var duration = paramArray[0];
var videoPixFmt = paramArray[1];
var videoWidth = paramArray[2];
var videoHeight = paramArray[3];
var audioSampleFmt = paramArray[4];
var audioChannels = paramArray[5];
var audioSampleRate = paramArray[6];
var objData = {
t: kOpenDecoderRsp,
e: ret,
v: {
d: duration,
p: videoPixFmt,
w: videoWidth,
h: videoHeight
},
a: {
f: audioSampleFmt,
c: audioChannels,
r: audioSampleRate
}
};
self.postMessage(objData);
} else {
var objData = {
t: kOpenDecoderRsp,
e: ret
};
self.postMessage(objData);
}
Module._free(paramByteBuffer);
};
Decoder.prototype.closeDecoder = function () {
this.logger.logInfo("closeDecoder.");
if (this.decodeTimer) {
clearInterval(this.decodeTimer);
this.decodeTimer = null;
this.logger.logInfo("Decode timer stopped.");
}
var ret = Module._closeDecoder();
this.logger.logInfo("Close ffmpeg decoder return " + ret + ".");
var objData = {
t: kCloseDecoderRsp,
e: 0
};
self.postMessage(objData);
};
Decoder.prototype.startDecoding = function (interval) {
//this.logger.logInfo("Start decoding.");
if (this.decodeTimer) {
clearInterval(this.decodeTimer);
}
this.decodeTimer = setInterval(this.decode, interval);
};
Decoder.prototype.pauseDecoding = function () {
//this.logger.logInfo("Pause decoding.");
if (this.decodeTimer) {
clearInterval(this.decodeTimer);
this.decodeTimer = null;
}
};
Decoder.prototype.decode = function () {
var ret = Module._decodeOnePacket();
if (ret == 7) {
self.decoder.logger.logInfo("Decoder finished.");
self.decoder.pauseDecoding();
var objData = {
t: kDecodeFinishedEvt,
};
self.postMessage(objData);
}
while (ret == 9) {
//self.decoder.logger.logInfo("One old frame");
ret = Module._decodeOnePacket();
}
};
Decoder.prototype.sendData = function (data) {
var typedArray = new Uint8Array(data);
Module.HEAPU8.set(typedArray, this.cacheBuffer);
Module._sendData(this.cacheBuffer, typedArray.length);
};
Decoder.prototype.seekTo = function (ms) {
var accurateSeek = this.accurateSeek ? 1 : 0;
var ret = Module._seekTo(ms, accurateSeek);
var objData = {
t: kSeekToRsp,
r: ret
};
self.postMessage(objData);
};
Decoder.prototype.processReq = function (req) {
//this.logger.logInfo("processReq " + req.t + ".");
switch (req.t) {
case kInitDecoderReq:
this.initDecoder(req.s, req.c);
break;
case kUninitDecoderReq:
this.uninitDecoder();
break;
case kOpenDecoderReq:
this.openDecoder();
break;
case kCloseDecoderReq:
this.closeDecoder();
break;
case kStartDecodingReq:
this.startDecoding(req.i);
break;
case kPauseDecodingReq:
this.pauseDecoding();
break;
case kFeedDataReq:
this.sendData(req.d);
break;
case kSeekToReq:
this.seekTo(req.ms);
break;
default:
this.logger.logError("Unsupport messsage " + req.t);
}
};
Decoder.prototype.cacheReq = function (req) {
if (req) {
this.tmpReqQue.push(req);
}
};
Decoder.prototype.onWasmLoaded = function () {
this.logger.logInfo("Wasm loaded.");
this.wasmLoaded = true;
this.videoCallback = Module.addFunction(function (buff, size, timestamp) {
var outArray = Module.HEAPU8.subarray(buff, buff + size);
var data = new Uint8Array(outArray);
var objData = {
t: kVideoFrame,
s: timestamp,
d: data
};
self.postMessage(objData, [objData.d.buffer]);
}, 'viid');
this.audioCallback = Module.addFunction(function (buff, size, timestamp) {
var outArray = Module.HEAPU8.subarray(buff, buff + size);
var data = new Uint8Array(outArray);
var objData = {
t: kAudioFrame,
s: timestamp,
d: data
};
self.postMessage(objData, [objData.d.buffer]);
}, 'viid');
this.requestCallback = Module.addFunction(function (offset, availble) {
var objData = {
t: kRequestDataEvt,
o: offset,
a: availble
};
self.postMessage(objData);
}, 'vii');
while (this.tmpReqQue.length > 0) {
var req = this.tmpReqQue.shift();
this.processReq(req);
}
};
self.decoder = new Decoder;
self.onmessage = function (evt) {
if (!self.decoder) {
console.log("[ER] Decoder not initialized!");
return;
}
var req = evt.data;
if (!self.decoder.wasmLoaded) {
self.decoder.cacheReq(req);
self.decoder.logger.logInfo("Temp cache req " + req.t + ".");
return;
}
self.decoder.processReq(req);
};
function onWasmLoaded() {
if (self.decoder) {
self.decoder.onWasmLoaded();
} else {
console.log("[ER] No decoder!");
}
}