UNPKG

@observertc/client-monitor-js

Version:

ObserveRTC Client Integration Javascript Library

1 lines 1.65 kB
import{createLogger as t}from"../utils/logger";import{convertRTCStatsReport as s}from"./utils";import*as r from"../schema/W3cStatsIdentifiers";const e=t("MediasoupTransportStatsCollector");export class MediasoupTransportStatsCollector{transport;clientMonitor;lastStats=[];_stats=[];producers=new Map;get browser(){return this.clientMonitor.browser}constructor(t,s){this.transport=t,this.clientMonitor=s,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=s(await this.transport.getStats()),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){e.error("Error extending stats",t)}return this._stats}catch(t){return e.error("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 s=await t.getStats(),e=[];let o,a;s.forEach((t=>{t.type===r.StatsType.outboundRtp?e.push(t):t.type===r.StatsType.mediaSource?o=t:t.type===r.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)}})))}}