DecodeWorker.js 26 KB

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