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

middleware.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {
  2. getLocalParticipant,
  3. getParticipantById,
  4. PIN_PARTICIPANT
  5. } from '../participants';
  6. import { MiddlewareRegistry } from '../redux';
  7. import {
  8. TRACK_ADDED,
  9. TRACK_REMOVED
  10. } from '../tracks';
  11. import {
  12. _addLocalTracksToConference,
  13. _handleParticipantError,
  14. _removeLocalTracksFromConference
  15. } from './functions';
  16. /**
  17. * This middleware intercepts TRACK_ADDED and TRACK_REMOVED actions to sync
  18. * conference's local tracks with local tracks in state. Also captures
  19. * PIN_PARTICIPANT action to pin participant in conference.
  20. *
  21. * @param {Store} store - Redux store.
  22. * @returns {Function}
  23. */
  24. MiddlewareRegistry.register(store => next => action => {
  25. switch (action.type) {
  26. case PIN_PARTICIPANT:
  27. pinParticipant(store, action.participant.id);
  28. break;
  29. case TRACK_ADDED:
  30. case TRACK_REMOVED: {
  31. const track = action.track;
  32. if (track && track.local) {
  33. return syncConferenceLocalTracksWithState(store, action)
  34. .then(() => next(action));
  35. }
  36. break;
  37. }
  38. }
  39. return next(action);
  40. });
  41. /**
  42. * Pins remote participant in conference, ignores local participant.
  43. *
  44. * @param {Store} store - Redux store.
  45. * @param {string|null} id - Participant id or null if no one is currently
  46. * pinned.
  47. * @returns {void}
  48. */
  49. function pinParticipant(store, id) {
  50. const state = store.getState();
  51. const participants = state['features/base/participants'];
  52. const participantById = getParticipantById(participants, id);
  53. let pin;
  54. // The following condition prevents signaling to pin local participant. The
  55. // logic is:
  56. // - If we have an ID, we check if the participant identified by that ID is
  57. // local.
  58. // - If we don't have an ID (i.e. no participant identified by an ID), we
  59. // check for local participant. If she's currently pinned, then this
  60. // action will unpin her and that's why we won't signal here too.
  61. if (participantById) {
  62. pin = !participantById.local;
  63. } else {
  64. const localParticipant = getLocalParticipant(participants);
  65. pin = !localParticipant || !localParticipant.pinned;
  66. }
  67. if (pin) {
  68. const conference = state['features/base/conference'].jitsiConference;
  69. try {
  70. conference.pinParticipant(id);
  71. } catch (err) {
  72. _handleParticipantError(err);
  73. }
  74. }
  75. }
  76. /**
  77. * Syncs local tracks from state with local tracks in JitsiConference instance.
  78. *
  79. * @param {Store} store - Redux store.
  80. * @param {Object} action - Action object.
  81. * @returns {Promise}
  82. */
  83. function syncConferenceLocalTracksWithState(store, action) {
  84. const conferenceState = store.getState()['features/base/conference'];
  85. const conference = conferenceState.jitsiConference;
  86. const leavingConference = conferenceState.leavingJitsiConference;
  87. let promise;
  88. // XXX The conference in state might be already in 'leaving' state, that's
  89. // why we should not add/remove local tracks to such conference.
  90. if (conference && conference !== leavingConference) {
  91. const track = action.track.jitsiTrack;
  92. if (action.type === TRACK_ADDED) {
  93. promise = _addLocalTracksToConference(conference, [ track ]);
  94. } else {
  95. promise = _removeLocalTracksFromConference(conference, [ track ]);
  96. }
  97. }
  98. return promise || Promise.resolve();
  99. }