Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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).then(() => {
  160. // iOS 13 doesn't like the mute state to be false before the call is started
  161. // so we update it here in case the user selected startWithAudioMuted.
  162. if (Platform.OS === 'ios') {
  163. _updateCallIntegrationMuted(action.conference, getState());
  164. }
  165. });
  166. }
  167. return result;
  168. }
  169. /**
  170. * Notifies the feature callkit that the action {@link CONFERENCE_LEFT} is being
  171. * dispatched within a specific redux {@code store}.
  172. *
  173. * @param {Store} store - The redux store in which the specified {@code action}
  174. * is being dispatched.
  175. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  176. * specified {@code action} in the specified {@code store}.
  177. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} which is
  178. * being dispatched in the specified {@code store}.
  179. * @private
  180. * @returns {*} The value returned by {@code next(action)}.
  181. */
  182. function _conferenceLeft({ getState }, next, action) {
  183. const result = next(action);
  184. if (!isCallIntegrationEnabled(getState)) {
  185. return result;
  186. }
  187. const { callUUID } = action.conference;
  188. if (callUUID) {
  189. delete action.conference.callUUID;
  190. CallIntegration.endCall(callUUID);
  191. }
  192. return result;
  193. }
  194. /**
  195. * Notifies the feature callkit that the action {@link CONFERENCE_WILL_JOIN} is
  196. * being dispatched within a specific redux {@code store}.
  197. *
  198. * @param {Store} store - The redux store in which the specified {@code action}
  199. * is being dispatched.
  200. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  201. * specified {@code action} in the specified {@code store}.
  202. * @param {Action} action - The redux action {@code CONFERENCE_WILL_JOIN} which
  203. * is being dispatched in the specified {@code store}.
  204. * @private
  205. * @returns {*} The value returned by {@code next(action)}.
  206. */
  207. function _conferenceWillJoin({ dispatch, getState }, next, action) {
  208. const result = next(action);
  209. if (!isCallIntegrationEnabled(getState)) {
  210. return result;
  211. }
  212. const { conference } = action;
  213. const state = getState();
  214. const { callHandle, callUUID } = state['features/base/config'];
  215. const url = getInviteURL(state);
  216. const handle = callHandle || url.toString();
  217. const hasVideo = !isVideoMutedByAudioOnly(state);
  218. // When assigning the call UUID, do so in upper case, since iOS will return
  219. // it upper cased.
  220. conference.callUUID = (callUUID || uuid.v4()).toUpperCase();
  221. CallIntegration.startCall(conference.callUUID, handle, hasVideo)
  222. .then(() => {
  223. const displayName = getConferenceName(state);
  224. CallIntegration.updateCall(
  225. conference.callUUID,
  226. {
  227. displayName,
  228. hasVideo
  229. });
  230. // iOS 13 doesn't like the mute state to be false before the call is started
  231. // so delay it until the conference was joined.
  232. if (Platform.OS !== 'ios') {
  233. _updateCallIntegrationMuted(conference, state);
  234. }
  235. })
  236. .catch(error => {
  237. // Currently this error codes are emitted only by Android.
  238. //
  239. if (error.code === 'CREATE_OUTGOING_CALL_FAILED') {
  240. // We're not tracking the call anymore - it doesn't exist on
  241. // the native side.
  242. delete conference.callUUID;
  243. dispatch(appNavigate(undefined));
  244. Alert.alert(
  245. 'Call aborted',
  246. 'There\'s already another call in progress.'
  247. + ' Please end it first and try again.',
  248. [
  249. { text: 'OK' }
  250. ],
  251. { cancelable: false });
  252. } else if (error.code === 'SECURITY_ERROR') {
  253. // Some devices fail because the CALL_PHONE permission is not granted, which is
  254. // nonsense, because it's not needed for self-managed connections. Alas, this also
  255. // means audio device management would be broken, so fallback to not using ConnectionService.
  256. // NOTE: We are not storing this in Settings, in case it's a transient issue, as far fetched as
  257. // that may be.
  258. if (AudioMode.setUseConnectionService) {
  259. AudioMode.setUseConnectionService(false);
  260. // Set the desired audio mode, since we just reset the whole thing.
  261. AudioMode.setMode(hasVideo ? AudioMode.VIDEO_CALL : AudioMode.AUDIO_CALL);
  262. }
  263. }
  264. });
  265. return result;
  266. }
  267. /**
  268. * Handles CallKit's event {@code performEndCallAction}.
  269. *
  270. * @param {Object} event - The details of the CallKit event
  271. * {@code performEndCallAction}.
  272. * @returns {void}
  273. */
  274. function _onPerformEndCallAction({ callUUID }) {
  275. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  276. const conference = getCurrentConference(getState);
  277. if (conference && conference.callUUID === callUUID) {
  278. // We arrive here when a call is ended by the system, for example, when
  279. // another incoming call is received and the user selects "End &
  280. // Accept".
  281. delete conference.callUUID;
  282. dispatch(appNavigate(undefined));
  283. }
  284. }
  285. /**
  286. * Handles CallKit's event {@code performSetMutedCallAction}.
  287. *
  288. * @param {Object} event - The details of the CallKit event
  289. * {@code performSetMutedCallAction}.
  290. * @returns {void}
  291. */
  292. function _onPerformSetMutedCallAction({ callUUID, muted }) {
  293. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  294. const conference = getCurrentConference(getState);
  295. if (conference && conference.callUUID === callUUID) {
  296. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  297. sendAnalytics(
  298. createTrackMutedEvent('audio', 'call-integration', muted));
  299. dispatch(setAudioMuted(muted, /* ensureTrack */ true));
  300. }
  301. }
  302. /**
  303. * Update CallKit with the audio only state of the conference. When a conference
  304. * is in audio only mode we will tell CallKit the call has no video. This
  305. * affects how the call is saved in the recent calls list.
  306. *
  307. * XXX: Note that here we are taking the `audioOnly` value straight from the
  308. * action, instead of examining the state. This is intentional, as setting the
  309. * audio only involves multiple actions which will be reflected in the state
  310. * later, but we are just interested in knowing if the mode is going to be
  311. * set or not.
  312. *
  313. * @param {Store} store - The redux store in which the specified {@code action}
  314. * is being dispatched.
  315. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  316. * specified {@code action} in the specified {@code store}.
  317. * @param {Action} action - The redux action which is being dispatched in the
  318. * specified {@code store}.
  319. * @private
  320. * @returns {*} The value returned by {@code next(action)}.
  321. */
  322. function _setAudioOnly({ getState }, next, action) {
  323. const result = next(action);
  324. const state = getState();
  325. if (!isCallIntegrationEnabled(state)) {
  326. return result;
  327. }
  328. const conference = getCurrentConference(state);
  329. if (conference && conference.callUUID) {
  330. CallIntegration.updateCall(
  331. conference.callUUID,
  332. { hasVideo: !action.audioOnly });
  333. }
  334. return result;
  335. }
  336. /**
  337. * Notifies the feature callkit that the action
  338. * {@link _SET_CALL_INTEGRATION_SUBSCRIPTIONS} is being dispatched within
  339. * a specific redux {@code store}.
  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
  346. * {@code _SET_CALL_INTEGRATION_SUBSCRIPTIONS} which is being dispatched in
  347. * the specified {@code store}.
  348. * @private
  349. * @returns {*} The value returned by {@code next(action)}.
  350. */
  351. function _setCallKitSubscriptions({ getState }, next, action) {
  352. const { subscriptions } = getState()['features/call-integration'];
  353. if (subscriptions) {
  354. for (const subscription of subscriptions) {
  355. subscription.remove();
  356. }
  357. }
  358. return next(action);
  359. }
  360. /**
  361. * Synchronize the muted state of tracks with CallKit.
  362. *
  363. * @param {Store} store - The redux store in which the specified {@code action}
  364. * is being dispatched.
  365. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  366. * specified {@code action} in the specified {@code store}.
  367. * @param {Action} action - The redux action which is being dispatched in the
  368. * specified {@code store}.
  369. * @private
  370. * @returns {*} The value returned by {@code next(action)}.
  371. */
  372. function _syncTrackState({ getState }, next, action) {
  373. const result = next(action);
  374. if (!isCallIntegrationEnabled(getState)) {
  375. return result;
  376. }
  377. const { jitsiTrack } = action.track;
  378. const state = getState();
  379. const conference = getCurrentConference(state);
  380. if (jitsiTrack.isLocal() && conference && conference.callUUID) {
  381. switch (jitsiTrack.getType()) {
  382. case 'audio': {
  383. _updateCallIntegrationMuted(conference, state);
  384. break;
  385. }
  386. case 'video': {
  387. CallIntegration.updateCall(
  388. conference.callUUID,
  389. { hasVideo: !isVideoMutedByAudioOnly(state) });
  390. break;
  391. }
  392. }
  393. }
  394. return result;
  395. }
  396. /**
  397. * Update the muted state in the native side.
  398. *
  399. * @param {Object} conference - The current active conference.
  400. * @param {Object} state - The redux store state.
  401. * @private
  402. * @returns {void}
  403. */
  404. function _updateCallIntegrationMuted(conference, state) {
  405. const muted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  406. CallIntegration.setMuted(conference.callUUID, muted);
  407. }