You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTCStats.js 3.9KB

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