1 line
3.5 KiB
JavaScript
1 line
3.5 KiB
JavaScript
"use strict";function Decoder(){this.logger=new Logger("Decoder"),this.coreLogLevel=0,this.accurateSeek=!0,this.wasmLoaded=!1,this.tmpReqQue=[],this.cacheBuffer=null,this.decodeTimer=null,this.videoCallback=null,this.audioCallback=null,this.requestCallback=null,this.coreCach=20<<20}function onWasmLoaded(){self.decoder&&self.decoder.onWasmLoaded()}self.Module={onRuntimeInitialized:function(){onWasmLoaded()}},self.importScripts("common.js"),self.importScripts("libffmpeg.js"),Decoder.prototype.initDecoder=function(e,o,t,s){e=Module._initDecoder(e,this.coreLogLevel,s),0==e&&(this.cacheBuffer=Module._malloc(o)),s={t:kInitDecoderRsp,e:e,num:t};self.postMessage(s)},Decoder.prototype.uninitDecoder=function(){Module._uninitDecoder();null!=this.cacheBuffer&&(Module._free(this.cacheBuffer),this.cacheBuffer=null)},Decoder.prototype.openDecoder=function(){var e,o,t,s,d,c,r,i=Module._malloc(28),a=Module._openDecoder(i,7,this.videoCallback,this.audioCallback,this.requestCallback);r=0==a?(c=i>>2,r=(c=Module.HEAP32.subarray(c,7+c))[0]+36e5,e=c[1],o=c[2],t=c[3],s=c[4],d=c[5],c=c[6],{t:kOpenDecoderRsp,e:a,v:{d:r,p:e,w:o,h:t},a:{f:s,c:d,r:c}}):{t:kOpenDecoderRsp,e:a},self.postMessage(r),Module._free(i)},Decoder.prototype.closeDecoder=function(e){this.decodeTimer&&(clearInterval(this.decodeTimer),this.decodeTimer=null);Module._closeDecoder();e={t:kCloseDecoderRsp,e:0,num:e};self.postMessage(e)},Decoder.prototype.startDecoding=function(e){this.decodeTimer&&clearInterval(this.decodeTimer),this.decodeTimer=setInterval(this.decode,e)},Decoder.prototype.pauseDecoding=function(){this.decodeTimer&&(clearInterval(this.decodeTimer),this.decodeTimer=null)},Decoder.prototype.decode=function(){var e,o=Module._decodeOnePacket();for(7==o&&(self.decoder.logger.logInfo("Decoder finished."),self.decoder.pauseDecoding(),e={t:kDecodeFinishedEvt},self.postMessage(e));9==o;)o=Module._decodeOnePacket()},Decoder.prototype.sendData=function(e){e=new Uint8Array(e);Module.HEAPU8.set(e,this.cacheBuffer),Module._sendData(this.cacheBuffer,e.length)},Decoder.prototype.seekTo=function(e){var o=this.accurateSeek?1:0,e=Module._seekTo(e,o),o={t:kSeekToRsp,r:e};self.postMessage(o)},Decoder.prototype.processReq=function(e){switch(e.t){case kInitDecoderReq:this.initDecoder(e.s,e.c,e.num,e.h264decoderTag);break;case kUninitDecoderReq:this.uninitDecoder();break;case kOpenDecoderReq:this.openDecoder();break;case kCloseDecoderReq:this.closeDecoder(e.num);break;case kStartDecodingReq:this.startDecoding(e.i);break;case kPauseDecodingReq:this.pauseDecoding();break;case kFeedDataReq:this.sendData(e.d);break;case kSeekToReq:this.seekTo(e.ms);break;default:this.logger.logError("Unsupport messsage "+e.t)}},Decoder.prototype.cacheReq=function(e){e&&this.tmpReqQue.push(e)},Decoder.prototype.onWasmLoaded=function(){for(this.wasmLoaded=!0,this.videoCallback=Module.addFunction(function(e,o,t,s,d){e=Module.HEAPU8.subarray(e,e+o),o=new Uint8Array(e),e={t:kVideoFrame,s:t,d:o,w:s,h:d};self.postMessage(e,[e.d.buffer])}),this.audioCallback=Module.addFunction(function(e,o,t){e=Module.HEAPU8.subarray(e,e+o),o=new Uint8Array(e),e={t:kAudioFrame,s:t,d:o};self.postMessage(e,[e.d.buffer])}),this.requestCallback=Module.addFunction(function(e){e={t:kRequestDataEvt,o:e};self.postMessage(e)});0<this.tmpReqQue.length;){var e=this.tmpReqQue.shift();this.processReq(e)}},self.decoder=new Decoder,self.onmessage=function(e){if(self.decoder)return e=e.data,self.decoder.wasmLoaded?void self.decoder.processReq(e):(self.decoder.cacheReq(e),void self.decoder.logger.logInfo("Temp cache req "+e.t+"."))}; |