2022-09-06 07:42:59 +00:00
|
|
|
// @ts-ignore
|
2021-11-10 13:50:36 +00:00
|
|
|
import rtcstatsInit from '@jitsi/rtcstats/rtcstats';
|
2022-10-11 10:47:54 +00:00
|
|
|
// eslint-disable-next-line lines-around-comment
|
2022-09-06 07:42:59 +00:00
|
|
|
// @ts-ignore
|
2021-11-10 13:50:36 +00:00
|
|
|
import traceInit from '@jitsi/rtcstats/trace-ws';
|
2020-07-15 15:22:00 +00:00
|
|
|
|
2022-09-07 08:20:05 +00:00
|
|
|
import { createRTCStatsTraceCloseEvent } from '../analytics/AnalyticsEvents';
|
|
|
|
import { sendAnalytics } from '../analytics/functions';
|
2020-07-15 15:22:00 +00:00
|
|
|
|
|
|
|
import logger from './logger';
|
2022-09-06 07:42:59 +00:00
|
|
|
import {
|
|
|
|
DominantSpeakerData,
|
|
|
|
E2ERTTData,
|
|
|
|
FaceLandmarksData,
|
|
|
|
InitOptions,
|
|
|
|
VideoTypeData
|
|
|
|
} from './types';
|
2020-07-15 15:22:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter out RTCPeerConnection that are created by callstats.io.
|
|
|
|
*
|
|
|
|
* @param {*} config - Config object sent to the PC c'tor.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
function connectionFilter(config: any) {
|
2022-09-08 09:52:36 +00:00
|
|
|
if (config?.iceServers[0] && config.iceServers[0].urls) {
|
2020-07-15 15:22:00 +00:00
|
|
|
for (const iceUrl of config.iceServers[0].urls) {
|
2022-09-06 07:42:59 +00:00
|
|
|
if (iceUrl.indexOf('callstats.io') >= 0) {
|
2020-07-15 15:22:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that controls the rtcstats flow, because it overwrites and proxies global function it should only be
|
|
|
|
* initialized once.
|
|
|
|
*/
|
|
|
|
class RTCStats {
|
2022-09-06 07:42:59 +00:00
|
|
|
trace: any;
|
|
|
|
initialized = false;
|
|
|
|
|
2020-07-15 15:22:00 +00:00
|
|
|
/**
|
|
|
|
* Initialize the rtcstats components. First off we initialize the trace, which is a wrapped websocket
|
|
|
|
* that does the actual communication with the server. Secondly, the rtcstats component is initialized,
|
|
|
|
* it overwrites GUM and PeerConnection global functions and adds a proxy over them used to capture stats.
|
|
|
|
* Note, lib-jitsi-meet takes references to these methods before initializing so the init method needs to be
|
|
|
|
* loaded before it does.
|
|
|
|
*
|
|
|
|
* @param {Object} options -.
|
2021-04-14 09:32:16 +00:00
|
|
|
* @param {string} options.endpoint - The Amplitude app key required.
|
2022-09-06 07:42:59 +00:00
|
|
|
* @param {string} options.meetingFqn - The meeting fqn.
|
|
|
|
* @param {boolean} options.useLegacy - Switch to legacy chrome webrtc statistics. Parameter will only have
|
2021-04-14 09:32:16 +00:00
|
|
|
* an effect on chrome based applications.
|
|
|
|
* @param {number} options.pollInterval - The getstats poll interval in ms.
|
2022-08-26 18:03:08 +00:00
|
|
|
* @param {boolean} options.sendSdp - Determines if the client sends SDP to the rtcstats server.
|
2020-07-15 15:22:00 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
init(options: InitOptions) {
|
2021-04-14 09:32:16 +00:00
|
|
|
|
2022-09-06 07:42:59 +00:00
|
|
|
const { endpoint, meetingFqn, useLegacy, pollInterval, sendSdp } = options;
|
2021-04-14 09:32:16 +00:00
|
|
|
|
|
|
|
const traceOptions = {
|
|
|
|
endpoint,
|
2022-09-06 07:42:59 +00:00
|
|
|
meetingFqn,
|
2021-04-14 09:32:16 +00:00
|
|
|
onCloseCallback: this.handleTraceWSClose.bind(this),
|
|
|
|
useLegacy
|
|
|
|
};
|
|
|
|
|
|
|
|
const rtcstatsOptions = {
|
|
|
|
connectionFilter,
|
|
|
|
pollInterval,
|
2022-08-26 18:03:08 +00:00
|
|
|
useLegacy,
|
|
|
|
sendSdp
|
2021-04-14 09:32:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.trace = traceInit(traceOptions);
|
|
|
|
rtcstatsInit(this.trace, rtcstatsOptions);
|
2020-07-15 15:22:00 +00:00
|
|
|
this.initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether or not the RTCStats is initialized.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isInitialized() {
|
|
|
|
return this.initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send identity data to rtcstats server, this will be reflected in the identity section of the stats dump.
|
|
|
|
* It can be generally used to send additional metadata that might be relevant such as amplitude user data
|
|
|
|
* or deployment specific information.
|
|
|
|
*
|
2022-09-06 07:42:59 +00:00
|
|
|
* @param {any} identityData - Metadata object to send as identity.
|
2020-07-15 15:22:00 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
sendIdentityData(identityData: any) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.identity('identity', null, identityData);
|
2020-07-15 15:22:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 12:41:05 +00:00
|
|
|
/**
|
|
|
|
* Send console logs to rtcstats server.
|
|
|
|
*
|
|
|
|
* @param {Array<string|any>} logEntries - The log entries to send to the rtcstats server.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
sendLogs(logEntries: Array<string | any>) {
|
|
|
|
this.trace?.statsEntry('logs', null, logEntries);
|
2022-08-25 12:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:20:50 +00:00
|
|
|
/**
|
|
|
|
* Send dominant speaker data, the data will be processed by rtcstats-server and saved in the dump file.
|
|
|
|
*
|
|
|
|
* @param {Object} dominantSpeakerData - Dominant speaker data to be saved in the rtcstats dump.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
sendDominantSpeakerData(dominantSpeakerData: DominantSpeakerData) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.statsEntry('dominantSpeaker', null, dominantSpeakerData);
|
2021-09-07 13:20:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 12:50:52 +00:00
|
|
|
/**
|
|
|
|
* Send e2e rtt data, the data will be processed by rtcstats-server and saved in the dump file.
|
|
|
|
*
|
|
|
|
* @param {Object} e2eRttData - The object that holds the e2e data.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
sendE2eRttData(e2eRttData: E2ERTTData) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.statsEntry('e2eRtt', null, e2eRttData);
|
2022-04-01 12:50:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 22:22:15 +00:00
|
|
|
/**
|
|
|
|
* Send the timestamp of the start of the conference, the data will be processed by the rtcstats-server
|
|
|
|
* and saved in the dump file.
|
|
|
|
*
|
2022-07-14 07:10:08 +00:00
|
|
|
* @param {Object} timestamp - The object which contains the timestamp.
|
2022-06-15 22:22:15 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
sendConferenceTimestamp(timestamp: number) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.statsEntry('conferenceStartTimestamp', null, timestamp);
|
2022-06-15 22:22:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 13:56:37 +00:00
|
|
|
/**
|
|
|
|
* Send videoType data, the data will be processed by rtcstats-server and saved in the dump file.
|
|
|
|
*
|
|
|
|
* @param {Object} videoTypeData - The object that holds the videoType data.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
sendVideoTypeData(videoTypeData: VideoTypeData) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.statsEntry('setVideoType', null, videoTypeData);
|
2022-05-16 13:56:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 10:24:28 +00:00
|
|
|
/**
|
2022-09-06 07:42:59 +00:00
|
|
|
* Send face landmarks data, the data will be processed by rtcstats-server and saved in the dump file.
|
2021-11-26 10:24:28 +00:00
|
|
|
*
|
2022-09-06 07:42:59 +00:00
|
|
|
* @param {Object} faceLandmarksData - Face landmarks data to be saved in the rtcstats dump.
|
2021-11-26 10:24:28 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
sendFaceLandmarksData(faceLandmarksData: FaceLandmarksData) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.statsEntry('faceLandmarks', null, faceLandmarksData);
|
2021-11-26 10:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:22:00 +00:00
|
|
|
/**
|
|
|
|
* Connect to the rtcstats server instance. Stats (data obtained from getstats) won't be send until the
|
|
|
|
* connect successfully initializes, however calls to GUM are recorded in an internal buffer even if not
|
|
|
|
* connected and sent once it is established.
|
|
|
|
*
|
2022-09-06 07:42:59 +00:00
|
|
|
* @param {boolean} isBreakoutRoom - Flag indicating if the user is in a breakout room.
|
2020-07-15 15:22:00 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
connect(isBreakoutRoom: boolean) {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.connect(isBreakoutRoom);
|
2020-07-15 15:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Self explanatory; closes the web socked connection.
|
|
|
|
* Note, at the point of writing this documentation there was no method to reset the function overwrites,
|
|
|
|
* thus even if the websocket is closed the global function proxies are still active but send no data,
|
|
|
|
* this shouldn't influence the normal flow of the application.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
close() {
|
2022-09-08 09:52:36 +00:00
|
|
|
this.trace?.close();
|
2020-07-15 15:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The way rtcstats is currently designed the ws wouldn't normally be closed by the application logic but rather
|
|
|
|
* by the page being closed/reloaded. Using this assumption any onclose event is most likely something abnormal
|
|
|
|
* that happened on the ws. We then track this in order to determine how many rtcstats connection were closed
|
|
|
|
* prematurely.
|
|
|
|
*
|
|
|
|
* @param {Object} closeEvent - Event sent by ws onclose.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-06 07:42:59 +00:00
|
|
|
handleTraceWSClose(closeEvent: any) {
|
2020-07-15 15:22:00 +00:00
|
|
|
logger.info('RTCStats trace ws closed', closeEvent);
|
|
|
|
|
|
|
|
sendAnalytics(createRTCStatsTraceCloseEvent(closeEvent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new RTCStats();
|