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

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