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

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