import{l as e,o as t}from"./vue.runtime.esm-uXCn7sOw.js";import{c as n,n as r,o as i,t as a}from"./_plugin-vue2_normalizer-BwvlkTga.js";import{t as o}from"./browser-1D2tZwAR.js";import{n as s}from"./Watch-BsFMHUzZ.js";import{t as c}from"./camera-C1rIv-Ce.js";import{t as l}from"./sleep-CEQq2wgp.js";t();var u=class extends n(c){cameraVideo;pc=null;ws=null;playbackAbortController=null;sleepAbortController=null;loadStream(){try{this.pc?.close(),this.ws?.close();let t=this.playbackAbortController?.signal;if(!t||t.aborted)return;this.updateStatus(`connecting`);let n=this.buildAbsoluteUrl(this.camera.stream_url||``),r=new URL(`api/ws`+n.search,n);r.protocol=r.protocol===`https:`?`wss:`:`ws:`,this.ws=e(new WebSocket(r)),this.ws.binaryType=`arraybuffer`,this.ws.onopen=this.onWebSocketOpen,this.ws.onmessage=this.onWebSocketMessage,this.ws.onclose=this.onWebSocketClose,this.updateRawCameraUrl(n.toString())}catch(e){o.error(`[WebrtcGo2RtcCamera] failed to start playback "${this.camera.name}"`,e)}}async onWebSocketOpen(){o.debug(`[WebrtcGo2RtcCamera] socket opened`);let t={bundlePolicy:`max-bundle`,iceServers:[{urls:`stun:stun.l.google.com:19302`}],sdpSemantics:`unified-plan`};this.pc=e(new RTCPeerConnection(t)),this.pc.onicecandidate=e=>{if(!e.candidate)return;let t={type:`webrtc/candidate`,value:e.candidate.toJSON().candidate};this.ws?.send(JSON.stringify(t))},this.pc.onconnectionstatechange=()=>{switch(this.pc?.connectionState){case`connected`:{let e=this.pc.getTransceivers().filter(e=>e.direction===`recvonly`).map(e=>e.receiver.track);this.cameraVideo.srcObject=new MediaStream(e);break}case`failed`:case`disconnected`:this.loadStream()}},this.pc.addTransceiver(`video`,{direction:`recvonly`});let n=await this.pc.createOffer();await this.pc.setLocalDescription(n);let r={type:`webrtc/offer`,value:n.sdp};this.ws?.send(JSON.stringify(r))}async onWebSocketMessage(e){let t=JSON.parse(e.data);switch(t.type){case`webrtc/candidate`:try{await this.pc?.addIceCandidate({candidate:t.value,sdpMid:`0`})}catch(e){o.warn(`[WebrtcGo2RtcCamera] RTCPeerConnection.addIceCandidate() error`,e)}break;case`webrtc/answer`:try{this.pc?.setRemoteDescription({type:`answer`,sdp:t.value})}catch(e){o.warn(`[WebrtcGo2RtcCamera] RTCPeerConnection.setRemoteDescription() error`,e)}break;case`error`:o.error(`[WebrtcGo2RtcCamera] ${t.value}`),this.updateStatus(`error`),this.pc?.close()}}async onWebSocketClose(e){if(!e.wasClean){this.updateStatus(`error`),o.error(`[WebrtcGo2RtcCamera] socket close was not clean`,e);let t=this.playbackAbortController?.signal;if(!t||t.aborted)return;this.sleepAbortController?.abort();let n=this.sleepAbortController=new AbortController;try{let e=[t,n.signal];await l(2e3,AbortSignal.any(e)),this.loadStream()}catch{}}}startPlayback(){this.playbackAbortController=new AbortController,this.loadStream()}stopPlayback(){this.playbackAbortController?.abort(),this.playbackAbortController=null,this.updateStatus(`disconnected`),this.pc&&=(this.pc.getSenders().forEach(e=>{e.track?.stop()}),this.pc.close(),null),this.ws?.close(),this.ws=null,this.cameraVideo.src=``,this.cameraVideo.srcObject=null}};r([s(`streamingElement`)],u.prototype,`cameraVideo`,void 0),u=r([i({})],u);var d=a(u,function(){var e=this,t=e._self._c;return e._self._setupProxy,t(`video`,{ref:`streamingElement`,style:e.cameraStyle,attrs:{autoplay:``,disablePictureInPicture:``,playsinline:``,muted:``,crossorigin:e.crossorigin},domProps:{muted:!0},on:{play:function(t){return e.updateStatus(`connected`)},error:function(t){return e.updateStatus(`error`)}}})},[],!1,null,null,null,null).exports;export{d as default};