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";t();var l=class extends o(c){cameraVideo;whepUrl=``;sessionUrl=``;pc=null;restartTimeout=null;offerData=null;queuedCandidates=[];unquoteCredential(e){return JSON.parse(`"${e}"`)}linkToIceServers(e){return e===null?[]:e.split(`, `).map(e=>{let t=e.match(/^<(.+?)>; rel="ice-server"(; username="(.*?)"; credential="(.*?)"; credential-type="password")?/i),n={urls:[t[1]]};return t[3]!==void 0&&(n.username=this.unquoteCredential(t[3]),n.credential=this.unquoteCredential(t[4]),n.credentialType=`password`),n})}parseOffer(e){let t={iceUfrag:``,icePwd:``,medias:[]};for(let n of e.split(`\r
`))n.startsWith(`m=`)?t.medias.push(n.slice(2)):t.iceUfrag===``&&n.startsWith(`a=ice-ufrag:`)?t.iceUfrag=n.slice(12):t.icePwd===``&&n.startsWith(`a=ice-pwd:`)&&(t.icePwd=n.slice(10));return t}generateSdpFragment(e,t){let n={};for(let e of t){let t=e.sdpMLineIndex;n[t]===void 0&&(n[t]=[]),n[t].push(e)}let r=`a=ice-ufrag:`+e.iceUfrag+`\r
a=ice-pwd:`+e.icePwd+`\r
`,i=0;for(let t of e.medias){if(n[i]!==void 0){r+=`m=`+t+`\r
a=mid:`+i+`\r
`;for(let e of n[i])r+=`a=`+e.candidate+`\r
`}i++}return r}async loadStream(){try{this.updateStatus(`connecting`);let t=await fetch(this.whepUrl,{method:`OPTIONS`}),n={iceServers:this.linkToIceServers(t.headers.get(`Link`)),sdpSemantics:`unified-plan`};this.pc=e(new RTCPeerConnection(n)),this.pc.addTransceiver(`video`,{direction:`recvonly`}),this.pc.onicecandidate=e=>{this.restartTimeout===null&&e.candidate!==null&&(this.sessionUrl===``?this.queuedCandidates.push(e.candidate):this.sendLocalCandidates([e.candidate]))},this.pc.oniceconnectionstatechange=()=>{this.restartTimeout===null&&this.pc?.iceConnectionState===`disconnected`&&(s.warn(`[WebrtcMediamtxCamera] peer connection disconnected`),this.onError())},this.pc.ontrack=e=>{this.cameraVideo.srcObject=e.streams[0]};let r=await this.pc.createOffer();this.offerData=this.parseOffer(r.sdp??``),this.pc.setLocalDescription(r),this.sendOffer(r)}catch(e){s.error(`[WebrtcMediamtxCamera] error on loadStream "${this.camera.name}"`,e),this.onError()}}onError(){this.updateStatus(`error`),this.restartTimeout===null&&(this.pc!==null&&(this.pc.close(),this.pc=null),this.restartTimeout=setTimeout(()=>{this.restartTimeout=null,this.loadStream()},2e3),this.sessionUrl&&=(fetch(this.sessionUrl,{method:`DELETE`}),``),this.queuedCandidates=[])}async sendLocalCandidates(e){try{let t=await fetch(this.sessionUrl,{method:`PATCH`,headers:{"Content-Type":`application/trickle-ice-sdpfrag`,"If-Match":`*`},body:this.generateSdpFragment(this.offerData,e)});switch(t.status){case 204:break;case 404:throw Error(`stream not found`);default:throw Error(`bad status code ${t.status}`)}}catch(e){s.error(`[WebrtcMediamtxCamera] error on sendLocalCandidates`,e),this.onError()}}onRemoteAnswer(e){this.restartTimeout===null&&(this.pc?.setRemoteDescription(new RTCSessionDescription({type:`answer`,sdp:e})),this.queuedCandidates.length!==0&&(this.sendLocalCandidates(this.queuedCandidates),this.queuedCandidates=[]))}async sendOffer(e){try{let t=await fetch(this.whepUrl,{method:`POST`,headers:{"Content-Type":`application/sdp`},body:e.sdp});switch(t.status){case 201:break;case 404:throw Error(`stream not found`);default:throw Error(`bad status code ${t.status}`)}this.sessionUrl=new URL(t.headers.get(`location`)??``,this.baseUrl).toString();let n=await t.text();this.onRemoteAnswer(n)}catch(e){s.error(`[WebrtcMediamtxCamera] error on sendOffer`,e),this.onError()}}get baseUrl(){let e=this.buildAbsoluteUrl(this.camera.stream_url||``);return e.pathname.endsWith(`/`)||(e.pathname+=`/`),e}startPlayback(){this.whepUrl=new URL(`whep`,this.baseUrl).toString(),this.loadStream()}stopPlayback(){this.updateStatus(`disconnected`),this.sessionUrl=``,this.queuedCandidates=[],this.restartTimeout&&=(clearTimeout(this.restartTimeout),null),this.pc&&=(this.pc.close(),null),this.cameraVideo.src=``,this.cameraVideo.srcObject=null}};i([n(`streamingElement`)],l.prototype,`cameraVideo`,void 0),l=i([r({})],l);var u=a(l,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{u as default};