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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. TOGGLE_CAMERA_FACING_MODE,
  9. toggleCameraFacingMode
  10. } from '../media';
  11. import { MiddlewareRegistry } from '../redux';
  12. import { createLocalTracksA } from './actions';
  13. import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
  14. import { getLocalTrack, setTrackMuted } from './functions';
  15. declare var APP: Object;
  16. /**
  17. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  18. * respectively, creates/destroys local media tracks. Also listens to
  19. * media-related actions and performs corresponding operations with tracks.
  20. *
  21. * @param {Store} store - The redux store.
  22. * @returns {Function}
  23. */
  24. MiddlewareRegistry.register(store => next => action => {
  25. switch (action.type) {
  26. case SET_AUDIO_MUTED:
  27. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  28. break;
  29. case SET_CAMERA_FACING_MODE: {
  30. // XXX The camera facing mode of a MediaStreamTrack can be specified
  31. // only at initialization time and then it can only be toggled. So in
  32. // order to set the camera facing mode, one may destroy the track and
  33. // then initialize a new instance with the new camera facing mode. But
  34. // that is inefficient on mobile at least so the following relies on the
  35. // fact that there are 2 camera facing modes and merely toggles between
  36. // them to (hopefully) get the camera in the specified state.
  37. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  38. let jitsiTrack;
  39. if (localTrack
  40. && (jitsiTrack = localTrack.jitsiTrack)
  41. && jitsiTrack.getCameraFacingMode()
  42. !== action.cameraFacingMode) {
  43. store.dispatch(toggleCameraFacingMode());
  44. }
  45. break;
  46. }
  47. case SET_VIDEO_MUTED:
  48. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  49. break;
  50. case TOGGLE_CAMERA_FACING_MODE: {
  51. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  52. let jitsiTrack;
  53. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  54. // XXX MediaStreamTrack._switchCamera is a custom function
  55. // implemented in react-native-webrtc for video which switches
  56. // between the cameras via a native WebRTC library implementation
  57. // without making any changes to the track.
  58. jitsiTrack._switchCamera();
  59. // Don't mirror the video of the back/environment-facing camera.
  60. const mirror
  61. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  62. store.dispatch({
  63. type: TRACK_UPDATED,
  64. track: {
  65. jitsiTrack,
  66. mirror
  67. }
  68. });
  69. }
  70. break;
  71. }
  72. case TRACK_ADDED:
  73. // TODO Remove this middleware case once all UI interested in new tracks
  74. // being added are converted to react and listening for store changes.
  75. if (typeof APP !== 'undefined' && !action.track.local) {
  76. APP.UI.addRemoteStream(action.track.jitsiTrack);
  77. }
  78. break;
  79. case TRACK_REMOVED:
  80. // TODO Remove this middleware case once all UI interested in tracks
  81. // being removed are converted to react and listening for store changes.
  82. if (typeof APP !== 'undefined' && !action.track.local) {
  83. APP.UI.removeRemoteStream(action.track.jitsiTrack);
  84. }
  85. break;
  86. case TRACK_UPDATED:
  87. // TODO Remove the following calls to APP.UI once components interested
  88. // in track mute changes are moved into React and/or redux.
  89. if (typeof APP !== 'undefined') {
  90. const { jitsiTrack } = action.track;
  91. const muted = jitsiTrack.isMuted();
  92. const participantID = jitsiTrack.getParticipantId();
  93. const isVideoTrack = jitsiTrack.isVideoTrack();
  94. if (isVideoTrack) {
  95. if (jitsiTrack.isLocal()) {
  96. APP.conference.setVideoMuteStatus(muted);
  97. } else {
  98. APP.UI.setVideoMuted(participantID, muted);
  99. }
  100. APP.UI.onPeerVideoTypeChanged(
  101. participantID,
  102. jitsiTrack.videoType);
  103. } else if (jitsiTrack.isLocal()) {
  104. APP.conference.setAudioMuteStatus(muted);
  105. } else {
  106. APP.UI.setAudioMuted(participantID, muted);
  107. }
  108. }
  109. }
  110. return next(action);
  111. });
  112. /**
  113. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  114. * specific redux store.
  115. *
  116. * @param {Store} store - The redux store from which the local track associated
  117. * with the specified {@code mediaType} is to be retrieved.
  118. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  119. * be retrieved from the specified {@code store}.
  120. * @private
  121. * @returns {Track} The local {@code Track} associated with the specified
  122. * {@code mediaType} in the specified {@code store}.
  123. */
  124. function _getLocalTrack({ getState }, mediaType: MEDIA_TYPE) {
  125. return getLocalTrack(getState()['features/base/tracks'], mediaType);
  126. }
  127. /**
  128. * Mutes or unmutes a local track with a specific media type.
  129. *
  130. * @param {Store} store - The redux store in which the specified action is
  131. * dispatched.
  132. * @param {Action} action - The redux action dispatched in the specified store.
  133. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  134. * which is being muted or unmuted.
  135. * @private
  136. * @returns {void}
  137. */
  138. function _setMuted(store, { ensureTrack, muted }, mediaType: MEDIA_TYPE) {
  139. const localTrack = _getLocalTrack(store, mediaType);
  140. if (localTrack) {
  141. setTrackMuted(localTrack.jitsiTrack, muted);
  142. } else if (!muted && ensureTrack && typeof APP === 'undefined') {
  143. // FIXME: This only runs on mobile now because web has its own way of
  144. // creating local tracks. Adjust the check once they are unified.
  145. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  146. }
  147. }