您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 19KB

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