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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. import uuid from 'uuid';
  4. import {
  5. createTrackMutedEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT, appNavigate } from '../../app';
  9. import {
  10. CONFERENCE_FAILED,
  11. CONFERENCE_LEFT,
  12. CONFERENCE_WILL_JOIN,
  13. CONFERENCE_JOINED,
  14. getCurrentConference
  15. } from '../../base/conference';
  16. import { getInviteURL } from '../../base/connection';
  17. import {
  18. MEDIA_TYPE,
  19. SET_AUDIO_MUTED,
  20. SET_VIDEO_MUTED,
  21. VIDEO_MUTISM_AUTHORITY,
  22. isVideoMutedByAudioOnly,
  23. setAudioMuted
  24. } from '../../base/media';
  25. import { MiddlewareRegistry } from '../../base/redux';
  26. import {
  27. TRACK_CREATE_ERROR,
  28. isLocalTrackMuted
  29. } from '../../base/tracks';
  30. import { _SET_CALLKIT_SUBSCRIPTIONS } from './actionTypes';
  31. import CallKit from './CallKit';
  32. /**
  33. * Middleware that captures several system actions and hooks up CallKit.
  34. *
  35. * @param {Store} store - The redux store.
  36. * @returns {Function}
  37. */
  38. CallKit && MiddlewareRegistry.register(store => next => action => {
  39. switch (action.type) {
  40. case _SET_CALLKIT_SUBSCRIPTIONS:
  41. return _setCallKitSubscriptions(store, next, action);
  42. case APP_WILL_MOUNT:
  43. return _appWillMount(store, next, action);
  44. case APP_WILL_UNMOUNT:
  45. store.dispatch({
  46. type: _SET_CALLKIT_SUBSCRIPTIONS,
  47. subscriptions: undefined
  48. });
  49. break;
  50. case CONFERENCE_FAILED:
  51. return _conferenceFailed(store, next, action);
  52. case CONFERENCE_JOINED:
  53. return _conferenceJoined(store, next, action);
  54. case CONFERENCE_LEFT:
  55. return _conferenceLeft(store, next, action);
  56. case CONFERENCE_WILL_JOIN:
  57. return _conferenceWillJoin(store, next, action);
  58. case SET_AUDIO_MUTED:
  59. return _setAudioMuted(store, next, action);
  60. case SET_VIDEO_MUTED:
  61. return _setVideoMuted(store, next, action);
  62. case TRACK_CREATE_ERROR:
  63. return _trackCreateError(store, next, action);
  64. }
  65. return next(action);
  66. });
  67. /**
  68. * Notifies the feature jwt that the action {@link APP_WILL_MOUNT} is being
  69. * dispatched within a specific redux {@code store}.
  70. *
  71. * @param {Store} store - The redux store in which the specified {@code action}
  72. * is being dispatched.
  73. * @param {Dispatch} next - The redux dispatch function to dispatch the
  74. * specified {@code action} to the specified {@code store}.
  75. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  76. * being dispatched in the specified {@code store}.
  77. * @private
  78. * @returns {*}
  79. */
  80. function _appWillMount({ dispatch, getState }, next, action) {
  81. const result = next(action);
  82. CallKit.setProviderConfiguration({
  83. iconTemplateImageName: 'CallKitIcon',
  84. localizedName: NativeModules.AppInfo.name
  85. });
  86. const context = {
  87. dispatch,
  88. getState
  89. };
  90. const subscriptions = [
  91. CallKit.addListener(
  92. 'performEndCallAction',
  93. _onPerformEndCallAction,
  94. context),
  95. CallKit.addListener(
  96. 'performSetMutedCallAction',
  97. _onPerformSetMutedCallAction,
  98. context),
  99. // According to CallKit's documentation, when the system resets we
  100. // should terminate all calls. Hence, providerDidReset is the same
  101. // to us as performEndCallAction.
  102. CallKit.addListener(
  103. 'providerDidReset',
  104. _onPerformEndCallAction,
  105. context)
  106. ];
  107. dispatch({
  108. type: _SET_CALLKIT_SUBSCRIPTIONS,
  109. subscriptions
  110. });
  111. return result;
  112. }
  113. /**
  114. * Notifies the feature jwt that the action {@link CONFERENCE_FAILED} is being
  115. * dispatched within a specific redux {@code store}.
  116. *
  117. * @param {Store} store - The redux store in which the specified {@code action}
  118. * is being dispatched.
  119. * @param {Dispatch} next - The redux dispatch function to dispatch the
  120. * specified {@code action} to the specified {@code store}.
  121. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  122. * being dispatched in the specified {@code store}.
  123. * @private
  124. * @returns {*}
  125. */
  126. function _conferenceFailed(store, next, action) {
  127. const result = next(action);
  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. CallKit.reportCallFailed(callUUID);
  135. }
  136. }
  137. return result;
  138. }
  139. /**
  140. * Notifies the feature jwt that the action {@link CONFERENCE_JOINED} is being
  141. * 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 dispatch function to dispatch the
  146. * specified {@code action} to 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 {*}
  151. */
  152. function _conferenceJoined(store, next, action) {
  153. const result = next(action);
  154. const { callUUID } = action.conference;
  155. if (callUUID) {
  156. CallKit.reportConnectedOutgoingCall(callUUID);
  157. }
  158. return result;
  159. }
  160. /**
  161. * Notifies the feature jwt that the action {@link CONFERENCE_LEFT} is being
  162. * dispatched within a specific redux {@code store}.
  163. *
  164. * @param {Store} store - The redux store in which the specified {@code action}
  165. * is being dispatched.
  166. * @param {Dispatch} next - The redux dispatch function to dispatch the
  167. * specified {@code action} to the specified {@code store}.
  168. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} which is
  169. * being dispatched in the specified {@code store}.
  170. * @private
  171. * @returns {*}
  172. */
  173. function _conferenceLeft(store, next, action) {
  174. const result = next(action);
  175. const { callUUID } = action.conference;
  176. if (callUUID) {
  177. CallKit.endCall(callUUID);
  178. }
  179. return result;
  180. }
  181. /**
  182. * Notifies the feature jwt that the action {@link CONFERENCE_WILL_JOIN} is
  183. * being dispatched within a specific redux {@code store}.
  184. *
  185. * @param {Store} store - The redux store in which the specified {@code action}
  186. * is being dispatched.
  187. * @param {Dispatch} next - The redux dispatch function to dispatch the
  188. * specified {@code action} to the specified {@code store}.
  189. * @param {Action} action - The redux action {@code CONFERENCE_WILL_JOIN} which
  190. * is being dispatched in the specified {@code store}.
  191. * @private
  192. * @returns {*}
  193. */
  194. function _conferenceWillJoin({ getState }, next, action) {
  195. const result = next(action);
  196. const { conference } = action;
  197. const state = getState();
  198. const url = getInviteURL(state);
  199. const hasVideo = !isVideoMutedByAudioOnly(state);
  200. // When assigning the call UUID, do so in upper case, since iOS will
  201. // return it upper cased.
  202. conference.callUUID = uuid.v4().toUpperCase();
  203. CallKit.startCall(conference.callUUID, url.toString(), hasVideo)
  204. .then(() => {
  205. const { room } = state['features/base/conference'];
  206. const { callee } = state['features/base/jwt'];
  207. const tracks = state['features/base/tracks'];
  208. const muted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  209. CallKit.updateCall(
  210. conference.callUUID,
  211. { displayName: (callee && callee.name) || room });
  212. CallKit.setMuted(conference.callUUID, muted);
  213. });
  214. return result;
  215. }
  216. /**
  217. * Handles CallKit's event {@code performEndCallAction}.
  218. *
  219. * @param {Object} event - The details of the CallKit event
  220. * {@code performEndCallAction}.
  221. * @returns {void}
  222. */
  223. function _onPerformEndCallAction({ callUUID }) {
  224. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  225. const conference = getCurrentConference(getState);
  226. if (conference && conference.callUUID === callUUID) {
  227. // We arrive here when a call is ended by the system, for example, when
  228. // another incoming call is received and the user selects "End &
  229. // Accept".
  230. delete conference.callUUID;
  231. dispatch(appNavigate(undefined));
  232. }
  233. }
  234. /**
  235. * Handles CallKit's event {@code performSetMutedCallAction}.
  236. *
  237. * @param {Object} event - The details of the CallKit event
  238. * {@code performSetMutedCallAction}.
  239. * @returns {void}
  240. */
  241. function _onPerformSetMutedCallAction({ callUUID, muted: newValue }) {
  242. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  243. const conference = getCurrentConference(getState);
  244. if (conference && conference.callUUID === callUUID) {
  245. // Break the loop. Audio can be muted from both CallKit and Jitsi Meet.
  246. // We must keep them in sync, but at some point the loop needs to be
  247. // broken. We are doing it here, on the CallKit handler.
  248. const { muted: oldValue } = getState()['features/base/media'].audio;
  249. if (oldValue !== newValue) {
  250. const value = Boolean(newValue);
  251. sendAnalytics(createTrackMutedEvent('audio', 'callkit', value));
  252. dispatch(setAudioMuted(
  253. value, VIDEO_MUTISM_AUTHORITY.USER, /* ensureTrack */ true));
  254. }
  255. }
  256. }
  257. /**
  258. * Notifies the feature jwt that the action {@link SET_AUDIO_MUTED} is being
  259. * dispatched within a specific redux {@code store}.
  260. *
  261. * @param {Store} store - The redux store in which the specified {@code action}
  262. * is being dispatched.
  263. * @param {Dispatch} next - The redux dispatch function to dispatch the
  264. * specified {@code action} to the specified {@code store}.
  265. * @param {Action} action - The redux action {@code SET_AUDIO_MUTED} which is
  266. * being dispatched in the specified {@code store}.
  267. * @private
  268. * @returns {*}
  269. */
  270. function _setAudioMuted({ getState }, next, action) {
  271. const result = next(action);
  272. const conference = getCurrentConference(getState);
  273. if (conference && conference.callUUID) {
  274. CallKit.setMuted(conference.callUUID, action.muted);
  275. }
  276. return result;
  277. }
  278. /**
  279. * Notifies the feature jwt that the action {@link _SET_CALLKIT_SUBSCRIPTIONS}
  280. * is being dispatched within a specific redux {@code store}.
  281. *
  282. * @param {Store} store - The redux store in which the specified {@code action}
  283. * is being dispatched.
  284. * @param {Dispatch} next - The redux dispatch function to dispatch the
  285. * specified {@code action} to the specified {@code store}.
  286. * @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
  287. * which is being dispatched in the specified {@code store}.
  288. * @private
  289. * @returns {*}
  290. */
  291. function _setCallKitSubscriptions({ getState }, next, action) {
  292. const { subscriptions } = getState()['features/callkit'];
  293. if (subscriptions) {
  294. for (const subscription of subscriptions) {
  295. subscription.remove();
  296. }
  297. }
  298. return next(action);
  299. }
  300. /**
  301. * Notifies the feature jwt that the action {@link SET_VIDEO_MUTED} is being
  302. * dispatched within a specific redux {@code store}.
  303. *
  304. * @param {Store} store - The redux store in which the specified {@code action}
  305. * is being dispatched.
  306. * @param {Dispatch} next - The redux dispatch function to dispatch the
  307. * specified {@code action} to the specified {@code store}.
  308. * @param {Action} action - The redux action {@code SET_VIDEO_MUTED} which is
  309. * being dispatched in the specified {@code store}.
  310. * @private
  311. * @returns {*}
  312. */
  313. function _setVideoMuted({ getState }, next, action) {
  314. const result = next(action);
  315. const conference = getCurrentConference(getState);
  316. if (conference && conference.callUUID) {
  317. CallKit.updateCall(
  318. conference.callUUID,
  319. { hasVideo: !isVideoMutedByAudioOnly(getState) });
  320. }
  321. return result;
  322. }
  323. /**
  324. * Handles a track creation failure. This is relevant to us in the following
  325. * (corner) case: if the user never gave their permission to use the microphone
  326. * and try to unmute from the CallKit interface, this will fail, and we need
  327. * to sync back the CallKit button state.
  328. *
  329. * @param {Store} store - The redux store in which the specified {@code action}
  330. * is being dispatched.
  331. * @param {Dispatch} next - The redux dispatch function to dispatch the
  332. * specified {@code action} to the specified {@code store}.
  333. * @param {Action} action - The redux action {@code TRACK_CREARE_ERROR} which is
  334. * being dispatched in the specified {@code store}.
  335. * @private
  336. * @returns {*}
  337. */
  338. function _trackCreateError({ getState }, next, action) {
  339. const result = next(action);
  340. const state = getState();
  341. const conference = getCurrentConference(state);
  342. if (conference && conference.callUUID) {
  343. const tracks = state['features/base/tracks'];
  344. const muted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  345. CallKit.setMuted(conference.callUUID, muted);
  346. }
  347. return result;
  348. }