import{l as e,o as t}from"./vue.runtime.esm-BGP6PPFT.js";import{a as n,l as r,n as i,t as a,u as o}from"./_plugin-vue2_normalizer-Vm4EDAeQ.js";import{t as s}from"./browser-1D2tZwAR.js";import{t as c}from"./camera-DKiOc18c.js";import{t as l}from"./sleep-CEQq2wgp.js";t();var u=class extends o(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){s.error(`[WebrtcGo2RtcCamera] failed to start playback "${this.camera.name}"`,e)}}async onWebSocketOpen(){s.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){s.warn(`[WebrtcGo2RtcCamera] RTCPeerConnection.addIceCandidate() error`,e)}break;case`webrtc/answer`:try{this.pc?.setRemoteDescription({type:`answer`,sdp:t.value})}catch(e){s.warn(`[WebrtcGo2RtcCamera] RTCPeerConnection.setRemoteDescription() error`,e)}break;case`error`:s.error(`[WebrtcGo2RtcCamera] ${t.value}`),this.updateStatus(`error`),this.pc?.close()}}async onWebSocketClose(e){if(!e.wasClean){this.updateStatus(`error`),s.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}};i([n(`streamingElement`)],u.prototype,`cameraVideo`,void 0),u=i([r({})],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};