/** * Created by wangweijie5 on 2016/12/5. */ (function (event) { const AUDIO_TYPE = 0; // 音频 const VIDEO_TYPE = 1; // 视频 const PRIVT_TYPE = 2; // 私有帧 const PLAYM4_AUDIO_FRAME = 100; // 音频帧 const PLAYM4_VIDEO_FRAME = 101; // 视频帧 const PLAYM4_OK = 1; const PLAYM4_DECODE_ERROR = 44 // 解码失败 const PLAYM4_NOT_KEYFRAME = 48; // 非关键帧 const PLAYM4_NEED_MORE_DATA = 31; // 需要更多数据才能解析 const PLAYM4_SYS_NOT_SUPPORT = 16; // 不支持 const PLAYM4_PARA_ENCODER_ERROR = 71; //音频编码参数错误 const PLAYM4_PRECONDITION_ENCODER_ERROR = 72; //不满足音频编码条件错误 const PLAYM4_ENCODER_ERROR = 73; //音频编码失败 const PLAYM4_CREATE_ENCODER_ERROR = 74; //创建音频编码器失败 const PLAYM4_NOSUPPORT_ENCODER_ERROR = 75; //音频编码不支持 const PLAYM4_ALLOC_MEMORY_ENCODER_ERROR = 76; //音频编码相关内存申请失败 const PLAYM4_BUF_OVER_ENCODER_ERROR = 77; //音频编码相关buffer满 const PLAYM4_NEED_MORE_DATA_ENCODER_ERROR = 78; //音频编码需要更多数据进行编码 const PLAYM4_CALL_ORDER_ENCODER_ERROR = 79; //音频编码调用顺序错误 const PLAYM4_ITYPE_DECODE_ERROR =100; //定位后送进来的第一帧I帧解码失败 const PLAYM4_FIRST_FRAME_NOT_ICURRENT =101; //定位后送进来的第一帧不是定位帧所在的I帧(Ni>Mp) importScripts('Decoder.js'); Module.addOnPostRun(function () { postMessage({'function': "loaded"}); }); var iStreamMode = 0; // 流模式 var bOpenMode = false; var bOpenStream = false; var funGetFrameData = null; var funGetAudFrameData = null; var bWorkerPrintLog=false;//worker层log开关 onmessage = function (event) { var eventData = event.data; var res = 0; switch (eventData.command) { case "printLog": let downloadFlag=eventData.data; if(downloadFlag===true) { bWorkerPrintLog=true; res = Module._SetPrintLogFlag(downloadFlag); } else { bWorkerPrintLog=false; res = Module._SetPrintLogFlag(downloadFlag); } if (res !== PLAYM4_OK) { console.log("DecodeWorker.js: PlayerSDK print log failed,res"+res); postMessage({'function': "printLog", 'errorCode': res}); } break; case "SetPlayPosition": let nFrameNumOrTime=eventData.data; let enPosType=eventData.type; res = Module._SetPlayPosition(nFrameNumOrTime,enPosType); if (res !== PLAYM4_OK) { postMessage({'function': "SetPlayPosition", 'errorCode': res}); return; } //有没有buffer需要清除 break; case "SetStreamOpenMode": iStreamMode = eventData.data; res = Module._SetStreamOpenMode(iStreamMode); if (res !== PLAYM4_OK) { postMessage({'function': "SetStreamOpenMode", 'errorCode': res}); return; } bOpenMode = true; break; case "OpenStream": // 接收到的数据 var iHeadLen = eventData.dataSize; var pHead = Module._malloc(iHeadLen + 4); if (pHead === null) { return; } var aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen); aHead.set(eventData.data); res = Module._OpenStream(pHead, iHeadLen, eventData.bufPoolSize); postMessage({'function': "OpenStream", 'errorCode': res}); if (res !== PLAYM4_OK) { //释放内存 Module._free(pHead); pHead = null; return; } bOpenStream = true; // 加4字节长度信息 var a32 = new Uint32Array([iHeadLen]); var a8 = new Uint8Array(a32.buffer); var tempBuf = new Uint8Array(iHeadLen + 4); tempBuf.set(a8, 0); tempBuf.set(eventData.data, 4); a32 = null; a8 = null; aHead = Module.HEAPU8.subarray(pHead, pHead + iHeadLen + 4); aHead.set(tempBuf); tempBuf = null; res = Module._InputData(pHead, iHeadLen + 4); if (res !== PLAYM4_OK) { postMessage({'function': "InputData", 'errorCode': res}); Module._free(pHead); pHead = null; return; } // 释放内存 Module._free(pHead); pHead = null; if (funGetFrameData === null) { funGetFrameData = Module.cwrap('GetFrameData', 'number'); } if (iStreamMode === 0) { // Module._GetFrameData(); funGetFrameData(); } break; case "InputData": // 接收到的数据 var iLen = eventData.dataSize; if(bWorkerPrintLog) { console.log("<< 0) { var pInputData = Module._malloc(iLen); if (pInputData === null) { return; } var inputData = new Uint8Array(eventData.data); // var aInputData = Module.HEAPU8.subarray(pInputData, pInputData + iLen); // aInputData.set(inputData); Module.writeArrayToMemory(inputData, pInputData); inputData = null; res = Module._InputData(pInputData, iLen); //console.log("DecodeWorker-InputData-ret:%d", res); if(bWorkerPrintLog) { console.log("<< 0) { var pAudInputData = Module._malloc(iLen); if (pAudInputData === null) { return; } var audinputData = new Uint8Array(eventData.data); Module.writeArrayToMemory(audinputData, pAudInputData); audinputData = null; res = Module._InputAudEncodeData(pAudInputData, iLen); if(bWorkerPrintLog) { console.log("<<> 8; // shift value down by 1 byte } while ( ch ); // add stack contents to result // done because chars have "wrong" endianness re = re.concat( st.reverse() ); } // return an array of bytes return re; } })();