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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // @flow
  2. import { Alert, NativeModules, Platform } from 'react-native';
  3. import { v4 as uuidv4 } from 'uuid';
  4. import { createTrackMutedEvent, sendAnalytics } from '../../analytics';
  5. import { appNavigate } from '../../app/actions';
  6. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../base/app';
  7. import { SET_AUDIO_ONLY } from '../../base/audio-only';
  8. import {
  9. CONFERENCE_FAILED,
  10. CONFERENCE_JOINED,
  11. CONFERENCE_LEFT,
  12. CONFERENCE_WILL_JOIN,
  13. CONFERENCE_WILL_LEAVE,
  14. getConferenceName,
  15. getCurrentConference
  16. } from '../../base/conference';
  17. import { getInviteURL } from '../../base/connection';
  18. import {
  19. MEDIA_TYPE,
  20. isVideoMutedByAudioOnly,
  21. setAudioMuted
  22. } from '../../base/media';
  23. import { MiddlewareRegistry } from '../../base/redux';
  24. import {
  25. TRACK_ADDED,
  26. TRACK_REMOVED,
  27. TRACK_UPDATED,
  28. isLocalTrackMuted
  29. } from '../../base/tracks';
  30. import CallKit from './CallKit';
  31. import ConnectionService from './ConnectionService';
  32. import { _SET_CALL_INTEGRATION_SUBSCRIPTIONS } from './actionTypes';
  33. import { isCallIntegrationEnabled } from './functions';
  34. const { AudioMode } = NativeModules;
  35. const CallIntegration = CallKit || ConnectionService;
  36. /**
  37. * Middleware that captures system actions and hooks up CallKit.
  38. *
  39. * @param {Store} store - The redux store.
  40. * @returns {Function}
  41. */
  42. CallIntegration && MiddlewareRegistry.register(store => next => action => {
  43. switch (action.type) {
  44. case _SET_CALL_INTEGRATION_SUBSCRIPTIONS:
  45. return _setCallKitSubscriptions(store, next, action);
  46. case APP_WILL_MOUNT:
  47. return _appWillMount(store, next, action);
  48. case APP_WILL_UNMOUNT:
  49. store.dispatch({
  50. type: _SET_CALL_INTEGRATION_SUBSCRIPTIONS,
  51. subscriptions: undefined
  52. });
  53. break;
  54. case CONFERENCE_FAILED:
  55. return _conferenceFailed(store, next, action);
  56. case CONFERENCE_JOINED:
  57. return _conferenceJoined(store, next, action);
  58. // If a conference is being left in a graceful manner then
  59. // the CONFERENCE_WILL_LEAVE fires as soon as the conference starts
  60. // disconnecting. We need to destroy the call on the native side as soon
  61. // as possible, because the disconnection process is asynchronous and
  62. // Android not always supports two simultaneous calls at the same time
  63. // (even though it should according to the spec).
  64. case CONFERENCE_LEFT:
  65. case CONFERENCE_WILL_LEAVE:
  66. return _conferenceLeft(store, next, action);
  67. case CONFERENCE_WILL_JOIN:
  68. return _conferenceWillJoin(store, next, action);
  69. case SET_AUDIO_ONLY:
  70. return _setAudioOnly(store, next, action);
  71. case TRACK_ADDED:
  72. case TRACK_REMOVED:
  73. case TRACK_UPDATED:
  74. return _syncTrackState(store, next, action);
  75. }
  76. return next(action);
  77. });
  78. /**
  79. * Notifies the feature callkit that the action {@link APP_WILL_MOUNT} is being
  80. * dispatched within a specific redux {@code store}.
  81. *
  82. * @param {Store} store - The redux store in which the specified {@code action}
  83. * is being dispatched.
  84. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  85. * specified {@code action} in the specified {@code store}.
  86. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  87. * being dispatched in the specified {@code store}.
  88. * @private
  89. * @returns {*} The value returned by {@code next(action)}.
  90. */
  91. function _appWillMount({ dispatch, getState }, next, action) {
  92. const result = next(action);
  93. const context = {
  94. dispatch,
  95. getState
  96. };
  97. const delegate = {
  98. _onPerformSetMutedCallAction,
  99. _onPerformEndCallAction
  100. };
  101. if (isCallIntegrationEnabled(getState)) {
  102. const subscriptions = CallIntegration.registerSubscriptions(context, delegate);
  103. subscriptions && dispatch({
  104. type: _SET_CALL_INTEGRATION_SUBSCRIPTIONS,
  105. subscriptions
  106. });
  107. }
  108. return result;
  109. }
  110. /**
  111. * Notifies the feature callkit that the action {@link CONFERENCE_FAILED} is
  112. * being dispatched within a specific redux {@code store}.
  113. *
  114. * @param {Store} store - The redux store in which the specified {@code action}
  115. * is being dispatched.
  116. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  117. * specified {@code action} in the specified {@code store}.
  118. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  119. * being dispatched in the specified {@code store}.
  120. * @private
  121. * @returns {*} The value returned by {@code next(action)}.
  122. */
  123. function _conferenceFailed({ getState }, next, action) {
  124. const result = next(action);
  125. if (!isCallIntegrationEnabled(getState)) {
  126. return result;
  127. }
  128. // XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
  129. // prevented the user from joining a specific conference but the app may be
  130. // able to eventually join the conference.
  131. if (!action.error.recoverable) {
  132. const { callUUID } = action.conference;
  133. if (callUUID) {
  134. delete action.conference.callUUID;
  135. CallIntegration.reportCallFailed(callUUID);
  136. }
  137. }
  138. return result;
  139. }
  140. /**
  141. * Notifies the feature callkit that the action {@link CONFERENCE_JOINED} is
  142. * being dispatched within a specific redux {@code store}.
  143. *
  144. * @param {Store} store - The redux store in which the specified {@code action}
  145. * is being dispatched.
  146. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  147. * specified {@code action} in the specified {@code store}.
  148. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  149. * being dispatched in the specified {@code store}.
  150. * @private
  151. * @returns {*} The value returned by {@code next(action)}.
  152. */
  153. function _conferenceJoined({ getState }, next, action) {
  154. const result = next(action);
  155. if (!isCallIntegrationEnabled(getState)) {
  156. return result;
  157. }
  158. const { callUUID } = action.conference;
  159. if (callUUID) {
  160. CallIntegration.reportConnectedOutgoingCall(callUUID)
  161. .then(() => {
  162. // iOS 13 doesn't like the mute state to be false before the call is started
  163. // so we update it here in case the user selected startWithAudioMuted.
  164. if (Platform.OS === 'ios') {
  165. _updateCallIntegrationMuted(action.conference, getState());
  166. }
  167. })
  168. .catch(() => {
  169. // Currently errors here are only emitted by Android.
  170. //
  171. // Some Samsung devices will fail to fully engage ConnectionService if no SIM card
  172. // was ever installed on the device. We could check for it, but it would require
  173. // the CALL_PHONE permission, which is not something we want to do, so fallback to
  174. // not using ConnectionService.
  175. _handleConnectionServiceFailure(getState());
  176. });
  177. }
  178. return result;
  179. }
  180. /**
  181. * Notifies the feature callkit that the action {@link CONFERENCE_LEFT} is being
  182. * dispatched within a specific redux {@code store}.
  183. *
  184. * @param {Store} store - The redux store in which the specified {@code action}
  185. * is being dispatched.
  186. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  187. * specified {@code action} in the specified {@code store}.
  188. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} which is
  189. * being dispatched in the specified {@code store}.
  190. * @private
  191. * @returns {*} The value returned by {@code next(action)}.
  192. */
  193. function _conferenceLeft({ getState }, next, action) {
  194. const result = next(action);
  195. if (!isCallIntegrationEnabled(getState)) {
  196. return result;
  197. }
  198. const { callUUID } = action.conference;
  199. if (callUUID) {
  200. delete action.conference.callUUID;
  201. CallIntegration.endCall(callUUID);
  202. }
  203. return result;
  204. }
  205. /**
  206. * Notifies the feature callkit that the action {@link CONFERENCE_WILL_JOIN} is
  207. * being dispatched within a specific redux {@code store}.
  208. *
  209. * @param {Store} store - The redux store in which the specified {@code action}
  210. * is being dispatched.
  211. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  212. * specified {@code action} in the specified {@code store}.
  213. * @param {Action} action - The redux action {@code CONFERENCE_WILL_JOIN} which
  214. * is being dispatched in the specified {@code store}.
  215. * @private
  216. * @returns {*} The value returned by {@code next(action)}.
  217. */
  218. function _conferenceWillJoin({ dispatch, getState }, next, action) {
  219. const result = next(action);
  220. if (!isCallIntegrationEnabled(getState)) {
  221. return result;
  222. }
  223. const { conference } = action;
  224. const state = getState();
  225. const { callHandle, callUUID } = state['features/base/config'];
  226. const url = getInviteURL(state);
  227. const handle = callHandle || url.toString();
  228. const hasVideo = !isVideoMutedByAudioOnly(state);
  229. // If we already have a callUUID set, don't start a new call.
  230. if (conference.callUUID) {
  231. return result;
  232. }
  233. // When assigning the call UUID, do so in upper case, since iOS will return
  234. // it upper cased.
  235. conference.callUUID = (callUUID || uuidv4()).toUpperCase();
  236. CallIntegration.startCall(conference.callUUID, handle, hasVideo)
  237. .then(() => {
  238. const displayName = getConferenceName(state);
  239. CallIntegration.updateCall(
  240. conference.callUUID,
  241. {
  242. displayName,
  243. hasVideo
  244. });
  245. // iOS 13 doesn't like the mute state to be false before the call is started
  246. // so delay it until the conference was joined.
  247. if (Platform.OS !== 'ios') {
  248. _updateCallIntegrationMuted(conference, state);
  249. }
  250. })
  251. .catch(error => {
  252. // Currently this error codes are emitted only by Android.
  253. //
  254. if (error.code === 'CREATE_OUTGOING_CALL_FAILED') {
  255. // We're not tracking the call anymore - it doesn't exist on
  256. // the native side.
  257. delete conference.callUUID;
  258. dispatch(appNavigate(undefined));
  259. Alert.alert(
  260. 'Call aborted',
  261. 'There\'s already another call in progress.'
  262. + ' Please end it first and try again.',
  263. [
  264. { text: 'OK' }
  265. ],
  266. { cancelable: false });
  267. } else {
  268. // Some devices fail because the CALL_PHONE permission is not granted, which is
  269. // nonsense, because it's not needed for self-managed connections.
  270. // Some other devices fail because ConnectionService is not supported.
  271. // Be that as it may, fallback to non-ConnectionService audio device handling.
  272. _handleConnectionServiceFailure(state);
  273. }
  274. });
  275. return result;
  276. }
  277. /**
  278. * Handles a ConnectionService fatal error by falling back to non-ConnectionService device management.
  279. *
  280. * @param {Object} state - Redux store.
  281. * @returns {void}
  282. */
  283. function _handleConnectionServiceFailure(state: Object) {
  284. const conference = getCurrentConference(state);
  285. if (conference) {
  286. // We're not tracking the call anymore.
  287. delete conference.callUUID;
  288. // ConnectionService has fatally failed. Alas, this also means audio device management would be broken, so
  289. // fallback to not using ConnectionService.
  290. // NOTE: We are not storing this in Settings, in case it's a transient issue, as far fetched as
  291. // that may be.
  292. if (AudioMode.setUseConnectionService) {
  293. AudioMode.setUseConnectionService(false);
  294. const hasVideo = !isVideoMutedByAudioOnly(state);
  295. // Set the desired audio mode, since we just reset the whole thing.
  296. AudioMode.setMode(hasVideo ? AudioMode.VIDEO_CALL : AudioMode.AUDIO_CALL);
  297. }
  298. }
  299. }
  300. /**
  301. * Handles CallKit's event {@code performEndCallAction}.
  302. *
  303. * @param {Object} event - The details of the CallKit event
  304. * {@code performEndCallAction}.
  305. * @returns {void}
  306. */
  307. function _onPerformEndCallAction({ callUUID }) {
  308. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  309. const conference = getCurrentConference(getState);
  310. if (conference && conference.callUUID === callUUID) {
  311. // We arrive here when a call is ended by the system, for example, when
  312. // another incoming call is received and the user selects "End &
  313. // Accept".
  314. delete conference.callUUID;
  315. dispatch(appNavigate(undefined));
  316. }
  317. }
  318. /**
  319. * Handles CallKit's event {@code performSetMutedCallAction}.
  320. *
  321. * @param {Object} event - The details of the CallKit event
  322. * {@code performSetMutedCallAction}.
  323. * @returns {void}
  324. */
  325. function _onPerformSetMutedCallAction({ callUUID, muted }) {
  326. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  327. const conference = getCurrentConference(getState);
  328. if (conference && conference.callUUID === callUUID) {
  329. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  330. sendAnalytics(
  331. createTrackMutedEvent('audio', 'call-integration', muted));
  332. dispatch(setAudioMuted(muted, /* ensureTrack */ true));
  333. }
  334. }
  335. /**
  336. * Update CallKit with the audio only state of the conference. When a conference
  337. * is in audio only mode we will tell CallKit the call has no video. This
  338. * affects how the call is saved in the recent calls list.
  339. *
  340. * XXX: Note that here we are taking the `audioOnly` value straight from the
  341. * action, instead of examining the state. This is intentional, as setting the
  342. * audio only involves multiple actions which will be reflected in the state
  343. * later, but we are just interested in knowing if the mode is going to be
  344. * set or not.
  345. *
  346. * @param {Store} store - The redux store in which the specified {@code action}
  347. * is being dispatched.
  348. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  349. * specified {@code action} in the specified {@code store}.
  350. * @param {Action} action - The redux action which is being dispatched in the
  351. * specified {@code store}.
  352. * @private
  353. * @returns {*} The value returned by {@code next(action)}.
  354. */
  355. function _setAudioOnly({ getState }, next, action) {
  356. const result = next(action);
  357. const state = getState();
  358. if (!isCallIntegrationEnabled(state)) {
  359. return result;
  360. }
  361. const conference = getCurrentConference(state);
  362. if (conference && conference.callUUID) {
  363. CallIntegration.updateCall(
  364. conference.callUUID,
  365. { hasVideo: !action.audioOnly });
  366. }
  367. return result;
  368. }
  369. /**
  370. * Notifies the feature callkit that the action
  371. * {@link _SET_CALL_INTEGRATION_SUBSCRIPTIONS} is being dispatched within
  372. * a specific redux {@code store}.
  373. *
  374. * @param {Store} store - The redux store in which the specified {@code action}
  375. * is being dispatched.
  376. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  377. * specified {@code action} in the specified {@code store}.
  378. * @param {Action} action - The redux action
  379. * {@code _SET_CALL_INTEGRATION_SUBSCRIPTIONS} which is being dispatched in
  380. * the specified {@code store}.
  381. * @private
  382. * @returns {*} The value returned by {@code next(action)}.
  383. */
  384. function _setCallKitSubscriptions({ getState }, next, action) {
  385. const { subscriptions } = getState()['features/call-integration'];
  386. if (subscriptions) {
  387. for (const subscription of subscriptions) {
  388. subscription.remove();
  389. }
  390. }
  391. return next(action);
  392. }
  393. /**
  394. * Synchronize the muted state of tracks with CallKit.
  395. *
  396. * @param {Store} store - The redux store in which the specified {@code action}
  397. * is being dispatched.
  398. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  399. * specified {@code action} in the specified {@code store}.
  400. * @param {Action} action - The redux action which is being dispatched in the
  401. * specified {@code store}.
  402. * @private
  403. * @returns {*} The value returned by {@code next(action)}.
  404. */
  405. function _syncTrackState({ getState }, next, action) {
  406. const result = next(action);
  407. if (!isCallIntegrationEnabled(getState)) {
  408. return result;
  409. }
  410. const { jitsiTrack } = action.track;
  411. const state = getState();
  412. const conference = getCurrentConference(state);
  413. if (jitsiTrack.isLocal() && conference && conference.callUUID) {
  414. switch (jitsiTrack.getType()) {
  415. case 'audio': {
  416. _updateCallIntegrationMuted(conference, state);
  417. break;
  418. }
  419. case 'video': {
  420. CallIntegration.updateCall(
  421. conference.callUUID,
  422. { hasVideo: !isVideoMutedByAudioOnly(state) });
  423. break;
  424. }
  425. }
  426. }
  427. return result;
  428. }
  429. /**
  430. * Update the muted state in the native side.
  431. *
  432. * @param {Object} conference - The current active conference.
  433. * @param {Object} state - The redux store state.
  434. * @private
  435. * @returns {void}
  436. */
  437. function _updateCallIntegrationMuted(conference, state) {
  438. const muted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  439. CallIntegration.setMuted(conference.callUUID, muted);
  440. }