-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Develop refactor stats adapter (#43)
* save * save
- Loading branch information
1 parent
2fd72f0
commit 1d1b41c
Showing
16 changed files
with
635 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { RtcStats } from "../schema/W3cStatsIdentifiers"; | ||
|
||
export class Firefox94StatsAdapter { | ||
public readonly name = 'firefox94StatsAdapter'; | ||
|
||
public adapt(rtcStats: RtcStats[]): RtcStats[] { | ||
for (const rtcStatValue of rtcStats) { | ||
if (!rtcStatValue) continue; | ||
const rawType = rtcStatValue.type; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const rawValue = rtcStatValue as any; | ||
if (!rawType || typeof rawType !== "string") continue; | ||
if ( | ||
rawType === "inbound-rtp" || | ||
rawType === "outbound-rtp" || | ||
rawType === "remote-inbound-rtp" || | ||
rawType === "remote-outbound-rtp" | ||
) { | ||
if (rawValue.mediaType && !rawValue.kind) { | ||
rawValue.kind = rawValue.mediaType; | ||
delete rawValue.mediaType; | ||
} | ||
} | ||
// firefox put the track identifier inside brackets ({}) | ||
if (rawValue.trackIdentifier) { | ||
rawValue.trackIdentifier = rawValue.trackIdentifier.replace("{", "").replace("}", ""); | ||
} | ||
} | ||
|
||
return rtcStats; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { IceCandidatePairStats, IceTransportStats } from "../schema/ClientSample"; | ||
import { RtcStats } from "../schema/W3cStatsIdentifiers"; | ||
import { StatsAdapter } from "./StatsAdapter"; | ||
import * as W3C from "../schema/W3cStatsIdentifiers"; | ||
|
||
type SelectedIceCandidatePairStats = (RtcStats & IceCandidatePairStats & { selected?: boolean }); | ||
|
||
export class FirefoxTransportStatsAdapter implements StatsAdapter { | ||
public readonly name = 'FirefoxTransportStatsAdapter'; | ||
|
||
private _selectedCandidatePair?: SelectedIceCandidatePairStats; | ||
|
||
public stats: IceTransportStats & RtcStats = { | ||
type: 'transport', | ||
timestamp: 0, | ||
id: '', | ||
packetsSent: 0, | ||
packetsReceived: 0, | ||
bytesSent: 0, | ||
bytesReceived: 0, | ||
// iceRole: 'unknown'; | ||
// iceLocalUsernameFragment?: string; | ||
// dtlsState?: string; | ||
// iceState?: string; | ||
selectedCandidatePairId: undefined, | ||
// localCertificateId?: string; | ||
// remoteCertificateId?: string; | ||
// tlsVersion?: string; | ||
// dtlsCipher?: string; | ||
// dtlsRole?: string; | ||
// srtpCipher?: string; | ||
selectedCandidatePairChanges: -1, | ||
}; | ||
|
||
|
||
public constructor() { | ||
// empty | ||
} | ||
|
||
adapt(stats: RtcStats[]): RtcStats[] { | ||
let selectedCandidatePair: SelectedIceCandidatePairStats | undefined; | ||
|
||
for (const stat of stats) { | ||
if (stat.type !== W3C.StatsType.candidatePair) continue; | ||
|
||
const pair = stat as SelectedIceCandidatePairStats; | ||
|
||
if (!pair.selected) continue; | ||
|
||
selectedCandidatePair = pair; | ||
break; | ||
} | ||
|
||
if (!selectedCandidatePair) return stats; | ||
|
||
if (this._selectedCandidatePair?.id !== selectedCandidatePair.id) { | ||
this.stats.selectedCandidatePairChanges = (this.stats.selectedCandidatePairChanges ?? 0) + 1; | ||
this.stats.selectedCandidatePairId = selectedCandidatePair.id; | ||
this.stats.id = selectedCandidatePair.transportId ?? 'transport_0'; | ||
} else { | ||
const deltaPacketsReceived = (selectedCandidatePair.packetsReceived ?? 0) - (this._selectedCandidatePair.packetsReceived ?? 0); | ||
const deltaPacketsSent = (selectedCandidatePair.packetsSent ?? 0) - (this._selectedCandidatePair.packetsSent ?? 0); | ||
const deltaBytesReceived = (selectedCandidatePair.bytesReceived ?? 0) - (this._selectedCandidatePair.bytesReceived ?? 0); | ||
const deltaBytesSent = (selectedCandidatePair.bytesSent ?? 0) - (this._selectedCandidatePair.bytesSent ?? 0); | ||
|
||
if (0 < deltaPacketsReceived) this.stats.bytesReceived = (this.stats.bytesReceived ?? 0) + deltaBytesReceived; | ||
if (0 < deltaPacketsSent) this.stats.bytesSent = (this.stats.bytesSent ?? 0) + deltaBytesSent; | ||
if (0 < deltaPacketsReceived) this.stats.packetsReceived = (this.stats.packetsReceived ?? 0) + deltaPacketsReceived; | ||
if (0 < deltaPacketsSent) this.stats.packetsSent = (this.stats.packetsSent ?? 0) + deltaPacketsSent; | ||
} | ||
|
||
this._selectedCandidatePair = selectedCandidatePair; | ||
this.stats.timestamp = selectedCandidatePair.timestamp; | ||
|
||
stats.push(this.stats); | ||
|
||
return stats; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as W3CStats from '../schema/W3cStatsIdentifiers'; | ||
|
||
|
||
export interface StatsAdapter { | ||
name: string; | ||
adapt(stats: W3CStats.RtcStats[]): W3CStats.RtcStats[]; | ||
postAdapt?(stats: W3CStats.RtcStats[]): W3CStats.RtcStats[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,53 @@ | ||
import * as W3CStats from '../schema/W3cStatsIdentifiers'; | ||
import { createLogger } from '../utils/logger'; | ||
import { StatsAdapter } from './StatsAdapter'; | ||
|
||
const logger = createLogger('StatsAdapter'); | ||
|
||
export type StatsAdapter = (stats: W3CStats.RtcStats[]) => W3CStats.RtcStats[]; | ||
|
||
export class StatsAdapters { | ||
private readonly _adapters: StatsAdapter[] = []; | ||
public readonly adapters = new Map<string, StatsAdapter>(); | ||
|
||
public add(adapter: StatsAdapter) { | ||
this._adapters.push(adapter); | ||
if (this.adapters.has(adapter.name)) { | ||
return logger.warn('Adapter with name already exists', adapter.name); | ||
} | ||
|
||
this.adapters.set(adapter.name, adapter); | ||
} | ||
|
||
public remove(adapter: StatsAdapter) { | ||
const index = this._adapters.indexOf(adapter); | ||
if (index < 0) return; | ||
this._adapters.splice(index, 1); | ||
public remove(adapter: StatsAdapter | string) { | ||
if (typeof adapter === 'string') { | ||
return this.adapters.delete(adapter); | ||
} | ||
return this.adapters.delete(adapter.name); | ||
} | ||
|
||
public adapt(input: W3CStats.RtcStats[]): W3CStats.RtcStats[] { | ||
let result: W3CStats.RtcStats[] = input; | ||
|
||
for (const adapter of this._adapters) { | ||
for (const adapter of this.adapters.values()) { | ||
try { | ||
result = adapter(result); | ||
result = adapter.adapt(result); | ||
} catch (err) { | ||
logger.warn('Error adapting stats', err); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public postAdapt(input: W3CStats.RtcStats[]): W3CStats.RtcStats[] { | ||
let result: W3CStats.RtcStats[] = input; | ||
|
||
for (const adapter of this.adapters.values()) { | ||
if (!adapter.postAdapt) continue; | ||
try { | ||
result = adapter.postAdapt(result); | ||
} catch (err) { | ||
logger.warn('Error post adapting stats', err); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.