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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. if (typeof APP !== 'undefined') {
  135. APP.API.notifyRecordingStatusChanged(true, 'local');
  136. }
  137. } catch (err) {
  138. logger.error('Capture failed', err);
  139. let descriptionKey = 'recording.error';
  140. if (err.message === 'WrongSurfaceSelected') {
  141. descriptionKey = 'recording.surfaceError';
  142. } else if (err.message === 'NoLocalStreams') {
  143. descriptionKey = 'recording.noStreams';
  144. }
  145. const props = {
  146. descriptionKey,
  147. titleKey: 'recording.failedToStart'
  148. };
  149. if (typeof APP !== 'undefined') {
  150. APP.API.notifyRecordingStatusChanged(false, 'local', err.message);
  151. }
  152. dispatch(showErrorNotification(props, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  153. }
  154. break;
  155. }
  156. case STOP_LOCAL_RECORDING: {
  157. const { localRecording } = getState()['features/base/config'];
  158. if (LocalRecordingManager.isRecordingLocally()) {
  159. LocalRecordingManager.stopLocalRecording();
  160. dispatch(updateLocalRecordingStatus(false));
  161. if (localRecording?.notifyAllParticipants && !LocalRecordingManager.selfRecording) {
  162. dispatch(playSound(RECORDING_OFF_SOUND_ID));
  163. }
  164. if (typeof APP !== 'undefined') {
  165. APP.API.notifyRecordingStatusChanged(false, 'local');
  166. }
  167. }
  168. break;
  169. }
  170. case RECORDING_SESSION_UPDATED: {
  171. // When in recorder mode no notifications are shown
  172. // or extra sounds are also not desired
  173. // but we want to indicate those in case of sip gateway
  174. const {
  175. iAmRecorder,
  176. iAmSipGateway,
  177. recordingLimit
  178. } = getState()['features/base/config'];
  179. if (iAmRecorder && !iAmSipGateway) {
  180. break;
  181. }
  182. const updatedSessionData
  183. = getSessionById(getState(), action.sessionData.id);
  184. const { initiator, mode, terminator } = updatedSessionData;
  185. const { PENDING, OFF, ON } = JitsiRecordingConstants.status;
  186. if (updatedSessionData.status === PENDING
  187. && (!oldSessionData || oldSessionData.status !== PENDING)) {
  188. dispatch(showPendingRecordingNotification(mode));
  189. } else if (updatedSessionData.status !== PENDING) {
  190. dispatch(hidePendingRecordingNotification(mode));
  191. if (updatedSessionData.status === ON
  192. && (!oldSessionData || oldSessionData.status !== ON)) {
  193. if (typeof recordingLimit === 'object') {
  194. // Show notification with additional information to the initiator.
  195. dispatch(showRecordingLimitNotification(mode));
  196. } else {
  197. dispatch(showStartedRecordingNotification(mode, initiator, action.sessionData.id));
  198. }
  199. sendAnalytics(createRecordingEvent('start', mode));
  200. let soundID;
  201. if (mode === JitsiRecordingConstants.mode.FILE) {
  202. soundID = RECORDING_ON_SOUND_ID;
  203. } else if (mode === JitsiRecordingConstants.mode.STREAM) {
  204. soundID = LIVE_STREAMING_ON_SOUND_ID;
  205. }
  206. if (soundID) {
  207. dispatch(playSound(soundID));
  208. }
  209. if (typeof APP !== 'undefined') {
  210. APP.API.notifyRecordingStatusChanged(true, mode);
  211. }
  212. } else if (updatedSessionData.status === OFF
  213. && (!oldSessionData || oldSessionData.status !== OFF)) {
  214. if (terminator) {
  215. dispatch(
  216. showStoppedRecordingNotification(
  217. mode, getParticipantDisplayName(getState, getResourceId(terminator))));
  218. }
  219. let duration = 0, soundOff, soundOn;
  220. if (oldSessionData && oldSessionData.timestamp) {
  221. duration
  222. = (Date.now() / 1000) - oldSessionData.timestamp;
  223. }
  224. sendAnalytics(createRecordingEvent('stop', mode, duration));
  225. if (mode === JitsiRecordingConstants.mode.FILE) {
  226. soundOff = RECORDING_OFF_SOUND_ID;
  227. soundOn = RECORDING_ON_SOUND_ID;
  228. } else if (mode === JitsiRecordingConstants.mode.STREAM) {
  229. soundOff = LIVE_STREAMING_OFF_SOUND_ID;
  230. soundOn = LIVE_STREAMING_ON_SOUND_ID;
  231. }
  232. if (soundOff && soundOn) {
  233. dispatch(stopSound(soundOn));
  234. dispatch(playSound(soundOff));
  235. }
  236. if (typeof APP !== 'undefined') {
  237. APP.API.notifyRecordingStatusChanged(false, mode);
  238. }
  239. }
  240. }
  241. break;
  242. }
  243. case TRACK_ADDED: {
  244. const { track } = action;
  245. if (LocalRecordingManager.isRecordingLocally() && track.mediaType === MEDIA_TYPE.AUDIO) {
  246. const audioTrack = track.jitsiTrack.track;
  247. LocalRecordingManager.addAudioTrackToLocalRecording(audioTrack);
  248. }
  249. break;
  250. }
  251. }
  252. return result;
  253. });
  254. /**
  255. * Shows a notification about an error in the recording session. A
  256. * default notification will display if no error is specified in the passed
  257. * in recording session.
  258. *
  259. * @private
  260. * @param {Object} recorderSession - The recorder session model from the
  261. * lib.
  262. * @param {Dispatch} dispatch - The Redux Dispatch function.
  263. * @returns {void}
  264. */
  265. function _showRecordingErrorNotification(recorderSession, dispatch) {
  266. const mode = recorderSession.getMode();
  267. const error = recorderSession.getError();
  268. const isStreamMode = mode === JitsiMeetJS.constants.recording.mode.STREAM;
  269. switch (error) {
  270. case JitsiMeetJS.constants.recording.error.SERVICE_UNAVAILABLE:
  271. dispatch(showRecordingError({
  272. descriptionKey: 'recording.unavailable',
  273. descriptionArguments: {
  274. serviceName: isStreamMode
  275. ? '$t(liveStreaming.serviceName)'
  276. : '$t(recording.serviceName)'
  277. },
  278. titleKey: isStreamMode
  279. ? 'liveStreaming.unavailableTitle'
  280. : 'recording.unavailableTitle'
  281. }));
  282. break;
  283. case JitsiMeetJS.constants.recording.error.RESOURCE_CONSTRAINT:
  284. dispatch(showRecordingError({
  285. descriptionKey: isStreamMode
  286. ? 'liveStreaming.busy'
  287. : 'recording.busy',
  288. titleKey: isStreamMode
  289. ? 'liveStreaming.busyTitle'
  290. : 'recording.busyTitle'
  291. }));
  292. break;
  293. case JitsiMeetJS.constants.recording.error.UNEXPECTED_REQUEST:
  294. dispatch(showRecordingWarning({
  295. descriptionKey: isStreamMode
  296. ? 'liveStreaming.sessionAlreadyActive'
  297. : 'recording.sessionAlreadyActive',
  298. titleKey: isStreamMode ? 'liveStreaming.inProgress' : 'recording.inProgress'
  299. }));
  300. break;
  301. default:
  302. dispatch(showRecordingError({
  303. descriptionKey: isStreamMode
  304. ? 'liveStreaming.error'
  305. : 'recording.error',
  306. titleKey: isStreamMode
  307. ? 'liveStreaming.failedToStart'
  308. : 'recording.failedToStart'
  309. }));
  310. break;
  311. }
  312. if (typeof APP !== 'undefined') {
  313. APP.API.notifyRecordingStatusChanged(false, mode, error);
  314. }
  315. }