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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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/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. 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. // If we already have a callUUID set, don't start a new call.
  229. if (conference.callUUID) {
  230. return result;
  231. }
  232. // When assigning the call UUID, do so in upper case, since iOS will return
  233. // it upper cased.
  234. conference.callUUID = (callUUID || uuid.v4()).toUpperCase();
  235. CallIntegration.startCall(conference.callUUID, handle, hasVideo)
  236. .then(() => {
  237. const displayName = getConferenceName(state);
  238. CallIntegration.updateCall(
  239. conference.callUUID,
  240. {
  241. displayName,
  242. hasVideo
  243. });
  244. // iOS 13 doesn't like the mute state to be false before the call is started
  245. // so delay it until the conference was joined.
  246. if (Platform.OS !== 'ios') {
  247. _updateCallIntegrationMuted(conference, state);
  248. }
  249. })
  250. .catch(error => {
  251. // Currently this error codes are emitted only by Android.
  252. //
  253. if (error.code === 'CREATE_OUTGOING_CALL_FAILED') {
  254. // We're not tracking the call anymore - it doesn't exist on
  255. // the native side.
  256. delete conference.callUUID;
  257. dispatch(appNavigate(undefined));
  258. Alert.alert(
  259. 'Call aborted',
  260. 'There\'s already another call in progress.'
  261. + ' Please end it first and try again.',
  262. [
  263. { text: 'OK' }
  264. ],
  265. { cancelable: false });
  266. } else {
  267. // Some devices fail because the CALL_PHONE permission is not granted, which is
  268. // nonsense, because it's not needed for self-managed connections.
  269. // Some other devices fail because ConnectionService is not supported.
  270. // Be that as it may, fallback to non-ConnectionService audio device handling.
  271. _handleConnectionServiceFailure(state);
  272. }
  273. });
  274. return result;
  275. }
  276. /**
  277. * Handles a ConnectionService fatal error by falling back to non-ConnectionService device management.
  278. *
  279. * @param {Object} state - Redux store.
  280. * @returns {void}
  281. */
  282. function _handleConnectionServiceFailure(state: Object) {
  283. const conference = getCurrentConference(state);
  284. if (conference) {
  285. // We're not tracking the call anymore.
  286. delete conference.callUUID;
  287. // ConnectionService has fatally failed. Alas, this also means audio device management would be broken, so
  288. // fallback to not using ConnectionService.
  289. // NOTE: We are not storing this in Settings, in case it's a transient issue, as far fetched as
  290. // that may be.
  291. if (AudioMode.setUseConnectionService) {
  292. AudioMode.setUseConnectionService(false);
  293. const hasVideo = !isVideoMutedByAudioOnly(state);
  294. // Set the desired audio mode, since we just reset the whole thing.
  295. AudioMode.setMode(hasVideo ? AudioMode.VIDEO_CALL : AudioMode.AUDIO_CALL);
  296. }
  297. }
  298. }
  299. /**
  300. * Handles CallKit's event {@code performEndCallAction}.
  301. *
  302. * @param {Object} event - The details of the CallKit event
  303. * {@code performEndCallAction}.
  304. * @returns {void}
  305. */
  306. function _onPerformEndCallAction({ callUUID }) {
  307. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  308. const conference = getCurrentConference(getState);
  309. if (conference && conference.callUUID === callUUID) {
  310. // We arrive here when a call is ended by the system, for example, when
  311. // another incoming call is received and the user selects "End &
  312. // Accept".
  313. delete conference.callUUID;
  314. dispatch(appNavigate(undefined));
  315. }
  316. }
  317. /**
  318. * Handles CallKit's event {@code performSetMutedCallAction}.
  319. *
  320. * @param {Object} event - The details of the CallKit event
  321. * {@code performSetMutedCallAction}.
  322. * @returns {void}
  323. */
  324. function _onPerformSetMutedCallAction({ callUUID, muted }) {
  325. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  326. const conference = getCurrentConference(getState);
  327. if (conference && conference.callUUID === callUUID) {
  328. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  329. sendAnalytics(
  330. createTrackMutedEvent('audio', 'call-integration', muted));
  331. dispatch(setAudioMuted(muted, /* ensureTrack */ true));
  332. }
  333. }
  334. /**
  335. * Update CallKit with the audio only state of the conference. When a conference
  336. * is in audio only mode we will tell CallKit the call has no video. This
  337. * affects how the call is saved in the recent calls list.
  338. *
  339. * XXX: Note that here we are taking the `audioOnly` value straight from the
  340. * action, instead of examining the state. This is intentional, as setting the
  341. * audio only involves multiple actions which will be reflected in the state
  342. * later, but we are just interested in knowing if the mode is going to be
  343. * set or not.
  344. *
  345. * @param {Store} store - The redux store in which the specified {@code action}
  346. * is being dispatched.
  347. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  348. * specified {@code action} in the specified {@code store}.
  349. * @param {Action} action - The redux action which is being dispatched in the
  350. * specified {@code store}.
  351. * @private
  352. * @returns {*} The value returned by {@code next(action)}.
  353. */
  354. function _setAudioOnly({ getState }, next, action) {
  355. const result = next(action);
  356. const state = getState();
  357. if (!isCallIntegrationEnabled(state)) {
  358. return result;
  359. }
  360. const conference = getCurrentConference(state);
  361. if (conference && conference.callUUID) {
  362. CallIntegration.updateCall(
  363. conference.callUUID,
  364. { hasVideo: !action.audioOnly });
  365. }
  366. return result;
  367. }
  368. /**
  369. * Notifies the feature callkit that the action
  370. * {@link _SET_CALL_INTEGRATION_SUBSCRIPTIONS} is being dispatched within
  371. * a specific redux {@code store}.
  372. *
  373. * @param {Store} store - The redux store in which the specified {@code action}
  374. * is being dispatched.
  375. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  376. * specified {@code action} in the specified {@code store}.
  377. * @param {Action} action - The redux action
  378. * {@code _SET_CALL_INTEGRATION_SUBSCRIPTIONS} which is being dispatched in
  379. * the specified {@code store}.
  380. * @private
  381. * @returns {*} The value returned by {@code next(action)}.
  382. */
  383. function _setCallKitSubscriptions({ getState }, next, action) {
  384. const { subscriptions } = getState()['features/call-integration'];
  385. if (subscriptions) {
  386. for (const subscription of subscriptions) {
  387. subscription.remove();
  388. }
  389. }
  390. return next(action);
  391. }
  392. /**
  393. * Synchronize the muted state of tracks with CallKit.
  394. *
  395. * @param {Store} store - The redux store in which the specified {@code action}
  396. * is being dispatched.
  397. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  398. * specified {@code action} in the specified {@code store}.
  399. * @param {Action} action - The redux action which is being dispatched in the
  400. * specified {@code store}.
  401. * @private
  402. * @returns {*} The value returned by {@code next(action)}.
  403. */
  404. function _syncTrackState({ getState }, next, action) {
  405. const result = next(action);
  406. if (!isCallIntegrationEnabled(getState)) {
  407. return result;
  408. }
  409. const { jitsiTrack } = action.track;
  410. const state = getState();
  411. const conference = getCurrentConference(state);
  412. if (jitsiTrack.isLocal() && conference && conference.callUUID) {
  413. switch (jitsiTrack.getType()) {
  414. case 'audio': {
  415. _updateCallIntegrationMuted(conference, state);
  416. break;
  417. }
  418. case 'video': {
  419. CallIntegration.updateCall(
  420. conference.callUUID,
  421. { hasVideo: !isVideoMutedByAudioOnly(state) });
  422. break;
  423. }
  424. }
  425. }
  426. return result;
  427. }
  428. /**
  429. * Update the muted state in the native side.
  430. *
  431. * @param {Object} conference - The current active conference.
  432. * @param {Object} state - The redux store state.
  433. * @private
  434. * @returns {void}
  435. */
  436. function _updateCallIntegrationMuted(conference, state) {
  437. const muted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  438. CallIntegration.setMuted(conference.callUUID, muted);
  439. }