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

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