您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. conferenceLeft,
  22. createConference,
  23. setLastN,
  24. toggleAudioOnly
  25. } from './actions';
  26. import {
  27. CONFERENCE_JOINED,
  28. DATA_CHANNEL_OPENED,
  29. SET_AUDIO_ONLY,
  30. SET_LASTN,
  31. SET_RECEIVE_VIDEO_QUALITY,
  32. SET_ROOM
  33. } from './actionTypes';
  34. import { JITSI_CONFERENCE_URL_KEY } from './constants';
  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_JOINED:
  53. return _conferenceJoined(store, next, action);
  54. case DATA_CHANNEL_OPENED:
  55. return _syncReceiveVideoQuality(store, next, action);
  56. case PIN_PARTICIPANT:
  57. return _pinParticipant(store, next, action);
  58. case SET_AUDIO_ONLY:
  59. return _setAudioOnly(store, next, action);
  60. case SET_LASTN:
  61. return _setLastN(store, next, action);
  62. case SET_RECEIVE_VIDEO_QUALITY:
  63. return _setReceiveVideoQuality(store, next, action);
  64. case SET_ROOM:
  65. return _setRoom(store, next, action);
  66. case TRACK_ADDED:
  67. case TRACK_REMOVED:
  68. return _trackAddedOrRemoved(store, next, action);
  69. }
  70. return next(action);
  71. });
  72. /**
  73. * Notifies the feature base/conference that the action CONNECTION_ESTABLISHED
  74. * is being dispatched within a specific redux store.
  75. *
  76. * @param {Store} store - The redux store in which the specified action is being
  77. * dispatched.
  78. * @param {Dispatch} next - The redux dispatch function to dispatch the
  79. * specified action to the specified store.
  80. * @param {Action} action - The redux action CONNECTION_ESTABLISHED which is
  81. * being dispatched in the specified store.
  82. * @private
  83. * @returns {Object} The value returned by {@code next(action)}.
  84. */
  85. function _connectionEstablished({ dispatch }, next, action) {
  86. const result = next(action);
  87. // FIXME: workaround for the web version. Currently the creation of the
  88. // conference is handled by /conference.js
  89. if (typeof APP === 'undefined') {
  90. dispatch(createConference());
  91. }
  92. return result;
  93. }
  94. /**
  95. * Does extra sync up on properties that may need to be updated after the
  96. * conference was joined.
  97. *
  98. * @param {Store} store - The redux store in which the specified action is being
  99. * dispatched.
  100. * @param {Dispatch} next - The redux dispatch function to dispatch the
  101. * specified action to the specified store.
  102. * @param {Action} action - The redux action CONFERENCE_JOINED which is being
  103. * dispatched in the specified store.
  104. * @private
  105. * @returns {Object} The value returned by {@code next(action)}.
  106. */
  107. function _conferenceJoined({ dispatch, getState }, next, action) {
  108. const result = next(action);
  109. const { audioOnly, conference } = getState()['features/base/conference'];
  110. // FIXME On Web the audio only mode for "start audio only" is toggled before
  111. // conference is added to the redux store ("on conference joined" action)
  112. // and the LastN value needs to be synchronized here.
  113. audioOnly && (conference.getLastN() !== 0) && dispatch(setLastN(0));
  114. return result;
  115. }
  116. /**
  117. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  118. * dispatched within a specific redux store. Pins the specified remote
  119. * participant in the associated conference, ignores the local participant.
  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 PIN_PARTICIPANT which is being
  126. * dispatched in the specified store.
  127. * @private
  128. * @returns {Object} The value returned by {@code next(action)}.
  129. */
  130. function _pinParticipant({ getState }, next, action) {
  131. const state = getState();
  132. const { conference } = state['features/base/conference'];
  133. if (!conference) {
  134. return next(action);
  135. }
  136. const participants = state['features/base/participants'];
  137. const id = action.participant.id;
  138. const participantById = getParticipantById(participants, id);
  139. if (typeof APP !== 'undefined') {
  140. const pinnedParticipant = getPinnedParticipant(participants);
  141. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  142. const local
  143. = (participantById && participantById.local)
  144. || (!id && pinnedParticipant && pinnedParticipant.local);
  145. sendAnalytics(createPinnedEvent(
  146. actionName,
  147. local ? 'local' : id,
  148. {
  149. local,
  150. 'participant_count': conference.getParticipantCount()
  151. }));
  152. }
  153. // The following condition prevents signaling to pin local participant and
  154. // shared videos. The logic is:
  155. // - If we have an ID, we check if the participant identified by that ID is
  156. // local or a bot/fake participant (such as with shared video).
  157. // - If we don't have an ID (i.e. no participant identified by an ID), we
  158. // check for local participant. If she's currently pinned, then this
  159. // action will unpin her and that's why we won't signal here too.
  160. let pin;
  161. if (participantById) {
  162. pin = !participantById.local && !participantById.isBot;
  163. } else {
  164. const localParticipant = getLocalParticipant(participants);
  165. pin = !localParticipant || !localParticipant.pinned;
  166. }
  167. if (pin) {
  168. try {
  169. conference.pinParticipant(id);
  170. } catch (err) {
  171. _handleParticipantError(err);
  172. }
  173. }
  174. return next(action);
  175. }
  176. /**
  177. * Sets the audio-only flag for the current conference. When audio-only is set,
  178. * local video is muted and last N is set to 0 to avoid receiving remote video.
  179. *
  180. * @param {Store} store - The redux store in which the specified action is being
  181. * dispatched.
  182. * @param {Dispatch} next - The redux dispatch function to dispatch the
  183. * specified action to the specified store.
  184. * @param {Action} action - The redux action SET_AUDIO_ONLY which is being
  185. * dispatched in the specified store.
  186. * @private
  187. * @returns {Object} The value returned by {@code next(action)}.
  188. */
  189. function _setAudioOnly({ dispatch, getState }, next, action) {
  190. const { audioOnly: oldValue } = getState()['features/base/conference'];
  191. const result = next(action);
  192. const { audioOnly: newValue } = getState()['features/base/conference'];
  193. // Send analytics. We could've done it in the action creator setAudioOnly.
  194. // I don't know why it has to happen as early as possible but the analytics
  195. // were originally sent before the SET_AUDIO_ONLY action was even dispatched
  196. // in the redux store so I'm now sending the analytics as early as possible.
  197. if (oldValue !== newValue) {
  198. sendAnalytics(createAudioOnlyChangedEvent(newValue));
  199. logger.log(`Audio-only ${newValue ? 'enabled' : 'disabled'}`);
  200. }
  201. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  202. // otherwise, and the default lastN value will be chosen automatically.
  203. dispatch(setLastN(newValue ? 0 : undefined));
  204. // Mute/unmute the local video.
  205. dispatch(
  206. setVideoMuted(
  207. newValue,
  208. VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY,
  209. /* ensureTrack */ true));
  210. if (typeof APP !== 'undefined') {
  211. // TODO This should be a temporary solution that lasts only until
  212. // video tracks and all ui is moved into react/redux on the web.
  213. APP.UI.emitEvent(UIEvents.TOGGLE_AUDIO_ONLY, newValue);
  214. }
  215. return result;
  216. }
  217. /**
  218. * Sets the last N (value) of the video channel in the conference.
  219. *
  220. * @param {Store} store - The redux store in which the specified action is being
  221. * dispatched.
  222. * @param {Dispatch} next - The redux dispatch function to dispatch the
  223. * specified action to the specified store.
  224. * @param {Action} action - The redux action SET_LASTN which is being dispatched
  225. * in the specified store.
  226. * @private
  227. * @returns {Object} The value returned by {@code next(action)}.
  228. */
  229. function _setLastN({ getState }, next, action) {
  230. const { conference } = getState()['features/base/conference'];
  231. if (conference) {
  232. try {
  233. conference.setLastN(action.lastN);
  234. } catch (err) {
  235. console.error(`Failed to set lastN: ${err}`);
  236. }
  237. }
  238. return next(action);
  239. }
  240. /**
  241. * Sets the maximum receive video quality and will turn off audio only mode if
  242. * enabled.
  243. *
  244. * @param {Store} store - The redux store in which the specified action is being
  245. * dispatched.
  246. * @param {Dispatch} next - The redux dispatch function to dispatch the
  247. * specified action to the specified store.
  248. * @param {Action} action - The redux action SET_RECEIVE_VIDEO_QUALITY which is
  249. * being dispatched in the specified store.
  250. * @private
  251. * @returns {Object} The value returned by {@code next(action)}.
  252. */
  253. function _setReceiveVideoQuality({ dispatch, getState }, next, action) {
  254. const { audioOnly, conference } = getState()['features/base/conference'];
  255. if (conference) {
  256. conference.setReceiverVideoConstraint(action.receiveVideoQuality);
  257. audioOnly && dispatch(toggleAudioOnly());
  258. }
  259. return next(action);
  260. }
  261. /**
  262. * Notifies the feature {@code base/conference} that the redix action
  263. * {@link SET_ROOM} is being dispatched within a specific redux store.
  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 {@code SET_ROOM} which is being
  270. * dispatched in the specified store.
  271. * @private
  272. * @returns {Object} The value returned by {@code next(action)}.
  273. */
  274. function _setRoom({ dispatch, getState }, next, action) {
  275. const result = next(action);
  276. // By the time SET_ROOM is dispatched, base/connection's locationURL and
  277. // base/conference's leaving should be the only conference-related sources
  278. // of truth.
  279. const state = getState();
  280. const {
  281. leaving,
  282. ...stateFeaturesBaseConference
  283. } = state['features/base/conference'];
  284. const { locationURL } = state['features/base/connection'];
  285. const dispatchConferenceLeft = new Set();
  286. // Figure out which of the JitsiConferences referenced by base/conference
  287. // have not dispatched or are not likely to dispatch CONFERENCE_FAILED and
  288. // CONFERENCE_LEFT.
  289. // eslint-disable-next-line guard-for-in
  290. for (const p in stateFeaturesBaseConference) {
  291. const v = stateFeaturesBaseConference[p];
  292. // Does the value of the base/conference's property look like a
  293. // JitsiConference?
  294. if (v && typeof v === 'object') {
  295. const url = v[JITSI_CONFERENCE_URL_KEY];
  296. if (url && v !== leaving && url !== locationURL) {
  297. dispatchConferenceLeft.add(v);
  298. }
  299. }
  300. }
  301. // Dispatch CONFERENCE_LEFT for the JitsiConferences referenced by
  302. // base/conference which have not dispatched or are not likely to dispatch
  303. // CONFERENCE_FAILED or CONFERENCE_LEFT.
  304. for (const conference of dispatchConferenceLeft) {
  305. dispatch(conferenceLeft(conference));
  306. }
  307. return result;
  308. }
  309. /**
  310. * Synchronizes local tracks from state with local tracks in JitsiConference
  311. * instance.
  312. *
  313. * @param {Store} store - The redux store.
  314. * @param {Object} action - Action object.
  315. * @private
  316. * @returns {Promise}
  317. */
  318. function _syncConferenceLocalTracksWithState({ getState }, action) {
  319. const state = getState()['features/base/conference'];
  320. const { conference } = state;
  321. let promise;
  322. // XXX The conference may already be in the process of being left, that's
  323. // why we should not add/remove local tracks to such conference.
  324. if (conference && conference !== state.leaving) {
  325. const track = action.track.jitsiTrack;
  326. if (action.type === TRACK_ADDED) {
  327. promise = _addLocalTracksToConference(conference, [ track ]);
  328. } else {
  329. promise = _removeLocalTracksFromConference(conference, [ track ]);
  330. }
  331. }
  332. return promise || Promise.resolve();
  333. }
  334. /**
  335. * Sets the maximum receive video quality.
  336. *
  337. * @param {Store} store - The redux store in which the specified action is being
  338. * dispatched.
  339. * @param {Dispatch} next - The redux dispatch function to dispatch the
  340. * specified action to the specified store.
  341. * @param {Action} action - The redux action DATA_CHANNEL_STATUS_CHANGED which
  342. * is being dispatched in the specified store.
  343. * @private
  344. * @returns {Object} The value returned by {@code next(action)}.
  345. */
  346. function _syncReceiveVideoQuality({ getState }, next, action) {
  347. const state = getState()['features/base/conference'];
  348. state.conference.setReceiverVideoConstraint(state.receiveVideoQuality);
  349. return next(action);
  350. }
  351. /**
  352. * Notifies the feature base/conference that the action TRACK_ADDED
  353. * or TRACK_REMOVED is being dispatched within a specific redux store.
  354. *
  355. * @param {Store} store - The redux store in which the specified action is being
  356. * dispatched.
  357. * @param {Dispatch} next - The redux dispatch function to dispatch the
  358. * specified action to the specified store.
  359. * @param {Action} action - The redux action TRACK_ADDED or TRACK_REMOVED which
  360. * is being dispatched in the specified store.
  361. * @private
  362. * @returns {Object} The value returned by {@code next(action)}.
  363. */
  364. function _trackAddedOrRemoved(store, next, action) {
  365. const track = action.track;
  366. if (track && track.local) {
  367. return (
  368. _syncConferenceLocalTracksWithState(store, action)
  369. .then(() => next(action)));
  370. }
  371. return next(action);
  372. }