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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* @flow */
  2. import { CONFERENCE_WILL_JOIN, getCurrentConference } from '../base/conference';
  3. import JitsiMeetJS, {
  4. JitsiConferenceEvents,
  5. JitsiRecordingConstants
  6. } from '../base/lib-jitsi-meet';
  7. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  8. import {
  9. playSound,
  10. registerSound,
  11. stopSound,
  12. unregisterSound
  13. } from '../base/sounds';
  14. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  15. import {
  16. clearRecordingSessions,
  17. hidePendingRecordingNotification,
  18. showPendingRecordingNotification,
  19. showRecordingError,
  20. showStoppedRecordingNotification,
  21. updateRecordingSessionData
  22. } from './actions';
  23. import { RECORDING_SESSION_UPDATED } from './actionTypes';
  24. import { RECORDING_OFF_SOUND_ID, RECORDING_ON_SOUND_ID } from './constants';
  25. import { getSessionById } from './functions';
  26. import {
  27. RECORDING_OFF_SOUND_FILE,
  28. RECORDING_ON_SOUND_FILE
  29. } from './sounds';
  30. /**
  31. * StateListenerRegistry provides a reliable way to detect the leaving of a
  32. * conference, where we need to clean up the recording sessions.
  33. */
  34. StateListenerRegistry.register(
  35. /* selector */ state => getCurrentConference(state),
  36. /* listener */ (conference, { dispatch }) => {
  37. if (!conference) {
  38. dispatch(clearRecordingSessions());
  39. }
  40. }
  41. );
  42. /**
  43. * The redux middleware to handle the recorder updates in a React way.
  44. *
  45. * @param {Store} store - The redux store.
  46. * @returns {Function}
  47. */
  48. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  49. let oldSessionData;
  50. if (action.type === RECORDING_SESSION_UPDATED) {
  51. oldSessionData
  52. = getSessionById(getState(), action.sessionData.id);
  53. }
  54. const result = next(action);
  55. switch (action.type) {
  56. case APP_WILL_MOUNT:
  57. dispatch(registerSound(
  58. RECORDING_OFF_SOUND_ID,
  59. RECORDING_OFF_SOUND_FILE));
  60. dispatch(registerSound(
  61. RECORDING_ON_SOUND_ID,
  62. RECORDING_ON_SOUND_FILE));
  63. break;
  64. case APP_WILL_UNMOUNT:
  65. dispatch(unregisterSound(RECORDING_OFF_SOUND_ID));
  66. dispatch(unregisterSound(RECORDING_ON_SOUND_ID));
  67. break;
  68. case CONFERENCE_WILL_JOIN: {
  69. const { conference } = action;
  70. conference.on(
  71. JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  72. recorderSession => {
  73. if (recorderSession) {
  74. recorderSession.getID()
  75. && dispatch(
  76. updateRecordingSessionData(recorderSession));
  77. recorderSession.getError()
  78. && _showRecordingErrorNotification(
  79. recorderSession, dispatch);
  80. }
  81. return;
  82. });
  83. break;
  84. }
  85. case RECORDING_SESSION_UPDATED: {
  86. const updatedSessionData
  87. = getSessionById(getState(), action.sessionData.id);
  88. if (updatedSessionData.mode === JitsiRecordingConstants.mode.FILE) {
  89. const { PENDING, OFF, ON } = JitsiRecordingConstants.status;
  90. if (updatedSessionData.status === PENDING
  91. && (!oldSessionData || oldSessionData.status !== PENDING)) {
  92. dispatch(showPendingRecordingNotification());
  93. } else if (updatedSessionData.status !== PENDING) {
  94. dispatch(hidePendingRecordingNotification());
  95. if (updatedSessionData.status === ON
  96. && (!oldSessionData || oldSessionData.status !== ON)) {
  97. dispatch(playSound(RECORDING_ON_SOUND_ID));
  98. } else if (updatedSessionData.status === OFF
  99. && (!oldSessionData || oldSessionData.status !== OFF)) {
  100. dispatch(stopSound(RECORDING_ON_SOUND_ID));
  101. dispatch(playSound(RECORDING_OFF_SOUND_ID));
  102. dispatch(showStoppedRecordingNotification());
  103. }
  104. }
  105. }
  106. break;
  107. }
  108. }
  109. return result;
  110. });
  111. /**
  112. * Shows a notification about an error in the recording session. A
  113. * default notification will display if no error is specified in the passed
  114. * in recording session.
  115. *
  116. * @private
  117. * @param {Object} recorderSession - The recorder session model from the
  118. * lib.
  119. * @param {Dispatch} dispatch - The Redux Dispatch function.
  120. * @returns {void}
  121. */
  122. function _showRecordingErrorNotification(recorderSession, dispatch) {
  123. const isStreamMode
  124. = recorderSession.getMode()
  125. === JitsiMeetJS.constants.recording.mode.STREAM;
  126. switch (recorderSession.getError()) {
  127. case JitsiMeetJS.constants.recording.error.SERVICE_UNAVAILABLE:
  128. dispatch(showRecordingError({
  129. descriptionKey: 'recording.unavailable',
  130. descriptionArguments: {
  131. serviceName: isStreamMode
  132. ? 'Live Streaming service'
  133. : 'Recording service'
  134. },
  135. titleKey: isStreamMode
  136. ? 'liveStreaming.unavailableTitle'
  137. : 'recording.unavailableTitle'
  138. }));
  139. break;
  140. case JitsiMeetJS.constants.recording.error.RESOURCE_CONSTRAINT:
  141. dispatch(showRecordingError({
  142. descriptionKey: isStreamMode
  143. ? 'liveStreaming.busy'
  144. : 'recording.busy',
  145. titleKey: isStreamMode
  146. ? 'liveStreaming.busyTitle'
  147. : 'recording.busyTitle'
  148. }));
  149. break;
  150. default:
  151. dispatch(showRecordingError({
  152. descriptionKey: isStreamMode
  153. ? 'liveStreaming.error'
  154. : 'recording.error',
  155. titleKey: isStreamMode
  156. ? 'liveStreaming.failedToStart'
  157. : 'recording.failedToStart'
  158. }));
  159. break;
  160. }
  161. }