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

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