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