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

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