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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. import uuid from 'uuid';
  4. import { sendAnalyticsEvent } 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. // XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
  118. // prevented the user from joining a specific conference but the app may be
  119. // able to eventually join the conference.
  120. if (!action.error.recoverable) {
  121. const { callUUID } = action.conference;
  122. if (callUUID) {
  123. CallKit.reportCallFailed(callUUID);
  124. }
  125. }
  126. return result;
  127. }
  128. /**
  129. * Notifies the feature jwt that the action {@link CONFERENCE_JOINED} is being
  130. * dispatched within a specific redux {@code store}.
  131. *
  132. * @param {Store} store - The redux store in which the specified {@code action}
  133. * is being dispatched.
  134. * @param {Dispatch} next - The redux dispatch function to dispatch the
  135. * specified {@code action} to the specified {@code store}.
  136. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  137. * being dispatched in the specified {@code store}.
  138. * @private
  139. * @returns {*}
  140. */
  141. function _conferenceJoined(store, next, action) {
  142. const result = next(action);
  143. const { callUUID } = action.conference;
  144. if (callUUID) {
  145. CallKit.reportConnectedOutgoingCall(callUUID);
  146. }
  147. return result;
  148. }
  149. /**
  150. * Notifies the feature jwt that the action {@link CONFERENCE_LEFT} is being
  151. * dispatched within a specific redux {@code store}.
  152. *
  153. * @param {Store} store - The redux store in which the specified {@code action}
  154. * is being dispatched.
  155. * @param {Dispatch} next - The redux dispatch function to dispatch the
  156. * specified {@code action} to the specified {@code store}.
  157. * @param {Action} action - The redux action {@code CONFERENCE_LEFT} which is
  158. * being dispatched in the specified {@code store}.
  159. * @private
  160. * @returns {*}
  161. */
  162. function _conferenceLeft(store, next, action) {
  163. const result = next(action);
  164. const { callUUID } = action.conference;
  165. if (callUUID) {
  166. CallKit.endCall(callUUID);
  167. }
  168. return result;
  169. }
  170. /**
  171. * Notifies the feature jwt that the action {@link CONFERENCE_WILL_JOIN} is
  172. * being dispatched within a specific redux {@code store}.
  173. *
  174. * @param {Store} store - The redux store in which the specified {@code action}
  175. * is being dispatched.
  176. * @param {Dispatch} next - The redux dispatch function to dispatch the
  177. * specified {@code action} to the specified {@code store}.
  178. * @param {Action} action - The redux action {@code CONFERENCE_WILL_JOIN} which
  179. * is being dispatched in the specified {@code store}.
  180. * @private
  181. * @returns {*}
  182. */
  183. function _conferenceWillJoin({ getState }, next, action) {
  184. const result = next(action);
  185. const { conference } = action;
  186. const state = getState();
  187. const url = getInviteURL(state);
  188. const hasVideo = !isVideoMutedByAudioOnly(state);
  189. // When assigning the call UUID, do so in upper case, since iOS will
  190. // return it upper cased.
  191. conference.callUUID = uuid.v4().toUpperCase();
  192. CallKit.startCall(conference.callUUID, url.toString(), hasVideo)
  193. .then(() => {
  194. const { room } = state['features/base/conference'];
  195. const { callee } = state['features/base/jwt'];
  196. CallKit.updateCall(
  197. conference.callUUID,
  198. { displayName: (callee && callee.name) || room });
  199. });
  200. return result;
  201. }
  202. /**
  203. * Handles CallKit's event {@code performEndCallAction}.
  204. *
  205. * @param {Object} event - The details of the CallKit event
  206. * {@code performEndCallAction}.
  207. * @returns {void}
  208. */
  209. function _onPerformEndCallAction({ callUUID }) {
  210. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  211. const conference = getCurrentConference(getState);
  212. if (conference && conference.callUUID === callUUID) {
  213. // We arrive here when a call is ended by the system, for example, when
  214. // another incoming call is received and the user selects "End &
  215. // Accept".
  216. delete conference.callUUID;
  217. dispatch(appNavigate(undefined));
  218. }
  219. }
  220. /**
  221. * Handles CallKit's event {@code performSetMutedCallAction}.
  222. *
  223. * @param {Object} event - The details of the CallKit event
  224. * {@code performSetMutedCallAction}.
  225. * @returns {void}
  226. */
  227. function _onPerformSetMutedCallAction({ callUUID, muted: newValue }) {
  228. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  229. const conference = getCurrentConference(getState);
  230. if (conference && conference.callUUID === callUUID) {
  231. // Break the loop. Audio can be muted from both CallKit and Jitsi Meet.
  232. // We must keep them in sync, but at some point the loop needs to be
  233. // broken. We are doing it here, on the CallKit handler.
  234. const { muted: oldValue } = getState()['features/base/media'].audio;
  235. if (oldValue !== newValue) {
  236. const value = Boolean(newValue);
  237. sendAnalyticsEvent(`callkit.audio.${value ? 'muted' : 'unmuted'}`);
  238. dispatch(setAudioMuted(value));
  239. }
  240. }
  241. }
  242. /**
  243. * Notifies the feature jwt that the action {@link SET_AUDIO_MUTED} is being
  244. * dispatched within a specific redux {@code store}.
  245. *
  246. * @param {Store} store - The redux store in which the specified {@code action}
  247. * is being dispatched.
  248. * @param {Dispatch} next - The redux dispatch function to dispatch the
  249. * specified {@code action} to the specified {@code store}.
  250. * @param {Action} action - The redux action {@code SET_AUDIO_MUTED} which is
  251. * being dispatched in the specified {@code store}.
  252. * @private
  253. * @returns {*}
  254. */
  255. function _setAudioMuted({ getState }, next, action) {
  256. const result = next(action);
  257. const conference = getCurrentConference(getState);
  258. if (conference && conference.callUUID) {
  259. CallKit.setMuted(conference.callUUID, action.muted);
  260. }
  261. return result;
  262. }
  263. /**
  264. * Notifies the feature jwt that the action {@link _SET_CALLKIT_SUBSCRIPTIONS}
  265. * is being dispatched within a specific redux {@code store}.
  266. *
  267. * @param {Store} store - The redux store in which the specified {@code action}
  268. * is being dispatched.
  269. * @param {Dispatch} next - The redux dispatch function to dispatch the
  270. * specified {@code action} to the specified {@code store}.
  271. * @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
  272. * which is being dispatched in the specified {@code store}.
  273. * @private
  274. * @returns {*}
  275. */
  276. function _setCallKitSubscriptions({ getState }, next, action) {
  277. const { subscriptions } = getState()['features/callkit'];
  278. if (subscriptions) {
  279. for (const subscription of subscriptions) {
  280. subscription.remove();
  281. }
  282. }
  283. return next(action);
  284. }
  285. /**
  286. * Notifies the feature jwt that the action {@link SET_VIDEO_MUTED} is being
  287. * dispatched within a specific redux {@code store}.
  288. *
  289. * @param {Store} store - The redux store in which the specified {@code action}
  290. * is being dispatched.
  291. * @param {Dispatch} next - The redux dispatch function to dispatch the
  292. * specified {@code action} to the specified {@code store}.
  293. * @param {Action} action - The redux action {@code SET_VIDEO_MUTED} which is
  294. * being dispatched in the specified {@code store}.
  295. * @private
  296. * @returns {*}
  297. */
  298. function _setVideoMuted({ getState }, next, action) {
  299. const result = next(action);
  300. const conference = getCurrentConference(getState);
  301. if (conference && conference.callUUID) {
  302. CallKit.updateCall(
  303. conference.callUUID,
  304. { hasVideo: !isVideoMutedByAudioOnly(getState) });
  305. }
  306. return result;
  307. }