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

middleware.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 }) {
  249. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  250. const conference = getCurrentConference(getState);
  251. if (conference && conference.callUUID === callUUID) {
  252. muted = Boolean(muted); // eslint-disable-line no-param-reassign
  253. sendAnalytics(createTrackMutedEvent('audio', 'callkit', muted));
  254. dispatch(setAudioMuted(muted, /* ensureTrack */ true));
  255. }
  256. }
  257. /**
  258. * Update CallKit with the audio only state of the conference. When a conference
  259. * is in audio only mode we will tell CallKit the call has no video. This
  260. * affects how the call is saved in the recent calls list.
  261. *
  262. * XXX: Note that here we are taking the `audioOnly` value straight from the
  263. * action, instead of examining the state. This is intentional, as setting the
  264. * audio only involves multiple actions which will be reflected in the state
  265. * later, but we are just interested in knowing if the mode is going to be
  266. * set or not.
  267. *
  268. * @param {Store} store - The redux store in which the specified {@code action}
  269. * is being dispatched.
  270. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  271. * specified {@code action} in the specified {@code store}.
  272. * @param {Action} action - The redux action which is being dispatched in the
  273. * specified {@code store}.
  274. * @private
  275. * @returns {*} The value returned by {@code next(action)}.
  276. */
  277. function _setAudioOnly({ getState }, next, action) {
  278. const result = next(action);
  279. const state = getState();
  280. const conference = getCurrentConference(state);
  281. if (conference && conference.callUUID) {
  282. CallKit.updateCall(
  283. conference.callUUID,
  284. { hasVideo: !action.audioOnly });
  285. }
  286. return result;
  287. }
  288. /**
  289. * Notifies the feature callkit that the action
  290. * {@link _SET_CALLKIT_SUBSCRIPTIONS} is being dispatched within a specific
  291. * redux {@code store}.
  292. *
  293. * @param {Store} store - The redux store in which the specified {@code action}
  294. * is being dispatched.
  295. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  296. * specified {@code action} in the specified {@code store}.
  297. * @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
  298. * which is being dispatched in the specified {@code store}.
  299. * @private
  300. * @returns {*} The value returned by {@code next(action)}.
  301. */
  302. function _setCallKitSubscriptions({ getState }, next, action) {
  303. const { subscriptions } = getState()['features/callkit'];
  304. if (subscriptions) {
  305. for (const subscription of subscriptions) {
  306. subscription.remove();
  307. }
  308. }
  309. return next(action);
  310. }
  311. /**
  312. * Synchronize the muted state of tracks with CallKit.
  313. *
  314. * @param {Store} store - The redux store in which the specified {@code action}
  315. * is being dispatched.
  316. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  317. * specified {@code action} in the specified {@code store}.
  318. * @param {Action} action - The redux action which is being dispatched in the
  319. * specified {@code store}.
  320. * @private
  321. * @returns {*} The value returned by {@code next(action)}.
  322. */
  323. function _syncTrackState({ getState }, next, action) {
  324. const result = next(action);
  325. const { jitsiTrack } = action.track;
  326. const state = getState();
  327. const conference = getCurrentConference(state);
  328. if (jitsiTrack.isLocal() && conference && conference.callUUID) {
  329. switch (jitsiTrack.getType()) {
  330. case 'audio': {
  331. const tracks = state['features/base/tracks'];
  332. const muted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  333. CallKit.setMuted(conference.callUUID, muted);
  334. break;
  335. }
  336. case 'video': {
  337. CallKit.updateCall(
  338. conference.callUUID,
  339. { hasVideo: !isVideoMutedByAudioOnly(state) });
  340. break;
  341. }
  342. }
  343. }
  344. return result;
  345. }