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.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* global APP */
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { CONNECTION_ESTABLISHED } from '../connection';
  4. import {
  5. getLocalParticipant,
  6. getParticipantById,
  7. PIN_PARTICIPANT
  8. } from '../participants';
  9. import { MiddlewareRegistry } from '../redux';
  10. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  11. import {
  12. createConference,
  13. _setAudioOnlyVideoMuted,
  14. setLastN
  15. } from './actions';
  16. import { SET_AUDIO_ONLY, SET_LASTN } from './actionTypes';
  17. import {
  18. _addLocalTracksToConference,
  19. _handleParticipantError,
  20. _removeLocalTracksFromConference
  21. } from './functions';
  22. /**
  23. * Implements the middleware of the feature base/conference.
  24. *
  25. * @param {Store} store - Redux store.
  26. * @returns {Function}
  27. */
  28. MiddlewareRegistry.register(store => next => action => {
  29. switch (action.type) {
  30. case CONNECTION_ESTABLISHED:
  31. return _connectionEstablished(store, next, action);
  32. case PIN_PARTICIPANT:
  33. return _pinParticipant(store, next, action);
  34. case SET_AUDIO_ONLY:
  35. return _setAudioOnly(store, next, action);
  36. case SET_LASTN:
  37. return _setLastN(store, next, action);
  38. case TRACK_ADDED:
  39. case TRACK_REMOVED:
  40. return _trackAddedOrRemoved(store, next, action);
  41. }
  42. return next(action);
  43. });
  44. /**
  45. * Notifies the feature base/conference that the action CONNECTION_ESTABLISHED
  46. * is being dispatched within a specific Redux store.
  47. *
  48. * @param {Store} store - The Redux store in which the specified action is being
  49. * dispatched.
  50. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  51. * specified action to the specified store.
  52. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED which is
  53. * being dispatched in the specified store.
  54. * @private
  55. * @returns {Object} The new state that is the result of the reduction of the
  56. * specified action.
  57. */
  58. function _connectionEstablished(store, next, action) {
  59. const result = next(action);
  60. // FIXME: workaround for the web version. Currently the creation of the
  61. // conference is handled by /conference.js
  62. if (typeof APP === 'undefined') {
  63. store.dispatch(createConference());
  64. }
  65. return result;
  66. }
  67. /**
  68. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  69. * dispatched within a specific Redux store. Pins the specified remote
  70. * participant in the associated conference, ignores the local participant.
  71. *
  72. * @param {Store} store - The Redux store in which the specified action is being
  73. * dispatched.
  74. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  75. * specified action to the specified store.
  76. * @param {Action} action - The Redux action PIN_PARTICIPANT which is being
  77. * dispatched in the specified store.
  78. * @private
  79. * @returns {Object} The new state that is the result of the reduction of the
  80. * specified action.
  81. */
  82. function _pinParticipant(store, next, action) {
  83. const state = store.getState();
  84. const participants = state['features/base/participants'];
  85. const id = action.participant.id;
  86. const participantById = getParticipantById(participants, id);
  87. let pin;
  88. // The following condition prevents signaling to pin local participant. The
  89. // logic is:
  90. // - If we have an ID, we check if the participant identified by that ID is
  91. // local.
  92. // - If we don't have an ID (i.e. no participant identified by an ID), we
  93. // check for local participant. If she's currently pinned, then this
  94. // action will unpin her and that's why we won't signal here too.
  95. if (participantById) {
  96. pin = !participantById.local;
  97. } else {
  98. const localParticipant = getLocalParticipant(participants);
  99. pin = !localParticipant || !localParticipant.pinned;
  100. }
  101. if (pin) {
  102. const conference = state['features/base/conference'].conference;
  103. try {
  104. conference.pinParticipant(id);
  105. } catch (err) {
  106. _handleParticipantError(err);
  107. }
  108. }
  109. return next(action);
  110. }
  111. /**
  112. * Sets the audio-only flag for the current conference. When audio-only is set,
  113. * local video is muted and last N is set to 0 to avoid receiving remote video.
  114. *
  115. * @param {Store} store - The Redux store in which the specified action is being
  116. * dispatched.
  117. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  118. * specified action to the specified store.
  119. * @param {Action} action - The Redux action SET_AUDIO_ONLY which is being
  120. * dispatched in the specified store.
  121. * @private
  122. * @returns {Object} The new state that is the result of the reduction of the
  123. * specified action.
  124. */
  125. function _setAudioOnly(store, next, action) {
  126. const result = next(action);
  127. const { audioOnly } = action;
  128. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  129. // otherwise, and the default lastN value will be chosen automatically.
  130. store.dispatch(setLastN(audioOnly ? 0 : undefined));
  131. // Mute local video
  132. store.dispatch(_setAudioOnlyVideoMuted(audioOnly));
  133. if (typeof APP !== 'undefined') {
  134. // TODO This should be a temporary solution that lasts only until
  135. // video tracks and all ui is moved into react/redux on the web.
  136. APP.UI.emitEvent(UIEvents.TOGGLE_AUDIO_ONLY, audioOnly);
  137. }
  138. return result;
  139. }
  140. /**
  141. * Sets the last N (value) of the video channel in the conference.
  142. *
  143. * @param {Store} store - The Redux store in which the specified action is being
  144. * dispatched.
  145. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  146. * specified action to the specified store.
  147. * @param {Action} action - The Redux action SET_LASTN which is being dispatched
  148. * in the specified store.
  149. * @private
  150. * @returns {Object} The new state that is the result of the reduction of the
  151. * specified action.
  152. */
  153. function _setLastN(store, next, action) {
  154. const { conference } = store.getState()['features/base/conference'];
  155. if (conference) {
  156. try {
  157. conference.setLastN(action.lastN);
  158. } catch (err) {
  159. console.error(`Failed to set lastN: ${err}`);
  160. }
  161. }
  162. return next(action);
  163. }
  164. /**
  165. * Synchronizes local tracks from state with local tracks in JitsiConference
  166. * instance.
  167. *
  168. * @param {Store} store - Redux store.
  169. * @param {Object} action - Action object.
  170. * @private
  171. * @returns {Promise}
  172. */
  173. function _syncConferenceLocalTracksWithState(store, action) {
  174. const state = store.getState()['features/base/conference'];
  175. const conference = state.conference;
  176. let promise;
  177. // XXX The conference may already be in the process of being left, that's
  178. // why we should not add/remove local tracks to such conference.
  179. if (conference && conference !== state.leaving) {
  180. const track = action.track.jitsiTrack;
  181. if (action.type === TRACK_ADDED) {
  182. promise = _addLocalTracksToConference(conference, [ track ]);
  183. } else {
  184. promise = _removeLocalTracksFromConference(conference, [ track ]);
  185. }
  186. }
  187. return promise || Promise.resolve();
  188. }
  189. /**
  190. * Notifies the feature base/conference that the action TRACK_ADDED
  191. * or TRACK_REMOVED is being dispatched within a specific Redux store.
  192. *
  193. * @param {Store} store - The Redux store in which the specified action is being
  194. * dispatched.
  195. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  196. * specified action to the specified store.
  197. * @param {Action} action - The Redux action TRACK_ADDED or TRACK_REMOVED which
  198. * is being dispatched in the specified store.
  199. * @private
  200. * @returns {Object} The new state that is the result of the reduction of the
  201. * specified action.
  202. */
  203. function _trackAddedOrRemoved(store, next, action) {
  204. const track = action.track;
  205. if (track && track.local) {
  206. return (
  207. _syncConferenceLocalTracksWithState(store, action)
  208. .then(() => next(action)));
  209. }
  210. return next(action);
  211. }