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

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