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

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