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

middleware.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. isVideoMutedByAudioOnly,
  21. setAudioMuted
  22. } from '../../base/media';
  23. import { MiddlewareRegistry } from '../../base/redux';
  24. import {
  25. TRACK_ADDED,
  26. TRACK_REMOVED,
  27. TRACK_UPDATED,
  28. isLocalTrackMuted
  29. } from '../../base/tracks';
  30. import { _SET_CALLKIT_SUBSCRIPTIONS } from './actionTypes';
  31. import CallKit from './CallKit';
  32. /**
  33. * Middleware that captures system actions and hooks up CallKit.
  34. *
  35. * @param {Store} store - The redux store.
  36. * @returns {Function}
  37. */
  38. CallKit && MiddlewareRegistry.register(store => next => action => {
  39. switch (action.type) {
  40. case _SET_CALLKIT_SUBSCRIPTIONS:
  41. return _setCallKitSubscriptions(store, next, action);
  42. case APP_WILL_MOUNT:
  43. return _appWillMount(store, next, action);
  44. case APP_WILL_UNMOUNT:
  45. store.dispatch({
  46. type: _SET_CALLKIT_SUBSCRIPTIONS,
  47. subscriptions: undefined
  48. });
  49. break;
  50. case CONFERENCE_FAILED:
  51. return _conferenceFailed(store, next, action);
  52. case CONFERENCE_JOINED:
  53. return _conferenceJoined(store, next, action);
  54. case CONFERENCE_LEFT:
  55. return _conferenceLeft(store, next, action);
  56. case CONFERENCE_WILL_JOIN:
  57. return _conferenceWillJoin(store, next, action);
  58. case TRACK_ADDED:
  59. case TRACK_REMOVED:
  60. case TRACK_UPDATED:
  61. return _syncTrackState(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 { callHandle, callUUID } = state['features/base/config'];
  197. const url = getInviteURL(state);
  198. const handle = callHandle || url.toString();
  199. const hasVideo = !isVideoMutedByAudioOnly(state);
  200. // When assigning the call UUID, do so in upper case, since iOS will return
  201. // it upper cased.
  202. conference.callUUID = (callUUID || uuid.v4()).toUpperCase();
  203. CallKit.startCall(conference.callUUID, handle, hasVideo)
  204. .then(() => {
  205. const { callee } = state['features/base/jwt'];
  206. const displayName
  207. = state['features/base/config'].callDisplayName
  208. || (callee && callee.name)
  209. || state['features/base/conference'].room;
  210. const muted
  211. = isLocalTrackMuted(
  212. state['features/base/tracks'],
  213. MEDIA_TYPE.AUDIO);
  214. CallKit.updateCall(conference.callUUID, { displayName });
  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: newValue }) {
  245. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  246. const conference = getCurrentConference(getState);
  247. if (conference && conference.callUUID === callUUID) {
  248. // Break the loop. Audio can be muted from both CallKit and Jitsi Meet.
  249. // We must keep them in sync, but at some point the loop needs to be
  250. // broken. We are doing it here, on the CallKit handler.
  251. const tracks = getState()['features/base/tracks'];
  252. const oldValue = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  253. newValue = Boolean(newValue); // eslint-disable-line no-param-reassign
  254. if (oldValue !== newValue) {
  255. sendAnalytics(createTrackMutedEvent('audio', 'callkit', newValue));
  256. dispatch(setAudioMuted(newValue, /* ensureTrack */ true));
  257. }
  258. }
  259. }
  260. /**
  261. * Notifies the feature callkit that the action
  262. * {@link _SET_CALLKIT_SUBSCRIPTIONS} is being dispatched within a specific
  263. * 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 {@code dispatch} function to dispatch the
  268. * specified {@code action} in the specified {@code store}.
  269. * @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
  270. * which is being dispatched in the specified {@code store}.
  271. * @private
  272. * @returns {*} The value returned by {@code next(action)}.
  273. */
  274. function _setCallKitSubscriptions({ getState }, next, action) {
  275. const { subscriptions } = getState()['features/callkit'];
  276. if (subscriptions) {
  277. for (const subscription of subscriptions) {
  278. subscription.remove();
  279. }
  280. }
  281. return next(action);
  282. }
  283. /**
  284. * Synchronize the muted state of tracks with CallKit.
  285. *
  286. * @param {Store} store - The redux store in which the specified {@code action}
  287. * is being dispatched.
  288. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  289. * specified {@code action} in the specified {@code store}.
  290. * @param {Action} action - The redux action which is being dispatched in the
  291. * specified {@code store}.
  292. * @private
  293. * @returns {*} The value returned by {@code next(action)}.
  294. */
  295. function _syncTrackState({ getState }, next, action) {
  296. const result = next(action);
  297. const { track } = action;
  298. const state = getState();
  299. const conference = getCurrentConference(state);
  300. if (track.local && conference && conference.callUUID) {
  301. const tracks = state['features/base/tracks'];
  302. const muted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  303. CallKit.setMuted(conference.callUUID, muted);
  304. CallKit.updateCall(
  305. conference.callUUID,
  306. { hasVideo: !isVideoMutedByAudioOnly(state) });
  307. }
  308. return result;
  309. }