|
@@ -0,0 +1,642 @@
|
|
1
|
+/**
|
|
2
|
+ * Created by wangweijie5 on 2016/12/5.
|
|
3
|
+ */
|
|
4
|
+(function (event) {
|
|
5
|
+ const AUDIO_TYPE = 0; // 音频
|
|
6
|
+ const VIDEO_TYPE = 1; // 视频
|
|
7
|
+ const PRIVT_TYPE = 2; // 私有帧
|
|
8
|
+
|
|
9
|
+ const PLAYM4_AUDIO_FRAME = 100; // 音频帧
|
|
10
|
+ const PLAYM4_VIDEO_FRAME = 101; // 视频帧
|
|
11
|
+
|
|
12
|
+ const PLAYM4_OK = 1;
|
|
13
|
+ const PLAYM4_ORDER_ERROR = 2;
|
|
14
|
+ const PLAYM4_DECODE_ERROR = 44 // 解码失败
|
|
15
|
+ const PLAYM4_NOT_KEYFRAME = 48; // 非关键帧
|
|
16
|
+ const PLAYM4_NEED_MORE_DATA = 31; // 需要更多数据才能解析
|
|
17
|
+ const PLAYM4_NEED_NEET_LOOP = 35; //丢帧需要下个循环
|
|
18
|
+ const PLAYM4_SYS_NOT_SUPPORT = 16; // 不支持
|
|
19
|
+
|
|
20
|
+ importScripts('Decoder.js');
|
|
21
|
+ Module.addOnPostRun(function () {
|
|
22
|
+ postMessage({ 'function': "loaded" });
|
|
23
|
+ });
|
|
24
|
+
|
|
25
|
+ var iStreamMode = 0; // 流模式
|
|
26
|
+
|
|
27
|
+ var bOpenMode = false;
|
|
28
|
+ var bOpenStream = false;
|
|
29
|
+
|
|
30
|
+ var funGetFrameData = null;
|
|
31
|
+ var funGetAudFrameData = null;
|
|
32
|
+
|
|
33
|
+ var bWorkerPrintLog = false;//worker层log开关
|
|
34
|
+
|
|
35
|
+ var g_nPort = -1;
|
|
36
|
+ var pInputData = null;
|
|
37
|
+ var inputBufferSize = 40960;
|
|
38
|
+
|
|
39
|
+ self.JSPlayM4_RunTimeInfoCallBack = function (nPort, pstRunTimeInfo, pUser) {
|
|
40
|
+ let port = nPort;
|
|
41
|
+ let user = pUser;
|
|
42
|
+ let nRunTimeModule = Module.HEAP32[pstRunTimeInfo >> 2];
|
|
43
|
+ let nStrVersion = Module.HEAP32[pstRunTimeInfo + 4 >> 2];
|
|
44
|
+ let nFrameTimeStamp = Module.HEAP32[pstRunTimeInfo + 8 >> 2];
|
|
45
|
+ let nFrameNum = Module.HEAP32[pstRunTimeInfo + 12 >> 2];
|
|
46
|
+ let nErrorCode = Module.HEAP32[pstRunTimeInfo + 16 >> 2];
|
|
47
|
+ // console.log("nRunTimeModule:"+nRunTimeModule+",nFrameNum:"+nFrameNum+",nErrorCode:"+nErrorCode);
|
|
48
|
+ postMessage({ 'function': "RunTimeInfoCallBack", 'nRunTimeModule': nRunTimeModule, 'nStrVersion': nStrVersion, 'nFrameTimeStamp': nFrameTimeStamp, 'nFrameNum': nFrameNum, 'nErrorCode': nErrorCode });
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ onmessage = function (event) {
|
|
52
|
+ var eventData = event.data;
|
|
53
|
+ var res = 0;
|
|
54
|
+ switch (eventData.command) {
|
|
55
|
+ case "printLog":
|
|
56
|
+ let downloadFlag = eventData.data;
|
|
57
|
+ if (downloadFlag === true) {
|
|
58
|
+ bWorkerPrintLog = true;
|
|
59
|
+ res = Module._SetPrintLogFlag(g_nPort, downloadFlag);
|
|
60
|
+ }
|
|
61
|
+ else {
|
|
62
|
+ bWorkerPrintLog = false;
|
|
63
|
+ res = Module._SetPrintLogFlag(g_nPort, downloadFlag);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ if (res !== PLAYM4_OK) {
|
|
67
|
+ console.log("DecodeWorker.js: PlayerSDK print log failed,res" + res);
|
|
68
|
+ postMessage({ 'function': "printLog", 'errorCode': res });
|
|
69
|
+ }
|
|
70
|
+ break;
|
|
71
|
+ case "SetPlayPosition":
|
|
72
|
+ let nFrameNumOrTime = eventData.data;
|
|
73
|
+ let enPosType = eventData.type;
|
|
74
|
+ // res = Module._SetPlayPosition(nFrameNumOrTime,enPosType);
|
|
75
|
+ // if (res !== PLAYM4_OK)
|
|
76
|
+ // {
|
|
77
|
+ // postMessage({'function': "SetPlayPosition", 'errorCode': res});
|
|
78
|
+ // return;
|
|
79
|
+ // }
|
|
80
|
+ // //有没有buffer需要清除
|
|
81
|
+
|
|
82
|
+ break;
|
|
83
|
+ case "SetStreamOpenMode":
|
|
84
|
+ //获取端口号
|
|
85
|
+ g_nPort = Module._GetPort();
|
|
86
|
+ //设置流打开模式
|
|
87
|
+ iStreamMode = eventData.data;
|
|
88
|
+ res = Module._SetStreamOpenMode(g_nPort, iStreamMode);
|
|
89
|
+ if (res !== PLAYM4_OK) {
|
|
90
|
+ postMessage({ 'function': "SetStreamOpenMode", 'errorCode': res });
|
|
91
|
+ return;
|
|
92
|
+ }
|
|
93
|
+ bOpenMode = true;
|
|
94
|
+ break;
|
|
95
|
+
|
|
96
|
+ case "OpenStream":
|
|
97
|
+ // 接收到的数据
|
|
98
|
+ var iHeadLen = eventData.dataSize;
|
|
99
|
+ var pHead = Module._malloc(iHeadLen + 4);
|
|
100
|
+ if (pHead === null) {
|
|
101
|
+ return;
|
|
102
|
+ }
|
|
103
|
+ var aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen);
|
|
104
|
+ aHead.set(new Uint8Array(eventData.data));
|
|
105
|
+ res = Module._OpenStream(g_nPort, pHead, iHeadLen, eventData.bufPoolSize);
|
|
106
|
+ postMessage({ 'function': "OpenStream", 'errorCode': res });
|
|
107
|
+ if (res !== PLAYM4_OK) {
|
|
108
|
+ //释放内存
|
|
109
|
+ Module._free(pHead);
|
|
110
|
+ pHead = null;
|
|
111
|
+ return;
|
|
112
|
+ }
|
|
113
|
+ bOpenStream = true;
|
|
114
|
+ break;
|
|
115
|
+ case "Play":
|
|
116
|
+ let resP = Module._Play(g_nPort);
|
|
117
|
+ if (resP !== PLAYM4_OK) {
|
|
118
|
+ return;
|
|
119
|
+ }
|
|
120
|
+ break;
|
|
121
|
+ case "InputData":
|
|
122
|
+ // 接收到的数据
|
|
123
|
+ var iLen = eventData.dataSize;
|
|
124
|
+ if (iLen > 0) {
|
|
125
|
+ if (pInputData == null || iLen > inputBufferSize) {
|
|
126
|
+ if (pInputData != null) {
|
|
127
|
+ Module._free(pInputData);
|
|
128
|
+ pInputData = null;
|
|
129
|
+ }
|
|
130
|
+ if (iLen > inputBufferSize) {
|
|
131
|
+ inputBufferSize = iLen;
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ pInputData = Module._malloc(inputBufferSize);
|
|
135
|
+ if (pInputData === null) {
|
|
136
|
+ return;
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ var inputData = new Uint8Array(eventData.data);
|
|
141
|
+ // var aInputData = Module.HEAPU8.subarray(pInputData, pInputData + iLen);
|
|
142
|
+ // aInputData.set(inputData);
|
|
143
|
+ Module.writeArrayToMemory(inputData, pInputData);
|
|
144
|
+ inputData = null;
|
|
145
|
+ res = Module._InputData(g_nPort, pInputData, iLen);
|
|
146
|
+ if (res !== PLAYM4_OK) {
|
|
147
|
+ let errorCode = Module._GetLastError(g_nPort);
|
|
148
|
+ let sourceRemain = Module._GetSourceBufferRemain(g_nPort);
|
|
149
|
+ postMessage({ 'function': "InputData", 'errorCode': errorCode, "sourceRemain": sourceRemain });
|
|
150
|
+ }
|
|
151
|
+ //Module._free(pInputData);
|
|
152
|
+ //pInputData = null;
|
|
153
|
+ } else {
|
|
154
|
+ let sourceRemain = Module._GetSourceBufferRemain(g_nPort);
|
|
155
|
+ if (sourceRemain == 0) {
|
|
156
|
+ postMessage({ 'function': "InputData", 'errorCode': PLAYM4_NEED_MORE_DATA });
|
|
157
|
+ return;
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ /////////////////////
|
|
162
|
+ // if (funGetFrameData === null) {
|
|
163
|
+ // funGetFrameData = Module.cwrap('GetFrameData', 'number');
|
|
164
|
+ // }
|
|
165
|
+
|
|
166
|
+ while (bOpenMode && bOpenStream) {
|
|
167
|
+
|
|
168
|
+ var ret = getFrameData();
|
|
169
|
+ // 直到获取视频帧或数据不足为止
|
|
170
|
+ if (PLAYM4_VIDEO_FRAME === ret || PLAYM4_NEED_MORE_DATA === ret || PLAYM4_ORDER_ERROR === ret)//PLAYM4_VIDEO_FRAME === ret || || PLAYM4_NEED_NEET_LOOP === ret
|
|
171
|
+ {
|
|
172
|
+ break;
|
|
173
|
+ }
|
|
174
|
+ }
|
|
175
|
+ break;
|
|
176
|
+
|
|
177
|
+ case "SetSecretKey":
|
|
178
|
+ var keyLen = eventData.nKeyLen;
|
|
179
|
+ var pKeyData = Module._malloc(keyLen);
|
|
180
|
+ if (pKeyData === null) {
|
|
181
|
+ return;
|
|
182
|
+ }
|
|
183
|
+ var nKeySize = eventData.data.length
|
|
184
|
+ var bufData = stringToBytes(eventData.data);
|
|
185
|
+ var aKeyData = Module.HEAPU8.subarray(pKeyData, pKeyData + keyLen);
|
|
186
|
+ let u8array = new Uint8Array(keyLen);
|
|
187
|
+ aKeyData.set(u8array, 0);
|
|
188
|
+ aKeyData.set(new Uint8Array(bufData));
|
|
189
|
+ aKeyData = null;
|
|
190
|
+ u8array = null;
|
|
191
|
+
|
|
192
|
+ res = Module._SetSecretKey(g_nPort, eventData.nKeyType, pKeyData, keyLen);//, nKeySize
|
|
193
|
+ if (res !== PLAYM4_OK) {
|
|
194
|
+ postMessage({ 'function': "SetSecretKey", 'errorCode': res });
|
|
195
|
+ Module._free(pKeyData);
|
|
196
|
+ pKeyData = null;
|
|
197
|
+ return;
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ Module._free(pKeyData);
|
|
201
|
+ pKeyData = null;
|
|
202
|
+ break;
|
|
203
|
+
|
|
204
|
+ case "GetBMP":
|
|
205
|
+ var nBMPWidth = eventData.width;
|
|
206
|
+ var nBMPHeight = eventData.height;
|
|
207
|
+ var pYUVData = eventData.data;
|
|
208
|
+ var nYUVSize = nBMPWidth * nBMPHeight * 3 / 2;
|
|
209
|
+ var oJpegCropRect = {
|
|
210
|
+ left: eventData.left,
|
|
211
|
+ top: eventData.top,
|
|
212
|
+ right: eventData.right,
|
|
213
|
+ bottom: eventData.bottom
|
|
214
|
+ };
|
|
215
|
+
|
|
216
|
+ var pDataYUV = Module._malloc(nYUVSize);
|
|
217
|
+ if (pDataYUV === null) {
|
|
218
|
+ return;
|
|
219
|
+ }
|
|
220
|
+
|
|
221
|
+ Module.writeArrayToMemory(new Uint8Array(pYUVData, 0, nYUVSize), pDataYUV);
|
|
222
|
+
|
|
223
|
+ // 分配BMP空间
|
|
224
|
+ var nBmpSize = nBMPWidth * nBMPHeight * 4 + 60;
|
|
225
|
+ var pBmpData = Module._malloc(nBmpSize);
|
|
226
|
+ var pBmpSize = Module._malloc(4);
|
|
227
|
+ if (pBmpData === null || pBmpSize === null) {
|
|
228
|
+ Module._free(pDataYUV);
|
|
229
|
+ pDataYUV = null;
|
|
230
|
+
|
|
231
|
+ if (pBmpData != null) {
|
|
232
|
+ Module._free(pBmpData);
|
|
233
|
+ pBmpData = null;
|
|
234
|
+ }
|
|
235
|
+
|
|
236
|
+ if (pBmpSize != null) {
|
|
237
|
+ Module._free(pBmpSize);
|
|
238
|
+ pBmpSize = null;
|
|
239
|
+ }
|
|
240
|
+ return;
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ //Module._memset(pBmpSize, nBmpSize, 4); // 防止bmp截图出现输入数据过大的错误码
|
|
244
|
+ Module.setValue(pBmpSize, nBmpSize, "i32");
|
|
245
|
+ res = Module._GetBMP(g_nPort, pDataYUV, nYUVSize, pBmpData, pBmpSize,
|
|
246
|
+ oBMPCropRect.left, oBMPCropRect.top, oBMPCropRect.right, oBMPCropRect.bottom);
|
|
247
|
+ if (res !== PLAYM4_OK) {
|
|
248
|
+ postMessage({ 'function': "GetBMP", 'errorCode': res });
|
|
249
|
+ Module._free(pDataYUV);
|
|
250
|
+ pDataYUV = null;
|
|
251
|
+ Module._free(pBmpData);
|
|
252
|
+ pBmpData = null;
|
|
253
|
+ Module._free(pBmpSize);
|
|
254
|
+ pBmpSize = null;
|
|
255
|
+ return;
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ // 获取BMP图片大小
|
|
259
|
+ var nBmpDataSize = Module.getValue(pBmpSize, "i32");
|
|
260
|
+
|
|
261
|
+ // 获取BMP图片数据
|
|
262
|
+ var aBmpData = new Uint8Array(nBmpDataSize);
|
|
263
|
+ aBmpData.set(Module.HEAPU8.subarray(pBmpData, pBmpData + nBmpDataSize));
|
|
264
|
+
|
|
265
|
+ postMessage({ 'function': "GetBMP", 'data': aBmpData, 'errorCode': res }, [aBmpData.buffer]);
|
|
266
|
+ aBmpData = null;
|
|
267
|
+ if (pDataYUV != null) {
|
|
268
|
+ Module._free(pDataYUV);
|
|
269
|
+ pDataYUV = null;
|
|
270
|
+ }
|
|
271
|
+ if (pBmpData != null) {
|
|
272
|
+ Module._free(pBmpData);
|
|
273
|
+ pBmpData = null;
|
|
274
|
+ }
|
|
275
|
+ if (pBmpSize != null) {
|
|
276
|
+ Module._free(pBmpSize);
|
|
277
|
+ pBmpSize = null;
|
|
278
|
+ }
|
|
279
|
+ break;
|
|
280
|
+
|
|
281
|
+ case "GetJPEG":
|
|
282
|
+ var nJpegWidth = eventData.width;
|
|
283
|
+ var nJpegHeight = eventData.height;
|
|
284
|
+ var pYUVData1 = eventData.data;
|
|
285
|
+ var nYUVSize1 = nJpegWidth * nJpegHeight * 3 / 2;
|
|
286
|
+ var oJpegCropRect = {
|
|
287
|
+ left: eventData.left,
|
|
288
|
+ top: eventData.top,
|
|
289
|
+ right: eventData.right,
|
|
290
|
+ bottom: eventData.bottom
|
|
291
|
+ };
|
|
292
|
+
|
|
293
|
+ var pDataYUV1 = Module._malloc(nYUVSize1);
|
|
294
|
+ if (pDataYUV1 === null) {
|
|
295
|
+ return;
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ Module.writeArrayToMemory(new Uint8Array(pYUVData1, 0, nYUVSize1), pDataYUV1);
|
|
299
|
+
|
|
300
|
+ // 分配JPEG空间
|
|
301
|
+ var pJpegData = Module._malloc(nYUVSize1);
|
|
302
|
+ var pJpegSize = Module._malloc(4);
|
|
303
|
+ if (pJpegData === null || pJpegSize === null) {
|
|
304
|
+ if (pJpegData != null) {
|
|
305
|
+ Module._free(pJpegData);
|
|
306
|
+ pJpegData = null;
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+ if (pJpegSize != null) {
|
|
310
|
+ Module._free(pJpegSize);
|
|
311
|
+ pJpegSize = null;
|
|
312
|
+ }
|
|
313
|
+
|
|
314
|
+ if (pDataYUV1 != null) {
|
|
315
|
+ Module._free(pDataYUV1);
|
|
316
|
+ pDataYUV1 = null;
|
|
317
|
+ }
|
|
318
|
+ return;
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ Module.setValue(pJpegSize, nJpegWidth * nJpegHeight * 2, "i32"); // JPEG抓图,输入缓冲长度不小于当前帧YUV大小
|
|
322
|
+
|
|
323
|
+ res = Module._GetJPEG(g_nPort, pDataYUV1, nYUVSize1, pJpegData, pJpegSize,
|
|
324
|
+ oJpegCropRect.left, oJpegCropRect.top, oJpegCropRect.right, oJpegCropRect.bottom);
|
|
325
|
+ if (res !== PLAYM4_OK) {
|
|
326
|
+ postMessage({ 'function': "GetJPEG", 'errorCode': res });
|
|
327
|
+ if (pJpegData != null) {
|
|
328
|
+ Module._free(pJpegData);
|
|
329
|
+ pJpegData = null;
|
|
330
|
+ }
|
|
331
|
+
|
|
332
|
+ if (pJpegSize != null) {
|
|
333
|
+ Module._free(pJpegSize);
|
|
334
|
+ pJpegSize = null;
|
|
335
|
+ }
|
|
336
|
+
|
|
337
|
+ if (pDataYUV1 != null) {
|
|
338
|
+ Module._free(pDataYUV1);
|
|
339
|
+ pDataYUV1 = null;
|
|
340
|
+ }
|
|
341
|
+ return;
|
|
342
|
+ }
|
|
343
|
+
|
|
344
|
+ // 获取JPEG图片大小
|
|
345
|
+ var nJpegSize = Module.getValue(pJpegSize, "i32");
|
|
346
|
+
|
|
347
|
+ // 获取JPEG图片数据
|
|
348
|
+ var aJpegData = new Uint8Array(nJpegSize);
|
|
349
|
+ aJpegData.set(Module.HEAPU8.subarray(pJpegData, pJpegData + nJpegSize));
|
|
350
|
+
|
|
351
|
+ postMessage({ 'function': "GetJPEG", 'data': aJpegData, 'errorCode': res }, [aJpegData.buffer]);
|
|
352
|
+
|
|
353
|
+ nJpegSize = null;
|
|
354
|
+ aJpegData = null;
|
|
355
|
+
|
|
356
|
+ if (pDataYUV1 != null) {
|
|
357
|
+ Module._free(pDataYUV1);
|
|
358
|
+ pDataYUV1 = null;
|
|
359
|
+ }
|
|
360
|
+ if (pJpegData != null) {
|
|
361
|
+ Module._free(pJpegData);
|
|
362
|
+ pJpegData = null;
|
|
363
|
+ }
|
|
364
|
+ if (pJpegSize != null) {
|
|
365
|
+ Module._free(pJpegSize);
|
|
366
|
+ pJpegSize = null;
|
|
367
|
+ }
|
|
368
|
+ break;
|
|
369
|
+
|
|
370
|
+ case "SetDecodeFrameType":
|
|
371
|
+ var nFrameType = eventData.data;
|
|
372
|
+ res = Module._SetDecodeFrameType(g_nPort, nFrameType);
|
|
373
|
+ if (res !== PLAYM4_OK) {
|
|
374
|
+ postMessage({ 'function': "SetDecodeFrameType", 'errorCode': res });
|
|
375
|
+ return;
|
|
376
|
+ }
|
|
377
|
+ break;
|
|
378
|
+ case "CloseStream":
|
|
379
|
+ //stop
|
|
380
|
+ let resS = Module._Stop(g_nPort);
|
|
381
|
+ if (resS !== PLAYM4_OK) {
|
|
382
|
+ postMessage({ 'function': "Stop", 'errorCode': res });
|
|
383
|
+ return;
|
|
384
|
+ }
|
|
385
|
+ //closeStream
|
|
386
|
+ res = Module._CloseStream(g_nPort);
|
|
387
|
+ if (res !== PLAYM4_OK) {
|
|
388
|
+ postMessage({ 'function': "CloseStream", 'errorCode': res });
|
|
389
|
+ return;
|
|
390
|
+ }
|
|
391
|
+ //freePort
|
|
392
|
+ let resF = Module._FreePort(g_nPort);
|
|
393
|
+ if (resF !== PLAYM4_OK) {
|
|
394
|
+ postMessage({ 'function': "FreePort", 'errorCode': res });
|
|
395
|
+ return;
|
|
396
|
+ }
|
|
397
|
+ if (pInputData != null) {
|
|
398
|
+ Module._free(pInputData);
|
|
399
|
+ pInputData = null;
|
|
400
|
+ }
|
|
401
|
+ break;
|
|
402
|
+ case "PlaySound":
|
|
403
|
+ let resPS = Module._PlaySound(g_nPort);
|
|
404
|
+ if (resPS !== PLAYM4_OK) {
|
|
405
|
+ console.log("PlaySound failed");
|
|
406
|
+ return;
|
|
407
|
+ }
|
|
408
|
+ break;
|
|
409
|
+ case "StopSound":
|
|
410
|
+ let resSS = Module._StopSound();
|
|
411
|
+ if (resSS !== PLAYM4_OK) {
|
|
412
|
+ console.log("StopSound failed");
|
|
413
|
+ return;
|
|
414
|
+ }
|
|
415
|
+ break;
|
|
416
|
+ case "SetVolume":
|
|
417
|
+ let resSV = Module._SetVolume(g_nPort, eventData.volume);
|
|
418
|
+ if (resSV !== PLAYM4_OK) {
|
|
419
|
+ console.log("Audio SetVolume failed");
|
|
420
|
+ return;
|
|
421
|
+ }
|
|
422
|
+ break;
|
|
423
|
+ case "GetVolume":
|
|
424
|
+ let volume = Module._GetVolume();
|
|
425
|
+ if (volume > 0) {
|
|
426
|
+ postMessage({ 'function': "GetVolume", 'volume': volume });
|
|
427
|
+ }
|
|
428
|
+ else {
|
|
429
|
+ console.log("Audio GetVolume failed");
|
|
430
|
+ return;
|
|
431
|
+ }
|
|
432
|
+ break;
|
|
433
|
+ case "OnlyPlaySound":
|
|
434
|
+ let resOPS = Module._OnlyPlaySound(g_nPort);
|
|
435
|
+ if (resOPS !== PLAYM4_OK) {
|
|
436
|
+ console.log("OnlyPlaySound failed");
|
|
437
|
+ return;
|
|
438
|
+ }
|
|
439
|
+ break;
|
|
440
|
+ case "Pause":
|
|
441
|
+ let resPa = Module._Pause(g_nPort, eventData.bPlay);
|
|
442
|
+ if (resPa !== PLAYM4_OK) {
|
|
443
|
+ console.log("Pause failed");
|
|
444
|
+ return;
|
|
445
|
+ }
|
|
446
|
+ case "PlayRate":
|
|
447
|
+ Module._SetPlayRate(g_nPort, eventData.playRate);
|
|
448
|
+ break;
|
|
449
|
+ case "SetIFrameDecInterval":
|
|
450
|
+ Module._SetIFrameDecInterval(g_nPort, eventData.data);
|
|
451
|
+ break;
|
|
452
|
+ case "SetLostFrameMode":
|
|
453
|
+ Module._SetLostFrameMode(g_nPort, eventData.data, 0);
|
|
454
|
+ break;
|
|
455
|
+ case "SetDemuxModel":
|
|
456
|
+ Module._SetDemuxModel(g_nPort, eventData.nIdemuxType, eventData.bTrue);
|
|
457
|
+ break;
|
|
458
|
+ case "SkipErrorData":
|
|
459
|
+ Module._SkipErrorData(g_nPort, eventData.bSkip);
|
|
460
|
+ break;
|
|
461
|
+ case "SetDecodeERC":
|
|
462
|
+ Module._SetDecodeERC(g_nPort, eventData.nLevel);
|
|
463
|
+ break;
|
|
464
|
+ case "SetANRParam":
|
|
465
|
+ Module._SetANRParam(g_nPort, eventData.nEnable, eventData.nANRLevel);
|
|
466
|
+ break;
|
|
467
|
+ case "SetResampleValue":
|
|
468
|
+ Module._SetResampleValue(g_nPort, eventData.nEnable, eventData.resampleValue);
|
|
469
|
+ break;
|
|
470
|
+ case "GetLastError":
|
|
471
|
+ let errorCode = Module._GetLastError(g_nPort);
|
|
472
|
+ postMessage({ 'function': "GetLastError", 'errorCode': errorCode });
|
|
473
|
+ break;
|
|
474
|
+ case "SetGlobalBaseTime":
|
|
475
|
+ Module._SetGlobalBaseTime(g_nPort, eventData.year, eventData.month, eventData.day, eventData.hour, eventData.min, eventData.sec, eventData.ms);
|
|
476
|
+ break;
|
|
477
|
+ case "SetRunTimeInfoCB":
|
|
478
|
+ Module._SetRunTimeInfoCallBackEx(g_nPort, eventData.nModuleType, 0);
|
|
479
|
+ break;
|
|
480
|
+ default:
|
|
481
|
+ break;
|
|
482
|
+ }
|
|
483
|
+ };
|
|
484
|
+
|
|
485
|
+ function getOSDTime(oFrameInfo) {
|
|
486
|
+ var iYear = oFrameInfo.year;
|
|
487
|
+ var iMonth = oFrameInfo.month;
|
|
488
|
+ var iDay = oFrameInfo.day;
|
|
489
|
+ var iHour = oFrameInfo.hour;
|
|
490
|
+ var iMinute = oFrameInfo.minute;
|
|
491
|
+ var iSecond = oFrameInfo.second;
|
|
492
|
+
|
|
493
|
+ if (iMonth < 10) {
|
|
494
|
+ iMonth = "0" + iMonth;
|
|
495
|
+ }
|
|
496
|
+ if (iDay < 10) {
|
|
497
|
+ iDay = "0" + iDay;
|
|
498
|
+ }
|
|
499
|
+ if (iHour < 10) {
|
|
500
|
+ iHour = "0" + iHour;
|
|
501
|
+ }
|
|
502
|
+ if (iMinute < 10) {
|
|
503
|
+ iMinute = "0" + iMinute;
|
|
504
|
+ }
|
|
505
|
+ if (iSecond < 10) {
|
|
506
|
+ iSecond = "0" + iSecond;
|
|
507
|
+ }
|
|
508
|
+
|
|
509
|
+ return iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
|
|
510
|
+ }
|
|
511
|
+ // 获取帧数据
|
|
512
|
+ function getFrameData() {
|
|
513
|
+ // function getFrameData() {
|
|
514
|
+ // 获取帧数据
|
|
515
|
+ var res = Module._GetFrameData();
|
|
516
|
+ //var res = fun();
|
|
517
|
+ if (res === PLAYM4_OK) {
|
|
518
|
+ var oFrameInfo = Module._GetFrameInfo();
|
|
519
|
+ switch (oFrameInfo.frameType) {
|
|
520
|
+ case AUDIO_TYPE:
|
|
521
|
+ var iSize = oFrameInfo.frameSize;
|
|
522
|
+ if (0 === iSize) {
|
|
523
|
+ return -1;
|
|
524
|
+ }
|
|
525
|
+ var pPCM = Module._GetFrameBuffer();
|
|
526
|
+ // var audioBuf = new ArrayBuffer(iSize);
|
|
527
|
+ var aPCMData = new Uint8Array(iSize);
|
|
528
|
+ aPCMData.set(Module.HEAPU8.subarray(pPCM, pPCM + iSize));
|
|
529
|
+ if (bWorkerPrintLog) {
|
|
530
|
+ console.log("<<<Worker: audio media Info: nSise:" + oFrameInfo.frameSize + ",nSampleRate:" + oFrameInfo.samplesPerSec + ',channel:' + oFrameInfo.channels + ',bitsPerSample:' + oFrameInfo.bitsPerSample);
|
|
531
|
+ }
|
|
532
|
+ postMessage({
|
|
533
|
+ 'function': "GetFrameData", 'type': "audioType", 'data': aPCMData.buffer,
|
|
534
|
+ 'frameInfo': oFrameInfo, 'errorCode': res
|
|
535
|
+ }, [aPCMData.buffer]);
|
|
536
|
+
|
|
537
|
+ oFrameInfo = null;
|
|
538
|
+ pPCM = null;
|
|
539
|
+ aPCMData = null;
|
|
540
|
+ return PLAYM4_AUDIO_FRAME;
|
|
541
|
+
|
|
542
|
+ case VIDEO_TYPE:
|
|
543
|
+ var szOSDTime = getOSDTime(oFrameInfo);
|
|
544
|
+
|
|
545
|
+ var iWidth = oFrameInfo.width;
|
|
546
|
+ var iHeight = oFrameInfo.height;
|
|
547
|
+
|
|
548
|
+ var iYUVSize = iWidth * iHeight * 3 / 2;
|
|
549
|
+ if (0 === iYUVSize) {
|
|
550
|
+ return -1;
|
|
551
|
+ }
|
|
552
|
+
|
|
553
|
+ var pYUV = Module._GetFrameBuffer();
|
|
554
|
+
|
|
555
|
+ // 图像数据渲染后压回,若从主码流切到子码流,存在数组大小与图像大小不匹配现象
|
|
556
|
+ var aYUVData = new Uint8Array(iYUVSize);
|
|
557
|
+ aYUVData.set(Module.HEAPU8.subarray(pYUV, pYUV + iYUVSize));
|
|
558
|
+ if (bWorkerPrintLog) {
|
|
559
|
+ console.log("<<<Worker: video media Info: Width:" + oFrameInfo.width + ",Height:" + oFrameInfo.height + ",timeStamp:" + oFrameInfo.timeStamp);
|
|
560
|
+ }
|
|
561
|
+
|
|
562
|
+ postMessage({
|
|
563
|
+ 'function': "GetFrameData", 'type': "videoType", 'data': aYUVData.buffer,
|
|
564
|
+ 'dataLen': aYUVData.length, 'osd': szOSDTime, 'frameInfo': oFrameInfo, 'errorCode': res
|
|
565
|
+ }, [aYUVData.buffer]);
|
|
566
|
+
|
|
567
|
+ oFrameInfo = null;
|
|
568
|
+ pYUV = null;
|
|
569
|
+ aYUVData = null;
|
|
570
|
+ return PLAYM4_VIDEO_FRAME;
|
|
571
|
+
|
|
572
|
+ case PRIVT_TYPE:
|
|
573
|
+ postMessage({
|
|
574
|
+ 'function': "GetFrameData", 'type': "", 'data': null,
|
|
575
|
+ 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
|
|
576
|
+ });
|
|
577
|
+ return PLAYM4_SYS_NOT_SUPPORT;
|
|
578
|
+
|
|
579
|
+ default:
|
|
580
|
+ postMessage({
|
|
581
|
+ 'function': "GetFrameData", 'type': "", 'data': null,
|
|
582
|
+ 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
|
|
583
|
+ });
|
|
584
|
+ return PLAYM4_SYS_NOT_SUPPORT;
|
|
585
|
+ }
|
|
586
|
+ }
|
|
587
|
+ else {
|
|
588
|
+ let errorCode = Module._GetLastError(g_nPort);
|
|
589
|
+ //解码失败返回裸数据
|
|
590
|
+ if (PLAYM4_DECODE_ERROR === errorCode) {
|
|
591
|
+ var rawInfo = Module._GetRawDataInfo();
|
|
592
|
+ var pRawData = Module._GetRawDataBuffer();
|
|
593
|
+ var aRawData = new Uint8Array(rawInfo.isize);
|
|
594
|
+ aRawData.set(Module.HEAPU8.subarray(pRawData, pRawData + rawInfo.isize));
|
|
595
|
+ postMessage({
|
|
596
|
+ 'function': "GetRawData", 'type': "", 'data': aRawData.buffer,
|
|
597
|
+ 'rawDataLen': rawInfo.isize, 'osd': 0, 'frameInfo': null, 'errorCode': errorCode
|
|
598
|
+ });
|
|
599
|
+ rawInfo = null;
|
|
600
|
+ pRawData = null;
|
|
601
|
+ aRawData = null;
|
|
602
|
+ }
|
|
603
|
+ //需要更多数据
|
|
604
|
+ if (PLAYM4_NEED_MORE_DATA === errorCode || PLAYM4_SYS_NOT_SUPPORT === errorCode || PLAYM4_NEED_NEET_LOOP === errorCode) {
|
|
605
|
+ postMessage({
|
|
606
|
+ 'function': "GetFrameData", 'type': "", 'data': null,
|
|
607
|
+ 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': errorCode
|
|
608
|
+ });
|
|
609
|
+ }
|
|
610
|
+ return errorCode;
|
|
611
|
+ }
|
|
612
|
+ }
|
|
613
|
+
|
|
614
|
+ // 开始计算时间
|
|
615
|
+ function startTime() {
|
|
616
|
+ return new Date().getTime();
|
|
617
|
+ }
|
|
618
|
+
|
|
619
|
+ // 结束计算时间
|
|
620
|
+ function endTime() {
|
|
621
|
+ return new Date().getTime();
|
|
622
|
+ }
|
|
623
|
+
|
|
624
|
+ // 字母字符串转byte数组
|
|
625
|
+ function stringToBytes(str) {
|
|
626
|
+ var ch, st, re = [];
|
|
627
|
+ for (var i = 0; i < str.length; i++) {
|
|
628
|
+ ch = str.charCodeAt(i); // get char
|
|
629
|
+ st = []; // set up "stack"
|
|
630
|
+ do {
|
|
631
|
+ st.push(ch & 0xFF); // push byte to stack
|
|
632
|
+ ch = ch >> 8; // shift value down by 1 byte
|
|
633
|
+ }
|
|
634
|
+ while (ch);
|
|
635
|
+ // add stack contents to result
|
|
636
|
+ // done because chars have "wrong" endianness
|
|
637
|
+ re = re.concat(st.reverse());
|
|
638
|
+ }
|
|
639
|
+ // return an array of bytes
|
|
640
|
+ return re;
|
|
641
|
+ }
|
|
642
|
+})();
|