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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // @flow
  2. import {
  3. ACTION_PINNED,
  4. ACTION_UNPINNED,
  5. createAudioOnlyChangedEvent,
  6. createPinnedEvent,
  7. sendAnalytics
  8. } from '../../analytics';
  9. import { CONNECTION_ESTABLISHED } from '../connection';
  10. import { setVideoMuted, VIDEO_MUTISM_AUTHORITY } from '../media';
  11. import {
  12. getLocalParticipant,
  13. getParticipantById,
  14. getPinnedParticipant,
  15. PIN_PARTICIPANT
  16. } from '../participants';
  17. import { MiddlewareRegistry } from '../redux';
  18. import UIEvents from '../../../../service/UI/UIEvents';
  19. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  20. import {
  21. conferenceLeft,
  22. createConference,
  23. setLastN,
  24. toggleAudioOnly
  25. } from './actions';
  26. import {
  27. CONFERENCE_FAILED,
  28. CONFERENCE_JOINED,
  29. DATA_CHANNEL_OPENED,
  30. SET_AUDIO_ONLY,
  31. SET_LASTN,
  32. SET_RECEIVE_VIDEO_QUALITY,
  33. SET_ROOM
  34. } from './actionTypes';
  35. import {
  36. _addLocalTracksToConference,
  37. forEachConference,
  38. _handleParticipantError,
  39. _removeLocalTracksFromConference
  40. } from './functions';
  41. const logger = require('jitsi-meet-logger').getLogger(__filename);
  42. declare var APP: Object;
  43. /**
  44. * Implements the middleware of the feature base/conference.
  45. *
  46. * @param {Store} store - The redux store.
  47. * @returns {Function}
  48. */
  49. MiddlewareRegistry.register(store => next => action => {
  50. switch (action.type) {
  51. case CONFERENCE_FAILED:
  52. return _conferenceFailed(store, next, action);
  53. case CONFERENCE_JOINED:
  54. return _conferenceJoined(store, next, action);
  55. case CONNECTION_ESTABLISHED:
  56. return _connectionEstablished(store, next, action);
  57. case DATA_CHANNEL_OPENED:
  58. return _syncReceiveVideoQuality(store, next, action);
  59. case PIN_PARTICIPANT:
  60. return _pinParticipant(store, next, action);
  61. case SET_AUDIO_ONLY:
  62. return _setAudioOnly(store, next, action);
  63. case SET_LASTN:
  64. return _setLastN(store, next, action);
  65. case SET_RECEIVE_VIDEO_QUALITY:
  66. return _setReceiveVideoQuality(store, next, action);
  67. case SET_ROOM:
  68. return _setRoom(store, next, action);
  69. case TRACK_ADDED:
  70. case TRACK_REMOVED:
  71. return _trackAddedOrRemoved(store, next, action);
  72. }
  73. return next(action);
  74. });
  75. /**
  76. * Makes sure to leave a failed conference in order to release any allocated
  77. * resources like peer connections, emit participant left events, etc.
  78. *
  79. * @param {Store} store - The redux store in which the specified {@code action}
  80. * is being dispatched.
  81. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  82. * specified {@code action} to the specified {@code store}.
  83. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  84. * being dispatched in the specified {@code store}.
  85. * @private
  86. * @returns {Object} The value returned by {@code next(action)}.
  87. */
  88. function _conferenceFailed(store, next, action) {
  89. const result = next(action);
  90. // XXX After next(action), it is clear whether the error is recoverable.
  91. const { conference, error } = action;
  92. !error.recoverable
  93. && conference
  94. && conference.leave().catch(reason => {
  95. // Even though we don't care too much about the failure, it may be
  96. // good to know that it happen, so log it (on the info level).
  97. logger.info('JitsiConference.leave() rejected with:', reason);
  98. });
  99. return result;
  100. }
  101. /**
  102. * Does extra sync up on properties that may need to be updated after the
  103. * conference was joined.
  104. *
  105. * @param {Store} store - The redux store in which the specified {@code action}
  106. * is being dispatched.
  107. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  108. * specified {@code action} to the specified {@code store}.
  109. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  110. * being dispatched in the specified {@code store}.
  111. * @private
  112. * @returns {Object} The value returned by {@code next(action)}.
  113. */
  114. function _conferenceJoined({ dispatch, getState }, next, action) {
  115. const result = next(action);
  116. const { audioOnly, conference } = 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. audioOnly && conference.getLastN() !== 0 && dispatch(setLastN(0));
  121. return result;
  122. }
  123. /**
  124. * Notifies the feature base/conference that the action
  125. * {@code CONNECTION_ESTABLISHED} is being dispatched within a specific redux
  126. * store.
  127. *
  128. * @param {Store} store - The redux store in which the specified {@code action}
  129. * is being dispatched.
  130. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  131. * specified {@code action} to the specified {@code store}.
  132. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  133. * which is being dispatched in the specified {@code store}.
  134. * @private
  135. * @returns {Object} The value returned by {@code next(action)}.
  136. */
  137. function _connectionEstablished({ dispatch }, next, action) {
  138. const result = next(action);
  139. // FIXME: Workaround for the web version. Currently, the creation of the
  140. // conference is handled by /conference.js.
  141. typeof APP === 'undefined' && dispatch(createConference());
  142. return result;
  143. }
  144. /**
  145. * Notifies the feature base/conference that the action {@code PIN_PARTICIPANT}
  146. * is being dispatched within a specific redux store. Pins the specified remote
  147. * participant in the associated conference, ignores the local participant.
  148. *
  149. * @param {Store} store - The redux store in which the specified {@code action}
  150. * is being dispatched.
  151. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  152. * specified {@code action} to the specified {@code store}.
  153. * @param {Action} action - The redux action {@code PIN_PARTICIPANT} which is
  154. * being dispatched in the specified {@code store}.
  155. * @private
  156. * @returns {Object} The value returned by {@code next(action)}.
  157. */
  158. function _pinParticipant({ getState }, next, action) {
  159. const state = getState();
  160. const { conference } = state['features/base/conference'];
  161. if (!conference) {
  162. return next(action);
  163. }
  164. const participants = state['features/base/participants'];
  165. const id = action.participant.id;
  166. const participantById = getParticipantById(participants, id);
  167. if (typeof APP !== 'undefined') {
  168. const pinnedParticipant = getPinnedParticipant(participants);
  169. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  170. const local
  171. = (participantById && participantById.local)
  172. || (!id && pinnedParticipant && pinnedParticipant.local);
  173. sendAnalytics(createPinnedEvent(
  174. actionName,
  175. local ? 'local' : id,
  176. {
  177. local,
  178. 'participant_count': conference.getParticipantCount()
  179. }));
  180. }
  181. // The following condition prevents signaling to pin local participant and
  182. // shared videos. The logic is:
  183. // - If we have an ID, we check if the participant identified by that ID is
  184. // local or a bot/fake participant (such as with shared video).
  185. // - If we don't have an ID (i.e. no participant identified by an ID), we
  186. // check for local participant. If she's currently pinned, then this
  187. // action will unpin her and that's why we won't signal here too.
  188. let pin;
  189. if (participantById) {
  190. pin = !participantById.local && !participantById.isBot;
  191. } else {
  192. const localParticipant = getLocalParticipant(participants);
  193. pin = !localParticipant || !localParticipant.pinned;
  194. }
  195. if (pin) {
  196. try {
  197. conference.pinParticipant(id);
  198. } catch (err) {
  199. _handleParticipantError(err);
  200. }
  201. }
  202. return next(action);
  203. }
  204. /**
  205. * Sets the audio-only flag for the current conference. When audio-only is set,
  206. * local video is muted and last N is set to 0 to avoid receiving remote video.
  207. *
  208. * @param {Store} store - The redux store in which the specified {@code action}
  209. * is being dispatched.
  210. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  211. * specified {@code action} to the specified {@code store}.
  212. * @param {Action} action - The redux action {@code SET_AUDIO_ONLY} which is
  213. * being dispatched in the specified {@code store}.
  214. * @private
  215. * @returns {Object} The value returned by {@code next(action)}.
  216. */
  217. function _setAudioOnly({ dispatch, getState }, next, action) {
  218. const { audioOnly: oldValue } = getState()['features/base/conference'];
  219. const result = next(action);
  220. const { audioOnly: newValue } = getState()['features/base/conference'];
  221. // Send analytics. We could've done it in the action creator setAudioOnly.
  222. // I don't know why it has to happen as early as possible but the analytics
  223. // were originally sent before the SET_AUDIO_ONLY action was even dispatched
  224. // in the redux store so I'm now sending the analytics as early as possible.
  225. if (oldValue !== newValue) {
  226. sendAnalytics(createAudioOnlyChangedEvent(newValue));
  227. logger.log(`Audio-only ${newValue ? 'enabled' : 'disabled'}`);
  228. }
  229. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  230. // otherwise, and the default lastN value will be chosen automatically.
  231. dispatch(setLastN(newValue ? 0 : undefined));
  232. // Mute/unmute the local video.
  233. dispatch(
  234. setVideoMuted(
  235. newValue,
  236. VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY,
  237. /* ensureTrack */ true));
  238. if (typeof APP !== 'undefined') {
  239. // TODO This should be a temporary solution that lasts only until video
  240. // tracks and all ui is moved into react/redux on the web.
  241. APP.UI.emitEvent(UIEvents.TOGGLE_AUDIO_ONLY, newValue);
  242. }
  243. return result;
  244. }
  245. /**
  246. * Sets the last N (value) of the video channel in the conference.
  247. *
  248. * @param {Store} store - The redux store in which the specified {@code action}
  249. * is being dispatched.
  250. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  251. * specified {@code action} to the specified {@code store}.
  252. * @param {Action} action - The redux action {@code SET_LASTN} which is being
  253. * dispatched in the specified {@code store}.
  254. * @private
  255. * @returns {Object} The value returned by {@code next(action)}.
  256. */
  257. function _setLastN({ getState }, next, action) {
  258. const { conference } = getState()['features/base/conference'];
  259. if (conference) {
  260. try {
  261. conference.setLastN(action.lastN);
  262. } catch (err) {
  263. console.error(`Failed to set lastN: ${err}`);
  264. }
  265. }
  266. return next(action);
  267. }
  268. /**
  269. * Sets the maximum receive video quality and will turn off audio only mode if
  270. * enabled.
  271. *
  272. * @param {Store} store - The redux store in which the specified {@code action}
  273. * is being dispatched.
  274. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  275. * specified {@code action} to the specified {@code store}.
  276. * @param {Action} action - The redux action {@code SET_RECEIVE_VIDEO_QUALITY}
  277. * which is being dispatched in the specified {@code store}.
  278. * @private
  279. * @returns {Object} The value returned by {@code next(action)}.
  280. */
  281. function _setReceiveVideoQuality({ dispatch, getState }, next, action) {
  282. const { audioOnly, conference } = getState()['features/base/conference'];
  283. if (conference) {
  284. conference.setReceiverVideoConstraint(action.receiveVideoQuality);
  285. audioOnly && dispatch(toggleAudioOnly());
  286. }
  287. return next(action);
  288. }
  289. /**
  290. * Notifies the feature {@code base/conference} that the redix action
  291. * {@link SET_ROOM} is being dispatched within a specific redux store.
  292. *
  293. * @param {Store} store - The redux store in which the specified {@code action}
  294. * is being dispatched.
  295. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  296. * specified {@code action} to the specified {@code store}.
  297. * @param {Action} action - The redux action {@code SET_ROOM} which is being
  298. * dispatched in the specified {@code store}.
  299. * @private
  300. * @returns {Object} The value returned by {@code next(action)}.
  301. */
  302. function _setRoom({ dispatch, getState }, next, action) {
  303. const result = next(action);
  304. // By the time SET_ROOM is dispatched, base/connection's locationURL and
  305. // base/conference's leaving should be the only conference-related sources
  306. // of truth.
  307. const state = getState();
  308. const { leaving } = state['features/base/conference'];
  309. const { locationURL } = state['features/base/connection'];
  310. const dispatchConferenceLeft = new Set();
  311. // Figure out which of the JitsiConferences referenced by base/conference
  312. // have not dispatched or are not likely to dispatch CONFERENCE_FAILED and
  313. // CONFERENCE_LEFT.
  314. forEachConference(state, (conference, url) => {
  315. if (conference !== leaving && url && url !== locationURL) {
  316. dispatchConferenceLeft.add(conference);
  317. }
  318. return true; // All JitsiConference instances are to be examined.
  319. });
  320. // Dispatch CONFERENCE_LEFT for the JitsiConferences referenced by
  321. // base/conference which have not dispatched or are not likely to dispatch
  322. // CONFERENCE_FAILED or CONFERENCE_LEFT.
  323. for (const conference of dispatchConferenceLeft) {
  324. dispatch(conferenceLeft(conference));
  325. }
  326. return result;
  327. }
  328. /**
  329. * Synchronizes local tracks from state with local tracks in JitsiConference
  330. * instance.
  331. *
  332. * @param {Store} store - The redux store.
  333. * @param {Object} action - Action object.
  334. * @private
  335. * @returns {Promise}
  336. */
  337. function _syncConferenceLocalTracksWithState({ getState }, action) {
  338. const state = getState()['features/base/conference'];
  339. const { conference } = state;
  340. let promise;
  341. // XXX The conference may already be in the process of being left, that's
  342. // why we should not add/remove local tracks to such conference.
  343. if (conference && conference !== state.leaving) {
  344. const track = action.track.jitsiTrack;
  345. if (action.type === TRACK_ADDED) {
  346. promise = _addLocalTracksToConference(conference, [ track ]);
  347. } else {
  348. promise = _removeLocalTracksFromConference(conference, [ track ]);
  349. }
  350. }
  351. return promise || Promise.resolve();
  352. }
  353. /**
  354. * Sets the maximum receive video quality.
  355. *
  356. * @param {Store} store - The redux store in which the specified {@code action}
  357. * is being dispatched.
  358. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  359. * specified {@code action} to the specified {@code store}.
  360. * @param {Action} action - The redux action {@code DATA_CHANNEL_STATUS_CHANGED}
  361. * which is being dispatched in the specified {@code store}.
  362. * @private
  363. * @returns {Object} The value returned by {@code next(action)}.
  364. */
  365. function _syncReceiveVideoQuality({ getState }, next, action) {
  366. const state = getState()['features/base/conference'];
  367. state.conference.setReceiverVideoConstraint(state.receiveVideoQuality);
  368. return next(action);
  369. }
  370. /**
  371. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  372. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  373. *
  374. * @param {Store} store - The redux store in which the specified {@code action}
  375. * is being dispatched.
  376. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  377. * specified {@code action} to the specified {@code store}.
  378. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  379. * {@code TRACK_REMOVED} which is being dispatched in the specified
  380. * {@code store}.
  381. * @private
  382. * @returns {Object} The value returned by {@code next(action)}.
  383. */
  384. function _trackAddedOrRemoved(store, next, action) {
  385. const track = action.track;
  386. if (track && track.local) {
  387. return (
  388. _syncConferenceLocalTracksWithState(store, action)
  389. .then(() => next(action)));
  390. }
  391. return next(action);
  392. }