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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.endpoint - The Amplitude app key required.
  37. * @param {string} options.useLegacy - Switch to legacy chrome webrtc statistics. Parameter will only have
  38. * an effect on chrome based applications.
  39. * @param {number} options.pollInterval - The getstats poll interval in ms.
  40. * @returns {void}
  41. */
  42. init(options) {
  43. const { endpoint, useLegacy, pollInterval } = options;
  44. const traceOptions = {
  45. endpoint,
  46. onCloseCallback: this.handleTraceWSClose.bind(this),
  47. useLegacy
  48. };
  49. const rtcstatsOptions = {
  50. connectionFilter,
  51. pollInterval,
  52. useLegacy
  53. };
  54. this.trace = traceInit(traceOptions);
  55. rtcstatsInit(this.trace, rtcstatsOptions);
  56. this.initialized = true;
  57. }
  58. /**
  59. * Check whether or not the RTCStats is initialized.
  60. *
  61. * @returns {boolean}
  62. */
  63. isInitialized() {
  64. return this.initialized;
  65. }
  66. /**
  67. * Send identity data to rtcstats server, this will be reflected in the identity section of the stats dump.
  68. * It can be generally used to send additional metadata that might be relevant such as amplitude user data
  69. * or deployment specific information.
  70. *
  71. * @param {Object} identityData - Metadata object to send as identity.
  72. * @returns {void}
  73. */
  74. sendIdentityData(identityData) {
  75. this.trace && this.trace.identity('identity', null, identityData);
  76. }
  77. /**
  78. * Connect to the rtcstats server instance. Stats (data obtained from getstats) won't be send until the
  79. * connect successfully initializes, however calls to GUM are recorded in an internal buffer even if not
  80. * connected and sent once it is established.
  81. *
  82. * @returns {void}
  83. */
  84. connect() {
  85. this.trace && this.trace.connect();
  86. }
  87. /**
  88. * Self explanatory; closes the web socked connection.
  89. * Note, at the point of writing this documentation there was no method to reset the function overwrites,
  90. * thus even if the websocket is closed the global function proxies are still active but send no data,
  91. * this shouldn't influence the normal flow of the application.
  92. *
  93. * @returns {void}
  94. */
  95. close() {
  96. this.trace && this.trace.close();
  97. }
  98. /**
  99. * The way rtcstats is currently designed the ws wouldn't normally be closed by the application logic but rather
  100. * by the page being closed/reloaded. Using this assumption any onclose event is most likely something abnormal
  101. * that happened on the ws. We then track this in order to determine how many rtcstats connection were closed
  102. * prematurely.
  103. *
  104. * @param {Object} closeEvent - Event sent by ws onclose.
  105. * @returns {void}
  106. */
  107. handleTraceWSClose(closeEvent) {
  108. logger.info('RTCStats trace ws closed', closeEvent);
  109. sendAnalytics(createRTCStatsTraceCloseEvent(closeEvent));
  110. }
  111. }
  112. export default new RTCStats();