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

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