DecodeWorker.js 29 KB

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