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

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