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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { PARTICIPANT_ID_CHANGED } from '../participants';
  2. import { ReducerRegistry, set } from '../redux';
  3. import {
  4. SET_NO_SRC_DATA_NOTIFICATION_UID,
  5. TRACK_ADDED,
  6. TRACK_CREATE_CANCELED,
  7. TRACK_CREATE_ERROR,
  8. TRACK_NO_DATA_FROM_SOURCE,
  9. TRACK_REMOVED,
  10. TRACK_UPDATE_LAST_VIDEO_MEDIA_EVENT,
  11. TRACK_UPDATED,
  12. TRACK_WILL_CREATE
  13. } from './actionTypes';
  14. /**
  15. * @typedef {Object} Track
  16. * @property {(JitsiLocalTrack|JitsiRemoteTrack)} [jitsiTrack] - The associated
  17. * {@code JitsiTrack} instance. Optional for local tracks if those are still
  18. * being created (i.e. {@code getUserMedia} is still in progress).
  19. * @property {Promise} [gumProcess] - If a local track is still being created,
  20. * it will have no {@code JitsiTrack}, but a {@code gumProcess} set to a
  21. * {@code Promise} with and extra {@code cancel()}.
  22. * @property {boolean} local=false - If the track is local.
  23. * @property {MEDIA_TYPE} mediaType=false - The media type of the track.
  24. * @property {boolean} mirror=false - The indicator which determines whether the
  25. * display/rendering of the track should be mirrored. It only makes sense in the
  26. * context of video (at least at the time of this writing).
  27. * @property {boolean} muted=false - If the track is muted.
  28. * @property {(string|undefined)} participantId - The ID of the participant whom
  29. * the track belongs to.
  30. * @property {boolean} videoStarted=false - If the video track has already
  31. * started to play.
  32. * @property {(VIDEO_TYPE|undefined)} videoType - The type of video track if
  33. * any.
  34. */
  35. /**
  36. * Reducer function for a single track.
  37. *
  38. * @param {Track|undefined} state - Track to be modified.
  39. * @param {Object} action - Action object.
  40. * @param {string} action.type - Type of action.
  41. * @param {string} action.name - Name of last media event.
  42. * @param {string} action.newValue - New participant ID value (in this
  43. * particular case).
  44. * @param {string} action.oldValue - Old participant ID value (in this
  45. * particular case).
  46. * @param {Track} action.track - Information about track to be changed.
  47. * @param {Participant} action.participant - Information about participant.
  48. * @returns {Track|undefined}
  49. */
  50. function track(state, action) {
  51. switch (action.type) {
  52. case PARTICIPANT_ID_CHANGED:
  53. if (state.participantId === action.oldValue) {
  54. return {
  55. ...state,
  56. participantId: action.newValue
  57. };
  58. }
  59. break;
  60. case TRACK_UPDATED: {
  61. const t = action.track;
  62. if (state.jitsiTrack === t.jitsiTrack) {
  63. // Make sure that there's an actual update in order to reduce the
  64. // risk of unnecessary React Component renders.
  65. for (const p in t) {
  66. if (state[p] !== t[p]) {
  67. // There's an actual update.
  68. return {
  69. ...state,
  70. ...t
  71. };
  72. }
  73. }
  74. }
  75. break;
  76. }
  77. case TRACK_UPDATE_LAST_VIDEO_MEDIA_EVENT: {
  78. const t = action.track;
  79. if (state.jitsiTrack === t) {
  80. if (state.lastMediaEvent !== action.name) {
  81. return {
  82. ...state,
  83. lastMediaEvent: action.name
  84. };
  85. }
  86. }
  87. break;
  88. }
  89. case TRACK_NO_DATA_FROM_SOURCE: {
  90. const t = action.track;
  91. if (state.jitsiTrack === t.jitsiTrack) {
  92. const isReceivingData = t.jitsiTrack.isReceivingData();
  93. if (state.isReceivingData !== isReceivingData) {
  94. return {
  95. ...state,
  96. isReceivingData
  97. };
  98. }
  99. }
  100. break;
  101. }
  102. }
  103. return state;
  104. }
  105. /**
  106. * Listen for actions that mutate (e.g. add, remove) local and remote tracks.
  107. */
  108. ReducerRegistry.register('features/base/tracks', (state = [], action) => {
  109. switch (action.type) {
  110. case PARTICIPANT_ID_CHANGED:
  111. case TRACK_NO_DATA_FROM_SOURCE:
  112. case TRACK_UPDATE_LAST_VIDEO_MEDIA_EVENT:
  113. case TRACK_UPDATED:
  114. return state.map(t => track(t, action));
  115. case TRACK_ADDED: {
  116. let withoutTrackStub = state;
  117. if (action.track.local) {
  118. withoutTrackStub
  119. = state.filter(
  120. t => !t.local || t.mediaType !== action.track.mediaType);
  121. }
  122. return [ ...withoutTrackStub, action.track ];
  123. }
  124. case TRACK_CREATE_CANCELED:
  125. case TRACK_CREATE_ERROR: {
  126. return state.filter(t => !t.local || t.mediaType !== action.trackType);
  127. }
  128. case TRACK_REMOVED:
  129. return state.filter(t => t.jitsiTrack !== action.track.jitsiTrack);
  130. case TRACK_WILL_CREATE:
  131. return [ ...state, action.track ];
  132. default:
  133. return state;
  134. }
  135. });
  136. /**
  137. * Listen for actions that mutate the no-src-data state, like the current notification id
  138. */
  139. ReducerRegistry.register('features/base/no-src-data', (state = {}, action) => {
  140. switch (action.type) {
  141. case SET_NO_SRC_DATA_NOTIFICATION_UID:
  142. return set(state, 'noSrcDataNotificationUid', action.uid);
  143. default:
  144. return state;
  145. }
  146. });