|
@@ -0,0 +1,111 @@
|
|
1
|
+import rtcstatsInit from 'rtcstats/rtcstats';
|
|
2
|
+import traceInit from 'rtcstats/trace-ws';
|
|
3
|
+
|
|
4
|
+import {
|
|
5
|
+ createRTCStatsTraceCloseEvent,
|
|
6
|
+ sendAnalytics
|
|
7
|
+} from '../analytics';
|
|
8
|
+
|
|
9
|
+import logger from './logger';
|
|
10
|
+
|
|
11
|
+/**
|
|
12
|
+ * Filter out RTCPeerConnection that are created by callstats.io.
|
|
13
|
+ *
|
|
14
|
+ * @param {*} config - Config object sent to the PC c'tor.
|
|
15
|
+ * @returns {boolean}
|
|
16
|
+ */
|
|
17
|
+function connectionFilter(config) {
|
|
18
|
+ if (config && config.iceServers[0] && config.iceServers[0].urls) {
|
|
19
|
+ for (const iceUrl of config.iceServers[0].urls) {
|
|
20
|
+ if (iceUrl.indexOf('taas.callstats.io') >= 0) {
|
|
21
|
+ return true;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ }
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+/**
|
|
28
|
+ * Class that controls the rtcstats flow, because it overwrites and proxies global function it should only be
|
|
29
|
+ * initialized once.
|
|
30
|
+ */
|
|
31
|
+class RTCStats {
|
|
32
|
+ /**
|
|
33
|
+ * Initialize the rtcstats components. First off we initialize the trace, which is a wrapped websocket
|
|
34
|
+ * that does the actual communication with the server. Secondly, the rtcstats component is initialized,
|
|
35
|
+ * it overwrites GUM and PeerConnection global functions and adds a proxy over them used to capture stats.
|
|
36
|
+ * Note, lib-jitsi-meet takes references to these methods before initializing so the init method needs to be
|
|
37
|
+ * loaded before it does.
|
|
38
|
+ *
|
|
39
|
+ * @param {Object} options -.
|
|
40
|
+ * @param {string} options.rtcstatsEndpoint - The Amplitude app key required.
|
|
41
|
+ * @param {number} options.rtcstatsPollInterval - The getstats poll interval in ms.
|
|
42
|
+ * @returns {void}
|
|
43
|
+ */
|
|
44
|
+ init(options) {
|
|
45
|
+ this.handleTraceWSClose = this.handleTraceWSClose.bind(this);
|
|
46
|
+ this.trace = traceInit(options.rtcstatsEndpoint, this.handleTraceWSClose);
|
|
47
|
+ rtcstatsInit(this.trace, options.rtcstatsPollInterval, [ '' ], connectionFilter);
|
|
48
|
+ this.initialized = true;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ /**
|
|
52
|
+ * Check whether or not the RTCStats is initialized.
|
|
53
|
+ *
|
|
54
|
+ * @returns {boolean}
|
|
55
|
+ */
|
|
56
|
+ isInitialized() {
|
|
57
|
+ return this.initialized;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ /**
|
|
61
|
+ * Send identity data to rtcstats server, this will be reflected in the identity section of the stats dump.
|
|
62
|
+ * It can be generally used to send additional metadata that might be relevant such as amplitude user data
|
|
63
|
+ * or deployment specific information.
|
|
64
|
+ *
|
|
65
|
+ * @param {Object} identityData - Metadata object to send as identity.
|
|
66
|
+ * @returns {void}
|
|
67
|
+ */
|
|
68
|
+ sendIdentityData(identityData) {
|
|
69
|
+ this.trace && this.trace('identity', null, identityData);
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ /**
|
|
73
|
+ * Connect to the rtcstats server instance. Stats (data obtained from getstats) won't be send until the
|
|
74
|
+ * connect successfully initializes, however calls to GUM are recorded in an internal buffer even if not
|
|
75
|
+ * connected and sent once it is established.
|
|
76
|
+ *
|
|
77
|
+ * @returns {void}
|
|
78
|
+ */
|
|
79
|
+ connect() {
|
|
80
|
+ this.trace && this.trace.connect();
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ /**
|
|
84
|
+ * Self explanatory; closes the web socked connection.
|
|
85
|
+ * Note, at the point of writing this documentation there was no method to reset the function overwrites,
|
|
86
|
+ * thus even if the websocket is closed the global function proxies are still active but send no data,
|
|
87
|
+ * this shouldn't influence the normal flow of the application.
|
|
88
|
+ *
|
|
89
|
+ * @returns {void}
|
|
90
|
+ */
|
|
91
|
+ close() {
|
|
92
|
+ this.trace && this.trace.close();
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ /**
|
|
96
|
+ * The way rtcstats is currently designed the ws wouldn't normally be closed by the application logic but rather
|
|
97
|
+ * by the page being closed/reloaded. Using this assumption any onclose event is most likely something abnormal
|
|
98
|
+ * that happened on the ws. We then track this in order to determine how many rtcstats connection were closed
|
|
99
|
+ * prematurely.
|
|
100
|
+ *
|
|
101
|
+ * @param {Object} closeEvent - Event sent by ws onclose.
|
|
102
|
+ * @returns {void}
|
|
103
|
+ */
|
|
104
|
+ handleTraceWSClose(closeEvent) {
|
|
105
|
+ logger.info('RTCStats trace ws closed', closeEvent);
|
|
106
|
+
|
|
107
|
+ sendAnalytics(createRTCStatsTraceCloseEvent(closeEvent));
|
|
108
|
+ }
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+export default new RTCStats();
|