UNPKG

@observertc/client-monitor-js

Version:

ObserveRTC Client Integration Javascript Library

1 lines 2.76 kB
import{Detectors as t}from"../detectors/Detectors";import{DryOutboundTrackDetector as e}from"../detectors/DryOutboundTrackDetector";import{CaptureFailureDetector as o}from"../detectors/CaptureFailureDetector";import{CodecChangeDetector as r}from"../detectors/CodecChangeDetector";import{SourceEncoderBottleneckDetector as i}from"../detectors/SourceEncoderBottleneckDetector";import{SimulcastLayerDetector as s}from"../detectors/SimulcastLayerDetector";import{VideoResolutionChangeDetector as c}from"../detectors/VideoResolutionChangeDetector";export class OutboundTrackMonitor{track;_mediaSource;direction="outbound";detectors;mappedOutboundRtps=new Map;calculatedScore={weight:0,value:void 0};get score(){return this.calculatedScore.value}get scoreReasons(){return this.calculatedScore.reasons}attachments;appData;constructor(a,n,d){this.track=a,this._mediaSource=n,this.attachments=d,this.detectors=new t;const u=this.getPeerConnection().parent.config;null!==u.dryOutboundTrackDetector&&this.detectors.add(new e(this)),null!==u.captureFailureDetector&&this.detectors.add(new o(this)),null!==u.codecChangeDetector&&this.detectors.add(new r(this)),"audio"===this.kind?this.calculatedScore.weight=1:"video"===this.kind&&(null!==u.sourceEncoderBottleneckDetector&&this.detectors.add(new i(this)),null!==u.simulcastLayerDetector&&this.detectors.add(new s(this)),null!==u.videoResolutionChangeDetector&&this.detectors.add(new c(this)),this.calculatedScore.weight=2)}getPeerConnection(){return this._mediaSource.getPeerConnection()}getMediaSource(){return this._mediaSource}get kind(){return this.track.kind}bitrate;jitter;fractionLost;sendingPacketRate;remoteReceivedPacketRate;update(){this.bitrate=0,this.jitter=0,this.fractionLost=0,this.sendingPacketRate=0,this.remoteReceivedPacketRate=0;for(const t of this.mappedOutboundRtps.values())this.bitrate+=t.bitrate??0,this.jitter+=t.getRemoteInboundRtp()?.jitter??0,this.fractionLost+=t.getRemoteInboundRtp()?.deltaFractionLost??0,this.sendingPacketRate+=t.packetRate??0,this.remoteReceivedPacketRate+=t.getRemoteInboundRtp()?.packetRate??0;this.detectors.update()}getOutboundRtps(){return Array.from(this.mappedOutboundRtps.values())}getHighestLayer(){let t,e,o=0,r=0;for(const i of this.mappedOutboundRtps.values())o+=1,t??=i,i.bitrate&&i.bitrate>r&&(e=i,r=i.bitrate);if(0!==o)return 1===o?t:e}createSample(){let t;return"audio"===this.kind?t=this.getPeerConnection()?.parent.scoreCalculator?.encodeOutboundAudioScoreReasons?.(this.calculatedScore.reasons):"video"===this.kind&&(t=this.getPeerConnection()?.parent.scoreCalculator?.encodeOutboundVideoScoreReasons?.(this.calculatedScore.reasons)),{id:this.track.id,kind:this.kind,timestamp:Date.now(),attachments:this.attachments,score:this.score,scoreReasons:t}}}