@observertc/client-monitor-js
Version:
ObserveRTC Client Integration Javascript Library
1 lines • 5.36 kB
JavaScript
import t from"eventemitter3";import{ClientEventTypes as e}from"../schema/ClientEventTypes";export class SelectedIcePath extends t{key;_pair;_peerConnection;createdAt=Date.now();updatedAt=Date.now();closed=!1;durations={direct:0,"turn-udp":0,"turn-tcp":0,"turn-tls":0,"turn-unknown":0};pathSwitches=0;directToRelaySwitches=0;relayToDirectSwitches=0;relayProtocolSwitches=0;turnServerSwitches=0;firstRelaySelectedAt;lastSwitchedAt;totalBytesSent=0;totalBytesReceived=0;totalPacketsSent=0;totalPacketsReceived=0;relayBytesSent=0;relayBytesReceived=0;relayPacketsSent=0;relayPacketsReceived=0;_kind;_turnServer;_tuple;_kindSince=Date.now();_switchTimestamps=[];appData;constructor(t,e,r){super(),this.key=t,this._pair=e,this._peerConnection=r,this._kind=e.pathKind,this._turnServer=e.turnServer,this._tuple=e.tuple,this.usingTurn&&(this.firstRelaySelectedAt=this.createdAt)}getPeerConnection(){return this._peerConnection}get pair(){return this._pair}get localCandidate(){return this._pair.getLocalCandidate()}get remoteCandidate(){return this._pair.getRemoteCandidate()}get iceTransport(){return this._pair.getIceTransport()}get pairId(){return this._pair.id}get transportId(){return this._pair.transportId}get kind(){return this._pair.pathKind}get usingTurn(){return this._pair.usingTurn}get usingTcp(){return this._pair.usingTcp}get relayProtocol(){return this._pair.relayProtocol}get turnUrl(){return this._pair.turnUrl}get turnServer(){return this._pair.turnServer}get tuple(){return this._pair.tuple}get protocol(){return this.localCandidate?.protocol}get localCandidateType(){return this.localCandidate?.candidateType}get remoteCandidateType(){return this.remoteCandidate?.candidateType}get localAddress(){return this.localCandidate?.address}get localPort(){return this.localCandidate?.port}get localAddressFamily(){return this.localCandidate?.addressFamily}get remoteAddress(){return this.remoteCandidate?.address}get remotePort(){return this.remoteCandidate?.port}get remoteAddressFamily(){return this.remoteCandidate?.addressFamily}get currentRoundTripTime(){return this._pair.currentRoundTripTime}get state(){return this._pair.state}get durationInMs(){return this.updatedAt-this.createdAt}get relayDurationInMs(){return this.durations["turn-udp"]+this.durations["turn-tcp"]+this.durations["turn-tls"]+this.durations["turn-unknown"]}get timeToFirstRelayInMs(){return void 0===this.firstRelaySelectedAt?void 0:this.firstRelaySelectedAt-this.createdAt}get relayBytesRatio(){const t=this.totalBytesSent+this.totalBytesReceived;if(!(t<=0))return(this.relayBytesSent+this.relayBytesReceived)/t}getSwitchCountSince(t){let e=0;for(let r=this._switchTimestamps.length-1;0<=r;--r){const i=this._switchTimestamps[r];if(void 0===i||i<t)break;++e}return e}update(t){if(this.closed)return;const e=Date.now(),r=this._kind,i=this._turnServer,s=this._tuple,n=this._describe();this.durations[r]+=Math.max(0,e-this._kindSince),this._kindSince=e,this._pair=t,this.updatedAt=e,this._accumulateTraffic(t);const a=t.pathKind,d=t.turnServer,o=t.tuple;if(this.usingTurn&&void 0===this.firstRelaySelectedAt&&(this.firstRelaySelectedAt=e),a===r&&d===i&&o===s)return;const c=function(t,e){const r="direct"!==t.kind,i="direct"!==e.kind;if(!r&&i)return"direct-to-relay";if(r&&!i)return"relay-to-direct";if(r&&i){if(t.turnServer!==e.turnServer)return"turn-server-changed";if(t.kind!==e.kind)return"relay-protocol-changed"}return"path-changed"}({kind:r,turnServer:i},{kind:a,turnServer:d});switch(this._kind=a,this._turnServer=d,this._tuple=o,++this.pathSwitches,this.lastSwitchedAt=e,this._switchTimestamps.push(e),64<this._switchTimestamps.length&&this._switchTimestamps.shift(),c){case"direct-to-relay":++this.directToRelaySwitches;break;case"relay-to-direct":++this.relayToDirectSwitches;break;case"relay-protocol-changed":++this.relayProtocolSwitches;break;case"turn-server-changed":++this.turnServerSwitches}this._notify(c,n)}notifyInitialSelection(){this._notify("initial-selection",void 0)}close(){this.closed||(this.closed=!0,this.durations[this._kind]+=Math.max(0,Date.now()-this._kindSince),this.emit("close"),this.removeAllListeners())}_accumulateTraffic(t){const e=t.deltaBytesSent??0,r=t.deltaBytesReceived??0,i=t.deltaPacketsSent??0,s=t.deltaPacketsReceived??0;this.totalBytesSent+=e,this.totalBytesReceived+=r,this.totalPacketsSent+=i,this.totalPacketsReceived+=s,t.usingTurn&&(this.relayBytesSent+=e,this.relayBytesReceived+=r,this.relayPacketsSent+=i,this.relayPacketsReceived+=s)}_notify(t,r){const i=this._describe(),s=this._peerConnection.parent;this.emit("changed",{transition:t,from:r,to:i}),s.emit("ice-path-changed",{clientMonitor:s,peerConnectionMonitor:this._peerConnection,selectedIcePath:this,transition:t,from:r,to:i}),s.addEvent({type:e.PEER_CONNECTION_ICE_PATH_CHANGED,payload:{peerConnectionId:this._peerConnection.peerConnectionId,transition:t,from:r,to:i}})}_describe(){const t=this.localCandidate,e=this.remoteCandidate;return{kind:this.kind,pairId:this.pairId,transportId:this.transportId,localCandidateId:this._pair.localCandidateId,remoteCandidateId:this._pair.remoteCandidateId,localCandidateType:t?.candidateType,remoteCandidateType:e?.candidateType,protocol:t?.protocol,relayProtocol:this.relayProtocol,turnUrl:this.turnUrl,turnServer:this.turnServer,localAddress:t?.address,localPort:t?.port,remoteAddress:e?.address,remotePort:e?.port}}}