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

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