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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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: 'AppIcon40x40',
  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.conference;
  180. const url = getInviteURL(getState);
  181. const hasVideo = !isVideoMutedByAudioOnly(getState);
  182. // When assigning the call UUID, do so in upper case, since iOS will
  183. // return it upper cased.
  184. conference.callUUID = uuid.v4().toUpperCase();
  185. CallKit.startCall(conference.callUUID, url.toString(), hasVideo)
  186. .then(() => {
  187. const { room } = getState()['features/base/conference'];
  188. CallKit.updateCall(conference.callUUID, { displayName: room });
  189. });
  190. return result;
  191. }
  192. /**
  193. * Handles CallKit's event {@code performEndCallAction}.
  194. *
  195. * @param {Object} event - The details of the CallKit event
  196. * {@code performEndCallAction}.
  197. * @returns {void}
  198. */
  199. function _onPerformEndCallAction({ callUUID }) {
  200. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  201. const conference = getCurrentConference(getState);
  202. if (conference && conference.callUUID === callUUID) {
  203. // We arrive here when a call is ended by the system, for
  204. // example when another incoming call is received and the user
  205. // selects "End & Accept".
  206. delete conference.callUUID;
  207. dispatch(appNavigate(undefined));
  208. }
  209. }
  210. /**
  211. * Handles CallKit's event {@code performSetMutedCallAction}.
  212. *
  213. * @param {Object} event - The details of the CallKit event
  214. * {@code performSetMutedCallAction}.
  215. * @returns {void}
  216. */
  217. function _onPerformSetMutedCallAction({ callUUID, muted: newValue }) {
  218. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  219. const conference = getCurrentConference(getState);
  220. if (conference && conference.callUUID === callUUID) {
  221. // Break the loop. Audio can be muted from both CallKit and Jitsi Meet.
  222. // We must keep them in sync, but at some point the loop needs to be
  223. // broken. We are doing it here, on the CallKit handler.
  224. const { muted: oldValue } = getState()['features/base/media'].audio;
  225. if (oldValue !== newValue) {
  226. dispatch(setAudioMuted(Boolean(newValue)));
  227. }
  228. }
  229. }
  230. /**
  231. * Notifies the feature jwt that the action {@link SET_AUDIO_MUTED} is being
  232. * dispatched within a specific redux {@code store}.
  233. *
  234. * @param {Store} store - The redux store in which the specified {@code action}
  235. * is being dispatched.
  236. * @param {Dispatch} next - The redux dispatch function to dispatch the
  237. * specified {@code action} to the specified {@code store}.
  238. * @param {Action} action - The redux action {@code SET_AUDIO_MUTED} which is
  239. * being dispatched in the specified {@code store}.
  240. * @private
  241. * @returns {*}
  242. */
  243. function _setAudioMuted({ getState }, next, action) {
  244. const result = next(action);
  245. const conference = getCurrentConference(getState);
  246. if (conference && conference.callUUID) {
  247. CallKit.setMuted(conference.callUUID, action.muted);
  248. }
  249. return result;
  250. }
  251. /**
  252. * Notifies the feature jwt that the action {@link _SET_CALLKIT_SUBSCRIPTIONS}
  253. * is being dispatched within a specific redux {@code store}.
  254. *
  255. * @param {Store} store - The redux store in which the specified {@code action}
  256. * is being dispatched.
  257. * @param {Dispatch} next - The redux dispatch function to dispatch the
  258. * specified {@code action} to the specified {@code store}.
  259. * @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
  260. * which is being dispatched in the specified {@code store}.
  261. * @private
  262. * @returns {*}
  263. */
  264. function _setCallKitSubscriptions({ getState }, next, action) {
  265. const { subscriptions } = getState()['features/callkit'];
  266. if (subscriptions) {
  267. for (const subscription of subscriptions) {
  268. subscription.remove();
  269. }
  270. }
  271. return next(action);
  272. }
  273. /**
  274. * Notifies the feature jwt that the action {@link SET_VIDEO_MUTED} is being
  275. * dispatched within a specific redux {@code store}.
  276. *
  277. * @param {Store} store - The redux store in which the specified {@code action}
  278. * is being dispatched.
  279. * @param {Dispatch} next - The redux dispatch function to dispatch the
  280. * specified {@code action} to the specified {@code store}.
  281. * @param {Action} action - The redux action {@code SET_VIDEO_MUTED} which is
  282. * being dispatched in the specified {@code store}.
  283. * @private
  284. * @returns {*}
  285. */
  286. function _setVideoMuted({ getState }, next, action) {
  287. const result = next(action);
  288. const conference = getCurrentConference(getState);
  289. if (conference && conference.callUUID) {
  290. CallKit.updateCall(
  291. conference.callUUID,
  292. { hasVideo: !isVideoMutedByAudioOnly(getState) });
  293. }
  294. return result;
  295. }