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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* @flow */
  2. import {
  3. createRecordingEvent,
  4. sendAnalytics
  5. } from '../analytics';
  6. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  7. import { CONFERENCE_JOIN_IN_PROGRESS, getCurrentConference } from '../base/conference';
  8. import JitsiMeetJS, {
  9. JitsiConferenceEvents,
  10. JitsiRecordingConstants
  11. } from '../base/lib-jitsi-meet';
  12. import { MEDIA_TYPE } from '../base/media';
  13. import { getParticipantDisplayName, updateLocalRecordingStatus } from '../base/participants';
  14. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  15. import {
  16. playSound,
  17. registerSound,
  18. stopSound,
  19. unregisterSound
  20. } from '../base/sounds';
  21. import { TRACK_ADDED } from '../base/tracks';
  22. import { NOTIFICATION_TIMEOUT_TYPE, showErrorNotification, showNotification } from '../notifications';
  23. import { RECORDING_SESSION_UPDATED, START_LOCAL_RECORDING, STOP_LOCAL_RECORDING } from './actionTypes';
  24. import {
  25. clearRecordingSessions,
  26. hidePendingRecordingNotification,
  27. showPendingRecordingNotification,
  28. showRecordingError,
  29. showRecordingLimitNotification,
  30. showRecordingWarning,
  31. showStartedRecordingNotification,
  32. showStoppedRecordingNotification,
  33. updateRecordingSessionData
  34. } from './actions';
  35. import LocalRecordingManager from './components/Recording/LocalRecordingManager';
  36. import {
  37. LIVE_STREAMING_OFF_SOUND_ID,
  38. LIVE_STREAMING_ON_SOUND_ID,
  39. RECORDING_OFF_SOUND_ID,
  40. RECORDING_ON_SOUND_ID
  41. } from './constants';
  42. import {
  43. getSessionById,
  44. getResourceId
  45. } from './functions';
  46. import logger from './logger';
  47. import {
  48. LIVE_STREAMING_OFF_SOUND_FILE,
  49. LIVE_STREAMING_ON_SOUND_FILE,
  50. RECORDING_OFF_SOUND_FILE,
  51. RECORDING_ON_SOUND_FILE
  52. } from './sounds';
  53. declare var APP: Object;
  54. declare var interfaceConfig: Object;
  55. /**
  56. * StateListenerRegistry provides a reliable way to detect the leaving of a
  57. * conference, where we need to clean up the recording sessions.
  58. */
  59. StateListenerRegistry.register(
  60. /* selector */ state => getCurrentConference(state),
  61. /* listener */ (conference, { dispatch }) => {
  62. if (!conference) {
  63. dispatch(clearRecordingSessions());
  64. }
  65. }
  66. );
  67. /**
  68. * The redux middleware to handle the recorder updates in a React way.
  69. *
  70. * @param {Store} store - The redux store.
  71. * @returns {Function}
  72. */
  73. MiddlewareRegistry.register(({ dispatch, getState }) => next => async action => {
  74. let oldSessionData;
  75. if (action.type === RECORDING_SESSION_UPDATED) {
  76. oldSessionData
  77. = getSessionById(getState(), action.sessionData.id);
  78. }
  79. const result = next(action);
  80. switch (action.type) {
  81. case APP_WILL_MOUNT:
  82. dispatch(registerSound(
  83. LIVE_STREAMING_OFF_SOUND_ID,
  84. LIVE_STREAMING_OFF_SOUND_FILE));
  85. dispatch(registerSound(
  86. LIVE_STREAMING_ON_SOUND_ID,
  87. LIVE_STREAMING_ON_SOUND_FILE));
  88. dispatch(registerSound(
  89. RECORDING_OFF_SOUND_ID,
  90. RECORDING_OFF_SOUND_FILE));
  91. dispatch(registerSound(
  92. RECORDING_ON_SOUND_ID,
  93. RECORDING_ON_SOUND_FILE));
  94. break;
  95. case APP_WILL_UNMOUNT:
  96. dispatch(unregisterSound(LIVE_STREAMING_OFF_SOUND_ID));
  97. dispatch(unregisterSound(LIVE_STREAMING_ON_SOUND_ID));
  98. dispatch(unregisterSound(RECORDING_OFF_SOUND_ID));
  99. dispatch(unregisterSound(RECORDING_ON_SOUND_ID));
  100. break;
  101. case CONFERENCE_JOIN_IN_PROGRESS: {
  102. const { conference } = action;
  103. conference.on(
  104. JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  105. recorderSession => {
  106. if (recorderSession) {
  107. recorderSession.getID() && dispatch(updateRecordingSessionData(recorderSession));
  108. recorderSession.getError() && _showRecordingErrorNotification(recorderSession, dispatch);
  109. }
  110. return;
  111. });
  112. break;
  113. }
  114. case START_LOCAL_RECORDING: {
  115. const { localRecording } = getState()['features/base/config'];
  116. const { onlySelf } = action;
  117. try {
  118. await LocalRecordingManager.startLocalRecording({ dispatch,
  119. getState }, action.onlySelf);
  120. const props = {
  121. descriptionKey: 'recording.on',
  122. titleKey: 'dialog.recording'
  123. };
  124. if (localRecording?.notifyAllParticipants && !onlySelf) {
  125. dispatch(playSound(RECORDING_ON_SOUND_ID));
  126. }
  127. dispatch(showNotification(props, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  128. dispatch(showNotification({
  129. titleKey: 'recording.localRecordingStartWarningTitle',
  130. descriptionKey: 'recording.localRecordingStartWarning'
  131. }, NOTIFICATION_TIMEOUT_TYPE.STICKY));
  132. dispatch(updateLocalRecordingStatus(true, onlySelf));
  133. sendAnalytics(createRecordingEvent('started', `local${onlySelf ? '.self' : ''}`));
  134. } catch (err) {
  135. logger.error('Capture failed', err);
  136. let descriptionKey = 'recording.error';
  137. if (err.message === 'WrongSurfaceSelected') {
  138. descriptionKey = 'recording.surfaceError';
  139. } else if (err.message === 'NoLocalStreams') {
  140. descriptionKey = 'recording.noStreams';
  141. }
  142. const props = {
  143. descriptionKey,
  144. titleKey: 'recording.failedToStart'
  145. };
  146. dispatch(showErrorNotification(props, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  147. }
  148. break;
  149. }
  150. case STOP_LOCAL_RECORDING: {
  151. const { localRecording } = getState()['features/base/config'];
  152. if (LocalRecordingManager.isRecordingLocally()) {
  153. LocalRecordingManager.stopLocalRecording();
  154. dispatch(updateLocalRecordingStatus(false));
  155. if (localRecording?.notifyAllParticipants && !LocalRecordingManager.selfRecording) {
  156. dispatch(playSound(RECORDING_OFF_SOUND_ID));
  157. }
  158. }
  159. break;
  160. }
  161. case RECORDING_SESSION_UPDATED: {
  162. // When in recorder mode no notifications are shown
  163. // or extra sounds are also not desired
  164. // but we want to indicate those in case of sip gateway
  165. const {
  166. iAmRecorder,
  167. iAmSipGateway,
  168. recordingLimit
  169. } = getState()['features/base/config'];
  170. if (iAmRecorder && !iAmSipGateway) {
  171. break;
  172. }
  173. const updatedSessionData
  174. = getSessionById(getState(), action.sessionData.id);
  175. const { initiator, mode, terminator } = updatedSessionData;
  176. const { PENDING, OFF, ON } = JitsiRecordingConstants.status;
  177. if (updatedSessionData.status === PENDING
  178. && (!oldSessionData || oldSessionData.status !== PENDING)) {
  179. dispatch(showPendingRecordingNotification(mode));
  180. } else if (updatedSessionData.status !== PENDING) {
  181. dispatch(hidePendingRecordingNotification(mode));
  182. if (updatedSessionData.status === ON
  183. && (!oldSessionData || oldSessionData.status !== ON)) {
  184. if (typeof recordingLimit === 'object') {
  185. // Show notification with additional information to the initiator.
  186. dispatch(showRecordingLimitNotification(mode));
  187. } else {
  188. dispatch(showStartedRecordingNotification(mode, initiator, action.sessionData.id));
  189. }
  190. sendAnalytics(createRecordingEvent('start', mode));
  191. let soundID;
  192. if (mode === JitsiRecordingConstants.mode.FILE) {
  193. soundID = RECORDING_ON_SOUND_ID;
  194. } else if (mode === JitsiRecordingConstants.mode.STREAM) {
  195. soundID = LIVE_STREAMING_ON_SOUND_ID;
  196. }
  197. if (soundID) {
  198. dispatch(playSound(soundID));
  199. }
  200. if (typeof APP !== 'undefined') {
  201. APP.API.notifyRecordingStatusChanged(true, mode);
  202. }
  203. } else if (updatedSessionData.status === OFF
  204. && (!oldSessionData || oldSessionData.status !== OFF)) {
  205. if (terminator) {
  206. dispatch(
  207. showStoppedRecordingNotification(
  208. mode, getParticipantDisplayName(getState, getResourceId(terminator))));
  209. }
  210. let duration = 0, soundOff, soundOn;
  211. if (oldSessionData && oldSessionData.timestamp) {
  212. duration
  213. = (Date.now() / 1000) - oldSessionData.timestamp;
  214. }
  215. sendAnalytics(createRecordingEvent('stop', mode, duration));
  216. if (mode === JitsiRecordingConstants.mode.FILE) {
  217. soundOff = RECORDING_OFF_SOUND_ID;
  218. soundOn = RECORDING_ON_SOUND_ID;
  219. } else if (mode === JitsiRecordingConstants.mode.STREAM) {
  220. soundOff = LIVE_STREAMING_OFF_SOUND_ID;
  221. soundOn = LIVE_STREAMING_ON_SOUND_ID;
  222. }
  223. if (soundOff && soundOn) {
  224. dispatch(stopSound(soundOn));
  225. dispatch(playSound(soundOff));
  226. }
  227. if (typeof APP !== 'undefined') {
  228. APP.API.notifyRecordingStatusChanged(false, mode);
  229. }
  230. }
  231. }
  232. break;
  233. }
  234. case TRACK_ADDED: {
  235. const { track } = action;
  236. if (LocalRecordingManager.isRecordingLocally() && track.mediaType === MEDIA_TYPE.AUDIO) {
  237. const audioTrack = track.jitsiTrack.track;
  238. LocalRecordingManager.addAudioTrackToLocalRecording(audioTrack);
  239. }
  240. break;
  241. }
  242. }
  243. return result;
  244. });
  245. /**
  246. * Shows a notification about an error in the recording session. A
  247. * default notification will display if no error is specified in the passed
  248. * in recording session.
  249. *
  250. * @private
  251. * @param {Object} recorderSession - The recorder session model from the
  252. * lib.
  253. * @param {Dispatch} dispatch - The Redux Dispatch function.
  254. * @returns {void}
  255. */
  256. function _showRecordingErrorNotification(recorderSession, dispatch) {
  257. const mode = recorderSession.getMode();
  258. const error = recorderSession.getError();
  259. const isStreamMode = mode === JitsiMeetJS.constants.recording.mode.STREAM;
  260. switch (error) {
  261. case JitsiMeetJS.constants.recording.error.SERVICE_UNAVAILABLE:
  262. dispatch(showRecordingError({
  263. descriptionKey: 'recording.unavailable',
  264. descriptionArguments: {
  265. serviceName: isStreamMode
  266. ? '$t(liveStreaming.serviceName)'
  267. : '$t(recording.serviceName)'
  268. },
  269. titleKey: isStreamMode
  270. ? 'liveStreaming.unavailableTitle'
  271. : 'recording.unavailableTitle'
  272. }));
  273. break;
  274. case JitsiMeetJS.constants.recording.error.RESOURCE_CONSTRAINT:
  275. dispatch(showRecordingError({
  276. descriptionKey: isStreamMode
  277. ? 'liveStreaming.busy'
  278. : 'recording.busy',
  279. titleKey: isStreamMode
  280. ? 'liveStreaming.busyTitle'
  281. : 'recording.busyTitle'
  282. }));
  283. break;
  284. case JitsiMeetJS.constants.recording.error.UNEXPECTED_REQUEST:
  285. dispatch(showRecordingWarning({
  286. descriptionKey: isStreamMode
  287. ? 'liveStreaming.sessionAlreadyActive'
  288. : 'recording.sessionAlreadyActive',
  289. titleKey: isStreamMode ? 'liveStreaming.inProgress' : 'recording.inProgress'
  290. }));
  291. break;
  292. default:
  293. dispatch(showRecordingError({
  294. descriptionKey: isStreamMode
  295. ? 'liveStreaming.error'
  296. : 'recording.error',
  297. titleKey: isStreamMode
  298. ? 'liveStreaming.failedToStart'
  299. : 'recording.failedToStart'
  300. }));
  301. break;
  302. }
  303. if (typeof APP !== 'undefined') {
  304. APP.API.notifyRecordingStatusChanged(false, mode, error);
  305. }
  306. }