var h=Object.defineProperty;var u=(n,a,e)=>a in n?h(n,a,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[a]=e;var l=(n,a,e)=>u(n,typeof a!="symbol"?a+"":a,e);import{m,C as f,f as p,l as w,R as C,j as y,k as S}from"./index-CBxrqwze.js";var g=Object.defineProperty,v=Object.getOwnPropertyDescriptor,b=(n,a,e,t)=>{for(var s=t>1?void 0:t?v(a,e):a,r=n.length-1,o;r>=0;r--)(o=n[r])&&(s=(t?o(a,e,s):o(s))||s);return t&&s&&g(a,e,s),s};let d=class extends m(f){constructor(){super(...arguments);l(this,"cameraVideo");l(this,"pc",null);l(this,"ws",null);l(this,"playbackAbortController",null);l(this,"sleepAbortController",null)}loadStream(){var e,t,s;try{(e=this.pc)==null||e.close(),(t=this.ws)==null||t.close();const r=(s=this.playbackAbortController)==null?void 0:s.signal;if(!r||r.aborted)return;this.updateStatus("connecting");const o=this.buildAbsoluteUrl(this.camera.stream_url||""),c=new URL("api/ws"+o.search,o);c.protocol=c.protocol==="https:"?"wss:":"ws:",this.ws=new WebSocket(c),this.ws.binaryType="arraybuffer",this.ws.onopen=this.onWebSocketOpen,this.ws.onmessage=this.onWebSocketMessage,this.ws.onclose=this.onWebSocketClose,this.updateRawCameraUrl(o.toString())}catch(r){p.error(`[WebrtcGo2RtcCamera] failed to start playback "${this.camera.name}"`,r)}}async onWebSocketOpen(){var r;p.debug("[WebrtcGo2RtcCamera] socket opened");const e={bundlePolicy:"max-bundle",iceServers:[{urls:"stun:stun.l.google.com:19302"}],sdpSemantics:"unified-plan"};this.pc=new RTCPeerConnection(e),this.pc.onicecandidate=o=>{var i;if(!o.candidate)return;const c={type:"webrtc/candidate",value:o.candidate.toJSON().candidate};(i=this.ws)==null||i.send(JSON.stringify(c))},this.pc.onconnectionstatechange=()=>{var o;switch((o=this.pc)==null?void 0:o.connectionState){case"connected":{const c=this.pc.getTransceivers().filter(i=>i.direction==="recvonly").map(i=>i.receiver.track);this.cameraVideo.srcObject=new MediaStream(c);break}case"failed":case"disconnected":this.loadStream()}},this.pc.addTransceiver("video",{direction:"recvonly"});const t=await this.pc.createOffer();await this.pc.setLocalDescription(t);const s={type:"webrtc/offer",value:t.sdp};(r=this.ws)==null||r.send(JSON.stringify(s))}async onWebSocketMessage(e){var s,r,o;const t=JSON.parse(e.data);switch(t.type){case"webrtc/candidate":try{await((s=this.pc)==null?void 0:s.addIceCandidate({candidate:t.value,sdpMid:"0"}))}catch(c){p.warn("[WebrtcGo2RtcCamera] RTCPeerConnection.addIceCandidate() error",c)}break;case"webrtc/answer":try{(r=this.pc)==null||r.setRemoteDescription({type:"answer",sdp:t.value})}catch(c){p.warn("[WebrtcGo2RtcCamera] RTCPeerConnection.setRemoteDescription() error",c)}break;case"error":p.error(`[WebrtcGo2RtcCamera] ${t.value}`),this.updateStatus("error"),(o=this.pc)==null||o.close()}}async onWebSocketClose(e){var t,s;if(!e.wasClean){this.updateStatus("error"),p.error("[WebrtcGo2RtcCamera] socket close was not clean",e);const r=(t=this.playbackAbortController)==null?void 0:t.signal;if(!r||r.aborted)return;(s=this.sleepAbortController)==null||s.abort();const o=this.sleepAbortController=new AbortController;try{const c=[r,o.signal];await w(2e3,AbortSignal.any(c)),this.loadStream()}catch{}}}startPlayback(){this.playbackAbortController=new AbortController,this.loadStream()}stopPlayback(){var e,t;(e=this.playbackAbortController)==null||e.abort(),this.playbackAbortController=null,this.updateStatus("disconnected"),this.pc&&(this.pc.getSenders().forEach(s=>{var r;(r=s.track)==null||r.stop()}),this.pc.close(),this.pc=null),(t=this.ws)==null||t.close(),this.ws=null,this.cameraVideo.src="",this.cameraVideo.srcObject=null}};b([C("streamingElement")],d.prototype,"cameraVideo",2);d=b([y({})],d);var k=function(){var a=this,e=a._self._c;return a._self._setupProxy,e("video",{ref:"streamingElement",style:a.cameraStyle,attrs:{autoplay:"",playsinline:"",muted:"",crossorigin:a.crossorigin},domProps:{muted:!0},on:{play:function(t){return a.updateStatus("connected")},error:function(t){return a.updateStatus("error")}}})},_=[],R=S(d,k,_,!1,null,null);const P=R.exports;export{P as default};
