DecodeWorker.js 29 KB

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