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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import {
  4. ACTION_PINNED,
  5. ACTION_UNPINNED,
  6. createAudioOnlyDisableEvent,
  7. createPinnedEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { CONNECTION_ESTABLISHED } from '../connection';
  11. import { setVideoMuted, VIDEO_MUTISM_AUTHORITY } from '../media';
  12. import {
  13. getLocalParticipant,
  14. getParticipantById,
  15. getPinnedParticipant,
  16. PIN_PARTICIPANT
  17. } from '../participants';
  18. import { MiddlewareRegistry } from '../redux';
  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 new state that is the result of the reduction of the
  85. * specified action.
  86. */
  87. function _connectionEstablished(store, next, action) {
  88. const result = next(action);
  89. // FIXME: workaround for the web version. Currently the creation of the
  90. // conference is handled by /conference.js
  91. if (typeof APP === 'undefined') {
  92. store.dispatch(createConference());
  93. }
  94. return result;
  95. }
  96. /**
  97. * Does extra sync up on properties that may need to be updated after the
  98. * conference failed or was left.
  99. *
  100. * @param {Store} store - The redux store in which the specified action is being
  101. * dispatched.
  102. * @param {Dispatch} next - The redux dispatch function to dispatch the
  103. * specified action to the specified store.
  104. * @param {Action} action - The redux action {@link CONFERENCE_FAILED} or
  105. * {@link CONFERENCE_LEFT} which is being dispatched in the specified store.
  106. * @private
  107. * @returns {Object} The new state that is the result of the reduction of the
  108. * specified action.
  109. */
  110. function _conferenceFailedOrLeft({ dispatch, getState }, next, action) {
  111. const result = next(action);
  112. if (getState()['features/base/conference'].audioOnly) {
  113. sendAnalytics(createAudioOnlyDisableEvent());
  114. logger.log('Audio only disabled');
  115. dispatch(setAudioOnly(false));
  116. }
  117. return result;
  118. }
  119. /**
  120. * Does extra sync up on properties that may need to be updated after the
  121. * conference was joined.
  122. *
  123. * @param {Store} store - The redux store in which the specified action is being
  124. * dispatched.
  125. * @param {Dispatch} next - The redux dispatch function to dispatch the
  126. * specified action to the specified store.
  127. * @param {Action} action - The redux action CONFERENCE_JOINED which is being
  128. * dispatched in the specified store.
  129. * @private
  130. * @returns {Object} The new state that is the result of the reduction of the
  131. * specified action.
  132. */
  133. function _conferenceJoined(store, next, action) {
  134. const result = next(action);
  135. const { audioOnly, conference }
  136. = store.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. if (audioOnly && conference.getLastN() !== 0) {
  141. store.dispatch(setLastN(0));
  142. }
  143. return result;
  144. }
  145. /**
  146. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  147. * dispatched within a specific redux store. Pins the specified remote
  148. * participant in the associated conference, ignores the local participant.
  149. *
  150. * @param {Store} store - The redux store in which the specified action is being
  151. * dispatched.
  152. * @param {Dispatch} next - The redux dispatch function to dispatch the
  153. * specified action to the specified store.
  154. * @param {Action} action - The redux action PIN_PARTICIPANT which is being
  155. * dispatched in the specified store.
  156. * @private
  157. * @returns {Object} The new state that is the result of the reduction of the
  158. * specified action.
  159. */
  160. function _pinParticipant(store, next, action) {
  161. const state = store.getState();
  162. const { conference } = state['features/base/conference'];
  163. const participants = state['features/base/participants'];
  164. const id = action.participant.id;
  165. const participantById = getParticipantById(participants, id);
  166. let pin;
  167. if (typeof APP !== 'undefined') {
  168. const pinnedParticipant = getPinnedParticipant(participants);
  169. const actionName
  170. = action.participant.id ? ACTION_PINNED : ACTION_UNPINNED;
  171. const local = (participantById && participantById.local)
  172. || (!id && pinnedParticipant && pinnedParticipant.local);
  173. sendAnalytics(createPinnedEvent(
  174. actionName,
  175. local ? 'local' : id,
  176. {
  177. 'participant_count': conference.getParticipantCount(),
  178. local
  179. }));
  180. }
  181. // The following condition prevents signaling to pin local participant and
  182. // shared videos. The logic is:
  183. // - If we have an ID, we check if the participant identified by that ID is
  184. // local or a bot/fake participant (such as with shared video).
  185. // - If we don't have an ID (i.e. no participant identified by an ID), we
  186. // check for local participant. If she's currently pinned, then this
  187. // action will unpin her and that's why we won't signal here too.
  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 new state that is the result of the reduction of the
  215. * specified action.
  216. */
  217. function _setAudioOnly({ dispatch, getState }, next, action) {
  218. const result = next(action);
  219. const { audioOnly } = getState()['features/base/conference'];
  220. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  221. // otherwise, and the default lastN value will be chosen automatically.
  222. dispatch(setLastN(audioOnly ? 0 : undefined));
  223. // Mute/unmute the local video.
  224. dispatch(
  225. setVideoMuted(
  226. audioOnly,
  227. VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY,
  228. /* ensureTrack */ true));
  229. if (typeof APP !== 'undefined') {
  230. // TODO This should be a temporary solution that lasts only until
  231. // video tracks and all ui is moved into react/redux on the web.
  232. APP.UI.emitEvent(UIEvents.TOGGLE_AUDIO_ONLY, audioOnly);
  233. }
  234. return result;
  235. }
  236. /**
  237. * Sets the last N (value) of the video channel in the conference.
  238. *
  239. * @param {Store} store - The redux store in which the specified action is being
  240. * dispatched.
  241. * @param {Dispatch} next - The redux dispatch function to dispatch the
  242. * specified action to the specified store.
  243. * @param {Action} action - The redux action SET_LASTN which is being dispatched
  244. * in the specified store.
  245. * @private
  246. * @returns {Object} The new state that is the result of the reduction of the
  247. * specified action.
  248. */
  249. function _setLastN(store, next, action) {
  250. const { conference } = store.getState()['features/base/conference'];
  251. if (conference) {
  252. try {
  253. conference.setLastN(action.lastN);
  254. } catch (err) {
  255. console.error(`Failed to set lastN: ${err}`);
  256. }
  257. }
  258. return next(action);
  259. }
  260. /**
  261. * Sets the maximum receive video quality and will turn off audio only mode if
  262. * enabled.
  263. *
  264. * @param {Store} store - The Redux store in which the specified action is being
  265. * dispatched.
  266. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  267. * specified action to the specified store.
  268. * @param {Action} action - The Redux action SET_RECEIVE_VIDEO_QUALITY which is
  269. * being dispatched in the specified store.
  270. * @private
  271. * @returns {Object} The new state that is the result of the reduction of the
  272. * specified action.
  273. */
  274. function _setReceiveVideoQuality({ dispatch, getState }, next, action) {
  275. const { audioOnly, conference } = getState()['features/base/conference'];
  276. conference.setReceiverVideoConstraint(action.receiveVideoQuality);
  277. if (audioOnly) {
  278. dispatch(toggleAudioOnly());
  279. }
  280. return next(action);
  281. }
  282. /**
  283. * Synchronizes local tracks from state with local tracks in JitsiConference
  284. * instance.
  285. *
  286. * @param {Store} store - The redux store.
  287. * @param {Object} action - Action object.
  288. * @private
  289. * @returns {Promise}
  290. */
  291. function _syncConferenceLocalTracksWithState({ getState }, action) {
  292. const state = getState()['features/base/conference'];
  293. const { conference } = state;
  294. let promise;
  295. // XXX The conference may already be in the process of being left, that's
  296. // why we should not add/remove local tracks to such conference.
  297. if (conference && conference !== state.leaving) {
  298. const track = action.track.jitsiTrack;
  299. if (action.type === TRACK_ADDED) {
  300. promise = _addLocalTracksToConference(conference, [ track ]);
  301. } else {
  302. promise = _removeLocalTracksFromConference(conference, [ track ]);
  303. }
  304. }
  305. return promise || Promise.resolve();
  306. }
  307. /**
  308. * Sets the maximum receive video quality.
  309. *
  310. * @param {Store} store - The Redux store in which the specified action is being
  311. * dispatched.
  312. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  313. * specified action to the specified store.
  314. * @param {Action} action - The Redux action DATA_CHANNEL_STATUS_CHANGED which
  315. * is being dispatched in the specified store.
  316. * @private
  317. * @returns {Object} The new state that is the result of the reduction of the
  318. * specified action.
  319. */
  320. function _syncReceiveVideoQuality({ getState }, next, action) {
  321. const state = getState()['features/base/conference'];
  322. state.conference.setReceiverVideoConstraint(state.receiveVideoQuality);
  323. return next(action);
  324. }
  325. /**
  326. * Notifies the feature base/conference that the action TRACK_ADDED
  327. * or TRACK_REMOVED is being dispatched within a specific redux store.
  328. *
  329. * @param {Store} store - The redux store in which the specified action is being
  330. * dispatched.
  331. * @param {Dispatch} next - The redux dispatch function to dispatch the
  332. * specified action to the specified store.
  333. * @param {Action} action - The redux action TRACK_ADDED or TRACK_REMOVED which
  334. * is being dispatched in the specified store.
  335. * @private
  336. * @returns {Object} The new state that is the result of the reduction of the
  337. * specified action.
  338. */
  339. function _trackAddedOrRemoved(store, next, action) {
  340. const track = action.track;
  341. if (track && track.local) {
  342. return (
  343. _syncConferenceLocalTracksWithState(store, action)
  344. .then(() => next(action)));
  345. }
  346. return next(action);
  347. }