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

middleware.any.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // @flow
  2. import {
  3. ACTION_PINNED,
  4. ACTION_UNPINNED,
  5. createOfferAnswerFailedEvent,
  6. createPinnedEvent,
  7. sendAnalytics
  8. } from '../../analytics';
  9. import { openDisplayNamePrompt } from '../../display-name';
  10. import { showErrorNotification } from '../../notifications';
  11. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED, connectionDisconnected } from '../connection';
  12. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  13. import { MEDIA_TYPE } from '../media';
  14. import {
  15. getLocalParticipant,
  16. getParticipantById,
  17. getPinnedParticipant,
  18. PARTICIPANT_ROLE,
  19. PARTICIPANT_UPDATED,
  20. PIN_PARTICIPANT
  21. } from '../participants';
  22. import { MiddlewareRegistry } from '../redux';
  23. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  24. import {
  25. CONFERENCE_FAILED,
  26. CONFERENCE_JOINED,
  27. CONFERENCE_SUBJECT_CHANGED,
  28. CONFERENCE_WILL_LEAVE,
  29. SEND_TONES,
  30. SET_PENDING_SUBJECT_CHANGE,
  31. SET_ROOM
  32. } from './actionTypes';
  33. import {
  34. conferenceFailed,
  35. conferenceWillLeave,
  36. createConference,
  37. setSubject
  38. } from './actions';
  39. import {
  40. _addLocalTracksToConference,
  41. _removeLocalTracksFromConference,
  42. forEachConference,
  43. getCurrentConference
  44. } from './functions';
  45. import logger from './logger';
  46. declare var APP: Object;
  47. /**
  48. * Handler for before unload event.
  49. */
  50. let beforeUnloadHandler;
  51. /**
  52. * Implements the middleware of the feature base/conference.
  53. *
  54. * @param {Store} store - The redux store.
  55. * @returns {Function}
  56. */
  57. MiddlewareRegistry.register(store => next => action => {
  58. switch (action.type) {
  59. case CONFERENCE_FAILED:
  60. return _conferenceFailed(store, next, action);
  61. case CONFERENCE_JOINED:
  62. return _conferenceJoined(store, next, action);
  63. case CONNECTION_ESTABLISHED:
  64. return _connectionEstablished(store, next, action);
  65. case CONNECTION_FAILED:
  66. return _connectionFailed(store, next, action);
  67. case CONFERENCE_SUBJECT_CHANGED:
  68. return _conferenceSubjectChanged(store, next, action);
  69. case CONFERENCE_WILL_LEAVE:
  70. _conferenceWillLeave();
  71. break;
  72. case PARTICIPANT_UPDATED:
  73. return _updateLocalParticipantInConference(store, next, action);
  74. case PIN_PARTICIPANT:
  75. return _pinParticipant(store, next, action);
  76. case SEND_TONES:
  77. return _sendTones(store, next, action);
  78. case SET_ROOM:
  79. return _setRoom(store, next, action);
  80. case TRACK_ADDED:
  81. case TRACK_REMOVED:
  82. return _trackAddedOrRemoved(store, next, action);
  83. }
  84. return next(action);
  85. });
  86. /**
  87. * Makes sure to leave a failed conference in order to release any allocated
  88. * resources like peer connections, emit participant left events, etc.
  89. *
  90. * @param {Store} store - The redux store in which the specified {@code action}
  91. * is being dispatched.
  92. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  93. * specified {@code action} to the specified {@code store}.
  94. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  95. * being dispatched in the specified {@code store}.
  96. * @private
  97. * @returns {Object} The value returned by {@code next(action)}.
  98. */
  99. function _conferenceFailed({ dispatch, getState }, next, action) {
  100. const result = next(action);
  101. const { conference, error } = action;
  102. // Handle specific failure reasons.
  103. switch (error.name) {
  104. case JitsiConferenceErrors.CONFERENCE_DESTROYED: {
  105. const [ reason ] = error.params;
  106. dispatch(showErrorNotification({
  107. description: reason,
  108. titleKey: 'dialog.sessTerminated'
  109. }));
  110. if (typeof APP !== 'undefined') {
  111. APP.UI.hideStats();
  112. }
  113. break;
  114. }
  115. case JitsiConferenceErrors.CONNECTION_ERROR: {
  116. const [ msg ] = error.params;
  117. dispatch(connectionDisconnected(getState()['features/base/connection'].connection));
  118. dispatch(showErrorNotification({
  119. descriptionArguments: { msg },
  120. descriptionKey: msg ? 'dialog.connectErrorWithMsg' : 'dialog.connectError',
  121. titleKey: 'connection.CONNFAIL'
  122. }));
  123. break;
  124. }
  125. case JitsiConferenceErrors.OFFER_ANSWER_FAILED:
  126. sendAnalytics(createOfferAnswerFailedEvent());
  127. break;
  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. * Notifies the feature base/conference that the action
  365. * {@code SET_ROOM} is being dispatched within a specific
  366. * redux store.
  367. *
  368. * @param {Store} store - The redux store in which the specified {@code action}
  369. * is being dispatched.
  370. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  371. * specified {@code action} to the specified {@code store}.
  372. * @param {Action} action - The redux action {@code SET_ROOM}
  373. * which is being dispatched in the specified {@code store}.
  374. * @private
  375. * @returns {Object} The value returned by {@code next(action)}.
  376. */
  377. function _setRoom({ dispatch, getState }, next, action) {
  378. const state = getState();
  379. const { subject } = state['features/base/config'];
  380. const { room } = action;
  381. if (room) {
  382. // Set the stored subject.
  383. dispatch(setSubject(subject));
  384. }
  385. return next(action);
  386. }
  387. /**
  388. * Synchronizes local tracks from state with local tracks in JitsiConference
  389. * instance.
  390. *
  391. * @param {Store} store - The redux store.
  392. * @param {Object} action - Action object.
  393. * @private
  394. * @returns {Promise}
  395. */
  396. function _syncConferenceLocalTracksWithState({ getState }, action) {
  397. const conference = getCurrentConference(getState);
  398. let promise;
  399. if (conference) {
  400. const track = action.track.jitsiTrack;
  401. if (action.type === TRACK_ADDED) {
  402. promise = _addLocalTracksToConference(conference, [ track ]);
  403. } else {
  404. promise = _removeLocalTracksFromConference(conference, [ track ]);
  405. }
  406. }
  407. return promise || Promise.resolve();
  408. }
  409. /**
  410. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  411. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  412. *
  413. * @param {Store} store - The redux store in which the specified {@code action}
  414. * is being dispatched.
  415. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  416. * specified {@code action} to the specified {@code store}.
  417. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  418. * {@code TRACK_REMOVED} which is being dispatched in the specified
  419. * {@code store}.
  420. * @private
  421. * @returns {Object} The value returned by {@code next(action)}.
  422. */
  423. function _trackAddedOrRemoved(store, next, action) {
  424. const track = action.track;
  425. // TODO All track swapping should happen here instead of conference.js.
  426. // Since we swap the tracks for the web client in conference.js, ignore
  427. // presenter tracks here and do not add/remove them to/from the conference.
  428. if (track && track.local && track.mediaType !== MEDIA_TYPE.PRESENTER) {
  429. return (
  430. _syncConferenceLocalTracksWithState(store, action)
  431. .then(() => next(action)));
  432. }
  433. return next(action);
  434. }
  435. /**
  436. * Updates the conference object when the local participant is updated.
  437. *
  438. * @param {Store} store - The redux store in which the specified {@code action}
  439. * is being dispatched.
  440. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  441. * specified {@code action} to the specified {@code store}.
  442. * @param {Action} action - The redux action which is being dispatched in the
  443. * specified {@code store}.
  444. * @private
  445. * @returns {Object} The value returned by {@code next(action)}.
  446. */
  447. function _updateLocalParticipantInConference({ dispatch, getState }, next, action) {
  448. const { conference } = getState()['features/base/conference'];
  449. const { participant } = action;
  450. const result = next(action);
  451. const localParticipant = getLocalParticipant(getState);
  452. if (conference && participant.id === localParticipant.id) {
  453. if ('name' in participant) {
  454. conference.setDisplayName(participant.name);
  455. }
  456. if ('role' in participant && participant.role === PARTICIPANT_ROLE.MODERATOR) {
  457. const { pendingSubjectChange, subject } = getState()['features/base/conference'];
  458. // When the local user role is updated to moderator and we have a pending subject change
  459. // which was not reflected we need to set it (the first time we tried was before becoming moderator).
  460. if (typeof pendingSubjectChange !== 'undefined' && pendingSubjectChange !== subject) {
  461. dispatch(setSubject(pendingSubjectChange));
  462. }
  463. }
  464. }
  465. return result;
  466. }