(function(){"use strict";class h{value;next;constructor(t){this.value=t}}class f{#e;#t;#i;constructor(){this.clear()}enqueue(t){const r=new h(t);this.#e?(this.#t.next=r,this.#t=r):(this.#e=r,this.#t=r),this.#i++}dequeue(){const t=this.#e;if(t)return this.#e=this.#e.next,this.#i--,this.#e||(this.#t=void 0),t.value}peek(){if(this.#e)return this.#e.value}clear(){this.#e=void 0,this.#t=void 0,this.#i=0}get size(){return this.#i}*[Symbol.iterator](){let t=this.#e;for(;t;)yield t.value,t=t.next}*drain(){for(;this.#e;)yield this.dequeue()}}let s=null,a=null,c=!1,i=null;const n=new f;self.onmessage=e=>{const{type:t,data:r,canvas:o}=e.data;switch(t){case"h264":s=o,a=s.getContext("2d");break;case"ws_message":g(r);break;case"error":case"close":l();break}};function g(e){try{if(e.byteLength<9)return;const t=new DataView(e),r=t.getUint8(0)===1,o=Number(t.getBigUint64(1,!0)),u=new Uint8Array(e,9);!i&&r&&y(),i?.state==="configured"&&w(r,o,u)}catch(t){console.error("Error processing WebSocket message in worker:",t)}}function y(){if(!self.VideoDecoder){console.log("Error: WebCodecs API not supported in this worker.");return}if(i&&i.state!=="unconfigured")return;const e={output:t=>{n.enqueue(t),n.size>=10&&n.dequeue()?.close(),c||(c=!0,d())},error:()=>{l()}};try{i=new VideoDecoder(e),i.configure({codec:"avc1.42E01F",optimizeForLatency:!0})}catch(t){console.log(t),i=null}}function w(e,t,r){const o=new EncodedVideoChunk({type:e?"key":"delta",timestamp:t,data:r});try{i?.decode(o)}catch(u){(u.name==="TypeError"||u.message.includes("configured"))&&l()}}function d(){const e=n.dequeue();e&&p(e),n.size>0?setTimeout(d,0):c=!1}function p(e){if(!s||!a){e.close();return}(s.width!==e.displayWidth||s.height!==e.displayHeight)&&(s.width=e.displayWidth,s.height=e.displayHeight),a.drawImage(e,0,0,s.width,s.height),e.close()}function l(){if(i&&i.state!=="closed")try{i.close()}catch(e){console.log(e)}i=null,c=!1,Array.from(n.drain()).forEach(e=>e.close())}})();
