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

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