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

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