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.

middleware.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import { getAmplitudeIdentity } from '../analytics';
  4. import { CONFERENCE_UNIQUE_ID_SET, E2E_RTT_CHANGED, getConferenceOptions, getRoomName } from '../base/conference';
  5. import { LIB_WILL_INIT } from '../base/lib-jitsi-meet/actionTypes';
  6. import { DOMINANT_SPEAKER_CHANGED, getLocalParticipant } from '../base/participants';
  7. import { MiddlewareRegistry } from '../base/redux';
  8. import { TRACK_ADDED, TRACK_UPDATED } from '../base/tracks';
  9. import { ADD_FACE_EXPRESSION } from '../face-landmarks/actionTypes';
  10. import RTCStats from './RTCStats';
  11. import { canSendRtcstatsData, isRtcstatsEnabled } from './functions';
  12. import logger from './logger';
  13. /**
  14. * Middleware which intercepts lib-jitsi-meet initialization and conference join in order init the
  15. * rtcstats-client.
  16. *
  17. * @param {Store} store - The redux store.
  18. * @returns {Function}
  19. */
  20. MiddlewareRegistry.register(store => next => action => {
  21. const state = store.getState();
  22. const config = state['features/base/config'];
  23. const { analytics } = config;
  24. switch (action.type) {
  25. case LIB_WILL_INIT: {
  26. if (isRtcstatsEnabled(state)) {
  27. // RTCStats "proxies" WebRTC functions such as GUM and RTCPeerConnection by rewriting the global
  28. // window functions. Because lib-jitsi-meet uses references to those functions that are taken on
  29. // init, we need to add these proxies before it initializes, otherwise lib-jitsi-meet will use the
  30. // original non proxy versions of these functions.
  31. try {
  32. // Default poll interval is 1000ms and standard stats will be used, if not provided in the config.
  33. const pollInterval = analytics.rtcstatsPollInterval || 1000;
  34. const useLegacy = analytics.rtcstatsUseLegacy || false;
  35. // Initialize but don't connect to the rtcstats server wss, as it will start sending data for all
  36. // media calls made even before the conference started.
  37. RTCStats.init({
  38. endpoint: analytics.rtcstatsEndpoint,
  39. useLegacy,
  40. pollInterval
  41. });
  42. } catch (error) {
  43. logger.error('Failed to initialize RTCStats: ', error);
  44. }
  45. }
  46. break;
  47. }
  48. case TRACK_ADDED: {
  49. if (canSendRtcstatsData(state)) {
  50. const jitsiTrack = action?.track?.jitsiTrack;
  51. const { ssrc, videoType } = jitsiTrack || { };
  52. // Remote tracks store their ssrc in the jitsiTrack object. Local tracks don't. See getSsrcByTrack.
  53. if (videoType && ssrc && !jitsiTrack.isLocal()) {
  54. RTCStats.sendVideoTypeData({
  55. ssrc,
  56. videoType
  57. });
  58. }
  59. }
  60. break;
  61. }
  62. case TRACK_UPDATED: {
  63. if (canSendRtcstatsData(state)) {
  64. const { videoType, jitsiTrack } = action?.track || { };
  65. const { ssrc } = jitsiTrack || { };
  66. // if the videoType of the remote track has changed we expect to find it in track.videoType. grep for
  67. // trackVideoTypeChanged.
  68. if (videoType && ssrc && !jitsiTrack.isLocal()) {
  69. RTCStats.sendVideoTypeData({
  70. ssrc,
  71. videoType
  72. });
  73. }
  74. }
  75. break;
  76. }
  77. case CONFERENCE_UNIQUE_ID_SET: {
  78. if (canSendRtcstatsData(state)) {
  79. // Once the conference started connect to the rtcstats server and send data.
  80. try {
  81. RTCStats.connect();
  82. const localParticipant = getLocalParticipant(state);
  83. const options = getConferenceOptions(state);
  84. // Unique identifier for a conference session, not to be confused with meeting name
  85. // i.e. If all participants leave a meeting it will have a different value on the next join.
  86. const { conference } = action;
  87. const meetingUniqueId = conference && conference.getMeetingUniqueId();
  88. // The current implementation of rtcstats-server is configured to send data to amplitude, thus
  89. // we add identity specific information so we can correlate on the amplitude side. If amplitude is
  90. // not configured an empty object will be sent.
  91. // The current configuration of the conference is also sent as metadata to rtcstats server.
  92. // This is done in order to facilitate queries based on different conference configurations.
  93. // e.g. Find all RTCPeerConnections that connect to a specific shard or were created in a
  94. // conference with a specific version.
  95. // XXX(george): we also want to be able to correlate between rtcstats and callstats, so we're
  96. // appending the callstats user name (if it exists) to the display name.
  97. const displayName = options.statisticsId
  98. || options.statisticsDisplayName
  99. || jitsiLocalStorage.getItem('callStatsUserName');
  100. RTCStats.sendIdentityData({
  101. ...getAmplitudeIdentity(),
  102. ...options,
  103. endpointId: localParticipant?.id,
  104. confName: getRoomName(state),
  105. displayName,
  106. meetingUniqueId
  107. });
  108. } catch (error) {
  109. // If the connection failed do not impact jitsi-meet just silently fail.
  110. logger.error('RTCStats connect failed with: ', error);
  111. }
  112. }
  113. break;
  114. }
  115. case DOMINANT_SPEAKER_CHANGED: {
  116. if (canSendRtcstatsData(state)) {
  117. const { id, previousSpeakers } = action.participant;
  118. RTCStats.sendDominantSpeakerData({ dominantSpeakerEndpoint: id,
  119. previousSpeakers });
  120. }
  121. break;
  122. }
  123. case E2E_RTT_CHANGED: {
  124. if (canSendRtcstatsData(state)) {
  125. const { participant, rtt } = action.e2eRtt;
  126. RTCStats.sendE2eRttData({
  127. remoteEndpointId: participant.getId(),
  128. rtt,
  129. remoteRegion: participant.getProperty('region')
  130. });
  131. }
  132. break;
  133. }
  134. case ADD_FACE_EXPRESSION: {
  135. if (canSendRtcstatsData(state)) {
  136. const { duration, faceExpression, timestamp } = action;
  137. RTCStats.sendFaceExpressionData({
  138. duration,
  139. faceExpression,
  140. timestamp
  141. });
  142. }
  143. break;
  144. }
  145. }
  146. return next(action);
  147. });