Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix: Reinitialize rtcstats when the config changes (#13181)

George Politis78ce68160a7f
diff --git a/react/features/rtcstats/RTCStats.ts b/react/features/rtcstats/RTCStats.ts
index 69567aa54b..02d218b1c4 100644
--- a/react/features/rtcstats/RTCStats.ts
+++ b/react/features/rtcstats/RTCStats.ts
@@ -47,9 +47,46 @@ function connectionFilter(config: any) {
*/
class RTCStats {
trace: any;
- initialized = false;
+ options?: InitOptions;
+ isPeerConnectionWrapped = false;
connStateEvents: any = [];
+ /**
+ * Initialize the rtcstats components, if the options have changed.
+ *
+ * @param {Oject} newOptions -.
+ * @returns {void}
+ */
+ maybeInit(newOptions: InitOptions) {
+ const oldOptions = this.options;
+ const changed = !oldOptions || (oldOptions.endpoint !== newOptions.endpoint
+ || oldOptions.meetingFqn !== newOptions.meetingFqn
+ || oldOptions.pollInterval !== newOptions.pollInterval
+ || oldOptions.sendSdp !== newOptions.sendSdp
+ || oldOptions.useLegacy !== newOptions.useLegacy);
+
+ if (changed) {
+ this.reset();
+
+ if (newOptions.meetingFqn && newOptions.endpoint) {
+ this.init(newOptions);
+ } else {
+ logger.warn('RTCStats is enabled but it has not been configured.');
+ }
+ }
+ }
+
+ /**
+ * Wrapper method for the underlying trace object to be used as a static reference from inside the wrapped
+ * PeerConnection.
+ *
+ * @param {any[]} data - The stats entry to send to the rtcstats endpoint.
+ * @returns {void}
+ */
+ statsEntry(...data: any[]) {
+ this.trace?.statsEntry(data);
+ }
+
/**
* 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,
@@ -77,17 +114,22 @@ class RTCStats {
useLegacy
};
- const rtcstatsOptions = {
- connectionFilter,
- pollInterval,
- useLegacy,
- sendSdp,
- eventCallback: this.handleRtcstatsEvent.bind(this)
- };
-
this.trace = traceInit(traceOptions);
- rtcstatsInit(this.trace, rtcstatsOptions);
- this.initialized = true;
+ if (!this.isPeerConnectionWrapped) {
+ const rtcstatsOptions = {
+ connectionFilter,
+ pollInterval,
+ useLegacy,
+ sendSdp,
+ eventCallback: this.handleRtcstatsEvent.bind(this)
+ };
+
+ const statsEntryCallback = { statsEntry: this.statsEntry.bind(this) };
+
+ rtcstatsInit(statsEntryCallback, rtcstatsOptions);
+ this.isPeerConnectionWrapped = true;
+ }
+ this.options = options;
}
/**
@@ -96,7 +138,21 @@ class RTCStats {
* @returns {boolean}
*/
isInitialized() {
- return this.initialized;
+ return this.options !== undefined;
+ }
+
+ /**
+ * Resets the rtcstats.
+ *
+ * @returns {void}
+ */
+ reset() {
+ delete this.options;
+
+ if (this.trace) {
+ this.trace.close();
+ delete this.trace;
+ }
}
/**
diff --git a/react/features/rtcstats/middleware.ts b/react/features/rtcstats/middleware.ts
index d7ca51dac6..31b869bb32 100644
--- a/react/features/rtcstats/middleware.ts
+++ b/react/features/rtcstats/middleware.ts
@@ -41,7 +41,7 @@ MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: AnyA
switch (action.type) {
case LIB_WILL_INIT: {
- if (isRtcstatsEnabled(state) && !RTCStats.isInitialized()) {
+ if (isRtcstatsEnabled(state)) {
// RTCStats "proxies" WebRTC functions such as GUM and RTCPeerConnection by rewriting the global
// window functions. Because lib-jitsi-meet uses references to those functions that are taken on
// init, we need to add these proxies before it initializes, otherwise lib-jitsi-meet will use the
@@ -54,7 +54,8 @@ MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: AnyA
// Initialize but don't connect to the rtcstats server wss, as it will start sending data for all
// media calls made even before the conference started.
- RTCStats.init({
+
+ RTCStats.maybeInit({
endpoint: analytics?.rtcstatsEndpoint,
meetingFqn: extractFqnFromPath(state),
useLegacy,
@@ -64,6 +65,8 @@ MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: AnyA
} catch (error) {
logger.error('Failed to initialize RTCStats: ', error);
}
+ } else {
+ RTCStats.reset();
}
break;
}