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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. setAudioMuted,
  9. setVideoMuted,
  10. TOGGLE_CAMERA_FACING_MODE,
  11. toggleCameraFacingMode
  12. } from '../media';
  13. import { MiddlewareRegistry } from '../redux';
  14. import { setTrackMuted } from './actions';
  15. import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
  16. import { getLocalTrack } from './functions';
  17. declare var APP: Object;
  18. /**
  19. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  20. * respectively, creates/destroys local media tracks. Also listens to
  21. * media-related actions and performs corresponding operations with tracks.
  22. *
  23. * @param {Store} store - The redux store.
  24. * @returns {Function}
  25. */
  26. MiddlewareRegistry.register(store => next => action => {
  27. switch (action.type) {
  28. case SET_AUDIO_MUTED:
  29. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  30. break;
  31. case SET_CAMERA_FACING_MODE: {
  32. // XXX The camera facing mode of a MediaStreamTrack can be specified
  33. // only at initialization time and then it can only be toggled. So in
  34. // order to set the camera facing mode, one may destroy the track and
  35. // then initialize a new instance with the new camera facing mode. But
  36. // that is inefficient on mobile at least so the following relies on the
  37. // fact that there are 2 camera facing modes and merely toggles between
  38. // them to (hopefully) get the camera in the specified state.
  39. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  40. let jitsiTrack;
  41. if (localTrack
  42. && (jitsiTrack = localTrack.jitsiTrack)
  43. && jitsiTrack.getCameraFacingMode()
  44. !== action.cameraFacingMode) {
  45. store.dispatch(toggleCameraFacingMode());
  46. }
  47. break;
  48. }
  49. case SET_VIDEO_MUTED:
  50. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  51. break;
  52. case TOGGLE_CAMERA_FACING_MODE: {
  53. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  54. let jitsiTrack;
  55. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  56. // XXX MediaStreamTrack._switchCamera is a custom function
  57. // implemented in react-native-webrtc for video which switches
  58. // between the cameras via a native WebRTC library implementation
  59. // without making any changes to the track.
  60. jitsiTrack._switchCamera();
  61. // Don't mirror the video of the back/environment-facing camera.
  62. const mirror
  63. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  64. store.dispatch({
  65. type: TRACK_UPDATED,
  66. track: {
  67. jitsiTrack,
  68. mirror
  69. }
  70. });
  71. }
  72. break;
  73. }
  74. case TRACK_ADDED:
  75. // TODO Remove this middleware case once all UI interested in new tracks
  76. // being added are converted to react and listening for store changes.
  77. if (typeof APP !== 'undefined' && !action.track.local) {
  78. APP.UI.addRemoteStream(action.track.jitsiTrack);
  79. }
  80. break;
  81. case TRACK_REMOVED:
  82. // TODO Remove this middleware case once all UI interested in tracks
  83. // being removed are converted to react and listening for store changes.
  84. if (typeof APP !== 'undefined' && !action.track.local) {
  85. APP.UI.removeRemoteStream(action.track.jitsiTrack);
  86. }
  87. break;
  88. case TRACK_UPDATED:
  89. // TODO Remove the following calls to APP.UI once components interested
  90. // in track mute changes are moved into React and/or redux.
  91. if (typeof APP !== 'undefined') {
  92. const { jitsiTrack } = action.track;
  93. const muted = jitsiTrack.isMuted();
  94. const participantID = jitsiTrack.getParticipantId();
  95. const isVideoTrack = jitsiTrack.isVideoTrack();
  96. if (jitsiTrack.isLocal()) {
  97. if (isVideoTrack) {
  98. APP.conference.setVideoMuteStatus(muted);
  99. } else {
  100. APP.conference.setAudioMuteStatus(muted);
  101. }
  102. }
  103. if (isVideoTrack) {
  104. APP.UI.setVideoMuted(participantID, muted);
  105. APP.UI.onPeerVideoTypeChanged(
  106. participantID,
  107. jitsiTrack.videoType);
  108. } else {
  109. APP.UI.setAudioMuted(participantID, muted);
  110. }
  111. // XXX The following synchronizes the state of base/tracks into the
  112. // state of base/media. Which is not required in React (and,
  113. // respectively, React Native) because base/media expresses the
  114. // app's and the user's desires/expectations/intents and base/tracks
  115. // expresses practice/reality. Unfortunately, the old Web does not
  116. // comply and/or does the opposite. Hence, the following:
  117. return _trackUpdated(store, next, action);
  118. }
  119. }
  120. return next(action);
  121. });
  122. /**
  123. * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
  124. * specific redux store.
  125. *
  126. * @param {Store} store - The redux store from which the local track associated
  127. * with the specified <tt>mediaType</tt> is to be retrieved.
  128. * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
  129. * be retrieved from the specified <tt>store</tt>.
  130. * @private
  131. * @returns {Track} The local <tt>Track</tt> associated with the specified
  132. * <tt>mediaType</tt> in the specified <tt>store</tt>.
  133. */
  134. function _getLocalTrack({ getState }, mediaType: MEDIA_TYPE) {
  135. return getLocalTrack(getState()['features/base/tracks'], mediaType);
  136. }
  137. /**
  138. * Mutes or unmutes a local track with a specific media type.
  139. *
  140. * @param {Store} store - The redux store in which the specified action is
  141. * dispatched.
  142. * @param {Action} action - The redux action dispatched in the specified store.
  143. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  144. * which is being muted or unmuted.
  145. * @private
  146. * @returns {void}
  147. */
  148. function _setMuted(store, { muted }, mediaType: MEDIA_TYPE) {
  149. const localTrack = _getLocalTrack(store, mediaType);
  150. localTrack && store.dispatch(setTrackMuted(localTrack.jitsiTrack, muted));
  151. }
  152. /**
  153. * Intercepts the action <tt>TRACK_UPDATED</tt> in order to synchronize the
  154. * muted states of the local tracks of features/base/tracks with the muted
  155. * states of features/base/media.
  156. *
  157. * @param {Store} store - The redux store in which the specified <tt>action</tt>
  158. * is being dispatched.
  159. * @param {Dispatch} next - The redux dispatch function to dispatch the
  160. * specified <tt>action</tt> to the specified <tt>store</tt>.
  161. * @param {Action} action - The redux action <tt>TRACK_UPDATED</tt> which is
  162. * being dispatched in the specified <tt>store</tt>.
  163. * @private
  164. * @returns {Object} The new state that is the result of the reduction of the
  165. * specified <tt>action</tt>.
  166. */
  167. function _trackUpdated(store, next, action) {
  168. // Determine the muted state of the local track before the update.
  169. const track = action.track;
  170. let mediaType;
  171. let oldMuted;
  172. if ('muted' in track) {
  173. // XXX The return value of JitsiTrack.getType() is of type MEDIA_TYPE
  174. // that happens to be compatible with the type MEDIA_TYPE defined by
  175. // jitsi-meet.
  176. mediaType = track.jitsiTrack.getType();
  177. const localTrack = _getLocalTrack(store, mediaType);
  178. if (localTrack) {
  179. oldMuted = localTrack.muted;
  180. }
  181. }
  182. const result = next(action);
  183. if (typeof oldMuted !== 'undefined') {
  184. // Determine the muted state of the local track after the update. If the
  185. // muted states before and after the update differ, then the respective
  186. // media state should by synchronized.
  187. const localTrack = _getLocalTrack(store, mediaType);
  188. if (localTrack) {
  189. const newMuted = localTrack.muted;
  190. if (oldMuted !== newMuted) {
  191. switch (mediaType) {
  192. case MEDIA_TYPE.AUDIO:
  193. store.dispatch(setAudioMuted(newMuted));
  194. break;
  195. case MEDIA_TYPE.VIDEO:
  196. store.dispatch(setVideoMuted(newMuted));
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. return result;
  203. }