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.

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