systemTransform-worker.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. importScripts('libSystemTransform.js');
  2. const RECORDRTP = 0; //录制一份未经过转封装的码流原始数据,用于定位问题
  3. let dataType = 1;
  4. // 转封装库回调函数
  5. self.STCallBack = function (fileIndex,indexLen, data, dataLen)
  6. {
  7. //stFrameInfo的类型见DETAIL_FRAME_INFO
  8. let stFrameInfo = Module._GetDetialFrameInfo();
  9. let nIsMp4Index = stFrameInfo.nIsMp4Index;
  10. //console.log("FrameType is " , stFrameInfo);
  11. //console.log("nIsMp4Index is " + nIsMp4Index);
  12. //debugger
  13. var pData = null;
  14. pData = new Uint8Array(dataLen);
  15. pData.set(Module.HEAPU8.subarray(data, data + dataLen));
  16. if (dataType === 1) {
  17. if (pData[0] == 0x49 && pData[1] == 0x4d && pData[2] == 0x4b && pData[3] == 0x48) {//码流头丢掉
  18. return;
  19. }
  20. postMessage({type: "outputData", buf: pData, dType: 1});
  21. dataType = 2;
  22. } else {
  23. if (nIsMp4Index) {
  24. postMessage({type: "outputData", buf: pData, dType: 6}); //6:索引类型
  25. } else {
  26. postMessage({type: "outputData", buf: pData, dType: 2}); //2:码流
  27. }
  28. }
  29. //stFrameInfo的类型见DETAIL_FRAME_INFO
  30. //let stFrameInfo = Module._GetDetialFrameInfo();
  31. //let stFrameType = stFrameInfo.nFrameType;
  32. //let nFrameNum = stFrameInfo.nFrameNum;
  33. //let nTimeStamp = stFrameInfo.nTimeStamp;
  34. //let nIsMp4Index = stFrameInfo.nIsMp4Index;
  35. //console.log("FrameType is " + stFrameType);
  36. //console.log("nIsMp4Index is " + nIsMp4Index);
  37. }
  38. // self.Module = { memoryInitializerRequest: loadMemInitFile(), TOTAL_MEMORY: 128*1024*1024 };
  39. // importScripts('SystemTransform.js');
  40. self.Module['onRuntimeInitialized'] = function (){
  41. postMessage({type: "loaded"});
  42. }
  43. onmessage = function (e) {
  44. var data = e.data;
  45. if ("create" === data.type) {
  46. if (RECORDRTP) {
  47. postMessage({type: "created"});
  48. postMessage({type: "outputData", buf: data.buf, dType: 1});
  49. } else {
  50. var iHeadLen = data.len;
  51. var pHead = Module._malloc(iHeadLen);
  52. self.writeArrayToMemory(new Uint8Array(data.buf), pHead);
  53. var iTransType = data.packType;//目标格式
  54. var iRet = Module._CreatHandle(pHead, iTransType, 4096);
  55. if (iRet != 0) {
  56. console.log("_CreatHandle failed!" + iRet);
  57. } else {
  58. iRet = Module._SysTransRegisterDataCallBack();
  59. if(iRet != 0)
  60. {
  61. console.log("_SysTransRegisterDataCallBack Failed:" + iRet);
  62. }
  63. iRet = Module._SysTransStart(null, null);
  64. if(iRet != 0)
  65. {
  66. console.log("_SysTransStart Failed:" + iRet);
  67. }
  68. postMessage({type: "created"});
  69. }
  70. }
  71. } else if ("inputData" === data.type) {
  72. if (RECORDRTP) {
  73. var aFileData = new Uint8Array(data.buf); // 拷贝一份
  74. var iBufferLen = aFileData.length;
  75. var szBufferLen = iBufferLen.toString(16);
  76. if (szBufferLen.length === 1) {
  77. szBufferLen = "000" + szBufferLen;
  78. } else if (szBufferLen.length === 2) {
  79. szBufferLen = "00" + szBufferLen;
  80. } else if (szBufferLen.length === 3) {
  81. szBufferLen = "0" + szBufferLen;
  82. }
  83. var aData = [0, 0, parseInt(szBufferLen.substring(0, 2), 16), parseInt(szBufferLen.substring(2, 4), 16)];
  84. for(var iIndex = 0, iDataLength = aFileData.length; iIndex < iDataLength; iIndex++) {
  85. aData[iIndex + 4] = aFileData[iIndex]
  86. }
  87. var dataUint8 = new Uint8Array(aData);
  88. postMessage({type: "outputData", buf: dataUint8.buffer, dType: 2});
  89. } else {
  90. let pInputDataBuf = Module._malloc(data.len);
  91. var idataLen = data.len;
  92. self.writeArrayToMemory(new Uint8Array(data.buf), pInputDataBuf);
  93. // 输入数据,每次最多2m
  94. let pp = Module._SysTransInputData(0, pInputDataBuf, idataLen);
  95. if(pp != 0) {
  96. //console.log("InputData Failed:" + pp);
  97. }
  98. Module._free(pInputDataBuf);
  99. }
  100. } else if ("release" === data.type) {
  101. var iRet = Module._SysTransStop();
  102. if (iRet != 0) {
  103. console.log("_SysTransStop Failed:", iRet);
  104. }
  105. Module._SysTransRelease();
  106. if (iRet != 0) {
  107. console.log("_SysTransRelease Failed:", iRet);
  108. }
  109. close();
  110. }
  111. };