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

middleware.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../app/types';
  3. import {
  4. CONFERENCE_JOINED,
  5. CONFERENCE_TIMESTAMP_CHANGED,
  6. E2E_RTT_CHANGED
  7. } from '../base/conference/actionTypes';
  8. import { DOMINANT_SPEAKER_CHANGED } from '../base/participants/actionTypes';
  9. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  10. import { TRACK_ADDED, TRACK_UPDATED } from '../base/tracks/actionTypes';
  11. import { ADD_FACE_LANDMARKS } from '../face-landmarks/actionTypes';
  12. import { FaceLandmarks } from '../face-landmarks/types';
  13. import { sendGetCustomerIdRequest } from '../jaas/functions';
  14. import RTCStats from './RTCStats';
  15. import {
  16. canSendFaceLandmarksRTCStatsData,
  17. isRTCStatsEnabled
  18. } from './functions';
  19. import logger from './logger';
  20. /**
  21. * Middleware which intercepts lib-jitsi-meet initialization and conference join in order init the
  22. * rtcstats-client.
  23. *
  24. * @param {Store} store - The redux store.
  25. * @returns {Function}
  26. */
  27. MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: AnyAction) => {
  28. const { getState } = store;
  29. const state = getState();
  30. switch (action.type) {
  31. case CONFERENCE_JOINED: {
  32. if (isRTCStatsEnabled(state)) {
  33. RTCStats.init();
  34. sendGetCustomerIdRequest(action?.conference, state)
  35. .then(customerData => {
  36. const { customerId } = customerData ?? {};
  37. customerId && RTCStats.sendIdentityData({ customerId });
  38. })
  39. .catch(error => {
  40. logger.error('Error while getting customer id:', error);
  41. });
  42. }
  43. break;
  44. }
  45. case TRACK_ADDED: {
  46. if (isRTCStatsEnabled(state)) {
  47. const jitsiTrack = action?.track?.jitsiTrack;
  48. const { ssrc, videoType } = jitsiTrack || { };
  49. // Remote tracks store their ssrc in the jitsiTrack object. Local tracks don't. See getSsrcByTrack.
  50. if (videoType && ssrc && !jitsiTrack.isLocal() && !jitsiTrack.isAudioTrack()) {
  51. RTCStats.sendVideoTypeData({
  52. ssrc,
  53. videoType
  54. });
  55. }
  56. }
  57. break;
  58. }
  59. case TRACK_UPDATED: {
  60. if (isRTCStatsEnabled(state)) {
  61. const { videoType, jitsiTrack, muted } = action?.track || { };
  62. const { ssrc, isLocal, videoType: trackVideoType, conference } = jitsiTrack || { };
  63. if (trackVideoType === 'camera' && conference && isLocal()) {
  64. RTCStats.sendFaceLandmarksData({
  65. duration: 0,
  66. faceLandmarks: muted ? 'camera-off' : 'camera-on',
  67. timestamp: Date.now()
  68. });
  69. }
  70. // if the videoType of the remote track has changed we expect to find it in track.videoType. grep for
  71. // trackVideoTypeChanged.
  72. if (videoType && ssrc && !jitsiTrack.isLocal() && !jitsiTrack.isAudioTrack()) {
  73. RTCStats.sendVideoTypeData({
  74. ssrc,
  75. videoType
  76. });
  77. }
  78. }
  79. break;
  80. }
  81. case DOMINANT_SPEAKER_CHANGED: {
  82. if (isRTCStatsEnabled(state)) {
  83. const { id, previousSpeakers, silence } = action.participant;
  84. RTCStats.sendDominantSpeakerData({
  85. dominantSpeakerEndpoint: silence ? null : id,
  86. previousSpeakers
  87. });
  88. }
  89. break;
  90. }
  91. case E2E_RTT_CHANGED: {
  92. if (isRTCStatsEnabled(state)) {
  93. const { participant, rtt } = action.e2eRtt;
  94. RTCStats.sendE2ERTTData({
  95. remoteEndpointId: participant.getId(),
  96. rtt,
  97. remoteRegion: participant.getProperty('region')
  98. });
  99. }
  100. break;
  101. }
  102. case ADD_FACE_LANDMARKS: {
  103. if (canSendFaceLandmarksRTCStatsData(state)) {
  104. const { duration, faceExpression, timestamp } = action.faceLandmarks as FaceLandmarks;
  105. const durationSeconds = Math.round(duration / 1000);
  106. RTCStats.sendFaceLandmarksData({
  107. duration: durationSeconds,
  108. faceLandmarks: faceExpression,
  109. timestamp
  110. });
  111. }
  112. break;
  113. }
  114. case CONFERENCE_TIMESTAMP_CHANGED: {
  115. if (isRTCStatsEnabled(state)) {
  116. const { conferenceTimestamp } = action;
  117. RTCStats.sendConferenceTimestamp(conferenceTimestamp);
  118. }
  119. break;
  120. }
  121. }
  122. return next(action);
  123. });