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

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