Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add probe to fetch webrtc statistics periodically #350

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"bugs:": "https://github.com/team-telnyx/webrtc/issues",
"license": "MIT",
"dependencies": {
"@peermetrics/webrtc-stats": "^5.7.1",
"loglevel": "^1.6.8",
"uuid": "^7.0.3"
},
Expand Down
58 changes: 52 additions & 6 deletions packages/js/src/Modules/Verto/webrtc/BaseCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ import {
import { MCULayoutEventHandler } from './LayoutHandler';
import Call from './Call';
import pkg from '../../../../package.json';

import { WebRTCStats } from '@peermetrics/webrtc-stats';
const SDK_VERSION = pkg.version;

/**
* @ignore Hide in docs output
*/
export default abstract class BaseCall implements IWebRTCCall {
private _webRTCStats: WebRTCStats | null;

/**
* The call identifier.
*/
Expand Down Expand Up @@ -183,6 +185,50 @@ export default abstract class BaseCall implements IWebRTCCall {
}
}

public get isDebuggerRunning(): boolean {
return this._webRTCStats != null;
}
public startDebugger() {
if (this.isDebuggerRunning) {
return;
}

this._webRTCStats = new WebRTCStats({
getStatsInterval: 1000,
rawStats: false,
statsObject: false,
filteredStats: false,
remote: true,
wrapGetUserMedia: true,
debug: false,
logLevel: 'none',
});

this._webRTCStats.addConnection({
pc: this.peer.instance,
peerId: this.options.id,
connectionId: `${this.options.telnyxSessionId}/${this.options.telnyxLegId}`,
});
}
public stopDebugger() {
if (this._webRTCStats == null) {
return;
}
const data = this._webRTCStats.getTimeline('stats');
const blob = new Blob([JSON.stringify(data)], {
type: 'application/json',
});
const url = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = url;
anchor.download = `call_${this.options.id}.json`;
anchor.click();
URL.revokeObjectURL(url);

this._webRTCStats.destroy();
this._webRTCStats = null;
}

get nodeId(): string {
return this._targetNodeId;
}
Expand Down Expand Up @@ -266,10 +312,10 @@ export default abstract class BaseCall implements IWebRTCCall {

this.direction = Direction.Inbound;

if(params?.customHeaders?.length > 0) {
if (params?.customHeaders?.length > 0) {
this.options = {
...this.options,
customHeaders: params.customHeaders
customHeaders: params.customHeaders,
};
}

Expand Down Expand Up @@ -330,7 +376,7 @@ export default abstract class BaseCall implements IWebRTCCall {
};

this.stopRingtone();

this.stopDebugger();
if (execute) {
const bye = new Bye({
sessid: this.session.sessionid,
Expand Down Expand Up @@ -921,11 +967,11 @@ export default abstract class BaseCall implements IWebRTCCall {
if (params.telnyx_call_control_id) {
this.options.telnyxCallControlId = params.telnyx_call_control_id;
}

if (params.telnyx_session_id) {
this.options.telnyxSessionId = params.telnyx_session_id;
}

if (params.telnyx_leg_id) {
this.options.telnyxLegId = params.telnyx_leg_id;
}
Expand Down
11 changes: 4 additions & 7 deletions packages/js/src/Modules/Verto/webrtc/Call.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logger from '../util/logger';
import { getDisplayMedia, setMediaElementSinkId } from '../util/webrtc';
import BaseCall from './BaseCall';
import { IVertoCallOptions } from './interfaces';
import { getDisplayMedia, setMediaElementSinkId } from '../util/webrtc';

/**
* A `Call` is the representation of an audio or video call between
Expand Down Expand Up @@ -53,6 +53,7 @@ import { getDisplayMedia, setMediaElementSinkId } from '../util/webrtc';
export class Call extends BaseCall {
public screenShare: Call;


private _statsInterval: any = null;

hangup(params: any = {}, execute: boolean = true) {
Expand All @@ -71,12 +72,8 @@ export class Call extends BaseCall {
}
});
});
const {
remoteCallerName,
remoteCallerNumber,
callerName,
callerNumber,
} = this.options;
const { remoteCallerName, remoteCallerNumber, callerName, callerNumber } =
this.options;
const options: IVertoCallOptions = {
screenShare: true,
localStream: displayStream,
Expand Down
Loading
Loading