Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
balazskreith committed Feb 18, 2025
1 parent b093f18 commit 92a9732
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 40 deletions.
9 changes: 5 additions & 4 deletions src/adapters/Firefox94StatsAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RtcStats } from "../schema/W3cStatsIdentifiers";
import * as W3C from "../schema/W3cStatsIdentifiers";

export class Firefox94StatsAdapter {
public readonly name = 'firefox94StatsAdapter';
Expand All @@ -11,10 +12,10 @@ export class Firefox94StatsAdapter {
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"
rawType === W3C.StatsType.inboundRtp ||
rawType === W3C.StatsType.outboundRtp ||
rawType === W3C.StatsType.remoteInboundRtp ||
rawType === W3C.StatsType.remoteOutboundRtp
) {
if (rawValue.mediaType && !rawValue.kind) {
rawValue.kind = rawValue.mediaType;
Expand Down
14 changes: 10 additions & 4 deletions src/adapters/FirefoxTransportStatsAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ export class FirefoxTransportStatsAdapter implements StatsAdapter {

adapt(stats: RtcStats[]): RtcStats[] {
let selectedCandidatePair: SelectedIceCandidatePairStats | undefined;
let hasTransportStats = false;

for (const stat of stats) {
if (stat.type === W3C.StatsType.transport) {
hasTransportStats = true;
break;
}
if (stat.type !== W3C.StatsType.candidatePair) continue;

const pair = stat as SelectedIceCandidatePairStats;

if (!pair.selected) continue;
Expand All @@ -51,7 +55,9 @@ export class FirefoxTransportStatsAdapter implements StatsAdapter {
break;
}

if (!selectedCandidatePair) return stats;
// console.warn('selectedCandidatePair', selectedCandidatePair, 'hasTransportStats', hasTransportStats);

if (hasTransportStats || !selectedCandidatePair) return stats;

if (this._selectedCandidatePair?.id !== selectedCandidatePair.id) {
this.stats.selectedCandidatePairChanges = (this.stats.selectedCandidatePairChanges ?? 0) + 1;
Expand All @@ -63,8 +69,8 @@ export class FirefoxTransportStatsAdapter implements StatsAdapter {
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 < deltaBytesReceived) this.stats.bytesReceived = (this.stats.bytesReceived ?? 0) + deltaBytesReceived;
if (0 < deltaBytesSent) 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;
}
Expand Down
32 changes: 28 additions & 4 deletions src/collectors/MediasoupTransportStatsCollector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ClientMonitor, InboundRtpStats } from "..";
import { ClientMonitor, CodecStats, InboundRtpStats, MediaSourceStats, OutboundRtpStats } from "..";
import { RtcStats } from "../schema/W3cStatsIdentifiers";
import { StatsCollector } from "./StatsCollector";
import { createLogger } from "../utils/logger";
import * as mediasoup from 'mediasoup-client';
import { convertRTCStatsReport } from "./utils";
import * as W3C from '../schema/W3cStatsIdentifiers';

const logger = createLogger('MediasoupTransportStatsCollector');

Expand Down Expand Up @@ -32,7 +33,8 @@ export class MediasoupTransportStatsCollector implements StatsCollector {
public async getStats(): Promise<RtcStats[]> {
try {
this.lastStats = convertRTCStatsReport(await this.transport.getStats());

this._stats = [];

// rip out the probator
for (const stats of this.lastStats) {
if (stats.type === 'inbound-rtp' ) {
Expand Down Expand Up @@ -82,7 +84,7 @@ export class MediasoupTransportStatsCollector implements StatsCollector {
break;
}
case 'firefox': {
this._firefoxTrackStats();
await this._firefoxTrackStats();
break;
}
case 'unknown' : {
Expand All @@ -91,7 +93,29 @@ export class MediasoupTransportStatsCollector implements StatsCollector {
}
}

private _firefoxTrackStats() {
private async _firefoxTrackStats() {
// get the track ids, then run the getStats again.
await Promise.allSettled([...this.producers.values()].map(async producer => {
const report = await producer.getStats();
const outboundRtpStats: OutboundRtpStats[] = [];
let mediaSourceStat: MediaSourceStats | undefined;
let codecStats: CodecStats | undefined;

report.forEach(stat => {
if (stat.type === W3C.StatsType.outboundRtp) outboundRtpStats.push(stat as OutboundRtpStats);
else if (stat.type === W3C.StatsType.mediaSource) mediaSourceStat = stat as MediaSourceStats;
else if (stat.type === W3C.StatsType.codec) codecStats = stat as CodecStats;
});

for (const outboundRtp of outboundRtpStats) {
const stat = this._stats.find(stat => stat.id === outboundRtp.id) as (OutboundRtpStats & RtcStats);

if (!stat) continue;


stat.codecId = codecStats?.id;
stat.mediaSourceId = mediaSourceStat?.id;
}
}));
}
}
59 changes: 32 additions & 27 deletions src/collectors/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,47 @@ import { createLogger } from '../utils/logger';

const logger = createLogger('StatsCollector');

export function convertRTCStatsReport(stats: RTCStatsReport) {
export function convertRTCStatsReport(input: RTCStatsReport) {
const result: W3C.RtcStats[] = [];

try {
input.forEach(report => {

// legacy support
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((stats as any).values) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rtcStats = stats as any;
if (rtcStats.values || typeof rtcStats.values === "function") {
for (const rtcStatValue of rtcStats.values()) {
if (
!rtcStatValue ||
!rtcStatValue.type ||
typeof rtcStatValue.type !== "string" ||
!rtcStatValue.id ||
!rtcStatValue.timestamp ||
false
) {
continue;
}
result.push(rtcStatValue);
}

return result;
}
}

stats.forEach((report) => {
if (!report.id) return;
if (!report.timestamp) return;
if (!report.type) return;

result.push(report);
});


// legacy support
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// if ((stats as any).values) {
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
// const rtcStats = stats as any;
// if (rtcStats.values || typeof rtcStats.values === "function") {
// for (const rtcStatValue of rtcStats.values()) {
// if (
// !rtcStatValue ||
// !rtcStatValue.type ||
// typeof rtcStatValue.type !== "string" ||
// !rtcStatValue.id ||
// !rtcStatValue.timestamp ||
// false
// ) {
// continue;
// }
// result.push(rtcStatValue);
// }

// return result;
// }
// }

// stats.forEach((report) => {

// });
} catch (err) {
logger.error('Error getting stats report', err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/DryOutboundTrackDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class DryOutboundTrackDetector implements Detector {

public update() {
if (this._evented || this.config.disabled) return;
if (this.trackMonitor.getOutboundRtps()?.[0].bytesSent !== 0) return;
if (this.trackMonitor.getOutboundRtps()?.[0]?.bytesSent !== 0) return;
if (this.trackMonitor.track.muted || this.trackMonitor.track.readyState !== 'live') {
this._activatedAt = undefined;
return;
Expand Down

0 comments on commit 92a9732

Please sign in to comment.