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=[{urls:[`stun:stun.l.google.com:19302`]}],d=class extends o(c){cameraVideo;pc=null;remoteId=null;playbackAbortController=null;sleepAbortController=null;sendIceServers=!0;async loadStream(){this.pc?.close();let t=this.playbackAbortController?.signal;if(!t||t.aborted)return;this.updateStatus(`connecting`);let n=this.buildAbsoluteUrl(this.camera.stream_url||``);this.updateRawCameraUrl(n.toString());try{let r=await this.sendInitialRequest(n,t);this.remoteId=`id`in r&&typeof r.id==`string`?r.id:null;let i={sdpSemantics:`unified-plan`};`iceServers`in r&&Array.isArray(r.iceServers)&&(i.iceServers=r.iceServers);let a=this.pc=e(new RTCPeerConnection(i));a.ondatachannel=e=>{let t=e.channel;t.label===`keepalive`&&(t.onmessage=()=>{t.send(`pong`)})},a.addTransceiver(`video`,{direction:`recvonly`}),a.ontrack=e=>{e.track.kind===`video`&&(this.cameraVideo.srcObject=e.streams[0])},i.iceServers&&(a.onicecandidate=async e=>{if(e.candidate)try{await this.sendRemoteCandidatesRequest(n,[e.candidate],t)}catch(e){s.error(`[WebrtcCamerastreamerCamera] onicecandidate`,e)}}),await a.setRemoteDescription(r);let o=await a.createAnswer();await a.setLocalDescription(o),a.localDescription&&await this.sendOfferRequest(n,a.localDescription,t)}catch(e){s.error(`[WebrtcCamerastreamerCamera] failed to start playback "${this.camera.name}"`,e),this.onError()}}async onError(){this.updateStatus(`error`),this.pc?.close(),this.pc=null;let e=this.playbackAbortController?.signal;if(!e||e.aborted)return;this.sleepAbortController?.abort();let t=this.sleepAbortController=new AbortController;try{let n=[e,t.signal];await l(2e3,AbortSignal.any(n)),this.loadStream()}catch{}}async startPlayback(){this.playbackAbortController=new AbortController,await this.loadStream()}async sendInitialRequest(e,t){try{let n=await fetch(e,{body:JSON.stringify({type:`request`,...this.sendIceServers?{iceServers:u}:void 0,keepAlive:!0}),headers:{"Content-Type":`application/json`},method:`POST`,signal:t});return await this.ensureResponseOk(n,`application/json`),await n.json()}catch(e){throw t.aborted||e instanceof Error&&e.name===`AbortError`||(this.sendIceServers=!this.sendIceServers),e}}async sendRemoteCandidatesRequest(e,t,n){let r=await fetch(e,{body:JSON.stringify({type:`remote_candidate`,id:this.remoteId,candidates:t}),headers:{"Content-Type":`application/json`},method:`POST`,signal:n});await this.ensureResponseOk(r)}async sendOfferRequest(e,t,n){let r=await fetch(e,{body:JSON.stringify({type:t.type,id:this.remoteId,sdp:t.sdp}),headers:{"Content-Type":`application/json`},method:`POST`,signal:n});await this.ensureResponseOk(r)}async ensureResponseOk(e,t){let n=e.headers.get(`Content-Type`);if(!(e.ok&&(!t||n?.includes(t)))){let t=await e.text();throw Error(`Invalid response! Status: ${e.status}, Content-Type: ${n}, Body: ${t}`)}}stopPlayback(){this.updateStatus(`disconnected`),this.playbackAbortController?.abort(),this.playbackAbortController=null,this.pc?.close(),this.pc=null,this.cameraVideo.src=``,this.cameraVideo.srcObject=null}};i([n(`streamingElement`)],d.prototype,`cameraVideo`,void 0),d=i([r({})],d);var f=a(d,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{f as default};