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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. import './subscriber';
  36. declare var APP: Object;
  37. /**
  38. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  39. * respectively, creates/destroys local media tracks. Also listens to
  40. * media-related actions and performs corresponding operations with tracks.
  41. *
  42. * @param {Store} store - The redux store.
  43. * @returns {Function}
  44. */
  45. MiddlewareRegistry.register(store => next => action => {
  46. switch (action.type) {
  47. case TRACK_ADDED: {
  48. // The devices list needs to be refreshed when no initial video permissions
  49. // were granted and a local video track is added by umuting the video.
  50. if (action.track.local) {
  51. store.dispatch(getAvailableDevices());
  52. }
  53. break;
  54. }
  55. case TRACK_NO_DATA_FROM_SOURCE: {
  56. const result = next(action);
  57. _handleNoDataFromSourceErrors(store, action);
  58. return result;
  59. }
  60. case TRACK_REMOVED: {
  61. _removeNoDataFromSourceNotification(store, action.track);
  62. break;
  63. }
  64. case SET_AUDIO_MUTED:
  65. if (!action.muted
  66. && isUserInteractionRequiredForUnmute(store.getState())) {
  67. return;
  68. }
  69. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  70. break;
  71. case SET_CAMERA_FACING_MODE: {
  72. // XXX The camera facing mode of a MediaStreamTrack can be specified
  73. // only at initialization time and then it can only be toggled. So in
  74. // order to set the camera facing mode, one may destroy the track and
  75. // then initialize a new instance with the new camera facing mode. But
  76. // that is inefficient on mobile at least so the following relies on the
  77. // fact that there are 2 camera facing modes and merely toggles between
  78. // them to (hopefully) get the camera in the specified state.
  79. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  80. let jitsiTrack;
  81. if (localTrack
  82. && (jitsiTrack = localTrack.jitsiTrack)
  83. && jitsiTrack.getCameraFacingMode()
  84. !== action.cameraFacingMode) {
  85. store.dispatch(toggleCameraFacingMode());
  86. }
  87. break;
  88. }
  89. case SET_VIDEO_MUTED:
  90. if (!action.muted
  91. && isUserInteractionRequiredForUnmute(store.getState())) {
  92. return;
  93. }
  94. _setMuted(store, action, action.mediaType);
  95. break;
  96. case TOGGLE_CAMERA_FACING_MODE: {
  97. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  98. let jitsiTrack;
  99. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  100. // XXX MediaStreamTrack._switchCamera is a custom function
  101. // implemented in react-native-webrtc for video which switches
  102. // between the cameras via a native WebRTC library implementation
  103. // without making any changes to the track.
  104. jitsiTrack._switchCamera();
  105. // Don't mirror the video of the back/environment-facing camera.
  106. const mirror
  107. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  108. store.dispatch({
  109. type: TRACK_UPDATED,
  110. track: {
  111. jitsiTrack,
  112. mirror
  113. }
  114. });
  115. }
  116. break;
  117. }
  118. case TOGGLE_SCREENSHARING:
  119. if (typeof APP === 'object') {
  120. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING, action.audioOnly);
  121. }
  122. break;
  123. case TRACK_UPDATED:
  124. // TODO Remove the following calls to APP.UI once components interested
  125. // in track mute changes are moved into React and/or redux.
  126. if (typeof APP !== 'undefined') {
  127. const result = next(action);
  128. if (isPrejoinPageVisible(store.getState())) {
  129. return result;
  130. }
  131. const { jitsiTrack } = action.track;
  132. const muted = jitsiTrack.isMuted();
  133. const participantID = jitsiTrack.getParticipantId();
  134. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  135. if (isVideoTrack) {
  136. // Do not change the video mute state for local presenter tracks.
  137. if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
  138. APP.conference.mutePresenter(muted);
  139. } else if (jitsiTrack.isLocal()) {
  140. APP.conference.setVideoMuteStatus();
  141. } else {
  142. APP.UI.setVideoMuted(participantID);
  143. }
  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. }