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.

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