var XpraVideoDecoderLoader={hasNativeDecoder(){return"undefined"!=typeof VideoDecoder}};class XpraVideoDecoder{constructor(){this.initialized=!1,this.had_first_key=!1,this.draining=!1,this.decoder_queue=[],this.decoded_frames=[],this.erroneous_frame=null,this.codec=null,this.vp9_params=null,this.frameWaitTimeout=1,this.frame_threshold=250}prepareVP9params(csc){"YUV444P"===csc&&".01.10.08"!==this.vp9_params?(this.vp9_params=".01.10.08",this._close()):"YUV444P10"===csc&&".03.10.10"!==this.vp9_params?(this.vp9_params=".03.10.10",this._close()):".00.20.08.01.02.02"!==this.vp9_params&&(this.vp9_params=".00.20.08.01.02.02",this._close())}init(coding){this.draining=!1,this.codec=this.resolveCodec(coding),this.videoDecoder=new VideoDecoder({output:this._on_decoded_frame.bind(this),error:this._on_decoder_error.bind(this)}),this.videoDecoder.configure({codec:this.codec,hardwareAcceleration:"no-preference",optimizeForLatency:!0,colorSpace:new VideoColorSpace({fullRange:!0})}),this.last_timestamp=0,this.initialized=!0}resolveCodec(coding){if("h264"===coding)return"avc1.42C01E";if("vp8"===coding)return"vp8";if("vp9"===coding)return"vp09"+this.vp9_params;throw"No codec defined for coding "+coding}_on_decoded_frame(videoFrame){var frame_timestamp,current_frame,packet;0===this.decoder_queue.length||(frame_timestamp=videoFrame.timestamp,1!==(current_frame=this.decoder_queue.filter(q=>q.p[10].frame===frame_timestamp)).length)?videoFrame.close():(this.decoder_queue=this.decoder_queue.filter(q=>q.p[10].frame!==frame_timestamp),current_frame=current_frame[0],0===frame_timestamp&&(this.last_timestamp=0),this.decoder_queue.length>this.frame_threshold||this.last_timestamp>frame_timestamp?(videoFrame.close(),(packet=current_frame.p)[6]="throttle",packet[7]=null,this.decoded_frames.push(packet)):(this.last_timestamp=frame_timestamp,packet=current_frame.p,this.draining?videoFrame.close():(packet[6]="frame:"+packet[6],packet[7]=videoFrame,this.decoded_frames.push(packet))))}_on_decoder_error(error){this.erroneous_frame="Error decoding frame: "+error,console.error(this.erroneous_frame)}queue_frame(packet){return new Promise(async(resolve,reject)=>{var options=packet[10]||{},data=packet[7],packet_sequence=packet[8];if(!this.codec.startsWith("avc1")||this.had_first_key||options.type&&"IDR"==options.type)if("closed"===this.videoDecoder.state)reject(new Error("video decoder is closed"));else if(this.draining)reject(new Error("video decoder is draining"));else{this.had_first_key=!0,this.decoder_queue.push({p:packet});data={type:"IDR"===options.type?"key":"delta",data:data,timestamp:options.frame},data=new EncodedVideoChunk(data);try{this.videoDecoder.decode(data)}catch(error){reject(new Error("failed to decode chunk: "+error))}for(var frame_out=this.decoded_frames.filter(p=>p[8]===packet_sequence);0===frame_out.length&&(await new Promise(r=>setTimeout(r,this.frameWaitTimeout)),null==this.erroneous_frame);)frame_out=this.decoded_frames.filter(p=>p[8]===packet_sequence);null!=this.erroneous_frame&&(data=this.erroneous_frame,this.erroneous_frame=null,reject(new Error(data))),this.decoded_frames=this.decoded_frames.filter(p=>p[8]!==packet_sequence),resolve(frame_out[0])}else reject(new Error(`first h264 frame must be a key frame but packet ${packet_sequence} is not: `+options))})}_close(){this.initialized&&("closed"!==this.videoDecoder.state&&this.videoDecoder.close(),this.had_first_key=!1,this.draining=!0,this.decoder_queue=[]),this.initialized=!1}}