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

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