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

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