@voicenter-team/opensips-js 1.0.96 → 1.0.97

Sign up to get free protection for your applications and to get access to all the features.
@@ -155,4 +155,4 @@ a=accept-types:text/plain text/html
155
155
  a=path:${s.getHeader("Use-Path")} msrp://${this._ua._configuration.authorization_user}.${this._ua._configuration.realm}:2856/${this.auth_id};ws
156
156
  `),this._newMSRPSession("local",this._request),this._id=this._request.call_id+this._from_tag,console.log("dialog be",this._dialog),new aa(this._ua,this._request,{onRequestTimeout:()=>{this.onRequestTimeout(),console.log("to")},onTransportError:t=>{this.onTransportError(),console.log(t)},onAuthenticated:t=>{this._request=t},onReceiveResponse:t=>{this._receiveInviteResponse(t),console.log("dialog af",this._dialog),t.status_code===200&&(t.parseSDP(!0),this._status=Ce.STATUS_CONFIRMED,this.target_addr=t.sdp.media[0].invalid[1].value.replaceAll("path:","").split(" ").reverse(),this.status="active",this.emit("active"),this.emit("confirmed"))}}).send(),this._status=Ce.STATUS_INVITE_SENT}terminate(s={}){console.log("terminate",this);const n=s.cause||L.causes.BYE,a=Me.cloneArray(s.extraHeaders),o=s.body;let t,r=s.status_code,_=s.reason_phrase;if(this._status===Ce.STATUS_TERMINATED)throw new Ds.InvalidStateError(this._status);switch(this.status="terminated",this._status){case Ce.STATUS_NULL:case Ce.STATUS_INVITE_SENT:case Ce.STATUS_1XX_RECEIVED:if(r&&(r<200||r>=700))throw new TypeError(`Invalid status_code: ${r}`);r&&(_=_||L.REASON_PHRASE[r]||"",t=`SIP ;cause=${r} ;text="${_}"`),this._status===Ce.STATUS_NULL||this._status===Ce.STATUS_INVITE_SENT?(this._is_canceled=!0,this._cancel_reason=t):this._status===Ce.STATUS_1XX_RECEIVED&&this._request.cancel(t),this._status=Ce.STATUS_CANCELED,console.log("failed 1"),this._failed("local",null,L.causes.CANCELED);break;case Ce.STATUS_WAITING_FOR_ANSWER:case Ce.STATUS_ANSWERED:if(r=r||480,console.log("REPLY 480"),r<300||r>=700)throw new TypeError(`Invalid status_code: ${r}`);this._request.reply(r,_,a,o),console.log("failed 2"),this._failed("local",null,L.causes.REJECTED);break;case Ce.STATUS_WAITING_FOR_ACK:case Ce.STATUS_CONFIRMED:if(_=s.reason_phrase||L.REASON_PHRASE[r]||"",r&&(r<200||r>=700))throw new TypeError(`Invalid status_code: ${r}`);if(r&&a.push(`Reason: SIP ;cause=${r}; text="${_}"`),this._status===Ce.STATUS_WAITING_FOR_ACK&&this._direction==="incoming"&&this._request.server_transaction.state!==$t.C.STATUS_TERMINATED){const h=this._dialog;this.receiveRequest=({method:d})=>{d===L.ACK&&(this.sendRequest(L.BYE,{extraHeaders:a,body:o}),h.terminate())},this._request.server_transaction.on("stateChanged",()=>{this._request.server_transaction.state===$t.C.STATUS_TERMINATED&&(this.sendRequest(L.BYE,{extraHeaders:a,body:o}),h.terminate())}),this._ended("local",null,n),this._dialog=h,this._ua.newDialog(h)}else console.log("here it is"),this.sendRequest(L.BYE,{extraHeaders:a,body:o}),this._ended("local",null,n)}}sendRequest(s,n){return this._dialog.sendRequest(s,n)}authenticate(s){this.status="auth";let n=new Vn("");n.method="AUTH",n.addHeader("To-Path",`msrp://${this._ua._configuration.realm}:2856;ws`),n.addHeader("From-Path",`msrp://${this.credentials.username}.${this.credentials.realm}:2856/${this.auth_id};ws`),s&&n.addHeader("Authorization",s.toString());let a=n.toString();console.log(a);let o=[];for(var t=0;t<a.length;t++)o.push(a.charCodeAt(t).toString(16));console.log(o),this._connection.send(n.toString())}onmessage(s){console.log("onmessage",s);const n=new Vn(s.data);if(this.status==="auth"&&n.code===401){const a=this.parseAuth(n.getHeader("WWW-Authenticate")),o=new bm(this.credentials);o.authenticate({method:"AUTH",ruri:`msrp://${this._ua._configuration.realm}:2856;ws`,body:null},a,Me.createRandomToken(12)),this.authenticate(o)}this.status==="auth"&&n.code===200&&this._direction==="outgoing"?(this.my_addr.push(n.getHeader("To-Path")),this.my_addr.push(n.getHeader("Use-Path")),this.status="active",this.inviteParty(n)):this.status==="auth"&&n.code===200&&this._direction==="incoming"?(this.my_addr.push(n.getHeader("To-Path")),this.my_addr.push(n.getHeader("Use-Path")),this.status="active",this.acceptParty(n),this.emit("confirmed")):n.method==="SEND"&&(this._sendOk(n),this._sendReport(n),n.direction="incoming",this.emit("newMessage",n),this._msgHistory.push(n),this.emit("msgHistoryUpdate",this._msgHistory),console.log("======================================================================")),n.code===480&&this._close()}onclose(){console.log("close")}onopen(){const s=new RTCPeerConnection({iceServers:[]});s.createDataChannel(""),s.createOffer().then(s.setLocalDescription.bind(s)),s.onicecandidate=n=>{if(!n||!n.candidate||!n.candidate.candidate)return;const a=/([0-9]{1,3}(\.[0-9]{1,3}){3})/,o=n.candidate.candidate.match(a);this.my_ip=o&&o[1],s.onicecandidate=()=>{},this.authenticate(null)}}onerror(s){console.log(s)}_receiveInviteResponse(s){if(console.log("resp0000000000000",s),this._dialog&&s.status_code>=200&&s.status_code<=299)if(console.log("200000000000000"),this._dialog.id.call_id===s.call_id&&this._dialog.id.local_tag===s.from_tag&&this._dialog.id.remote_tag===s.to_tag){this.sendRequest(L.ACK);return}else{const n=new ca(this,s,"UAC");if(n.error!==void 0){console.log(n.error);return}this.sendRequest(L.ACK),this.sendRequest(L.BYE);return}if(this._is_canceled){s.status_code>=100&&s.status_code<200?this._request.cancel(this._cancel_reason):s.status_code>=200&&s.status_code<299&&this._acceptAndTerminate(s);return}if(!(this._status!==Ce.STATUS_INVITE_SENT&&this._status!==Ce.STATUS_1XX_RECEIVED))switch(console.log("start Switch"),!0){case/^100$/.test(s.status_code):this._status=Ce.STATUS_1XX_RECEIVED;break;case/^1[0-9]{2}$/.test(s.status_code):{if(!s.to_tag){console.log("1xx response received without to tag");break}if(s.hasHeader("contact")&&!this._createDialog(s,"UAC",!0))break;if(this._status=Ce.STATUS_1XX_RECEIVED,!s.body){this._progress("remote",s);break}const n={originator:"remote",type:"answer",sdp:s.body};console.log('emit "sdp"'),this.emit("sdp",n);const a=new RTCSessionDescription({type:"answer",sdp:n.sdp});this._connectionPromiseQueue=this._connectionPromiseQueue.then(()=>this._connection.setRemoteDescription(a)).then(()=>this._progress("remote",s)).catch(o=>{console.log('emit "peerconnection:setremotedescriptionfailed" [error:%o]',o),this.emit("peerconnection:setremotedescriptionfailed",o)});break}case/^2[0-9]{2}$/.test(s.status_code):{if(console.log("maybe here???"),this._status=Ce.STATUS_CONFIRMED,!s.body){this._acceptAndTerminate(s,400,L.causes.MISSING_SDP),console.log("failed 3"),this._failed("remote",s,L.causes.BAD_MEDIA_DESCRIPTION);break}if(!this._createDialog(s,"UAC"))break;const n={originator:"remote",type:"answer",sdp:s.body};console.log('emit "sdp"'),this.emit("sdp",n),new RTCSessionDescription({type:"answer",sdp:n.sdp}),this._connectionPromiseQueue=this._connectionPromiseQueue.then(()=>{if(this._connection.signalingState==="stable")return this._connection.createOffer(this._rtcOfferConstraints).then(a=>this._connection.setLocalDescription(a)).catch(a=>{this._acceptAndTerminate(s,500,a.toString()),console.log("failed 4"),this._failed("local",s,L.causes.WEBRTC_ERROR)})}).then(()=>{this._handleSessionTimersInIncomingResponse(s),this._accepted("remote",s),this.sendRequest(L.ACK),this._confirmed("local",null)});break}default:{const n=Me.sipErrorCause(s.status_code);console.log("failed 5"),this._failed("remote",s,n)}}}sendMSRP(s){const n=new Vn("");n.method="SEND",n.addHeader("To-Path",`${this.my_addr[1]} ${this.target_addr[1]} ${this.target_addr[0]}`),n.addHeader("From-Path",`${this.my_addr[0]}`),n.addHeader("Message-ID",Me.createRandomToken(10)),n.addHeader("Byte-Range","1-25/25"),n.addHeader("Content-Type","text/plain"),n.addHeader("Success-Report","yes"),n.addHeader("Failure-Report","yes"),n.body=s;let a=n.toString();console.log(a);let o=[];for(var t=0;t<a.length;t++)o.push(a.charCodeAt(t).toString(16));console.log(o),this._connection.send(n.toString()),n.direction="outgoing",this.emit("newMessage",n),this._msgHistory.push(n),this.emit("msgHistoryUpdate",this._msgHistory)}_sendOk(s){let n=s.ident,a=s.getHeader("Message-ID"),o=new Vn("");o.method="200 OK",o.addHeader("To-Path",`${this.my_addr[1]}`),o.addHeader("From-Path",`${this.my_addr[0]}`),o.addHeader("Message-ID",a),o.ident=n;let t=o.toString();console.log(t);let r=[];for(var _=0;_<t.length;_++)r.push(t.charCodeAt(_).toString(16));console.log(r),this._connection.send(o.toString())}_sendReport(s){let n=s.ident,a=s.getHeader("Message-ID"),o=new Vn("");o.method="REPORT",o.addHeader("To-Path",`${s.getHeader("From-Path")}`),o.addHeader("From-Path",`${this.my_addr[0]}`),o.addHeader("Message-ID",a),o.addHeader("Byte-Range","1-25/25"),o.addHeader("Status","000 200 OK"),o.ident=n;let t=o.toString();console.log(t);let r=[];for(var _=0;_<t.length;_++)r.push(t.charCodeAt(_).toString(16));console.log(r),this._connection.send(o.toString())}parseAuth(s){const n={},a=s.replace("Digest","").split(",");for(const o of a){const t=o.trim().split("=");n[t[0]]=t[1].match('^"(.+)"$')[1]}return n}init_incoming(s,n){let a;const o=s.hasHeader("Content-Type")?s.getHeader("Content-Type").toLowerCase():void 0;if(s.body&&o!=="application/sdp"){s.reply(415);return}if(this._status=Ce.STATUS_INVITE_RECEIVED,this._from_tag=s.from_tag,this._id=s.call_id+this._from_tag,this._request=s,this._contact=this._ua.contact.toString(),s.hasHeader("expires")&&(a=s.getHeader("expires")*1e3),s.to_tag=Me.newTag(),!this._createDialog(s,"UAS",!0)){s.reply(500,"Missing Contact header field");return}s.body?this._late_sdp=!1:this._late_sdp=!0,this._status=Ce.STATUS_WAITING_FOR_ANSWER,this._timers.userNoAnswerTimer=setTimeout(()=>{s.reply(408),console.log("failed 6"),this._failed("local",null,L.causes.NO_ANSWER)},this._ua.configuration.no_answer_timeout),a&&(this._timers.expiresTimer=setTimeout(()=>{this._status===Ce.STATUS_WAITING_FOR_ANSWER&&(s.reply(487),console.log("failed 7"),this._failed("system",null,L.causes.EXPIRES))},a)),this._direction="incoming",this._local_identity=s.to,this._remote_identity=s.from,n&&n(this),s.parseSDP(!0),this.target_addr=s.sdp.media[0].invalid[1].value.replaceAll("path:","").split(" ").reverse(),this._newMSRPSession("remote",s),this._status!==Ce.STATUS_TERMINATED&&(s.reply(180,null,[`Contact: ${this._ua._contact}`]),this._progress("local",null))}_failed(s,n,a){this.emit("_failed",{originator:s,message:n||null,cause:a}),this._close(),this.emit("failed",{originator:s,message:n||null,cause:a})}_close(){if(console.log("CLOSE SESSION"),this._status!==Ce.STATUS_TERMINATED){if(this._status=Ce.STATUS_TERMINATED,this._connection)try{this._connection.close()}catch(s){console.log("close() | error closing the RTCPeerConnection: %o",s)}for(const s in this._timers)Object.prototype.hasOwnProperty.call(this._timers,s)&&clearTimeout(this._timers[s]);clearTimeout(this._sessionTimers.timer),this._dialog&&(this._dialog.terminate(),delete this._dialog);for(const s in this._earlyDialogs)Object.prototype.hasOwnProperty.call(this._earlyDialogs,s)&&(this._earlyDialogs[s].terminate(),delete this._earlyDialogs[s]);for(const s in this._referSubscribers)Object.prototype.hasOwnProperty.call(this._referSubscribers,s)&&delete this._referSubscribers[s];this._ua.destroyMSRPSession(this)}}_createDialog(s,n,a){const o=n==="UAS"?s.to_tag:s.from_tag,t=n==="UAS"?s.from_tag:s.to_tag,r=s.call_id+o+t;let _=this._earlyDialogs[r];if(a)return _?!0:(_=new ca(this,s,n,ca.C.STATUS_EARLY),_.error?(console.log("failed 8"),this._failed("remote",s,L.causes.INTERNAL_ERROR),!1):(this._earlyDialogs[r]=_,!0));{if(this._from_tag=s.from_tag,this._to_tag=s.to_tag,_)return _.update(s,n),this._dialog=_,delete this._earlyDialogs[r],!0;const h=new ca(this,s,n);return h.error?(console.log("failed 9"),this._failed("remote",s,L.causes.INTERNAL_ERROR),!1):(this._dialog=h,!0)}}_newMSRPSession(s,n){this._ua.newMSRPSession(this,{originator:s,session:this,request:n})}_progress(s,n){this.emit("progress",{originator:s,response:n||null})}isEnded(){switch(this._status){case Ce.STATUS_CANCELED:case Ce.STATUS_TERMINATED:return!0;default:return!1}}_accepted(s,n){console.log("session accepted"),this._start_time=new Date,console.log('emit "accepted"'),this.emit("accepted",{originator:s,response:n||null})}_confirmed(s,n){console.log("session confirmed"),this._is_confirmed=!0,console.log('emit "confirmed"'),this.emit("confirmed",{originator:s,ack:n||null})}_ended(s,n,a){console.log("session ended"),this._end_time=new Date,this._close(),console.log('emit "ended"'),this.emit("ended",{originator:s,message:n||null,cause:a})}_handleSessionTimersInIncomingResponse(s){if(!this._sessionTimers.enabled)return;let n;s.session_expires&&s.session_expires>=L.MIN_SESSION_EXPIRES?(this._sessionTimers.currentExpires=s.session_expires,n=s.session_expires_refresher||"uac"):(this._sessionTimers.currentExpires=this._sessionTimers.defaultExpires,n="uac"),this._sessionTimers.refresher=n==="uac",this._runSessionTimer()}receiveRequest(s){if(console.log("receiveRequest()"),s.method===L.CANCEL)(this._status===Ce.STATUS_WAITING_FOR_ANSWER||this._status===Ce.STATUS_ANSWERED)&&(this._status=Ce.STATUS_CANCELED,this._request.reply(487),console.log("failed 10"),this._failed("remote",s,L.causes.CANCELED));else switch(s.method){case L.ACK:if(this._status!==Ce.STATUS_WAITING_FOR_ACK)return;if(this._status=Ce.STATUS_CONFIRMED,clearTimeout(this._timers.ackTimer),clearTimeout(this._timers.invite2xxTimer),this._late_sdp){if(!s.body){this.terminate({cause:L.causes.MISSING_SDP,status_code:400});break}const n={originator:"remote",type:"answer",sdp:s.body};console.log('emit "sdp"'),this.emit("sdp",n);const a=new RTCSessionDescription({type:"answer",sdp:n.sdp});this._connectionPromiseQueue=this._connectionPromiseQueue.then(()=>this._connection.setRemoteDescription(a)).then(()=>{this._is_confirmed||this._confirmed("remote",s)}).catch(o=>{this.terminate({cause:L.causes.BAD_MEDIA_DESCRIPTION,status_code:488}),console.log('emit "peerconnection:setremotedescriptionfailed" [error:%o]',o),this.emit("peerconnection:setremotedescriptionfailed",o)})}else this._is_confirmed||this._confirmed("remote",s);break;case L.BYE:this._status===Ce.STATUS_CONFIRMED||this._status===Ce.STATUS_WAITING_FOR_ACK?(s.reply(200),this._ended("remote",s,L.causes.BYE)):this._status===Ce.STATUS_INVITE_RECEIVED||this._status===Ce.STATUS_WAITING_FOR_ANSWER?(s.reply(200),this._request.reply(487,"BYE Received"),this._ended("remote",s,L.causes.BYE)):s.reply(403,"Wrong Status");break;case L.INVITE:this._status===Ce.STATUS_CONFIRMED?s.hasHeader("replaces")?this._receiveReplaces(s):this._receiveReinvite(s):s.reply(403,"Wrong Status");break;case L.INFO:this._status===Ce.STATUS_1XX_RECEIVED||this._status===Ce.STATUS_WAITING_FOR_ANSWER||this._status===Ce.STATUS_ANSWERED||this._status===Ce.STATUS_WAITING_FOR_ACK||this._status===Ce.STATUS_CONFIRMED?(s.hasHeader("Content-Type")?s.getHeader("Content-Type").toLowerCase():void 0)!==void 0?new nu(this).init_incoming(s):s.reply(415):s.reply(403,"Wrong Status");break;case L.UPDATE:this._status===Ce.STATUS_CONFIRMED?this._receiveUpdate(s):s.reply(403,"Wrong Status");break;case L.REFER:this._status===Ce.STATUS_CONFIRMED?this._receiveRefer(s):s.reply(403,"Wrong Status");break;case L.NOTIFY:this._status===Ce.STATUS_CONFIRMED?this._receiveNotify(s):s.reply(403,"Wrong Status");break;default:s.reply(501)}}onTransportError(){console.log("onTransportError()"),this._status!==Ce.STATUS_TERMINATED&&this.terminate({status_code:500,reason_phrase:L.causes.CONNECTION_ERROR,cause:L.causes.CONNECTION_ERROR})}onRequestTimeout(){console.log("onRequestTimeout()"),this._status!==Ce.STATUS_TERMINATED&&this.terminate({status_code:408,reason_phrase:L.causes.REQUEST_TIMEOUT,cause:L.causes.REQUEST_TIMEOUT})}onDialogError(){console.log("onDialogError()"),this._status!==Ce.STATUS_TERMINATED&&this.terminate({status_code:500,reason_phrase:L.causes.DIALOG_ERROR,cause:L.causes.DIALOG_ERROR})}}var mT=Kt();const yu=xe(mT),ya=new na("Parser"),gT=(c,s)=>{let n,a,o=c.indexOf(`\r
157
157
  `);if(o===-1){ya.warn("parseMessage() | no CRLF found, not a SIP message");return}const t=c.substring(0,o);let r=yu.parse(t,"Request_Response");if(r===-1){ya.warn(`parseMessage() | error parsing first line of SIP message: "${t}"`);return}else r.status_code?(n=new en.IncomingResponse,n.status_code=r.status_code,n.reason_phrase=r.reason_phrase):(n=new en.IncomingRequest(s),n.method=r.method,n.ruri=r.uri);n.data=c;let _=o+2;for(;;){if(o=TT(c,_),o===-2){a=_+2;break}else if(o===-1){ya.warn("parseMessage() | malformed message");return}if(r=vT(n,c,_,o),r!==!0){ya.warn("parseMessage() |",r.error);return}_=o+2}if(n.hasHeader("content-length")){const h=n.getHeader("content-length");n.body=c.substr(a,h)}else n.body=c.substring(a);return n};function TT(c,s){let n=s,a=0,o=0;if(c.substring(n,n+2).match(/(^\r\n)/))return-2;for(;a===0;){if(o=c.indexOf(`\r
