Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import {
  4. ACTION_PINNED,
  5. ACTION_UNPINNED,
  6. createAudioOnlyDisableEvent,
  7. createPinnedEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { CONNECTION_ESTABLISHED } from '../connection';
  11. import { setVideoMuted, VIDEO_MUTISM_AUTHORITY } from '../media';
  12. import {
  13. getLocalParticipant,
  14. getParticipantById,
  15. getPinnedParticipant,
  16. PIN_PARTICIPANT
  17. } from '../participants';
  18. import { MiddlewareRegistry } from '../redux';
  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 new state that is the result of the reduction of the
  85. * specified action.
  86. */
  87. function _connectionEstablished(store, next, action) {
  88. const result = next(action);
  89. // FIXME: workaround for the web version. Currently the creation of the
  90. // conference is handled by /conference.js
  91. if (typeof APP === 'undefined') {
  92. store.dispatch(createConference());
  93. }
  94. return result;
  95. }
  96. /**
  97. * Does extra sync up on properties that may need to be updated after the
  98. * conference failed or was left.
  99. *
  100. * @param {Store} store - The redux store in which the specified action is being
  101. * dispatched.
  102. * @param {Dispatch} next - The redux dispatch function to dispatch the
  103. * specified action to the specified store.
  104. * @param {Action} action - The redux action {@link CONFERENCE_FAILED} or
  105. * {@link CONFERENCE_LEFT} which is being dispatched in the specified store.
  106. * @private
  107. * @returns {Object} The new state that is the result of the reduction of the
  108. * specified action.
  109. */
  110. function _conferenceFailedOrLeft({ dispatch, getState }, next, action) {
  111. const result = next(action);
  112. if (getState()['features/base/conference'].audioOnly) {
  113. sendAnalytics(createAudioOnlyDisableEvent());
  114. logger.log('Audio only disabled');
  115. dispatch(setAudioOnly(false));
  116. }
  117. return result;
  118. }
  119. /**
  120. * Does extra sync up on properties that may need to be updated after the
  121. * conference was joined.
  122. *
  123. * @param {Store} store - The redux store in which the specified action is being
  124. * dispatched.
  125. * @param {Dispatch} next - The redux dispatch function to dispatch the
  126. * specified action to the specified store.
  127. * @param {Action} action - The redux action CONFERENCE_JOINED which is being
  128. * dispatched in the specified store.
  129. * @private
  130. * @returns {Object} The new state that is the result of the reduction of the
  131. * specified action.
  132. */
  133. function _conferenceJoined(store, next, action) {
  134. const result = next(action);
  135. const { audioOnly, conference }
  136. = store.getState()['features/base/conference'];
  137. // FIXME On Web the audio only mode for "start audio only" is toggled before
  138. // conference is added to the redux store ("on conference joined" action)
  139. // and the LastN value needs to be synchronized here.
  140. if (audioOnly && conference.getLastN() !== 0) {
  141. store.dispatch(setLastN(0));
  142. }
  143. return result;
  144. }
  145. /**
  146. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  147. * dispatched within a specific redux store. Pins the specified remote
  148. * participant in the associated conference, ignores the local participant.
  149. *
  150. * @param {Store} store - The redux store in which the specified action is being
  151. * dispatched.
  152. * @param {Dispatch} next - The redux dispatch function to dispatch the
  153. * specified action to the specified store.
  154. * @param {Action} action - The redux action PIN_PARTICIPANT which is being
  155. * dispatched in the specified store.
  156. * @private
  157. * @returns {Object} The new state that is the result of the reduction of the
  158. * specified action.
  159. */
  160. function _pinParticipant(store, next, action) {
  161. const state = store.getState();
  162. const { conference } = state['features/base/conference'];
  163. if (!conference) {
  164. return next(action);
  165. }
  166. const participants = state['features/base/participants'];
  167. const id = action.participant.id;
  168. const participantById = getParticipantById(participants, id);
  169. if (typeof APP !== 'undefined') {
  170. const pinnedParticipant = getPinnedParticipant(participants);
  171. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  172. const local
  173. = (participantById && participantById.local)
  174. || (!id && pinnedParticipant && pinnedParticipant.local);
  175. sendAnalytics(createPinnedEvent(
  176. actionName,
  177. local ? 'local' : id,
  178. {
  179. local,
  180. 'participant_count': conference.getParticipantCount()
  181. }));
  182. }
  183. // The following condition prevents signaling to pin local participant and
  184. // shared videos. The logic is:
  185. // - If we have an ID, we check if the participant identified by that ID is
  186. // local or a bot/fake participant (such as with shared video).
  187. // - If we don't have an ID (i.e. no participant identified by an ID), we
  188. // check for local participant. If she's currently pinned, then this
  189. // action will unpin her and that's why we won't signal here too.
  190. let pin;
  191. if (participantById) {
  192. pin = !participantById.local && !participantById.isBot;
  193. } else {
  194. const localParticipant = getLocalParticipant(participants);
  195. pin = !localParticipant || !localParticipant.pinned;
  196. }
  197. if (pin) {
  198. try {
  199. conference.pinParticipant(id);
  200. } catch (err) {
  201. _handleParticipantError(err);
  202. }
  203. }
  204. return next(action);
  205. }
  206. /**
  207. * Sets the audio-only flag for the current conference. When audio-only is set,
  208. * local video is muted and last N is set to 0 to avoid receiving remote video.
  209. *
  210. * @param {Store} store - The redux store in which the specified action is being
  211. * dispatched.
  212. * @param {Dispatch} next - The redux dispatch function to dispatch the
  213. * specified action to the specified store.
  214. * @param {Action} action - The redux action SET_AUDIO_ONLY which is being
  215. * dispatched in the specified store.
  216. * @private
  217. * @returns {Object} The new state that is the result of the reduction of the
  218. * specified action.
  219. */
  220. function _setAudioOnly({ dispatch, getState }, next, action) {
  221. const result = next(action);
  222. const { audioOnly } = getState()['features/base/conference'];
  223. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  224. // otherwise, and the default lastN value will be chosen automatically.
  225. dispatch(setLastN(audioOnly ? 0 : undefined));
  226. // Mute/unmute the local video.
  227. dispatch(
  228. setVideoMuted(
  229. audioOnly,
  230. VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY,
  231. /* ensureTrack */ true));
  232. if (typeof APP !== 'undefined') {
  233. // TODO This should be a temporary solution that lasts only until
  234. // video tracks and all ui is moved into react/redux on the web.
  235. APP.UI.emitEvent(UIEvents.TOGGLE_AUDIO_ONLY, audioOnly);
  236. }
  237. return result;
  238. }
  239. /**
  240. * Sets the last N (value) of the video channel in the conference.
  241. *
  242. * @param {Store} store - The redux store in which the specified action is being
  243. * dispatched.
  244. * @param {Dispatch} next - The redux dispatch function to dispatch the
  245. * specified action to the specified store.
  246. * @param {Action} action - The redux action SET_LASTN which is being dispatched
  247. * in the specified store.
  248. * @private
  249. * @returns {Object} The new state that is the result of the reduction of the
  250. * specified action.
  251. */
  252. function _setLastN(store, next, action) {
  253. const { conference } = store.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 new state that is the result of the reduction of the
  275. * specified action.
  276. */
  277. function _setReceiveVideoQuality({ dispatch, getState }, next, action) {
  278. const { audioOnly, conference } = getState()['features/base/conference'];
  279. if (conference) {
  280. conference.setReceiverVideoConstraint(action.receiveVideoQuality);
  281. if (audioOnly) {
  282. dispatch(toggleAudioOnly());
  283. }
  284. }
  285. return next(action);
  286. }
  287. /**
  288. * Synchronizes local tracks from state with local tracks in JitsiConference
  289. * instance.
  290. *
  291. * @param {Store} store - The redux store.
  292. * @param {Object} action - Action object.
  293. * @private
  294. * @returns {Promise}
  295. */
  296. function _syncConferenceLocalTracksWithState({ getState }, action) {
  297. const state = getState()['features/base/conference'];
  298. const { conference } = state;
  299. let promise;
  300. // XXX The conference may already be in the process of being left, that's
  301. // why we should not add/remove local tracks to such conference.
  302. if (conference && conference !== state.leaving) {
  303. const track = action.track.jitsiTrack;
  304. if (action.type === TRACK_ADDED) {
  305. promise = _addLocalTracksToConference(conference, [ track ]);
  306. } else {
  307. promise = _removeLocalTracksFromConference(conference, [ track ]);
  308. }
  309. }
  310. return promise || Promise.resolve();
  311. }
  312. /**
  313. * Sets the maximum receive video quality.
  314. *
  315. * @param {Store} store - The Redux store in which the specified action is being
  316. * dispatched.
  317. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  318. * specified action to the specified store.
  319. * @param {Action} action - The Redux action DATA_CHANNEL_STATUS_CHANGED which
  320. * is being dispatched in the specified store.
  321. * @private
  322. * @returns {Object} The new state that is the result of the reduction of the
  323. * specified action.
  324. */
  325. function _syncReceiveVideoQuality({ getState }, next, action) {
  326. const state = getState()['features/base/conference'];
  327. state.conference.setReceiverVideoConstraint(state.receiveVideoQuality);
  328. return next(action);
  329. }
  330. /**
  331. * Notifies the feature base/conference that the action TRACK_ADDED
  332. * or TRACK_REMOVED is being dispatched within a specific redux store.
  333. *
  334. * @param {Store} store - The redux store in which the specified action is being
  335. * dispatched.
  336. * @param {Dispatch} next - The redux dispatch function to dispatch the
  337. * specified action to the specified store.
  338. * @param {Action} action - The redux action TRACK_ADDED or TRACK_REMOVED which
  339. * is being dispatched in the specified store.
  340. * @private
  341. * @returns {Object} The new state that is the result of the reduction of the
  342. * specified action.
  343. */
  344. function _trackAddedOrRemoved(store, next, action) {
  345. const track = action.track;
  346. if (track && track.local) {
  347. return (
  348. _syncConferenceLocalTracksWithState(store, action)
  349. .then(() => next(action)));
  350. }
  351. return next(action);
  352. }