選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

middleware.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // @flow
  2. import { reloadNow } from '../../app';
  3. import {
  4. ACTION_PINNED,
  5. ACTION_UNPINNED,
  6. createConnectionEvent,
  7. createOfferAnswerFailedEvent,
  8. createPinnedEvent,
  9. sendAnalytics
  10. } from '../../analytics';
  11. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../connection';
  12. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  13. import {
  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. SET_PENDING_SUBJECT_CHANGE,
  34. SET_ROOM
  35. } from './actionTypes';
  36. import {
  37. _addLocalTracksToConference,
  38. _removeLocalTracksFromConference,
  39. forEachConference,
  40. getCurrentConference
  41. } from './functions';
  42. import logger from './logger';
  43. declare var APP: Object;
  44. /**
  45. * Handler for before unload event.
  46. */
  47. let beforeUnloadHandler;
  48. /**
  49. * Implements the middleware of the feature base/conference.
  50. *
  51. * @param {Store} store - The redux store.
  52. * @returns {Function}
  53. */
  54. MiddlewareRegistry.register(store => next => action => {
  55. switch (action.type) {
  56. case CONFERENCE_FAILED:
  57. return _conferenceFailed(store, next, action);
  58. case CONFERENCE_JOINED:
  59. return _conferenceJoined(store, next, action);
  60. case CONNECTION_ESTABLISHED:
  61. return _connectionEstablished(store, next, action);
  62. case CONNECTION_FAILED:
  63. return _connectionFailed(store, next, action);
  64. case CONFERENCE_SUBJECT_CHANGED:
  65. return _conferenceSubjectChanged(store, next, action);
  66. case CONFERENCE_WILL_LEAVE:
  67. _conferenceWillLeave();
  68. break;
  69. case DATA_CHANNEL_OPENED:
  70. return _syncReceiveVideoQuality(store, next, action);
  71. case PARTICIPANT_UPDATED:
  72. return _updateLocalParticipantInConference(store, next, action);
  73. case PIN_PARTICIPANT:
  74. return _pinParticipant(store, next, action);
  75. case SET_ROOM:
  76. return _setRoom(store, next, action);
  77. case TRACK_ADDED:
  78. case TRACK_REMOVED:
  79. return _trackAddedOrRemoved(store, next, action);
  80. }
  81. return next(action);
  82. });
  83. /**
  84. * Registers a change handler for state['features/base/conference'] to update
  85. * the preferred video quality levels based on user preferred and internal
  86. * settings.
  87. */
  88. StateListenerRegistry.register(
  89. /* selector */ state => state['features/base/conference'],
  90. /* listener */ (currentState, store, previousState = {}) => {
  91. const {
  92. conference,
  93. maxReceiverVideoQuality,
  94. preferredReceiverVideoQuality
  95. } = currentState;
  96. const changedPreferredVideoQuality = preferredReceiverVideoQuality
  97. !== previousState.preferredReceiverVideoQuality;
  98. const changedMaxVideoQuality = maxReceiverVideoQuality
  99. !== previousState.maxReceiverVideoQuality;
  100. if (changedPreferredVideoQuality || changedMaxVideoQuality) {
  101. _setReceiverVideoConstraint(
  102. conference,
  103. preferredReceiverVideoQuality,
  104. 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. pendingSubjectChange && dispatch(setSubject(pendingSubjectChange));
  164. // FIXME: Very dirty solution. This will work on web only.
  165. // When the user closes the window or quits the browser, lib-jitsi-meet
  166. // handles the process of leaving the conference. This is temporary solution
  167. // that should cover the described use case as part of the effort to
  168. // implement the conferenceWillLeave action for web.
  169. beforeUnloadHandler = () => {
  170. dispatch(conferenceWillLeave(conference));
  171. };
  172. window.addEventListener('beforeunload', beforeUnloadHandler);
  173. return result;
  174. }
  175. /**
  176. * Notifies the feature base/conference that the action
  177. * {@code CONNECTION_ESTABLISHED} is being dispatched within a specific redux
  178. * store.
  179. *
  180. * @param {Store} store - The redux store in which the specified {@code action}
  181. * is being dispatched.
  182. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  183. * specified {@code action} to the specified {@code store}.
  184. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  185. * which is being dispatched in the specified {@code store}.
  186. * @private
  187. * @returns {Object} The value returned by {@code next(action)}.
  188. */
  189. function _connectionEstablished({ dispatch }, next, action) {
  190. const result = next(action);
  191. // FIXME: Workaround for the web version. Currently, the creation of the
  192. // conference is handled by /conference.js.
  193. typeof APP === 'undefined' && dispatch(createConference());
  194. return result;
  195. }
  196. /**
  197. * Notifies the feature base/conference that the action
  198. * {@code CONNECTION_FAILED} is being dispatched within a specific redux
  199. * store.
  200. *
  201. * @param {Store} store - The redux store in which the specified {@code action}
  202. * is being dispatched.
  203. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  204. * specified {@code action} to the specified {@code store}.
  205. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is
  206. * being dispatched in the specified {@code store}.
  207. * @private
  208. * @returns {Object} The value returned by {@code next(action)}.
  209. */
  210. function _connectionFailed({ dispatch, getState }, next, action) {
  211. // In the case of a split-brain error, reload early and prevent further
  212. // handling of the action.
  213. if (_isMaybeSplitBrainError(getState, action)) {
  214. dispatch(reloadNow());
  215. return;
  216. }
  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. * Returns whether or not a CONNECTION_FAILED action is for a possible split
  297. * brain error. A split brain error occurs when at least two users join a
  298. * conference on different bridges. It is assumed the split brain scenario
  299. * occurs very early on in the call.
  300. *
  301. * @param {Function} getState - The redux function for fetching the current
  302. * state.
  303. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is
  304. * being dispatched in the specified {@code store}.
  305. * @private
  306. * @returns {boolean}
  307. */
  308. function _isMaybeSplitBrainError(getState, action) {
  309. const { error } = action;
  310. const isShardChangedError = error
  311. && error.message === 'item-not-found'
  312. && error.details
  313. && error.details.shard_changed;
  314. if (isShardChangedError) {
  315. const state = getState();
  316. const { timeEstablished } = state['features/base/connection'];
  317. const { _immediateReloadThreshold } = state['features/base/config'];
  318. const timeSinceConnectionEstablished
  319. = timeEstablished && Date.now() - timeEstablished;
  320. const reloadThreshold = typeof _immediateReloadThreshold === 'number'
  321. ? _immediateReloadThreshold : 1500;
  322. const isWithinSplitBrainThreshold = !timeEstablished
  323. || timeSinceConnectionEstablished <= reloadThreshold;
  324. sendAnalytics(createConnectionEvent('failed', {
  325. ...error,
  326. connectionEstablished: timeEstablished,
  327. splitBrain: isWithinSplitBrainThreshold,
  328. timeSinceConnectionEstablished
  329. }));
  330. return isWithinSplitBrainThreshold;
  331. }
  332. return false;
  333. }
  334. /**
  335. * Notifies the feature base/conference that the action {@code PIN_PARTICIPANT}
  336. * is being dispatched within a specific redux store. Pins the specified remote
  337. * participant in the associated conference, ignores the local participant.
  338. *
  339. * @param {Store} store - The redux store in which the specified {@code action}
  340. * is being dispatched.
  341. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  342. * specified {@code action} to the specified {@code store}.
  343. * @param {Action} action - The redux action {@code PIN_PARTICIPANT} which is
  344. * being dispatched in the specified {@code store}.
  345. * @private
  346. * @returns {Object} The value returned by {@code next(action)}.
  347. */
  348. function _pinParticipant({ getState }, next, action) {
  349. const state = getState();
  350. const { conference } = state['features/base/conference'];
  351. if (!conference) {
  352. return next(action);
  353. }
  354. const participants = state['features/base/participants'];
  355. const id = action.participant.id;
  356. const participantById = getParticipantById(participants, id);
  357. const pinnedParticipant = getPinnedParticipant(participants);
  358. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  359. const local
  360. = (participantById && participantById.local)
  361. || (!id && pinnedParticipant && pinnedParticipant.local);
  362. let participantIdForEvent;
  363. if (local) {
  364. participantIdForEvent = local;
  365. } else {
  366. participantIdForEvent
  367. = actionName === ACTION_PINNED ? id : pinnedParticipant && pinnedParticipant.id;
  368. }
  369. sendAnalytics(createPinnedEvent(
  370. actionName,
  371. participantIdForEvent,
  372. {
  373. local,
  374. 'participant_count': conference.getParticipantCount()
  375. }));
  376. return next(action);
  377. }
  378. /**
  379. * Helper function for updating the preferred receiver video constraint, based
  380. * on the user preference and the internal maximum.
  381. *
  382. * @param {JitsiConference} conference - The JitsiConference instance for the
  383. * current call.
  384. * @param {number} preferred - The user preferred max frame height.
  385. * @param {number} max - The maximum frame height the application should
  386. * receive.
  387. * @returns {void}
  388. */
  389. function _setReceiverVideoConstraint(conference, preferred, max) {
  390. if (conference) {
  391. conference.setReceiverVideoConstraint(Math.min(preferred, max));
  392. }
  393. }
  394. /**
  395. * Notifies the feature base/conference that the action
  396. * {@code SET_ROOM} is being dispatched within a specific
  397. * redux store.
  398. *
  399. * @param {Store} store - The redux store in which the specified {@code action}
  400. * is being dispatched.
  401. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  402. * specified {@code action} to the specified {@code store}.
  403. * @param {Action} action - The redux action {@code SET_ROOM}
  404. * which is being dispatched in the specified {@code store}.
  405. * @private
  406. * @returns {Object} The value returned by {@code next(action)}.
  407. */
  408. function _setRoom({ dispatch, getState }, next, action) {
  409. const state = getState();
  410. const { subject } = state['features/base/config'];
  411. const { room } = action;
  412. if (room) {
  413. // Set the stored subject.
  414. dispatch(setSubject(subject));
  415. }
  416. return next(action);
  417. }
  418. /**
  419. * Synchronizes local tracks from state with local tracks in JitsiConference
  420. * instance.
  421. *
  422. * @param {Store} store - The redux store.
  423. * @param {Object} action - Action object.
  424. * @private
  425. * @returns {Promise}
  426. */
  427. function _syncConferenceLocalTracksWithState({ getState }, action) {
  428. const conference = getCurrentConference(getState);
  429. let promise;
  430. if (conference) {
  431. const track = action.track.jitsiTrack;
  432. if (action.type === TRACK_ADDED) {
  433. promise = _addLocalTracksToConference(conference, [ track ]);
  434. } else {
  435. promise = _removeLocalTracksFromConference(conference, [ track ]);
  436. }
  437. }
  438. return promise || Promise.resolve();
  439. }
  440. /**
  441. * Sets the maximum receive video quality.
  442. *
  443. * @param {Store} store - The redux store in which the specified {@code action}
  444. * is being dispatched.
  445. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  446. * specified {@code action} to the specified {@code store}.
  447. * @param {Action} action - The redux action {@code DATA_CHANNEL_STATUS_CHANGED}
  448. * which is being dispatched in the specified {@code store}.
  449. * @private
  450. * @returns {Object} The value returned by {@code next(action)}.
  451. */
  452. function _syncReceiveVideoQuality({ getState }, next, action) {
  453. const {
  454. conference,
  455. maxReceiverVideoQuality,
  456. preferredReceiverVideoQuality
  457. } = getState()['features/base/conference'];
  458. _setReceiverVideoConstraint(
  459. conference,
  460. preferredReceiverVideoQuality,
  461. maxReceiverVideoQuality);
  462. return next(action);
  463. }
  464. /**
  465. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  466. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  467. *
  468. * @param {Store} store - The redux store in which the specified {@code action}
  469. * is being dispatched.
  470. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  471. * specified {@code action} to the specified {@code store}.
  472. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  473. * {@code TRACK_REMOVED} which is being dispatched in the specified
  474. * {@code store}.
  475. * @private
  476. * @returns {Object} The value returned by {@code next(action)}.
  477. */
  478. function _trackAddedOrRemoved(store, next, action) {
  479. const track = action.track;
  480. if (track && track.local) {
  481. return (
  482. _syncConferenceLocalTracksWithState(store, action)
  483. .then(() => next(action)));
  484. }
  485. return next(action);
  486. }
  487. /**
  488. * Updates the conference object when the local participant is updated.
  489. *
  490. * @param {Store} store - The redux store in which the specified {@code action}
  491. * is being dispatched.
  492. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  493. * specified {@code action} to the specified {@code store}.
  494. * @param {Action} action - The redux action which is being dispatched in the
  495. * specified {@code store}.
  496. * @private
  497. * @returns {Object} The value returned by {@code next(action)}.
  498. */
  499. function _updateLocalParticipantInConference({ getState }, next, action) {
  500. const { conference } = getState()['features/base/conference'];
  501. const { participant } = action;
  502. const result = next(action);
  503. if (conference && participant.local && 'name' in participant) {
  504. conference.setDisplayName(participant.name);
  505. }
  506. return result;
  507. }