@observertc/client-monitor-js
Version:
ObserveRTC Client Integration Javascript Library
1 lines • 2.16 kB
JavaScript
export class CpuPerformanceDetector{clientMonitor;static ISSUE_TYPE="cpulimitation";name="cpu-performance-detector";disabled=!1;issueKey=CpuPerformanceDetector.ISSUE_TYPE;_startedAlertAt;constructor(t){this.clientMonitor=t}get config(){return this.clientMonitor.config.cpuPerformanceDetector}update(){if(this.disabled)return;const t=this.clientMonitor.cpuPerformanceAlertOn;let e=!1;const{alertOn:i,alertOff:o,minReceivedFrames:n}=this.config.incomingDecodedFramesRatioThresholds??{};if(this.config.durationOfCollectingStatsThreshold){const{lowWatermark:i,highWatermark:o}=this.config.durationOfCollectingStatsThreshold;t?e=i<this.clientMonitor.durationOfCollectingStatsInMs:o<this.clientMonitor.durationOfCollectingStatsInMs&&(e=!0)}for(const t of this.clientMonitor.outboundRtps){if(e)break;e="cpu"===t.qualityLimitationReason||this._checkEncoderPressure(t)}if(!e&&void 0!==i&&void 0!==o){const r=n??0;for(const n of this.clientMonitor.inboundRtps){if(e)break;if("video"!==n.kind)continue;const s=n.deltaFramesReceived??0,c=n.deltaFramesDecoded??0;if(s<r)continue;const a=Math.min(c/s,1);t?e=a<o:a<=i&&(e=!0)}}if(e){if(t)return;this.clientMonitor.cpuPerformanceAlertOn=!0,this.clientMonitor.emit("cpulimitation",{clientMonitor:this.clientMonitor}),this._raise()}else{if(!t)return;this.clientMonitor.cpuPerformanceAlertOn=!1,this._resolve("cpu limitation ended")}}_checkEncoderPressure(t){if("video"!==t.kind)return!1;const e=this.config.encoderCpuLimitationShareThreshold,i=this.config.encodeTimeBudgetRatio;if(void 0!==e){const i=t.qualityLimitationDurationShares?.cpu;if(void 0!==i&&e<i)return!0}if(void 0!==i){const e=t.framesPerSecond,o=t.encodeTimePerFrameInMs;if(!e||e<1||void 0===o)return!1;if(1e3/e*i<o)return!0}return!1}_raise(){this._startedAlertAt=Date.now(),this.clientMonitor.raiseIssue(this.issueKey,{type:CpuPerformanceDetector.ISSUE_TYPE,payload:{}})}_resolve(t){const e=this.clientMonitor.activeIssues.get(this.issueKey);let i;e&&(i={...e.payload,durationInMs:this._startedAlertAt?Date.now()-this._startedAlertAt:void 0}),this.clientMonitor.resolveIssue(this.issueKey,{comment:t,payload:i,resolvedAt:Date.now()}),this._startedAlertAt=void 0}}