import{_ as m}from"./WebcamNozzleCrosshair-OgFJAMdG.js";import{m as d,B as u,H as g,J as f,P as h,R as _,W as p,C as S,n as y}from"./index-Bku7SJna.js";import{m as w,n as C,r as v}from"./vuetify-Ce4WN_Dg.js";import"./overlayscrollbars-CiKU261J.js";import"./echarts-DzoUeqWp.js";var T=Object.defineProperty,b=Object.getOwnPropertyDescriptor,n=(l,t,e,s)=>{for(var r=s>1?void 0:s?b(t,e):t,o=l.length-1,c;o>=0;o--)(c=l[o])&&(r=(s?c(t,e,r):c(r))||r);return s&&r&&T(t,e,r),r};let a=class extends d(u,g){constructor(){super(...arguments),this.capitalize=f,this.pc=null,this.useStun=!1,this.aspectRatio=null,this.status="connecting",this.restartTimer=null}get url(){var t;return this.convertUrl((t=this.camSettings)==null?void 0:t.stream_url,this.printerUrl)}get webcamStyle(){var e,s,r;const t={transform:this.generateTransform((e=this.camSettings.flip_horizontal)!=null?e:!1,(s=this.camSettings.flip_vertical)!=null?s:!1,(r=this.camSettings.rotation)!=null?r:0),aspectRatio:1.7777777777777777};return this.aspectRatio&&(t.aspectRatio=this.aspectRatio),t}get nozzleCrosshair(){var t,e;return(e=(t=this.camSettings.extra_data)==null?void 0:t.nozzleCrosshair)!=null?e:!1}get expanded(){var t;return this.page!=="dashboard"?!0:(t=this.$store.getters["gui/getPanelExpand"]("webcam-panel",this.viewport))!=null?t:!1}expandChanged(t){if(!t){this.terminate();return}this.start()}async start(){if(this.restartTimer&&(this.log("Clearing restart timer before starting stream"),window.clearTimeout(this.restartTimer)),!this.expanded){this.log("Not expanded, not starting stream");return}this.log("Requesting ICE servers from ".concat(this.url));try{const t=this.useStun?[{urls:["stun:stun.l.google.com:19302"]}]:null,e=await fetch(this.url,{body:JSON.stringify({type:"request",iceServers:t}),method:"POST"});if(e.status!==200){this.log("Failed to start stream: ".concat(e.status)),this.restartStream();return}const s=await e.json();await this.onIceServers(s)}catch(t){this.log("Failed to start stream",t)}}async onIceServers(t){var o,c;this.pc&&this.pc.close();let e={iceServers:(o=t.iceServers)!=null?o:[],sdpSemantics:"unified-plan"};this.pc=new RTCPeerConnection(e),this.pc.addTransceiver("video",{direction:"recvonly"}),"iceServers"in t?this.pc.onicecandidate=i=>this.onIceCandidate(i,t.id):this.log("No ICE servers returned, so the current camera-streamer version may not support them"),this.pc.onconnectionstatechange=()=>this.onConnectionStateChange(),this.pc.ontrack=i=>this.onTrack(i),await((c=this.pc)==null?void 0:c.setRemoteDescription(t));const s=await this.pc.createAnswer();await this.pc.setLocalDescription(s);const r=this.pc.localDescription;if(!r){this.log("Failed to create offer"),this.restartStream();return}try{const i=await fetch(this.url,{body:JSON.stringify({type:r==null?void 0:r.type,id:t.id,sdp:r==null?void 0:r.sdp}),headers:{"Content-Type":"application/json"},method:"POST"});i.status!==200&&(this.log("Failed to send offer: ".concat(i.status)),this.restartStream())}catch(i){this.log("Failed to send offer",i),this.restartStream()}}async onIceCandidate(t,e){if(t.candidate)try{const s=await fetch(this.url,{body:JSON.stringify({id:e,type:"remote_candidate",candidates:[t.candidate]}),headers:{"Content-Type":"application/json"},method:"POST"});s.status!==200&&(this.log("Failed to send ICE candidate: ".concat(s.status)),this.restartStream())}catch(s){this.log("Failed to send ICE candidate",s),this.restartStream()}}onConnectionStateChange(){var t,e;this.status=(e=(t=this.pc)==null?void 0:t.connectionState)!=null?e:"connecting",this.log("State: ".concat(this.status)),["failed","disconnected"].includes(this.status)&&this.restartStream(5e3)}onTrack(t){t.track.kind==="video"&&(this.stream.srcObject=t.streams[0])}log(t,e){const s="[WebRTC camera-streamer] ".concat(t);if(e){window.console.log(s,e);return}window.console.log(s)}beforeDestroy(){this.terminate(),this.restartTimer&&window.clearTimeout(this.restartTimer)}terminate(){var t;this.log("Terminating stream"),(t=this.pc)==null||t.close()}restartStream(t=500){this.terminate(),!this.restartTimer&&(this.restartTimer=window.setTimeout(async()=>{this.restartTimer=null,await this.start()},t))}changedUrl(){this.restartStream()}};n([h({required:!0})],a.prototype,"camSettings",2);n([h({default:null})],a.prototype,"printerUrl",2);n([h({type:String,default:null})],a.prototype,"page",2);n([_()],a.prototype,"stream",2);n([p("expanded",{immediate:!0})],a.prototype,"expandChanged",1);n([p("url")],a.prototype,"changedUrl",1);a=n([S({components:{WebcamNozzleCrosshair:m}})],a);var x=function(){var t=this,e=t._self._c;return t._self._setupProxy,e("div",{staticClass:"position-relative d-flex"},[e("video",{directives:[{name:"show",rawName:"v-show",value:t.status==="connected",expression:"status === 'connected'"}],ref:"stream",staticClass:"webcamStream",style:t.webcamStyle,attrs:{autoplay:"",muted:"",playsinline:""},domProps:{muted:!0}}),t.nozzleCrosshair?e(m,{attrs:{webcam:t.camSettings}}):t._e(),t.status!=="connected"?e(w,[e(C,{staticClass:"_webcam_webrtc_output text-center d-flex flex-column justify-center align-center"},[t.status==="connecting"?e(v,{staticClass:"mb-3",attrs:{indeterminate:"",color:"primary"}}):t._e(),e("span",{staticClass:"mt-3"},[t._v(t._s(t.capitalize(t.status)))])],1)],1):t._e()],1)},z=[],P=y(a,x,z,!1,null,"378940ea");const N=P.exports;export{N as default};