Skip to content

Commit

Permalink
Client metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Oct 3, 2024
1 parent c4d72f4 commit 0fb55c3
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 7 deletions.
11 changes: 8 additions & 3 deletions lib/dc_msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { zlibSync, unzlibSync, strToU8, strFromU8 } from 'fflate';
import { DCMessageType } from './types';
export function encodeDCMsg(enc, msgType, payload) {
const mt = enc.encode(msgType);
if (!payload) {
if (typeof payload === 'undefined') {
return mt;
}
const pl = msgType === DCMessageType.SDP ?
enc.encode(zlibSync(strToU8(JSON.stringify(payload)))) : enc.encode(JSON.stringify(payload));
let pl;
if (msgType === DCMessageType.SDP) {
pl = enc.encode(zlibSync(strToU8(JSON.stringify(payload))));
}
else {
pl = enc.encode(payload);
}
// Flat encoding
const msg = new Uint8Array(mt.byteLength + pl.byteLength);
msg.set(mt);
Expand Down
1 change: 1 addition & 0 deletions lib/rtc_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class RTCMonitor extends EventEmitter {
// Step 5 (or the magic step): calculate MOS (Mean Opinion Score)
const mos = this.calculateMOS(latency, jitter, lossRate);
this.emit('mos', mos);
this.peer.handleMetrics(lossRate, this.peer.getRTT(), jitter);
this.logger.logDebug(`RTCMonitor: MOS --> ${mos}`);
}
calculateMOS(latency, jitter, lossRate) {
Expand Down
1 change: 1 addition & 0 deletions lib/rtc_peer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export declare class RTCPeer extends EventEmitter {
replaceTrack(oldTrackID: string, newTrack: MediaStreamTrack | null): void;
removeTrack(trackID: string): void;
getStats(): Promise<RTCStatsReport>;
handleMetrics(lossRate: number, rtt: number, jitter: number): void;
static getVideoCodec(mimeType: string): Promise<RTCRtpCodecCapability | null>;
destroy(): void;
}
16 changes: 16 additions & 0 deletions lib/rtc_peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ export class RTCPeer extends EventEmitter {
}
return this.pc.getStats(null);
}
handleMetrics(lossRate, rtt, jitter) {
try {
if (lossRate >= 0) {
this.dc.send(encodeDCMsg(this.enc, DCMessageType.LossRate, lossRate));
}
if (rtt > 0) {
this.dc.send(encodeDCMsg(this.enc, DCMessageType.RoundTripTime, rtt));
}
if (jitter > 0) {
this.dc.send(encodeDCMsg(this.enc, DCMessageType.Jitter, jitter));
}
}
catch (err) {
this.logger.logErr('failed to send metrics through dc', err);
}
}
static getVideoCodec(mimeType) {
return __awaiter(this, void 0, void 0, function* () {
if (RTCRtpReceiver.getCapabilities) {
Expand Down
8 changes: 7 additions & 1 deletion lib/types/dc_msg.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export declare enum DCMessageType {
Ping = 1,
Pong = 2,
SDP = 3
SDP = 3,
LossRate = 4,
RoundTripTime = 5,
Jitter = 6
}
export type DCMessageSDP = Uint8Array;
export type DCMessageLossRate = number;
export type DCMessageRoundTripTime = number;
export type DCMessageJitter = number;
3 changes: 3 additions & 0 deletions lib/types/dc_msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ export var DCMessageType;
DCMessageType[DCMessageType["Ping"] = 1] = "Ping";
DCMessageType[DCMessageType["Pong"] = 2] = "Pong";
DCMessageType[DCMessageType["SDP"] = 3] = "SDP";
DCMessageType[DCMessageType["LossRate"] = 4] = "LossRate";
DCMessageType[DCMessageType["RoundTripTime"] = 5] = "RoundTripTime";
DCMessageType[DCMessageType["Jitter"] = 6] = "Jitter";
})(DCMessageType || (DCMessageType = {}));
10 changes: 7 additions & 3 deletions src/dc_msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import {DCMessageType, DCMessageSDP} from './types';

export function encodeDCMsg(enc: Encoder, msgType: DCMessageType, payload?: any) {
const mt = enc.encode(msgType);
if (!payload) {
if (typeof payload === 'undefined') {
return mt;
}

const pl = msgType === DCMessageType.SDP ?
enc.encode(zlibSync(strToU8(JSON.stringify(payload)))) : enc.encode(JSON.stringify(payload));
let pl;
if (msgType === DCMessageType.SDP) {
pl = enc.encode(zlibSync(strToU8(JSON.stringify(payload))));
} else {
pl = enc.encode(payload);
}

// Flat encoding
const msg = new Uint8Array(mt.byteLength + pl.byteLength);
Expand Down
1 change: 1 addition & 0 deletions src/rtc_monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class RTCMonitor extends EventEmitter {
// Step 5 (or the magic step): calculate MOS (Mean Opinion Score)
const mos = this.calculateMOS(latency!, jitter, lossRate);
this.emit('mos', mos);
this.peer.handleMetrics(lossRate, this.peer.getRTT(), jitter);
this.logger.logDebug(`RTCMonitor: MOS --> ${mos}`);
}

Expand Down
16 changes: 16 additions & 0 deletions src/rtc_peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,22 @@ export class RTCPeer extends EventEmitter {
return this.pc.getStats(null);
}

public handleMetrics(lossRate: number, rtt: number, jitter: number) {
try {
if (lossRate >= 0) {
this.dc.send(encodeDCMsg(this.enc, DCMessageType.LossRate, lossRate));
}
if (rtt > 0) {
this.dc.send(encodeDCMsg(this.enc, DCMessageType.RoundTripTime, rtt));
}
if (jitter > 0) {
this.dc.send(encodeDCMsg(this.enc, DCMessageType.Jitter, jitter));
}
} catch (err) {
this.logger.logErr('failed to send metrics through dc', err);
}
}

static async getVideoCodec(mimeType: string) {
if (RTCRtpReceiver.getCapabilities) {
const videoCapabilities = await RTCRtpReceiver.getCapabilities('video');
Expand Down
6 changes: 6 additions & 0 deletions src/types/dc_msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ export enum DCMessageType {
Ping = 1,
Pong,
SDP,
LossRate,
RoundTripTime,
Jitter,
}

export type DCMessageSDP = Uint8Array;
export type DCMessageLossRate = number;
export type DCMessageRoundTripTime = number;
export type DCMessageJitter = number;

0 comments on commit 0fb55c3

Please sign in to comment.