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

middleware.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* @flow */
  2. import Logger from 'jitsi-meet-logger';
  3. import { CONFERENCE_WILL_JOIN } from '../base/conference';
  4. import {
  5. SIP_GW_AVAILABILITY_CHANGED,
  6. SIP_GW_INVITE_ROOMS
  7. } from './actionTypes';
  8. import {
  9. JitsiConferenceEvents,
  10. JitsiSIPVideoGWStatus
  11. } from '../base/lib-jitsi-meet';
  12. import { MiddlewareRegistry } from '../base/redux';
  13. import {
  14. showErrorNotification,
  15. showNotification,
  16. showWarningNotification
  17. } from '../notifications';
  18. const logger = Logger.getLogger(__filename);
  19. /**
  20. * Middleware that captures conference video sip gw events and stores
  21. * the global sip gw availability in redux or show appropriate notification
  22. * for sip gw sessions.
  23. * Captures invitation actions that create sip gw sessions or display
  24. * appropriate error/warning notifications.
  25. *
  26. * @param {Store} store - The redux store.
  27. * @returns {Function}
  28. */
  29. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  30. const result = next(action);
  31. switch (action.type) {
  32. case CONFERENCE_WILL_JOIN: {
  33. const conference = getState()['features/base/conference'].joining;
  34. conference.on(
  35. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  36. (...args) => dispatch(_availabilityChanged(...args)));
  37. conference.on(
  38. JitsiConferenceEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  39. event => {
  40. const toDispatch = _sessionStateChanged(event);
  41. // sessionStateChanged can decide there is nothing to dispatch
  42. if (toDispatch) {
  43. dispatch(toDispatch);
  44. }
  45. });
  46. break;
  47. }
  48. case SIP_GW_INVITE_ROOMS:
  49. _inviteRooms(action.rooms, action.conference, dispatch);
  50. break;
  51. }
  52. return result;
  53. });
  54. /**
  55. * Signals that sip gw availability had changed.
  56. *
  57. * @param {string} status - The new status of the service.
  58. * @returns {{
  59. * type: SIP_GW_AVAILABILITY_CHANGED,
  60. * status: string
  61. * }}
  62. * @private
  63. */
  64. function _availabilityChanged(status: string) {
  65. return {
  66. type: SIP_GW_AVAILABILITY_CHANGED,
  67. status
  68. };
  69. }
  70. /**
  71. * Processes the action from the actionType {@code SIP_GW_INVITE_ROOMS} by
  72. * inviting rooms into the conference or showing an error message.
  73. *
  74. * @param {Array} rooms - The conference rooms to invite.
  75. * @param {Object} conference - The JitsiConference to invite the rooms to.
  76. * @param {Function} dispatch - The redux dispatch function for emitting state
  77. * changes (queuing error notifications).
  78. * @private
  79. * @returns {void}
  80. */
  81. function _inviteRooms(rooms, conference, dispatch) {
  82. for (const room of rooms) {
  83. const { id: sipAddress, name: displayName } = room;
  84. if (sipAddress && displayName) {
  85. const newSession = conference
  86. .createVideoSIPGWSession(sipAddress, displayName);
  87. if (newSession instanceof Error) {
  88. const e = newSession;
  89. switch (e.message) {
  90. case JitsiSIPVideoGWStatus.ERROR_NO_CONNECTION: {
  91. dispatch(showErrorNotification({
  92. descriptionKey: 'videoSIPGW.errorInvite',
  93. titleKey: 'videoSIPGW.errorInviteTitle'
  94. }));
  95. return;
  96. }
  97. case JitsiSIPVideoGWStatus.ERROR_SESSION_EXISTS: {
  98. dispatch(showWarningNotification({
  99. titleKey: 'videoSIPGW.errorAlreadyInvited',
  100. titleArguments: { displayName }
  101. }));
  102. return;
  103. }
  104. }
  105. logger.error(
  106. 'Unknown error trying to create sip videogw session',
  107. e);
  108. return;
  109. }
  110. newSession.start();
  111. } else {
  112. logger.error(`No display name or sip number for ${
  113. JSON.stringify(room)}`);
  114. }
  115. }
  116. }
  117. /**
  118. * Signals that a session we created has a change in its status.
  119. *
  120. * @param {string} event - The event describing the session state change.
  121. * @returns {{
  122. * type: SHOW_NOTIFICATION
  123. * }}|null
  124. * @private
  125. */
  126. function _sessionStateChanged(
  127. event: Object) {
  128. switch (event.newState) {
  129. case JitsiSIPVideoGWStatus.STATE_PENDING: {
  130. return showNotification({
  131. titleKey: 'videoSIPGW.pending',
  132. titleArguments: {
  133. displayName: event.displayName
  134. }
  135. }, 2000);
  136. }
  137. case JitsiSIPVideoGWStatus.STATE_FAILED: {
  138. return showErrorNotification({
  139. titleKey: 'videoSIPGW.errorInviteFailedTitle',
  140. titleArguments: {
  141. displayName: event.displayName
  142. },
  143. descriptionKey: 'videoSIPGW.errorInviteFailed'
  144. });
  145. }
  146. case JitsiSIPVideoGWStatus.STATE_OFF: {
  147. if (event.failureReason === JitsiSIPVideoGWStatus.STATUS_BUSY) {
  148. return showErrorNotification({
  149. descriptionKey: 'videoSIPGW.busy',
  150. titleKey: 'videoSIPGW.busyTitle'
  151. });
  152. } else if (event.failureReason) {
  153. logger.error(`Unknown sip videogw error ${event.newState} ${
  154. event.failureReason}`);
  155. }
  156. }
  157. }
  158. // nothing to show
  159. return null;
  160. }