DecodeWorker.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 = eventData.rect;
  202. var pDataYUV = Module._malloc(nYUVSize);
  203. if (pDataYUV === null) {
  204. return;
  205. }
  206. Module.writeArrayToMemory(new Uint8Array(pYUVData, 0, nYUVSize), pDataYUV);
  207. // 分配BMP空间
  208. var nBmpSize = nBMPWidth * nBMPHeight * 4 + 60;
  209. var pBmpData = Module._malloc(nBmpSize);
  210. var pBmpSize = Module._malloc(4);
  211. if (pBmpData === null || pBmpSize === null) {
  212. Module._free(pDataYUV);
  213. pDataYUV = null;
  214. if (pBmpData != null) {
  215. Module._free(pBmpData);
  216. pBmpData = null;
  217. }
  218. if (pBmpSize != null) {
  219. Module._free(pBmpSize);
  220. pBmpSize = null;
  221. }
  222. return;
  223. }
  224. //Module._memset(pBmpSize, nBmpSize, 4); // 防止bmp截图出现输入数据过大的错误码
  225. Module.setValue(pBmpSize, nBmpSize, "i32");
  226. res = Module._GetBMP(g_nPort, pDataYUV, nYUVSize, pBmpData, pBmpSize,
  227. oBMPCropRect.left, oBMPCropRect.top, oBMPCropRect.right, oBMPCropRect.bottom);
  228. if (res !== PLAYM4_OK) {
  229. postMessage({ 'function': "GetBMP", 'errorCode': res });
  230. Module._free(pDataYUV);
  231. pDataYUV = null;
  232. Module._free(pBmpData);
  233. pBmpData = null;
  234. Module._free(pBmpSize);
  235. pBmpSize = null;
  236. return;
  237. }
  238. // 获取BMP图片大小
  239. var nBmpDataSize = Module.getValue(pBmpSize, "i32");
  240. // 获取BMP图片数据
  241. var aBmpData = new Uint8Array(nBmpDataSize);
  242. aBmpData.set(Module.HEAPU8.subarray(pBmpData, pBmpData + nBmpDataSize));
  243. postMessage({ 'function': "GetBMP", 'data': aBmpData, 'errorCode': res }, [aBmpData.buffer]);
  244. aBmpData = null;
  245. if (pDataYUV != null) {
  246. Module._free(pDataYUV);
  247. pDataYUV = null;
  248. }
  249. if (pBmpData != null) {
  250. Module._free(pBmpData);
  251. pBmpData = null;
  252. }
  253. if (pBmpSize != null) {
  254. Module._free(pBmpSize);
  255. pBmpSize = null;
  256. }
  257. break;
  258. case "GetJPEG":
  259. var nJpegWidth = eventData.width;
  260. var nJpegHeight = eventData.height;
  261. var pYUVData1 = eventData.data;
  262. var nYUVSize1 = nJpegWidth * nJpegHeight * 3 / 2;
  263. var oJpegCropRect = eventData.rect;
  264. var pDataYUV1 = Module._malloc(nYUVSize1);
  265. if (pDataYUV1 === null) {
  266. return;
  267. }
  268. Module.writeArrayToMemory(new Uint8Array(pYUVData1, 0, nYUVSize1), pDataYUV1);
  269. // 分配JPEG空间
  270. var pJpegData = Module._malloc(nYUVSize1);
  271. var pJpegSize = Module._malloc(4);
  272. if (pJpegData === null || pJpegSize === null) {
  273. if (pJpegData != null) {
  274. Module._free(pJpegData);
  275. pJpegData = null;
  276. }
  277. if (pJpegSize != null) {
  278. Module._free(pJpegSize);
  279. pJpegSize = null;
  280. }
  281. if (pDataYUV1 != null) {
  282. Module._free(pDataYUV1);
  283. pDataYUV1 = null;
  284. }
  285. return;
  286. }
  287. Module.setValue(pJpegSize, nJpegWidth * nJpegHeight * 2, "i32"); // JPEG抓图,输入缓冲长度不小于当前帧YUV大小
  288. res = Module._GetJPEG(g_nPort, pDataYUV1, nYUVSize1, pJpegData, pJpegSize,
  289. oJpegCropRect.left, oJpegCropRect.top, oJpegCropRect.right, oJpegCropRect.bottom);
  290. if (res !== PLAYM4_OK) {
  291. postMessage({ 'function': "GetJPEG", 'errorCode': res });
  292. if (pJpegData != null) {
  293. Module._free(pJpegData);
  294. pJpegData = null;
  295. }
  296. if (pJpegSize != null) {
  297. Module._free(pJpegSize);
  298. pJpegSize = null;
  299. }
  300. if (pDataYUV1 != null) {
  301. Module._free(pDataYUV1);
  302. pDataYUV1 = null;
  303. }
  304. return;
  305. }
  306. // 获取JPEG图片大小
  307. var nJpegSize = Module.getValue(pJpegSize, "i32");
  308. // 获取JPEG图片数据
  309. var aJpegData = new Uint8Array(nJpegSize);
  310. aJpegData.set(Module.HEAPU8.subarray(pJpegData, pJpegData + nJpegSize));
  311. postMessage({ 'function': "GetJPEG", 'data': aJpegData, 'errorCode': res }, [aJpegData.buffer]);
  312. nJpegSize = null;
  313. aJpegData = null;
  314. if (pDataYUV1 != null) {
  315. Module._free(pDataYUV1);
  316. pDataYUV1 = null;
  317. }
  318. if (pJpegData != null) {
  319. Module._free(pJpegData);
  320. pJpegData = null;
  321. }
  322. if (pJpegSize != null) {
  323. Module._free(pJpegSize);
  324. pJpegSize = null;
  325. }
  326. break;
  327. case "SetDecodeFrameType":
  328. var nFrameType = eventData.data;
  329. res = Module._SetDecodeFrameType(g_nPort, nFrameType);
  330. if (res !== PLAYM4_OK) {
  331. postMessage({ 'function': "SetDecodeFrameType", 'errorCode': res });
  332. return;
  333. }
  334. break;
  335. case "CloseStream":
  336. //stop
  337. let resS = Module._Stop(g_nPort);
  338. if (resS !== PLAYM4_OK) {
  339. postMessage({ 'function': "Stop", 'errorCode': res });
  340. return;
  341. }
  342. //closeStream
  343. res = Module._CloseStream(g_nPort);
  344. if (res !== PLAYM4_OK) {
  345. postMessage({ 'function': "CloseStream", 'errorCode': res });
  346. return;
  347. }
  348. //freePort
  349. let resF = Module._FreePort(g_nPort);
  350. if (resF !== PLAYM4_OK) {
  351. postMessage({ 'function': "FreePort", 'errorCode': res });
  352. return;
  353. }
  354. if (pInputData != null) {
  355. Module._free(pInputData);
  356. pInputData = null;
  357. }
  358. break;
  359. case "PlaySound":
  360. let resPS = Module._PlaySound(g_nPort);
  361. if (resPS !== PLAYM4_OK) {
  362. console.log("PlaySound failed");
  363. return;
  364. }
  365. break;
  366. case "StopSound":
  367. let resSS = Module._StopSound();
  368. if (resSS !== PLAYM4_OK) {
  369. console.log("StopSound failed");
  370. return;
  371. }
  372. break;
  373. case "SetVolume":
  374. let resSV = Module._SetVolume(g_nPort, eventData.volume);
  375. if (resSV !== PLAYM4_OK) {
  376. console.log("Audio SetVolume failed");
  377. return;
  378. }
  379. break;
  380. case "GetVolume":
  381. let volume = Module._GetVolume();
  382. if (volume > 0) {
  383. postMessage({ 'function': "GetVolume", 'volume': volume });
  384. }
  385. else {
  386. console.log("Audio GetVolume failed");
  387. return;
  388. }
  389. break;
  390. case "OnlyPlaySound":
  391. let resOPS = Module._OnlyPlaySound(g_nPort);
  392. if (resOPS !== PLAYM4_OK) {
  393. console.log("OnlyPlaySound failed");
  394. return;
  395. }
  396. break;
  397. case "Pause":
  398. let resPa = Module._Pause(g_nPort, eventData.bPlay);
  399. if (resPa !== PLAYM4_OK) {
  400. console.log("Pause failed");
  401. return;
  402. }
  403. case "PlayRate":
  404. Module._SetPlayRate(g_nPort, eventData.playRate);
  405. break;
  406. case "SetIFrameDecInterval":
  407. Module._SetIFrameDecInterval(g_nPort, eventData.data);
  408. break;
  409. case "SetLostFrameMode":
  410. Module._SetLostFrameMode(g_nPort, eventData.data, 0);
  411. break;
  412. case "SetDemuxModel":
  413. let resSDM = Module._SetDemuxModel(g_nPort, eventData.nIdemuxType, eventData.bTrue);
  414. break;
  415. case "SkipErrorData":
  416. Module._SkipErrorData(g_nPort, eventData.bSkip);
  417. break;
  418. case "SetDecodeERC":
  419. Module._SetDecodeERC(g_nPort, eventData.nLevel);
  420. break;
  421. case "SetANRParam":
  422. Module._SetANRParam(g_nPort, eventData.nEnable, eventData.nANRLevel);
  423. break;
  424. case "SetResampleValue":
  425. Module._SetResampleValue(g_nPort, eventData.nEnable, eventData.resampleValue);
  426. break;
  427. case "GetLastError":
  428. let errorCode = Module._GetLastError(g_nPort);
  429. postMessage({ 'function': "GetLastError", 'errorCode': errorCode });
  430. break;
  431. case "SetGlobalBaseTime":
  432. Module._SetGlobalBaseTime(g_nPort, eventData.year, eventData.month, eventData.day, eventData.hour, eventData.min, eventData.sec, eventData.ms);
  433. break;
  434. case "SetRunTimeInfoCB":
  435. Module._SetRunTimeInfoCallBackEx(g_nPort, eventData.nModuleType, 0);
  436. break;
  437. case "SetStreamInfoCB":
  438. Module._SetStreamInfoCallBack(g_nPort, eventData.nType, 0);
  439. break;
  440. default:
  441. break;
  442. }
  443. };
  444. function getOSDTime(oFrameInfo) {
  445. var iYear = oFrameInfo.year;
  446. var iMonth = oFrameInfo.month;
  447. var iDay = oFrameInfo.day;
  448. var iHour = oFrameInfo.hour;
  449. var iMinute = oFrameInfo.minute;
  450. var iSecond = oFrameInfo.second;
  451. var iMiSecond = oFrameInfo.misecond
  452. if (iMonth < 10) {
  453. iMonth = "0" + iMonth;
  454. }
  455. if (iDay < 10) {
  456. iDay = "0" + iDay;
  457. }
  458. if (iHour < 10) {
  459. iHour = "0" + iHour;
  460. }
  461. if (iMinute < 10) {
  462. iMinute = "0" + iMinute;
  463. }
  464. if (iSecond < 10) {
  465. iSecond = "0" + iSecond;
  466. }
  467. let osdTime = {};
  468. osdTime.year = iYear;
  469. osdTime.month = iMonth;
  470. osdTime.week = 0;
  471. osdTime.day = iDay;
  472. osdTime.hour = iHour;
  473. osdTime.minute = iMinute;
  474. osdTime.second = iSecond;
  475. osdTime.milliseconds = iMiSecond;
  476. return osdTime;
  477. //return iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
  478. }
  479. // 获取帧数据
  480. function getFrameData() {
  481. // function getFrameData() {
  482. // 获取帧数据
  483. var res = Module._GetFrameData();
  484. //var res = fun();
  485. if (res === PLAYM4_OK) {
  486. var iFrameInfo = Module._GetFrameInfo();
  487. let oFrameInfo = {};
  488. oFrameInfo.frameType = Module.HEAP32[iFrameInfo >> 2];
  489. oFrameInfo.frameSize = Module.HEAP32[iFrameInfo + 4 >> 2];
  490. oFrameInfo.width = Module.HEAP32[iFrameInfo + 8 >> 2];
  491. oFrameInfo.height = Module.HEAP32[iFrameInfo + 12 >> 2];
  492. oFrameInfo.timeStamp = Module.HEAP32[iFrameInfo + 16 >> 2];
  493. oFrameInfo.frameRate = Module.HEAP32[iFrameInfo + 20 >> 2];
  494. oFrameInfo.bitsPerSample = Module.HEAP32[iFrameInfo + 24 >> 2];
  495. oFrameInfo.samplesPerSec = Module.HEAP32[iFrameInfo + 28 >> 2];
  496. oFrameInfo.channels = Module.HEAP32[iFrameInfo + 32 >> 2];
  497. oFrameInfo.frameNum = Module.HEAP32[iFrameInfo + 36 >> 2];
  498. oFrameInfo.cropLeft = Module.HEAP32[iFrameInfo + 40 >> 2];
  499. oFrameInfo.cropRight = Module.HEAP32[iFrameInfo + 44 >> 2];
  500. oFrameInfo.cropTop = Module.HEAP32[iFrameInfo + 48 >> 2];
  501. oFrameInfo.cropBottom = Module.HEAP32[iFrameInfo + 52 >> 2];
  502. oFrameInfo.year = Module.HEAP16[iFrameInfo + 56 >> 1];
  503. oFrameInfo.month = Module.HEAP16[iFrameInfo + 58 >> 1];
  504. oFrameInfo.day = Module.HEAP16[iFrameInfo + 60 >> 1];
  505. oFrameInfo.hour = Module.HEAP16[iFrameInfo + 62 >> 1];
  506. oFrameInfo.minute = Module.HEAP16[iFrameInfo + 64 >> 1];
  507. oFrameInfo.second = Module.HEAP16[iFrameInfo + 66 >> 1];
  508. oFrameInfo.misecond = Module.HEAP16[iFrameInfo + 68 >> 1];
  509. switch (oFrameInfo.frameType) {
  510. case AUDIO_TYPE:
  511. var iSize = oFrameInfo.frameSize;
  512. if (0 === iSize) {
  513. return -1;
  514. }
  515. var pPCM = Module._GetFrameBuffer();
  516. // var audioBuf = new ArrayBuffer(iSize);
  517. var aPCMData = new Uint8Array(iSize);
  518. aPCMData.set(Module.HEAPU8.subarray(pPCM, pPCM + iSize));
  519. if (bWorkerPrintLog) {
  520. console.log("<<<Worker: audio media Info: nSise:" + oFrameInfo.frameSize + ",nSampleRate:" + oFrameInfo.samplesPerSec + ',channel:' + oFrameInfo.channels + ',bitsPerSample:' + oFrameInfo.bitsPerSample);
  521. }
  522. postMessage({
  523. 'function': "GetFrameData", 'type': "audioType", 'data': aPCMData.buffer,
  524. 'frameInfo': oFrameInfo, 'errorCode': res
  525. }, [aPCMData.buffer]);
  526. oFrameInfo = null;
  527. pPCM = null;
  528. aPCMData = null;
  529. return PLAYM4_AUDIO_FRAME;
  530. case VIDEO_TYPE:
  531. var szOSDTime = getOSDTime(oFrameInfo);
  532. var iWidth = oFrameInfo.width;
  533. var iHeight = oFrameInfo.height;
  534. var iYUVSize = iWidth * iHeight * 3 / 2;
  535. if (0 === iYUVSize) {
  536. return -1;
  537. }
  538. var pYUV = Module._GetFrameBuffer();
  539. // 图像数据渲染后压回,若从主码流切到子码流,存在数组大小与图像大小不匹配现象
  540. var aYUVData = new Uint8Array(iYUVSize);
  541. aYUVData.set(Module.HEAPU8.subarray(pYUV, pYUV + iYUVSize));
  542. if (bWorkerPrintLog) {
  543. console.log("<<<Worker: video media Info: Width:" + oFrameInfo.width + ",Height:" + oFrameInfo.height + ",timeStamp:" + oFrameInfo.timeStamp);
  544. }
  545. postMessage({
  546. 'function': "GetFrameData", 'type': "videoType", 'data': aYUVData.buffer,
  547. 'dataLen': aYUVData.length, 'osd': szOSDTime, 'frameInfo': oFrameInfo, 'errorCode': res
  548. }, [aYUVData.buffer]);
  549. oFrameInfo = null;
  550. pYUV = null;
  551. aYUVData = null;
  552. return PLAYM4_VIDEO_FRAME;
  553. case PRIVT_TYPE:
  554. postMessage({
  555. 'function': "GetFrameData", 'type': "", 'data': null,
  556. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
  557. });
  558. return PLAYM4_SYS_NOT_SUPPORT;
  559. default:
  560. postMessage({
  561. 'function': "GetFrameData", 'type': "", 'data': null,
  562. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
  563. });
  564. return PLAYM4_SYS_NOT_SUPPORT;
  565. }
  566. }
  567. else {
  568. let errorCode = Module._GetLastError(g_nPort);
  569. //解码失败返回裸数据
  570. if (PLAYM4_DECODE_ERROR === errorCode) {
  571. var rawInfo = Module._GetRawDataInfo();
  572. var pRawData = Module._GetRawDataBuffer();
  573. var aRawData = new Uint8Array(rawInfo.isize);
  574. aRawData.set(Module.HEAPU8.subarray(pRawData, pRawData + rawInfo.isize));
  575. postMessage({
  576. 'function': "GetRawData", 'type': "", 'data': aRawData.buffer,
  577. 'rawDataLen': rawInfo.isize, 'osd': 0, 'frameInfo': null, 'errorCode': errorCode
  578. });
  579. rawInfo = null;
  580. pRawData = null;
  581. aRawData = null;
  582. }
  583. //需要更多数据
  584. if (PLAYM4_NEED_MORE_DATA === errorCode || PLAYM4_SYS_NOT_SUPPORT === errorCode || PLAYM4_NEED_NEET_LOOP === errorCode) {
  585. postMessage({
  586. 'function': "GetFrameData", 'type': "", 'data': null,
  587. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': errorCode
  588. });
  589. }
  590. return errorCode;
  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. })();