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