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.5KB

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