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