Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 8.9KB

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