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