DecodeWorker.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 HK_TRUE = 1; // true
  11. const PLAYM4_NOT_KEYFRAME = 48; // 非关键帧
  12. const PLAYM4_NEED_MORE_DATA = 31; // 需要更多数据才能解析
  13. const PLAYM4_SYS_NOT_SUPPORT = 16; // 不支持
  14. importScripts('Decoder.js');
  15. Module.postRun.push(function () {
  16. postMessage({'function': "loaded"});
  17. });
  18. var iStreamMode = 0; // 流模式
  19. var bOpenMode = false;
  20. var bOpenStream = false;
  21. var funGetFrameData = null;
  22. onmessage = function (event)
  23. {
  24. var eventData = event.data;
  25. var res = 0;
  26. switch (eventData.command)
  27. {
  28. case "SetStreamOpenMode":
  29. iStreamMode = eventData.data;
  30. res = Module._SetStreamOpenMode(iStreamMode);
  31. if (res !== HK_TRUE)
  32. {
  33. postMessage({'function': "SetStreamOpenMode", 'errorCode': res});
  34. return;
  35. }
  36. bOpenMode = true;
  37. break;
  38. case "OpenStream":
  39. // 接收到的数据
  40. var iHeadLen = eventData.dataSize;
  41. var pHead = Module._malloc(iHeadLen + 4);
  42. if (pHead === null)
  43. {
  44. return;
  45. }
  46. var aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen);
  47. aHead.set(eventData.data);
  48. res = Module._OpenStream(pHead, iHeadLen, eventData.bufPoolSize);
  49. postMessage({'function': "OpenStream", 'errorCode': res});
  50. if (res !== HK_TRUE)
  51. {
  52. //释放内存
  53. Module._free(pHead);
  54. pHead = null;
  55. return;
  56. }
  57. bOpenStream = true;
  58. // 加4字节长度信息
  59. var a32 = new Uint32Array([iHeadLen]);
  60. var a8 = new Uint8Array(a32.buffer);
  61. var tempBuf = new Uint8Array(iHeadLen + 4);
  62. tempBuf.set(a8, 0);
  63. tempBuf.set(eventData.data, 4);
  64. a32 = null;
  65. a8 = null;
  66. aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen + 4);
  67. aHead.set(tempBuf);
  68. tempBuf = null;
  69. res = Module._InputData(pHead, iHeadLen + 4);
  70. if (res !== HK_TRUE)
  71. {
  72. postMessage({'function': "InputData", 'errorCode': res});
  73. Module._free(pHead);
  74. pHead = null;
  75. return;
  76. }
  77. // 释放内存
  78. Module._free(pHead);
  79. pHead = null;
  80. if (funGetFrameData === null) {
  81. funGetFrameData = Module.cwrap('GetFrameData', 'number');
  82. }
  83. if (iStreamMode === 0) {
  84. // Module._GetFrameData();
  85. funGetFrameData();
  86. }
  87. break;
  88. case "InputData":
  89. // 接收到的数据
  90. var iLen = eventData.dataSize;
  91. //console.log("DecodeWorker-InputData-len:%d", iLen);
  92. if (iLen > 0)
  93. {
  94. var pInputData = Module._malloc(iLen);
  95. if (pInputData === null)
  96. {
  97. return;
  98. }
  99. var inputData = new Uint8Array(eventData.data);
  100. // var aInputData = Module.HEAPU8.subarray(pInputData, pInputData + iLen);
  101. // aInputData.set(inputData);
  102. Module.writeArrayToMemory(inputData, pInputData);
  103. inputData = null;
  104. res = Module._InputData(pInputData, iLen);
  105. //console.log("DecodeWorker-InputData-ret:%d", res);
  106. if (res !== HK_TRUE)
  107. {
  108. if (res === 98)
  109. {
  110. res = 1;
  111. }
  112. postMessage({'function': "InputData", 'errorCode': res});
  113. }
  114. Module._free(pInputData);
  115. pData = null;
  116. }
  117. /////////////////////
  118. if (funGetFrameData === null)
  119. {
  120. funGetFrameData = Module.cwrap('GetFrameData', 'number');
  121. }
  122. while (bOpenMode && bOpenStream)
  123. {
  124. var ret = getFrameData(funGetFrameData);
  125. // var ret = getFrameData();
  126. // 直到获取视频帧或数据不足为止
  127. if (PLAYM4_VIDEO_FRAME === ret || PLAYM4_NEED_MORE_DATA === ret)
  128. {
  129. break;
  130. }
  131. }
  132. break;
  133. case "SetSecretKey":
  134. var keyLen = eventData.nKeyLen;
  135. var pKeyData = Module._malloc(keyLen);
  136. if (pKeyData === null) {
  137. return;
  138. }
  139. var nKeySize = eventData.data.length
  140. var bufData = stringToBytes (eventData.data);
  141. var aKeyData = Module.HEAPU8.subarray(pKeyData, pKeyData + keyLen);
  142. aKeyData.set(new Uint8Array(bufData));
  143. res = Module._SetSecretKey(eventData.nKeyType, pKeyData, keyLen, nKeySize);
  144. if (res !== HK_TRUE) {
  145. postMessage({'function': "SetSecretKey", 'errorCode': res});
  146. Module._free(pKeyData);
  147. pKeyData = null;
  148. return;
  149. }
  150. Module._free(pKeyData);
  151. pKeyData = null;
  152. break;
  153. case "GetBMP":
  154. var nBMPWidth = eventData.width;
  155. var nBMPHeight = eventData.height;
  156. var pYUVData = eventData.data;
  157. var nYUVSize = nBMPWidth * nBMPHeight * 3 / 2;
  158. var oBMPCropRect = eventData.rect;
  159. var pDataYUV = Module._malloc(nYUVSize);
  160. if (pDataYUV === null) {
  161. return;
  162. }
  163. Module.writeArrayToMemory(new Uint8Array(pYUVData, 0, nYUVSize), pDataYUV);
  164. // 分配BMP空间
  165. var nBmpSize = nBMPWidth * nBMPHeight * 4 + 60;
  166. var pBmpData = Module._malloc(nBmpSize);
  167. var pBmpSize = Module._malloc(4);
  168. if (pBmpData === null || pBmpSize === null) {
  169. Module._free(pDataYUV);
  170. pDataYUV = null;
  171. if (pBmpData != null) {
  172. Module._free(pBmpData);
  173. pBmpData = null;
  174. }
  175. if (pBmpSize != null) {
  176. Module._free(pBmpSize);
  177. pBmpSize = null;
  178. }
  179. return;
  180. }
  181. Module._memset(pBmpSize, nBmpSize, 4); // 防止bmp截图出现输入数据过大的错误码
  182. res = Module._GetBMP(pDataYUV, nYUVSize, pBmpData, pBmpSize,
  183. oBMPCropRect.left, oBMPCropRect.top, oBMPCropRect.right, oBMPCropRect.bottom);
  184. if (res !== HK_TRUE) {
  185. postMessage({'function': "GetBMP", 'errorCode': res});
  186. Module._free(pDataYUV);
  187. pDataYUV = null;
  188. Module._free(pBmpData);
  189. pBmpData = null;
  190. Module._free(pBmpSize);
  191. pBmpSize = null;
  192. return;
  193. }
  194. // 获取BMP图片大小
  195. var nBmpDataSize = Module.getValue(pBmpSize, "i32");
  196. // 获取BMP图片数据
  197. var aBmpData = new Uint8Array(nBmpDataSize);
  198. aBmpData.set(Module.HEAPU8.subarray(pBmpData, pBmpData + nBmpDataSize));
  199. postMessage({'function': "GetBMP", 'data': aBmpData, 'errorCode': res}, [aBmpData.buffer]);
  200. if (pDataYUV != null) {
  201. Module._free(pDataYUV);
  202. pDataYUV = null;
  203. }
  204. if (pBmpData != null) {
  205. Module._free(pBmpData);
  206. pBmpData = null;
  207. }
  208. if (pBmpSize != null) {
  209. Module._free(pBmpSize);
  210. pBmpSize = null;
  211. }
  212. break;
  213. case "GetJPEG":
  214. var nJpegWidth = eventData.width;
  215. var nJpegHeight = eventData.height;
  216. var pYUVData1 = eventData.data;
  217. var nYUVSize1 = nJpegWidth * nJpegHeight * 3 / 2;
  218. var oJpegCropRect = eventData.rect;
  219. var pDataYUV1 = Module._malloc(nYUVSize1);
  220. if (pDataYUV1 === null) {
  221. return;
  222. }
  223. Module.writeArrayToMemory(new Uint8Array(pYUVData1, 0, nYUVSize1), pDataYUV1);
  224. // 分配JPEG空间
  225. var pJpegData = Module._malloc(nYUVSize1);
  226. var pJpegSize = Module._malloc(4);
  227. if (pJpegData === null || pJpegSize === null) {
  228. if (pJpegData != null) {
  229. Module._free(pJpegData);
  230. pJpegData = null;
  231. }
  232. if (pJpegSize != null) {
  233. Module._free(pJpegSize);
  234. pJpegSize = null;
  235. }
  236. if (pDataYUV1 != null) {
  237. Module._free(pDataYUV1);
  238. pDataYUV1 = null;
  239. }
  240. return;
  241. }
  242. Module.setValue(pJpegSize, nJpegWidth * nJpegHeight * 2, "i32"); // JPEG抓图,输入缓冲长度不小于当前帧YUV大小
  243. res = Module._GetJPEG(pDataYUV1, nYUVSize1, pJpegData, pJpegSize,
  244. oJpegCropRect.left, oJpegCropRect.top, oJpegCropRect.right, oJpegCropRect.bottom);
  245. if (res !== HK_TRUE) {
  246. postMessage({'function': "GetJPEG", 'errorCode': res});
  247. if (pJpegData != null) {
  248. Module._free(pJpegData);
  249. pJpegData = null;
  250. }
  251. if (pJpegSize != null) {
  252. Module._free(pJpegSize);
  253. pJpegSize = null;
  254. }
  255. if (pDataYUV1 != null) {
  256. Module._free(pDataYUV1);
  257. pDataYUV1 = null;
  258. }
  259. return;
  260. }
  261. // 获取JPEG图片大小
  262. var nJpegSize = Module.getValue(pJpegSize, "i32");
  263. // 获取JPEG图片数据
  264. var aJpegData = new Uint8Array(nJpegSize);
  265. aJpegData.set(Module.HEAPU8.subarray(pJpegData, pJpegData + nJpegSize));
  266. postMessage({'function': "GetJPEG", 'data': aJpegData, 'errorCode': res}, [aJpegData.buffer]);
  267. ajpegSizeData = null;
  268. aJpegData = null;
  269. if (pDataYUV1 != null) {
  270. Module._free(pDataYUV1);
  271. pDataYUV1 = null;
  272. }
  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. break;
  282. case "SetDecodeFrameType":
  283. var nFrameType = eventData.data;
  284. res = Module._SetDecodeFrameType(nFrameType);
  285. if (res !== HK_TRUE) {
  286. postMessage({'function': "SetDecodeFrameType", 'errorCode': res});
  287. return;
  288. }
  289. break;
  290. case "DisplayRegion":
  291. var nRegionNum = eventData.nRegionNum;
  292. var srcRect = eventData.srcRect;
  293. var hDestWnd = eventData.hDestWnd;
  294. var bEnable = eventData.bEnable;
  295. res = Module._SetDisplayRegion(nRegionNum, srcRect, hDestWnd, bEnable);
  296. if (res !== HK_TRUE) {
  297. postMessage({'function': "DisplayRegion", 'errorCode': res});
  298. return;
  299. }
  300. break;
  301. case "CloseStream":
  302. res = Module._CloseStream();
  303. if (res !== HK_TRUE) {
  304. postMessage({'function': "CloseStream", 'errorCode': res});
  305. return;
  306. }
  307. break;
  308. case "SetIFrameDecInterval":
  309. Module._SetIFrameDecInterval(eventData.data);
  310. break;
  311. default:
  312. break;
  313. }
  314. };
  315. function getOSDTime(oFrameInfo) {
  316. var iYear = oFrameInfo.year;
  317. var iMonth = oFrameInfo.month;
  318. var iDay = oFrameInfo.day;
  319. var iHour = oFrameInfo.hour;
  320. var iMinute = oFrameInfo.minute;
  321. var iSecond = oFrameInfo.second;
  322. if (iMonth < 10) {
  323. iMonth = "0" + iMonth;
  324. }
  325. if (iDay < 10) {
  326. iDay = "0" + iDay;
  327. }
  328. if (iHour < 10) {
  329. iHour = "0" + iHour;
  330. }
  331. if (iMinute < 10) {
  332. iMinute = "0" + iMinute;
  333. }
  334. if (iSecond < 10) {
  335. iSecond = "0" + iSecond;
  336. }
  337. return iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
  338. }
  339. // 获取帧数据
  340. function getFrameData(fun)
  341. {
  342. // function getFrameData() {
  343. // 获取帧数据
  344. // var res = Module._GetFrameData();
  345. var res = fun();
  346. if (res === HK_TRUE)
  347. {
  348. var oFrameInfo = Module._GetFrameInfo();
  349. //console.log("getFrameData-ok:%d %d %d %d %d %d \n", oFrameInfo.year, oFrameInfo.month, oFrameInfo.day, oFrameInfo.hour, oFrameInfo.minute, oFrameInfo.second);
  350. switch (oFrameInfo.frameType)
  351. {
  352. case AUDIO_TYPE:
  353. var iSize = oFrameInfo.frameSize;
  354. if (0 === iSize)
  355. {
  356. return -1;
  357. }
  358. var pPCM = Module._GetFrameBuffer();
  359. // var audioBuf = new ArrayBuffer(iSize);
  360. var aPCMData = new Uint8Array(iSize);
  361. aPCMData.set(Module.HEAPU8.subarray(pPCM, pPCM + iSize));
  362. postMessage({
  363. 'function': "GetFrameData", 'type': "audioType", 'data': aPCMData.buffer,
  364. 'frameInfo': oFrameInfo, 'errorCode': res
  365. }, [aPCMData.buffer]);
  366. oFrameInfo = null;
  367. pPCM = null;
  368. audioBuf = null;
  369. aPCMData = null;
  370. return PLAYM4_AUDIO_FRAME;
  371. case VIDEO_TYPE:
  372. var szOSDTime = getOSDTime(oFrameInfo);
  373. var iWidth = oFrameInfo.width;
  374. var iHeight = oFrameInfo.height;
  375. var iYUVSize = iWidth * iHeight * 3 / 2;
  376. if (0 === iYUVSize)
  377. {
  378. return -1;
  379. }
  380. var pYUV = Module._GetFrameBuffer();
  381. // 图像数据渲染后压回,若从主码流切到子码流,存在数组大小与图像大小不匹配现象
  382. var aYUVData = new Uint8Array(iYUVSize);
  383. aYUVData.set(Module.HEAPU8.subarray(pYUV, pYUV + iYUVSize));
  384. postMessage({
  385. 'function': "GetFrameData", 'type': "videoType", 'data': aYUVData.buffer,
  386. 'dataLen': aYUVData.length, 'osd': szOSDTime, 'frameInfo': oFrameInfo, 'errorCode': res
  387. }, [aYUVData.buffer]);
  388. oFrameInfo = null;
  389. pYUV = null;
  390. buf = null;
  391. aYUVData = null;
  392. return PLAYM4_VIDEO_FRAME;
  393. case PRIVT_TYPE:
  394. postMessage({
  395. 'function': "GetFrameData", 'type': "", 'data': null,
  396. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
  397. });
  398. return PLAYM4_SYS_NOT_SUPPORT;
  399. default:
  400. postMessage({
  401. 'function': "GetFrameData", 'type': "", 'data': null,
  402. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': PLAYM4_SYS_NOT_SUPPORT
  403. });
  404. return PLAYM4_SYS_NOT_SUPPORT;
  405. }
  406. } else {
  407. if (PLAYM4_NEED_MORE_DATA === res || PLAYM4_SYS_NOT_SUPPORT === res) {
  408. postMessage({
  409. 'function': "GetFrameData", 'type': "", 'data': null,
  410. 'dataLen': -1, 'osd': 0, 'frameInfo': null, 'errorCode': res
  411. });
  412. }
  413. return res;
  414. }
  415. }
  416. // 开始计算时间
  417. function startTime() {
  418. return new Date().getTime();
  419. }
  420. // 结束计算时间
  421. function endTime() {
  422. return new Date().getTime();
  423. }
  424. // 字母字符串转byte数组
  425. function stringToBytes ( str ) {
  426. var ch, st, re = [];
  427. for (var i = 0; i < str.length; i++ ) {
  428. ch = str.charCodeAt(i); // get char
  429. st = []; // set up "stack"
  430. do {
  431. st.push( ch & 0xFF ); // push byte to stack
  432. ch = ch >> 8; // shift value down by 1 byte
  433. }
  434. while ( ch );
  435. // add stack contents to result
  436. // done because chars have "wrong" endianness
  437. re = re.concat( st.reverse() );
  438. }
  439. // return an array of bytes
  440. return re;
  441. }
  442. })();