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

middleware.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import { getAmplitudeIdentity } from '../analytics';
  4. import { CONFERENCE_UNIQUE_ID_SET, getConferenceOptions, getRoomName } from '../base/conference';
  5. import { LIB_WILL_INIT } from '../base/lib-jitsi-meet';
  6. import { DOMINANT_SPEAKER_CHANGED, getLocalParticipant } from '../base/participants';
  7. import { MiddlewareRegistry } from '../base/redux';
  8. import RTCStats from './RTCStats';
  9. import { canSendRtcstatsData, isRtcstatsEnabled } from './functions';
  10. import logger from './logger';
  11. /**
  12. * Middleware which intercepts lib-jitsi-meet initialization and conference join in order init the
  13. * rtcstats-client.
  14. *
  15. * @param {Store} store - The redux store.
  16. * @returns {Function}
  17. */
  18. MiddlewareRegistry.register(store => next => action => {
  19. const state = store.getState();
  20. const config = state['features/base/config'];
  21. const { analytics } = config;
  22. switch (action.type) {
  23. case LIB_WILL_INIT: {
  24. if (isRtcstatsEnabled(state)) {
  25. // RTCStats "proxies" WebRTC functions such as GUM and RTCPeerConnection by rewriting the global
  26. // window functions. Because lib-jitsi-meet uses references to those functions that are taken on
  27. // init, we need to add these proxies before it initializes, otherwise lib-jitsi-meet will use the
  28. // original non proxy versions of these functions.
  29. try {
  30. // Default poll interval is 1000ms and standard stats will be used, if not provided in the config.
  31. const pollInterval = analytics.rtcstatsPollInterval || 1000;
  32. const useLegacy = analytics.rtcstatsUseLegacy || false;
  33. // Initialize but don't connect to the rtcstats server wss, as it will start sending data for all
  34. // media calls made even before the conference started.
  35. RTCStats.init({
  36. endpoint: analytics.rtcstatsEndpoint,
  37. useLegacy,
  38. pollInterval
  39. });
  40. } catch (error) {
  41. logger.error('Failed to initialize RTCStats: ', error);
  42. }
  43. }
  44. break;
  45. }
  46. case CONFERENCE_UNIQUE_ID_SET: {
  47. if (canSendRtcstatsData(state)) {
  48. // Once the conference started connect to the rtcstats server and send data.
  49. try {
  50. RTCStats.connect();
  51. const localParticipant = getLocalParticipant(state);
  52. const options = getConferenceOptions(state);
  53. // Unique identifier for a conference session, not to be confused with meeting name
  54. // i.e. If all participants leave a meeting it will have a different value on the next join.
  55. const { conference } = action;
  56. const meetingUniqueId = conference && conference.getMeetingUniqueId();
  57. // The current implementation of rtcstats-server is configured to send data to amplitude, thus
  58. // we add identity specific information so we can correlate on the amplitude side. If amplitude is
  59. // not configured an empty object will be sent.
  60. // The current configuration of the conference is also sent as metadata to rtcstats server.
  61. // This is done in order to facilitate queries based on different conference configurations.
  62. // e.g. Find all RTCPeerConnections that connect to a specific shard or were created in a
  63. // conference with a specific version.
  64. // XXX(george): we also want to be able to correlate between rtcstats and callstats, so we're
  65. // appending the callstats user name (if it exists) to the display name.
  66. const displayName = options.statisticsId
  67. || options.statisticsDisplayName
  68. || jitsiLocalStorage.getItem('callStatsUserName');
  69. RTCStats.sendIdentityData({
  70. ...getAmplitudeIdentity(),
  71. ...options,
  72. endpointId: localParticipant?.id,
  73. confName: getRoomName(state),
  74. displayName,
  75. meetingUniqueId
  76. });
  77. } catch (error) {
  78. // If the connection failed do not impact jitsi-meet just silently fail.
  79. logger.error('RTCStats connect failed with: ', error);
  80. }
  81. }
  82. break;
  83. }
  84. case DOMINANT_SPEAKER_CHANGED: {
  85. if (canSendRtcstatsData(state)) {
  86. const { id, previousSpeakers } = action.participant;
  87. RTCStats.sendDominantSpeakerData({ dominantSpeakerEndpoint: id,
  88. previousSpeakers });
  89. }
  90. break;
  91. }
  92. }
  93. return next(action);
  94. });