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 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // @flow
  2. import { getAmplitudeIdentity } from '../analytics';
  3. import { CONFERENCE_UNIQUE_ID_SET, getRoomName } from '../base/conference';
  4. import { LIB_WILL_INIT } from '../base/lib-jitsi-meet';
  5. import { getLocalParticipant } from '../base/participants';
  6. import { MiddlewareRegistry } from '../base/redux';
  7. import RTCStats from './RTCStats';
  8. import { 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 (isRtcstatsEnabled(state) && RTCStats.isInitialized()) {
  47. // Once the conference started connect to the rtcstats server and send data.
  48. try {
  49. RTCStats.connect();
  50. const localParticipant = getLocalParticipant(state);
  51. // Unique identifier for a conference session, not to be confused with meeting name
  52. // i.e. If all participants leave a meeting it will have a different value on the next join.
  53. const { conference } = action;
  54. const meetingUniqueId = conference && conference.getMeetingUniqueId();
  55. // The current implementation of rtcstats-server is configured to send data to amplitude, thus
  56. // we add identity specific information so we can correlate on the amplitude side. If amplitude is
  57. // not configured an empty object will be sent.
  58. // The current configuration of the conference is also sent as metadata to rtcstats server.
  59. // This is done in order to facilitate queries based on different conference configurations.
  60. // e.g. Find all RTCPeerConnections that connect to a specific shard or were created in a
  61. // conference with a specific version.
  62. RTCStats.sendIdentityData({
  63. ...getAmplitudeIdentity(),
  64. ...config,
  65. confName: getRoomName(state),
  66. displayName: localParticipant?.name,
  67. meetingUniqueId
  68. });
  69. } catch (error) {
  70. // If the connection failed do not impact jitsi-meet just silently fail.
  71. logger.error('RTCStats connect failed with: ', error);
  72. }
  73. }
  74. break;
  75. }
  76. }
  77. return next(action);
  78. });