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

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