DecodeWorker.js 25 KB

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