158
- `,n),o===-1)return o;!c.substring(o+2,o+4).match(/(^\r\n)/)&&c.charAt(o+2).match(/(^\s+)/)?n=o+2:a=o}return a}function vT(c,s,n,a){let o;const t=s.indexOf(":",n),r=s.substring(n,t).trim(),_=s.substring(t+1,a).trim();switch(r.toLowerCase()){case"via":case"v":c.addHeader("via",_),c.getHeaders("via").length===1?(o=c.parseHeader("Via"),o&&(c.via=o,c.via_branch=o.branch)):o=0;break;case"from":case"f":c.setHeader("from",_),o=c.parseHeader("from"),o&&(c.from=o,c.from_tag=o.getParam("tag"));break;case"to":case"t":c.setHeader("to",_),o=c.parseHeader("to"),o&&(c.to=o,c.to_tag=o.getParam("tag"));break;case"record-route":if(o=yu.parse(_,"Record_Route"),o===-1)o=void 0;else for(const h of o)c.addHeader("record-route",_.substring(h.possition,h.offset)),c.headers["Record-Route"][c.getHeaders("record-route").length-1].parsed=h.parsed;break;case"call-id":case"i":c.setHeader("call-id",_),o=c.parseHeader("call-id"),o&&(c.call_id=_);break;case"contact":case"m":if(o=yu.parse(_,"Contact"),o===-1)o=void 0;else for(const h of o)c.addHeader("contact",_.substring(h.possition,h.offset)),c.headers.Contact[c.getHeaders("contact").length-1].parsed=h.parsed;break;case"content-length":case"l":c.setHeader("content-length",_),o=c.parseHeader("content-length");break;case"content-type":case"c":c.setHeader("content-type",_),o=c.parseHeader("content-type");break;case"cseq":c.setHeader("cseq",_),o=c.parseHeader("cseq"),o&&(c.cseq=o.value),c instanceof en.IncomingResponse&&(c.method=o.method);break;case"max-forwards":c.setHeader("max-forwards",_),o=c.parseHeader("max-forwards");break;case"www-authenticate":c.setHeader("www-authenticate",_),o=c.parseHeader("www-authenticate");break;case"proxy-authenticate":c.setHeader("proxy-authenticate",_),o=c.parseHeader("proxy-authenticate");break;case"session-expires":case"x":c.setHeader("session-expires",_),o=c.parseHeader("session-expires"),o&&(c.session_expires=o.expires,c.session_expires_refresher=o.refresher);break;case"refer-to":case"r":c.setHeader("refer-to",_),o=c.parseHeader("refer-to"),o&&(c.refer_to=o);break;case"replaces":c.setHeader("replaces",_),o=c.parseHeader("replaces"),o&&(c.replaces=o);break;case"event":case"o":c.setHeader("event",_),o=c.parseHeader("event"),o&&(c.event=o);break;default:c.addHeader(r,_),o=0}return o===void 0?{error:`error parsing header "${r}"`}:!0}const ET={parseMessage:gT},si=new na("Registrator"),Aa=10;class ST{constructor(s,n){this._reg_id=1,this._ua=s,this._transport=n,this._registrar=s.configuration.registrar_server,this._expires=s.configuration.register_expires,this._call_id=Ts.createRandomToken(22),this._cseq=0,this._to_uri=s.configuration.uri,this._registrationTimer=null,this._registering=!1,this._registered=!1,this._contact=this._ua.contact.toString();const a=this._contact.indexOf(">");if(a!==-1){const o=this._contact.slice(0,a)+this._contact.slice(a+1,this._contact.length);this._contact=o}this._extra_contact="",this._extraContactParams="",this._extra_contact+=";+sip.ice",this._extraHeaders=[],this._sipInstance=`"<urn:uuid:${this._ua.configuration.instance_id}>"`,this._extra_contact+=`;reg-id=${this._reg_id}`,this._extra_contact+=`;+sip.instance=${this._sipInstance}`}get registered(){return this._registered}setExtraHeaders(s){Array.isArray(s)||(s=[]),this._extraHeaders=s.slice()}setExtraContactParams(s){s instanceof Object||(s={}),this._extraContactParams="";for(const n in s)if(Object.prototype.hasOwnProperty.call(s,n)){const a=s[n];this._extraContactParams+=`;${n}`,a&&(this._extraContactParams+=`=${a}`)}}setExtraContactUriParams(s){s instanceof Object||(s={}),this._extraContactParams="";for(const n in s)if(Object.prototype.hasOwnProperty.call(s,n)){const a=s[n];this._extraContactParams+=`;${n}`,a&&(this._extraContactParams+=`=${a}`)}}register(){if(this._registering){si.debug("Register request in progress...");return}const s=this._extraHeaders.slice();s.push(`Contact: ${this._contact}${this._extraContactParams}>${this._extra_contact};expires=${this._expires}`),s.push(`Expires: ${this._expires}`);const n=new Rt.OutgoingRequest(L.REGISTER,this._registrar,this._ua,{to_uri:this._to_uri,call_id:this._call_id,cseq:this._cseq+=1},s),a=new aa(this._ua,n,{onRequestTimeout:()=>{this._registrationFailure(null,L.causes.REQUEST_TIMEOUT)},onTransportError:()=>{this._registrationFailure(null,L.causes.CONNECTION_ERROR)},onAuthenticated:()=>{this._cseq+=1},onReceiveResponse:o=>{if(o.cseq===this._cseq)switch(this._registrationTimer!==null&&(clearTimeout(this._registrationTimer),this._registrationTimer=null),!0){case/^1[0-9]{2}$/.test(o.status_code):break;case/^2[0-9]{2}$/.test(o.status_code):{if(this._registering=!1,!o.hasHeader("Contact")){si.debug("no Contact header in response to REGISTER, response ignored");break}const t=o.headers.Contact.reduce((d,g)=>d.concat(g.parsed),[]);let r=t.find(d=>this._sipInstance===d.getParam("+sip.instance")&&this._reg_id===parseInt(d.getParam("reg-id")));if(r||(r=t.find(d=>d.uri.user===this._ua.contact.uri.user)),!r){si.debug("no Contact header pointing to us, response ignored");break}let _=r.getParam("expires");!_&&o.hasHeader("expires")&&(_=o.getHeader("expires")),_||(_=this._expires),_=Number(_),_<Aa&&(_=Aa);const h=_>64?_*1e3/2+Math.floor((_/2-32)*1e3*Math.random()):_*1e3-5e3;this._registrationTimer=setTimeout(()=>{this._registrationTimer=null,this._ua.listeners("registrationExpiring").length===0?this.register():this._ua.emit("registrationExpiring")},h),r.hasParam("temp-gruu")&&(this._ua.contact.temp_gruu=r.getParam("temp-gruu").replace(/"/g,"")),r.hasParam("pub-gruu")&&(this._ua.contact.pub_gruu=r.getParam("pub-gruu").replace(/"/g,"")),this._registered||(this._registered=!0,this._ua.registered({response:o}));break}case/^423$/.test(o.status_code):{o.hasHeader("min-expires")?(this._expires=Number(o.getHeader("min-expires")),this._expires<Aa&&(this._expires=Aa),this.register()):(si.debug("423 response received for REGISTER without Min-Expires"),this._registrationFailure(o,L.causes.SIP_FAILURE_CODE));break}default:{const t=Ts.sipErrorCause(o.status_code);this._registrationFailure(o,t)}}}});this._registering=!0,a.send()}unregister(s={}){if(!this._registered){si.debug("already unregistered");return}this._registered=!1,this._registrationTimer!==null&&(clearTimeout(this._registrationTimer),this._registrationTimer=null);const n=this._extraHeaders.slice();s.all?n.push(`Contact: *${this._extraContactParams}`):n.push(`Contact: ${this._contact}${this._extraContactParams}>${this._extra_contact};expires=0`),n.push("Expires: 0");const a=new Rt.OutgoingRequest(L.REGISTER,this._registrar,this._ua,{to_uri:this._to_uri,call_id:this._call_id,cseq:this._cseq+=1},n);new aa(this._ua,a,{onRequestTimeout:()=>{this._unregistered(null,L.causes.REQUEST_TIMEOUT)},onTransportError:()=>{this._unregistered(null,L.causes.CONNECTION_ERROR)},onAuthenticated:()=>{this._cseq+=1},onReceiveResponse:t=>{switch(!0){case/^1[0-9]{2}$/.test(t.status_code):break;case/^2[0-9]{2}$/.test(t.status_code):this._unregistered(t);break;default:{const r=Ts.sipErrorCause(t.status_code);this._unregistered(t,r)}}}}).send()}close(){this._registered&&this.unregister()}onTransportClosed(){this._registering=!1,this._registrationTimer!==null&&(clearTimeout(this._registrationTimer),this._registrationTimer=null),this._registered&&(this._registered=!1,this._ua.unregistered({}))}_registrationFailure(s,n){this._registering=!1,this._ua.registrationFailed({response:s||null,cause:n}),this._registered&&(this._registered=!1,this._ua.unregistered({response:s||null,cause:n}))}_unregistered(s,n){this._registering=!1,this._registered=!1,this._ua.unregistered({response:s||null,cause:n||null})}}const rt=console,Ps={STATUS_INIT:0,STATUS_READY:1,STATUS_USER_CLOSED:2,STATUS_NOT_READY:3,CONFIGURATION_ERROR:1,NETWORK_ERROR:2},yT=oT;class AT extends yT{constructor(s){super(s),this._msrp_sessions=[],this._transactions={nist:{},nict:{},ist:{},ict:{}},this._janus_sessions=[],this._registrator=new ST(this)}call(s,n){return super.call(s,n)}joinVideoCall(s,n,a){rt.debug("call()");const o=new Sa(this);return o.configureMedia({audio:!0,video:!0}),o.connect(s,n,a),o}_loadConfig(s){try{ti.load(this._configuration,s)}catch(o){throw o}this._configuration.display_name===0&&(this._configuration.display_name="0"),this._configuration.instance_id||(this._configuration.instance_id=Ts.newUUID()),this._configuration.jssip_id=Ts.createRandomToken(5);const n=this._configuration.uri.clone();n.user=null,this._configuration.hostport_params=n.toString().replace(/^sip:/i,"");try{this._transport=new Pg(this._configuration.sockets,{max_interval:this._configuration.connection_recovery_max_interval,min_interval:this._configuration.connection_recovery_min_interval}),this._transport.onconnecting=CT.bind(this),this._transport.onconnect=RT.bind(this),this._transport.ondisconnect=IT.bind(this),this._transport.ondata=bT.bind(this)}catch(o){throw rt.warn(o),new Ds.ConfigurationError("sockets",this._configuration.sockets)}if(delete this._configuration.sockets,this._configuration.authorization_user||(this._configuration.authorization_user=this._configuration.uri.user),!this._configuration.registrar_server){const o=this._configuration.uri.clone();o.user=null,o.clearParams(),o.clearHeaders(),this._configuration.registrar_server=o}this._configuration.no_answer_timeout*=1e3,this._configuration.contact_uri?this._configuration.via_host=this._configuration.contact_uri.host:this._configuration.contact_uri=new jn("sip",Ts.createRandomToken(8),this._configuration.via_host,null,{transport:"ws"}),this._contact={pub_gruu:null,temp_gruu:null,uri:this._configuration.contact_uri,toString(o={}){const t=o.anonymous||null,r=o.outbound||null;let _="<";return t?_+=this.temp_gruu||"sip:anonymous@anonymous.invalid;transport=ws":_+=this.pub_gruu||this.uri.toString(),r&&(t?!this.temp_gruu:!this.pub_gruu)&&(_+=";ob"),_+=">",_}};const a=["authorization_user","password","realm","ha1","authorization_jwt","display_name","register"];for(const o in this._configuration)Object.prototype.hasOwnProperty.call(this._configuration,o)&&(a.indexOf(o)!==-1?Object.defineProperty(this._configuration,o,{writable:!0,configurable:!1}):Object.defineProperty(this._configuration,o,{writable:!1,configurable:!1}));rt.debug("configuration parameters after validation:");for(const o in this._configuration)if(Object.prototype.hasOwnProperty.call(ti.settings,o))switch(o){case"uri":case"registrar_server":rt.debug(`- ${o}: ${this._configuration[o]}`);break;case"password":case"ha1":case"authorization_jwt":rt.debug(`- ${o}: NOT SHOWN`);break;default:rt.debug(`- ${o}: ${JSON.stringify(this._configuration[o])}`)}}newMSRPSession(s,n){s.on("msgHistoryUpdate",a=>{console.log(a)}),this._msrp_sessions[s.id]=s,this.emit("newMSRPSession",n)}newJanusSession(s,n){this._janus_sessions[s.id]=s,this.emit("newJanusSession",n)}destroyMSRPSession(s){delete this._msrp_sessions[s.id]}destroyJanusSession(s){delete this._janus_sessions[s.id]}receiveRequest(s){var t,r,_,h;const n=s.method;if(console.log("-----------"),s.ruri.user!==this._configuration.uri.user&&s.ruri.user!==this._contact.uri.user){rt.debug("Request-URI does not point to us"),s.method!==De.ACK&&s.reply_sl(404);return}if(s.ruri.scheme===De.SIPS){s.reply_sl(416);return}if($t.checkTransaction(this,s))return;if(n===De.INVITE?new $t.InviteServerTransaction(this,this._transport,s):n!==De.ACK&&n!==De.CANCEL&&new $t.NonInviteServerTransaction(this,this._transport,s),n===De.OPTIONS){if(this.listeners("newOptions").length===0){s.reply(200);return}new yf.Options(this).init_incoming(s)}else if(n===De.MESSAGE){if(this.listeners("newMessage").length===0){s.reply(405);return}new yf.Message(this).init_incoming(s)}else if(n===De.INVITE&&!s.to_tag&&this.listeners("newRTCSession").length===0){s.reply(405);return}let a,o;if(s.to_tag)if(a=this._findDialog(s.call_id,s.from_tag,s.to_tag),a)a.receiveRequest(s);else if(n===De.NOTIFY)if(o=this._findSession(s),o)o.receiveRequest(s);else{if(s.body)try{const d=JSON.parse(s.body)||{};(r=(t=d.plugindata)==null?void 0:t.data)!=null&&r.publishers&&Object.values(this._janus_sessions)[0].receivePublishers(d),(h=(_=d.plugindata)==null?void 0:_.data)!=null&&h.unpublished&&Object.values(this._janus_sessions)[0].receiveUnpublished(d.plugindata.data.unpublished)}catch(d){console.error(d)}s.reply(200)}else n!==De.ACK&&s.reply(481);else switch(n){case De.INVITE:if(window.RTCPeerConnection)if(s.hasHeader("replaces")){const d=s.replaces;a=this._findDialog(d.call_id,d.from_tag,d.to_tag),a?(o=a.owner,o.isEnded()?s.reply(603):o.receiveRequest(s)):s.reply(481)}else s.body.search(/MSRP/ig)>-1?(o=new Su(this),o.init_incoming(s)):s.body.search(/JANUS/ig)>-1||(o=new Tg(this),o.init_incoming(s));else rt.warn("INVITE received but WebRTC is not supported"),s.reply(488);break;case De.BYE:s.reply(481);break;case De.CANCEL:o=this._findSession(s),o?o.receiveRequest(s):rt.debug("received CANCEL request for a non existent session");break;case De.ACK:break;case De.NOTIFY:this.emit("sipEvent",{event:s.event,request:s}),s.reply(200);break;default:s.reply(405);break}}startMSRP(s,n){rt.debug("startMSRP()",n);const a=new Su(this);return a.connect(s),a}startJanus(s,n){rt.debug("startJanus()",n);const a=new Su(this);return a.connect(s),a}terminateMSRPSessions(s){rt.debug("terminateSessions()");for(const n in this._msrp_sessions)this._msrp_sessions[n].isEnded()||this._msrp_sessions[n].terminate(s)}terminateJanusSessions(s){rt.debug("terminateSessions()");for(const n in this._janus_sessions)this._janus_sessions[n].isEnded()||this._janus_sessions[n].terminate(s)}enableJanusAudio(s){rt.debug("enableJanusAudio()");for(const n in this._janus_sessions)this._janus_sessions[n].isEnded()||(s?this._janus_sessions[n].startAudio():this._janus_sessions[n].stopAudio())}enableJanusVideo(s){rt.debug("enableJanusVideo()");for(const n in this._janus_sessions)this._janus_sessions[n].isEnded()||(s?this._janus_sessions[n].startVideo():this._janus_sessions[n].stopVideo())}stop(){if(rt.debug("stop()"),this._dynConfiguration={},this._status===Ps.STATUS_USER_CLOSED){rt.debug("UA already closed");return}this._registrator.close();const s=Object.keys(this._sessions).length;for(const a in this._sessions)if(Object.prototype.hasOwnProperty.call(this._sessions,a)){rt.debug(`closing session ${a}`);try{this._sessions[a].terminate()}catch(o){console.error(o)}}for(const a in this._msrp_sessions)if(Object.prototype.hasOwnProperty.call(this._msrp_sessions,a)){rt.debug(`closing session ${a}`);try{this._msrp_sessions[a].terminate()}catch(o){console.error(o)}}for(const a in this._janus_sessions)if(Object.prototype.hasOwnProperty.call(this._janus_sessions,a)){rt.debug(`closing session ${a}`);try{this._janus_sessions[a].terminate()}catch(o){console.error(o)}}for(const a in this._applicants)if(Object.prototype.hasOwnProperty.call(this._applicants,a))try{this._applicants[a].close()}catch(o){console.error(o)}this._status=Ps.STATUS_USER_CLOSED,Object.keys(this._transactions.nict).length+Object.keys(this._transactions.nist).length+Object.keys(this._transactions.ict).length+Object.keys(this._transactions.ist).length===0&&s===0?this._transport.disconnect():this._closeTimer=setTimeout(()=>{this._closeTimer=null,this._transport.disconnect()},2e3)}}function CT(c){this.emit("connecting",c)}function RT(c){this._status!==Ps.STATUS_USER_CLOSED&&(this._status=Ps.STATUS_READY,this._error=null,this.emit("connected",c),this._dynConfiguration.register&&this._registrator.register())}function IT(c){const s=["nict","ict","nist","ist"];for(const n of s)for(const a in this._transactions[n])Object.prototype.hasOwnProperty.call(this._transactions[n],a)&&this._transactions[n][a].onTransportError();this.emit("disconnected",c),this._registrator.onTransportClosed(),this._status!==Ps.STATUS_USER_CLOSED&&(this._status=Ps.STATUS_NOT_READY,this._error=Ps.NETWORK_ERROR)}function bT(c){console.log("onTransportData",c);const s=c.transport;let n=c.message;if(n=ET.parseMessage(n,this),!n){console.log("if 1 return");return}if(this._status===Ps.STATUS_USER_CLOSED&&n instanceof en.IncomingRequest){console.log("if 2 return");return}if(!Jg(n,this,s)){console.log("if 3 return");return}if(console.log("onTransportData message",n),console.log("onTransportData instanceof",n instanceof en.IncomingRequest),n instanceof en.IncomingRequest)n.transport=s,console.log("onTransportData receiveRequest"),this.receiveRequest(n);else if(n instanceof en.IncomingResponse){let a;switch(n.method){case De.INVITE:a=this._transactions.ict[n.via_branch],a&&a.receiveResponse(n);break;case De.ACK:break;default:a=this._transactions.nict[n.via_branch],a&&a.receiveResponse(n);break}}}const wT=["roomId","_audioMuted","_cancel_reason","_contact","direction","_end_time","_eventsCount","_from_tag","_id","_is_canceled","_is_confirmed","_late_sdp","_localHold","_videoMuted","status","start_time","_remote_identity","audioTag","isOnHold","localMuted","autoAnswer"],OT=["_cancel_reason","_contact","direction","_end_time","_eventsCount","_from_tag","_id","_is_canceled","_is_confirmed","_late_sdp","status","start_time","_remote_identity","target_addr"];function gh(c){const s={};return wT.forEach(n=>{c[n]!==void 0&&(s[n]=c[n])}),s.localHold=c._localHold,s}function Th(c){const s={};return OT.forEach(n=>{c[n]!==void 0&&(s[n]=c[n])}),s}function vh(c,s){const n=new AudioContext,a=n.createMediaStreamSource(c),o=n.createMediaStreamDestination(),t=n.createGain();return a.connect(t),t.connect(o),t.gain.value=s,o.stream}function DT(c,s,n,a){const o=document.createElement("audio");o.id=s._id,o.className="audioTag",o.srcObject=c,Eh()||(o.setSinkId(n),o.volume=a),o.play(),s.audioTag=o}function NT(c){if(c&&typeof c.log=="function"&&typeof c.warn=="function"&&typeof c.error=="function")return!0}function Eh(){return/Mobi|Android|iPhone/i.test(navigator.userAgent)}const UT=at,xT=Kt(),Ot=new UT("WebSocketInterface");var PT=class{constructor(s){Ot.debug('new() [url:"%s"]',s),this._url=s,this._sip_uri=null,this._via_transport=null,this._ws=null;const n=xT.parse(s,"absoluteURI");if(n===-1)throw Ot.warn(`invalid WebSocket URI: ${s}`),new TypeError(`Invalid argument: ${s}`);if(n.scheme!=="wss"&&n.scheme!=="ws")throw Ot.warn(`invalid WebSocket URI scheme: ${n.scheme}`),new TypeError(`Invalid argument: ${s}`);this._sip_uri=`sip:${n.host}${n.port?`:${n.port}`:""};transport=ws`,this._via_transport=n.scheme.toUpperCase()}get via_transport(){return this._via_transport}set via_transport(s){this._via_transport=s.toUpperCase()}get sip_uri(){return this._sip_uri}get url(){return this._url}connect(){if(Ot.debug("connect()"),this.isConnected()){Ot.debug(`WebSocket ${this._url} is already connected`);return}else if(this.isConnecting()){Ot.debug(`WebSocket ${this._url} is connecting`);return}this._ws&&this.disconnect(),Ot.debug(`connecting to WebSocket ${this._url}`);try{this._ws=new WebSocket(this._url,"sip"),this._ws.binaryType="arraybuffer",this._ws.onopen=this._onOpen.bind(this),this._ws.onclose=this._onClose.bind(this),this._ws.onmessage=this._onMessage.bind(this),this._ws.onerror=this._onError.bind(this)}catch(s){this._onError(s)}}disconnect(){Ot.debug("disconnect()"),this._ws&&(this._ws.onopen=()=>{},this._ws.onclose=()=>{},this._ws.onmessage=()=>{},this._ws.onerror=()=>{},this._ws.close(),this._ws=null)}send(s){return Ot.debug("send()"),this.isConnected()?(this._ws.send(s),!0):(Ot.warn("unable to send message, WebSocket is not open"),!1)}isConnected(){return this._ws&&this._ws.readyState===this._ws.OPEN}isConnecting(){return this._ws&&this._ws.readyState===this._ws.CONNECTING}_onOpen(){Ot.debug(`WebSocket ${this._url} connected`),this.onconnect()}_onClose({wasClean:s,code:n,reason:a}){Ot.debug(`WebSocket ${this._url} closed`),s===!1&&Ot.debug("WebSocket abrupt disconnection"),this.ondisconnect(!s,n,a)}_onMessage({data:s}){Ot.debug("received WebSocket message"),this.ondata(s)}_onError(s){Ot.warn(`WebSocket ${this._url} error: `,s)}};const Au=Tl,kT=De,MT=gs,LT=mt(),$T=dh,HT=ds(),FT=qo(),jT=Kt(),VT=PT;Un("JsSIP")("version %s",Au.version);var qT={C:kT,Exceptions:MT,Utils:LT,UA:$T,URI:HT,NameAddrHeader:FT,WebSocketInterface:VT,Grammar:jT,debug:Un,get name(){return Au.title},get version(){return Au.version}};const BT=xe(qT);function Cu(c){return c<10?`0${c}`:`${c}`}function GT(c){let s=c.hours||0,n=c.minutes||0,a=c.seconds||0;a++,a===60&&(a=0,n++,n===60&&(n=0,s++));const o=`${Cu(s)}:${Cu(n)}:${Cu(a)}`;return{seconds:a,minutes:n,hours:s,formatted:o}}var St={};St.forEach=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)if(o in c){const t=Promise.resolve(c[o]).then(r=>s.call(n||globalThis,r,o,c));a.push(t)}await Promise.all(a)},St.forEachSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)a in c&&await s.call(n||globalThis,await c[a],a,c)},St.map=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)o in c&&(a[o]=Promise.resolve(c[o]).then(t=>s.call(n||globalThis,t,o,c)));return Promise.all(a)},St.mapSeries=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)o in c&&(a[o]=await s.call(n||globalThis,await c[o],o,c));return a},St.find=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a();let t=1;for(let r=0;r<c.length;r++){const _=h=>{h?a(c[r]):t===c.length&&a(),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.findSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(await s.call(n||globalThis,await c[a],a,c))return c[a]},St.findIndex=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a(-1);let t=1;for(let r=0;r<c.length;r++){const _=h=>{h?a(r):t===c.length&&a(-1),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.findIndexSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(await s.call(n||globalThis,await c[a],a,c))return a},St.some=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a(!1);let t=1;for(let r=0;r<c.length;r++){if(!(r in c)){t++;continue}const _=h=>{h?a(!0):t===c.length&&a(!1),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.someSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(a in c&&await s.call(n||globalThis,await c[a],a,c))return!0;return!1},St.every=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a(!0);let t=1;for(let r=0;r<c.length;r++){if(!(r in c)){t++;continue}const _=h=>{h?t===c.length&&a(!0):a(!1),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.everySeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(a in c&&!await s.call(n||globalThis,await c[a],a,c))return!1;return!0},St.filter=(c,s,n)=>new Promise(async(a,o)=>{const t=[];for(let _=0;_<c.length;_++)_ in c&&(t[_]=Promise.resolve(c[_]).then(h=>s.call(n||globalThis,h,_,c)).catch(o));const r=[];for(let _=0;_<t.length;_++)await t[_]&&r.push(await c[_]);a(r)}),St.filterSeries=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)o in c&&await s.call(n||globalThis,await c[o],o,c)&&a.push(await c[o]);return a},St.reduce=async(c,s,n)=>{if(c.length===0&&n===void 0)throw TypeError("Reduce of empty array with no initial value");let a,o;for(n!==void 0?(o=n,a=0):(o=c[0],a=1),a;a<c.length;a++)a in c&&(o=await s(await o,await c[a],a,c));return o};var Sh={};(function(c){const s=St;Object.keys(s).forEach(n=>{const a=n.charAt(0).toUpperCase()+n.slice(1);c[`async${a}`]=async function(...o){return s[n](this,...o)}})})(Sh);var WT=Object.assign(St,{instanceMethods:Sh});const ln=new AudioContext,zt={NEW_CALL:"new_call",CALL_CONFIRMED:"confirmed",CALL_FAILED:"failed",CALL_PROGRESS:"progress",CALL_ENDED:"ended"};var KT={exports:{}};(function(c){var s=function(n){var a=Object.prototype,o=a.hasOwnProperty,t=Object.defineProperty||function(ee,Y,G){ee[Y]=G.value},r,_=typeof Symbol=="function"?Symbol:{},h=_.iterator||"@@iterator",d=_.asyncIterator||"@@asyncIterator",g=_.toStringTag||"@@toStringTag";function T(ee,Y,G){return Object.defineProperty(ee,Y,{value:G,enumerable:!0,configurable:!0,writable:!0}),ee[Y]}try{T({},"")}catch{T=function(Y,G,re){return Y[G]=re}}function E(ee,Y,G,re){var ge=Y&&Y.prototype instanceof oe?Y:oe,be=Object.create(ge.prototype),ke=new x(re||[]);return t(be,"_invoke",{value:J(ee,G,ke)}),be}n.wrap=E;function y(ee,Y,G){try{return{type:"normal",arg:ee.call(Y,G)}}catch(re){return{type:"throw",arg:re}}}var A="suspendedStart",C="suspendedYield",I="executing",H="completed",$={};function oe(){}function N(){}function j(){}var Se={};T(Se,h,function(){return this});var ve=Object.getPrototypeOf,k=ve&&ve(ve(se([])));k&&k!==a&&o.call(k,h)&&(Se=k);var D=j.prototype=oe.prototype=Object.create(Se);N.prototype=j,t(D,"constructor",{value:j,configurable:!0}),t(j,"constructor",{value:N,configurable:!0}),N.displayName=T(j,g,"GeneratorFunction");function B(ee){["next","throw","return"].forEach(function(Y){T(ee,Y,function(G){return this._invoke(Y,G)})})}n.isGeneratorFunction=function(ee){var Y=typeof ee=="function"&&ee.constructor;return Y?Y===N||(Y.displayName||Y.name)==="GeneratorFunction":!1},n.mark=function(ee){return Object.setPrototypeOf?Object.setPrototypeOf(ee,j):(ee.__proto__=j,T(ee,g,"GeneratorFunction")),ee.prototype=Object.create(D),ee},n.awrap=function(ee){return{__await:ee}};function q(ee,Y){function G(be,ke,Re,le){var ye=y(ee[be],ee,ke);if(ye.type==="throw")le(ye.arg);else{var lt=ye.arg,ot=lt.value;return ot&&typeof ot=="object"&&o.call(ot,"__await")?Y.resolve(ot.__await).then(function(Ee){G("next",Ee,Re,le)},function(Ee){G("throw",Ee,Re,le)}):Y.resolve(ot).then(function(Ee){lt.value=Ee,Re(lt)},function(Ee){return G("throw",Ee,Re,le)})}}var re;function ge(be,ke){function Re(){return new Y(function(le,ye){G(be,ke,le,ye)})}return re=re?re.then(Re,Re):Re()}t(this,"_invoke",{value:ge})}B(q.prototype),T(q.prototype,d,function(){return this}),n.AsyncIterator=q,n.async=function(ee,Y,G,re,ge){ge===void 0&&(ge=Promise);var be=new q(E(ee,Y,G,re),ge);return n.isGeneratorFunction(Y)?be:be.next().then(function(ke){return ke.done?ke.value:be.next()})};function J(ee,Y,G){var re=A;return function(be,ke){if(re===I)throw new Error("Generator is already running");if(re===H){if(be==="throw")throw ke;return it()}for(G.method=be,G.arg=ke;;){var Re=G.delegate;if(Re){var le=F(Re,G);if(le){if(le===$)continue;return le}}if(G.method==="next")G.sent=G._sent=G.arg;else if(G.method==="throw"){if(re===A)throw re=H,G.arg;G.dispatchException(G.arg)}else G.method==="return"&&G.abrupt("return",G.arg);re=I;var ye=y(ee,Y,G);if(ye.type==="normal"){if(re=G.done?H:C,ye.arg===$)continue;return{value:ye.arg,done:G.done}}else ye.type==="throw"&&(re=H,G.method="throw",G.arg=ye.arg)}}}function F(ee,Y){var G=Y.method,re=ee.iterator[G];if(re===r)return Y.delegate=null,G==="throw"&&ee.iterator.return&&(Y.method="return",Y.arg=r,F(ee,Y),Y.method==="throw")||G!=="return"&&(Y.method="throw",Y.arg=new TypeError("The iterator does not provide a '"+G+"' method")),$;var ge=y(re,ee.iterator,Y.arg);if(ge.type==="throw")return Y.method="throw",Y.arg=ge.arg,Y.delegate=null,$;var be=ge.arg;if(!be)return Y.method="throw",Y.arg=new TypeError("iterator result is not an object"),Y.delegate=null,$;if(be.done)Y[ee.resultName]=be.value,Y.next=ee.nextLoc,Y.method!=="return"&&(Y.method="next",Y.arg=r);else return be;return Y.delegate=null,$}B(D),T(D,g,"Generator"),T(D,h,function(){return this}),T(D,"toString",function(){return"[object Generator]"});function Z(ee){var Y={tryLoc:ee[0]};1 in ee&&(Y.catchLoc=ee[1]),2 in ee&&(Y.finallyLoc=ee[2],Y.afterLoc=ee[3]),this.tryEntries.push(Y)}function U(ee){var Y=ee.completion||{};Y.type="normal",delete Y.arg,ee.completion=Y}function x(ee){this.tryEntries=[{tryLoc:"root"}],ee.forEach(Z,this),this.reset(!0)}n.keys=function(ee){var Y=Object(ee),G=[];for(var re in Y)G.push(re);return G.reverse(),function ge(){for(;G.length;){var be=G.pop();if(be in Y)return ge.value=be,ge.done=!1,ge}return ge.done=!0,ge}};function se(ee){if(ee){var Y=ee[h];if(Y)return Y.call(ee);if(typeof ee.next=="function")return ee;if(!isNaN(ee.length)){var G=-1,re=function ge(){for(;++G<ee.length;)if(o.call(ee,G))return ge.value=ee[G],ge.done=!1,ge;return ge.value=r,ge.done=!0,ge};return re.next=re}}return{next:it}}n.values=se;function it(){return{value:r,done:!0}}return x.prototype={constructor:x,reset:function(ee){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(U),!ee)for(var Y in this)Y.charAt(0)==="t"&&o.call(this,Y)&&!isNaN(+Y.slice(1))&&(this[Y]=r)},stop:function(){this.done=!0;var ee=this.tryEntries[0],Y=ee.completion;if(Y.type==="throw")throw Y.arg;return this.rval},dispatchException:function(ee){if(this.done)throw ee;var Y=this;function G(le,ye){return be.type="throw",be.arg=ee,Y.next=le,ye&&(Y.method="next",Y.arg=r),!!ye}for(var re=this.tryEntries.length-1;re>=0;--re){var ge=this.tryEntries[re],be=ge.completion;if(ge.tryLoc==="root")return G("end");if(ge.tryLoc<=this.prev){var ke=o.call(ge,"catchLoc"),Re=o.call(ge,"finallyLoc");if(ke&&Re){if(this.prev<ge.catchLoc)return G(ge.catchLoc,!0);if(this.prev<ge.finallyLoc)return G(ge.finallyLoc)}else if(ke){if(this.prev<ge.catchLoc)return G(ge.catchLoc,!0)}else if(Re){if(this.prev<ge.finallyLoc)return G(ge.finallyLoc)}else throw new Error("try statement without catch or finally")}}},abrupt:function(ee,Y){for(var G=this.tryEntries.length-1;G>=0;--G){var re=this.tryEntries[G];if(re.tryLoc<=this.prev&&o.call(re,"finallyLoc")&&this.prev<re.finallyLoc){var ge=re;break}}ge&&(ee==="break"||ee==="continue")&&ge.tryLoc<=Y&&Y<=ge.finallyLoc&&(ge=null);var be=ge?ge.completion:{};return be.type=ee,be.arg=Y,ge?(this.method="next",this.next=ge.finallyLoc,$):this.complete(be)},complete:function(ee,Y){if(ee.type==="throw")throw ee.arg;return ee.type==="break"||ee.type==="continue"?this.next=ee.arg:ee.type==="return"?(this.rval=this.arg=ee.arg,this.method="return",this.next="end"):ee.type==="normal"&&Y&&(this.next=Y),$},finish:function(ee){for(var Y=this.tryEntries.length-1;Y>=0;--Y){var G=this.tryEntries[Y];if(G.finallyLoc===ee)return this.complete(G.completion,G.afterLoc),U(G),$}},catch:function(ee){for(var Y=this.tryEntries.length-1;Y>=0;--Y){var G=this.tryEntries[Y];if(G.tryLoc===ee){var re=G.completion;if(re.type==="throw"){var ge=re.arg;U(G)}return ge}}throw new Error("illegal catch attempt")},delegateYield:function(ee,Y,G){return this.delegate={iterator:se(ee),resultName:Y,nextLoc:G},this.method==="next"&&(this.arg=r),$}},n}(c.exports);try{regeneratorRuntime=s}catch{typeof globalThis=="object"?globalThis.regeneratorRuntime=s:Function("r","regeneratorRuntime = r")(s)}})(KT);var yh={exports:{}};(function(c){(function(s,n){c.exports?c.exports=n():s.log=n()})(Gt,function(){var s=function(){},n="undefined",a=typeof window!==n&&typeof window.navigator!==n&&/Trident\/|MSIE /.test(window.navigator.userAgent),o=["trace","debug","info","warn","error"];function t(C,I){var H=C[I];if(typeof H.bind=="function")return H.bind(C);try{return Function.prototype.bind.call(H,C)}catch{return function(){return Function.prototype.apply.apply(H,[C,arguments])}}}function r(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function _(C){return C==="debug"&&(C="log"),typeof console===n?!1:C==="trace"&&a?r:console[C]!==void 0?t(console,C):console.log!==void 0?t(console,"log"):s}function h(C,I){for(var H=0;H<o.length;H++){var $=o[H];this[$]=H<C?s:this.methodFactory($,C,I)}this.log=this.debug}function d(C,I,H){return function(){typeof console!==n&&(h.call(this,I,H),this[C].apply(this,arguments))}}function g(C,I,H){return _(C)||d.apply(this,arguments)}function T(C,I,H){var $=this,oe;I=I??"WARN";var N="loglevel";typeof C=="string"?N+=":"+C:typeof C=="symbol"&&(N=void 0);function j(D){var B=(o[D]||"silent").toUpperCase();if(!(typeof window===n||!N)){try{window.localStorage[N]=B;return}catch{}try{window.document.cookie=encodeURIComponent(N)+"="+B+";"}catch{}}}function Se(){var D;if(!(typeof window===n||!N)){try{D=window.localStorage[N]}catch{}if(typeof D===n)try{var B=window.document.cookie,q=B.indexOf(encodeURIComponent(N)+"=");q!==-1&&(D=/^([^;]+)/.exec(B.slice(q))[1])}catch{}return $.levels[D]===void 0&&(D=void 0),D}}function ve(){if(!(typeof window===n||!N)){try{window.localStorage.removeItem(N);return}catch{}try{window.document.cookie=encodeURIComponent(N)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch{}}}$.name=C,$.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},$.methodFactory=H||g,$.getLevel=function(){return oe},$.setLevel=function(D,B){if(typeof D=="string"&&$.levels[D.toUpperCase()]!==void 0&&(D=$.levels[D.toUpperCase()]),typeof D=="number"&&D>=0&&D<=$.levels.SILENT){if(oe=D,B!==!1&&j(D),h.call($,D,C),typeof console===n&&D<$.levels.SILENT)return"No console available for logging"}else throw"log.setLevel() called with invalid level: "+D},$.setDefaultLevel=function(D){I=D,Se()||$.setLevel(D,!1)},$.resetLevel=function(){$.setLevel(I,!1),ve()},$.enableAll=function(D){$.setLevel($.levels.TRACE,D)},$.disableAll=function(D){$.setLevel($.levels.SILENT,D)};var k=Se();k==null&&(k=I),$.setLevel(k,!1)}var E=new T,y={};E.getLogger=function(I){if(typeof I!="symbol"&&typeof I!="string"||I==="")throw new TypeError("You must supply a name when creating a logger.");var H=y[I];return H||(H=y[I]=new T(I,E.getLevel(),E.methodFactory)),H};var A=typeof window!==n?window.log:void 0;return E.noConflict=function(){return typeof window!==n&&window.log===E&&(window.log=A),E},E.getLoggers=function(){return y},E.default=E,E})})(yh);var It=yh.exports;const vs=()=>`${new Date().toISOString()} | metrics`,Es=(c,s,n)=>`${c} | ${s} | ${n}`;It.setDefaultLevel(It.levels.TRACE);const YT=c=>{It.info(Es(vs(),"log ",`set log level to ${c?"verbose":"info"}`)),It.setLevel(c?It.levels.TRACE:It.levels.INFO)},JT=c=>{const s=[...Object.keys(It.levels)];s.includes(c)?(It.info(Es(vs(),"log ",`update log level to ${c.toLowerCase()}`)),It.setLevel(c)):It.warn(Es(vs(),"log ","Incorrect log level please choose one of "),s)},Pe=(c,s,n)=>{n?It.debug(Es(vs(),c,s),n):It.debug(Es(vs(),c,s))},Ru=(c,s)=>{It.info(Es(vs(),c,s))},an=(c,s)=>{It.info(Es(vs(),c,s))},Ca=(c,s)=>{It.warn(Es(vs(),c,s))},qn=(c,s)=>{It.error(Es(vs(),c,s))};function zT(c){return Math.floor(Math.random()*c).toString()}function Ah(c,s){let n=s;return c.forEach(a=>{n=n.replace(a,"")}),n}function XT(c,s){let n="";for(let a=0;a<s;a+=1)n+=c[zT(c.length)];return n}function QT({length:c=20,useLetters:s=!0,useNumbers:n=!0,includeSymbols:a=[],excludeSymbols:o=[]}={}){let t="abcdefghijklmnopqrstuvwxyz",r="0123456789",_=[],h=[],d=[];return s&&(o.length&&(t=Ah(o,t)),h=t.split("")),n&&(o.length&&(r=Ah(o,r)),d=r.split("")),_=[...h,...d,...a],XT(_,c)}var ZT=QT;const ni=xe(ZT),ev=()=>"WebRTCMetrics",tv=()=>"5.0.3",$e={INBOUND:"inbound",OUTBOUND:"outbound"},Ss={IDLE:"idle",RUNNING:"running",MUTED:"muted"},ri={NEW:"new",CHECKING:"checking",CONNECTED:"connected",COMPLETED:"completed",DISCONNECTED:"disconnected",FAILED:"failed",CLOSED:"closed"},sv=()=>({...{delta_time_to_measure_probes_ms:0,delta_time_consumed_to_measure_ms:0,delta_KBytes_in:0,delta_KBytes_out:0,delta_kbs_in:0,delta_kbs_out:0,total_time_decoded_in:0,total_time_encoded_out:0,probes:[]}}),Ch={level_in:0,codec_id_in:"",codec_in:{mime_type:null,clock_rate:null,sdp_fmtp_line:null},delta_jitter_ms_in:0,percent_packets_lost_in:0,delta_packets_in:0,delta_packets_lost_in:0,total_packets_in:0,total_packets_lost_in:0,total_KBytes_in:0,delta_KBytes_in:0,delta_kbs_in:0,mos_in:0,mos_emodel_in:0,ssrc:"",direction:$e.INBOUND},Rh={level_out:0,codec_id_out:"",codec_out:{mime_type:null,clock_rate:null,sdp_fmtp_line:null},delta_jitter_ms_out:0,delta_rtt_ms_out:null,total_rtt_ms_out:0,total_rtt_measure_out:0,percent_packets_lost_out:0,delta_packets_out:0,delta_packets_lost_out:0,total_packets_out:0,total_packets_lost_out:0,total_KBytes_out:0,delta_KBytes_out:0,delta_kbs_out:0,timestamp_out:null,mos_out:0,mos_emodel_out:0,ssrc:"",direction:$e.OUTBOUND},Ih={codec_id_in:"",size_in:{width:null,height:null,framerate:null},codec_in:{mime_type:null,clock_rate:null},delta_jitter_ms_in:0,percent_packets_lost_in:0,delta_packets_in:0,delta_packets_lost_in:0,total_packets_in:0,total_packets_lost_in:0,total_KBytes_in:0,delta_KBytes_in:0,delta_kbs_in:0,decoder_in:null,delta_ms_decode_frame_in:0,total_frames_decoded_in:0,total_time_decoded_in:0,delta_nack_sent_in:0,delta_pli_sent_in:0,total_nack_sent_in:0,total_pli_sent_in:0,ssrc:"",direction:$e.INBOUND},bh={codec_id_out:"",size_out:{width:null,height:null,framerate:null},codec_out:{mime_type:null,clock_rate:null},delta_jitter_ms_out:0,delta_rtt_ms_out:null,total_rtt_ms_out:0,total_rtt_measure_out:0,percent_packets_lost_out:0,delta_packets_out:0,delta_packets_lost_out:0,total_packets_out:0,total_packets_lost_out:0,total_KBytes_out:0,delta_KBytes_out:0,delta_kbs_out:0,encoder_out:null,delta_ms_encode_frame_out:0,total_time_encoded_out:0,total_frames_encoded_out:0,delta_nack_received_out:0,delta_pli_received_out:0,total_nack_received_out:0,total_pli_received_out:0,limitation_out:{reason:null,durations:null,resolutionChanges:0},timestamp_out:null,ssrc:"",direction:$e.OUTBOUND},nv=c=>{const s={pname:"",call_id:"",user_id:"",timestamp:null,count:0,audio:{},video:{},network:{infrastructure:3,local_candidate_id:"",local_candidate_type:"",local_candidate_protocol:"",local_candidate_relay_protocol:"",remote_candidate_id:"",remote_candidate_type:"",remote_candidate_protocol:""},data:{total_KBytes_in:0,total_KBytes_out:0,delta_KBytes_in:0,delta_KBytes_out:0,delta_kbs_in:0,delta_kbs_out:0,delta_kbs_bandwidth_in:0,delta_kbs_bandwidth_out:0,delta_rtt_connectivity_ms:null,total_rtt_connectivity_ms:0,total_rtt_connectivity_measure:0},experimental:{time_to_measure_ms:0}};if(c){const n={...c,audio:{},video:{},data:{...c.data},network:{...c.network},experimental:{...c.experimental}};return Object.keys(c.audio).forEach(a=>{n.audio[a]={...c.audio[a]}}),Object.keys(c.video).forEach(a=>{n.video[a]={...c.video[a]}}),n}return{...s,audio:{},video:{},data:{...s.data},network:{...s.network},experimental:{...s.experimental}}},rv={refreshEvery:2e3,startAfter:0,stopAfter:-1,verbose:!1,pname:`p-${ni()}`,cid:`c-${ni()}`,uid:`u-${ni()}`,record:!1,ticket:!0},Xt={CANDIDATE_PAIR:"candidate-pair",CODEC:"codec",INBOUND_RTP:"inbound-rtp",LOCAL_CANDIDATE:"local-candidate",MEDIA_SOURCE:"media-source",OUTBOUND_RTP:"outbound-rtp",REMOTE_CANDIDATE:"remote-candidate",REMOTE_INBOUND_RTP:"remote-inbound-rtp",TRACK:"track"},V={AUDIO_LEVEL:"audioLevel",AVAILABLE_OUTGOING_BITRATE:"availableOutgoingBitrate",AVAILABLE_INCOMING_BITRATE:"availableIncomingBitrate",BYTES_RECEIVED:"bytesReceived",BYTES_SENT:"bytesSent",CANDIDATE_TYPE:"candidateType",CHANNELS:"channels",CLOCK_RATE:"clockRate",CODEC_ID:"codecId",CURRENT_ROUND_TRIP_TIME:"currentRoundTripTime",ROUND_TRIP_TIME:"roundTripTime",FRACTION_LOST:"fractionLost",FRAME_HEIGHT:"frameHeight",FRAME_WIDTH:"frameWidth",QUALITY_LIMITATION_REASON:"qualityLimitationReason",QUALITY_LIMITATION_DURATIONS:"qualityLimitationDurations",QUALITY_LIMITATION_RESOLUTION_CHANGES:"qualityLimitationResolutionChanges",ID:"id",JITTER:"jitter",KIND:"kind",MEDIA_TYPE:"mediaType",MIME_TYPE:"mimeType",LOCAL_CANDIDATE_ID:"localCandidateId",NETWORK_TYPE:"networkType",RELAY_PROTOCOL:"relayProtocol",NOMINATED:"nominated",PACKETS_LOST:"packetsLost",PACKETS_RECEIVED:"packetsReceived",PACKETS_SENT:"packetsSent",PROTOCOL:"protocol",PORT:"port",REMOTE_CANDIDATE_ID:"remoteCandidateId",REMOTE_SOURCE:"remoteSource",RESPONSES_RECEIVED:"responsesReceived",SDP_FMTP_LINE:"sdpFmtpLine",SSRC:"ssrc",SELECTED:"selected",STATE:"state",TIMESTAMP:"timestamp",TOTAL_ROUND_TRIP_TIME:"totalRoundTripTime",TOTAL_ROUND_TRIP_TIME_MEASUREMENTS:"roundTripTimeMeasurements",TYPE:"type",DECODER_IMPLEMENTATION:"decoderImplementation",ENCODER_IMPLEMENTATION:"encoderImplementation",FRAMES_DECODED:"framesDecoded",FRAMES_ENCODED:"framesEncoded",FRAMES_PER_SECOND:"framesPerSecond",TOTAL_DECODE_TIME:"totalDecodeTime",TOTAL_ENCODE_TIME:"totalEncodeTime",PLI:"pliCount",NACK:"nackCount"},P={SUCCEEDED:"succeeded",AUDIO:"audio",VIDEO:"video"},ii={ETHERNET:0,CELLULAR_5G:2,WIFI:3,CELLULAR_4G:5,CELLULAR:10},Iu={ETHERNET:"ethernet",CELLULAR_4G:"cellular",WIFI:"wifi"},ne={AUDIO:"audio",VIDEO:"video",NETWORK:"network",DATA:"data"},bu="config ",iv=(c,s={},n)=>{const a={...n,...s};return s.pname||Ca(bu,`Argument [String] 'cfg.pname' for the peerConnection name or id is missing - use generated '${n.pname}'`),s.cid||Ca(bu,`Argument [String] 'cfg.cid' for the call name or id is missing - use generated '${n.cid}'`),s.uid||Ca(bu,`Argument [String] 'cfg.uid' for the user name or id is missing - use generated '${n.uid}'`),a.pc=c,a},lv=(c={})=>{const s={...rv,...c};return s.name=ev(),s.version=tv(),s},li=(c,s,n,a=!1,o)=>{let t=c.map(r=>{if(!n)return r[s];if(!o)return r[s][n];const _=r[s][o];return _?_[n]:null});return t=t.filter(r=>a?Number.isFinite(r)&&r>0:Number.isFinite(r)),t.length===0?[]:t},ks=c=>c.reduce((s,n)=>s+n,0)/c.length,av=()=>`probe-${ni()}`,ov=()=>`coltr-${ni()}`,wh=c=>new Promise(s=>setTimeout(s,c)),wu=(c,s,n)=>{s?c.call(s,n):c(n)},We=(c,s,n,a)=>{const o=li(c,s,n,!0,a);if(o.length===0)return null;const t=o.reduce((d,g)=>d+g,0)/o.length;return t===0?null:o.map(d=>Math.abs(t-d)).reduce((d,g)=>d+g,0)/o.length*100/t},Xe=(c,s,n,a=!1,o)=>{const t=li(c,s,n,a,o);return t.length===0?null:t.reduce((r,_)=>r+_,0)/t.length},on=(c,s,n)=>li(c,s,n).reduce((o,t)=>o+t,0),Ke=(c,s,n,a)=>{const o=li(c,s,n,!0,a);return o.length===0?null:Math.min(...o)},Ye=(c,s,n,a)=>{const o=li(c,s,n,!1,a);return o.length===0?null:Math.max(...o)},yt=(c,s,n,a)=>{const o=c.slice().pop();if(!o)return null;if(!n)return o[s];if(!a)return o[s][n];const t=o[s][a];return t?t[n]:null},uv=c=>c.slice().pop(),ht=(c,s,n)=>{if(!s)return null;const a={};let o=s[P.AUDIO][c];o||(o=n===$e.INBOUND?{...Ch}:{...Rh}),a[P.AUDIO]=o;let t=s[P.VIDEO][c];return t||(t=n===$e.INBOUND?{...Ih}:{...bh}),a[P.VIDEO]=t,a},ai="exporter ",cv="2.0",Oh=(c,s,n)=>{if(!c||c.length===0)return 0;const a=c[c.length-1];if(!a)return 0;const o=a[s][n];if(o){const t=o.total_rtt_ms_out,r=o.total_rtt_measure_out;return!r||!t?Xe(c,s,"delta_rtt_ms_out",!1,n):Number(t/r)}return null},fv=(c,s)=>{if(!c||c.length===0)return 0;const n=c[c.length-1];if(!n)return 0;const a=n[s].total_rtt_connectivity_ms,o=n[s].total_rtt_connectivity_measure;return!o||!a?Xe(c,s,"delta_rtt_connectivity_ms"):Number(a/o)},hv=c=>yt(c,"network","local_candidate_type")!=="relay"?`direct/${yt(c,"network","local_candidate_protocol")}`:`turn/${yt(c,"network","local_candidate_relay_protocol")}`,_v=c=>{const s=yt(c,"network","remote_candidate_type"),n=yt(c,"network","remote_candidate_protocol");return s!=="relay"?`direct/${n}`:`turn/${n}`};class dv{constructor(s){this._start=null,this._end=null,this._cfg=s,this._referenceReport=null,this._reports=[],this._events=[]}start(){Ru(ai,"start() - start exporter...");const s=new Date;return this._start=s.toJSON(),s}stop(){Ru(ai,"stop() - stop exporter...");const s=new Date;return this._end=s.toJSON(),s}saveReferenceReport(s){this._referenceReport=s}getReferenceReport(){return this._referenceReport}addReport(s){this._cfg.ticket&&(Pe(ai,`addReport() - add report to exporter at ${s.timestamp}`),this._reports.push(s))}addCustomEvent(s){this._events.push(s)}reset(){Ru(ai,"resetReports() - reset reports"),this._reports=[],this._referenceReport=null,this._start=null,this._end=null}get ticket(){Pe(ai,"ticket() - generate ticket");const s=yt(this._reports,"audio","total_packets_lost_in"),n=yt(this._reports,"audio","total_packets_in"),a=yt(this._reports,"video","total_packets_lost_in"),o=yt(this._reports,"video","total_packets_in"),t={},r=uv(this._reports);return r&&(Object.keys(r[P.AUDIO]).forEach(_=>{const h=r[P.AUDIO][_];if(t[h.ssrc]={type:P.AUDIO,direction:h.direction},h.direction===$e.INBOUND){const d={avg:Xe(this._reports,P.AUDIO,"delta_jitter_ms_in",!1,_),min:Ke(this._reports,P.AUDIO,"delta_jitter_ms_in",_),max:Ye(this._reports,P.AUDIO,"delta_jitter_ms_in",_),volatility:We(this._reports,P.AUDIO,"delta_jitter_ms_in",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.AUDIO,"delta_kbs_in",!1,_),min:Ke(this._reports,P.AUDIO,"delta_kbs_in",_),max:Ye(this._reports,P.AUDIO,"delta_kbs_in",_),volatility:We(this._reports,P.AUDIO,"delta_kbs_in",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.AUDIO,"delta_KBytes_in",!1,_),min:Ke(this._reports,P.AUDIO,"delta_KBytes_in",_),max:Ye(this._reports,P.AUDIO,"delta_KBytes_in",_),volatility:We(this._reports,P.AUDIO,"delta_KBytes_in",_),_unit:{avg:"KB",min:"KB",max:"KB",volatility:"percent"}},E={emodel:{avg:Xe(this._reports,P.AUDIO,"mos_emodel_in",!1,_),min:Ke(this._reports,P.AUDIO,"mos_emodel_in",_),max:Ye(this._reports,P.AUDIO,"mos_emodel_in",_),volatility:We(this._reports,P.AUDIO,"mos_emodel_in",_)},effective:{avg:Xe(this._reports,P.AUDIO,"mos_in",!1,_),min:Ke(this._reports,P.AUDIO,"mos_in",_),max:Ye(this._reports,P.AUDIO,"mos_in",_),volatility:We(this._reports,P.AUDIO,"mos_in",_)},_unit:{avg:"number (1-5)",min:"number (1-5)",max:"number (1-5)",volatility:"percent"}},y=yt(this._reports,P.AUDIO,"total_packets_lost_in",_),A=yt(this._reports,P.AUDIO,"total_packets_in",_),C={lost:y,avg:Math.round((y/(y+A)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}};t[_].jitter=d,t[_].mos=E,t[_].traffic=T,t[_].bitrate=g,t[_].loss=C}else{const d={avg:Xe(this._reports,P.AUDIO,"delta_jitter_ms_out",!1,_),min:Ke(this._reports,P.AUDIO,"delta_jitter_ms_out",_),max:Ye(this._reports,P.AUDIO,"delta_jitter_ms_out",_),volatility:We(this._reports,P.AUDIO,"delta_jitter_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.AUDIO,"delta_kbs_out",!1,_),min:Ke(this._reports,P.AUDIO,"delta_kbs_out",_),max:Ye(this._reports,P.AUDIO,"delta_kbs_out",_),volatility:We(this._reports,P.AUDIO,"delta_kbs_out",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.AUDIO,"delta_KBytes_out",!1,_),min:Ke(this._reports,P.AUDIO,"delta_KBytes_out",_),max:Ye(this._reports,P.AUDIO,"delta_KBytes_out",_),volatility:We(this._reports,P.AUDIO,"delta_KBytes_out",_),_unit:{avg:"KB",min:"KB",max:"KB",bitrate:"kbs",volatility:"percent"}},E={avg:Oh(this._reports,P.AUDIO,_),min:Ke(this._reports,P.AUDIO,"delta_rtt_ms_out",_),max:Ye(this._reports,P.AUDIO,"delta_rtt_ms_out",_),volatility:We(this._reports,P.AUDIO,"delta_rtt_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},y=yt(this._reports,P.AUDIO,"total_packets_lost_out",_),A=yt(this._reports,P.AUDIO,"total_packets_out",_),C={lost:y,avg:Math.round((y/(y+A)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}},I={emodel:{avg:Xe(this._reports,P.AUDIO,"mos_emodel_out",!1,_),min:Ke(this._reports,P.AUDIO,"mos_emodel_out",_),max:Ye(this._reports,P.AUDIO,"mos_emodel_out",_),volatility:We(this._reports,P.AUDIO,"mos_emodel_out",_)},effective:{avg:Xe(this._reports,P.AUDIO,"mos_out",!1,_),min:Ke(this._reports,P.AUDIO,"mos_out",_),max:Ye(this._reports,P.AUDIO,"mos_out",_),volatility:We(this._reports,P.AUDIO,"mos_out",_)},_unit:{avg:"number (1-5)",min:"number (1-5)",max:"number (1-5)",volatility:"percent"}};t[_].jitter=d,t[_].rtt=E,t[_].traffic=T,t[_].bitrate=g,t[_].loss=C,t[_].mos=I}}),Object.keys(r[P.VIDEO]).forEach(_=>{const h=r[P.VIDEO][_];if(t[_]={type:P.VIDEO,direction:h.direction},h.direction===$e.INBOUND){const d={avg:Xe(this._reports,P.VIDEO,"delta_jitter_ms_in",!1,_),min:Ke(this._reports,P.VIDEO,"delta_jitter_ms_in",_),max:Ye(this._reports,P.VIDEO,"delta_jitter_ms_in",_),volatility:We(this._reports,P.VIDEO,"delta_jitter_ms_in",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.VIDEO,"delta_kbs_in",!1,_),min:Ke(this._reports,P.VIDEO,"delta_kbs_in",_),max:Ye(this._reports,P.VIDEO,"delta_kbs_in",_),volatility:We(this._reports,P.VIDEO,"delta_kbs_in",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.VIDEO,"delta_KBytes_in",!1,_),min:Ke(this._reports,P.VIDEO,"delta_KBytes_in",_),max:Ye(this._reports,P.VIDEO,"delta_KBytes_in",_),volatility:We(this._reports,P.VIDEO,"delta_KBytes_in",_),_unit:{avg:"KB",min:"KB",max:"KB",volatility:"percent"}},E=yt(this._reports,P.VIDEO,"total_packets_lost_in",_),y=yt(this._reports,P.VIDEO,"total_packets_in",_),A={lost:E,avg:Math.round((E/(E+y)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}};t[_].jitter=d,t[_].traffic=T,t[_].bitrate=g,t[_].loss=A}else{const d={avg:Xe(this._reports,P.VIDEO,"delta_jitter_ms_out",!1,_),min:Ke(this._reports,P.VIDEO,"delta_jitter_ms_out",_),max:Ye(this._reports,P.VIDEO,"delta_jitter_ms_out",_),volatility:We(this._reports,P.VIDEO,"delta_jitter_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.VIDEO,"delta_kbs_out",!1,_),min:Ke(this._reports,P.VIDEO,"delta_kbs_out",_),max:Ye(this._reports,P.VIDEO,"delta_kbs_out",_),volatility:We(this._reports,P.VIDEO,"delta_kbs_out",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.VIDEO,"delta_KBytes_out",!1,_),min:Ke(this._reports,P.VIDEO,"delta_KBytes_out",_),max:Ye(this._reports,P.VIDEO,"delta_KBytes_out",_),volatility:We(this._reports,P.VIDEO,"delta_KBytes_out",_),_unit:{avg:"KB",min:"KB",max:"KB",volatility:"percent"}},E={avg:Oh(this._reports,P.VIDEO,_),min:Ke(this._reports,P.VIDEO,"delta_rtt_ms_out",_),max:Ye(this._reports,P.VIDEO,"delta_rtt_ms_out",_),volatility:We(this._reports,P.VIDEO,"delta_rtt_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},y=yt(this._reports,P.VIDEO,"total_packets_lost_out",_),A=yt(this._reports,P.VIDEO,"total_packets_out",_),C={lost:y,avg:Math.round((y/(y+A)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}};t[_].jitter=d,t[_].rtt=E,t[_].traffic=T,t[_].bitrate=g,t[_].loss=C}})),{version:cv,configuration:{frequency:this._cfg.refreshEvery},started:this._start,ended:this._end,ua:{agent:navigator.userAgent,pname:this._cfg.pname,user_id:this._cfg.uid},call:{call_id:this._cfg.cid,events:this._events},details:{count:this._reports.length,reports:this._cfg.record?this._reports:[],reference:this._referenceReport||null},ssrc:t,data:{rtt:{avg:fv(this._reports,"data"),min:Ke(this._reports,"data","delta_rtt_connectivity_ms"),max:Ye(this._reports,"data","delta_rtt_connectivity_ms"),volatility:We(this._reports,"data","delta_rtt_connectivity_ms"),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},packetsLost:{audio:{in:{avg:Math.round((s/(s+n)*100||0)*100)/100}},video:{in:{avg:Math.round((a/(a+o)*100||0)*100)/100}},unit:{avg:"percent"}},bitrate:{in:{avg:Xe(this._reports,"data","delta_kbs_in"),min:Ke(this._reports,"data","delta_kbs_in"),max:Ye(this._reports,"data","delta_kbs_in"),volatility:We(this._reports,"data","delta_kbs_in")},out:{avg:Xe(this._reports,"data","delta_kbs_out"),min:Ke(this._reports,"data","delta_kbs_out"),max:Ye(this._reports,"data","delta_kbs_out"),volatility:We(this._reports,"data","delta_kbs_out")},unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},traffic:{in:{avg:Xe(this._reports,"data","delta_KBytes_in"),min:Ke(this._reports,"data","delta_KBytes_in"),max:Ye(this._reports,"data","delta_KBytes_in"),volatility:We(this._reports,"data","delta_KBytes_in")},out:{avg:Xe(this._reports,"data","delta_KBytes_out"),min:Ke(this._reports,"data","delta_KBytes_out"),max:Ye(this._reports,"data","delta_KBytes_out"),volatility:We(this._reports,"data","delta_KBytes_out")},unit:{avg:"KBytes",min:"KBytes",max:"KBytes",volatility:"percent"}},network:{localConnection:hv(this._reports),remoteConnection:_v(this._reports)}}}}updateConfig(s){this._cfg=s}getLastReport(){return this._reports.slice().pop()||null}getBeforeLastReport(){const s=this._reports.slice();return s.pop(),s.pop()||null}getReportsNumber(){return this._reports.length}}const Ms="extractor ",Dh=(c,s,n,a)=>{let o=!1;const t=a[s].total_rtt_ms_out,r=a[s].total_rtt_measure_out,_=n?n[s].total_rtt_ms_out:0,h=n?n[s].total_rtt_measure_out:0,d={rtt:null,totalRTT:t,totalRTTMeasurements:r};if(c[V.TIMESTAMP]===a[s].timestamp_out||!Object.prototype.hasOwnProperty.call(c,V.ROUND_TRIP_TIME)||Object.prototype.hasOwnProperty.call(c,V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS)&&(o=!0,Number(c[V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS])===0||Number(c[V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS])-h===r))return d;const g=1e3*Number(c[V.ROUND_TRIP_TIME]);let T=t+g,E=r+1;return o&&(T=1e3*Number(c[V.TOTAL_ROUND_TRIP_TIME])-_,E=Number(c[V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS])-h),{rtt:g,totalRTT:T,totalRTTMeasurements:E}},pv=(c,s,n,a)=>{if(!Object.prototype.hasOwnProperty.call(c,V.CURRENT_ROUND_TRIP_TIME))return{rtt:null,totalRTT:a[s].total_rtt_connectivity_ms,totalRTTMeasurements:a[s].total_rtt_connectivity_measure};const o=1e3*Number(c[V.CURRENT_ROUND_TRIP_TIME]);let t=a[s].total_rtt_connectivity_ms+o,r=a[s].total_rtt_connectivity_measure+1;return Object.prototype.hasOwnProperty.call(c,V.TOTAL_ROUND_TRIP_TIME)&&(t=1e3*Number(c[V.TOTAL_ROUND_TRIP_TIME])-(n?n[s].total_rtt_connectivity_ms:0)),Object.prototype.hasOwnProperty.call(c,V.RESPONSES_RECEIVED)&&(r=Number(c[V.RESPONSES_RECEIVED])-(n?n[s].total_rtt_connectivity_measure:0)),{rtt:o,totalRTT:t,totalRTTMeasurements:r}},Ra=(c,s,n)=>c[V.TIMESTAMP]===n[s].timestamp_out||!Object.prototype.hasOwnProperty.call(c,V.JITTER)?null:1e3*(Number(c[V.JITTER])||0),mv=(c,s)=>{if(!Object.prototype.hasOwnProperty.call(c,V.FRAMES_DECODED)||!Object.prototype.hasOwnProperty.call(c,V.TOTAL_DECODE_TIME))return{delta_ms_decode_frame:s[P.VIDEO].delta_ms_decode_frame_in,frames_decoded:s[P.VIDEO].total_frames_decoded_in,total_decode_time:s[P.VIDEO].total_time_decoded_in};const n=c[V.FRAMES_DECODED],a=c[V.TOTAL_DECODE_TIME],o=a-s[P.VIDEO].total_time_decoded_in,t=n-s[P.VIDEO].total_frames_decoded_in;return{delta_ms_decode_frame:t>0?o*1e3/t:0,frames_decoded:n,total_decode_time:a}},gv=(c,s)=>{if(!Object.prototype.hasOwnProperty.call(c,V.FRAMES_ENCODED)||!Object.prototype.hasOwnProperty.call(c,V.TOTAL_ENCODE_TIME))return{delta_ms_encode_frame:s[P.VIDEO].delta_ms_encode_frame_out,frames_encoded:s[P.VIDEO].total_frames_encoded_out,total_encode_time:s[P.VIDEO].total_time_encoded_out};const n=c[V.FRAMES_ENCODED],a=c[V.TOTAL_ENCODE_TIME],o=a-s[P.VIDEO].total_time_encoded_out,t=n-s[P.VIDEO].total_frames_encoded_out;return{delta_ms_encode_frame:t>0&&o?o*1e3/t:0,frames_encoded:n,total_encode_time:a}},Nh=(c,s,n,a)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PACKETS_SENT)||!Object.prototype.hasOwnProperty.call(c,V.BYTES_SENT))return{packetsSent:n[s].total_packets_out,packetsLost:n[s].total_packets_lost_out,bytesSent:n[s].total_KBytes_out};const o=Number(c[V.PACKETS_SENT])||0-(a?a[s].total_packets_out:0),t=o-n[s].total_packets_out,r=Number(c[V.BYTES_SENT])/1024-(a?a[s].total_KBytes_out:0),_=r-n[s].total_KBytes_out,h=c[V.TIMESTAMP]||Date.now(),d=a?a.timestamp:null;let g=n.timestamp;!g&&d&&(g=d);const T=g?h-g:0,E=T>0?_*.008*1024/T*1e3:0;return{packetsSent:o,deltaPacketsSent:t,KBytesSent:r,deltaKBytesSent:_,kbsSent:E}},Uh=(c,s,n,a)=>{let o=n[s].total_packets_lost_out,t=0,r=0;return Object.prototype.hasOwnProperty.call(c,V.PACKETS_LOST)&&(o=Number(c[V.PACKETS_LOST])||0-(a?a[s].total_packets_lost_out:0),t=o-n[s].total_packets_lost_out),Object.prototype.hasOwnProperty.call(c,V.FRACTION_LOST)&&(r=Number(100*c[V.FRACTION_LOST])),{packetsLost:o,deltaPacketsLost:t,fractionLost:r}},xh=(c,s,n,a)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PACKETS_RECEIVED)||!Object.prototype.hasOwnProperty.call(c,V.PACKETS_LOST)||!Object.prototype.hasOwnProperty.call(c,V.BYTES_RECEIVED))return{percent_packets_lost:n[s].percent_packets_lost_in,packetsReceived:n[s].total_packets_in,packetsLost:n[s].total_packets_lost_in,bytesReceived:n[s].total_KBytes_in};const o=Number(c[V.PACKETS_RECEIVED])||0-(a?a[s].total_packets_in:0),t=Number(c[V.PACKETS_LOST])||0-(a?a[s].total_packets_lost_in:0),r=t-n[s].total_packets_lost_in,_=o-n[s].total_packets_in,h=o!==n[s].total_packets_in?r*100/(r+_):0,d=Number(c[V.BYTES_RECEIVED])/1024-(a?a[s].total_KBytes_in:0),g=d-n[s].total_KBytes_in,T=c[V.TIMESTAMP]||Date.now(),E=a?a.timestamp:null;let y=n.timestamp;!y&&E&&(y=E);const A=y?T-y:0,C=A>0?g*.008*1024/A*1e3:0;return{percentPacketsLost:h,packetsReceived:o,deltaPacketsReceived:_,packetsLost:t,deltaPacketsLost:r,KBytesReceived:d,deltaKBytesReceived:g,kbsReceived:C}},Tv=c=>c[V.CANDIDATE_TYPE]!=="relay"?"":c[V.RELAY_PROTOCOL]||"",vv=c=>{if(!Object.prototype.hasOwnProperty.call(c,V.NETWORK_TYPE))return ii.WIFI;switch(c[V.NETWORK_TYPE]){case Iu.ETHERNET:return ii.ETHERNET;case Iu.CELLULAR_4G:return ii.CELLULAR_4G;case Iu.WIFI:return ii.WIFI;default:return ii.CELLULAR}},Ph=c=>!Object.prototype.hasOwnProperty.call(c,V.FRAME_HEIGHT)||!Object.prototype.hasOwnProperty.call(c,V.FRAME_WIDTH)?{width:null,height:null,framerate:null}:{width:c[V.FRAME_WIDTH]||null,height:c[V.FRAME_HEIGHT]||null,framerate:c[V.FRAMES_PER_SECOND]},Ev=c=>{const s=Object.prototype.hasOwnProperty.call(c,V.QUALITY_LIMITATION_REASON)?c[V.QUALITY_LIMITATION_REASON]:null,n=Object.prototype.hasOwnProperty.call(c,V.QUALITY_LIMITATION_RESOLUTION_CHANGES)?c[V.QUALITY_LIMITATION_RESOLUTION_CHANGES]:null,a=Object.prototype.hasOwnProperty.call(c,V.QUALITY_LIMITATION_DURATIONS)?c[V.QUALITY_LIMITATION_DURATIONS]:null;return a&&Object.keys(a).forEach(o=>{a[o]>1e3&&(a[o]=Number(a[o]/1e3))}),{reason:s,durations:a,resolutionChanges:n}},Sv=(c,s,n)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PLI)||!Object.prototype.hasOwnProperty.call(c,V.NACK))return{pliCount:s.total_pli_sent_in,nackCount:s.total_nack_sent_in,deltaPliCount:0,deltaNackCount:0};const a=(c[V.PLI]||0)-(n?n[P.VIDEO].total_pli_sent_in:0),o=(c[V.NACK]||0)-(n?n[P.VIDEO].total_nack_sent_in:0);return{pliCount:a,nackCount:o,deltaPliCount:a-s[P.VIDEO].total_pli_sent_in,deltaNackCount:o-s[P.VIDEO].total_nack_sent_in}},yv=(c,s,n)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PLI)||!Object.prototype.hasOwnProperty.call(c,V.NACK))return{pliCount:s.total_pli_received_out,nackCount:s.total_nack_received_out,deltaPliCount:0,deltaNackCount:0};const a=(c[V.PLI]||0)-(n?n[P.VIDEO].total_pli_received_out:0),o=(c[V.NACK]||0)-(n?n[P.VIDEO].total_nack_received_out:0);return{pliCount:a,nackCount:o,deltaPliCount:a-s[P.VIDEO].total_pli_received_out,deltaNackCount:o-s[P.VIDEO].total_nack_received_out}},Av=c=>({channels:c[V.CHANNELS]||null,clock_rate:c[V.CLOCK_RATE]||null,mime_type:c[V.MIME_TYPE]||null,sdp_fmtp_line:c[V.SDP_FMTP_LINE]||null}),Cv=c=>({clock_rate:c[V.CLOCK_RATE]||null,mime_type:c[V.MIME_TYPE]||null}),Rv=(c,s,n)=>{const a=(c[V.BYTES_RECEIVED]||0)/1024-(n?n.data.total_KBytes_in:0),o=(c[V.BYTES_SENT]||0)/1024-(n?n.data.total_KBytes_out:0),t=c[V.TIMESTAMP]||Date.now(),r=a-s.data.total_KBytes_in,_=o-s.data.total_KBytes_out,h=n?n.timestamp:null;let d=s.timestamp;!d&&h&&(d=h);const g=d?t-d:0,T=g>0?r*.008*1024/g*1e3:0,E=g>0?_*.008*1024/g*1e3:0;return{total_KBytes_received:a,total_KBytes_sent:o,delta_KBytes_received:r,delta_KBytes_sent:_,kbs_speed_received:T,kbs_speed_sent:E}},Iv=c=>{const s=c[V.AVAILABLE_INCOMING_BITRATE]/1024||0,n=c[V.AVAILABLE_OUTGOING_BITRATE]/1024||0;return{kbs_incoming_bandwidth:s,kbs_outgoing_bandwidth:n}},bv=(c,s,n,a)=>{if(!c)return[];switch(c[V.TYPE]){case Xt.CANDIDATE_PAIR:let o=!1;if(c[V.NOMINATED]&&c[V.STATE]===P.SUCCEEDED&&(o=!0,Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c),V.SELECTED in c&&!c[V.SELECTED]&&(o=!1)),o){const r=c[V.LOCAL_CANDIDATE_ID],_=c[V.REMOTE_CANDIDATE_ID],h=Rv(c,s,a),d=Iv(c),g=pv(c,"data",a,s);return[{type:ne.NETWORK,value:{local_candidate_id:r}},{type:ne.NETWORK,value:{remote_candidate_id:_}},{type:ne.DATA,value:{total_KBytes_in:h.total_KBytes_received}},{type:ne.DATA,value:{total_KBytes_out:h.total_KBytes_sent}},{type:ne.DATA,value:{delta_KBytes_in:h.delta_KBytes_received}},{type:ne.DATA,value:{delta_KBytes_out:h.delta_KBytes_sent}},{type:ne.DATA,value:{delta_kbs_in:h.kbs_speed_received}},{type:ne.DATA,value:{delta_kbs_out:h.kbs_speed_sent}},{type:ne.DATA,value:{delta_kbs_bandwidth_in:d.kbs_incoming_bandwidth}},{type:ne.DATA,value:{delta_kbs_bandwidth_out:d.kbs_outgoing_bandwidth}},{type:ne.DATA,value:{delta_rtt_connectivity_ms:g.rtt}},{type:ne.DATA,value:{total_rtt_connectivity_ms:g.totalRTT}},{type:ne.DATA,value:{total_rtt_connectivity_measure:g.totalRTTMeasurements}}]}break;case Xt.LOCAL_CANDIDATE:if(c[V.ID]===s.network.local_candidate_id)return[{type:ne.NETWORK,value:{infrastructure:vv(c)}},{type:ne.NETWORK,value:{local_candidate_type:c[V.CANDIDATE_TYPE]||""}},{type:ne.NETWORK,value:{local_candidate_protocol:c[V.PROTOCOL]||""}},{type:ne.NETWORK,value:{local_candidate_relay_protocol:Tv(c)}}];break;case Xt.REMOTE_CANDIDATE:if(c[V.ID]===s.network.remote_candidate_id)return[{type:ne.NETWORK,value:{remote_candidate_type:c[V.CANDIDATE_TYPE]||""}},{type:ne.NETWORK,value:{remote_candidate_protocol:c[V.PROTOCOL]||""}}];break;case Xt.INBOUND_RTP:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const r=c[V.SSRC],_=ht(r,s,$e.INBOUND);_&&(_.timestamp=s.timestamp);const h=ht(r,a,$e.INBOUND);if(h&&(h.timestamp=a.timestamp),c[V.MEDIA_TYPE]===P.AUDIO){const d=xh(c,P.AUDIO,_,h),g=Ra(c,P.AUDIO,_),T=c[V.CODEC_ID]||"";return[{ssrc:r,type:ne.AUDIO,value:{codec_id_in:T}},{ssrc:r,type:ne.AUDIO,value:{total_packets_in:d.packetsReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_in:d.deltaPacketsReceived}},{ssrc:r,type:ne.AUDIO,value:{total_packets_lost_in:d.packetsLost}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_lost_in:d.deltaPacketsLost}},{ssrc:r,type:ne.AUDIO,value:{percent_packets_lost_in:d.percentPacketsLost}},{ssrc:r,type:ne.AUDIO,value:{total_KBytes_in:d.KBytesReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_KBytes_in:d.deltaKBytesReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_kbs_in:d.kbsReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_jitter_ms_in:g}}]}if(c[V.MEDIA_TYPE]===P.VIDEO){const d=mv(c,_),g=xh(c,P.VIDEO,_,h),T=Ra(c,P.VIDEO,_),E=c[V.DECODER_IMPLEMENTATION]||null,y=c[V.CODEC_ID]||null,A=Ph(c),C=Sv(c,_,h);return[{ssrc:r,type:ne.VIDEO,value:{codec_id_in:y}},{ssrc:r,type:ne.VIDEO,value:{total_packets_in:g.packetsReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_in:g.deltaPacketsReceived}},{ssrc:r,type:ne.VIDEO,value:{total_packets_lost_in:g.packetsLost}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_lost_in:g.deltaPacketsLost}},{ssrc:r,type:ne.VIDEO,value:{percent_packets_lost_in:g.percentPacketsLost}},{ssrc:r,type:ne.VIDEO,value:{total_KBytes_in:g.KBytesReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_KBytes_in:g.deltaKBytesReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_kbs_in:g.kbsReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_jitter_ms_in:T}},{ssrc:r,type:ne.VIDEO,value:{decoder_in:E}},{ssrc:r,type:ne.VIDEO,value:{delta_ms_decode_frame_in:d.delta_ms_decode_frame}},{ssrc:r,type:ne.VIDEO,value:{total_frames_decoded_in:d.frames_decoded}},{ssrc:r,type:ne.VIDEO,value:{total_time_decoded_in:d.total_decode_time}},{ssrc:r,type:ne.VIDEO,value:{total_nack_sent_in:C.nackCount}},{ssrc:r,type:ne.VIDEO,value:{delta_nack_sent_in:C.deltaNackCount}},{ssrc:r,type:ne.VIDEO,value:{total_pli_sent_in:C.pliCount}},{ssrc:r,type:ne.VIDEO,value:{delta_pli_sent_in:C.deltaPliCount}},{ssrc:r,type:ne.VIDEO,value:{size_in:A}}]}break}case Xt.OUTBOUND_RTP:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const r=c[V.SSRC],_=ht(r,s,$e.OUTBOUND);_&&(_.timestamp=s.timestamp);const h=ht(r,a,$e.OUTBOUND);if(h&&(h.timestamp=a.timestamp),c[V.MEDIA_TYPE]===P.AUDIO){const d=c[V.CODEC_ID]||null,g=Nh(c,P.AUDIO,_,h);return[{ssrc:r,type:ne.AUDIO,value:{codec_id_out:d}},{ssrc:r,type:ne.AUDIO,value:{total_packets_out:g.packetsSent}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_out:g.deltaPacketsSent}},{ssrc:r,type:ne.AUDIO,value:{total_KBytes_out:g.KBytesSent}},{ssrc:r,type:ne.AUDIO,value:{delta_KBytes_out:g.deltaKBytesSent}},{ssrc:r,type:ne.AUDIO,value:{delta_kbs_out:g.kbsSent}}]}if(c[V.MEDIA_TYPE]===P.VIDEO){const d=c[V.ENCODER_IMPLEMENTATION]||null,g=c[V.CODEC_ID]||null,T=gv(c,_),E=Ph(c),y=Ev(c),A=yv(c,_,h),C=Nh(c,P.VIDEO,_,h);return[{ssrc:r,type:ne.VIDEO,value:{codec_id_out:g}},{ssrc:r,type:ne.VIDEO,value:{total_packets_out:C.packetsSent}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_out:C.deltaPacketsSent}},{ssrc:r,type:ne.VIDEO,value:{total_KBytes_out:C.KBytesSent}},{ssrc:r,type:ne.VIDEO,value:{delta_KBytes_out:C.deltaKBytesSent}},{ssrc:r,type:ne.VIDEO,value:{delta_kbs_out:C.kbsSent}},{ssrc:r,type:ne.VIDEO,value:{encoder_out:d}},{ssrc:r,type:ne.VIDEO,value:{delta_ms_encode_frame_out:T.delta_ms_encode_frame}},{ssrc:r,type:ne.VIDEO,value:{total_frames_encoded_out:T.frames_encoded}},{ssrc:r,type:ne.VIDEO,value:{total_time_encoded_out:T.total_encode_time}},{ssrc:r,type:ne.VIDEO,value:{total_nack_received_out:A.nackCount}},{ssrc:r,type:ne.VIDEO,value:{delta_nack_received_out:A.deltaNackCount}},{ssrc:r,type:ne.VIDEO,value:{total_pli_received_out:A.pliCount}},{ssrc:r,type:ne.VIDEO,value:{delta_pli_received_out:A.deltaPliCount}},{ssrc:r,type:ne.VIDEO,value:{size_out:E}},{ssrc:r,type:ne.VIDEO,value:{limitation_out:y}}]}break}case Xt.MEDIA_SOURCE:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);break}case Xt.TRACK:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);break}case Xt.CODEC:const t=[];return Object.keys(s[P.AUDIO]).forEach(r=>{const _=s[P.AUDIO][r];if(_.codec_id_in===c[V.ID]||_.codec_id_out===c[V.ID]){Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const h=Av(c);c[V.ID]===_.codec_id_in?t.push({ssrc:_.ssrc,type:ne.AUDIO,value:{codec_in:h}}):t.push({ssrc:_.ssrc,type:ne.AUDIO,value:{codec_out:h}})}}),Object.keys(s[P.VIDEO]).forEach(r=>{const _=s[P.VIDEO][r];if(_.codec_id_in===c[V.ID]||_.codec_id_out===c[V.ID]){Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const h=Cv(c);c[V.ID]===_.codec_id_in?t.push({ssrc:_.ssrc,type:ne.VIDEO,value:{codec_in:h}}):t.push({ssrc:_.ssrc,type:ne.VIDEO,value:{codec_out:h}})}}),t;case Xt.REMOTE_INBOUND_RTP:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const r=c[V.SSRC],_=ht(r,s,$e.OUTBOUND),h=ht(r,a,$e.OUTBOUND);if(c[V.KIND]===P.AUDIO){const d=Dh(c,P.AUDIO,h,_),g=Ra(c,P.AUDIO,_),T=Uh(c,P.AUDIO,_,h);return[{ssrc:r,type:ne.AUDIO,value:{delta_rtt_ms_out:d.rtt}},{ssrc:r,type:ne.AUDIO,value:{total_rtt_ms_out:d.totalRTT}},{ssrc:r,type:ne.AUDIO,value:{total_rtt_measure_out:d.totalRTTMeasurements}},{ssrc:r,type:ne.AUDIO,value:{delta_jitter_ms_out:g}},{ssrc:r,type:ne.AUDIO,value:{timestamp_out:c[V.TIMESTAMP]}},{ssrc:r,type:ne.AUDIO,value:{total_packets_lost_out:T.packetsLost}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_lost_out:T.deltaPacketsLost}},{ssrc:r,type:ne.AUDIO,value:{percent_packets_lost_out:T.fractionLost}}]}if(c[V.KIND]===P.VIDEO){const d=Dh(c,P.VIDEO,h,_),g=Ra(c,P.VIDEO,_),T=Uh(c,P.VIDEO,_,h);return[{ssrc:r,type:ne.VIDEO,value:{delta_rtt_ms_out:d.rtt}},{ssrc:r,type:ne.VIDEO,value:{total_rtt_ms_out:d.totalRTT}},{ssrc:r,type:ne.VIDEO,value:{total_rtt_measure_out:d.totalRTTMeasurements}},{ssrc:r,type:ne.VIDEO,value:{delta_jitter_ms_out:g}},{ssrc:r,type:ne.VIDEO,value:{timestamp_out:c[V.TIMESTAMP]}},{ssrc:r,type:ne.VIDEO,value:{total_packets_lost_out:T.packetsLost}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_lost_out:T.deltaPacketsLost}},{ssrc:r,type:ne.VIDEO,value:{percent_packets_lost_out:T.fractionLost}}]}break}}return[]},Ia=c=>c<0?1:c>100?4.5:1+.035*c+7/1e6*c*(c-60)*(100-c),wv=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.INBOUND),r=ht(o,n,$e.INBOUND),_=ht(o,a,$e.INBOUND),h=[],d=[],g=t[s].percent_packets_lost_in,T=t[s].delta_jitter_ms_in,E=r&&r[s].delta_jitter_ms_in||null,y=_&&_[s].delta_jitter_ms_in||null,A=c.data.delta_rtt_connectivity_ms,C=n&&n.data.delta_rtt_connectivity_ms||null,I=a&&a.data.delta_rtt_connectivity_ms||null;A&&h.push(A),C&&h.push(C),I&&h.push(I),T&&d.push(T),n&&E&&d.push(E),a&&y&&d.push(y);const H=h.length>0?ks(h):100,$=d.length>0?ks(d):10,oe=93.2-g,N=.18*oe*oe-27.9*oe+1126.62,j=(H+$)/2,Se=j-177.3<0?0:1,ve=.024*j+.11*(j-177.3)*Se,k=N-ve;return Ia(k)},Ov=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.OUTBOUND),r=ht(o,n,$e.OUTBOUND),_=ht(o,a,$e.OUTBOUND),h=[],d=[],g=t[s].percent_packets_lost_out,T=t[s].delta_rtt_ms_out,E=r&&r[s].delta_rtt_ms_out||null,y=_&&_[s].delta_rtt_ms_out||null,A=t[s].delta_jitter_ms_out,C=r&&r[s].delta_jitter_ms_out||null,I=_&&_[s].delta_jitter_ms_out||null,H=c.data.delta_rtt_connectivity_ms,$=n&&n.data.delta_rtt_connectivity_ms||null,oe=a&&a.data.delta_rtt_connectivity_ms||null;T?h.push(T):H&&h.push(H),E?h.push(E):$&&h.push($),y?h.push(y):oe&&h.push(oe),A&&d.push(A),n&&C&&d.push(C),a&&I&&d.push(I);const N=h.length>0?ks(h):100,j=d.length>0?ks(d):10,Se=93.2-g,ve=.18*Se*Se-27.9*Se+1126.62,k=(N+j)/2,D=k-177.3<0?0:1,B=.024*k+.11*(k-177.3)*D,q=ve-B;return Ia(q)},Dv=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.INBOUND),r=ht(o,n,$e.INBOUND),_=ht(o,a,$e.INBOUND),h=[],d=[],g=t[s].percent_packets_lost_in/100,T=t[s].delta_jitter_ms_in,E=r&&r[s].delta_jitter_ms_in||null,y=_&&_[s].delta_jitter_ms_in||null,A=c.data.delta_rtt_connectivity_ms,C=n&&n.data.delta_rtt_connectivity_ms||null,I=a&&a.data.delta_rtt_connectivity_ms||null;A&&h.push(A),C&&h.push(C),I&&h.push(I),T&&d.push(T),r&&E&&d.push(E),_&&y&&d.push(y);const H=h.length>0?ks(h):100,$=d.length>0?ks(d):10,oe=0,N=19.8,j=29.7,Se=30,ve=(H+$)/2+Se,k=ve-177.3<0?0:1,D=.024*ve+.11*(ve-177.3)*k,q=93.2-(oe+N*Math.log(1+j*g)+D);return Ia(q)},Nv=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.OUTBOUND),r=ht(o,n,$e.OUTBOUND),_=ht(o,a,$e.OUTBOUND),h=[],d=[],g=t[s].percent_packets_lost_out/100,T=t[s].delta_rtt_ms_out,E=r&&r[s].delta_rtt_ms_out||null,y=_&&_[s].delta_rtt_ms_out||null,A=t[s].delta_jitter_ms_out,C=r&&r[s].delta_jitter_ms_out||null,I=_&&_[s].delta_jitter_ms_out||null,H=c.data.delta_rtt_connectivity_ms,$=n&&n.data.delta_rtt_connectivity_ms||null,oe=a&&a.data.delta_rtt_connectivity_ms||null;T?h.push(T):H&&h.push(H),E?h.push(E):$&&h.push($),y?h.push(y):oe&&h.push(oe),A&&d.push(A),r&&C&&d.push(C),_&&I&&d.push(I);const N=h.length>0?ks(h):100,j=d.length>0?ks(d):10,Se=0,ve=19.8,k=29.7,D=30,B=(N+j)/2+D,q=B-177.3<0?0:1,J=.024*B+.11*(B-177.3)*q,Z=93.2-(Se+ve*Math.log(1+k*g)+J);return Ia(Z)};class Uv{constructor(s,n){this._callbacks={onreport:null,onticket:null},this._id=ov(),this._moduleName=this._id,this._probeId=n,this._config=s,this._exporter=new dv(s),this._state=Ss.IDLE,this.registerToPCEvents(),an(this._moduleName,`new collector created for probe ${this._probeId}`)}analyze(s,n,a,o){const t=(h,d)=>h===P.AUDIO?d===Xt.INBOUND_RTP?{...Ch}:{...Rh}:d===Xt.INBOUND_RTP?{...Ih}:{...bh},r=nv(n);r.pname=this._config.pname,r.call_id=this._config.cid,r.user_id=this._config.uid,r.count=n?n.count+1:1;let _=null;return s.forEach(h=>{!_&&h.timestamp&&(_=h.timestamp),bv(h,r,r.pname,o).forEach(g=>{if(g.value&&g.type)if(g.ssrc){let T=r[g.type][g.ssrc];T||(T=t(g.type,h.type),T.ssrc=g.ssrc,r[g.type][g.ssrc]=T),Object.keys(g.value).forEach(E=>{T[E]=g.value[E]})}else Object.keys(g.value).forEach(T=>{r[g.type][T]=g.value[T]})})}),r.timestamp=_,Object.keys(r[P.AUDIO]).forEach(h=>{const d=r[P.AUDIO][h];d.direction===$e.INBOUND?(d.mos_emodel_in=wv(r,P.AUDIO,n,a,d.ssrc),d.mos_in=Dv(r,P.AUDIO,n,a,d.ssrc)):(d.mos_emodel_out=Ov(r,P.AUDIO,n,a,d.ssrc),d.mos_out=Nv(r,P.AUDIO,n,a,d.ssrc))}),r}async takeReferenceStats(){return new Promise((s,n)=>{const a=Date.now();setTimeout(async()=>{try{const o=Date.now()-a,t=Date.now(),r=await this._config.pc.getStats(),_=this.analyze(r,null,null,null),h=Date.now();_.experimental.time_to_measure_ms=h-t,_.experimental.time_to_wait_ms=o,this._exporter.saveReferenceReport(_),Pe(this._moduleName,`got reference report for probe ${this._probeId}`),s()}catch(o){n(o)}},this._config.startAfter)})}async collectStats(){try{if(this._state!==Ss.RUNNING||!this._config.pc)return Pe(this._moduleName,`report discarded (too late) for probe ${this._probeId}`),null;const s=Date.now(),n=await this._config.pc.getStats(),a=this.analyze(n,this._exporter.getLastReport(),this._exporter.getBeforeLastReport(),this._exporter.getReferenceReport()),o=Date.now();return a.experimental.time_to_measure_ms=o-s,this._exporter.addReport(a),Pe(this._moduleName,`got report for probe ${this._probeId}#${this._exporter.getReportsNumber()+1}`),this.fireOnReport(a),a}catch(s){return qn(this._moduleName,`got error ${s}`),null}}async start(){Pe(this._moduleName,"starting"),this.state=Ss.RUNNING,this._startedTime=this._exporter.start(),Pe(this._moduleName,"started")}async mute(){this.state=Ss.MUTED,Pe(this._moduleName,"muted")}async unmute(){this.state=Ss.RUNNING,Pe(this._moduleName,"unmuted")}async stop(s){if(Pe(this._moduleName,`stopping${s?" by watchdog":""}...`),this._stoppedTime=this._exporter.stop(),this.state=Ss.IDLE,this._config.ticket){const{ticket:n}=this._exporter;this.fireOnTicket(n)}this._exporter.reset(),Pe(this._moduleName,"stopped")}registerCallback(s,n,a){s in this._callbacks?(this._callbacks[s]={callback:n,context:a},Pe(this._moduleName,`registered callback '${s}'`)):qn(this._moduleName,`can't register callback for '${s}' - not found`)}unregisterCallback(s){s in this._callbacks?(this._callbacks[s]=null,delete this._callbacks[s],Pe(this._moduleName,`unregistered callback '${s}'`)):qn(this._moduleName,`can't unregister callback for '${s}' - not found`)}fireOnReport(s){this._callbacks.onreport&&wu(this._callbacks.onreport.callback,this._callbacks.onreport.context,s)}fireOnTicket(s){this._callbacks.onticket&&wu(this._callbacks.onticket.callback,this._callbacks.onticket.context,s)}updateConfig(s){this._config=s,this._exporter.updateConfig(s)}get state(){return this._state}set state(s){this._state=s,Pe(this._moduleName,`state changed to ${s}`)}addCustomEvent(s,n,a,o){this._exporter.addCustomEvent({at:typeof s=="object"?s.toJSON():s,category:n,name:a,description:o})}async registerToPCEvents(){const{pc:s}=this._config;if(navigator.mediaDevices.ondevicechange=async()=>{try{const n=await navigator.mediaDevices.enumerateDevices();this.addCustomEvent(new Date().toJSON(),"device",`${n.length} devices found`,"Media Devices state")}catch{qn(this._moduleName,"can't get devices")}},s){s.oniceconnectionstatechange=()=>{const a=s.iceConnectionState;a===ri.CONNECTED||a===ri.COMPLETED?this.addCustomEvent(new Date().toJSON(),"call",a,"ICE connection state"):a===ri.DISCONNECTED||a===ri.FAILED?this.addCustomEvent(new Date().toJSON(),"call",a,"ICE connection state"):a===ri.CLOSED&&this.addCustomEvent(new Date().toJSON(),"call","ended","ICE connection state")},s.onicegatheringstatechange=()=>{const a=s.iceGatheringState;this.addCustomEvent(new Date().toJSON(),"call",a,"ICE gathering state")},s.ontrack=a=>{this.addCustomEvent(new Date().toJSON(),"call",`${a.track.kind}track`,"MediaStreamTrack received")},s.onnegotiationneeded=()=>{this.addCustomEvent(new Date().toJSON(),"call","negotiation","Media changed")};const n=s.getReceivers();if(n&&n.length>0){const a=n[0],{transport:o}=a;if(o){const{iceTransport:t}=o;t&&(t.onselectedcandidatepairchange=()=>{this.addCustomEvent(new Date().toJSON(),"call","transport","Candidates Pair changed")})}}}}}class xv{constructor(s){this._id=s.pname&&s.pname.substr(0,12).padEnd(12," ")||av(),this._moduleName=this._id,an(this._moduleName,"probe created"),this._config=s,this._collector=new Uv(this._config,this._id)}set onreport(s){s?this._collector.registerCallback("onreport",s):this._collector.unregisterCallback("onreport")}set onticket(s){s?this._collector.registerCallback("onticket",s):this._collector.unregisterCallback("onticket")}get id(){return this._id}get pname(){return this._config.pname}get cid(){return this._config.cid}get uid(){return this._config.uid}get state(){return this._collector.state}set state(s){this._collector.state=s}addCustomEvent(s,n,a,o=new Date().toJSON()){this._collector.addCustomEvent(o,n,s,a)}get isRunning(){return this._collector.state===Ss.RUNNING}get isIdle(){return this._collector.state===Ss.IDLE}updateUserId(s){this._config.uid=s,this._collector.updateConfig(this._config)}updateCallId(s){this._config.cid=s,this._collector.updateConfig(this._config)}start(){if(!this.isIdle){Ca(this._moduleName,"probe is already running");return}this._collector.start()}stop(s=!1){this.isRunning&&this._collector.stop(s)}async takeReferenceStats(){return this._collector.takeReferenceStats()}async collectStats(){return this._collector.collectStats()}}const bt="engine ";class Pv{constructor(s){this._config=s,this._probes=[],this._startedTime=null,this._callbacks={onresult:null},an(bt,`configured for probing every ${this._config.refreshEvery}ms`),an(bt,`configured for starting after ${this._config.startAfter}ms`),an(bt,`${!this._config.stopAfter||this._config.stopAfter!==-1?`configured for stopped after ${this._config.stopAfter}ms`:"configured for never stopped"}`),Pe(bt,"engine initialized")}get probes(){return this._probes}get isRunning(){return this._probes.some(s=>s.isRunning)}get isIdle(){return this._probes.every(s=>s.isIdle)}addNewProbe(s,n){if(!s)throw new Error("undefined peer connection");const a=iv(s,n,this._config),o=new xv(a);return this._probes.push(o),Pe(bt,`${this._probes.length} probes registered`),o}removeExistingProbe(s){if(!s)throw new Error("undefined probe");s.state===Ss.RUNNING&&s.stop(),this._probes=this._probes.filter(n=>s.id!==n.id)}async start(){const s=()=>{this._probes.forEach(t=>t.start())},n=async()=>Promise.all(this._probes.map(t=>t.takeReferenceStats())),a=()=>this.isIdle?!1:!this._config.stopAfter||this._config.stopAfter<0?!0:Date.now()<this._startedTime+this._config.stopAfter,o=async()=>{const t=sv(),r=this._probes.filter(_=>_.isRunning);for(const _ of r){const h=await _.collectStats();h&&t.probes.push(h),Pe(bt,`got probe ${_.id}`),await wh(0)}return t.delta_time_to_measure_probes_ms=on(t.probes,"experimental","time_to_measure_ms"),t.delta_KBytes_in=on(t.probes,"data","delta_KBytes_in"),t.delta_KBytes_out=on(t.probes,"data","delta_KBytes_out"),t.delta_kbs_in=on(t.probes,"data","delta_kbs_in"),t.delta_kbs_out=on(t.probes,"data","delta_kbs_out"),t.total_time_decoded_in=on(t.probes,"video","total_time_decoded_in"),t.total_time_encoded_out=on(t.probes,"video","total_time_encoded_out"),t};for(Pe(bt,"starting to collect"),s(),Pe(bt,"generating reference reports..."),await n(),Pe(bt,"reference reports generated"),this._startedTime=Date.now();a();)if(Pe(bt,`wait ${this._config.refreshEvery}ms before collecting`),await wh(this._config.refreshEvery),a()){Pe(bt,"collecting...");const t=Date.now(),r=await o(),_=Date.now();r.delta_time_consumed_to_measure_ms=_-t,this.fireOnReports(r),Pe(bt,"collected")}Pe(bt,"reaching end of the collecting period..."),this.isRunning&&setTimeout(()=>{this.stop()},0)}stop(s){const n=a=>{this._probes.forEach(o=>{o.stop(a)})};an(bt,"stop collecting"),n(s)}registerCallback(s,n,a){s in this._callbacks?(this._callbacks[s]={callback:n,context:a},Pe(bt,`registered callback '${s}'`)):qn(bt,`can't register callback for '${s}' - not found`)}unregisterCallback(s){s in this._callbacks?(this._callbacks[s]=null,delete this._callbacks[s],Pe(this._moduleName,`unregistered callback '${s}'`)):qn(this._moduleName,`can't unregister callback for '${s}' - not found`)}fireOnReports(s){this._callbacks.onresult&&s.probes.length>0&&wu(this._callbacks.onresult.callback,this._callbacks.onresult.context,s)}}const kv="interface ";class Mv{constructor(s){this._config=lv(s),an(kv,`welcome to ${this._config.name} version ${this._config.version}`),YT(this._config.verbose||!1),this._engine=new Pv(this._config)}setupLogLevel(s){JT(s)}get version(){return this._config.version}get name(){return this._config.name}get probes(){return this._engine.probes}createProbe(s,n){return this._engine.addNewProbe(s,n)}startAllProbes(){this._engine.start()}stopAllProbes(){this._engine.stop()}get running(){return this._engine.isRunning}get idle(){return this._engine.isIdle}removeProbe(s){this._engine.removeExistingProbe(s)}set onresult(s){s?this._engine.registerCallback("onresult",s):this._engine.unregisterCallback("onresult")}}function Lv(c,s){return Object.keys(c).filter(n=>s.includes(n)).reduce((n,a)=>{const o=a;return{...n,[o]:c[o]}},{})}const $v=["mos_in","codec_in","delta_KBytes_in","delta_kbs_in","delta_jitter_ms_in","delta_packets_lost_in"];class Hv{constructor(s){this.intervals={},this.emitInterval=s.emitInterval||200,this.onChangeFunction=s.onChangeFunction}start(s,n){s&&s.getTracks().length&&requestAnimationFrame(()=>this.beginCalculation(s,n))}stop(s){this.clearVolumeInterval(s)}clearVolumeInterval(s){console.log("clearVolumeInterval",s),clearInterval(this.intervals[s]),delete this.intervals[s]}clearAllIntervals(){Object.keys(this.intervals).forEach(s=>{clearInterval(this.intervals[s])}),this.intervals={}}beginCalculation(s,n){this.clearVolumeInterval(n);const a=ln.createAnalyser(),o=ln.createMediaStreamSource(s),t=ln.createScriptProcessor(2048,1,1);a.smoothingTimeConstant=.8,a.fftSize=1024,o.connect(a),a.connect(t),t.connect(ln.destination),this.intervals[n]=setInterval(()=>{const r=new Uint8Array(a.frequencyBinCount);a.getByteFrequencyData(r);let _=0;const h=r.length;for(let g=0;g<h;g++)_+=r[g];const d=_/h;this.onChangeFunction(n,d)},this.emitInterval)}}const ba={SELECTED_INPUT_DEVICE:"OpensipsJSInputDevice",SELECTED_OUTPUT_DEVICE:"OpensipsJSOutputDevice"},Fv=0;class jv{constructor(s){this.isAutoAnswer=!1,this.muteWhenJoinEnabled=!1,this.isDNDEnabled=!1,this.muted=!1,this.microphoneInputLevelValue=1,this.speakerVolumeValue=1,this.activeRooms={},this.activeCalls={},this.extendedCalls={},this.availableMediaDevices=[],this.selectedMediaDevices={input:"default",output:"default"},this.callStatus={},this.callTime={},this.callMetrics={},this.timeIntervals={},this.metricConfig={refreshEvery:1e3},this.activeStreamValue=null,this.initialStreamValue=null,this.context=s,this.context.on(this.context.newRTCSessionEventName,this.newRTCSessionCallback.bind(this)),this.VUMeter=new Hv({onChangeFunction:this.emitVolumeChange.bind(this)}),this.initializeMediaDevices()}get sipOptions(){return{...this.context.options.sipOptions,mediaConstraints:this.getUserMediaConstraints}}get currentActiveRoomId(){return this.currentActiveRoomIdValue}set currentActiveRoomId(s){this.currentActiveRoomIdValue=s,this.context.emit("currentActiveRoomChanged",s)}get autoAnswer(){return this.isAutoAnswer}get callAddingInProgress(){return this.isCallAddingInProgress}set callAddingInProgress(s){this.isCallAddingInProgress=s,this.context.emit("callAddingInProgressChanged",s)}get muteWhenJoin(){return this.muteWhenJoinEnabled}get isDND(){return this.isDNDEnabled}get speakerVolume(){return this.speakerVolumeValue}get microphoneInputLevel(){return this.microphoneInputLevelValue}get getActiveCalls(){return this.activeCalls}get hasActiveCalls(){return Object.values(this.extendedCalls).length>0}get getActiveRooms(){return this.activeRooms}get isMuted(){return this.muted}get getInputDeviceList(){return this.availableMediaDevices.filter(s=>s.kind==="audioinput")}get getOutputDeviceList(){return this.availableMediaDevices.filter(s=>s.kind==="audiooutput")}get getUserMediaConstraints(){return Eh()?{video:!1,audio:!0}:{audio:{deviceId:{exact:this.selectedMediaDevices.input}},video:!1}}get selectedInputDevice(){return this.selectedMediaDevices.input}get selectedOutputDevice(){return this.selectedMediaDevices.output}get activeStream(){return this.activeStreamValue}setAvailableMediaDevices(s){this.availableMediaDevices=s,this.context.emit("changeAvailableDeviceList",s)}async updateDeviceList(){await navigator.mediaDevices.getUserMedia(this.getUserMediaConstraints);const s=await navigator.mediaDevices.enumerateDevices();this.setAvailableMediaDevices(s)}async initializeMediaDevices(){const s=localStorage.getItem(ba.SELECTED_INPUT_DEVICE)||"default",n=localStorage.getItem(ba.SELECTED_OUTPUT_DEVICE)||"default";try{const a=await navigator.mediaDevices.getUserMedia(this.getUserMediaConstraints),o=await navigator.mediaDevices.enumerateDevices();this.setAvailableMediaDevices(o),await this.setMicrophone(s),await this.setSpeaker(n),navigator.mediaDevices.addEventListener("devicechange",async()=>{const t=await navigator.mediaDevices.enumerateDevices();this.setAvailableMediaDevices(t)}),a.getTracks().forEach(t=>t.stop())}catch(a){console.error(a)}}setCallTime(s){const n={...s};delete n.callId,this.callTime={...this.callTime,[s.callId]:n},this.context.emit("changeCallTime",this.callTime)}removeCallTime(s){const n={...this.callTime};delete n[s],this.callTime={...n},this.context.emit("changeCallTime",this.callTime)}setTimeInterval(s,n){this.timeIntervals={...this.timeIntervals,[s]:n}}removeTimeInterval(s){const n={...this.timeIntervals};n[s]&&(clearInterval(n[s]),delete n[s],this.timeIntervals={...n})}stopCallTimer(s){this.removeTimeInterval(s),this.removeCallTime(s)}emitVolumeChange(s,n){this.context.emit("changeCallVolume",{callId:s,volume:n})}setMetricsConfig(s){this.metricConfig={...this.metricConfig,...s}}sendDTMF(s,n){if(!/^[A-D0-9]+$/g.test(n))throw new Error("Not allowed character in DTMF input");this.extendedCalls[s].sendDTMF(n)}setIsMuted(s){this.muted=s,this.context.emit("changeIsMuted",s)}processMute(s){const n=this.currentActiveRoomId;this.setIsMuted(s),this.initialStreamValue.getTracks().forEach(a=>a.enabled=!s),this.roomReconfigure(n)}mute(){this.processMute(!0)}unmute(){this.processMute(!1)}async processHold({callId:s,toHold:n,automatic:a}){const o=this.extendedCalls[s];o._automaticHold=a??!1,await new Promise(_=>{const h=()=>{_()};n?o.hold({},h):o.unhold({},h)}),this.updateCall(o);const r=Object.values(this.extendedCalls).filter(_=>_.roomId===this.currentActiveRoomId&&(n?s!==_._id:!0));r.length>1&&await this.doConference(r)}holdCall(s,n=!1){return this.processHold({callId:s,automatic:n,toHold:!0})}unholdCall(s){return this.processHold({callId:s,toHold:!1})}cancelAllOutgoingUnanswered(){Object.values(this.getActiveCalls).filter(s=>s.direction==="outgoing"&&s.status===Fv).forEach(s=>this.terminateCall(s._id))}answerCall(s){const n=this.extendedCalls[s];this.cancelAllOutgoingUnanswered(),n.answer(this.sipOptions),this.updateCall(n),this.setActiveRoom(n.roomId),n.connection.addEventListener("track",a=>{this.triggerAddStream(a,n)})}async moveCall(s,n){this.updateCallStatus({callId:s,isMoving:!0}),await this.processRoomChange({callId:s,roomId:n}),this.updateCallStatus({callId:s,isMoving:!1})}updateCall(s){this.activeCalls[s._id]=gh(s),this.context.emit("changeActiveCalls",this.activeCalls)}updateRoom(s){const a={...this.activeRooms[s.roomId],...s};this.activeRooms={...this.activeRooms,[s.roomId]:{...a}},this.context.emit("updateRoom",{room:a,roomList:this.activeRooms})}hasAutoAnswerHeaders(s){const n=/answer-after=0/,o=s.request.getHeader("Call-Info");return o&&n.test(o)}addCall(s,n=!0){this.activeCalls={...this.activeCalls,[s._id]:gh(s)},this.extendedCalls[s._id]=s,n&&this.context.emit("changeActiveCalls",this.activeCalls)}addCallStatus(s){this.callStatus={...this.callStatus,[s]:{isMoving:!1,isTransferring:!1,isMerging:!1}},this.context.emit("changeCallStatus",this.callStatus)}updateCallStatus(s){const a={...{...this.callStatus[s.callId]}};s.isMoving!==void 0&&(a.isMoving=s.isMoving),s.isTransferring!==void 0&&(a.isTransferring=s.isTransferring),s.isMerging!==void 0&&(a.isMerging=s.isMerging),this.callStatus={...this.callStatus,[s.callId]:{...a}},this.context.emit("changeCallStatus",this.callStatus)}removeCallStatus(s){const n={...this.callStatus};delete n[s],this.callStatus={...n},this.context.emit("changeCallStatus",this.callStatus)}addRoom(s){this.activeRooms={...this.activeRooms,[s.roomId]:s},this.context.emit("addRoom",{room:s,roomList:this.activeRooms})}getActiveStream(){const s=vh(this.initialStreamValue,this.microphoneInputLevel*2);return s.getTracks().forEach(n=>n.enabled=!this.isMuted),this.setActiveStream(s),s}async setMicrophone(s){if(!this.getInputDeviceList.find(({deviceId:a})=>a===s)||(this.setSelectedInputDevice(s),Object.keys(this.getActiveCalls).length===0))return;await this.setupStream();const n=Object.values(this.extendedCalls).filter(a=>a.roomId===this.currentActiveRoomId);n.length===1?Object.values(n).forEach(async a=>{const o=this.getActiveStream();a.connection.getSenders()[0].replaceTrack(o.getTracks()[0]),this.updateCall(a)}):await this.doConference(n)}setActiveStream(s){this.activeStream&&this.stopVUMeter("origin"),this.setupVUMeter(s,"origin"),this.activeStreamValue=s,this.context.emit("changeActiveStream",s)}async setSpeaker(s){if(!this.getOutputDeviceList.find(({deviceId:o})=>o===s))return;this.setSelectedOutputDevice(s);const n=Object.values(this.extendedCalls);if(n.length===0)return;const a=n.filter(o=>o.roomId===this.currentActiveRoomId);a.length===1?n.forEach(o=>{var t;(t=o.audioTag)==null||t.setSinkId(s),this.updateCall(o)}):await this.doConference(a)}removeRoom(s){const n={...this.activeRooms},a={...n[s]};delete n[s],this.activeRooms={...n},this.context.emit("removeRoom",{room:a,roomList:this.activeRooms})}deleteRoomIfEmpty(s){s!==void 0&&Object.values(this.extendedCalls).filter(n=>n.roomId===s).length===0&&(this.removeRoom(s),this.currentActiveRoomId===s&&(this.currentActiveRoomId=void 0))}checkInitialized(){if(!this.context.initialized)throw new Error("[OpenSIPSJS] You must call `start` method first!")}muteReconfigure(s){this.muted?s.mute({audio:!0}):s.unmute({audio:!0})}async roomReconfigure(s){var a;if(s===void 0)return;const n=Object.values(this.extendedCalls).filter(o=>o.roomId===s);if(this.currentActiveRoomId===s?n.forEach(o=>{o.audioTag&&(this.muteReconfigure(o),o.audioTag.muted=!1,this.updateCall(o))}):n.forEach(o=>{o.audioTag&&(o.audioTag.muted=!0,this.updateCall(o))}),n.length===0)this.deleteRoomIfEmpty(s);else if(n.length===1&&this.currentActiveRoomId!==s)n[0].isOnHold().local||await this.holdCall(n[0].id,!0);else if(n.length===1&&this.currentActiveRoomId===s){if(n[0].isOnHold().local&&n[0]._automaticHold&&await this.unholdCall(n[0].id),n[0].connection&&((a=n[0].connection)!=null&&a.getSenders()[0])){const o=this.getActiveStream();await n[0].connection.getSenders()[0].replaceTrack(o.getTracks()[0]),this.muteReconfigure(n[0])}}else n.length>1&&await this.doConference(n)}async doConference(s){const n=[];s.forEach(a=>{a!=null&&a.connection.getReceivers().forEach(o=>{n.push(o.track)})}),await WT.forEach(s,async a=>{var r;if(a==null)return;const o=new MediaStream,t=ln.createMediaStreamDestination();if(a.connection.getReceivers().forEach(_=>{n.forEach(h=>{o.addTrack(_.track),_.track.id!==h.id&&ln.createMediaStreamSource(new MediaStream([h])).connect(t)})}),s[0].roomId===this.currentActiveRoomId){const _=this.getActiveStream();ln.createMediaStreamSource(_).connect(t)}(r=a.connection)!=null&&r.getSenders()[0]&&(await a.connection.getSenders()[0].replaceTrack(t.stream.getTracks()[0]),this.muteReconfigure(a))})}processCallerMute(s,n){const a=this.extendedCalls[s];a&&a.connection.getReceivers().length&&(a.localMuted=n,a.connection.getReceivers().forEach(o=>{o.track.enabled=!n}),this.updateCall(a))}muteCaller(s){this.processCallerMute(s,!0)}unmuteCaller(s){this.processCallerMute(s,!1)}terminateCall(s){const n=this.extendedCalls[s];n._status!==8&&n.terminate()}transferCall(s,n){if(n.toString().length===0)return new Error("Target must be passed");const a=this.extendedCalls[s];if(!a._is_confirmed&&!a._is_canceled){const o=`sip:${n}@${this.context.sipDomain}`;a.terminate({status_code:302,reason_phrase:"Moved Temporarily",extraHeaders:[`Contact: ${o}`]});return}this.updateCallStatus({callId:s,isTransferring:!0}),a.refer(`sip:${n}@${this.context.sipDomain}`),this.updateCall(a)}mergeCall(s){const n=Object.values(this.extendedCalls).filter(t=>t.roomId===s);if(n.length!==2)return;const a=n[0],o=n[1];!a||!o||(this.updateCallStatus({callId:a._id,isMerging:!0}),this.updateCallStatus({callId:o._id,isMerging:!0}),a.refer(o.remote_identity.uri.toString(),{replaces:o}),this.updateCall(a))}mergeCallByIds(s,n){const a=Object.values(this.extendedCalls).find(t=>t._id===s),o=Object.values(this.extendedCalls).find(t=>t._id===n);if(!a||!o)throw new Error("Call ID is not provided");this.updateCallStatus({callId:s,isMerging:!0}),this.updateCallStatus({callId:n,isMerging:!0}),a.refer(o.remote_identity.uri.toString(),{replaces:o}),this.updateCall(a)}setDND(s){this.isDNDEnabled=s,this.context.emit("changeIsDND",s)}startCallTimer(s){this.removeTimeInterval(s);const n={callId:s,hours:0,minutes:0,seconds:0,formatted:""};this.setCallTime(n);const a=setInterval(()=>{const o={...this.callTime[s]},t=GT(o);this.setCallTime({callId:s,...t})},1e3);this.setTimeInterval(s,a)}async setActiveRoom(s){const n=this.currentActiveRoomId;s!==n&&(this.currentActiveRoomId=s,await this.roomReconfigure(n),await this.roomReconfigure(s))}getNewRoomId(){const s=Object.keys(this.activeRooms);return s.length===0?1:parseInt(s.sort()[s.length-1])+1}async setupCall(s){var d,g;const n=s.session;if(this.getActiveCalls[n.id]!==void 0)return;const o=this.getNewRoomId(),t={started:new Date,incomingInProgress:!1,roomId:o};n.direction==="incoming"?(this.context.logger.log("New incoming call from",(g=(d=n._remote_identity)==null?void 0:d._uri)==null?void 0:g._user),t.incomingInProgress=!0,this.context.subscribe(zt.CALL_CONFIRMED,T=>{n.id===T.id&&(this.updateRoom({incomingInProgress:!1,roomId:o}),this.startCallTimer(n.id))}),this.context.subscribe(zt.CALL_FAILED,T=>{n.id===T.id&&(this.updateRoom({incomingInProgress:!1,roomId:o}),this.deleteRoomIfEmpty(o))})):n.direction==="outgoing"&&(n.once("confirmed",()=>{this.startCallTimer(n.id)}),this.startCallTimer(n.id));const r=n,_=this.hasAutoAnswerHeaders(s),h=r.direction==="incoming"&&!this.hasActiveCalls&&(_||this.autoAnswer);r.roomId=o,r.localMuted=!1,r.autoAnswer=h,h?this.addCall(r,!1):this.addCall(r),this.addCallStatus(n.id),this.addRoom(t),h&&this.answerCall(r._id)}removeCall(s){const n={...this.activeCalls};delete n[s],this.activeCalls={...n};const a={...this.extendedCalls};delete a[s],this.extendedCalls={...a},this.context.emit("changeActiveCalls",this.activeCalls)}activeCallListRemove(s){var o;const n=this.extendedCalls[s._id];this.stopVUMeter("origin"),(o=n.connection)==null||o.getSenders().forEach(t=>{t.track.stop()});const a=n.roomId;this.removeCall(s._id),this.roomReconfigure(a)}async newRTCSessionCallback(s){const n=s.session;if(this.isDND){n.terminate({status_code:486,reason_phrase:"Do Not Disturb"});return}if(this.context.triggerListener({listenerType:zt.NEW_CALL,session:n,event:s}),n.on("ended",a=>{var t,r,_;this.stopVUMeter(n.id),this.context.logger.log("Session ended for",(r=(t=n._remote_identity)==null?void 0:t._uri)==null?void 0:r._user),this.context.triggerListener({listenerType:zt.CALL_ENDED,session:n,event:a});const o=this.getActiveCalls[n.id];o&&this.activeCallListRemove(o),this.stopCallTimer(n.id),this.removeCallStatus(n.id),this.removeCallMetrics(n.id),Object.keys(this.extendedCalls).length||(this.setIsMuted(!1),(_=this.initialStreamValue)==null||_.getTracks().forEach(h=>h.stop()),this.initialStreamValue=null)}),n.on("progress",a=>{var o,t;this.context.logger.log("Session in progress for",(t=(o=n._remote_identity)==null?void 0:o._uri)==null?void 0:t._user),this.context.triggerListener({listenerType:zt.CALL_PROGRESS,session:n,event:a})}),n.on("failed",a=>{var t,r,_;this.stopVUMeter(n.id),this.context.logger.log("Session failed for",(r=(t=n._remote_identity)==null?void 0:t._uri)==null?void 0:r._user),this.context.triggerListener({listenerType:zt.CALL_FAILED,session:n,event:a}),n.id===this.callAddingInProgress&&(this.callAddingInProgress=void 0);const o=this.getActiveCalls[n.id];o&&this.activeCallListRemove(o),this.stopCallTimer(n.id),this.removeCallStatus(n.id),this.removeCallMetrics(n.id),Object.keys(this.extendedCalls).length||(this.setIsMuted(!1),(_=this.initialStreamValue)==null||_.getTracks().forEach(h=>h.stop()),this.initialStreamValue=null)}),n.on("confirmed",a=>{var o,t;this.context.logger.log("Session confirmed for",(t=(o=n._remote_identity)==null?void 0:o._uri)==null?void 0:t._user),this.context.triggerListener({listenerType:zt.CALL_CONFIRMED,session:n,event:a}),this.updateCall(n),n.id===this.callAddingInProgress&&(this.callAddingInProgress=void 0)}),await this.setupCall(s),n.direction==="outgoing"){const a=this.getActiveCalls[n.id].roomId;this.setActiveRoom(a)}}setMuteWhenJoin(s){this.muteWhenJoinEnabled=s,this.context.emit("changeMuteWhenJoin",s)}setMicrophoneSensitivity(s){if(s<0||s>1)throw new Error("Value should be in range from 0 to 1!");this.microphoneInputLevelValue=s,this.roomReconfigure(this.currentActiveRoomId)}setSpeakerVolume(s){this.speakerVolumeValue=s,Object.values(this.extendedCalls).forEach(n=>{n.audioTag&&(n.audioTag.volume=s,this.updateCall(n))})}setAutoAnswer(s){this.isAutoAnswer=s}setSelectedInputDevice(s){localStorage.setItem(ba.SELECTED_INPUT_DEVICE,s),this.selectedMediaDevices.input=s,this.context.emit("changeActiveInputMediaDevice",s)}setSelectedOutputDevice(s){localStorage.setItem(ba.SELECTED_OUTPUT_DEVICE,s),this.selectedMediaDevices.output=s,this.context.emit("changeActiveOutputMediaDevice",s)}setCallMetrics(s){const n={...s};delete n.callId,this.callMetrics={...this.callMetrics,[s.callId]:n},this.context.emit("changeCallMetrics",this.callMetrics)}removeCallMetrics(s){const n={...this.callMetrics};delete n[s],this.callMetrics={...n},this.context.emit("changeCallMetrics",this.callMetrics)}getCallQuality(s){const n=new Mv(this.metricConfig),a=n.createProbe(s.connection,{cid:s._id}),o=[];let t;a.onreport=r=>{Object.entries(r.audio).forEach(([d,g])=>{g.direction==="inbound"&&!o.includes(d)&&(o.push(d),t=d)});const _=r.audio[t];if(!_)return;const h=Lv(_,$v);h.callId=s._id,this.setCallMetrics(h)},this.context.subscribe(zt.CALL_ENDED,r=>{r._id===s._id&&n.stopAllProbes()}),n.startAllProbes()}setupVUMeter(s,n){this.VUMeter.start(s,n)}stopVUMeter(s){this.VUMeter.stop(s)}async setupStream(){const s=await navigator.mediaDevices.getUserMedia(this.getUserMediaConstraints);this.initialStreamValue&&(this.initialStreamValue.getTracks().forEach(n=>n.stop()),this.initialStreamValue=null),this.initialStreamValue=s}async triggerAddStream(s,n){this.setIsMuted(this.muteWhenJoin||this.isMuted),this.initialStreamValue||await this.setupStream();const a=vh(this.initialStreamValue,this.microphoneInputLevel*2),o=this.isMuted||this.muteWhenJoin;a.getTracks().forEach(r=>r.enabled=!o),this.setActiveStream(a),await n.connection.getSenders()[0].replaceTrack(a.getTracks()[0]);const t=new MediaStream([s.track]);DT(t,n,this.selectedOutputDevice,this.speakerVolume),this.setupVUMeter(t,n._id),this.getCallQuality(n),this.updateCall(n)}initCall(s,n){if(s.length===0)return console.error("Target must be a valid string");this.context.logger.log(`Calling sip:${s}@${this.context.sipDomain}...`);const a=this.context.call(`sip:${s}@${this.context.sipDomain}`,this.sipOptions);this.callAddingInProgress=a.id,n&&this.currentActiveRoomId!==void 0&&this.processRoomChange({callId:a.id,roomId:this.currentActiveRoomId}),a.connection.addEventListener("track",o=>{this.triggerAddStream(o,a)})}async processRoomChange({callId:s,roomId:n}){const a=this.extendedCalls[s].roomId;this.extendedCalls[s].roomId=n;const o=this.extendedCalls[s];return this.updateCall(o),await this.setActiveRoom(n),Promise.all([this.roomReconfigure(a),this.roomReconfigure(n)]).then(()=>{this.deleteRoomIfEmpty(a),this.deleteRoomIfEmpty(n)})}}class Vv{constructor(s){this.context=s}get sipOptions(){return{...this.context.options.sipOptions}}initCall(s,n){if(s.length===0)return console.error("Target must be a valid string");this.context.logger.log(`Calling sip:${s}@${this.context.sipDomain}...`),this.context.joinVideoCall(`sip:${s}@${this.context.sipDomain}`,n,this.sipOptions)}stop(s={}){this.context.terminateJanusSessions(s)}startAudio(){this.context.enableJanusAudio(!0)}stopAudio(){this.context.enableJanusAudio(!1)}startVideo(){this.context.enableJanusVideo(!0)}stopVideo(){this.context.enableJanusVideo(!1)}}class qv{constructor(s){this.activeMessages={},this.extendedMessages={},this.msrpHistory={},this.context=s,this.context.on(this.context.newMSRPSessionEventName,this.newMSRPSessionCallback.bind(this.context))}get isMSRPInitializing(){return this.isMSRPInitializingValue}get getActiveMessages(){return this.activeMessages}msrpAnswer(s){const n=this.extendedMessages[s];this.updateMSRPSession(n)}updateMSRPSession(s){this.activeMessages[s._id]=Th(s),this.context.emit("changeActiveMessages",this.activeMessages)}addMMSRPSession(s){this.activeMessages={...this.activeMessages,[s._id]:Th(s)},this.extendedMessages[s._id]=s,this.context.emit("changeActiveMessages",this.activeMessages)}addMSRPMessage(s,n){const a=this.msrpHistory[n.id]||[];a.push(s),this.msrpHistory={...this.msrpHistory,[n.id]:[...a]},this.context.emit("newMSRPMessage",{message:s,session:n})}messageTerminate(s){const n=this.extendedMessages[s];n._status!==8&&n.terminate()}addMessageSession(s){if(!s._id||this.getActiveMessages[s._id]!==void 0)return;const a=s;this.addMMSRPSession(a)}triggerMSRPListener({listenerType:s,session:n,event:a}){const o=this.context.listenersList[s];!o||!o.length||o.forEach(t=>{t(n,a)})}removeMMSRPSession(s){const n={...this.activeMessages};delete n[s],this.activeMessages={...n};const a={...this.extendedMessages};delete a[s],this.extendedMessages={...a},this.context.emit("changeActiveMessages",this.activeMessages)}activeMessageListRemove(s){this.removeMMSRPSession(s._id)}newMSRPSessionCallback(s){const n=s.session;n.on("ended",a=>{this.triggerMSRPListener({listenerType:zt.CALL_ENDED,session:n,event:a});const o=this.getActiveMessages[n.id];this.activeMessageListRemove(o)}),n.on("failed",a=>{this.triggerMSRPListener({listenerType:zt.CALL_FAILED,session:n,event:a});const o=this.getActiveMessages[n.id];this.activeMessageListRemove(o)}),n.on("confirmed",a=>{this.triggerMSRPListener({listenerType:zt.CALL_CONFIRMED,session:n,event:a}),this.updateMSRPSession(n)}),n.on("newMessage",a=>{this.addMSRPMessage(a,n)}),this.addMessageSession(n)}setIsMSRPInitializing(s){this.isMSRPInitializingValue=s,this.context.emit("isMSRPInitializingChanged",s)}initMSRP(s,n,a){if(s.length===0)return console.error("Target must be a valid string");const o=this.context.startMSRP(s,a);o.on("active",()=>{this.addMessageSession(o),o.sendMSRP(n),this.setIsMSRPInitializing(!1)}),this.setIsMSRPInitializing(!0)}sendMSRP(s,n){const a=this.extendedMessages[s];if(!a)throw new Error(`MSRP session with id ${s} doesn't exist!`);a.sendMSRP(n)}}const Ou={AUDIO:"audio",VIDEO:"video",MSRP:"msrp"};class Bv extends AT{constructor(s,n){if(!s.modules.length)throw new Error("options.modules should include at least 1 module");const a={...s.configuration,sockets:s.socketInterfaces.map(o=>new BT.WebSocketInterface(o))};super(a),this.initialized=!1,this.connected=!1,this.logger=console,this.newRTCSessionEventName="newRTCSession",this.registeredEventName="registered",this.unregisteredEventName="unregistered",this.disconnectedEventName="disconnected",this.connectedEventName="connected",this.newMSRPSessionEventName="newMSRPSession",this.isReconnecting=!1,this.audio=null,this.msrp=null,this.video=null,this.listenersList={},this.modules=[],s.pnExtraHeaders&&Object.keys(s.pnExtraHeaders).length&&this.registrator().setExtraContactUriParams(s.pnExtraHeaders),this.options=s,this.modules=s.modules,n&&NT(n)&&(this.logger=n)}on(s,n){return super.on(s,n)}off(s,n){return super.off(s,n)}emit(s,n){return super.emit(s,n)}get sipDomain(){return this.options.sipDomain}begin(){if(this.isConnected()){console.error("Connection is already established");return}return this.modules.includes(Ou.AUDIO)&&(this.audio=new jv(this)),this.modules.includes(Ou.MSRP)&&(this.msrp=new qv(this)),this.modules.includes(Ou.VIDEO)&&(this.video=new Vv(this)),this.on(this.registeredEventName,()=>{this.logger.log("Successfully registered to",this.options.socketInterfaces[0]),this.setInitialized(!0)}),this.on(this.unregisteredEventName,()=>{this.logger.log("Unregistered from",this.options.socketInterfaces[0]),this.setInitialized(!1)}),this.on(this.connectedEventName,()=>{this.logger.log("Connected to",this.options.socketInterfaces[0]),this.setConnected(!0),this.isReconnecting=!1}),this.on(this.disconnectedEventName,()=>{this.isReconnecting||(this.logger.log("Disconnected from",this.options.socketInterfaces[0]),this.logger.log("Reconnecting to",this.options.socketInterfaces[0]),this.isReconnecting=!0,this.stop(),this.setInitialized(!1),this.setConnected(!1),setTimeout(this.start.bind(this),5e3))}),this.logger.log("Connecting to",this.options.socketInterfaces[0]),this.start(),this}subscribe(s,n){const o=!this.listenersList[s]||!this.listenersList[s].length?[n]:[...this.listenersList[s],n];this.listenersList={...this.listenersList,[s]:o}}removeIListener(s){const n={...this.listenersList};delete n[s],this.listenersList={...n}}triggerListener({listenerType:s,session:n,event:a}){const o=this.listenersList[s];!o||!o.length||o.forEach(t=>{t(n,a)})}setInitialized(s){this.initialized=s,this.emit("ready",s)}setConnected(s){this.connected=s,this.emit("connection",s)}}return Bv});
158
+ `,n),o===-1)return o;!c.substring(o+2,o+4).match(/(^\r\n)/)&&c.charAt(o+2).match(/(^\s+)/)?n=o+2:a=o}return a}function vT(c,s,n,a){let o;const t=s.indexOf(":",n),r=s.substring(n,t).trim(),_=s.substring(t+1,a).trim();switch(r.toLowerCase()){case"via":case"v":c.addHeader("via",_),c.getHeaders("via").length===1?(o=c.parseHeader("Via"),o&&(c.via=o,c.via_branch=o.branch)):o=0;break;case"from":case"f":c.setHeader("from",_),o=c.parseHeader("from"),o&&(c.from=o,c.from_tag=o.getParam("tag"));break;case"to":case"t":c.setHeader("to",_),o=c.parseHeader("to"),o&&(c.to=o,c.to_tag=o.getParam("tag"));break;case"record-route":if(o=yu.parse(_,"Record_Route"),o===-1)o=void 0;else for(const h of o)c.addHeader("record-route",_.substring(h.possition,h.offset)),c.headers["Record-Route"][c.getHeaders("record-route").length-1].parsed=h.parsed;break;case"call-id":case"i":c.setHeader("call-id",_),o=c.parseHeader("call-id"),o&&(c.call_id=_);break;case"contact":case"m":if(o=yu.parse(_,"Contact"),o===-1)o=void 0;else for(const h of o)c.addHeader("contact",_.substring(h.possition,h.offset)),c.headers.Contact[c.getHeaders("contact").length-1].parsed=h.parsed;break;case"content-length":case"l":c.setHeader("content-length",_),o=c.parseHeader("content-length");break;case"content-type":case"c":c.setHeader("content-type",_),o=c.parseHeader("content-type");break;case"cseq":c.setHeader("cseq",_),o=c.parseHeader("cseq"),o&&(c.cseq=o.value),c instanceof en.IncomingResponse&&(c.method=o.method);break;case"max-forwards":c.setHeader("max-forwards",_),o=c.parseHeader("max-forwards");break;case"www-authenticate":c.setHeader("www-authenticate",_),o=c.parseHeader("www-authenticate");break;case"proxy-authenticate":c.setHeader("proxy-authenticate",_),o=c.parseHeader("proxy-authenticate");break;case"session-expires":case"x":c.setHeader("session-expires",_),o=c.parseHeader("session-expires"),o&&(c.session_expires=o.expires,c.session_expires_refresher=o.refresher);break;case"refer-to":case"r":c.setHeader("refer-to",_),o=c.parseHeader("refer-to"),o&&(c.refer_to=o);break;case"replaces":c.setHeader("replaces",_),o=c.parseHeader("replaces"),o&&(c.replaces=o);break;case"event":case"o":c.setHeader("event",_),o=c.parseHeader("event"),o&&(c.event=o);break;default:c.addHeader(r,_),o=0}return o===void 0?{error:`error parsing header "${r}"`}:!0}const ET={parseMessage:gT},si=new na("Registrator"),Aa=10;class ST{constructor(s,n){this._reg_id=1,this._ua=s,this._transport=n,this._registrar=s.configuration.registrar_server,this._expires=s.configuration.register_expires,this._call_id=Ts.createRandomToken(22),this._cseq=0,this._to_uri=s.configuration.uri,this._registrationTimer=null,this._registering=!1,this._registered=!1,this._contact=this._ua.contact.toString();const a=this._contact.indexOf(">");if(a!==-1){const o=this._contact.slice(0,a)+this._contact.slice(a+1,this._contact.length);this._contact=o}this._extra_contact="",this._extraContactParams="",this._extra_contact+=";+sip.ice",this._extraHeaders=[],this._sipInstance=`"<urn:uuid:${this._ua.configuration.instance_id}>"`,this._extra_contact+=`;reg-id=${this._reg_id}`,this._extra_contact+=`;+sip.instance=${this._sipInstance}`}get registered(){return this._registered}setExtraHeaders(s){Array.isArray(s)||(s=[]),this._extraHeaders=s.slice()}setExtraContactParams(s){s instanceof Object||(s={}),this._extraContactParams="";for(const n in s)if(Object.prototype.hasOwnProperty.call(s,n)){const a=s[n];this._extraContactParams+=`;${n}`,a&&(this._extraContactParams+=`=${a}`)}}setExtraContactUriParams(s){s instanceof Object||(s={}),this._extraContactParams="";for(const n in s)if(Object.prototype.hasOwnProperty.call(s,n)){const a=s[n];this._extraContactParams+=`;${n}`,a&&(this._extraContactParams+=`=${a}`)}}register(){if(this._registering){si.debug("Register request in progress...");return}const s=this._extraHeaders.slice();s.push(`Contact: ${this._contact}${this._extraContactParams}>${this._extra_contact};expires=${this._expires}`),s.push(`Expires: ${this._expires}`);const n=new Rt.OutgoingRequest(L.REGISTER,this._registrar,this._ua,{to_uri:this._to_uri,call_id:this._call_id,cseq:this._cseq+=1},s),a=new aa(this._ua,n,{onRequestTimeout:()=>{this._registrationFailure(null,L.causes.REQUEST_TIMEOUT)},onTransportError:()=>{this._registrationFailure(null,L.causes.CONNECTION_ERROR)},onAuthenticated:()=>{this._cseq+=1},onReceiveResponse:o=>{if(o.cseq===this._cseq)switch(this._registrationTimer!==null&&(clearTimeout(this._registrationTimer),this._registrationTimer=null),!0){case/^1[0-9]{2}$/.test(o.status_code):break;case/^2[0-9]{2}$/.test(o.status_code):{if(this._registering=!1,!o.hasHeader("Contact")){si.debug("no Contact header in response to REGISTER, response ignored");break}const t=o.headers.Contact.reduce((d,g)=>d.concat(g.parsed),[]);let r=t.find(d=>this._sipInstance===d.getParam("+sip.instance")&&this._reg_id===parseInt(d.getParam("reg-id")));if(r||(r=t.find(d=>d.uri.user===this._ua.contact.uri.user)),!r){si.debug("no Contact header pointing to us, response ignored");break}let _=r.getParam("expires");!_&&o.hasHeader("expires")&&(_=o.getHeader("expires")),_||(_=this._expires),_=Number(_),_<Aa&&(_=Aa);const h=_>64?_*1e3/2+Math.floor((_/2-32)*1e3*Math.random()):_*1e3-5e3;this._registrationTimer=setTimeout(()=>{this._registrationTimer=null,this._ua.listeners("registrationExpiring").length===0?this.register():this._ua.emit("registrationExpiring")},h),r.hasParam("temp-gruu")&&(this._ua.contact.temp_gruu=r.getParam("temp-gruu").replace(/"/g,"")),r.hasParam("pub-gruu")&&(this._ua.contact.pub_gruu=r.getParam("pub-gruu").replace(/"/g,"")),this._registered||(this._registered=!0,this._ua.registered({response:o}));break}case/^423$/.test(o.status_code):{o.hasHeader("min-expires")?(this._expires=Number(o.getHeader("min-expires")),this._expires<Aa&&(this._expires=Aa),this.register()):(si.debug("423 response received for REGISTER without Min-Expires"),this._registrationFailure(o,L.causes.SIP_FAILURE_CODE));break}default:{const t=Ts.sipErrorCause(o.status_code);this._registrationFailure(o,t)}}}});this._registering=!0,a.send()}unregister(s={}){if(!this._registered){si.debug("already unregistered");return}this._registered=!1,this._registrationTimer!==null&&(clearTimeout(this._registrationTimer),this._registrationTimer=null);const n=this._extraHeaders.slice();s.all?n.push(`Contact: *${this._extraContactParams}`):n.push(`Contact: ${this._contact}${this._extraContactParams}>${this._extra_contact};expires=0`),n.push("Expires: 0");const a=new Rt.OutgoingRequest(L.REGISTER,this._registrar,this._ua,{to_uri:this._to_uri,call_id:this._call_id,cseq:this._cseq+=1},n);new aa(this._ua,a,{onRequestTimeout:()=>{this._unregistered(null,L.causes.REQUEST_TIMEOUT)},onTransportError:()=>{this._unregistered(null,L.causes.CONNECTION_ERROR)},onAuthenticated:()=>{this._cseq+=1},onReceiveResponse:t=>{switch(!0){case/^1[0-9]{2}$/.test(t.status_code):break;case/^2[0-9]{2}$/.test(t.status_code):this._unregistered(t);break;default:{const r=Ts.sipErrorCause(t.status_code);this._unregistered(t,r)}}}}).send()}close(){this._registered&&this.unregister()}onTransportClosed(){this._registering=!1,this._registrationTimer!==null&&(clearTimeout(this._registrationTimer),this._registrationTimer=null),this._registered&&(this._registered=!1,this._ua.unregistered({}))}_registrationFailure(s,n){this._registering=!1,this._ua.registrationFailed({response:s||null,cause:n}),this._registered&&(this._registered=!1,this._ua.unregistered({response:s||null,cause:n}))}_unregistered(s,n){this._registering=!1,this._registered=!1,this._ua.unregistered({response:s||null,cause:n||null})}}const rt=console,Ps={STATUS_INIT:0,STATUS_READY:1,STATUS_USER_CLOSED:2,STATUS_NOT_READY:3,CONFIGURATION_ERROR:1,NETWORK_ERROR:2},yT=oT;class AT extends yT{constructor(s){super(s),this._msrp_sessions=[],this._transactions={nist:{},nict:{},ist:{},ict:{}},this._janus_sessions=[],this._registrator=new ST(this)}call(s,n){return super.call(s,n)}joinVideoCall(s,n,a){rt.debug("call()");const o=new Sa(this);return o.configureMedia({audio:!0,video:!0}),o.connect(s,n,a),o}_loadConfig(s){try{ti.load(this._configuration,s)}catch(o){throw o}this._configuration.display_name===0&&(this._configuration.display_name="0"),this._configuration.instance_id||(this._configuration.instance_id=Ts.newUUID()),this._configuration.jssip_id=Ts.createRandomToken(5);const n=this._configuration.uri.clone();n.user=null,this._configuration.hostport_params=n.toString().replace(/^sip:/i,"");try{this._transport=new Pg(this._configuration.sockets,{max_interval:this._configuration.connection_recovery_max_interval,min_interval:this._configuration.connection_recovery_min_interval}),this._transport.onconnecting=CT.bind(this),this._transport.onconnect=RT.bind(this),this._transport.ondisconnect=IT.bind(this),this._transport.ondata=bT.bind(this)}catch(o){throw rt.warn(o),new Ds.ConfigurationError("sockets",this._configuration.sockets)}if(delete this._configuration.sockets,this._configuration.authorization_user||(this._configuration.authorization_user=this._configuration.uri.user),!this._configuration.registrar_server){const o=this._configuration.uri.clone();o.user=null,o.clearParams(),o.clearHeaders(),this._configuration.registrar_server=o}this._configuration.no_answer_timeout*=1e3,this._configuration.contact_uri?this._configuration.via_host=this._configuration.contact_uri.host:this._configuration.contact_uri=new jn("sip",Ts.createRandomToken(8),this._configuration.via_host,null,{transport:"ws"}),this._contact={pub_gruu:null,temp_gruu:null,uri:this._configuration.contact_uri,toString(o={}){const t=o.anonymous||null,r=o.outbound||null;let _="<";return t?_+=this.temp_gruu||"sip:anonymous@anonymous.invalid;transport=ws":_+=this.pub_gruu||this.uri.toString(),r&&(t?!this.temp_gruu:!this.pub_gruu)&&(_+=";ob"),_+=">",_}};const a=["authorization_user","password","realm","ha1","authorization_jwt","display_name","register"];for(const o in this._configuration)Object.prototype.hasOwnProperty.call(this._configuration,o)&&(a.indexOf(o)!==-1?Object.defineProperty(this._configuration,o,{writable:!0,configurable:!1}):Object.defineProperty(this._configuration,o,{writable:!1,configurable:!1}));rt.debug("configuration parameters after validation:");for(const o in this._configuration)if(Object.prototype.hasOwnProperty.call(ti.settings,o))switch(o){case"uri":case"registrar_server":rt.debug(`- ${o}: ${this._configuration[o]}`);break;case"password":case"ha1":case"authorization_jwt":rt.debug(`- ${o}: NOT SHOWN`);break;default:rt.debug(`- ${o}: ${JSON.stringify(this._configuration[o])}`)}}newMSRPSession(s,n){s.on("msgHistoryUpdate",a=>{console.log(a)}),this._msrp_sessions[s.id]=s,this.emit("newMSRPSession",n)}newJanusSession(s,n){this._janus_sessions[s.id]=s,this.emit("newJanusSession",n)}destroyMSRPSession(s){delete this._msrp_sessions[s.id]}destroyJanusSession(s){delete this._janus_sessions[s.id]}receiveRequest(s){var t,r,_,h;const n=s.method;if(console.log("-----------"),s.ruri.user!==this._configuration.uri.user&&s.ruri.user!==this._contact.uri.user){rt.debug("Request-URI does not point to us"),s.method!==De.ACK&&s.reply_sl(404);return}if(s.ruri.scheme===De.SIPS){s.reply_sl(416);return}if($t.checkTransaction(this,s))return;if(n===De.INVITE?new $t.InviteServerTransaction(this,this._transport,s):n!==De.ACK&&n!==De.CANCEL&&new $t.NonInviteServerTransaction(this,this._transport,s),n===De.OPTIONS){if(this.listeners("newOptions").length===0){s.reply(200);return}new yf.Options(this).init_incoming(s)}else if(n===De.MESSAGE){if(this.listeners("newMessage").length===0){s.reply(405);return}new yf.Message(this).init_incoming(s)}else if(n===De.INVITE&&!s.to_tag&&this.listeners("newRTCSession").length===0){s.reply(405);return}let a,o;if(s.to_tag)if(a=this._findDialog(s.call_id,s.from_tag,s.to_tag),a)a.receiveRequest(s);else if(n===De.NOTIFY)if(o=this._findSession(s),o)o.receiveRequest(s);else{if(s.body)try{const d=JSON.parse(s.body)||{};(r=(t=d.plugindata)==null?void 0:t.data)!=null&&r.publishers&&Object.values(this._janus_sessions)[0].receivePublishers(d),(h=(_=d.plugindata)==null?void 0:_.data)!=null&&h.unpublished&&Object.values(this._janus_sessions)[0].receiveUnpublished(d.plugindata.data.unpublished)}catch(d){console.error(d)}s.reply(200)}else n!==De.ACK&&s.reply(481);else switch(n){case De.INVITE:if(window.RTCPeerConnection)if(s.hasHeader("replaces")){const d=s.replaces;a=this._findDialog(d.call_id,d.from_tag,d.to_tag),a?(o=a.owner,o.isEnded()?s.reply(603):o.receiveRequest(s)):s.reply(481)}else s.body.search(/MSRP/ig)>-1?(o=new Su(this),o.init_incoming(s)):s.body.search(/JANUS/ig)>-1||(o=new Tg(this),o.init_incoming(s));else rt.warn("INVITE received but WebRTC is not supported"),s.reply(488);break;case De.BYE:s.reply(481);break;case De.CANCEL:o=this._findSession(s),o?o.receiveRequest(s):rt.debug("received CANCEL request for a non existent session");break;case De.ACK:break;case De.NOTIFY:this.emit("sipEvent",{event:s.event,request:s}),s.reply(200);break;default:s.reply(405);break}}startMSRP(s,n){rt.debug("startMSRP()",n);const a=new Su(this);return a.connect(s),a}startJanus(s,n){rt.debug("startJanus()",n);const a=new Su(this);return a.connect(s),a}terminateMSRPSessions(s){rt.debug("terminateSessions()");for(const n in this._msrp_sessions)this._msrp_sessions[n].isEnded()||this._msrp_sessions[n].terminate(s)}terminateJanusSessions(s){rt.debug("terminateSessions()");for(const n in this._janus_sessions)this._janus_sessions[n].isEnded()||this._janus_sessions[n].terminate(s)}enableJanusAudio(s){rt.debug("enableJanusAudio()");for(const n in this._janus_sessions)this._janus_sessions[n].isEnded()||(s?this._janus_sessions[n].startAudio():this._janus_sessions[n].stopAudio())}enableJanusVideo(s){rt.debug("enableJanusVideo()");for(const n in this._janus_sessions)this._janus_sessions[n].isEnded()||(s?this._janus_sessions[n].startVideo():this._janus_sessions[n].stopVideo())}stop(){if(rt.debug("stop()"),this._dynConfiguration={},this._status===Ps.STATUS_USER_CLOSED){rt.debug("UA already closed");return}this._registrator.close();const s=Object.keys(this._sessions).length;for(const a in this._sessions)if(Object.prototype.hasOwnProperty.call(this._sessions,a)){rt.debug(`closing session ${a}`);try{this._sessions[a].terminate()}catch(o){console.error(o)}}for(const a in this._msrp_sessions)if(Object.prototype.hasOwnProperty.call(this._msrp_sessions,a)){rt.debug(`closing session ${a}`);try{this._msrp_sessions[a].terminate()}catch(o){console.error(o)}}for(const a in this._janus_sessions)if(Object.prototype.hasOwnProperty.call(this._janus_sessions,a)){rt.debug(`closing session ${a}`);try{this._janus_sessions[a].terminate()}catch(o){console.error(o)}}for(const a in this._applicants)if(Object.prototype.hasOwnProperty.call(this._applicants,a))try{this._applicants[a].close()}catch(o){console.error(o)}this._status=Ps.STATUS_USER_CLOSED,Object.keys(this._transactions.nict).length+Object.keys(this._transactions.nist).length+Object.keys(this._transactions.ict).length+Object.keys(this._transactions.ist).length===0&&s===0?this._transport.disconnect():this._closeTimer=setTimeout(()=>{this._closeTimer=null,this._transport.disconnect()},2e3)}}function CT(c){this.emit("connecting",c)}function RT(c){this._status!==Ps.STATUS_USER_CLOSED&&(this._status=Ps.STATUS_READY,this._error=null,this.emit("connected",c),this._dynConfiguration.register&&this._registrator.register())}function IT(c){const s=["nict","ict","nist","ist"];for(const n of s)for(const a in this._transactions[n])Object.prototype.hasOwnProperty.call(this._transactions[n],a)&&this._transactions[n][a].onTransportError();this.emit("disconnected",c),this._registrator.onTransportClosed(),this._status!==Ps.STATUS_USER_CLOSED&&(this._status=Ps.STATUS_NOT_READY,this._error=Ps.NETWORK_ERROR)}function bT(c){console.log("onTransportData",c);const s=c.transport;let n=c.message;if(n=ET.parseMessage(n,this),!n){console.log("if 1 return");return}if(this._status===Ps.STATUS_USER_CLOSED&&n instanceof en.IncomingRequest){console.log("if 2 return");return}if(!Jg(n,this,s)){console.log("if 3 return");return}if(console.log("onTransportData message",n),console.log("onTransportData instanceof",n instanceof en.IncomingRequest),n instanceof en.IncomingRequest)n.transport=s,console.log("onTransportData receiveRequest"),this.receiveRequest(n);else if(n instanceof en.IncomingResponse){let a;switch(n.method){case De.INVITE:a=this._transactions.ict[n.via_branch],a&&a.receiveResponse(n);break;case De.ACK:break;default:a=this._transactions.nict[n.via_branch],a&&a.receiveResponse(n);break}}}const wT=["roomId","_audioMuted","_cancel_reason","_contact","direction","_end_time","_eventsCount","_from_tag","_id","_is_canceled","_is_confirmed","_late_sdp","_localHold","_videoMuted","status","start_time","_remote_identity","audioTag","isOnHold","localMuted","autoAnswer"],OT=["_cancel_reason","_contact","direction","_end_time","_eventsCount","_from_tag","_id","_is_canceled","_is_confirmed","_late_sdp","status","start_time","_remote_identity","target_addr"];function gh(c){const s={};return wT.forEach(n=>{c[n]!==void 0&&(s[n]=c[n])}),s.localHold=c._localHold,s}function Th(c){const s={};return OT.forEach(n=>{c[n]!==void 0&&(s[n]=c[n])}),s}function vh(c,s){const n=new AudioContext,a=n.createMediaStreamSource(c),o=n.createMediaStreamDestination(),t=n.createGain();return a.connect(t),t.connect(o),t.gain.value=s,o.stream}function DT(c,s,n,a){const o=document.createElement("audio");o.id=s._id,o.className="audioTag",o.srcObject=c,Eh()||(o.setSinkId(n),o.volume=a),o.play(),s.audioTag=o}function NT(c){if(c&&typeof c.log=="function"&&typeof c.warn=="function"&&typeof c.error=="function")return!0}function Eh(){return/Mobi|Android|iPhone/i.test(navigator.userAgent)}const UT=at,xT=Kt(),Ot=new UT("WebSocketInterface");var PT=class{constructor(s){Ot.debug('new() [url:"%s"]',s),this._url=s,this._sip_uri=null,this._via_transport=null,this._ws=null;const n=xT.parse(s,"absoluteURI");if(n===-1)throw Ot.warn(`invalid WebSocket URI: ${s}`),new TypeError(`Invalid argument: ${s}`);if(n.scheme!=="wss"&&n.scheme!=="ws")throw Ot.warn(`invalid WebSocket URI scheme: ${n.scheme}`),new TypeError(`Invalid argument: ${s}`);this._sip_uri=`sip:${n.host}${n.port?`:${n.port}`:""};transport=ws`,this._via_transport=n.scheme.toUpperCase()}get via_transport(){return this._via_transport}set via_transport(s){this._via_transport=s.toUpperCase()}get sip_uri(){return this._sip_uri}get url(){return this._url}connect(){if(Ot.debug("connect()"),this.isConnected()){Ot.debug(`WebSocket ${this._url} is already connected`);return}else if(this.isConnecting()){Ot.debug(`WebSocket ${this._url} is connecting`);return}this._ws&&this.disconnect(),Ot.debug(`connecting to WebSocket ${this._url}`);try{this._ws=new WebSocket(this._url,"sip"),this._ws.binaryType="arraybuffer",this._ws.onopen=this._onOpen.bind(this),this._ws.onclose=this._onClose.bind(this),this._ws.onmessage=this._onMessage.bind(this),this._ws.onerror=this._onError.bind(this)}catch(s){this._onError(s)}}disconnect(){Ot.debug("disconnect()"),this._ws&&(this._ws.onopen=()=>{},this._ws.onclose=()=>{},this._ws.onmessage=()=>{},this._ws.onerror=()=>{},this._ws.close(),this._ws=null)}send(s){return Ot.debug("send()"),this.isConnected()?(this._ws.send(s),!0):(Ot.warn("unable to send message, WebSocket is not open"),!1)}isConnected(){return this._ws&&this._ws.readyState===this._ws.OPEN}isConnecting(){return this._ws&&this._ws.readyState===this._ws.CONNECTING}_onOpen(){Ot.debug(`WebSocket ${this._url} connected`),this.onconnect()}_onClose({wasClean:s,code:n,reason:a}){Ot.debug(`WebSocket ${this._url} closed`),s===!1&&Ot.debug("WebSocket abrupt disconnection"),this.ondisconnect(!s,n,a)}_onMessage({data:s}){Ot.debug("received WebSocket message"),this.ondata(s)}_onError(s){Ot.warn(`WebSocket ${this._url} error: `,s)}};const Au=Tl,kT=De,MT=gs,LT=mt(),$T=dh,HT=ds(),FT=qo(),jT=Kt(),VT=PT;Un("JsSIP")("version %s",Au.version);var qT={C:kT,Exceptions:MT,Utils:LT,UA:$T,URI:HT,NameAddrHeader:FT,WebSocketInterface:VT,Grammar:jT,debug:Un,get name(){return Au.title},get version(){return Au.version}};const BT=xe(qT);function Cu(c){return c<10?`0${c}`:`${c}`}function GT(c){let s=c.hours||0,n=c.minutes||0,a=c.seconds||0;a++,a===60&&(a=0,n++,n===60&&(n=0,s++));const o=`${Cu(s)}:${Cu(n)}:${Cu(a)}`;return{seconds:a,minutes:n,hours:s,formatted:o}}var St={};St.forEach=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)if(o in c){const t=Promise.resolve(c[o]).then(r=>s.call(n||globalThis,r,o,c));a.push(t)}await Promise.all(a)},St.forEachSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)a in c&&await s.call(n||globalThis,await c[a],a,c)},St.map=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)o in c&&(a[o]=Promise.resolve(c[o]).then(t=>s.call(n||globalThis,t,o,c)));return Promise.all(a)},St.mapSeries=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)o in c&&(a[o]=await s.call(n||globalThis,await c[o],o,c));return a},St.find=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a();let t=1;for(let r=0;r<c.length;r++){const _=h=>{h?a(c[r]):t===c.length&&a(),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.findSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(await s.call(n||globalThis,await c[a],a,c))return c[a]},St.findIndex=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a(-1);let t=1;for(let r=0;r<c.length;r++){const _=h=>{h?a(r):t===c.length&&a(-1),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.findIndexSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(await s.call(n||globalThis,await c[a],a,c))return a},St.some=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a(!1);let t=1;for(let r=0;r<c.length;r++){if(!(r in c)){t++;continue}const _=h=>{h?a(!0):t===c.length&&a(!1),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.someSeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(a in c&&await s.call(n||globalThis,await c[a],a,c))return!0;return!1},St.every=(c,s,n)=>new Promise((a,o)=>{if(c.length===0)return a(!0);let t=1;for(let r=0;r<c.length;r++){if(!(r in c)){t++;continue}const _=h=>{h?t===c.length&&a(!0):a(!1),t++};Promise.resolve(c[r]).then(h=>s.call(n||globalThis,h,r,c)).then(_).catch(o)}}),St.everySeries=async(c,s,n)=>{for(let a=0;a<c.length;a++)if(a in c&&!await s.call(n||globalThis,await c[a],a,c))return!1;return!0},St.filter=(c,s,n)=>new Promise(async(a,o)=>{const t=[];for(let _=0;_<c.length;_++)_ in c&&(t[_]=Promise.resolve(c[_]).then(h=>s.call(n||globalThis,h,_,c)).catch(o));const r=[];for(let _=0;_<t.length;_++)await t[_]&&r.push(await c[_]);a(r)}),St.filterSeries=async(c,s,n)=>{const a=[];for(let o=0;o<c.length;o++)o in c&&await s.call(n||globalThis,await c[o],o,c)&&a.push(await c[o]);return a},St.reduce=async(c,s,n)=>{if(c.length===0&&n===void 0)throw TypeError("Reduce of empty array with no initial value");let a,o;for(n!==void 0?(o=n,a=0):(o=c[0],a=1),a;a<c.length;a++)a in c&&(o=await s(await o,await c[a],a,c));return o};var Sh={};(function(c){const s=St;Object.keys(s).forEach(n=>{const a=n.charAt(0).toUpperCase()+n.slice(1);c[`async${a}`]=async function(...o){return s[n](this,...o)}})})(Sh);var WT=Object.assign(St,{instanceMethods:Sh});const ln=new AudioContext,zt={NEW_CALL:"new_call",CALL_CONFIRMED:"confirmed",CALL_FAILED:"failed",CALL_PROGRESS:"progress",CALL_ENDED:"ended"};var KT={exports:{}};(function(c){var s=function(n){var a=Object.prototype,o=a.hasOwnProperty,t=Object.defineProperty||function(ee,Y,G){ee[Y]=G.value},r,_=typeof Symbol=="function"?Symbol:{},h=_.iterator||"@@iterator",d=_.asyncIterator||"@@asyncIterator",g=_.toStringTag||"@@toStringTag";function T(ee,Y,G){return Object.defineProperty(ee,Y,{value:G,enumerable:!0,configurable:!0,writable:!0}),ee[Y]}try{T({},"")}catch{T=function(Y,G,re){return Y[G]=re}}function E(ee,Y,G,re){var ge=Y&&Y.prototype instanceof oe?Y:oe,be=Object.create(ge.prototype),ke=new x(re||[]);return t(be,"_invoke",{value:J(ee,G,ke)}),be}n.wrap=E;function y(ee,Y,G){try{return{type:"normal",arg:ee.call(Y,G)}}catch(re){return{type:"throw",arg:re}}}var A="suspendedStart",C="suspendedYield",I="executing",H="completed",$={};function oe(){}function N(){}function j(){}var Se={};T(Se,h,function(){return this});var ve=Object.getPrototypeOf,k=ve&&ve(ve(se([])));k&&k!==a&&o.call(k,h)&&(Se=k);var D=j.prototype=oe.prototype=Object.create(Se);N.prototype=j,t(D,"constructor",{value:j,configurable:!0}),t(j,"constructor",{value:N,configurable:!0}),N.displayName=T(j,g,"GeneratorFunction");function B(ee){["next","throw","return"].forEach(function(Y){T(ee,Y,function(G){return this._invoke(Y,G)})})}n.isGeneratorFunction=function(ee){var Y=typeof ee=="function"&&ee.constructor;return Y?Y===N||(Y.displayName||Y.name)==="GeneratorFunction":!1},n.mark=function(ee){return Object.setPrototypeOf?Object.setPrototypeOf(ee,j):(ee.__proto__=j,T(ee,g,"GeneratorFunction")),ee.prototype=Object.create(D),ee},n.awrap=function(ee){return{__await:ee}};function q(ee,Y){function G(be,ke,Re,le){var ye=y(ee[be],ee,ke);if(ye.type==="throw")le(ye.arg);else{var lt=ye.arg,ot=lt.value;return ot&&typeof ot=="object"&&o.call(ot,"__await")?Y.resolve(ot.__await).then(function(Ee){G("next",Ee,Re,le)},function(Ee){G("throw",Ee,Re,le)}):Y.resolve(ot).then(function(Ee){lt.value=Ee,Re(lt)},function(Ee){return G("throw",Ee,Re,le)})}}var re;function ge(be,ke){function Re(){return new Y(function(le,ye){G(be,ke,le,ye)})}return re=re?re.then(Re,Re):Re()}t(this,"_invoke",{value:ge})}B(q.prototype),T(q.prototype,d,function(){return this}),n.AsyncIterator=q,n.async=function(ee,Y,G,re,ge){ge===void 0&&(ge=Promise);var be=new q(E(ee,Y,G,re),ge);return n.isGeneratorFunction(Y)?be:be.next().then(function(ke){return ke.done?ke.value:be.next()})};function J(ee,Y,G){var re=A;return function(be,ke){if(re===I)throw new Error("Generator is already running");if(re===H){if(be==="throw")throw ke;return it()}for(G.method=be,G.arg=ke;;){var Re=G.delegate;if(Re){var le=F(Re,G);if(le){if(le===$)continue;return le}}if(G.method==="next")G.sent=G._sent=G.arg;else if(G.method==="throw"){if(re===A)throw re=H,G.arg;G.dispatchException(G.arg)}else G.method==="return"&&G.abrupt("return",G.arg);re=I;var ye=y(ee,Y,G);if(ye.type==="normal"){if(re=G.done?H:C,ye.arg===$)continue;return{value:ye.arg,done:G.done}}else ye.type==="throw"&&(re=H,G.method="throw",G.arg=ye.arg)}}}function F(ee,Y){var G=Y.method,re=ee.iterator[G];if(re===r)return Y.delegate=null,G==="throw"&&ee.iterator.return&&(Y.method="return",Y.arg=r,F(ee,Y),Y.method==="throw")||G!=="return"&&(Y.method="throw",Y.arg=new TypeError("The iterator does not provide a '"+G+"' method")),$;var ge=y(re,ee.iterator,Y.arg);if(ge.type==="throw")return Y.method="throw",Y.arg=ge.arg,Y.delegate=null,$;var be=ge.arg;if(!be)return Y.method="throw",Y.arg=new TypeError("iterator result is not an object"),Y.delegate=null,$;if(be.done)Y[ee.resultName]=be.value,Y.next=ee.nextLoc,Y.method!=="return"&&(Y.method="next",Y.arg=r);else return be;return Y.delegate=null,$}B(D),T(D,g,"Generator"),T(D,h,function(){return this}),T(D,"toString",function(){return"[object Generator]"});function Z(ee){var Y={tryLoc:ee[0]};1 in ee&&(Y.catchLoc=ee[1]),2 in ee&&(Y.finallyLoc=ee[2],Y.afterLoc=ee[3]),this.tryEntries.push(Y)}function U(ee){var Y=ee.completion||{};Y.type="normal",delete Y.arg,ee.completion=Y}function x(ee){this.tryEntries=[{tryLoc:"root"}],ee.forEach(Z,this),this.reset(!0)}n.keys=function(ee){var Y=Object(ee),G=[];for(var re in Y)G.push(re);return G.reverse(),function ge(){for(;G.length;){var be=G.pop();if(be in Y)return ge.value=be,ge.done=!1,ge}return ge.done=!0,ge}};function se(ee){if(ee){var Y=ee[h];if(Y)return Y.call(ee);if(typeof ee.next=="function")return ee;if(!isNaN(ee.length)){var G=-1,re=function ge(){for(;++G<ee.length;)if(o.call(ee,G))return ge.value=ee[G],ge.done=!1,ge;return ge.value=r,ge.done=!0,ge};return re.next=re}}return{next:it}}n.values=se;function it(){return{value:r,done:!0}}return x.prototype={constructor:x,reset:function(ee){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(U),!ee)for(var Y in this)Y.charAt(0)==="t"&&o.call(this,Y)&&!isNaN(+Y.slice(1))&&(this[Y]=r)},stop:function(){this.done=!0;var ee=this.tryEntries[0],Y=ee.completion;if(Y.type==="throw")throw Y.arg;return this.rval},dispatchException:function(ee){if(this.done)throw ee;var Y=this;function G(le,ye){return be.type="throw",be.arg=ee,Y.next=le,ye&&(Y.method="next",Y.arg=r),!!ye}for(var re=this.tryEntries.length-1;re>=0;--re){var ge=this.tryEntries[re],be=ge.completion;if(ge.tryLoc==="root")return G("end");if(ge.tryLoc<=this.prev){var ke=o.call(ge,"catchLoc"),Re=o.call(ge,"finallyLoc");if(ke&&Re){if(this.prev<ge.catchLoc)return G(ge.catchLoc,!0);if(this.prev<ge.finallyLoc)return G(ge.finallyLoc)}else if(ke){if(this.prev<ge.catchLoc)return G(ge.catchLoc,!0)}else if(Re){if(this.prev<ge.finallyLoc)return G(ge.finallyLoc)}else throw new Error("try statement without catch or finally")}}},abrupt:function(ee,Y){for(var G=this.tryEntries.length-1;G>=0;--G){var re=this.tryEntries[G];if(re.tryLoc<=this.prev&&o.call(re,"finallyLoc")&&this.prev<re.finallyLoc){var ge=re;break}}ge&&(ee==="break"||ee==="continue")&&ge.tryLoc<=Y&&Y<=ge.finallyLoc&&(ge=null);var be=ge?ge.completion:{};return be.type=ee,be.arg=Y,ge?(this.method="next",this.next=ge.finallyLoc,$):this.complete(be)},complete:function(ee,Y){if(ee.type==="throw")throw ee.arg;return ee.type==="break"||ee.type==="continue"?this.next=ee.arg:ee.type==="return"?(this.rval=this.arg=ee.arg,this.method="return",this.next="end"):ee.type==="normal"&&Y&&(this.next=Y),$},finish:function(ee){for(var Y=this.tryEntries.length-1;Y>=0;--Y){var G=this.tryEntries[Y];if(G.finallyLoc===ee)return this.complete(G.completion,G.afterLoc),U(G),$}},catch:function(ee){for(var Y=this.tryEntries.length-1;Y>=0;--Y){var G=this.tryEntries[Y];if(G.tryLoc===ee){var re=G.completion;if(re.type==="throw"){var ge=re.arg;U(G)}return ge}}throw new Error("illegal catch attempt")},delegateYield:function(ee,Y,G){return this.delegate={iterator:se(ee),resultName:Y,nextLoc:G},this.method==="next"&&(this.arg=r),$}},n}(c.exports);try{regeneratorRuntime=s}catch{typeof globalThis=="object"?globalThis.regeneratorRuntime=s:Function("r","regeneratorRuntime = r")(s)}})(KT);var yh={exports:{}};(function(c){(function(s,n){c.exports?c.exports=n():s.log=n()})(Gt,function(){var s=function(){},n="undefined",a=typeof window!==n&&typeof window.navigator!==n&&/Trident\/|MSIE /.test(window.navigator.userAgent),o=["trace","debug","info","warn","error"];function t(C,I){var H=C[I];if(typeof H.bind=="function")return H.bind(C);try{return Function.prototype.bind.call(H,C)}catch{return function(){return Function.prototype.apply.apply(H,[C,arguments])}}}function r(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function _(C){return C==="debug"&&(C="log"),typeof console===n?!1:C==="trace"&&a?r:console[C]!==void 0?t(console,C):console.log!==void 0?t(console,"log"):s}function h(C,I){for(var H=0;H<o.length;H++){var $=o[H];this[$]=H<C?s:this.methodFactory($,C,I)}this.log=this.debug}function d(C,I,H){return function(){typeof console!==n&&(h.call(this,I,H),this[C].apply(this,arguments))}}function g(C,I,H){return _(C)||d.apply(this,arguments)}function T(C,I,H){var $=this,oe;I=I??"WARN";var N="loglevel";typeof C=="string"?N+=":"+C:typeof C=="symbol"&&(N=void 0);function j(D){var B=(o[D]||"silent").toUpperCase();if(!(typeof window===n||!N)){try{window.localStorage[N]=B;return}catch{}try{window.document.cookie=encodeURIComponent(N)+"="+B+";"}catch{}}}function Se(){var D;if(!(typeof window===n||!N)){try{D=window.localStorage[N]}catch{}if(typeof D===n)try{var B=window.document.cookie,q=B.indexOf(encodeURIComponent(N)+"=");q!==-1&&(D=/^([^;]+)/.exec(B.slice(q))[1])}catch{}return $.levels[D]===void 0&&(D=void 0),D}}function ve(){if(!(typeof window===n||!N)){try{window.localStorage.removeItem(N);return}catch{}try{window.document.cookie=encodeURIComponent(N)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch{}}}$.name=C,$.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},$.methodFactory=H||g,$.getLevel=function(){return oe},$.setLevel=function(D,B){if(typeof D=="string"&&$.levels[D.toUpperCase()]!==void 0&&(D=$.levels[D.toUpperCase()]),typeof D=="number"&&D>=0&&D<=$.levels.SILENT){if(oe=D,B!==!1&&j(D),h.call($,D,C),typeof console===n&&D<$.levels.SILENT)return"No console available for logging"}else throw"log.setLevel() called with invalid level: "+D},$.setDefaultLevel=function(D){I=D,Se()||$.setLevel(D,!1)},$.resetLevel=function(){$.setLevel(I,!1),ve()},$.enableAll=function(D){$.setLevel($.levels.TRACE,D)},$.disableAll=function(D){$.setLevel($.levels.SILENT,D)};var k=Se();k==null&&(k=I),$.setLevel(k,!1)}var E=new T,y={};E.getLogger=function(I){if(typeof I!="symbol"&&typeof I!="string"||I==="")throw new TypeError("You must supply a name when creating a logger.");var H=y[I];return H||(H=y[I]=new T(I,E.getLevel(),E.methodFactory)),H};var A=typeof window!==n?window.log:void 0;return E.noConflict=function(){return typeof window!==n&&window.log===E&&(window.log=A),E},E.getLoggers=function(){return y},E.default=E,E})})(yh);var It=yh.exports;const vs=()=>`${new Date().toISOString()} | metrics`,Es=(c,s,n)=>`${c} | ${s} | ${n}`;It.setDefaultLevel(It.levels.TRACE);const YT=c=>{It.info(Es(vs(),"log ",`set log level to ${c?"verbose":"info"}`)),It.setLevel(c?It.levels.TRACE:It.levels.INFO)},JT=c=>{const s=[...Object.keys(It.levels)];s.includes(c)?(It.info(Es(vs(),"log ",`update log level to ${c.toLowerCase()}`)),It.setLevel(c)):It.warn(Es(vs(),"log ","Incorrect log level please choose one of "),s)},Pe=(c,s,n)=>{n?It.debug(Es(vs(),c,s),n):It.debug(Es(vs(),c,s))},Ru=(c,s)=>{It.info(Es(vs(),c,s))},an=(c,s)=>{It.info(Es(vs(),c,s))},Ca=(c,s)=>{It.warn(Es(vs(),c,s))},qn=(c,s)=>{It.error(Es(vs(),c,s))};function zT(c){return Math.floor(Math.random()*c).toString()}function Ah(c,s){let n=s;return c.forEach(a=>{n=n.replace(a,"")}),n}function XT(c,s){let n="";for(let a=0;a<s;a+=1)n+=c[zT(c.length)];return n}function QT({length:c=20,useLetters:s=!0,useNumbers:n=!0,includeSymbols:a=[],excludeSymbols:o=[]}={}){let t="abcdefghijklmnopqrstuvwxyz",r="0123456789",_=[],h=[],d=[];return s&&(o.length&&(t=Ah(o,t)),h=t.split("")),n&&(o.length&&(r=Ah(o,r)),d=r.split("")),_=[...h,...d,...a],XT(_,c)}var ZT=QT;const ni=xe(ZT),ev=()=>"WebRTCMetrics",tv=()=>"5.0.3",$e={INBOUND:"inbound",OUTBOUND:"outbound"},Ss={IDLE:"idle",RUNNING:"running",MUTED:"muted"},ri={NEW:"new",CHECKING:"checking",CONNECTED:"connected",COMPLETED:"completed",DISCONNECTED:"disconnected",FAILED:"failed",CLOSED:"closed"},sv=()=>({...{delta_time_to_measure_probes_ms:0,delta_time_consumed_to_measure_ms:0,delta_KBytes_in:0,delta_KBytes_out:0,delta_kbs_in:0,delta_kbs_out:0,total_time_decoded_in:0,total_time_encoded_out:0,probes:[]}}),Ch={level_in:0,codec_id_in:"",codec_in:{mime_type:null,clock_rate:null,sdp_fmtp_line:null},delta_jitter_ms_in:0,percent_packets_lost_in:0,delta_packets_in:0,delta_packets_lost_in:0,total_packets_in:0,total_packets_lost_in:0,total_KBytes_in:0,delta_KBytes_in:0,delta_kbs_in:0,mos_in:0,mos_emodel_in:0,ssrc:"",direction:$e.INBOUND},Rh={level_out:0,codec_id_out:"",codec_out:{mime_type:null,clock_rate:null,sdp_fmtp_line:null},delta_jitter_ms_out:0,delta_rtt_ms_out:null,total_rtt_ms_out:0,total_rtt_measure_out:0,percent_packets_lost_out:0,delta_packets_out:0,delta_packets_lost_out:0,total_packets_out:0,total_packets_lost_out:0,total_KBytes_out:0,delta_KBytes_out:0,delta_kbs_out:0,timestamp_out:null,mos_out:0,mos_emodel_out:0,ssrc:"",direction:$e.OUTBOUND},Ih={codec_id_in:"",size_in:{width:null,height:null,framerate:null},codec_in:{mime_type:null,clock_rate:null},delta_jitter_ms_in:0,percent_packets_lost_in:0,delta_packets_in:0,delta_packets_lost_in:0,total_packets_in:0,total_packets_lost_in:0,total_KBytes_in:0,delta_KBytes_in:0,delta_kbs_in:0,decoder_in:null,delta_ms_decode_frame_in:0,total_frames_decoded_in:0,total_time_decoded_in:0,delta_nack_sent_in:0,delta_pli_sent_in:0,total_nack_sent_in:0,total_pli_sent_in:0,ssrc:"",direction:$e.INBOUND},bh={codec_id_out:"",size_out:{width:null,height:null,framerate:null},codec_out:{mime_type:null,clock_rate:null},delta_jitter_ms_out:0,delta_rtt_ms_out:null,total_rtt_ms_out:0,total_rtt_measure_out:0,percent_packets_lost_out:0,delta_packets_out:0,delta_packets_lost_out:0,total_packets_out:0,total_packets_lost_out:0,total_KBytes_out:0,delta_KBytes_out:0,delta_kbs_out:0,encoder_out:null,delta_ms_encode_frame_out:0,total_time_encoded_out:0,total_frames_encoded_out:0,delta_nack_received_out:0,delta_pli_received_out:0,total_nack_received_out:0,total_pli_received_out:0,limitation_out:{reason:null,durations:null,resolutionChanges:0},timestamp_out:null,ssrc:"",direction:$e.OUTBOUND},nv=c=>{const s={pname:"",call_id:"",user_id:"",timestamp:null,count:0,audio:{},video:{},network:{infrastructure:3,local_candidate_id:"",local_candidate_type:"",local_candidate_protocol:"",local_candidate_relay_protocol:"",remote_candidate_id:"",remote_candidate_type:"",remote_candidate_protocol:""},data:{total_KBytes_in:0,total_KBytes_out:0,delta_KBytes_in:0,delta_KBytes_out:0,delta_kbs_in:0,delta_kbs_out:0,delta_kbs_bandwidth_in:0,delta_kbs_bandwidth_out:0,delta_rtt_connectivity_ms:null,total_rtt_connectivity_ms:0,total_rtt_connectivity_measure:0},experimental:{time_to_measure_ms:0}};if(c){const n={...c,audio:{},video:{},data:{...c.data},network:{...c.network},experimental:{...c.experimental}};return Object.keys(c.audio).forEach(a=>{n.audio[a]={...c.audio[a]}}),Object.keys(c.video).forEach(a=>{n.video[a]={...c.video[a]}}),n}return{...s,audio:{},video:{},data:{...s.data},network:{...s.network},experimental:{...s.experimental}}},rv={refreshEvery:2e3,startAfter:0,stopAfter:-1,verbose:!1,pname:`p-${ni()}`,cid:`c-${ni()}`,uid:`u-${ni()}`,record:!1,ticket:!0},Xt={CANDIDATE_PAIR:"candidate-pair",CODEC:"codec",INBOUND_RTP:"inbound-rtp",LOCAL_CANDIDATE:"local-candidate",MEDIA_SOURCE:"media-source",OUTBOUND_RTP:"outbound-rtp",REMOTE_CANDIDATE:"remote-candidate",REMOTE_INBOUND_RTP:"remote-inbound-rtp",TRACK:"track"},V={AUDIO_LEVEL:"audioLevel",AVAILABLE_OUTGOING_BITRATE:"availableOutgoingBitrate",AVAILABLE_INCOMING_BITRATE:"availableIncomingBitrate",BYTES_RECEIVED:"bytesReceived",BYTES_SENT:"bytesSent",CANDIDATE_TYPE:"candidateType",CHANNELS:"channels",CLOCK_RATE:"clockRate",CODEC_ID:"codecId",CURRENT_ROUND_TRIP_TIME:"currentRoundTripTime",ROUND_TRIP_TIME:"roundTripTime",FRACTION_LOST:"fractionLost",FRAME_HEIGHT:"frameHeight",FRAME_WIDTH:"frameWidth",QUALITY_LIMITATION_REASON:"qualityLimitationReason",QUALITY_LIMITATION_DURATIONS:"qualityLimitationDurations",QUALITY_LIMITATION_RESOLUTION_CHANGES:"qualityLimitationResolutionChanges",ID:"id",JITTER:"jitter",KIND:"kind",MEDIA_TYPE:"mediaType",MIME_TYPE:"mimeType",LOCAL_CANDIDATE_ID:"localCandidateId",NETWORK_TYPE:"networkType",RELAY_PROTOCOL:"relayProtocol",NOMINATED:"nominated",PACKETS_LOST:"packetsLost",PACKETS_RECEIVED:"packetsReceived",PACKETS_SENT:"packetsSent",PROTOCOL:"protocol",PORT:"port",REMOTE_CANDIDATE_ID:"remoteCandidateId",REMOTE_SOURCE:"remoteSource",RESPONSES_RECEIVED:"responsesReceived",SDP_FMTP_LINE:"sdpFmtpLine",SSRC:"ssrc",SELECTED:"selected",STATE:"state",TIMESTAMP:"timestamp",TOTAL_ROUND_TRIP_TIME:"totalRoundTripTime",TOTAL_ROUND_TRIP_TIME_MEASUREMENTS:"roundTripTimeMeasurements",TYPE:"type",DECODER_IMPLEMENTATION:"decoderImplementation",ENCODER_IMPLEMENTATION:"encoderImplementation",FRAMES_DECODED:"framesDecoded",FRAMES_ENCODED:"framesEncoded",FRAMES_PER_SECOND:"framesPerSecond",TOTAL_DECODE_TIME:"totalDecodeTime",TOTAL_ENCODE_TIME:"totalEncodeTime",PLI:"pliCount",NACK:"nackCount"},P={SUCCEEDED:"succeeded",AUDIO:"audio",VIDEO:"video"},ii={ETHERNET:0,CELLULAR_5G:2,WIFI:3,CELLULAR_4G:5,CELLULAR:10},Iu={ETHERNET:"ethernet",CELLULAR_4G:"cellular",WIFI:"wifi"},ne={AUDIO:"audio",VIDEO:"video",NETWORK:"network",DATA:"data"},bu="config ",iv=(c,s={},n)=>{const a={...n,...s};return s.pname||Ca(bu,`Argument [String] 'cfg.pname' for the peerConnection name or id is missing - use generated '${n.pname}'`),s.cid||Ca(bu,`Argument [String] 'cfg.cid' for the call name or id is missing - use generated '${n.cid}'`),s.uid||Ca(bu,`Argument [String] 'cfg.uid' for the user name or id is missing - use generated '${n.uid}'`),a.pc=c,a},lv=(c={})=>{const s={...rv,...c};return s.name=ev(),s.version=tv(),s},li=(c,s,n,a=!1,o)=>{let t=c.map(r=>{if(!n)return r[s];if(!o)return r[s][n];const _=r[s][o];return _?_[n]:null});return t=t.filter(r=>a?Number.isFinite(r)&&r>0:Number.isFinite(r)),t.length===0?[]:t},ks=c=>c.reduce((s,n)=>s+n,0)/c.length,av=()=>`probe-${ni()}`,ov=()=>`coltr-${ni()}`,wh=c=>new Promise(s=>setTimeout(s,c)),wu=(c,s,n)=>{s?c.call(s,n):c(n)},We=(c,s,n,a)=>{const o=li(c,s,n,!0,a);if(o.length===0)return null;const t=o.reduce((d,g)=>d+g,0)/o.length;return t===0?null:o.map(d=>Math.abs(t-d)).reduce((d,g)=>d+g,0)/o.length*100/t},Xe=(c,s,n,a=!1,o)=>{const t=li(c,s,n,a,o);return t.length===0?null:t.reduce((r,_)=>r+_,0)/t.length},on=(c,s,n)=>li(c,s,n).reduce((o,t)=>o+t,0),Ke=(c,s,n,a)=>{const o=li(c,s,n,!0,a);return o.length===0?null:Math.min(...o)},Ye=(c,s,n,a)=>{const o=li(c,s,n,!1,a);return o.length===0?null:Math.max(...o)},yt=(c,s,n,a)=>{const o=c.slice().pop();if(!o)return null;if(!n)return o[s];if(!a)return o[s][n];const t=o[s][a];return t?t[n]:null},uv=c=>c.slice().pop(),ht=(c,s,n)=>{if(!s)return null;const a={};let o=s[P.AUDIO][c];o||(o=n===$e.INBOUND?{...Ch}:{...Rh}),a[P.AUDIO]=o;let t=s[P.VIDEO][c];return t||(t=n===$e.INBOUND?{...Ih}:{...bh}),a[P.VIDEO]=t,a},ai="exporter ",cv="2.0",Oh=(c,s,n)=>{if(!c||c.length===0)return 0;const a=c[c.length-1];if(!a)return 0;const o=a[s][n];if(o){const t=o.total_rtt_ms_out,r=o.total_rtt_measure_out;return!r||!t?Xe(c,s,"delta_rtt_ms_out",!1,n):Number(t/r)}return null},fv=(c,s)=>{if(!c||c.length===0)return 0;const n=c[c.length-1];if(!n)return 0;const a=n[s].total_rtt_connectivity_ms,o=n[s].total_rtt_connectivity_measure;return!o||!a?Xe(c,s,"delta_rtt_connectivity_ms"):Number(a/o)},hv=c=>yt(c,"network","local_candidate_type")!=="relay"?`direct/${yt(c,"network","local_candidate_protocol")}`:`turn/${yt(c,"network","local_candidate_relay_protocol")}`,_v=c=>{const s=yt(c,"network","remote_candidate_type"),n=yt(c,"network","remote_candidate_protocol");return s!=="relay"?`direct/${n}`:`turn/${n}`};class dv{constructor(s){this._start=null,this._end=null,this._cfg=s,this._referenceReport=null,this._reports=[],this._events=[]}start(){Ru(ai,"start() - start exporter...");const s=new Date;return this._start=s.toJSON(),s}stop(){Ru(ai,"stop() - stop exporter...");const s=new Date;return this._end=s.toJSON(),s}saveReferenceReport(s){this._referenceReport=s}getReferenceReport(){return this._referenceReport}addReport(s){this._cfg.ticket&&(Pe(ai,`addReport() - add report to exporter at ${s.timestamp}`),this._reports.push(s))}addCustomEvent(s){this._events.push(s)}reset(){Ru(ai,"resetReports() - reset reports"),this._reports=[],this._referenceReport=null,this._start=null,this._end=null}get ticket(){Pe(ai,"ticket() - generate ticket");const s=yt(this._reports,"audio","total_packets_lost_in"),n=yt(this._reports,"audio","total_packets_in"),a=yt(this._reports,"video","total_packets_lost_in"),o=yt(this._reports,"video","total_packets_in"),t={},r=uv(this._reports);return r&&(Object.keys(r[P.AUDIO]).forEach(_=>{const h=r[P.AUDIO][_];if(t[h.ssrc]={type:P.AUDIO,direction:h.direction},h.direction===$e.INBOUND){const d={avg:Xe(this._reports,P.AUDIO,"delta_jitter_ms_in",!1,_),min:Ke(this._reports,P.AUDIO,"delta_jitter_ms_in",_),max:Ye(this._reports,P.AUDIO,"delta_jitter_ms_in",_),volatility:We(this._reports,P.AUDIO,"delta_jitter_ms_in",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.AUDIO,"delta_kbs_in",!1,_),min:Ke(this._reports,P.AUDIO,"delta_kbs_in",_),max:Ye(this._reports,P.AUDIO,"delta_kbs_in",_),volatility:We(this._reports,P.AUDIO,"delta_kbs_in",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.AUDIO,"delta_KBytes_in",!1,_),min:Ke(this._reports,P.AUDIO,"delta_KBytes_in",_),max:Ye(this._reports,P.AUDIO,"delta_KBytes_in",_),volatility:We(this._reports,P.AUDIO,"delta_KBytes_in",_),_unit:{avg:"KB",min:"KB",max:"KB",volatility:"percent"}},E={emodel:{avg:Xe(this._reports,P.AUDIO,"mos_emodel_in",!1,_),min:Ke(this._reports,P.AUDIO,"mos_emodel_in",_),max:Ye(this._reports,P.AUDIO,"mos_emodel_in",_),volatility:We(this._reports,P.AUDIO,"mos_emodel_in",_)},effective:{avg:Xe(this._reports,P.AUDIO,"mos_in",!1,_),min:Ke(this._reports,P.AUDIO,"mos_in",_),max:Ye(this._reports,P.AUDIO,"mos_in",_),volatility:We(this._reports,P.AUDIO,"mos_in",_)},_unit:{avg:"number (1-5)",min:"number (1-5)",max:"number (1-5)",volatility:"percent"}},y=yt(this._reports,P.AUDIO,"total_packets_lost_in",_),A=yt(this._reports,P.AUDIO,"total_packets_in",_),C={lost:y,avg:Math.round((y/(y+A)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}};t[_].jitter=d,t[_].mos=E,t[_].traffic=T,t[_].bitrate=g,t[_].loss=C}else{const d={avg:Xe(this._reports,P.AUDIO,"delta_jitter_ms_out",!1,_),min:Ke(this._reports,P.AUDIO,"delta_jitter_ms_out",_),max:Ye(this._reports,P.AUDIO,"delta_jitter_ms_out",_),volatility:We(this._reports,P.AUDIO,"delta_jitter_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.AUDIO,"delta_kbs_out",!1,_),min:Ke(this._reports,P.AUDIO,"delta_kbs_out",_),max:Ye(this._reports,P.AUDIO,"delta_kbs_out",_),volatility:We(this._reports,P.AUDIO,"delta_kbs_out",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.AUDIO,"delta_KBytes_out",!1,_),min:Ke(this._reports,P.AUDIO,"delta_KBytes_out",_),max:Ye(this._reports,P.AUDIO,"delta_KBytes_out",_),volatility:We(this._reports,P.AUDIO,"delta_KBytes_out",_),_unit:{avg:"KB",min:"KB",max:"KB",bitrate:"kbs",volatility:"percent"}},E={avg:Oh(this._reports,P.AUDIO,_),min:Ke(this._reports,P.AUDIO,"delta_rtt_ms_out",_),max:Ye(this._reports,P.AUDIO,"delta_rtt_ms_out",_),volatility:We(this._reports,P.AUDIO,"delta_rtt_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},y=yt(this._reports,P.AUDIO,"total_packets_lost_out",_),A=yt(this._reports,P.AUDIO,"total_packets_out",_),C={lost:y,avg:Math.round((y/(y+A)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}},I={emodel:{avg:Xe(this._reports,P.AUDIO,"mos_emodel_out",!1,_),min:Ke(this._reports,P.AUDIO,"mos_emodel_out",_),max:Ye(this._reports,P.AUDIO,"mos_emodel_out",_),volatility:We(this._reports,P.AUDIO,"mos_emodel_out",_)},effective:{avg:Xe(this._reports,P.AUDIO,"mos_out",!1,_),min:Ke(this._reports,P.AUDIO,"mos_out",_),max:Ye(this._reports,P.AUDIO,"mos_out",_),volatility:We(this._reports,P.AUDIO,"mos_out",_)},_unit:{avg:"number (1-5)",min:"number (1-5)",max:"number (1-5)",volatility:"percent"}};t[_].jitter=d,t[_].rtt=E,t[_].traffic=T,t[_].bitrate=g,t[_].loss=C,t[_].mos=I}}),Object.keys(r[P.VIDEO]).forEach(_=>{const h=r[P.VIDEO][_];if(t[_]={type:P.VIDEO,direction:h.direction},h.direction===$e.INBOUND){const d={avg:Xe(this._reports,P.VIDEO,"delta_jitter_ms_in",!1,_),min:Ke(this._reports,P.VIDEO,"delta_jitter_ms_in",_),max:Ye(this._reports,P.VIDEO,"delta_jitter_ms_in",_),volatility:We(this._reports,P.VIDEO,"delta_jitter_ms_in",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.VIDEO,"delta_kbs_in",!1,_),min:Ke(this._reports,P.VIDEO,"delta_kbs_in",_),max:Ye(this._reports,P.VIDEO,"delta_kbs_in",_),volatility:We(this._reports,P.VIDEO,"delta_kbs_in",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.VIDEO,"delta_KBytes_in",!1,_),min:Ke(this._reports,P.VIDEO,"delta_KBytes_in",_),max:Ye(this._reports,P.VIDEO,"delta_KBytes_in",_),volatility:We(this._reports,P.VIDEO,"delta_KBytes_in",_),_unit:{avg:"KB",min:"KB",max:"KB",volatility:"percent"}},E=yt(this._reports,P.VIDEO,"total_packets_lost_in",_),y=yt(this._reports,P.VIDEO,"total_packets_in",_),A={lost:E,avg:Math.round((E/(E+y)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}};t[_].jitter=d,t[_].traffic=T,t[_].bitrate=g,t[_].loss=A}else{const d={avg:Xe(this._reports,P.VIDEO,"delta_jitter_ms_out",!1,_),min:Ke(this._reports,P.VIDEO,"delta_jitter_ms_out",_),max:Ye(this._reports,P.VIDEO,"delta_jitter_ms_out",_),volatility:We(this._reports,P.VIDEO,"delta_jitter_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},g={avg:Xe(this._reports,P.VIDEO,"delta_kbs_out",!1,_),min:Ke(this._reports,P.VIDEO,"delta_kbs_out",_),max:Ye(this._reports,P.VIDEO,"delta_kbs_out",_),volatility:We(this._reports,P.VIDEO,"delta_kbs_out",_),_unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},T={avg:Xe(this._reports,P.VIDEO,"delta_KBytes_out",!1,_),min:Ke(this._reports,P.VIDEO,"delta_KBytes_out",_),max:Ye(this._reports,P.VIDEO,"delta_KBytes_out",_),volatility:We(this._reports,P.VIDEO,"delta_KBytes_out",_),_unit:{avg:"KB",min:"KB",max:"KB",volatility:"percent"}},E={avg:Oh(this._reports,P.VIDEO,_),min:Ke(this._reports,P.VIDEO,"delta_rtt_ms_out",_),max:Ye(this._reports,P.VIDEO,"delta_rtt_ms_out",_),volatility:We(this._reports,P.VIDEO,"delta_rtt_ms_out",_),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},y=yt(this._reports,P.VIDEO,"total_packets_lost_out",_),A=yt(this._reports,P.VIDEO,"total_packets_out",_),C={lost:y,avg:Math.round((y/(y+A)*100||0)*100)/100,_unit:{avg:"percent",lost:"number"}};t[_].jitter=d,t[_].rtt=E,t[_].traffic=T,t[_].bitrate=g,t[_].loss=C}})),{version:cv,configuration:{frequency:this._cfg.refreshEvery},started:this._start,ended:this._end,ua:{agent:navigator.userAgent,pname:this._cfg.pname,user_id:this._cfg.uid},call:{call_id:this._cfg.cid,events:this._events},details:{count:this._reports.length,reports:this._cfg.record?this._reports:[],reference:this._referenceReport||null},ssrc:t,data:{rtt:{avg:fv(this._reports,"data"),min:Ke(this._reports,"data","delta_rtt_connectivity_ms"),max:Ye(this._reports,"data","delta_rtt_connectivity_ms"),volatility:We(this._reports,"data","delta_rtt_connectivity_ms"),_unit:{avg:"ms",min:"ms",max:"ms",volatility:"percent"}},packetsLost:{audio:{in:{avg:Math.round((s/(s+n)*100||0)*100)/100}},video:{in:{avg:Math.round((a/(a+o)*100||0)*100)/100}},unit:{avg:"percent"}},bitrate:{in:{avg:Xe(this._reports,"data","delta_kbs_in"),min:Ke(this._reports,"data","delta_kbs_in"),max:Ye(this._reports,"data","delta_kbs_in"),volatility:We(this._reports,"data","delta_kbs_in")},out:{avg:Xe(this._reports,"data","delta_kbs_out"),min:Ke(this._reports,"data","delta_kbs_out"),max:Ye(this._reports,"data","delta_kbs_out"),volatility:We(this._reports,"data","delta_kbs_out")},unit:{avg:"kbs",min:"kbs",max:"kbs",volatility:"percent"}},traffic:{in:{avg:Xe(this._reports,"data","delta_KBytes_in"),min:Ke(this._reports,"data","delta_KBytes_in"),max:Ye(this._reports,"data","delta_KBytes_in"),volatility:We(this._reports,"data","delta_KBytes_in")},out:{avg:Xe(this._reports,"data","delta_KBytes_out"),min:Ke(this._reports,"data","delta_KBytes_out"),max:Ye(this._reports,"data","delta_KBytes_out"),volatility:We(this._reports,"data","delta_KBytes_out")},unit:{avg:"KBytes",min:"KBytes",max:"KBytes",volatility:"percent"}},network:{localConnection:hv(this._reports),remoteConnection:_v(this._reports)}}}}updateConfig(s){this._cfg=s}getLastReport(){return this._reports.slice().pop()||null}getBeforeLastReport(){const s=this._reports.slice();return s.pop(),s.pop()||null}getReportsNumber(){return this._reports.length}}const Ms="extractor ",Dh=(c,s,n,a)=>{let o=!1;const t=a[s].total_rtt_ms_out,r=a[s].total_rtt_measure_out,_=n?n[s].total_rtt_ms_out:0,h=n?n[s].total_rtt_measure_out:0,d={rtt:null,totalRTT:t,totalRTTMeasurements:r};if(c[V.TIMESTAMP]===a[s].timestamp_out||!Object.prototype.hasOwnProperty.call(c,V.ROUND_TRIP_TIME)||Object.prototype.hasOwnProperty.call(c,V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS)&&(o=!0,Number(c[V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS])===0||Number(c[V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS])-h===r))return d;const g=1e3*Number(c[V.ROUND_TRIP_TIME]);let T=t+g,E=r+1;return o&&(T=1e3*Number(c[V.TOTAL_ROUND_TRIP_TIME])-_,E=Number(c[V.TOTAL_ROUND_TRIP_TIME_MEASUREMENTS])-h),{rtt:g,totalRTT:T,totalRTTMeasurements:E}},pv=(c,s,n,a)=>{if(!Object.prototype.hasOwnProperty.call(c,V.CURRENT_ROUND_TRIP_TIME))return{rtt:null,totalRTT:a[s].total_rtt_connectivity_ms,totalRTTMeasurements:a[s].total_rtt_connectivity_measure};const o=1e3*Number(c[V.CURRENT_ROUND_TRIP_TIME]);let t=a[s].total_rtt_connectivity_ms+o,r=a[s].total_rtt_connectivity_measure+1;return Object.prototype.hasOwnProperty.call(c,V.TOTAL_ROUND_TRIP_TIME)&&(t=1e3*Number(c[V.TOTAL_ROUND_TRIP_TIME])-(n?n[s].total_rtt_connectivity_ms:0)),Object.prototype.hasOwnProperty.call(c,V.RESPONSES_RECEIVED)&&(r=Number(c[V.RESPONSES_RECEIVED])-(n?n[s].total_rtt_connectivity_measure:0)),{rtt:o,totalRTT:t,totalRTTMeasurements:r}},Ra=(c,s,n)=>c[V.TIMESTAMP]===n[s].timestamp_out||!Object.prototype.hasOwnProperty.call(c,V.JITTER)?null:1e3*(Number(c[V.JITTER])||0),mv=(c,s)=>{if(!Object.prototype.hasOwnProperty.call(c,V.FRAMES_DECODED)||!Object.prototype.hasOwnProperty.call(c,V.TOTAL_DECODE_TIME))return{delta_ms_decode_frame:s[P.VIDEO].delta_ms_decode_frame_in,frames_decoded:s[P.VIDEO].total_frames_decoded_in,total_decode_time:s[P.VIDEO].total_time_decoded_in};const n=c[V.FRAMES_DECODED],a=c[V.TOTAL_DECODE_TIME],o=a-s[P.VIDEO].total_time_decoded_in,t=n-s[P.VIDEO].total_frames_decoded_in;return{delta_ms_decode_frame:t>0?o*1e3/t:0,frames_decoded:n,total_decode_time:a}},gv=(c,s)=>{if(!Object.prototype.hasOwnProperty.call(c,V.FRAMES_ENCODED)||!Object.prototype.hasOwnProperty.call(c,V.TOTAL_ENCODE_TIME))return{delta_ms_encode_frame:s[P.VIDEO].delta_ms_encode_frame_out,frames_encoded:s[P.VIDEO].total_frames_encoded_out,total_encode_time:s[P.VIDEO].total_time_encoded_out};const n=c[V.FRAMES_ENCODED],a=c[V.TOTAL_ENCODE_TIME],o=a-s[P.VIDEO].total_time_encoded_out,t=n-s[P.VIDEO].total_frames_encoded_out;return{delta_ms_encode_frame:t>0&&o?o*1e3/t:0,frames_encoded:n,total_encode_time:a}},Nh=(c,s,n,a)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PACKETS_SENT)||!Object.prototype.hasOwnProperty.call(c,V.BYTES_SENT))return{packetsSent:n[s].total_packets_out,packetsLost:n[s].total_packets_lost_out,bytesSent:n[s].total_KBytes_out};const o=Number(c[V.PACKETS_SENT])||0-(a?a[s].total_packets_out:0),t=o-n[s].total_packets_out,r=Number(c[V.BYTES_SENT])/1024-(a?a[s].total_KBytes_out:0),_=r-n[s].total_KBytes_out,h=c[V.TIMESTAMP]||Date.now(),d=a?a.timestamp:null;let g=n.timestamp;!g&&d&&(g=d);const T=g?h-g:0,E=T>0?_*.008*1024/T*1e3:0;return{packetsSent:o,deltaPacketsSent:t,KBytesSent:r,deltaKBytesSent:_,kbsSent:E}},Uh=(c,s,n,a)=>{let o=n[s].total_packets_lost_out,t=0,r=0;return Object.prototype.hasOwnProperty.call(c,V.PACKETS_LOST)&&(o=Number(c[V.PACKETS_LOST])||0-(a?a[s].total_packets_lost_out:0),t=o-n[s].total_packets_lost_out),Object.prototype.hasOwnProperty.call(c,V.FRACTION_LOST)&&(r=Number(100*c[V.FRACTION_LOST])),{packetsLost:o,deltaPacketsLost:t,fractionLost:r}},xh=(c,s,n,a)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PACKETS_RECEIVED)||!Object.prototype.hasOwnProperty.call(c,V.PACKETS_LOST)||!Object.prototype.hasOwnProperty.call(c,V.BYTES_RECEIVED))return{percent_packets_lost:n[s].percent_packets_lost_in,packetsReceived:n[s].total_packets_in,packetsLost:n[s].total_packets_lost_in,bytesReceived:n[s].total_KBytes_in};const o=Number(c[V.PACKETS_RECEIVED])||0-(a?a[s].total_packets_in:0),t=Number(c[V.PACKETS_LOST])||0-(a?a[s].total_packets_lost_in:0),r=t-n[s].total_packets_lost_in,_=o-n[s].total_packets_in,h=o!==n[s].total_packets_in?r*100/(r+_):0,d=Number(c[V.BYTES_RECEIVED])/1024-(a?a[s].total_KBytes_in:0),g=d-n[s].total_KBytes_in,T=c[V.TIMESTAMP]||Date.now(),E=a?a.timestamp:null;let y=n.timestamp;!y&&E&&(y=E);const A=y?T-y:0,C=A>0?g*.008*1024/A*1e3:0;return{percentPacketsLost:h,packetsReceived:o,deltaPacketsReceived:_,packetsLost:t,deltaPacketsLost:r,KBytesReceived:d,deltaKBytesReceived:g,kbsReceived:C}},Tv=c=>c[V.CANDIDATE_TYPE]!=="relay"?"":c[V.RELAY_PROTOCOL]||"",vv=c=>{if(!Object.prototype.hasOwnProperty.call(c,V.NETWORK_TYPE))return ii.WIFI;switch(c[V.NETWORK_TYPE]){case Iu.ETHERNET:return ii.ETHERNET;case Iu.CELLULAR_4G:return ii.CELLULAR_4G;case Iu.WIFI:return ii.WIFI;default:return ii.CELLULAR}},Ph=c=>!Object.prototype.hasOwnProperty.call(c,V.FRAME_HEIGHT)||!Object.prototype.hasOwnProperty.call(c,V.FRAME_WIDTH)?{width:null,height:null,framerate:null}:{width:c[V.FRAME_WIDTH]||null,height:c[V.FRAME_HEIGHT]||null,framerate:c[V.FRAMES_PER_SECOND]},Ev=c=>{const s=Object.prototype.hasOwnProperty.call(c,V.QUALITY_LIMITATION_REASON)?c[V.QUALITY_LIMITATION_REASON]:null,n=Object.prototype.hasOwnProperty.call(c,V.QUALITY_LIMITATION_RESOLUTION_CHANGES)?c[V.QUALITY_LIMITATION_RESOLUTION_CHANGES]:null,a=Object.prototype.hasOwnProperty.call(c,V.QUALITY_LIMITATION_DURATIONS)?c[V.QUALITY_LIMITATION_DURATIONS]:null;return a&&Object.keys(a).forEach(o=>{a[o]>1e3&&(a[o]=Number(a[o]/1e3))}),{reason:s,durations:a,resolutionChanges:n}},Sv=(c,s,n)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PLI)||!Object.prototype.hasOwnProperty.call(c,V.NACK))return{pliCount:s.total_pli_sent_in,nackCount:s.total_nack_sent_in,deltaPliCount:0,deltaNackCount:0};const a=(c[V.PLI]||0)-(n?n[P.VIDEO].total_pli_sent_in:0),o=(c[V.NACK]||0)-(n?n[P.VIDEO].total_nack_sent_in:0);return{pliCount:a,nackCount:o,deltaPliCount:a-s[P.VIDEO].total_pli_sent_in,deltaNackCount:o-s[P.VIDEO].total_nack_sent_in}},yv=(c,s,n)=>{if(!Object.prototype.hasOwnProperty.call(c,V.PLI)||!Object.prototype.hasOwnProperty.call(c,V.NACK))return{pliCount:s.total_pli_received_out,nackCount:s.total_nack_received_out,deltaPliCount:0,deltaNackCount:0};const a=(c[V.PLI]||0)-(n?n[P.VIDEO].total_pli_received_out:0),o=(c[V.NACK]||0)-(n?n[P.VIDEO].total_nack_received_out:0);return{pliCount:a,nackCount:o,deltaPliCount:a-s[P.VIDEO].total_pli_received_out,deltaNackCount:o-s[P.VIDEO].total_nack_received_out}},Av=c=>({channels:c[V.CHANNELS]||null,clock_rate:c[V.CLOCK_RATE]||null,mime_type:c[V.MIME_TYPE]||null,sdp_fmtp_line:c[V.SDP_FMTP_LINE]||null}),Cv=c=>({clock_rate:c[V.CLOCK_RATE]||null,mime_type:c[V.MIME_TYPE]||null}),Rv=(c,s,n)=>{const a=(c[V.BYTES_RECEIVED]||0)/1024-(n?n.data.total_KBytes_in:0),o=(c[V.BYTES_SENT]||0)/1024-(n?n.data.total_KBytes_out:0),t=c[V.TIMESTAMP]||Date.now(),r=a-s.data.total_KBytes_in,_=o-s.data.total_KBytes_out,h=n?n.timestamp:null;let d=s.timestamp;!d&&h&&(d=h);const g=d?t-d:0,T=g>0?r*.008*1024/g*1e3:0,E=g>0?_*.008*1024/g*1e3:0;return{total_KBytes_received:a,total_KBytes_sent:o,delta_KBytes_received:r,delta_KBytes_sent:_,kbs_speed_received:T,kbs_speed_sent:E}},Iv=c=>{const s=c[V.AVAILABLE_INCOMING_BITRATE]/1024||0,n=c[V.AVAILABLE_OUTGOING_BITRATE]/1024||0;return{kbs_incoming_bandwidth:s,kbs_outgoing_bandwidth:n}},bv=(c,s,n,a)=>{if(!c)return[];switch(c[V.TYPE]){case Xt.CANDIDATE_PAIR:let o=!1;if(c[V.NOMINATED]&&c[V.STATE]===P.SUCCEEDED&&(o=!0,Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c),V.SELECTED in c&&!c[V.SELECTED]&&(o=!1)),o){const r=c[V.LOCAL_CANDIDATE_ID],_=c[V.REMOTE_CANDIDATE_ID],h=Rv(c,s,a),d=Iv(c),g=pv(c,"data",a,s);return[{type:ne.NETWORK,value:{local_candidate_id:r}},{type:ne.NETWORK,value:{remote_candidate_id:_}},{type:ne.DATA,value:{total_KBytes_in:h.total_KBytes_received}},{type:ne.DATA,value:{total_KBytes_out:h.total_KBytes_sent}},{type:ne.DATA,value:{delta_KBytes_in:h.delta_KBytes_received}},{type:ne.DATA,value:{delta_KBytes_out:h.delta_KBytes_sent}},{type:ne.DATA,value:{delta_kbs_in:h.kbs_speed_received}},{type:ne.DATA,value:{delta_kbs_out:h.kbs_speed_sent}},{type:ne.DATA,value:{delta_kbs_bandwidth_in:d.kbs_incoming_bandwidth}},{type:ne.DATA,value:{delta_kbs_bandwidth_out:d.kbs_outgoing_bandwidth}},{type:ne.DATA,value:{delta_rtt_connectivity_ms:g.rtt}},{type:ne.DATA,value:{total_rtt_connectivity_ms:g.totalRTT}},{type:ne.DATA,value:{total_rtt_connectivity_measure:g.totalRTTMeasurements}}]}break;case Xt.LOCAL_CANDIDATE:if(c[V.ID]===s.network.local_candidate_id)return[{type:ne.NETWORK,value:{infrastructure:vv(c)}},{type:ne.NETWORK,value:{local_candidate_type:c[V.CANDIDATE_TYPE]||""}},{type:ne.NETWORK,value:{local_candidate_protocol:c[V.PROTOCOL]||""}},{type:ne.NETWORK,value:{local_candidate_relay_protocol:Tv(c)}}];break;case Xt.REMOTE_CANDIDATE:if(c[V.ID]===s.network.remote_candidate_id)return[{type:ne.NETWORK,value:{remote_candidate_type:c[V.CANDIDATE_TYPE]||""}},{type:ne.NETWORK,value:{remote_candidate_protocol:c[V.PROTOCOL]||""}}];break;case Xt.INBOUND_RTP:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const r=c[V.SSRC],_=ht(r,s,$e.INBOUND);_&&(_.timestamp=s.timestamp);const h=ht(r,a,$e.INBOUND);if(h&&(h.timestamp=a.timestamp),c[V.MEDIA_TYPE]===P.AUDIO){const d=xh(c,P.AUDIO,_,h),g=Ra(c,P.AUDIO,_),T=c[V.CODEC_ID]||"";return[{ssrc:r,type:ne.AUDIO,value:{codec_id_in:T}},{ssrc:r,type:ne.AUDIO,value:{total_packets_in:d.packetsReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_in:d.deltaPacketsReceived}},{ssrc:r,type:ne.AUDIO,value:{total_packets_lost_in:d.packetsLost}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_lost_in:d.deltaPacketsLost}},{ssrc:r,type:ne.AUDIO,value:{percent_packets_lost_in:d.percentPacketsLost}},{ssrc:r,type:ne.AUDIO,value:{total_KBytes_in:d.KBytesReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_KBytes_in:d.deltaKBytesReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_kbs_in:d.kbsReceived}},{ssrc:r,type:ne.AUDIO,value:{delta_jitter_ms_in:g}}]}if(c[V.MEDIA_TYPE]===P.VIDEO){const d=mv(c,_),g=xh(c,P.VIDEO,_,h),T=Ra(c,P.VIDEO,_),E=c[V.DECODER_IMPLEMENTATION]||null,y=c[V.CODEC_ID]||null,A=Ph(c),C=Sv(c,_,h);return[{ssrc:r,type:ne.VIDEO,value:{codec_id_in:y}},{ssrc:r,type:ne.VIDEO,value:{total_packets_in:g.packetsReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_in:g.deltaPacketsReceived}},{ssrc:r,type:ne.VIDEO,value:{total_packets_lost_in:g.packetsLost}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_lost_in:g.deltaPacketsLost}},{ssrc:r,type:ne.VIDEO,value:{percent_packets_lost_in:g.percentPacketsLost}},{ssrc:r,type:ne.VIDEO,value:{total_KBytes_in:g.KBytesReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_KBytes_in:g.deltaKBytesReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_kbs_in:g.kbsReceived}},{ssrc:r,type:ne.VIDEO,value:{delta_jitter_ms_in:T}},{ssrc:r,type:ne.VIDEO,value:{decoder_in:E}},{ssrc:r,type:ne.VIDEO,value:{delta_ms_decode_frame_in:d.delta_ms_decode_frame}},{ssrc:r,type:ne.VIDEO,value:{total_frames_decoded_in:d.frames_decoded}},{ssrc:r,type:ne.VIDEO,value:{total_time_decoded_in:d.total_decode_time}},{ssrc:r,type:ne.VIDEO,value:{total_nack_sent_in:C.nackCount}},{ssrc:r,type:ne.VIDEO,value:{delta_nack_sent_in:C.deltaNackCount}},{ssrc:r,type:ne.VIDEO,value:{total_pli_sent_in:C.pliCount}},{ssrc:r,type:ne.VIDEO,value:{delta_pli_sent_in:C.deltaPliCount}},{ssrc:r,type:ne.VIDEO,value:{size_in:A}}]}break}case Xt.OUTBOUND_RTP:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const r=c[V.SSRC],_=ht(r,s,$e.OUTBOUND);_&&(_.timestamp=s.timestamp);const h=ht(r,a,$e.OUTBOUND);if(h&&(h.timestamp=a.timestamp),c[V.MEDIA_TYPE]===P.AUDIO){const d=c[V.CODEC_ID]||null,g=Nh(c,P.AUDIO,_,h);return[{ssrc:r,type:ne.AUDIO,value:{codec_id_out:d}},{ssrc:r,type:ne.AUDIO,value:{total_packets_out:g.packetsSent}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_out:g.deltaPacketsSent}},{ssrc:r,type:ne.AUDIO,value:{total_KBytes_out:g.KBytesSent}},{ssrc:r,type:ne.AUDIO,value:{delta_KBytes_out:g.deltaKBytesSent}},{ssrc:r,type:ne.AUDIO,value:{delta_kbs_out:g.kbsSent}}]}if(c[V.MEDIA_TYPE]===P.VIDEO){const d=c[V.ENCODER_IMPLEMENTATION]||null,g=c[V.CODEC_ID]||null,T=gv(c,_),E=Ph(c),y=Ev(c),A=yv(c,_,h),C=Nh(c,P.VIDEO,_,h);return[{ssrc:r,type:ne.VIDEO,value:{codec_id_out:g}},{ssrc:r,type:ne.VIDEO,value:{total_packets_out:C.packetsSent}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_out:C.deltaPacketsSent}},{ssrc:r,type:ne.VIDEO,value:{total_KBytes_out:C.KBytesSent}},{ssrc:r,type:ne.VIDEO,value:{delta_KBytes_out:C.deltaKBytesSent}},{ssrc:r,type:ne.VIDEO,value:{delta_kbs_out:C.kbsSent}},{ssrc:r,type:ne.VIDEO,value:{encoder_out:d}},{ssrc:r,type:ne.VIDEO,value:{delta_ms_encode_frame_out:T.delta_ms_encode_frame}},{ssrc:r,type:ne.VIDEO,value:{total_frames_encoded_out:T.frames_encoded}},{ssrc:r,type:ne.VIDEO,value:{total_time_encoded_out:T.total_encode_time}},{ssrc:r,type:ne.VIDEO,value:{total_nack_received_out:A.nackCount}},{ssrc:r,type:ne.VIDEO,value:{delta_nack_received_out:A.deltaNackCount}},{ssrc:r,type:ne.VIDEO,value:{total_pli_received_out:A.pliCount}},{ssrc:r,type:ne.VIDEO,value:{delta_pli_received_out:A.deltaPliCount}},{ssrc:r,type:ne.VIDEO,value:{size_out:E}},{ssrc:r,type:ne.VIDEO,value:{limitation_out:y}}]}break}case Xt.MEDIA_SOURCE:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);break}case Xt.TRACK:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);break}case Xt.CODEC:const t=[];return Object.keys(s[P.AUDIO]).forEach(r=>{const _=s[P.AUDIO][r];if(_.codec_id_in===c[V.ID]||_.codec_id_out===c[V.ID]){Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const h=Av(c);c[V.ID]===_.codec_id_in?t.push({ssrc:_.ssrc,type:ne.AUDIO,value:{codec_in:h}}):t.push({ssrc:_.ssrc,type:ne.AUDIO,value:{codec_out:h}})}}),Object.keys(s[P.VIDEO]).forEach(r=>{const _=s[P.VIDEO][r];if(_.codec_id_in===c[V.ID]||_.codec_id_out===c[V.ID]){Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const h=Cv(c);c[V.ID]===_.codec_id_in?t.push({ssrc:_.ssrc,type:ne.VIDEO,value:{codec_in:h}}):t.push({ssrc:_.ssrc,type:ne.VIDEO,value:{codec_out:h}})}}),t;case Xt.REMOTE_INBOUND_RTP:{Pe(Ms,`analyze() - got stats ${c[V.TYPE]} for ${n}`,c);const r=c[V.SSRC],_=ht(r,s,$e.OUTBOUND),h=ht(r,a,$e.OUTBOUND);if(c[V.KIND]===P.AUDIO){const d=Dh(c,P.AUDIO,h,_),g=Ra(c,P.AUDIO,_),T=Uh(c,P.AUDIO,_,h);return[{ssrc:r,type:ne.AUDIO,value:{delta_rtt_ms_out:d.rtt}},{ssrc:r,type:ne.AUDIO,value:{total_rtt_ms_out:d.totalRTT}},{ssrc:r,type:ne.AUDIO,value:{total_rtt_measure_out:d.totalRTTMeasurements}},{ssrc:r,type:ne.AUDIO,value:{delta_jitter_ms_out:g}},{ssrc:r,type:ne.AUDIO,value:{timestamp_out:c[V.TIMESTAMP]}},{ssrc:r,type:ne.AUDIO,value:{total_packets_lost_out:T.packetsLost}},{ssrc:r,type:ne.AUDIO,value:{delta_packets_lost_out:T.deltaPacketsLost}},{ssrc:r,type:ne.AUDIO,value:{percent_packets_lost_out:T.fractionLost}}]}if(c[V.KIND]===P.VIDEO){const d=Dh(c,P.VIDEO,h,_),g=Ra(c,P.VIDEO,_),T=Uh(c,P.VIDEO,_,h);return[{ssrc:r,type:ne.VIDEO,value:{delta_rtt_ms_out:d.rtt}},{ssrc:r,type:ne.VIDEO,value:{total_rtt_ms_out:d.totalRTT}},{ssrc:r,type:ne.VIDEO,value:{total_rtt_measure_out:d.totalRTTMeasurements}},{ssrc:r,type:ne.VIDEO,value:{delta_jitter_ms_out:g}},{ssrc:r,type:ne.VIDEO,value:{timestamp_out:c[V.TIMESTAMP]}},{ssrc:r,type:ne.VIDEO,value:{total_packets_lost_out:T.packetsLost}},{ssrc:r,type:ne.VIDEO,value:{delta_packets_lost_out:T.deltaPacketsLost}},{ssrc:r,type:ne.VIDEO,value:{percent_packets_lost_out:T.fractionLost}}]}break}}return[]},Ia=c=>c<0?1:c>100?4.5:1+.035*c+7/1e6*c*(c-60)*(100-c),wv=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.INBOUND),r=ht(o,n,$e.INBOUND),_=ht(o,a,$e.INBOUND),h=[],d=[],g=t[s].percent_packets_lost_in,T=t[s].delta_jitter_ms_in,E=r&&r[s].delta_jitter_ms_in||null,y=_&&_[s].delta_jitter_ms_in||null,A=c.data.delta_rtt_connectivity_ms,C=n&&n.data.delta_rtt_connectivity_ms||null,I=a&&a.data.delta_rtt_connectivity_ms||null;A&&h.push(A),C&&h.push(C),I&&h.push(I),T&&d.push(T),n&&E&&d.push(E),a&&y&&d.push(y);const H=h.length>0?ks(h):100,$=d.length>0?ks(d):10,oe=93.2-g,N=.18*oe*oe-27.9*oe+1126.62,j=(H+$)/2,Se=j-177.3<0?0:1,ve=.024*j+.11*(j-177.3)*Se,k=N-ve;return Ia(k)},Ov=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.OUTBOUND),r=ht(o,n,$e.OUTBOUND),_=ht(o,a,$e.OUTBOUND),h=[],d=[],g=t[s].percent_packets_lost_out,T=t[s].delta_rtt_ms_out,E=r&&r[s].delta_rtt_ms_out||null,y=_&&_[s].delta_rtt_ms_out||null,A=t[s].delta_jitter_ms_out,C=r&&r[s].delta_jitter_ms_out||null,I=_&&_[s].delta_jitter_ms_out||null,H=c.data.delta_rtt_connectivity_ms,$=n&&n.data.delta_rtt_connectivity_ms||null,oe=a&&a.data.delta_rtt_connectivity_ms||null;T?h.push(T):H&&h.push(H),E?h.push(E):$&&h.push($),y?h.push(y):oe&&h.push(oe),A&&d.push(A),n&&C&&d.push(C),a&&I&&d.push(I);const N=h.length>0?ks(h):100,j=d.length>0?ks(d):10,Se=93.2-g,ve=.18*Se*Se-27.9*Se+1126.62,k=(N+j)/2,D=k-177.3<0?0:1,B=.024*k+.11*(k-177.3)*D,q=ve-B;return Ia(q)},Dv=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.INBOUND),r=ht(o,n,$e.INBOUND),_=ht(o,a,$e.INBOUND),h=[],d=[],g=t[s].percent_packets_lost_in/100,T=t[s].delta_jitter_ms_in,E=r&&r[s].delta_jitter_ms_in||null,y=_&&_[s].delta_jitter_ms_in||null,A=c.data.delta_rtt_connectivity_ms,C=n&&n.data.delta_rtt_connectivity_ms||null,I=a&&a.data.delta_rtt_connectivity_ms||null;A&&h.push(A),C&&h.push(C),I&&h.push(I),T&&d.push(T),r&&E&&d.push(E),_&&y&&d.push(y);const H=h.length>0?ks(h):100,$=d.length>0?ks(d):10,oe=0,N=19.8,j=29.7,Se=30,ve=(H+$)/2+Se,k=ve-177.3<0?0:1,D=.024*ve+.11*(ve-177.3)*k,q=93.2-(oe+N*Math.log(1+j*g)+D);return Ia(q)},Nv=(c,s=P.AUDIO,n,a,o)=>{const t=ht(o,c,$e.OUTBOUND),r=ht(o,n,$e.OUTBOUND),_=ht(o,a,$e.OUTBOUND),h=[],d=[],g=t[s].percent_packets_lost_out/100,T=t[s].delta_rtt_ms_out,E=r&&r[s].delta_rtt_ms_out||null,y=_&&_[s].delta_rtt_ms_out||null,A=t[s].delta_jitter_ms_out,C=r&&r[s].delta_jitter_ms_out||null,I=_&&_[s].delta_jitter_ms_out||null,H=c.data.delta_rtt_connectivity_ms,$=n&&n.data.delta_rtt_connectivity_ms||null,oe=a&&a.data.delta_rtt_connectivity_ms||null;T?h.push(T):H&&h.push(H),E?h.push(E):$&&h.push($),y?h.push(y):oe&&h.push(oe),A&&d.push(A),r&&C&&d.push(C),_&&I&&d.push(I);const N=h.length>0?ks(h):100,j=d.length>0?ks(d):10,Se=0,ve=19.8,k=29.7,D=30,B=(N+j)/2+D,q=B-177.3<0?0:1,J=.024*B+.11*(B-177.3)*q,Z=93.2-(Se+ve*Math.log(1+k*g)+J);return Ia(Z)};class Uv{constructor(s,n){this._callbacks={onreport:null,onticket:null},this._id=ov(),this._moduleName=this._id,this._probeId=n,this._config=s,this._exporter=new dv(s),this._state=Ss.IDLE,this.registerToPCEvents(),an(this._moduleName,`new collector created for probe ${this._probeId}`)}analyze(s,n,a,o){const t=(h,d)=>h===P.AUDIO?d===Xt.INBOUND_RTP?{...Ch}:{...Rh}:d===Xt.INBOUND_RTP?{...Ih}:{...bh},r=nv(n);r.pname=this._config.pname,r.call_id=this._config.cid,r.user_id=this._config.uid,r.count=n?n.count+1:1;let _=null;return s.forEach(h=>{!_&&h.timestamp&&(_=h.timestamp),bv(h,r,r.pname,o).forEach(g=>{if(g.value&&g.type)if(g.ssrc){let T=r[g.type][g.ssrc];T||(T=t(g.type,h.type),T.ssrc=g.ssrc,r[g.type][g.ssrc]=T),Object.keys(g.value).forEach(E=>{T[E]=g.value[E]})}else Object.keys(g.value).forEach(T=>{r[g.type][T]=g.value[T]})})}),r.timestamp=_,Object.keys(r[P.AUDIO]).forEach(h=>{const d=r[P.AUDIO][h];d.direction===$e.INBOUND?(d.mos_emodel_in=wv(r,P.AUDIO,n,a,d.ssrc),d.mos_in=Dv(r,P.AUDIO,n,a,d.ssrc)):(d.mos_emodel_out=Ov(r,P.AUDIO,n,a,d.ssrc),d.mos_out=Nv(r,P.AUDIO,n,a,d.ssrc))}),r}async takeReferenceStats(){return new Promise((s,n)=>{const a=Date.now();setTimeout(async()=>{try{const o=Date.now()-a,t=Date.now(),r=await this._config.pc.getStats(),_=this.analyze(r,null,null,null),h=Date.now();_.experimental.time_to_measure_ms=h-t,_.experimental.time_to_wait_ms=o,this._exporter.saveReferenceReport(_),Pe(this._moduleName,`got reference report for probe ${this._probeId}`),s()}catch(o){n(o)}},this._config.startAfter)})}async collectStats(){try{if(this._state!==Ss.RUNNING||!this._config.pc)return Pe(this._moduleName,`report discarded (too late) for probe ${this._probeId}`),null;const s=Date.now(),n=await this._config.pc.getStats(),a=this.analyze(n,this._exporter.getLastReport(),this._exporter.getBeforeLastReport(),this._exporter.getReferenceReport()),o=Date.now();return a.experimental.time_to_measure_ms=o-s,this._exporter.addReport(a),Pe(this._moduleName,`got report for probe ${this._probeId}#${this._exporter.getReportsNumber()+1}`),this.fireOnReport(a),a}catch(s){return qn(this._moduleName,`got error ${s}`),null}}async start(){Pe(this._moduleName,"starting"),this.state=Ss.RUNNING,this._startedTime=this._exporter.start(),Pe(this._moduleName,"started")}async mute(){this.state=Ss.MUTED,Pe(this._moduleName,"muted")}async unmute(){this.state=Ss.RUNNING,Pe(this._moduleName,"unmuted")}async stop(s){if(Pe(this._moduleName,`stopping${s?" by watchdog":""}...`),this._stoppedTime=this._exporter.stop(),this.state=Ss.IDLE,this._config.ticket){const{ticket:n}=this._exporter;this.fireOnTicket(n)}this._exporter.reset(),Pe(this._moduleName,"stopped")}registerCallback(s,n,a){s in this._callbacks?(this._callbacks[s]={callback:n,context:a},Pe(this._moduleName,`registered callback '${s}'`)):qn(this._moduleName,`can't register callback for '${s}' - not found`)}unregisterCallback(s){s in this._callbacks?(this._callbacks[s]=null,delete this._callbacks[s],Pe(this._moduleName,`unregistered callback '${s}'`)):qn(this._moduleName,`can't unregister callback for '${s}' - not found`)}fireOnReport(s){this._callbacks.onreport&&wu(this._callbacks.onreport.callback,this._callbacks.onreport.context,s)}fireOnTicket(s){this._callbacks.onticket&&wu(this._callbacks.onticket.callback,this._callbacks.onticket.context,s)}updateConfig(s){this._config=s,this._exporter.updateConfig(s)}get state(){return this._state}set state(s){this._state=s,Pe(this._moduleName,`state changed to ${s}`)}addCustomEvent(s,n,a,o){this._exporter.addCustomEvent({at:typeof s=="object"?s.toJSON():s,category:n,name:a,description:o})}async registerToPCEvents(){const{pc:s}=this._config;if(navigator.mediaDevices.ondevicechange=async()=>{try{const n=await navigator.mediaDevices.enumerateDevices();this.addCustomEvent(new Date().toJSON(),"device",`${n.length} devices found`,"Media Devices state")}catch{qn(this._moduleName,"can't get devices")}},s){s.oniceconnectionstatechange=()=>{const a=s.iceConnectionState;a===ri.CONNECTED||a===ri.COMPLETED?this.addCustomEvent(new Date().toJSON(),"call",a,"ICE connection state"):a===ri.DISCONNECTED||a===ri.FAILED?this.addCustomEvent(new Date().toJSON(),"call",a,"ICE connection state"):a===ri.CLOSED&&this.addCustomEvent(new Date().toJSON(),"call","ended","ICE connection state")},s.onicegatheringstatechange=()=>{const a=s.iceGatheringState;this.addCustomEvent(new Date().toJSON(),"call",a,"ICE gathering state")},s.ontrack=a=>{this.addCustomEvent(new Date().toJSON(),"call",`${a.track.kind}track`,"MediaStreamTrack received")},s.onnegotiationneeded=()=>{this.addCustomEvent(new Date().toJSON(),"call","negotiation","Media changed")};const n=s.getReceivers();if(n&&n.length>0){const a=n[0],{transport:o}=a;if(o){const{iceTransport:t}=o;t&&(t.onselectedcandidatepairchange=()=>{this.addCustomEvent(new Date().toJSON(),"call","transport","Candidates Pair changed")})}}}}}class xv{constructor(s){this._id=s.pname&&s.pname.substr(0,12).padEnd(12," ")||av(),this._moduleName=this._id,an(this._moduleName,"probe created"),this._config=s,this._collector=new Uv(this._config,this._id)}set onreport(s){s?this._collector.registerCallback("onreport",s):this._collector.unregisterCallback("onreport")}set onticket(s){s?this._collector.registerCallback("onticket",s):this._collector.unregisterCallback("onticket")}get id(){return this._id}get pname(){return this._config.pname}get cid(){return this._config.cid}get uid(){return this._config.uid}get state(){return this._collector.state}set state(s){this._collector.state=s}addCustomEvent(s,n,a,o=new Date().toJSON()){this._collector.addCustomEvent(o,n,s,a)}get isRunning(){return this._collector.state===Ss.RUNNING}get isIdle(){return this._collector.state===Ss.IDLE}updateUserId(s){this._config.uid=s,this._collector.updateConfig(this._config)}updateCallId(s){this._config.cid=s,this._collector.updateConfig(this._config)}start(){if(!this.isIdle){Ca(this._moduleName,"probe is already running");return}this._collector.start()}stop(s=!1){this.isRunning&&this._collector.stop(s)}async takeReferenceStats(){return this._collector.takeReferenceStats()}async collectStats(){return this._collector.collectStats()}}const bt="engine ";class Pv{constructor(s){this._config=s,this._probes=[],this._startedTime=null,this._callbacks={onresult:null},an(bt,`configured for probing every ${this._config.refreshEvery}ms`),an(bt,`configured for starting after ${this._config.startAfter}ms`),an(bt,`${!this._config.stopAfter||this._config.stopAfter!==-1?`configured for stopped after ${this._config.stopAfter}ms`:"configured for never stopped"}`),Pe(bt,"engine initialized")}get probes(){return this._probes}get isRunning(){return this._probes.some(s=>s.isRunning)}get isIdle(){return this._probes.every(s=>s.isIdle)}addNewProbe(s,n){if(!s)throw new Error("undefined peer connection");const a=iv(s,n,this._config),o=new xv(a);return this._probes.push(o),Pe(bt,`${this._probes.length} probes registered`),o}removeExistingProbe(s){if(!s)throw new Error("undefined probe");s.state===Ss.RUNNING&&s.stop(),this._probes=this._probes.filter(n=>s.id!==n.id)}async start(){const s=()=>{this._probes.forEach(t=>t.start())},n=async()=>Promise.all(this._probes.map(t=>t.takeReferenceStats())),a=()=>this.isIdle?!1:!this._config.stopAfter||this._config.stopAfter<0?!0:Date.now()<this._startedTime+this._config.stopAfter,o=async()=>{const t=sv(),r=this._probes.filter(_=>_.isRunning);for(const _ of r){const h=await _.collectStats();h&&t.probes.push(h),Pe(bt,`got probe ${_.id}`),await wh(0)}return t.delta_time_to_measure_probes_ms=on(t.probes,"experimental","time_to_measure_ms"),t.delta_KBytes_in=on(t.probes,"data","delta_KBytes_in"),t.delta_KBytes_out=on(t.probes,"data","delta_KBytes_out"),t.delta_kbs_in=on(t.probes,"data","delta_kbs_in"),t.delta_kbs_out=on(t.probes,"data","delta_kbs_out"),t.total_time_decoded_in=on(t.probes,"video","total_time_decoded_in"),t.total_time_encoded_out=on(t.probes,"video","total_time_encoded_out"),t};for(Pe(bt,"starting to collect"),s(),Pe(bt,"generating reference reports..."),await n(),Pe(bt,"reference reports generated"),this._startedTime=Date.now();a();)if(Pe(bt,`wait ${this._config.refreshEvery}ms before collecting`),await wh(this._config.refreshEvery),a()){Pe(bt,"collecting...");const t=Date.now(),r=await o(),_=Date.now();r.delta_time_consumed_to_measure_ms=_-t,this.fireOnReports(r),Pe(bt,"collected")}Pe(bt,"reaching end of the collecting period..."),this.isRunning&&setTimeout(()=>{this.stop()},0)}stop(s){const n=a=>{this._probes.forEach(o=>{o.stop(a)})};an(bt,"stop collecting"),n(s)}registerCallback(s,n,a){s in this._callbacks?(this._callbacks[s]={callback:n,context:a},Pe(bt,`registered callback '${s}'`)):qn(bt,`can't register callback for '${s}' - not found`)}unregisterCallback(s){s in this._callbacks?(this._callbacks[s]=null,delete this._callbacks[s],Pe(this._moduleName,`unregistered callback '${s}'`)):qn(this._moduleName,`can't unregister callback for '${s}' - not found`)}fireOnReports(s){this._callbacks.onresult&&s.probes.length>0&&wu(this._callbacks.onresult.callback,this._callbacks.onresult.context,s)}}const kv="interface ";class Mv{constructor(s){this._config=lv(s),an(kv,`welcome to ${this._config.name} version ${this._config.version}`),YT(this._config.verbose||!1),this._engine=new Pv(this._config)}setupLogLevel(s){JT(s)}get version(){return this._config.version}get name(){return this._config.name}get probes(){return this._engine.probes}createProbe(s,n){return this._engine.addNewProbe(s,n)}startAllProbes(){this._engine.start()}stopAllProbes(){this._engine.stop()}get running(){return this._engine.isRunning}get idle(){return this._engine.isIdle}removeProbe(s){this._engine.removeExistingProbe(s)}set onresult(s){s?this._engine.registerCallback("onresult",s):this._engine.unregisterCallback("onresult")}}function Lv(c,s){return Object.keys(c).filter(n=>s.includes(n)).reduce((n,a)=>{const o=a;return{...n,[o]:c[o]}},{})}const $v=["mos_in","codec_in","delta_KBytes_in","delta_kbs_in","delta_jitter_ms_in","delta_packets_lost_in"];class Hv{constructor(s){this.intervals={},this.emitInterval=s.emitInterval||200,this.onChangeFunction=s.onChangeFunction}start(s,n){s&&s.getTracks().length&&requestAnimationFrame(()=>this.beginCalculation(s,n))}stop(s){this.clearVolumeInterval(s)}clearVolumeInterval(s){console.log("clearVolumeInterval",s),clearInterval(this.intervals[s]),delete this.intervals[s]}clearAllIntervals(){Object.keys(this.intervals).forEach(s=>{clearInterval(this.intervals[s])}),this.intervals={}}beginCalculation(s,n){this.clearVolumeInterval(n);const a=ln.createAnalyser(),o=ln.createMediaStreamSource(s),t=ln.createScriptProcessor(2048,1,1);a.smoothingTimeConstant=.8,a.fftSize=1024,o.connect(a),a.connect(t),t.connect(ln.destination),this.intervals[n]=setInterval(()=>{const r=new Uint8Array(a.frequencyBinCount);a.getByteFrequencyData(r);let _=0;const h=r.length;for(let g=0;g<h;g++)_+=r[g];const d=_/h;this.onChangeFunction(n,d)},this.emitInterval)}}const ba={SELECTED_INPUT_DEVICE:"OpensipsJSInputDevice",SELECTED_OUTPUT_DEVICE:"OpensipsJSOutputDevice"},Fv=0;class jv{constructor(s){this.isAutoAnswer=!1,this.muteWhenJoinEnabled=!1,this.isDNDEnabled=!1,this.muted=!1,this.microphoneInputLevelValue=1,this.speakerVolumeValue=1,this.activeRooms={},this.activeCalls={},this.extendedCalls={},this.availableMediaDevices=[],this.selectedMediaDevices={input:"default",output:"default"},this.callStatus={},this.callTime={},this.callMetrics={},this.timeIntervals={},this.metricConfig={refreshEvery:1e3},this.activeStreamValue=null,this.initialStreamValue=null,this.context=s,this.context.on(this.context.newRTCSessionEventName,this.newRTCSessionCallback.bind(this)),this.VUMeter=new Hv({onChangeFunction:this.emitVolumeChange.bind(this)}),this.initializeMediaDevices()}get sipOptions(){return{...this.context.options.sipOptions,mediaConstraints:this.getUserMediaConstraints}}get currentActiveRoomId(){return this.currentActiveRoomIdValue}set currentActiveRoomId(s){this.currentActiveRoomIdValue=s,this.context.emit("currentActiveRoomChanged",s)}get autoAnswer(){return this.isAutoAnswer}get callAddingInProgress(){return this.isCallAddingInProgress}set callAddingInProgress(s){this.isCallAddingInProgress=s,this.context.emit("callAddingInProgressChanged",s)}get muteWhenJoin(){return this.muteWhenJoinEnabled}get isDND(){return this.isDNDEnabled}get speakerVolume(){return this.speakerVolumeValue}get microphoneInputLevel(){return this.microphoneInputLevelValue}get getActiveCalls(){return this.activeCalls}get hasActiveCalls(){return Object.values(this.extendedCalls).length>0}get getActiveRooms(){return this.activeRooms}get isMuted(){return this.muted}get getInputDeviceList(){return this.availableMediaDevices.filter(s=>s.kind==="audioinput")}get getOutputDeviceList(){return this.availableMediaDevices.filter(s=>s.kind==="audiooutput")}get getUserMediaConstraints(){return Eh()?{video:!1,audio:!0}:{audio:{deviceId:{exact:this.selectedMediaDevices.input}},video:!1}}get selectedInputDevice(){return this.selectedMediaDevices.input}get selectedOutputDevice(){return this.selectedMediaDevices.output}get activeStream(){return this.activeStreamValue}setAvailableMediaDevices(s){this.availableMediaDevices=s,this.context.emit("changeAvailableDeviceList",s)}async updateDeviceList(){await navigator.mediaDevices.getUserMedia(this.getUserMediaConstraints);const s=await navigator.mediaDevices.enumerateDevices();this.setAvailableMediaDevices(s)}async initializeMediaDevices(){const s=localStorage.getItem(ba.SELECTED_INPUT_DEVICE)||"default",n=localStorage.getItem(ba.SELECTED_OUTPUT_DEVICE)||"default";try{const a=await navigator.mediaDevices.getUserMedia(this.getUserMediaConstraints),o=await navigator.mediaDevices.enumerateDevices();this.setAvailableMediaDevices(o),await this.setMicrophone(s),await this.setSpeaker(n),navigator.mediaDevices.addEventListener("devicechange",async()=>{const t=await navigator.mediaDevices.enumerateDevices();this.setAvailableMediaDevices(t)}),a.getTracks().forEach(t=>t.stop())}catch(a){console.error(a)}}setCallTime(s){const n={...s};delete n.callId,this.callTime={...this.callTime,[s.callId]:n},this.context.emit("changeCallTime",this.callTime)}removeCallTime(s){const n={...this.callTime};delete n[s],this.callTime={...n},this.context.emit("changeCallTime",this.callTime)}setTimeInterval(s,n){this.timeIntervals={...this.timeIntervals,[s]:n}}removeTimeInterval(s){const n={...this.timeIntervals};n[s]&&(clearInterval(n[s]),delete n[s],this.timeIntervals={...n})}stopCallTimer(s){this.removeTimeInterval(s),this.removeCallTime(s)}emitVolumeChange(s,n){this.context.emit("changeCallVolume",{callId:s,volume:n})}setMetricsConfig(s){this.metricConfig={...this.metricConfig,...s}}sendDTMF(s,n){if(!/^[A-D0-9]+$/g.test(n))throw new Error("Not allowed character in DTMF input");this.extendedCalls[s].sendDTMF(n)}setIsMuted(s){this.muted=s,this.context.emit("changeIsMuted",s)}processMute(s){const n=this.currentActiveRoomId;this.setIsMuted(s),this.initialStreamValue.getTracks().forEach(a=>a.enabled=!s),this.roomReconfigure(n)}mute(){this.processMute(!0)}unmute(){this.processMute(!1)}async processHold({callId:s,toHold:n,automatic:a}){const o=this.extendedCalls[s];o._automaticHold=a??!1,await new Promise(_=>{const h=()=>{_()};n?o.hold({},h):o.unhold({},h)}),this.updateCall(o);const r=Object.values(this.extendedCalls).filter(_=>_.roomId===this.currentActiveRoomId&&(n?s!==_._id:!0));r.length>1&&await this.doConference(r)}holdCall(s,n=!1){return this.processHold({callId:s,automatic:n,toHold:!0})}unholdCall(s){return this.processHold({callId:s,toHold:!1})}cancelAllOutgoingUnanswered(){Object.values(this.getActiveCalls).filter(s=>s.direction==="outgoing"&&s.status===Fv).forEach(s=>this.terminateCall(s._id))}answerCall(s){const n=this.extendedCalls[s];this.cancelAllOutgoingUnanswered(),n.answer(this.sipOptions),this.updateCall(n),this.setActiveRoom(n.roomId),n.connection.addEventListener("track",a=>{this.triggerAddStream(a,n)})}async moveCall(s,n){this.updateCallStatus({callId:s,isMoving:!0}),await this.processRoomChange({callId:s,roomId:n}),this.updateCallStatus({callId:s,isMoving:!1})}updateCall(s){this.activeCalls[s._id]=gh(s),this.context.emit("changeActiveCalls",this.activeCalls)}updateRoom(s){const a={...this.activeRooms[s.roomId],...s};this.activeRooms={...this.activeRooms,[s.roomId]:{...a}},this.context.emit("updateRoom",{room:a,roomList:this.activeRooms})}hasAutoAnswerHeaders(s){const n=/answer-after=0/,o=s.request.getHeader("Call-Info");return o&&n.test(o)}addCall(s,n=!0){this.activeCalls={...this.activeCalls,[s._id]:gh(s)},this.extendedCalls[s._id]=s,n&&this.context.emit("changeActiveCalls",this.activeCalls)}addCallStatus(s){this.callStatus={...this.callStatus,[s]:{isMoving:!1,isTransferring:!1,isMerging:!1}},this.context.emit("changeCallStatus",this.callStatus)}updateCallStatus(s){const a={...{...this.callStatus[s.callId]}};s.isMoving!==void 0&&(a.isMoving=s.isMoving),s.isTransferring!==void 0&&(a.isTransferring=s.isTransferring),s.isMerging!==void 0&&(a.isMerging=s.isMerging),this.callStatus={...this.callStatus,[s.callId]:{...a}},this.context.emit("changeCallStatus",this.callStatus)}removeCallStatus(s){const n={...this.callStatus};delete n[s],this.callStatus={...n},this.context.emit("changeCallStatus",this.callStatus)}addRoom(s){this.activeRooms={...this.activeRooms,[s.roomId]:s},this.context.emit("addRoom",{room:s,roomList:this.activeRooms})}getActiveStream(){const s=vh(this.initialStreamValue,this.microphoneInputLevel*2);return s.getTracks().forEach(n=>n.enabled=!this.isMuted),this.setActiveStream(s),s}async setMicrophone(s){if(!this.getInputDeviceList.find(({deviceId:a})=>a===s)||(this.setSelectedInputDevice(s),Object.keys(this.getActiveCalls).length===0))return;await this.setupStream();const n=Object.values(this.extendedCalls).filter(a=>a.roomId===this.currentActiveRoomId);n.length===1?Object.values(n).forEach(async a=>{const o=this.getActiveStream();a.connection.getSenders()[0].replaceTrack(o.getTracks()[0]),this.updateCall(a)}):await this.doConference(n)}setActiveStream(s){this.activeStream&&this.stopVUMeter("origin"),this.setupVUMeter(s,"origin"),this.activeStreamValue=s,this.context.emit("changeActiveStream",s)}async setSpeaker(s){if(!this.getOutputDeviceList.find(({deviceId:o})=>o===s))return;this.setSelectedOutputDevice(s);const n=Object.values(this.extendedCalls);if(n.length===0)return;const a=n.filter(o=>o.roomId===this.currentActiveRoomId);a.length===1?n.forEach(o=>{var t;(t=o.audioTag)==null||t.setSinkId(s),this.updateCall(o)}):await this.doConference(a)}removeRoom(s){const n={...this.activeRooms},a={...n[s]};delete n[s],this.activeRooms={...n},this.context.emit("removeRoom",{room:a,roomList:this.activeRooms})}deleteRoomIfEmpty(s){s!==void 0&&Object.values(this.extendedCalls).filter(n=>n.roomId===s).length===0&&(this.removeRoom(s),this.currentActiveRoomId===s&&(this.currentActiveRoomId=void 0))}checkInitialized(){if(!this.context.initialized)throw new Error("[OpenSIPSJS] You must call `start` method first!")}muteReconfigure(s){this.muted?s.mute({audio:!0}):s.unmute({audio:!0})}async roomReconfigure(s){var a;if(s===void 0)return;const n=Object.values(this.extendedCalls).filter(o=>o.roomId===s);if(this.currentActiveRoomId===s?n.forEach(o=>{o.audioTag&&(this.muteReconfigure(o),o.audioTag.muted=!1,this.updateCall(o))}):n.forEach(o=>{o.audioTag&&(o.audioTag.muted=!0,this.updateCall(o))}),n.length===0)this.deleteRoomIfEmpty(s);else if(n.length===1&&this.currentActiveRoomId!==s)n[0].isOnHold().local||await this.holdCall(n[0].id,!0);else if(n.length===1&&this.currentActiveRoomId===s){if(n[0].isOnHold().local&&n[0]._automaticHold&&await this.unholdCall(n[0].id),n[0].connection&&((a=n[0].connection)!=null&&a.getSenders()[0])){const o=this.getActiveStream();await n[0].connection.getSenders()[0].replaceTrack(o.getTracks()[0]),this.muteReconfigure(n[0])}}else n.length>1&&await this.doConference(n)}async doConference(s){const n=[];s.forEach(a=>{a!=null&&a.connection.getReceivers().forEach(o=>{n.push(o.track)})}),await WT.forEach(s,async a=>{var r;if(a==null)return;const o=new MediaStream,t=ln.createMediaStreamDestination();if(a.connection.getReceivers().forEach(_=>{n.forEach(h=>{o.addTrack(_.track),_.track.id!==h.id&&ln.createMediaStreamSource(new MediaStream([h])).connect(t)})}),s[0].roomId===this.currentActiveRoomId){const _=this.getActiveStream();ln.createMediaStreamSource(_).connect(t)}(r=a.connection)!=null&&r.getSenders()[0]&&(await a.connection.getSenders()[0].replaceTrack(t.stream.getTracks()[0]),this.muteReconfigure(a))})}processCallerMute(s,n){const a=this.extendedCalls[s];a&&a.connection.getReceivers().length&&(a.localMuted=n,a.connection.getReceivers().forEach(o=>{o.track.enabled=!n}),this.updateCall(a))}muteCaller(s){this.processCallerMute(s,!0)}unmuteCaller(s){this.processCallerMute(s,!1)}terminateCall(s){const n=this.extendedCalls[s];n._status!==8&&n.terminate()}transferCall(s,n){if(n.toString().length===0)return new Error("Target must be passed");const a=this.extendedCalls[s];if(!a._is_confirmed&&!a._is_canceled){const o=`sip:${n}@${this.context.sipDomain}`;a.terminate({status_code:302,reason_phrase:"Moved Temporarily",extraHeaders:[`Contact: ${o}`]});return}this.updateCallStatus({callId:s,isTransferring:!0}),a.refer(`sip:${n}@${this.context.sipDomain}`),this.updateCall(a)}mergeCall(s){const n=Object.values(this.extendedCalls).filter(t=>t.roomId===s);if(n.length!==2)return;const a=n[0],o=n[1];!a||!o||(this.updateCallStatus({callId:a._id,isMerging:!0}),this.updateCallStatus({callId:o._id,isMerging:!0}),a.refer(o.remote_identity.uri.toString(),{replaces:o}),this.updateCall(a))}mergeCallByIds(s,n){const a=Object.values(this.extendedCalls).find(t=>t._id===s),o=Object.values(this.extendedCalls).find(t=>t._id===n);if(!a||!o)throw new Error("Call ID is not provided");this.updateCallStatus({callId:s,isMerging:!0}),this.updateCallStatus({callId:n,isMerging:!0}),a.refer(o.remote_identity.uri.toString(),{replaces:o}),this.updateCall(a)}setDND(s){this.isDNDEnabled=s,this.context.emit("changeIsDND",s)}startCallTimer(s){this.removeTimeInterval(s);const n={callId:s,hours:0,minutes:0,seconds:0,formatted:""};this.setCallTime(n);const a=setInterval(()=>{const o={...this.callTime[s]},t=GT(o);this.setCallTime({callId:s,...t})},1e3);this.setTimeInterval(s,a)}async setActiveRoom(s){const n=this.currentActiveRoomId;s!==n&&(this.currentActiveRoomId=s,await this.roomReconfigure(n),await this.roomReconfigure(s))}getNewRoomId(){const s=Object.keys(this.activeRooms);return s.length===0?1:parseInt(s.sort()[s.length-1])+1}async setupCall(s){var d,g;const n=s.session;if(this.getActiveCalls[n.id]!==void 0)return;const o=this.getNewRoomId(),t={started:new Date,incomingInProgress:!1,roomId:o};n.direction==="incoming"?(this.context.logger.log("New incoming call from",(g=(d=n._remote_identity)==null?void 0:d._uri)==null?void 0:g._user),t.incomingInProgress=!0,this.context.subscribe(zt.CALL_CONFIRMED,T=>{n.id===T.id&&(this.updateRoom({incomingInProgress:!1,roomId:o}),this.startCallTimer(n.id))}),this.context.subscribe(zt.CALL_FAILED,T=>{n.id===T.id&&(this.updateRoom({incomingInProgress:!1,roomId:o}),this.deleteRoomIfEmpty(o))})):n.direction==="outgoing"&&(n.once("confirmed",()=>{this.startCallTimer(n.id)}),this.startCallTimer(n.id));const r=n,_=this.hasAutoAnswerHeaders(s),h=r.direction==="incoming"&&!this.hasActiveCalls&&(_||this.autoAnswer);r.roomId=o,r.localMuted=!1,r.autoAnswer=h,h?this.addCall(r,!1):this.addCall(r),this.addCallStatus(n.id),this.addRoom(t),h&&this.answerCall(r._id)}removeCall(s){const n={...this.activeCalls};delete n[s],this.activeCalls={...n};const a={...this.extendedCalls};delete a[s],this.extendedCalls={...a},this.context.emit("changeActiveCalls",this.activeCalls)}activeCallListRemove(s){var o;const n=this.extendedCalls[s._id];this.stopVUMeter("origin"),(o=n.connection)==null||o.getSenders().forEach(t=>{t.track.stop()});const a=n.roomId;this.removeCall(s._id),this.roomReconfigure(a)}async newRTCSessionCallback(s){const n=s.session;if(this.isDND){n.terminate({status_code:486,reason_phrase:"Do Not Disturb"});return}if(this.context.triggerListener({listenerType:zt.NEW_CALL,session:n,event:s}),n.on("ended",a=>{var t,r,_;this.stopVUMeter(n.id),this.context.logger.log("Session ended for",(r=(t=n._remote_identity)==null?void 0:t._uri)==null?void 0:r._user),this.context.triggerListener({listenerType:zt.CALL_ENDED,session:n,event:a});const o=this.getActiveCalls[n.id];o&&this.activeCallListRemove(o),this.stopCallTimer(n.id),this.removeCallStatus(n.id),this.removeCallMetrics(n.id),Object.keys(this.extendedCalls).length||(this.setIsMuted(!1),(_=this.initialStreamValue)==null||_.getTracks().forEach(h=>h.stop()),this.initialStreamValue=null)}),n.on("progress",a=>{var o,t;this.context.logger.log("Session in progress for",(t=(o=n._remote_identity)==null?void 0:o._uri)==null?void 0:t._user),this.context.triggerListener({listenerType:zt.CALL_PROGRESS,session:n,event:a})}),n.on("failed",a=>{var t,r,_;this.stopVUMeter(n.id),this.context.logger.log("Session failed for",(r=(t=n._remote_identity)==null?void 0:t._uri)==null?void 0:r._user),this.context.triggerListener({listenerType:zt.CALL_FAILED,session:n,event:a}),n.id===this.callAddingInProgress&&(this.callAddingInProgress=void 0);const o=this.getActiveCalls[n.id];o&&this.activeCallListRemove(o),this.stopCallTimer(n.id),this.removeCallStatus(n.id),this.removeCallMetrics(n.id),Object.keys(this.extendedCalls).length||(this.setIsMuted(!1),(_=this.initialStreamValue)==null||_.getTracks().forEach(h=>h.stop()),this.initialStreamValue=null)}),n.on("confirmed",a=>{var o,t;this.context.logger.log("Session confirmed for",(t=(o=n._remote_identity)==null?void 0:o._uri)==null?void 0:t._user),this.context.triggerListener({listenerType:zt.CALL_CONFIRMED,session:n,event:a}),this.updateCall(n),n.id===this.callAddingInProgress&&(this.callAddingInProgress=void 0)}),await this.setupCall(s),n.direction==="outgoing"){const a=this.getActiveCalls[n.id].roomId;this.setActiveRoom(a)}}setMuteWhenJoin(s){this.muteWhenJoinEnabled=s,this.context.emit("changeMuteWhenJoin",s)}setMicrophoneSensitivity(s){if(s<0||s>1)throw new Error("Value should be in range from 0 to 1!");this.microphoneInputLevelValue=s,this.roomReconfigure(this.currentActiveRoomId)}setSpeakerVolume(s){this.speakerVolumeValue=s,Object.values(this.extendedCalls).forEach(n=>{n.audioTag&&(n.audioTag.volume=s,this.updateCall(n))})}setAutoAnswer(s){this.isAutoAnswer=s}setSelectedInputDevice(s){localStorage.setItem(ba.SELECTED_INPUT_DEVICE,s),this.selectedMediaDevices.input=s,this.context.emit("changeActiveInputMediaDevice",s)}setSelectedOutputDevice(s){localStorage.setItem(ba.SELECTED_OUTPUT_DEVICE,s),this.selectedMediaDevices.output=s,this.context.emit("changeActiveOutputMediaDevice",s)}setCallMetrics(s){const n={...s};delete n.callId,this.callMetrics={...this.callMetrics,[s.callId]:n},this.context.emit("changeCallMetrics",this.callMetrics)}removeCallMetrics(s){const n={...this.callMetrics};delete n[s],this.callMetrics={...n},this.context.emit("changeCallMetrics",this.callMetrics)}getCallQuality(s){const n=new Mv(this.metricConfig),a=n.createProbe(s.connection,{cid:s._id}),o=[];let t;a.onreport=r=>{Object.entries(r.audio).forEach(([d,g])=>{g.direction==="inbound"&&!o.includes(d)&&(o.push(d),t=d)});const _=r.audio[t];if(!_)return;const h=Lv(_,$v);h.callId=s._id,this.setCallMetrics(h)},this.context.subscribe(zt.CALL_ENDED,r=>{r._id===s._id&&n.stopAllProbes()}),n.startAllProbes()}setupVUMeter(s,n){this.VUMeter.start(s,n)}stopVUMeter(s){this.VUMeter.stop(s)}async setupStream(){const s=await navigator.mediaDevices.getUserMedia(this.getUserMediaConstraints);this.initialStreamValue&&(this.initialStreamValue.getTracks().forEach(n=>n.stop()),this.initialStreamValue=null),this.initialStreamValue=s}async triggerAddStream(s,n){this.setIsMuted(this.muteWhenJoin||this.isMuted),this.initialStreamValue||await this.setupStream();const a=vh(this.initialStreamValue,this.microphoneInputLevel*2),o=this.isMuted||this.muteWhenJoin;a.getTracks().forEach(r=>r.enabled=!o),this.setActiveStream(a),await n.connection.getSenders()[0].replaceTrack(a.getTracks()[0]);const t=new MediaStream([s.track]);DT(t,n,this.selectedOutputDevice,this.speakerVolume),this.setupVUMeter(t,n._id),this.getCallQuality(n),this.updateCall(n)}initCall(s,n){if(s.length===0)return console.error("Target must be a valid string");this.context.logger.log(`Calling sip:${s}@${this.context.sipDomain}...`);const a=this.context.call(`sip:${s}@${this.context.sipDomain}`,this.sipOptions);this.callAddingInProgress=a.id,n&&this.currentActiveRoomId!==void 0&&this.processRoomChange({callId:a.id,roomId:this.currentActiveRoomId}),a.connection.addEventListener("track",o=>{this.triggerAddStream(o,a)})}async processRoomChange({callId:s,roomId:n}){const a=this.extendedCalls[s].roomId;this.extendedCalls[s].roomId=n;const o=this.extendedCalls[s];return this.updateCall(o),await this.setActiveRoom(n),Promise.all([this.roomReconfigure(a),this.roomReconfigure(n)]).then(()=>{this.deleteRoomIfEmpty(a),this.deleteRoomIfEmpty(n)})}}class Vv{constructor(s){this.context=s}get sipOptions(){return{...this.context.options.sipOptions}}initCall(s,n){if(s.length===0)return console.error("Target must be a valid string");this.context.logger.log(`Calling sip:${s}@${this.context.sipDomain}...`),this.context.joinVideoCall(`sip:${s}@${this.context.sipDomain}`,n,this.sipOptions)}stop(s={}){this.context.terminateJanusSessions(s)}startAudio(){this.context.enableJanusAudio(!0)}stopAudio(){this.context.enableJanusAudio(!1)}startVideo(){this.context.enableJanusVideo(!0)}stopVideo(){this.context.enableJanusVideo(!1)}}class qv{constructor(s){this.activeMessages={},this.extendedMessages={},this.msrpHistory={},this.context=s,this.context.on(this.context.newMSRPSessionEventName,this.newMSRPSessionCallback.bind(this.context))}get isMSRPInitializing(){return this.isMSRPInitializingValue}get getActiveMessages(){return this.activeMessages}msrpAnswer(s){const n=this.extendedMessages[s];this.updateMSRPSession(n)}updateMSRPSession(s){this.activeMessages[s._id]=Th(s),this.context.emit("changeActiveMessages",this.activeMessages)}addMMSRPSession(s){this.activeMessages={...this.activeMessages,[s._id]:Th(s)},this.extendedMessages[s._id]=s,this.context.emit("changeActiveMessages",this.activeMessages)}addMSRPMessage(s,n){const a=this.msrpHistory[n.id]||[];a.push(s),this.msrpHistory={...this.msrpHistory,[n.id]:[...a]},this.context.emit("newMSRPMessage",{message:s,session:n})}messageTerminate(s){const n=this.extendedMessages[s];n._status!==8&&n.terminate()}addMessageSession(s){if(!s._id||this.getActiveMessages[s._id]!==void 0)return;const a=s;this.addMMSRPSession(a)}triggerMSRPListener({listenerType:s,session:n,event:a}){const o=this.context.listenersList[s];!o||!o.length||o.forEach(t=>{t(n,a)})}removeMMSRPSession(s){const n={...this.activeMessages};delete n[s],this.activeMessages={...n};const a={...this.extendedMessages};delete a[s],this.extendedMessages={...a},this.context.emit("changeActiveMessages",this.activeMessages)}activeMessageListRemove(s){this.removeMMSRPSession(s._id)}newMSRPSessionCallback(s){const n=s.session;n.on("ended",a=>{this.triggerMSRPListener({listenerType:zt.CALL_ENDED,session:n,event:a});const o=this.getActiveMessages[n.id];this.activeMessageListRemove(o)}),n.on("failed",a=>{this.triggerMSRPListener({listenerType:zt.CALL_FAILED,session:n,event:a});const o=this.getActiveMessages[n.id];this.activeMessageListRemove(o)}),n.on("confirmed",a=>{this.triggerMSRPListener({listenerType:zt.CALL_CONFIRMED,session:n,event:a}),this.updateMSRPSession(n)}),n.on("newMessage",a=>{this.addMSRPMessage(a,n)}),this.addMessageSession(n)}setIsMSRPInitializing(s){this.isMSRPInitializingValue=s,this.context.emit("isMSRPInitializingChanged",s)}initMSRP(s,n,a){if(s.length===0)return console.error("Target must be a valid string");const o=this.context.startMSRP(s,a);o.on("active",()=>{this.addMessageSession(o),o.sendMSRP(n),this.setIsMSRPInitializing(!1)}),this.setIsMSRPInitializing(!0)}sendMSRP(s,n){const a=this.extendedMessages[s];if(!a)throw new Error(`MSRP session with id ${s} doesn't exist!`);a.sendMSRP(n)}}const Ou={AUDIO:"audio",VIDEO:"video",MSRP:"msrp"};class Bv extends AT{constructor(s,n){if(!s.modules.length)throw new Error("options.modules should include at least 1 module");const a={...s.configuration,sockets:s.socketInterfaces.map(o=>new BT.WebSocketInterface(o))};super(a),this.initialized=!1,this.connected=!1,this.logger=console,this.newRTCSessionEventName="newRTCSession",this.registeredEventName="registered",this.unregisteredEventName="unregistered",this.disconnectedEventName="disconnected",this.connectedEventName="connected",this.newMSRPSessionEventName="newMSRPSession",this.isReconnecting=!1,this.activeConnection=!1,this.audio=null,this.msrp=null,this.video=null,this.listenersList={},this.modules=[],s.pnExtraHeaders&&Object.keys(s.pnExtraHeaders).length&&this.registrator().setExtraContactUriParams(s.pnExtraHeaders),this.options=s,this.modules=s.modules,n&&NT(n)&&(this.logger=n)}on(s,n){return super.on(s,n)}off(s,n){return super.off(s,n)}emit(s,n){return super.emit(s,n)}get sipDomain(){return this.options.sipDomain}begin(){if(this.isConnected()){console.error("Connection is already established");return}return this.modules.includes(Ou.AUDIO)&&(this.audio=new jv(this)),this.modules.includes(Ou.MSRP)&&(this.msrp=new qv(this)),this.modules.includes(Ou.VIDEO)&&(this.video=new Vv(this)),this.on(this.registeredEventName,()=>{this.logger.log("Successfully registered to",this.options.socketInterfaces[0]),this.setInitialized(!0)}),this.on(this.unregisteredEventName,()=>{this.logger.log("Unregistered from",this.options.socketInterfaces[0]),this.setInitialized(!1)}),this.on(this.connectedEventName,()=>{this.logger.log("Connected to",this.options.socketInterfaces[0]),this.setConnected(!0),this.isReconnecting=!1,this.activeConnection=!0}),this.on(this.disconnectedEventName,()=>{this.isReconnecting||(this.isReconnecting=!0,this.logger.log("Disconnected from",this.options.socketInterfaces[0]),this.logger.log("Reconnecting to",this.options.socketInterfaces[0]),this.stop(),this.setInitialized(!1),this.setConnected(!1),this.activeConnection&&setTimeout(this.start.bind(this),5e3))}),this.logger.log("Connecting to",this.options.socketInterfaces[0]),this.start(),this}disconnect(){this.activeConnection=!1,this.stop()}subscribe(s,n){const o=!this.listenersList[s]||!this.listenersList[s].length?[n]:[...this.listenersList[s],n];this.listenersList={...this.listenersList,[s]:o}}removeIListener(s){const n={...this.listenersList};delete n[s],this.listenersList={...n}}triggerListener({listenerType:s,session:n,event:a}){const o=this.listenersList[s];!o||!o.length||o.forEach(t=>{t(n,a)})}setInitialized(s){this.initialized=s,this.emit("ready",s)}setConnected(s){this.connected=s,this.emit("connection",s)}}return Bv});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicenter-team/opensips-js",
3
- "version": "1.0.96",
3
+ "version": "1.0.97",
4
4
  "description": "The JS package for opensips",
5
5
  "default": "src/index.ts",
6
6
  "jsdelivr": "dist/opensips-js.umd.js",