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

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