您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 7.1KB

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