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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. const { jitsiTrack } = action.track;
  128. const muted = jitsiTrack.isMuted();
  129. const participantID = jitsiTrack.getParticipantId();
  130. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  131. if (isVideoTrack) {
  132. if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
  133. APP.conference.mutePresenter(muted);
  134. }
  135. // Make sure we change the video mute state only for camera tracks.
  136. if (jitsiTrack.isLocal() && jitsiTrack.videoType !== 'desktop') {
  137. APP.conference.setVideoMuteStatus(muted);
  138. } else {
  139. APP.UI.setVideoMuted(participantID, muted);
  140. }
  141. APP.UI.onPeerVideoTypeChanged(participantID, jitsiTrack.videoType);
  142. } else if (jitsiTrack.isLocal()) {
  143. APP.conference.setAudioMuteStatus(muted);
  144. } else {
  145. APP.UI.setAudioMuted(participantID, muted);
  146. }
  147. return result;
  148. }
  149. }
  150. return next(action);
  151. });
  152. /**
  153. * Handles no data from source errors.
  154. *
  155. * @param {Store} store - The redux store in which the specified action is
  156. * dispatched.
  157. * @param {Action} action - The redux action dispatched in the specified store.
  158. * @private
  159. * @returns {void}
  160. */
  161. function _handleNoDataFromSourceErrors(store, action) {
  162. const { getState, dispatch } = store;
  163. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  164. if (!track || !track.local) {
  165. return;
  166. }
  167. const { jitsiTrack } = track;
  168. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  169. _removeNoDataFromSourceNotification(store, action.track);
  170. }
  171. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  172. const { noDataFromSourceNotificationInfo = {} } = track;
  173. if (track.isReceivingData) {
  174. if (noDataFromSourceNotificationInfo.timeout) {
  175. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  176. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  177. }
  178. // try to remove the notification if there is one.
  179. _removeNoDataFromSourceNotification(store, action.track);
  180. } else {
  181. if (noDataFromSourceNotificationInfo.timeout) {
  182. return;
  183. }
  184. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  185. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  186. }
  187. }
  188. }
  189. /**
  190. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  191. * specific redux store.
  192. *
  193. * @param {Store} store - The redux store from which the local track associated
  194. * with the specified {@code mediaType} is to be retrieved.
  195. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  196. * be retrieved from the specified {@code store}.
  197. * @param {boolean} [includePending] - Indicates whether a local track is to be
  198. * returned if it is still pending. A local track is pending if
  199. * {@code getUserMedia} is still executing to create it and, consequently, its
  200. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  201. * track is not returned.
  202. * @private
  203. * @returns {Track} The local {@code Track} associated with the specified
  204. * {@code mediaType} in the specified {@code store}.
  205. */
  206. function _getLocalTrack(
  207. { getState }: { getState: Function },
  208. mediaType: MEDIA_TYPE,
  209. includePending: boolean = false) {
  210. return (
  211. getLocalTrack(
  212. getState()['features/base/tracks'],
  213. mediaType,
  214. includePending));
  215. }
  216. /**
  217. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  218. *
  219. * @param {Store} store - The redux store.
  220. * @param {Track} track - The redux action dispatched in the specified store.
  221. * @returns {void}
  222. */
  223. function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
  224. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  225. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  226. if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
  227. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  228. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  229. }
  230. }
  231. /**
  232. * Mutes or unmutes a local track with a specific media type.
  233. *
  234. * @param {Store} store - The redux store in which the specified action is
  235. * dispatched.
  236. * @param {Action} action - The redux action dispatched in the specified store.
  237. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  238. * which is being muted or unmuted.
  239. * @private
  240. * @returns {void}
  241. */
  242. function _setMuted(store, { ensureTrack, authority, muted }, mediaType: MEDIA_TYPE) {
  243. const localTrack
  244. = _getLocalTrack(store, mediaType, /* includePending */ true);
  245. if (localTrack) {
  246. // The `jitsiTrack` property will have a value only for a localTrack for
  247. // which `getUserMedia` has already completed. If there's no
  248. // `jitsiTrack`, then the `muted` state will be applied once the
  249. // `jitsiTrack` is created.
  250. const { jitsiTrack } = localTrack;
  251. const isAudioOnly = authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY;
  252. // screenshare cannot be muted or unmuted using the video mute button
  253. // anymore, unless it is muted by audioOnly.
  254. jitsiTrack && (jitsiTrack.videoType !== 'desktop' || isAudioOnly)
  255. && setTrackMuted(jitsiTrack, muted);
  256. } else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(store.getState()))) {
  257. // FIXME: This only runs on mobile now because web has its own way of
  258. // creating local tracks. Adjust the check once they are unified.
  259. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  260. }
  261. }