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

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