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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // @flow
  2. import { openDisplayNamePrompt } from '../../display-name';
  3. import {
  4. ACTION_PINNED,
  5. ACTION_UNPINNED,
  6. createOfferAnswerFailedEvent,
  7. createPinnedEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../connection';
  11. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  12. import {
  13. getLocalParticipant,
  14. getParticipantById,
  15. getPinnedParticipant,
  16. PARTICIPANT_UPDATED,
  17. PIN_PARTICIPANT
  18. } from '../participants';
  19. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  20. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  21. import {
  22. conferenceFailed,
  23. conferenceWillLeave,
  24. createConference,
  25. setSubject
  26. } from './actions';
  27. import {
  28. CONFERENCE_FAILED,
  29. CONFERENCE_JOINED,
  30. CONFERENCE_SUBJECT_CHANGED,
  31. CONFERENCE_WILL_LEAVE,
  32. DATA_CHANNEL_OPENED,
  33. SEND_TONES,
  34. SET_PENDING_SUBJECT_CHANGE,
  35. SET_ROOM
  36. } from './actionTypes';
  37. import {
  38. _addLocalTracksToConference,
  39. _removeLocalTracksFromConference,
  40. forEachConference,
  41. getCurrentConference
  42. } from './functions';
  43. import logger from './logger';
  44. import { MEDIA_TYPE } from '../media';
  45. declare var APP: Object;
  46. /**
  47. * Handler for before unload event.
  48. */
  49. let beforeUnloadHandler;
  50. /**
  51. * Implements the middleware of the feature base/conference.
  52. *
  53. * @param {Store} store - The redux store.
  54. * @returns {Function}
  55. */
  56. MiddlewareRegistry.register(store => next => action => {
  57. switch (action.type) {
  58. case CONFERENCE_FAILED:
  59. return _conferenceFailed(store, next, action);
  60. case CONFERENCE_JOINED:
  61. return _conferenceJoined(store, next, action);
  62. case CONNECTION_ESTABLISHED:
  63. return _connectionEstablished(store, next, action);
  64. case CONNECTION_FAILED:
  65. return _connectionFailed(store, next, action);
  66. case CONFERENCE_SUBJECT_CHANGED:
  67. return _conferenceSubjectChanged(store, next, action);
  68. case CONFERENCE_WILL_LEAVE:
  69. _conferenceWillLeave();
  70. break;
  71. case DATA_CHANNEL_OPENED:
  72. return _syncReceiveVideoQuality(store, next, action);
  73. case PARTICIPANT_UPDATED:
  74. return _updateLocalParticipantInConference(store, next, action);
  75. case PIN_PARTICIPANT:
  76. return _pinParticipant(store, next, action);
  77. case SEND_TONES:
  78. return _sendTones(store, next, action);
  79. case SET_ROOM:
  80. return _setRoom(store, next, action);
  81. case TRACK_ADDED:
  82. case TRACK_REMOVED:
  83. return _trackAddedOrRemoved(store, next, action);
  84. }
  85. return next(action);
  86. });
  87. /**
  88. * Registers a change handler for state['features/base/conference'] to update
  89. * the preferred video quality levels based on user preferred and internal
  90. * settings.
  91. */
  92. StateListenerRegistry.register(
  93. /* selector */ state => state['features/base/conference'],
  94. /* listener */ (currentState, store, previousState = {}) => {
  95. const {
  96. conference,
  97. maxReceiverVideoQuality,
  98. preferredReceiverVideoQuality
  99. } = currentState;
  100. const changedPreferredVideoQuality
  101. = preferredReceiverVideoQuality !== previousState.preferredReceiverVideoQuality;
  102. const changedMaxVideoQuality = maxReceiverVideoQuality !== previousState.maxReceiverVideoQuality;
  103. if (changedPreferredVideoQuality || changedMaxVideoQuality) {
  104. _setReceiverVideoConstraint(conference, preferredReceiverVideoQuality, maxReceiverVideoQuality);
  105. }
  106. });
  107. /**
  108. * Makes sure to leave a failed conference in order to release any allocated
  109. * resources like peer connections, emit participant left events, etc.
  110. *
  111. * @param {Store} store - The redux store in which the specified {@code action}
  112. * is being dispatched.
  113. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  114. * specified {@code action} to the specified {@code store}.
  115. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  116. * being dispatched in the specified {@code store}.
  117. * @private
  118. * @returns {Object} The value returned by {@code next(action)}.
  119. */
  120. function _conferenceFailed(store, next, action) {
  121. const result = next(action);
  122. const { conference, error } = action;
  123. if (error.name === JitsiConferenceErrors.OFFER_ANSWER_FAILED) {
  124. sendAnalytics(createOfferAnswerFailedEvent());
  125. }
  126. // FIXME: Workaround for the web version. Currently, the creation of the
  127. // conference is handled by /conference.js and appropriate failure handlers
  128. // are set there.
  129. if (typeof APP !== 'undefined') {
  130. if (typeof beforeUnloadHandler !== 'undefined') {
  131. window.removeEventListener('beforeunload', beforeUnloadHandler);
  132. beforeUnloadHandler = undefined;
  133. }
  134. return result;
  135. }
  136. // XXX After next(action), it is clear whether the error is recoverable.
  137. !error.recoverable
  138. && conference
  139. && conference.leave().catch(reason => {
  140. // Even though we don't care too much about the failure, it may be
  141. // good to know that it happen, so log it (on the info level).
  142. logger.info('JitsiConference.leave() rejected with:', reason);
  143. });
  144. return result;
  145. }
  146. /**
  147. * Does extra sync up on properties that may need to be updated after the
  148. * conference was joined.
  149. *
  150. * @param {Store} store - The redux store in which the specified {@code action}
  151. * is being dispatched.
  152. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  153. * specified {@code action} to the specified {@code store}.
  154. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  155. * being dispatched in the specified {@code store}.
  156. * @private
  157. * @returns {Object} The value returned by {@code next(action)}.
  158. */
  159. function _conferenceJoined({ dispatch, getState }, next, action) {
  160. const result = next(action);
  161. const { conference } = action;
  162. const { pendingSubjectChange } = getState()['features/base/conference'];
  163. const { requireDisplayName } = getState()['features/base/config'];
  164. pendingSubjectChange && dispatch(setSubject(pendingSubjectChange));
  165. // FIXME: Very dirty solution. This will work on web only.
  166. // When the user closes the window or quits the browser, lib-jitsi-meet
  167. // handles the process of leaving the conference. This is temporary solution
  168. // that should cover the described use case as part of the effort to
  169. // implement the conferenceWillLeave action for web.
  170. beforeUnloadHandler = () => {
  171. dispatch(conferenceWillLeave(conference));
  172. };
  173. window.addEventListener('beforeunload', beforeUnloadHandler);
  174. if (requireDisplayName
  175. && !getLocalParticipant(getState)?.name
  176. && !conference.isHidden()) {
  177. dispatch(openDisplayNamePrompt(undefined));
  178. }
  179. return result;
  180. }
  181. /**
  182. * Notifies the feature base/conference that the action
  183. * {@code CONNECTION_ESTABLISHED} is being dispatched within a specific redux
  184. * store.
  185. *
  186. * @param {Store} store - The redux store in which the specified {@code action}
  187. * is being dispatched.
  188. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  189. * specified {@code action} to the specified {@code store}.
  190. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  191. * which is being dispatched in the specified {@code store}.
  192. * @private
  193. * @returns {Object} The value returned by {@code next(action)}.
  194. */
  195. function _connectionEstablished({ dispatch }, next, action) {
  196. const result = next(action);
  197. // FIXME: Workaround for the web version. Currently, the creation of the
  198. // conference is handled by /conference.js.
  199. typeof APP === 'undefined' && dispatch(createConference());
  200. return result;
  201. }
  202. /**
  203. * Notifies the feature base/conference that the action
  204. * {@code CONNECTION_FAILED} is being dispatched within a specific redux
  205. * store.
  206. *
  207. * @param {Store} store - The redux store in which the specified {@code action}
  208. * is being dispatched.
  209. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  210. * specified {@code action} to the specified {@code store}.
  211. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is
  212. * being dispatched in the specified {@code store}.
  213. * @private
  214. * @returns {Object} The value returned by {@code next(action)}.
  215. */
  216. function _connectionFailed({ dispatch, getState }, next, action) {
  217. const result = next(action);
  218. if (typeof beforeUnloadHandler !== 'undefined') {
  219. window.removeEventListener('beforeunload', beforeUnloadHandler);
  220. beforeUnloadHandler = undefined;
  221. }
  222. // FIXME: Workaround for the web version. Currently, the creation of the
  223. // conference is handled by /conference.js and appropriate failure handlers
  224. // are set there.
  225. if (typeof APP === 'undefined') {
  226. const { connection } = action;
  227. const { error } = action;
  228. forEachConference(getState, conference => {
  229. // It feels that it would make things easier if JitsiConference
  230. // in lib-jitsi-meet would monitor it's connection and emit
  231. // CONFERENCE_FAILED when it's dropped. It has more knowledge on
  232. // whether it can recover or not. But because the reload screen
  233. // and the retry logic is implemented in the app maybe it can be
  234. // left this way for now.
  235. if (conference.getConnection() === connection) {
  236. // XXX Note that on mobile the error type passed to
  237. // connectionFailed is always an object with .name property.
  238. // This fact needs to be checked prior to enabling this logic on
  239. // web.
  240. const conferenceAction
  241. = conferenceFailed(conference, error.name);
  242. // Copy the recoverable flag if set on the CONNECTION_FAILED
  243. // action to not emit recoverable action caused by
  244. // a non-recoverable one.
  245. if (typeof error.recoverable !== 'undefined') {
  246. conferenceAction.error.recoverable = error.recoverable;
  247. }
  248. dispatch(conferenceAction);
  249. }
  250. return true;
  251. });
  252. }
  253. return result;
  254. }
  255. /**
  256. * Notifies the feature base/conference that the action
  257. * {@code CONFERENCE_SUBJECT_CHANGED} is being dispatched within a specific
  258. * redux store.
  259. *
  260. * @param {Store} store - The redux store in which the specified {@code action}
  261. * is being dispatched.
  262. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  263. * specified {@code action} to the specified {@code store}.
  264. * @param {Action} action - The redux action {@code CONFERENCE_SUBJECT_CHANGED}
  265. * which is being dispatched in the specified {@code store}.
  266. * @private
  267. * @returns {Object} The value returned by {@code next(action)}.
  268. */
  269. function _conferenceSubjectChanged({ dispatch, getState }, next, action) {
  270. const result = next(action);
  271. const { subject } = getState()['features/base/conference'];
  272. if (subject) {
  273. dispatch({
  274. type: SET_PENDING_SUBJECT_CHANGE,
  275. subject: undefined
  276. });
  277. }
  278. typeof APP === 'object' && APP.API.notifySubjectChanged(subject);
  279. return result;
  280. }
  281. /**
  282. * Notifies the feature base/conference that the action
  283. * {@code CONFERENCE_WILL_LEAVE} is being dispatched within a specific redux
  284. * store.
  285. *
  286. * @private
  287. * @returns {void}
  288. */
  289. function _conferenceWillLeave() {
  290. if (typeof beforeUnloadHandler !== 'undefined') {
  291. window.removeEventListener('beforeunload', beforeUnloadHandler);
  292. beforeUnloadHandler = undefined;
  293. }
  294. }
  295. /**
  296. * Notifies the feature base/conference that the action {@code PIN_PARTICIPANT}
  297. * is being dispatched within a specific redux store. Pins the specified remote
  298. * participant in the associated conference, ignores the local participant.
  299. *
  300. * @param {Store} store - The redux store in which the specified {@code action}
  301. * is being dispatched.
  302. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  303. * specified {@code action} to the specified {@code store}.
  304. * @param {Action} action - The redux action {@code PIN_PARTICIPANT} which is
  305. * being dispatched in the specified {@code store}.
  306. * @private
  307. * @returns {Object} The value returned by {@code next(action)}.
  308. */
  309. function _pinParticipant({ getState }, next, action) {
  310. const state = getState();
  311. const { conference } = state['features/base/conference'];
  312. if (!conference) {
  313. return next(action);
  314. }
  315. const participants = state['features/base/participants'];
  316. const id = action.participant.id;
  317. const participantById = getParticipantById(participants, id);
  318. const pinnedParticipant = getPinnedParticipant(participants);
  319. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  320. const local
  321. = (participantById && participantById.local)
  322. || (!id && pinnedParticipant && pinnedParticipant.local);
  323. let participantIdForEvent;
  324. if (local) {
  325. participantIdForEvent = local;
  326. } else {
  327. participantIdForEvent
  328. = actionName === ACTION_PINNED ? id : pinnedParticipant && pinnedParticipant.id;
  329. }
  330. sendAnalytics(createPinnedEvent(
  331. actionName,
  332. participantIdForEvent,
  333. {
  334. local,
  335. 'participant_count': conference.getParticipantCount()
  336. }));
  337. return next(action);
  338. }
  339. /**
  340. * Requests the specified tones to be played.
  341. *
  342. * @param {Store} store - The redux store in which the specified {@code action}
  343. * is being dispatched.
  344. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  345. * specified {@code action} to the specified {@code store}.
  346. * @param {Action} action - The redux action {@code SEND_TONES} which is
  347. * being dispatched in the specified {@code store}.
  348. * @private
  349. * @returns {Object} The value returned by {@code next(action)}.
  350. */
  351. function _sendTones({ getState }, next, action) {
  352. const state = getState();
  353. const { conference } = state['features/base/conference'];
  354. if (conference) {
  355. const { duration, tones, pause } = action;
  356. conference.sendTones(tones, duration, pause);
  357. }
  358. return next(action);
  359. }
  360. /**
  361. * Helper function for updating the preferred receiver video constraint, based
  362. * on the user preference and the internal maximum.
  363. *
  364. * @param {JitsiConference} conference - The JitsiConference instance for the
  365. * current call.
  366. * @param {number} preferred - The user preferred max frame height.
  367. * @param {number} max - The maximum frame height the application should
  368. * receive.
  369. * @returns {void}
  370. */
  371. function _setReceiverVideoConstraint(conference, preferred, max) {
  372. if (conference) {
  373. conference.setReceiverVideoConstraint(Math.min(preferred, max));
  374. }
  375. }
  376. /**
  377. * Notifies the feature base/conference that the action
  378. * {@code SET_ROOM} is being dispatched within a specific
  379. * redux store.
  380. *
  381. * @param {Store} store - The redux store in which the specified {@code action}
  382. * is being dispatched.
  383. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  384. * specified {@code action} to the specified {@code store}.
  385. * @param {Action} action - The redux action {@code SET_ROOM}
  386. * which is being dispatched in the specified {@code store}.
  387. * @private
  388. * @returns {Object} The value returned by {@code next(action)}.
  389. */
  390. function _setRoom({ dispatch, getState }, next, action) {
  391. const state = getState();
  392. const { subject } = state['features/base/config'];
  393. const { room } = action;
  394. if (room) {
  395. // Set the stored subject.
  396. dispatch(setSubject(subject));
  397. }
  398. return next(action);
  399. }
  400. /**
  401. * Synchronizes local tracks from state with local tracks in JitsiConference
  402. * instance.
  403. *
  404. * @param {Store} store - The redux store.
  405. * @param {Object} action - Action object.
  406. * @private
  407. * @returns {Promise}
  408. */
  409. function _syncConferenceLocalTracksWithState({ getState }, action) {
  410. const conference = getCurrentConference(getState);
  411. let promise;
  412. if (conference) {
  413. const track = action.track.jitsiTrack;
  414. if (action.type === TRACK_ADDED) {
  415. promise = _addLocalTracksToConference(conference, [ track ]);
  416. } else {
  417. promise = _removeLocalTracksFromConference(conference, [ track ]);
  418. }
  419. }
  420. return promise || Promise.resolve();
  421. }
  422. /**
  423. * Sets the maximum receive video quality.
  424. *
  425. * @param {Store} store - The redux store in which the specified {@code action}
  426. * is being dispatched.
  427. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  428. * specified {@code action} to the specified {@code store}.
  429. * @param {Action} action - The redux action {@code DATA_CHANNEL_STATUS_CHANGED}
  430. * which is being dispatched in the specified {@code store}.
  431. * @private
  432. * @returns {Object} The value returned by {@code next(action)}.
  433. */
  434. function _syncReceiveVideoQuality({ getState }, next, action) {
  435. const {
  436. conference,
  437. maxReceiverVideoQuality,
  438. preferredReceiverVideoQuality
  439. } = getState()['features/base/conference'];
  440. _setReceiverVideoConstraint(
  441. conference,
  442. preferredReceiverVideoQuality,
  443. maxReceiverVideoQuality);
  444. return next(action);
  445. }
  446. /**
  447. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  448. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  449. *
  450. * @param {Store} store - The redux store in which the specified {@code action}
  451. * is being dispatched.
  452. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  453. * specified {@code action} to the specified {@code store}.
  454. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  455. * {@code TRACK_REMOVED} which is being dispatched in the specified
  456. * {@code store}.
  457. * @private
  458. * @returns {Object} The value returned by {@code next(action)}.
  459. */
  460. function _trackAddedOrRemoved(store, next, action) {
  461. const track = action.track;
  462. // TODO All track swapping should happen here instead of conference.js.
  463. // Since we swap the tracks for the web client in conference.js, ignore
  464. // presenter tracks here and do not add/remove them to/from the conference.
  465. if (track && track.local && track.mediaType !== MEDIA_TYPE.PRESENTER) {
  466. return (
  467. _syncConferenceLocalTracksWithState(store, action)
  468. .then(() => next(action)));
  469. }
  470. return next(action);
  471. }
  472. /**
  473. * Updates the conference object when the local participant is updated.
  474. *
  475. * @param {Store} store - The redux store in which the specified {@code action}
  476. * is being dispatched.
  477. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  478. * specified {@code action} to the specified {@code store}.
  479. * @param {Action} action - The redux action which is being dispatched in the
  480. * specified {@code store}.
  481. * @private
  482. * @returns {Object} The value returned by {@code next(action)}.
  483. */
  484. function _updateLocalParticipantInConference({ getState }, next, action) {
  485. const { conference } = getState()['features/base/conference'];
  486. const { participant } = action;
  487. const result = next(action);
  488. if (conference && participant.local && 'name' in participant) {
  489. conference.setDisplayName(participant.name);
  490. }
  491. return result;
  492. }