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

middleware.js 4.3KB

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