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

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