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

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