@observertc/client-monitor-js
Version:
ObserveRTC Client Integration Javascript Library
1 lines • 1.68 kB
JavaScript
import{convertRTCStatsReport as t}from"./utils";import*as s from"../schema/W3cStatsIdentifiers";const r="MediasoupTransportStatsCollector";export class MediasoupTransportStatsCollector{transport;clientMonitor;logger;lastStats=[];_stats=[];producers=new Map;get browser(){return this.clientMonitor.browser}constructor(t,s,r=s.logger){this.transport=t,this.clientMonitor=s,this.logger=r,this._newProducers=this._newProducers.bind(this),this.transport.observer.once("close",(()=>{this.transport.observer.off("newproducer",this._newProducers)})),this.transport.observer.on("newproducer",this._newProducers)}async getStats(){try{this.lastStats=t(await this.transport.getStats(),this.logger),this._stats=[];for(const t of this.lastStats){if("inbound-rtp"===t.type){if("probator"===t.trackIdentifier)continue}this._stats.push(t)}try{await this._extend()}catch(t){this.logger.error(`[${r}]:`,"Error extending stats",t)}return this._stats}catch(t){return this.logger.error(`[${r}]:`,"Error getting stats report",t),[]}}_newProducers(t){t.observer.once("close",(()=>{this.producers.delete(t.id)})),this.producers.set(t.id,t)}async _extend(){if(this.lastStats&&this.browser)switch(this.browser.name){case"chrome":case"edge":case"opera":case"safari":case"unknown":break;case"firefox":await this._firefoxTrackStats()}}async _firefoxTrackStats(){await Promise.allSettled([...this.producers.values()].map((async t=>{const r=await t.getStats(),e=[];let o,a;r.forEach((t=>{t.type===s.StatsType.outboundRtp?e.push(t):t.type===s.StatsType.mediaSource?o=t:t.type===s.StatsType.codec&&(a=t)}));for(const t of e){const s=this._stats.find((s=>s.id===t.id));s&&(s.codecId=a?.id,s.mediaSourceId=o?.id)}})))}}