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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 { room } = state['features/base/conference'];
  205. const { callee } = state['features/base/jwt'];
  206. const tracks = state['features/base/tracks'];
  207. const muted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  208. CallKit.updateCall(
  209. conference.callUUID,
  210. { displayName: (callee && callee.name) || room });
  211. CallKit.setMuted(conference.callUUID, muted);
  212. });
  213. return result;
  214. }
  215. /**
  216. * Handles CallKit's event {@code performEndCallAction}.
  217. *
  218. * @param {Object} event - The details of the CallKit event
  219. * {@code performEndCallAction}.
  220. * @returns {void}
  221. */
  222. function _onPerformEndCallAction({ callUUID }) {
  223. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  224. const conference = getCurrentConference(getState);
  225. if (conference && conference.callUUID === callUUID) {
  226. // We arrive here when a call is ended by the system, for example, when
  227. // another incoming call is received and the user selects "End &
  228. // Accept".
  229. delete conference.callUUID;
  230. dispatch(appNavigate(undefined));
  231. }
  232. }
  233. /**
  234. * Handles CallKit's event {@code performSetMutedCallAction}.
  235. *
  236. * @param {Object} event - The details of the CallKit event
  237. * {@code performSetMutedCallAction}.
  238. * @returns {void}
  239. */
  240. function _onPerformSetMutedCallAction({ callUUID, muted: newValue }) {
  241. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  242. const conference = getCurrentConference(getState);
  243. if (conference && conference.callUUID === callUUID) {
  244. // Break the loop. Audio can be muted from both CallKit and Jitsi Meet.
  245. // We must keep them in sync, but at some point the loop needs to be
  246. // broken. We are doing it here, on the CallKit handler.
  247. const { muted: oldValue } = getState()['features/base/media'].audio;
  248. if (oldValue !== newValue) {
  249. const value = Boolean(newValue);
  250. sendAnalytics(createTrackMutedEvent('audio', 'callkit', value));
  251. dispatch(setAudioMuted(
  252. value, VIDEO_MUTISM_AUTHORITY.USER, /* ensureTrack */ true));
  253. }
  254. }
  255. }
  256. /**
  257. * Notifies the feature callkit that the action {@link SET_AUDIO_MUTED} is being
  258. * dispatched within a specific redux {@code store}.
  259. *
  260. * @param {Store} store - The redux store in which the specified {@code action}
  261. * is being dispatched.
  262. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  263. * specified {@code action} in the specified {@code store}.
  264. * @param {Action} action - The redux action {@code SET_AUDIO_MUTED} which is
  265. * being dispatched in the specified {@code store}.
  266. * @private
  267. * @returns {*} The value returned by {@code next(action)}.
  268. */
  269. function _setAudioMuted({ getState }, next, action) {
  270. const result = next(action);
  271. const conference = getCurrentConference(getState);
  272. if (conference && conference.callUUID) {
  273. CallKit.setMuted(conference.callUUID, action.muted);
  274. }
  275. return result;
  276. }
  277. /**
  278. * Notifies the feature callkit that the action
  279. * {@link _SET_CALLKIT_SUBSCRIPTIONS} is being dispatched within a specific
  280. * redux {@code store}.
  281. *
  282. * @param {Store} store - The redux store in which the specified {@code action}
  283. * is being dispatched.
  284. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  285. * specified {@code action} in the specified {@code store}.
  286. * @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
  287. * which is being dispatched in the specified {@code store}.
  288. * @private
  289. * @returns {*} The value returned by {@code next(action)}.
  290. */
  291. function _setCallKitSubscriptions({ getState }, next, action) {
  292. const { subscriptions } = getState()['features/callkit'];
  293. if (subscriptions) {
  294. for (const subscription of subscriptions) {
  295. subscription.remove();
  296. }
  297. }
  298. return next(action);
  299. }
  300. /**
  301. * Notifies the feature callkit that the action {@link SET_VIDEO_MUTED} is being
  302. * dispatched within a specific redux {@code store}.
  303. *
  304. * @param {Store} store - The redux store in which the specified {@code action}
  305. * is being dispatched.
  306. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  307. * specified {@code action} in the specified {@code store}.
  308. * @param {Action} action - The redux action {@code SET_VIDEO_MUTED} which is
  309. * being dispatched in the specified {@code store}.
  310. * @private
  311. * @returns {*} The value returned by {@code next(action)}.
  312. */
  313. function _setVideoMuted({ getState }, next, action) {
  314. const result = next(action);
  315. const conference = getCurrentConference(getState);
  316. if (conference && conference.callUUID) {
  317. CallKit.updateCall(
  318. conference.callUUID,
  319. { hasVideo: !isVideoMutedByAudioOnly(getState) });
  320. }
  321. return result;
  322. }
  323. /**
  324. * Handles a track creation failure. This is relevant to us in the following
  325. * (corner) case: if the user never gave their permission to use the microphone
  326. * and try to unmute from the CallKit interface, this will fail, and we need to
  327. * sync back the CallKit button state.
  328. *
  329. * @param {Store} store - The redux store in which the specified {@code action}
  330. * is being dispatched.
  331. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  332. * specified {@code action} in the specified {@code store}.
  333. * @param {Action} action - The redux action {@code TRACK_CREARE_ERROR} which is
  334. * being dispatched in the specified {@code store}.
  335. * @private
  336. * @returns {*} The value returned by {@code next(action)}.
  337. */
  338. function _trackCreateError({ getState }, next, action) {
  339. const result = next(action);
  340. const state = getState();
  341. const conference = getCurrentConference(state);
  342. if (conference && conference.callUUID) {
  343. const tracks = state['features/base/tracks'];
  344. const muted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  345. CallKit.setMuted(conference.callUUID, muted);
  346. }
  347. return result;
  348. }