DecodeWorker.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. const PLAYM4_AUDIO_FRAME = 100; // 音频帧
  9. const PLAYM4_VIDEO_FRAME = 101; // 视频帧
  10. const PLAYM4_OK = 1;
  11. const PLAYM4_DECODE_ERROR = 44 // 解码失败
  12. const PLAYM4_NOT_KEYFRAME = 48; // 非关键帧
  13. const PLAYM4_NEED_MORE_DATA = 31; // 需要更多数据才能解析
  14. const PLAYM4_SYS_NOT_SUPPORT = 16; // 不支持
  15. const PLAYM4_PARA_ENCODER_ERROR = 71; //音频编码参数错误
  16. const PLAYM4_PRECONDITION_ENCODER_ERROR = 72; //不满足音频编码条件错误
  17. const PLAYM4_ENCODER_ERROR = 73; //音频编码失败
  18. const PLAYM4_CREATE_ENCODER_ERROR = 74; //创建音频编码器失败
  19. const PLAYM4_NOSUPPORT_ENCODER_ERROR = 75; //音频编码不支持
  20. const PLAYM4_ALLOC_MEMORY_ENCODER_ERROR = 76; //音频编码相关内存申请失败
  21. const PLAYM4_BUF_OVER_ENCODER_ERROR = 77; //音频编码相关buffer满
  22. const PLAYM4_NEED_MORE_DATA_ENCODER_ERROR = 78; //音频编码需要更多数据进行编码
  23. const PLAYM4_CALL_ORDER_ENCODER_ERROR = 79; //音频编码调用顺序错误
  24. const PLAYM4_ITYPE_DECODE_ERROR = 100; //定位后送进来的第一帧I帧解码失败
  25. const PLAYM4_FIRST_FRAME_NOT_ICURRENT = 101; //定位后送进来的第一帧不是定位帧所在的I帧(Ni>Mp)
  26. importScripts('Decoder.js');
  27. Module.postRun.push(function () {
  28. postMessage({'function': "loaded"});
  29. });
  30. var iStreamMode = 0; // 流模式
  31. var bOpenMode = false;
  32. var bOpenStream = false;
  33. var funGetFrameData = null;
  34. var funGetAudFrameData = null;
  35. var bWorkerPrintLog = false;//worker层log开关
  36. onmessage = function (event) {
  37. var eventData = event.data;
  38. var res = 0;
  39. switch (eventData.command) {
  40. case "printLog":
  41. let downloadFlag = eventData.data;
  42. if (downloadFlag === true) {
  43. bWorkerPrintLog = true;
  44. res = Module._SetPrintLogFlag(downloadFlag);
  45. } else {
  46. bWorkerPrintLog = false;
  47. res = Module._SetPrintLogFlag(downloadFlag);
  48. }
  49. if (res !== PLAYM4_OK) {
  50. console.log("DecodeWorker.js: PlayerSDK print log failed,res" + res);
  51. postMessage({'function': "printLog", 'errorCode': res});
  52. }
  53. break;
  54. case "SetPlayPosition":
  55. let nFrameNumOrTime = eventData.data;
  56. let enPosType = eventData.type;
  57. res = Module._SetPlayPosition(nFrameNumOrTime, enPosType);
  58. if (res !== PLAYM4_OK) {
  59. postMessage({'function': "SetPlayPosition", 'errorCode': res});
  60. return;
  61. }
  62. //有没有buffer需要清除
  63. break;
  64. case "SetStreamOpenMode":
  65. iStreamMode = eventData.data;
  66. res = Module._SetStreamOpenMode(iStreamMode);
  67. if (res !== PLAYM4_OK) {
  68. postMessage({'function': "SetStreamOpenMode", 'errorCode': res});
  69. return;
  70. }
  71. bOpenMode = true;
  72. break;
  73. case "OpenStream":
  74. // 接收到的数据
  75. var iHeadLen = eventData.dataSize;
  76. var pHead = Module._malloc(iHeadLen + 4);
  77. if (pHead === null) {
  78. return;
  79. }
  80. var aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen);
  81. aHead.set(eventData.data);
  82. res = Module._OpenStream(pHead, iHeadLen, eventData.bufPoolSize);
  83. postMessage({'function': "OpenStream", 'errorCode': res});
  84. if (res !== PLAYM4_OK) {
  85. //释放内存
  86. Module._free(pHead);
  87. pHead = null;
  88. return;
  89. }
  90. bOpenStream = true;
  91. // 加4字节长度信息
  92. var a32 = new Uint32Array([iHeadLen]);
  93. var a8 = new Uint8Array(a32.buffer);
  94. var tempBuf = new Uint8Array(iHeadLen + 4);
  95. tempBuf.set(a8, 0);
  96. tempBuf.set(eventData.data, 4);
  97. a32 = null;
  98. a8 = null;
  99. aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen + 4);
  100. aHead.set(tempBuf);
  101. tempBuf = null;
  102. res = Module._InputData(pHead, iHeadLen + 4);
  103. if (res !== PLAYM4_OK) {
  104. postMessage({'function': "InputData", 'errorCode': res});
  105. Module._free(pHead);
  106. pHead = null;
  107. return;
  108. }
  109. // 释放内存
  110. Module._free(pHead);
  111. pHead = null;
  112. if (funGetFrameData === null) {
  113. funGetFrameData = Module.cwrap('GetFrameData', 'number');
  114. }
  115. if (iStreamMode === 0) {
  116. // Module._GetFrameData();
  117. funGetFrameData();
  118. }
  119. break;
  120. case "InputData":
  121. // 接收到的数据
  122. var iLen = eventData.dataSize;
  123. if (bWorkerPrintLog) {
  124. console.log("<<<Worker: DecodeWorker-InputData iLen:" + iLen);
  125. }
  126. if (iLen > 0) {
  127. var pInputData = Module._malloc(iLen);
  128. if (pInputData === null) {
  129. return;
  130. }
  131. var inputData = new Uint8Array(eventData.data);
  132. // var aInputData = Module.HEAPU8.subarray(pInputData, pInputData + iLen);
  133. // aInputData.set(inputData);
  134. Module.writeArrayToMemory(inputData, pInputData);
  135. inputData = null;
  136. res = Module._InputData(pInputData, iLen);
  137. //console.log("DecodeWorker-InputData-ret:%d", res);
  138. if (bWorkerPrintLog) {
  139. console.log("<<<Worker:InputData result:" + +res);
  140. }
  141. if (res !== PLAYM4_OK) {
  142. if (res === 98) {
  143. res = 1;
  144. }
  145. postMessage({'function': "InputData", 'errorCode': res});
  146. }
  147. Module._free(pInputData);
  148. pInputData = null;
  149. }
  150. /////////////////////
  151. if (funGetFrameData === null) {
  152. funGetFrameData = Module.cwrap('GetFrameData', 'number');
  153. }
  154. while (bOpenMode && bOpenStream) {
  155. var ret = getFrameData(funGetFrameData);
  156. // 直到获取视频帧或数据不足为止
  157. if (PLAYM4_VIDEO_FRAME === ret || PLAYM4_NEED_MORE_DATA === ret) {
  158. break;
  159. }
  160. }
  161. break;
  162. case "SetSecretKey":
  163. var keyLen = eventData.nKeyLen;
  164. var pKeyData = Module._malloc(keyLen);
  165. if (pKeyData === null) {
  166. return;
  167. }
  168. var nKeySize = eventData.data.length
  169. var bufData = stringToBytes(eventData.data);
  170. var aKeyData = Module.HEAPU8.subarray(pKeyData, pKeyData + keyLen);
  171. aKeyData.set(new Uint8Array(bufData));
  172. res = Module._SetSecretKey(eventData.nKeyType, pKeyData, keyLen, nKeySize);
  173. if (res !== PLAYM4_OK) {
  174. postMessage({'function': "SetSecretKey", 'errorCode': res});
  175. Module._free(pKeyData);
  176. pKeyData = null;
  177. return;
  178. }
  179. Module._free(pKeyData);
  180. pKeyData = null;
  181. break;
  182. case "GetBMP":
  183. var nBMPWidth = eventData.width;
  184. var nBMPHeight = eventData.height;
  185. var pYUVData = eventData.data;
  186. var nYUVSize = nBMPWidth * nBMPHeight * 3 / 2;
  187. var oBMPCropRect = eventData.rect;
  188. var pDataYUV = Module._malloc(nYUVSize);
  189. if (pDataYUV === null) {
  190. return;
  191. }
  192. Module.writeArrayToMemory(new Uint8Array(pYUVData, 0, nYUVSize), pDataYUV);
  193. // 分配BMP空间
  194. var nBmpSize = nBMPWidth * nBMPHeight * 4 + 60;
  195. var pBmpData = Module._malloc(nBmpSize);
  196. var pBmpSize = Module._malloc(4);
  197. if (pBmpData === null || pBmpSize === null) {
  198. Module._free(pDataYUV);
  199. pDataYUV = null;
  200. if (pBmpData != null) {
  201. Module._free(pBmpData);
  202. pBmpData = null;
  203. }
  204. if (pBmpSize != null) {
  205. Module._free(pBmpSize);
  206. pBmpSize = null;
  207. }
  208. return;
  209. }
  210. Module._memset(pBmpSize, nBmpSize, 4); // 防止bmp截图出现输入数据过大的错误码
  211. res = Module._GetBMP(pDataYUV, nYUVSize, pBmpData, pBmpSize,
  212. oBMPCropRect.left, oBMPCropRect.top, oBMPCropRect.right, oBMPCropRect.bottom);
  213. if (res !== PLAYM4_OK) {
  214. postMessage({'function': "GetBMP", 'errorCode': res});
  215. Module._free(pDataYUV);
  216. pDataYUV = null;
  217. Module._free(pBmpData);
  218. pBmpData = null;
  219. Module._free(pBmpSize);
  220. pBmpSize = null;
  221. return;
  222. }
  223. // 获取BMP图片大小
  224. var nBmpDataSize = Module.getValue(pBmpSize, "i32");
  225. // 获取BMP图片数据
  226. var aBmpData = new Uint8Array(nBmpDataSize);
  227. aBmpData.set(Module.HEAPU8.subarray(pBmpData, pBmpData + nBmpDataSize));
  228. postMessage({'function': "GetBMP", 'data': aBmpData, 'errorCode': res}, [aBmpData.buffer]);
  229. aBmpData = null;
  230. if (pDataYUV != null) {
  231. Module._free(pDataYUV);
  232. pDataYUV = null;
  233. }
  234. if (pBmpData != null) {
  235. Module._free(pBmpData);
  236. pBmpData = null;
  237. }
  238. if (pBmpSize != null) {
  239. Module._free(pBmpSize);
  240. pBmpSize = null;
  241. }
  242. break;
  243. case "GetJPEG":
  244. var nJpegWidth = eventData.width;
  245. var nJpegHeight = eventData.height;
  246. var pYUVData1 = eventData.data;
  247. var nYUVSize1 = nJpegWidth * nJpegHeight * 3 / 2;
  248. var oJpegCropRect = eventData.rect;
  249. var pDataYUV1 = Module._malloc(nYUVSize1);
  250. if (pDataYUV1 === null) {
  251. return;
  252. }
  253. Module.writeArrayToMemory(new Uint8Array(pYUVData1, 0, nYUVSize1), pDataYUV1);
  254. // 分配JPEG空间
  255. var pJpegData = Module._malloc(nYUVSize1);
  256. var pJpegSize = Module._malloc(4);
  257. if (pJpegData === null || pJpegSize === null) {
  258. if (pJpegData != null) {
  259. Module._free(pJpegData);
  260. pJpegData = null;
  261. }
  262. if (pJpegSize != null) {
  263. Module._free(pJpegSize);
  264. pJpegSize = null;
  265. }
  266. if (pDataYUV1 != null) {
  267. Module._free(pDataYUV1);
  268. pDataYUV1 = null;
  269. }
  270. return;
  271. }
  272. Module.setValue(pJpegSize, nJpegWidth * nJpegHeight * 2, "i32"); // JPEG抓图,输入缓冲长度不小于当前帧YUV大小
  273. res = Module._GetJPEG(pDataYUV1, nYUVSize1, pJpegData, pJpegSize,
  274. oJpegCropRect.left, oJpegCropRect.top, oJpegCropRect.right, oJpegCropRect.bottom);
  275. if (res !== PLAYM4_OK) {
  276. postMessage({'function': "GetJPEG", 'errorCode': res});
  277. if (pJpegData != null) {
  278. Module._free(pJpegData);
  279. pJpegData = null;
  280. }
  281. if (pJpegSize != null) {
  282. Module._free(pJpegSize);
  283. pJpegSize = null;
  284. }
  285. if (pDataYUV1 != null) {
  286. Module._free(pDataYUV1);
  287. pDataYUV1 = null;
  288. }
  289. return;
  290. }
  291. // 获取JPEG图片大小
  292. var nJpegSize = Module.getValue(pJpegSize, "i32");
  293. // 获取JPEG图片数据
  294. var aJpegData = new Uint8Array(nJpegSize);
  295. aJpegData.set(Module.HEAPU8.subarray(pJpegData, pJpegData + nJpegSize));
  296. postMessage({'function': "GetJPEG", 'data': aJpegData, 'errorCode': res}, [aJpegData.buffer]);
  297. nJpegSize = null;
  298. aJpegData = null;
  299. if (pDataYUV1 != null) {
  300. Module._free(pDataYUV1);
  301. pDataYUV1 = null;
  302. }
  303. if (pJpegData != null) {
  304. Module._free(pJpegData);
  305. pJpegData = null;
  306. }
  307. if (pJpegSize != null) {
  308. Module._free(pJpegSize);
  309. pJpegSize = null;
  310. }
  311. break;
  312. case "SetDecodeFrameType":
  313. var nFrameType = eventData.data;
  314. res = Module._SetDecodeFrameType(nFrameType);
  315. if (res !== PLAYM4_OK) {
  316. postMessage({'function': "SetDecodeFrameType", 'errorCode': res});
  317. return;
  318. }
  319. break;
  320. case "DisplayRegion":
  321. var nRegionNum = eventData.nRegionNum;
  322. var srcRect = eventData.srcRect;
  323. var hDestWnd = eventData.hDestWnd;
  324. var bEnable = eventData.bEnable;
  325. res = Module._SetDisplayRegion(nRegionNum, srcRect, hDestWnd, bEnable);
  326. if (res !== PLAYM4_OK) {
  327. postMessage({'function': "DisplayRegion", 'errorCode': res});
  328. return;
  329. }
  330. break;
  331. case "CloseStream":
  332. res = Module._CloseStream();
  333. if (res !== PLAYM4_OK) {
  334. postMessage({'function': "CloseStream", 'errorCode': res});
  335. return;
  336. }
  337. break;
  338. case "SetIFrameDecInterval":
  339. Module._SetIFrameDecInterval(eventData.data);
  340. break;
  341. /*******************************************worker音频编码相关接口实现**********************************************************/
  342. case "CreateAudEncode":
  343. res = Module._CreateAudEncode(eventData.encodertype);
  344. postMessage({'function': "CreateAudEncode", 'errorCode': res});
  345. break;
  346. case "SetAudEncodeParam":
  347. res = Module._SetAudEncodeParam(eventData.samplerate, eventData.channel, eventData.bitrate, eventData.bitwidth);
  348. postMessage({'function': "SetAudEncodeParam", 'errorCode': res});
  349. break;
  350. case "DestroyAudEncode":
  351. res = Module._DestroyAudEncode();
  352. postMessage({'function': "DestroyAudEncode", 'errorCode': res});
  353. break;
  354. case "InputAudEncodeData":
  355. if (bWorkerPrintLog) {
  356. console.log("<<<Worker: 20200113 DecodeWorker-InputAudEncodeData 1");
  357. }
  358. var iLen = eventData.dataSize;
  359. if (iLen > 0) {
  360. var pAudInputData = Module._malloc(iLen);
  361. if (pAudInputData === null) {
  362. return;
  363. }
  364. var audinputData = new Uint8Array(eventData.data);
  365. Module.writeArrayToMemory(audinputData, pAudInputData);
  366. audinputData = null;
  367. res = Module._InputAudEncodeData(pAudInputData, iLen);
  368. if (bWorkerPrintLog) {
  369. console.log("<<<Worker: 20200113 DecodeWorker-InputAudEncodeData 2 res:" + res);
  370. }
  371. if (res == PLAYM4_OK) //接口返回成功,表明已编码好一帧音频数据
  372. {
  373. if (funGetAudFrameData === null) {
  374. funGetAudFrameData = Module.cwrap('GetAudFrameData', 'number');
  375. }
  376. if (bWorkerPrintLog) {
  377. console.log("<<<Worker: 20200113 DecodeWorker-InputAudEncodeData 2-1 succ");
  378. }
  379. //调用C++ GetAudFrameData
  380. var ret = getAudFrameData(funGetAudFrameData);
  381. }
  382. Module._free(pAudInputData);
  383. pAudInputData = null;
  384. }
  385. break;
  386. default:
  387. break;
  388. }
  389. };
  390. function getOSDTime(oFrameInfo) {
  391. var iYear = oFrameInfo.year;
  392. var iMonth = oFrameInfo.month;
  393. var iDay = oFrameInfo.day;
  394. var iHour = oFrameInfo.hour;
  395. var iMinute = oFrameInfo.minute;
  396. var iSecond = oFrameInfo.second;
  397. if (iMonth < 10) {
  398. iMonth = "0" + iMonth;
  399. }
  400. if (iDay < 10) {
  401. iDay = "0" + iDay;
  402. }
  403. if (iHour < 10) {
  404. iHour = "0" + iHour;
  405. }
  406. if (iMinute < 10) {
  407. iMinute = "0" + iMinute;
  408. }
  409. if (iSecond < 10) {
  410. iSecond = "0" + iSecond;
  411. }
  412. return iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
  413. }
  414. function getAudFrameData(fun) {
  415. if (bWorkerPrintLog) {
  416. console.log("<<<Worker: DecodeWorker-getAudFrameData 1");
  417. }
  418. var res = fun(); // 调用C++GetAudFrameData函数
  419. if (res === PLAYM4_OK) {
  420. var oFrameInfo = Module._GetAudFrameInfo();
  421. var iSize = oFrameInfo.frameSize;
  422. if (0 === iSize) {
  423. return null;
  424. }
  425. var pEncodeAud = Module._GetAudFrameBuffer();
  426. if (pEncodeAud == null) {
  427. return null;
  428. }
  429. var aEncodeAudData = new Uint8Array(iSize);
  430. aEncodeAudData.set(Module.HEAPU8.subarray(pEncodeAud, pEncodeAud + iSize));
  431. if (bWorkerPrintLog) {
  432. console.log("<<<Worker: DecodeWorker-getAudFrameData 2");
  433. }
  434. //获取到音频编码帧数据通过worker返回
  435. postMessage({
  436. 'function': "GetAudEncodeData",
  437. 'data': aEncodeAudData.buffer,
  438. 'dataSize': iSize,
  439. 'errorCode': res
  440. });
  441. } else {
  442. postMessage({'function': "GetAudEncodeData", 'data': null, 'dataSize': -1, 'errorCode': res});
  443. }
  444. oFrameInfo = null;
  445. pEncodeAud = null;
  446. aEncodeAudData = null;
  447. return res;
  448. }
  449. // 获取帧数据
  450. function getFrameData(fun) {
  451. // function getFrameData() {
  452. // 获取帧数据
  453. // var res = Module._GetFrameData();
  454. var res = fun();
  455. if (bWorkerPrintLog) {
  456. console.log("<<<Worker: getFrameData Result:" + res);
  457. }
  458. if (res === PLAYM4_OK) {
  459. var oFrameInfo = Module._GetFrameInfo();
  460. switch (oFrameInfo.frameType) {
  461. case AUDIO_TYPE:
  462. var iSize = oFrameInfo.frameSize;
  463. if (0 === iSize) {
  464. return -1;
  465. }
  466. var pPCM = Module._GetFrameBuffer();
  467. // var audioBuf = new ArrayBuffer(iSize);
  468. var aPCMData = new Uint8Array(iSize);
  469. aPCMData.set(Module.HEAPU8.subarray(pPCM, pPCM + iSize));
  470. if (bWorkerPrintLog) {
  471. console.log("<<<Worker: audio media Info: nSise:" + oFrameInfo.frameSize + ",nSampleRate:" + oFrameInfo.samplesPerSec + ',channel:' + oFrameInfo.channels + ',bitsPerSample:' + oFrameInfo.bitsPerSample);
  472. }
  473. postMessage({
  474. 'function': "GetFrameData", 'type': "audioType", 'data': aPCMData.buffer,
  475. 'frameInfo': oFrameInfo, 'errorCode': res
  476. }, [aPCMData.buffer]);
  477. oFrameInfo = null;
  478. pPCM = null;
  479. aPCMData = null;
  480. return PLAYM4_AUDIO_FRAME;
  481. case VIDEO_TYPE:
  482. var szOSDTime = getOSDTime(oFrameInfo);
  483. var iWidth = oFrameInfo.width;
  484. var iHeight = oFrameInfo.height;
  485. var iYUVSize = iWidth * iHeight * 3 / 2;
  486. if (0 === iYUVSize) {
  487. return -1;
  488. }
  489. var pYUV = Module._GetFrameBuffer();
  490. // 图像数据渲染后压回,若从主码流切到子码流,存在数组大小与图像大小不匹配现象
  491. var aYUVData = new Uint8Array(iYUVSize);
  492. aYUVData.set(Module.HEAPU8.subarray(pYUV, pYUV + iYUVSize));
  493. if (bWorkerPrintLog) {
  494. console.log("<<<Worker: InputData-getFrameData Video: Width:" + oFrameInfo.width + ",Height:" + oFrameInfo.height + ",timeStamp:" + oFrameInfo.timeStamp);
  495. }
  496. postMessage({
  497. 'function': "GetFrameData", 'type': "videoType", 'data': aYUVData.buffer,
  498. 'dataLen': aYUVData.length, 'osd': szOSDTime, 'frameInfo': oFrameInfo, 'errorCode': res
  499. }, [aYUVData.buffer]);
  500. oFrameInfo = null;
  501. pYUV = null;
  502. aYUVData = null;
  503. return PLAYM4_VIDEO_FRAME;
  504. case PRIVT_TYPE:
  505. postMessage({
  506. 'function': "GetFrameData", 'type': "", 'data': null,
  507. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
  508. });
  509. return PLAYM4_SYS_NOT_SUPPORT;
  510. default:
  511. postMessage({
  512. 'function': "GetFrameData", 'type': "", 'data': null,
  513. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
  514. });
  515. return PLAYM4_SYS_NOT_SUPPORT;
  516. }
  517. } else {
  518. //解码失败返回裸数据
  519. if (PLAYM4_DECODE_ERROR === res) {
  520. var rawInfo = Module._GetRawDataInfo();
  521. var pRawData = Module._GetRawDataBuffer();
  522. var aRawData = new Uint8Array(rawInfo.isize);
  523. aRawData.set(Module.HEAPU8.subarray(pRawData, pRawData + rawInfo.isize));
  524. postMessage({
  525. 'function': "GetRawData", 'type': "", 'data': aRawData.buffer,
  526. 'rawDataLen': rawInfo.isize, 'osd': 0, 'frameInfo': null, 'errorCode': res
  527. });
  528. rawInfo = null;
  529. pRawData = null;
  530. aRawData = null;
  531. }
  532. //定位返回的错误
  533. if (PLAYM4_FIRST_FRAME_NOT_ICURRENT === res || PLAYM4_ITYPE_DECODE_ERROR === res) {
  534. postMessage({
  535. 'function': "GetFrameData", 'type': "", 'data': null,
  536. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': res
  537. });
  538. }
  539. //需要更多数据
  540. if (PLAYM4_NEED_MORE_DATA === res || PLAYM4_SYS_NOT_SUPPORT === res) {
  541. postMessage({
  542. 'function': "GetFrameData", 'type': "", 'data': null,
  543. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': res
  544. });
  545. }
  546. return res;
  547. }
  548. }
  549. // 开始计算时间
  550. function startTime() {
  551. return new Date().getTime();
  552. }
  553. // 结束计算时间
  554. function endTime() {
  555. return new Date().getTime();
  556. }
  557. // 字母字符串转byte数组
  558. function stringToBytes(str) {
  559. var ch, st, re = [];
  560. for (var i = 0; i < str.length; i++) {
  561. ch = str.charCodeAt(i); // get char
  562. st = []; // set up "stack"
  563. do {
  564. st.push(ch & 0xFF); // push byte to stack
  565. ch = ch >> 8; // shift value down by 1 byte
  566. }
  567. while (ch);
  568. // add stack contents to result
  569. // done because chars have "wrong" endianness
  570. re = re.concat(st.reverse());
  571. }
  572. // return an array of bytes
  573. return re;
  574. }
  575. })();