var f=Object.defineProperty;var S=(s,t,e)=>t in s?f(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var p=(s,t,e)=>S(s,typeof t!="symbol"?t+"":t,e);import{m as g,C as v,f as b,l as _,R as A,j as w,k as O}from"./index-CBxrqwze.js";var P=Object.defineProperty,j=Object.getOwnPropertyDescriptor,h=(s,t,e,r)=>{for(var a=r>1?void 0:r?j(t,e):t,o=s.length-1,n;o>=0;o--)(n=s[o])&&(a=(r?n(t,e,a):n(a))||a);return r&&a&&P(t,e,a),a};let m=class extends g(v){constructor(){super(...arguments);p(this,"cameraVideo");p(this,"pc",null);p(this,"remoteId",null);p(this,"playbackAbortController",null);p(this,"sleepAbortController",null)}async loadStream(){var a,o;(a=this.pc)==null||a.close();const e=(o=this.playbackAbortController)==null?void 0:o.signal;if(!e||e.aborted)return;this.updateStatus("connecting");const r=this.buildAbsoluteUrl(this.camera.stream_url||"");this.updateRawCameraUrl(r.toString());try{const i=await(await fetch(r,{body:JSON.stringify({type:"request",iceServers:[{urls:["stun:stun.l.google.com:19302"]}]}),headers:{"Content-Type":"application/json"},method:"POST",signal:e})).json();this.remoteId="id"in i&&typeof i.id=="string"?i.id:null;const u={sdpSemantics:"unified-plan"};"iceServers"in i&&Array.isArray(i.iceServers)&&(u.iceServers=i.iceServers);const c=this.pc=new RTCPeerConnection(u);c.addTransceiver("video",{direction:"recvonly"}),c.ontrack=d=>{d.track.kind==="video"&&(this.cameraVideo.srcObject=d.streams[0])},u.iceServers&&(c.onicecandidate=async d=>{if(d.candidate)try{await fetch(r,{body:JSON.stringify({type:"remote_candidate",id:this.remoteId,candidates:[d.candidate]}),headers:{"Content-Type":"application/json"},method:"POST",signal:e})}catch(C){b.error("[WebrtcCamerastreamerCamera] onicecandidate",C)}}),await c.setRemoteDescription(i);const y=await c.createAnswer();await c.setLocalDescription(y);const l=c.localDescription;await(await fetch(r,{body:JSON.stringify({type:l==null?void 0:l.type,id:this.remoteId,sdp:l==null?void 0:l.sdp}),headers:{"Content-Type":"application/json"},method:"POST",signal:e})).json()}catch(n){b.error(`[WebrtcCamerastreamerCamera] failed to start playback "${this.camera.name}"`,n),this.onError()}}async onError(){var a,o,n;this.updateStatus("error"),(a=this.pc)==null||a.close(),this.pc=null;const e=(o=this.playbackAbortController)==null?void 0:o.signal;if(!e||e.aborted)return;(n=this.sleepAbortController)==null||n.abort();const r=this.sleepAbortController=new AbortController;try{const i=[e,r.signal];await _(2e3,AbortSignal.any(i)),this.loadStream()}catch{}}async startPlayback(){this.playbackAbortController=new AbortController,await this.loadStream()}stopPlayback(){var e,r;this.updateStatus("disconnected"),(e=this.playbackAbortController)==null||e.abort(),this.playbackAbortController=null,(r=this.pc)==null||r.close(),this.pc=null,this.cameraVideo.src="",this.cameraVideo.srcObject=null}};h([A("streamingElement")],m.prototype,"cameraVideo",2);m=h([w({})],m);var k=function(){var t=this,e=t._self._c;return t._self._setupProxy,e("video",{ref:"streamingElement",style:t.cameraStyle,attrs:{autoplay:"",playsinline:"",muted:"",crossorigin:t.crossorigin},domProps:{muted:!0},on:{play:function(r){return t.updateStatus("connected")},error:function(r){return t.updateStatus("error")}}})},T=[],D=O(m,k,T,!1,null,null);const W=D.exports;export{W as default};
