Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RTCStats.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 the timestamp of the start of the conference, the data will be processed by the rtcstats-server
  97. * and saved in the dump file.
  98. *
  99. * @param {Object} timestamp - The object which contains the timestamp.
  100. * @returns {void}
  101. */
  102. sendConferenceTimestamp(timestamp) {
  103. this.trace && this.trace.statsEntry('conferenceStartTimestamp', null, timestamp);
  104. }
  105. /**
  106. * Send videoType data, the data will be processed by rtcstats-server and saved in the dump file.
  107. *
  108. * @param {Object} videoTypeData - The object that holds the videoType data.
  109. * @returns {void}
  110. */
  111. sendVideoTypeData(videoTypeData) {
  112. this.trace && this.trace.statsEntry('setVideoType', null, videoTypeData);
  113. }
  114. /**
  115. * Send face expression data, the data will be processed by rtcstats-server and saved in the dump file.
  116. *
  117. * @param {Object} faceExpressionData - Face expression data to be saved in the rtcstats dump.
  118. * @returns {void}
  119. */
  120. sendFaceExpressionData(faceExpressionData) {
  121. this.trace && this.trace.statsEntry('faceLandmarks', null, faceExpressionData);
  122. }
  123. /**
  124. * Connect to the rtcstats server instance. Stats (data obtained from getstats) won't be send until the
  125. * connect successfully initializes, however calls to GUM are recorded in an internal buffer even if not
  126. * connected and sent once it is established.
  127. *
  128. * @returns {void}
  129. */
  130. connect() {
  131. this.trace && this.trace.connect();
  132. }
  133. /**
  134. * Self explanatory; closes the web socked connection.
  135. * Note, at the point of writing this documentation there was no method to reset the function overwrites,
  136. * thus even if the websocket is closed the global function proxies are still active but send no data,
  137. * this shouldn't influence the normal flow of the application.
  138. *
  139. * @returns {void}
  140. */
  141. close() {
  142. this.trace && this.trace.close();
  143. }
  144. /**
  145. * The way rtcstats is currently designed the ws wouldn't normally be closed by the application logic but rather
  146. * by the page being closed/reloaded. Using this assumption any onclose event is most likely something abnormal
  147. * that happened on the ws. We then track this in order to determine how many rtcstats connection were closed
  148. * prematurely.
  149. *
  150. * @param {Object} closeEvent - Event sent by ws onclose.
  151. * @returns {void}
  152. */
  153. handleTraceWSClose(closeEvent) {
  154. logger.info('RTCStats trace ws closed', closeEvent);
  155. sendAnalytics(createRTCStatsTraceCloseEvent(closeEvent));
  156. }
  157. }
  158. export default new RTCStats();