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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { hideNotification } from '../../notifications';
  4. import { isPrejoinPageVisible } from '../../prejoin/functions';
  5. import { getAvailableDevices } from '../devices/actions';
  6. import {
  7. CAMERA_FACING_MODE,
  8. MEDIA_TYPE,
  9. SET_AUDIO_MUTED,
  10. SET_CAMERA_FACING_MODE,
  11. SET_VIDEO_MUTED,
  12. VIDEO_MUTISM_AUTHORITY,
  13. TOGGLE_CAMERA_FACING_MODE,
  14. toggleCameraFacingMode
  15. } from '../media';
  16. import { MiddlewareRegistry } from '../redux';
  17. import {
  18. TRACK_ADDED,
  19. TOGGLE_SCREENSHARING,
  20. TRACK_NO_DATA_FROM_SOURCE,
  21. TRACK_REMOVED,
  22. TRACK_UPDATED
  23. } from './actionTypes';
  24. import {
  25. createLocalTracksA,
  26. showNoDataFromSourceVideoError,
  27. trackNoDataFromSourceNotificationInfoChanged
  28. } from './actions';
  29. import {
  30. getLocalTrack,
  31. getTrackByJitsiTrack,
  32. isUserInteractionRequiredForUnmute,
  33. setTrackMuted
  34. } from './functions';
  35. declare var APP: Object;
  36. /**
  37. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  38. * respectively, creates/destroys local media tracks. Also listens to
  39. * media-related actions and performs corresponding operations with tracks.
  40. *
  41. * @param {Store} store - The redux store.
  42. * @returns {Function}
  43. */
  44. MiddlewareRegistry.register(store => next => action => {
  45. switch (action.type) {
  46. case TRACK_ADDED: {
  47. // The devices list needs to be refreshed when no initial video permissions
  48. // were granted and a local video track is added by umuting the video.
  49. if (action.track.local) {
  50. store.dispatch(getAvailableDevices());
  51. }
  52. break;
  53. }
  54. case TRACK_NO_DATA_FROM_SOURCE: {
  55. const result = next(action);
  56. _handleNoDataFromSourceErrors(store, action);
  57. return result;
  58. }
  59. case TRACK_REMOVED: {
  60. _removeNoDataFromSourceNotification(store, action.track);
  61. break;
  62. }
  63. case SET_AUDIO_MUTED:
  64. if (!action.muted
  65. && isUserInteractionRequiredForUnmute(store.getState())) {
  66. return;
  67. }
  68. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  69. break;
  70. case SET_CAMERA_FACING_MODE: {
  71. // XXX The camera facing mode of a MediaStreamTrack can be specified
  72. // only at initialization time and then it can only be toggled. So in
  73. // order to set the camera facing mode, one may destroy the track and
  74. // then initialize a new instance with the new camera facing mode. But
  75. // that is inefficient on mobile at least so the following relies on the
  76. // fact that there are 2 camera facing modes and merely toggles between
  77. // them to (hopefully) get the camera in the specified state.
  78. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  79. let jitsiTrack;
  80. if (localTrack
  81. && (jitsiTrack = localTrack.jitsiTrack)
  82. && jitsiTrack.getCameraFacingMode()
  83. !== action.cameraFacingMode) {
  84. store.dispatch(toggleCameraFacingMode());
  85. }
  86. break;
  87. }
  88. case SET_VIDEO_MUTED:
  89. if (!action.muted
  90. && isUserInteractionRequiredForUnmute(store.getState())) {
  91. return;
  92. }
  93. _setMuted(store, action, action.mediaType);
  94. break;
  95. case TOGGLE_CAMERA_FACING_MODE: {
  96. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  97. let jitsiTrack;
  98. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  99. // XXX MediaStreamTrack._switchCamera is a custom function
  100. // implemented in react-native-webrtc for video which switches
  101. // between the cameras via a native WebRTC library implementation
  102. // without making any changes to the track.
  103. jitsiTrack._switchCamera();
  104. // Don't mirror the video of the back/environment-facing camera.
  105. const mirror
  106. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  107. store.dispatch({
  108. type: TRACK_UPDATED,
  109. track: {
  110. jitsiTrack,
  111. mirror
  112. }
  113. });
  114. }
  115. break;
  116. }
  117. case TOGGLE_SCREENSHARING:
  118. if (typeof APP === 'object') {
  119. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING);
  120. }
  121. break;
  122. case TRACK_UPDATED:
  123. // TODO Remove the following calls to APP.UI once components interested
  124. // in track mute changes are moved into React and/or redux.
  125. if (typeof APP !== 'undefined') {
  126. const result = next(action);
  127. if (isPrejoinPageVisible(store.getState())) {
  128. return result;
  129. }
  130. const { jitsiTrack } = action.track;
  131. const muted = jitsiTrack.isMuted();
  132. const participantID = jitsiTrack.getParticipantId();
  133. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  134. if (isVideoTrack) {
  135. // Do not change the video mute state for local presenter tracks.
  136. if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
  137. APP.conference.mutePresenter(muted);
  138. } else if (jitsiTrack.isLocal()) {
  139. APP.conference.setVideoMuteStatus(muted);
  140. } else {
  141. APP.UI.setVideoMuted(participantID);
  142. }
  143. APP.UI.onPeerVideoTypeChanged(participantID, jitsiTrack.videoType);
  144. } else if (jitsiTrack.isLocal()) {
  145. APP.conference.setAudioMuteStatus(muted);
  146. } else {
  147. APP.UI.setAudioMuted(participantID, muted);
  148. }
  149. return result;
  150. }
  151. }
  152. return next(action);
  153. });
  154. /**
  155. * Handles no data from source errors.
  156. *
  157. * @param {Store} store - The redux store in which the specified action is
  158. * dispatched.
  159. * @param {Action} action - The redux action dispatched in the specified store.
  160. * @private
  161. * @returns {void}
  162. */
  163. function _handleNoDataFromSourceErrors(store, action) {
  164. const { getState, dispatch } = store;
  165. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  166. if (!track || !track.local) {
  167. return;
  168. }
  169. const { jitsiTrack } = track;
  170. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  171. _removeNoDataFromSourceNotification(store, action.track);
  172. }
  173. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  174. const { noDataFromSourceNotificationInfo = {} } = track;
  175. if (track.isReceivingData) {
  176. if (noDataFromSourceNotificationInfo.timeout) {
  177. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  178. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  179. }
  180. // try to remove the notification if there is one.
  181. _removeNoDataFromSourceNotification(store, action.track);
  182. } else {
  183. if (noDataFromSourceNotificationInfo.timeout) {
  184. return;
  185. }
  186. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  187. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  188. }
  189. }
  190. }
  191. /**
  192. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  193. * specific redux store.
  194. *
  195. * @param {Store} store - The redux store from which the local track associated
  196. * with the specified {@code mediaType} is to be retrieved.
  197. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  198. * be retrieved from the specified {@code store}.
  199. * @param {boolean} [includePending] - Indicates whether a local track is to be
  200. * returned if it is still pending. A local track is pending if
  201. * {@code getUserMedia} is still executing to create it and, consequently, its
  202. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  203. * track is not returned.
  204. * @private
  205. * @returns {Track} The local {@code Track} associated with the specified
  206. * {@code mediaType} in the specified {@code store}.
  207. */
  208. function _getLocalTrack(
  209. { getState }: { getState: Function },
  210. mediaType: MEDIA_TYPE,
  211. includePending: boolean = false) {
  212. return (
  213. getLocalTrack(
  214. getState()['features/base/tracks'],
  215. mediaType,
  216. includePending));
  217. }
  218. /**
  219. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  220. *
  221. * @param {Store} store - The redux store.
  222. * @param {Track} track - The redux action dispatched in the specified store.
  223. * @returns {void}
  224. */
  225. function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
  226. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  227. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  228. if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
  229. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  230. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  231. }
  232. }
  233. /**
  234. * Mutes or unmutes a local track with a specific media type.
  235. *
  236. * @param {Store} store - The redux store in which the specified action is
  237. * dispatched.
  238. * @param {Action} action - The redux action dispatched in the specified store.
  239. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  240. * which is being muted or unmuted.
  241. * @private
  242. * @returns {void}
  243. */
  244. function _setMuted(store, { ensureTrack, authority, muted }, mediaType: MEDIA_TYPE) {
  245. const localTrack
  246. = _getLocalTrack(store, mediaType, /* includePending */ true);
  247. if (localTrack) {
  248. // The `jitsiTrack` property will have a value only for a localTrack for
  249. // which `getUserMedia` has already completed. If there's no
  250. // `jitsiTrack`, then the `muted` state will be applied once the
  251. // `jitsiTrack` is created.
  252. const { jitsiTrack } = localTrack;
  253. const isAudioOnly = authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY;
  254. // screenshare cannot be muted or unmuted using the video mute button
  255. // anymore, unless it is muted by audioOnly.
  256. jitsiTrack && (jitsiTrack.videoType !== 'desktop' || isAudioOnly)
  257. && setTrackMuted(jitsiTrack, muted);
  258. } else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(store.getState()))) {
  259. // FIXME: This only runs on mobile now because web has its own way of
  260. // creating local tracks. Adjust the check once they are unified.
  261. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  262. }
  263. }