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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * @param {boolean} options.sendSdp - Determines if the client sends SDP to the rtcstats server.
  41. * @returns {void}
  42. */
  43. init(options) {
  44. const { endpoint, useLegacy, pollInterval, sendSdp } = options;
  45. const traceOptions = {
  46. endpoint,
  47. onCloseCallback: this.handleTraceWSClose.bind(this),
  48. useLegacy
  49. };
  50. const rtcstatsOptions = {
  51. connectionFilter,
  52. pollInterval,
  53. useLegacy,
  54. sendSdp
  55. };
  56. this.trace = traceInit(traceOptions);
  57. rtcstatsInit(this.trace, rtcstatsOptions);
  58. this.initialized = true;
  59. }
  60. /**
  61. * Check whether or not the RTCStats is initialized.
  62. *
  63. * @returns {boolean}
  64. */
  65. isInitialized() {
  66. return this.initialized;
  67. }
  68. /**
  69. * Send identity data to rtcstats server, this will be reflected in the identity section of the stats dump.
  70. * It can be generally used to send additional metadata that might be relevant such as amplitude user data
  71. * or deployment specific information.
  72. *
  73. * @param {Object} identityData - Metadata object to send as identity.
  74. * @returns {void}
  75. */
  76. sendIdentityData(identityData) {
  77. this.trace && this.trace.identity('identity', null, identityData);
  78. }
  79. /**
  80. * Send console logs to rtcstats server.
  81. *
  82. * @param {Array<string|any>} logEntries - The log entries to send to the rtcstats server.
  83. * @returns {void}
  84. */
  85. sendLogs(logEntries) {
  86. this.trace && this.trace.statsEntry('logs', null, logEntries);
  87. }
  88. /**
  89. * Send dominant speaker data, the data will be processed by rtcstats-server and saved in the dump file.
  90. *
  91. * @param {Object} dominantSpeakerData - Dominant speaker data to be saved in the rtcstats dump.
  92. * @returns {void}
  93. */
  94. sendDominantSpeakerData(dominantSpeakerData) {
  95. this.trace && this.trace.statsEntry('dominantSpeaker', null, dominantSpeakerData);
  96. }
  97. /**
  98. * Send e2e rtt data, the data will be processed by rtcstats-server and saved in the dump file.
  99. *
  100. * @param {Object} e2eRttData - The object that holds the e2e data.
  101. * @returns {void}
  102. */
  103. sendE2eRttData(e2eRttData) {
  104. this.trace && this.trace.statsEntry('e2eRtt', null, e2eRttData);
  105. }
  106. /**
  107. * Send the timestamp of the start of the conference, the data will be processed by the rtcstats-server
  108. * and saved in the dump file.
  109. *
  110. * @param {Object} timestamp - The object which contains the timestamp.
  111. * @returns {void}
  112. */
  113. sendConferenceTimestamp(timestamp) {
  114. this.trace && this.trace.statsEntry('conferenceStartTimestamp', null, timestamp);
  115. }
  116. /**
  117. * Send videoType data, the data will be processed by rtcstats-server and saved in the dump file.
  118. *
  119. * @param {Object} videoTypeData - The object that holds the videoType data.
  120. * @returns {void}
  121. */
  122. sendVideoTypeData(videoTypeData) {
  123. this.trace && this.trace.statsEntry('setVideoType', null, videoTypeData);
  124. }
  125. /**
  126. * Send face expression data, the data will be processed by rtcstats-server and saved in the dump file.
  127. *
  128. * @param {Object} faceExpressionData - Face expression data to be saved in the rtcstats dump.
  129. * @returns {void}
  130. */
  131. sendFaceExpressionData(faceExpressionData) {
  132. this.trace && this.trace.statsEntry('faceLandmarks', null, faceExpressionData);
  133. }
  134. /**
  135. * Connect to the rtcstats server instance. Stats (data obtained from getstats) won't be send until the
  136. * connect successfully initializes, however calls to GUM are recorded in an internal buffer even if not
  137. * connected and sent once it is established.
  138. *
  139. * @returns {void}
  140. */
  141. connect() {
  142. this.trace && this.trace.connect();
  143. }
  144. /**
  145. * Self explanatory; closes the web socked connection.
  146. * Note, at the point of writing this documentation there was no method to reset the function overwrites,
  147. * thus even if the websocket is closed the global function proxies are still active but send no data,
  148. * this shouldn't influence the normal flow of the application.
  149. *
  150. * @returns {void}
  151. */
  152. close() {
  153. this.trace && this.trace.close();
  154. }
  155. /**
  156. * The way rtcstats is currently designed the ws wouldn't normally be closed by the application logic but rather
  157. * by the page being closed/reloaded. Using this assumption any onclose event is most likely something abnormal
  158. * that happened on the ws. We then track this in order to determine how many rtcstats connection were closed
  159. * prematurely.
  160. *
  161. * @param {Object} closeEvent - Event sent by ws onclose.
  162. * @returns {void}
  163. */
  164. handleTraceWSClose(closeEvent) {
  165. logger.info('RTCStats trace ws closed', closeEvent);
  166. sendAnalytics(createRTCStatsTraceCloseEvent(closeEvent));
  167. }
  168. }
  169. export default new RTCStats();