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

middleware.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // @flow
  2. import {
  3. ACTION_PINNED,
  4. ACTION_UNPINNED,
  5. createAudioOnlyChangedEvent,
  6. createPinnedEvent,
  7. sendAnalytics
  8. } from '../../analytics';
  9. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../connection';
  10. import { setVideoMuted, VIDEO_MUTISM_AUTHORITY } from '../media';
  11. import {
  12. getLocalParticipant,
  13. getParticipantById,
  14. getPinnedParticipant,
  15. PIN_PARTICIPANT
  16. } from '../participants';
  17. import { MiddlewareRegistry } from '../redux';
  18. import UIEvents from '../../../../service/UI/UIEvents';
  19. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  20. import {
  21. conferenceFailed,
  22. conferenceLeft,
  23. createConference,
  24. setLastN,
  25. toggleAudioOnly
  26. } from './actions';
  27. import {
  28. CONFERENCE_FAILED,
  29. CONFERENCE_JOINED,
  30. DATA_CHANNEL_OPENED,
  31. SET_AUDIO_ONLY,
  32. SET_LASTN,
  33. SET_RECEIVE_VIDEO_QUALITY,
  34. SET_ROOM
  35. } from './actionTypes';
  36. import {
  37. _addLocalTracksToConference,
  38. forEachConference,
  39. _handleParticipantError,
  40. _removeLocalTracksFromConference
  41. } from './functions';
  42. const logger = require('jitsi-meet-logger').getLogger(__filename);
  43. declare var APP: Object;
  44. /**
  45. * Implements the middleware of the feature base/conference.
  46. *
  47. * @param {Store} store - The redux store.
  48. * @returns {Function}
  49. */
  50. MiddlewareRegistry.register(store => next => action => {
  51. switch (action.type) {
  52. case CONFERENCE_FAILED:
  53. return _conferenceFailed(store, next, action);
  54. case CONFERENCE_JOINED:
  55. return _conferenceJoined(store, next, action);
  56. case CONNECTION_ESTABLISHED:
  57. return _connectionEstablished(store, next, action);
  58. case CONNECTION_FAILED:
  59. return _connectionFailed(store, next, action);
  60. case DATA_CHANNEL_OPENED:
  61. return _syncReceiveVideoQuality(store, next, action);
  62. case PIN_PARTICIPANT:
  63. return _pinParticipant(store, next, action);
  64. case SET_AUDIO_ONLY:
  65. return _setAudioOnly(store, next, action);
  66. case SET_LASTN:
  67. return _setLastN(store, next, action);
  68. case SET_RECEIVE_VIDEO_QUALITY:
  69. return _setReceiveVideoQuality(store, next, action);
  70. case SET_ROOM:
  71. return _setRoom(store, next, action);
  72. case TRACK_ADDED:
  73. case TRACK_REMOVED:
  74. return _trackAddedOrRemoved(store, next, action);
  75. }
  76. return next(action);
  77. });
  78. /**
  79. * Makes sure to leave a failed conference in order to release any allocated
  80. * resources like peer connections, emit participant left events, etc.
  81. *
  82. * @param {Store} store - The redux store in which the specified {@code action}
  83. * is being dispatched.
  84. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  85. * specified {@code action} to the specified {@code store}.
  86. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  87. * being dispatched in the specified {@code store}.
  88. * @private
  89. * @returns {Object} The value returned by {@code next(action)}.
  90. */
  91. function _conferenceFailed(store, next, action) {
  92. const result = next(action);
  93. // FIXME: Workaround for the web version. Currently, the creation of the
  94. // conference is handled by /conference.js and appropriate failure handlers
  95. // are set there.
  96. if (typeof APP !== 'undefined') {
  97. return result;
  98. }
  99. // XXX After next(action), it is clear whether the error is recoverable.
  100. const { conference, error } = action;
  101. !error.recoverable
  102. && conference
  103. && conference.leave().catch(reason => {
  104. // Even though we don't care too much about the failure, it may be
  105. // good to know that it happen, so log it (on the info level).
  106. logger.info('JitsiConference.leave() rejected with:', reason);
  107. });
  108. return result;
  109. }
  110. /**
  111. * Does extra sync up on properties that may need to be updated after the
  112. * conference was joined.
  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_JOINED} 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 _conferenceJoined({ dispatch, getState }, next, action) {
  124. const result = next(action);
  125. const { audioOnly, conference } = getState()['features/base/conference'];
  126. // FIXME On Web the audio only mode for "start audio only" is toggled before
  127. // conference is added to the redux store ("on conference joined" action)
  128. // and the LastN value needs to be synchronized here.
  129. audioOnly && conference.getLastN() !== 0 && dispatch(setLastN(0));
  130. return result;
  131. }
  132. /**
  133. * Notifies the feature base/conference that the action
  134. * {@code CONNECTION_ESTABLISHED} is being dispatched within a specific redux
  135. * store.
  136. *
  137. * @param {Store} store - The redux store in which the specified {@code action}
  138. * is being dispatched.
  139. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  140. * specified {@code action} to the specified {@code store}.
  141. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  142. * which is being dispatched in the specified {@code store}.
  143. * @private
  144. * @returns {Object} The value returned by {@code next(action)}.
  145. */
  146. function _connectionEstablished({ dispatch }, next, action) {
  147. const result = next(action);
  148. // FIXME: Workaround for the web version. Currently, the creation of the
  149. // conference is handled by /conference.js.
  150. typeof APP === 'undefined' && dispatch(createConference());
  151. return result;
  152. }
  153. /**
  154. * Notifies the feature base/conference that the action
  155. * {@code CONNECTION_FAILED} is being dispatched within a specific redux
  156. * store.
  157. *
  158. * @param {Store} store - The redux store in which the specified {@code action}
  159. * is being dispatched.
  160. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  161. * specified {@code action} to the specified {@code store}.
  162. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is
  163. * being dispatched in the specified {@code store}.
  164. * @private
  165. * @returns {Object} The value returned by {@code next(action)}.
  166. */
  167. function _connectionFailed({ dispatch, getState }, next, action) {
  168. const result = next(action);
  169. // FIXME: Workaround for the web version. Currently, the creation of the
  170. // conference is handled by /conference.js and appropriate failure handlers
  171. // are set there.
  172. if (typeof APP === 'undefined') {
  173. const { connection } = action;
  174. const { error } = action;
  175. forEachConference(getState, conference => {
  176. // It feels that it would make things easier if JitsiConference
  177. // in lib-jitsi-meet would monitor it's connection and emit
  178. // CONFERENCE_FAILED when it's dropped. It has more knowledge on
  179. // whether it can recover or not. But because the reload screen
  180. // and the retry logic is implemented in the app maybe it can be
  181. // left this way for now.
  182. if (conference.getConnection() === connection) {
  183. // XXX Note that on mobile the error type passed to
  184. // connectionFailed is always an object with .name property.
  185. // This fact needs to be checked prior to enabling this logic on
  186. // web.
  187. const conferenceAction
  188. = conferenceFailed(conference, error.name);
  189. // Copy the recoverable flag if set on the CONNECTION_FAILED
  190. // action to not emit recoverable action caused by
  191. // a non-recoverable one.
  192. if (typeof error.recoverable !== 'undefined') {
  193. conferenceAction.error.recoverable = error.recoverable;
  194. }
  195. dispatch(conferenceAction);
  196. }
  197. return true;
  198. });
  199. }
  200. return result;
  201. }
  202. /**
  203. * Notifies the feature base/conference that the action {@code PIN_PARTICIPANT}
  204. * is being dispatched within a specific redux store. Pins the specified remote
  205. * participant in the associated conference, ignores the local participant.
  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 PIN_PARTICIPANT} 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 _pinParticipant({ getState }, next, action) {
  217. const state = getState();
  218. const { conference } = state['features/base/conference'];
  219. if (!conference) {
  220. return next(action);
  221. }
  222. const participants = state['features/base/participants'];
  223. const id = action.participant.id;
  224. const participantById = getParticipantById(participants, id);
  225. if (typeof APP !== 'undefined') {
  226. const pinnedParticipant = getPinnedParticipant(participants);
  227. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  228. const local
  229. = (participantById && participantById.local)
  230. || (!id && pinnedParticipant && pinnedParticipant.local);
  231. sendAnalytics(createPinnedEvent(
  232. actionName,
  233. local ? 'local' : id,
  234. {
  235. local,
  236. 'participant_count': conference.getParticipantCount()
  237. }));
  238. }
  239. // The following condition prevents signaling to pin local participant and
  240. // shared videos. The logic is:
  241. // - If we have an ID, we check if the participant identified by that ID is
  242. // local or a bot/fake participant (such as with shared video).
  243. // - If we don't have an ID (i.e. no participant identified by an ID), we
  244. // check for local participant. If she's currently pinned, then this
  245. // action will unpin her and that's why we won't signal here too.
  246. let pin;
  247. if (participantById) {
  248. pin = !participantById.local && !participantById.isFakeParticipant;
  249. } else {
  250. const localParticipant = getLocalParticipant(participants);
  251. pin = !localParticipant || !localParticipant.pinned;
  252. }
  253. if (pin) {
  254. try {
  255. conference.pinParticipant(id);
  256. } catch (err) {
  257. _handleParticipantError(err);
  258. }
  259. }
  260. return next(action);
  261. }
  262. /**
  263. * Sets the audio-only flag for the current conference. When audio-only is set,
  264. * local video is muted and last N is set to 0 to avoid receiving remote video.
  265. *
  266. * @param {Store} store - The redux store in which the specified {@code action}
  267. * is being dispatched.
  268. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  269. * specified {@code action} to the specified {@code store}.
  270. * @param {Action} action - The redux action {@code SET_AUDIO_ONLY} which is
  271. * being dispatched in the specified {@code store}.
  272. * @private
  273. * @returns {Object} The value returned by {@code next(action)}.
  274. */
  275. function _setAudioOnly({ dispatch, getState }, next, action) {
  276. const { audioOnly: oldValue } = getState()['features/base/conference'];
  277. const result = next(action);
  278. const { audioOnly: newValue } = getState()['features/base/conference'];
  279. // Send analytics. We could've done it in the action creator setAudioOnly.
  280. // I don't know why it has to happen as early as possible but the analytics
  281. // were originally sent before the SET_AUDIO_ONLY action was even dispatched
  282. // in the redux store so I'm now sending the analytics as early as possible.
  283. if (oldValue !== newValue) {
  284. sendAnalytics(createAudioOnlyChangedEvent(newValue));
  285. logger.log(`Audio-only ${newValue ? 'enabled' : 'disabled'}`);
  286. }
  287. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  288. // otherwise, and the default lastN value will be chosen automatically.
  289. dispatch(setLastN(newValue ? 0 : undefined));
  290. // Mute/unmute the local video.
  291. dispatch(
  292. setVideoMuted(
  293. newValue,
  294. VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY,
  295. action.ensureVideoTrack));
  296. if (typeof APP !== 'undefined') {
  297. // TODO This should be a temporary solution that lasts only until video
  298. // tracks and all ui is moved into react/redux on the web.
  299. APP.UI.emitEvent(UIEvents.TOGGLE_AUDIO_ONLY, newValue);
  300. }
  301. return result;
  302. }
  303. /**
  304. * Sets the last N (value) of the video channel in the conference.
  305. *
  306. * @param {Store} store - The redux store in which the specified {@code action}
  307. * is being dispatched.
  308. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  309. * specified {@code action} to the specified {@code store}.
  310. * @param {Action} action - The redux action {@code SET_LASTN} which is being
  311. * dispatched in the specified {@code store}.
  312. * @private
  313. * @returns {Object} The value returned by {@code next(action)}.
  314. */
  315. function _setLastN({ getState }, next, action) {
  316. const { conference } = getState()['features/base/conference'];
  317. if (conference) {
  318. try {
  319. conference.setLastN(action.lastN);
  320. } catch (err) {
  321. console.error(`Failed to set lastN: ${err}`);
  322. }
  323. }
  324. return next(action);
  325. }
  326. /**
  327. * Sets the maximum receive video quality and will turn off audio only mode if
  328. * enabled.
  329. *
  330. * @param {Store} store - The redux store in which the specified {@code action}
  331. * is being dispatched.
  332. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  333. * specified {@code action} to the specified {@code store}.
  334. * @param {Action} action - The redux action {@code SET_RECEIVE_VIDEO_QUALITY}
  335. * which is being dispatched in the specified {@code store}.
  336. * @private
  337. * @returns {Object} The value returned by {@code next(action)}.
  338. */
  339. function _setReceiveVideoQuality({ dispatch, getState }, next, action) {
  340. const { audioOnly, conference } = getState()['features/base/conference'];
  341. if (conference) {
  342. conference.setReceiverVideoConstraint(action.receiveVideoQuality);
  343. audioOnly && dispatch(toggleAudioOnly());
  344. }
  345. return next(action);
  346. }
  347. /**
  348. * Notifies the feature {@code base/conference} that the redix action
  349. * {@link SET_ROOM} is being dispatched within a specific redux store.
  350. *
  351. * @param {Store} store - The redux store in which the specified {@code action}
  352. * is being dispatched.
  353. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  354. * specified {@code action} to the specified {@code store}.
  355. * @param {Action} action - The redux action {@code SET_ROOM} which is being
  356. * dispatched in the specified {@code store}.
  357. * @private
  358. * @returns {Object} The value returned by {@code next(action)}.
  359. */
  360. function _setRoom({ dispatch, getState }, next, action) {
  361. const result = next(action);
  362. // By the time SET_ROOM is dispatched, base/connection's locationURL and
  363. // base/conference's leaving should be the only conference-related sources
  364. // of truth.
  365. const state = getState();
  366. const { leaving } = state['features/base/conference'];
  367. const { locationURL } = state['features/base/connection'];
  368. const dispatchConferenceLeft = new Set();
  369. // Figure out which of the JitsiConferences referenced by base/conference
  370. // have not dispatched or are not likely to dispatch CONFERENCE_FAILED and
  371. // CONFERENCE_LEFT.
  372. forEachConference(state, (conference, url) => {
  373. if (conference !== leaving && url && url !== locationURL) {
  374. dispatchConferenceLeft.add(conference);
  375. }
  376. return true; // All JitsiConference instances are to be examined.
  377. });
  378. // Dispatch CONFERENCE_LEFT for the JitsiConferences referenced by
  379. // base/conference which have not dispatched or are not likely to dispatch
  380. // CONFERENCE_FAILED or CONFERENCE_LEFT.
  381. for (const conference of dispatchConferenceLeft) {
  382. dispatch(conferenceLeft(conference));
  383. }
  384. return result;
  385. }
  386. /**
  387. * Synchronizes local tracks from state with local tracks in JitsiConference
  388. * instance.
  389. *
  390. * @param {Store} store - The redux store.
  391. * @param {Object} action - Action object.
  392. * @private
  393. * @returns {Promise}
  394. */
  395. function _syncConferenceLocalTracksWithState({ getState }, action) {
  396. const state = getState()['features/base/conference'];
  397. const { conference } = state;
  398. let promise;
  399. // XXX The conference may already be in the process of being left, that's
  400. // why we should not add/remove local tracks to such conference.
  401. if (conference && conference !== state.leaving) {
  402. const track = action.track.jitsiTrack;
  403. if (action.type === TRACK_ADDED) {
  404. promise = _addLocalTracksToConference(conference, [ track ]);
  405. } else {
  406. promise = _removeLocalTracksFromConference(conference, [ track ]);
  407. }
  408. }
  409. return promise || Promise.resolve();
  410. }
  411. /**
  412. * Sets the maximum receive video quality.
  413. *
  414. * @param {Store} store - The redux store in which the specified {@code action}
  415. * is being dispatched.
  416. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  417. * specified {@code action} to the specified {@code store}.
  418. * @param {Action} action - The redux action {@code DATA_CHANNEL_STATUS_CHANGED}
  419. * which is being dispatched in the specified {@code store}.
  420. * @private
  421. * @returns {Object} The value returned by {@code next(action)}.
  422. */
  423. function _syncReceiveVideoQuality({ getState }, next, action) {
  424. const state = getState()['features/base/conference'];
  425. state.conference.setReceiverVideoConstraint(state.receiveVideoQuality);
  426. return next(action);
  427. }
  428. /**
  429. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  430. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  431. *
  432. * @param {Store} store - The redux store in which the specified {@code action}
  433. * is being dispatched.
  434. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  435. * specified {@code action} to the specified {@code store}.
  436. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  437. * {@code TRACK_REMOVED} which is being dispatched in the specified
  438. * {@code store}.
  439. * @private
  440. * @returns {Object} The value returned by {@code next(action)}.
  441. */
  442. function _trackAddedOrRemoved(store, next, action) {
  443. const track = action.track;
  444. if (track && track.local) {
  445. return (
  446. _syncConferenceLocalTracksWithState(store, action)
  447. .then(() => next(action)));
  448. }
  449. return next(action);
  450. }