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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import rtcstatsInit from '@jitsi/rtcstats/rtcstats';
  2. import traceInit from '@jitsi/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. * Send dominant speaker data, the data will be processed by rtcstats-server and saved in the dump file.
  79. *
  80. * @param {Object} dominantSpeakerData - Dominant speaker data to be saved in the rtcstats dump.
  81. * @returns {void}
  82. */
  83. sendDominantSpeakerData(dominantSpeakerData) {
  84. this.trace && this.trace.statsEntry('dominantSpeaker', null, dominantSpeakerData);
  85. }
  86. /**
  87. * Send e2e rtt data, the data will be processed by rtcstats-server and saved in the dump file.
  88. *
  89. * @param {Object} e2eRttData - The object that holds the e2e data.
  90. * @returns {void}
  91. */
  92. sendE2eRttData(e2eRttData) {
  93. this.trace && this.trace.statsEntry('e2eRtt', null, e2eRttData);
  94. }
  95. /**
  96. * Send face expression data, the data will be processed by rtcstats-server and saved in the dump file.
  97. *
  98. * @param {Object} faceExpressionData - Face expression data to be saved in the rtcstats dump.
  99. * @returns {void}
  100. */
  101. sendFaceExpressionData(faceExpressionData) {
  102. this.trace && this.trace.statsEntry('faceExpression', null, faceExpressionData);
  103. }
  104. /**
  105. * Connect to the rtcstats server instance. Stats (data obtained from getstats) won't be send until the
  106. * connect successfully initializes, however calls to GUM are recorded in an internal buffer even if not
  107. * connected and sent once it is established.
  108. *
  109. * @returns {void}
  110. */
  111. connect() {
  112. this.trace && this.trace.connect();
  113. }
  114. /**
  115. * Self explanatory; closes the web socked connection.
  116. * Note, at the point of writing this documentation there was no method to reset the function overwrites,
  117. * thus even if the websocket is closed the global function proxies are still active but send no data,
  118. * this shouldn't influence the normal flow of the application.
  119. *
  120. * @returns {void}
  121. */
  122. close() {
  123. this.trace && this.trace.close();
  124. }
  125. /**
  126. * The way rtcstats is currently designed the ws wouldn't normally be closed by the application logic but rather
  127. * by the page being closed/reloaded. Using this assumption any onclose event is most likely something abnormal
  128. * that happened on the ws. We then track this in order to determine how many rtcstats connection were closed
  129. * prematurely.
  130. *
  131. * @param {Object} closeEvent - Event sent by ws onclose.
  132. * @returns {void}
  133. */
  134. handleTraceWSClose(closeEvent) {
  135. logger.info('RTCStats trace ws closed', closeEvent);
  136. sendAnalytics(createRTCStatsTraceCloseEvent(closeEvent));
  137. }
  138. }
  139. export default new RTCStats();