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.ts 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../app/types';
  3. import {
  4. CONFERENCE_JOINED,
  5. CONFERENCE_TIMESTAMP_CHANGED,
  6. CONFERENCE_UNIQUE_ID_SET,
  7. CONFERENCE_WILL_LEAVE,
  8. E2E_RTT_CHANGED
  9. } from '../base/conference/actionTypes';
  10. import { LIB_WILL_INIT } from '../base/lib-jitsi-meet/actionTypes';
  11. import { DOMINANT_SPEAKER_CHANGED } from '../base/participants/actionTypes';
  12. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  13. import { TRACK_ADDED, TRACK_UPDATED } from '../base/tracks/actionTypes';
  14. import { getCurrentRoomId, isInBreakoutRoom } from '../breakout-rooms/functions';
  15. import { extractFqnFromPath } from '../dynamic-branding/functions.any';
  16. import { ADD_FACE_LANDMARKS } from '../face-landmarks/actionTypes';
  17. import { FaceLandmarks } from '../face-landmarks/types';
  18. import RTCStats from './RTCStats';
  19. import {
  20. canSendFaceLandmarksRtcstatsData,
  21. canSendRtcstatsData,
  22. connectAndSendIdentity,
  23. isRtcstatsEnabled
  24. } from './functions';
  25. import logger from './logger';
  26. /**
  27. * Middleware which intercepts lib-jitsi-meet initialization and conference join in order init the
  28. * rtcstats-client.
  29. *
  30. * @param {Store} store - The redux store.
  31. * @returns {Function}
  32. */
  33. MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: AnyAction) => {
  34. const { getState } = store;
  35. const state = getState();
  36. const config = state['features/base/config'];
  37. const { analytics } = config;
  38. switch (action.type) {
  39. case LIB_WILL_INIT: {
  40. if (isRtcstatsEnabled(state) && !RTCStats.isInitialized()) {
  41. // RTCStats "proxies" WebRTC functions such as GUM and RTCPeerConnection by rewriting the global
  42. // window functions. Because lib-jitsi-meet uses references to those functions that are taken on
  43. // init, we need to add these proxies before it initializes, otherwise lib-jitsi-meet will use the
  44. // original non proxy versions of these functions.
  45. try {
  46. // Default poll interval is 10000ms and standard stats will be used, if not provided in the config.
  47. const pollInterval = analytics?.rtcstatsPollInterval || 10000;
  48. const useLegacy = analytics?.rtcstatsUseLegacy || false;
  49. const sendSdp = analytics?.rtcstatsSendSdp || false;
  50. // Initialize but don't connect to the rtcstats server wss, as it will start sending data for all
  51. // media calls made even before the conference started.
  52. RTCStats.init({
  53. endpoint: analytics?.rtcstatsEndpoint,
  54. meetingFqn: extractFqnFromPath(state),
  55. useLegacy,
  56. pollInterval,
  57. sendSdp
  58. });
  59. } catch (error) {
  60. logger.error('Failed to initialize RTCStats: ', error);
  61. }
  62. }
  63. break;
  64. }
  65. // Used for connecting to rtcstats server when joining a breakout room.
  66. // Breakout rooms do not have a meetingUniqueId.
  67. case CONFERENCE_JOINED: {
  68. if (isInBreakoutRoom(getState())) {
  69. connectAndSendIdentity(
  70. store,
  71. {
  72. isBreakoutRoom: true,
  73. roomId: getCurrentRoomId(getState())
  74. }
  75. );
  76. }
  77. break;
  78. }
  79. // Used for connecting to rtcstats server when joining the main room.
  80. // Using this event to be sure the meetingUniqueId can be retrieved.
  81. case CONFERENCE_UNIQUE_ID_SET: {
  82. if (!isInBreakoutRoom(getState())) {
  83. // Unique identifier for a conference session, not to be confused with meeting name
  84. // i.e. If all participants leave a meeting it will have a different value on the next join.
  85. const { conference } = action;
  86. const meetingUniqueId = conference?.getMeetingUniqueId();
  87. connectAndSendIdentity(
  88. store,
  89. {
  90. isBreakoutRoom: false,
  91. meetingUniqueId
  92. }
  93. );
  94. }
  95. break;
  96. }
  97. case TRACK_ADDED: {
  98. if (canSendRtcstatsData(state)) {
  99. const jitsiTrack = action?.track?.jitsiTrack;
  100. const { ssrc, videoType } = jitsiTrack || { };
  101. // Remote tracks store their ssrc in the jitsiTrack object. Local tracks don't. See getSsrcByTrack.
  102. if (videoType && ssrc && !jitsiTrack.isLocal() && !jitsiTrack.isAudioTrack()) {
  103. RTCStats.sendVideoTypeData({
  104. ssrc,
  105. videoType
  106. });
  107. }
  108. }
  109. break;
  110. }
  111. case TRACK_UPDATED: {
  112. if (canSendRtcstatsData(state)) {
  113. const { videoType, jitsiTrack, muted } = action?.track || { };
  114. const { ssrc, isLocal, videoType: trackVideoType, conference } = jitsiTrack || { };
  115. if (trackVideoType === 'camera' && conference && isLocal()) {
  116. RTCStats.sendFaceLandmarksData({
  117. duration: 0,
  118. faceLandmarks: muted ? 'camera-off' : 'camera-on',
  119. timestamp: Date.now()
  120. });
  121. }
  122. // if the videoType of the remote track has changed we expect to find it in track.videoType. grep for
  123. // trackVideoTypeChanged.
  124. if (videoType && ssrc && !jitsiTrack.isLocal() && !jitsiTrack.isAudioTrack()) {
  125. RTCStats.sendVideoTypeData({
  126. ssrc,
  127. videoType
  128. });
  129. }
  130. }
  131. break;
  132. }
  133. case DOMINANT_SPEAKER_CHANGED: {
  134. if (canSendRtcstatsData(state)) {
  135. const { id, previousSpeakers, silence } = action.participant;
  136. RTCStats.sendDominantSpeakerData({
  137. dominantSpeakerEndpoint: silence ? null : id,
  138. previousSpeakers
  139. });
  140. }
  141. break;
  142. }
  143. case E2E_RTT_CHANGED: {
  144. if (canSendRtcstatsData(state)) {
  145. const { participant, rtt } = action.e2eRtt;
  146. RTCStats.sendE2eRttData({
  147. remoteEndpointId: participant.getId(),
  148. rtt,
  149. remoteRegion: participant.getProperty('region')
  150. });
  151. }
  152. break;
  153. }
  154. case ADD_FACE_LANDMARKS: {
  155. if (canSendFaceLandmarksRtcstatsData(state)) {
  156. const { duration, faceExpression, timestamp } = action.faceLandmarks as FaceLandmarks;
  157. const durationSeconds = Math.round(duration / 1000);
  158. RTCStats.sendFaceLandmarksData({
  159. duration: durationSeconds,
  160. faceLandmarks: faceExpression,
  161. timestamp
  162. });
  163. }
  164. break;
  165. }
  166. case CONFERENCE_TIMESTAMP_CHANGED: {
  167. if (canSendRtcstatsData(state)) {
  168. const { conferenceTimestamp } = action;
  169. RTCStats.sendConferenceTimestamp(conferenceTimestamp);
  170. }
  171. break;
  172. }
  173. case CONFERENCE_WILL_LEAVE: {
  174. if (canSendRtcstatsData(state)) {
  175. RTCStats.close();
  176. }
  177. break;
  178. }
  179. }
  180. return next(action);
  181. });