您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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. isVideoMutedByAudioOnly,
  23. setAudioMuted
  24. } from '../../base/media';
  25. import { MiddlewareRegistry } from '../../base/redux';
  26. import { TRACK_CREATE_ERROR, isLocalTrackMuted } 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_MUTED:
  56. return _setAudioMuted(store, next, action);
  57. case SET_VIDEO_MUTED:
  58. return _setVideoMuted(store, next, action);
  59. case TRACK_CREATE_ERROR:
  60. return _trackCreateError(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. 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 tracks = getState()['features/base/tracks'];
  251. const oldValue = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  252. if (oldValue !== newValue) {
  253. const value = Boolean(newValue);
  254. sendAnalytics(createTrackMutedEvent('audio', 'callkit', value));
  255. dispatch(setAudioMuted(value, /* 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. }