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

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