Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.ts 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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)) {
  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.maybeInit({
  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. } else {
  63. RTCStats.reset();
  64. }
  65. break;
  66. }
  67. // Used for connecting to rtcstats server when joining a breakout room.
  68. // Breakout rooms do not have a meetingUniqueId.
  69. case CONFERENCE_JOINED: {
  70. if (isInBreakoutRoom(getState())) {
  71. connectAndSendIdentity(
  72. store,
  73. {
  74. isBreakoutRoom: true,
  75. roomId: getCurrentRoomId(getState())
  76. }
  77. );
  78. }
  79. break;
  80. }
  81. // Used for connecting to rtcstats server when joining the main room.
  82. // Using this event to be sure the meetingUniqueId can be retrieved.
  83. case CONFERENCE_UNIQUE_ID_SET: {
  84. if (!isInBreakoutRoom(getState())) {
  85. // Unique identifier for a conference session, not to be confused with meeting name
  86. // i.e. If all participants leave a meeting it will have a different value on the next join.
  87. const { conference } = action;
  88. const meetingUniqueId = conference?.getMeetingUniqueId();
  89. connectAndSendIdentity(
  90. store,
  91. {
  92. isBreakoutRoom: false,
  93. meetingUniqueId
  94. }
  95. );
  96. }
  97. break;
  98. }
  99. case TRACK_ADDED: {
  100. if (canSendRtcstatsData(state)) {
  101. const jitsiTrack = action?.track?.jitsiTrack;
  102. const { ssrc, videoType } = jitsiTrack || { };
  103. // Remote tracks store their ssrc in the jitsiTrack object. Local tracks don't. See getSsrcByTrack.
  104. if (videoType && ssrc && !jitsiTrack.isLocal() && !jitsiTrack.isAudioTrack()) {
  105. RTCStats.sendVideoTypeData({
  106. ssrc,
  107. videoType
  108. });
  109. }
  110. }
  111. break;
  112. }
  113. case TRACK_UPDATED: {
  114. if (canSendRtcstatsData(state)) {
  115. const { videoType, jitsiTrack, muted } = action?.track || { };
  116. const { ssrc, isLocal, videoType: trackVideoType, conference } = jitsiTrack || { };
  117. if (trackVideoType === 'camera' && conference && isLocal()) {
  118. RTCStats.sendFaceLandmarksData({
  119. duration: 0,
  120. faceLandmarks: muted ? 'camera-off' : 'camera-on',
  121. timestamp: Date.now()
  122. });
  123. }
  124. // if the videoType of the remote track has changed we expect to find it in track.videoType. grep for
  125. // trackVideoTypeChanged.
  126. if (videoType && ssrc && !jitsiTrack.isLocal() && !jitsiTrack.isAudioTrack()) {
  127. RTCStats.sendVideoTypeData({
  128. ssrc,
  129. videoType
  130. });
  131. }
  132. }
  133. break;
  134. }
  135. case DOMINANT_SPEAKER_CHANGED: {
  136. if (canSendRtcstatsData(state)) {
  137. const { id, previousSpeakers, silence } = action.participant;
  138. RTCStats.sendDominantSpeakerData({
  139. dominantSpeakerEndpoint: silence ? null : id,
  140. previousSpeakers
  141. });
  142. }
  143. break;
  144. }
  145. case E2E_RTT_CHANGED: {
  146. if (canSendRtcstatsData(state)) {
  147. const { participant, rtt } = action.e2eRtt;
  148. RTCStats.sendE2eRttData({
  149. remoteEndpointId: participant.getId(),
  150. rtt,
  151. remoteRegion: participant.getProperty('region')
  152. });
  153. }
  154. break;
  155. }
  156. case ADD_FACE_LANDMARKS: {
  157. if (canSendFaceLandmarksRtcstatsData(state)) {
  158. const { duration, faceExpression, timestamp } = action.faceLandmarks as FaceLandmarks;
  159. const durationSeconds = Math.round(duration / 1000);
  160. RTCStats.sendFaceLandmarksData({
  161. duration: durationSeconds,
  162. faceLandmarks: faceExpression,
  163. timestamp
  164. });
  165. }
  166. break;
  167. }
  168. case CONFERENCE_TIMESTAMP_CHANGED: {
  169. if (canSendRtcstatsData(state)) {
  170. const { conferenceTimestamp } = action;
  171. RTCStats.sendConferenceTimestamp(conferenceTimestamp);
  172. }
  173. break;
  174. }
  175. case CONFERENCE_WILL_LEAVE: {
  176. if (canSendRtcstatsData(state)) {
  177. RTCStats.close();
  178. }
  179. break;
  180. }
  181. }
  182. return next(action);
  183. });