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

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