您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 below calls to APP.UI once components interested in
  90. // track mute changes are moved into react.
  91. if (typeof APP !== 'undefined') {
  92. const { jitsiTrack } = action.track;
  93. const isMuted = jitsiTrack.isMuted();
  94. const participantID = jitsiTrack.getParticipantId();
  95. const isVideoTrack = jitsiTrack.isVideoTrack();
  96. if (jitsiTrack.isLocal()) {
  97. if (isVideoTrack) {
  98. APP.conference.videoMuted = isMuted;
  99. } else {
  100. APP.conference.audioMuted = isMuted;
  101. }
  102. }
  103. if (isVideoTrack) {
  104. APP.UI.setVideoMuted(participantID, isMuted);
  105. APP.UI.onPeerVideoTypeChanged(
  106. participantID, jitsiTrack.videoType);
  107. } else {
  108. APP.UI.setAudioMuted(participantID, isMuted);
  109. }
  110. }
  111. return _trackUpdated(store, next, action);
  112. }
  113. return next(action);
  114. });
  115. /**
  116. * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
  117. * specific redux store.
  118. *
  119. * @param {Store} store - The redux store from which the local track associated
  120. * with the specified <tt>mediaType</tt> is to be retrieved.
  121. * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
  122. * be retrieved from the specified <tt>store</tt>.
  123. * @private
  124. * @returns {Track} The local <tt>Track</tt> associated with the specified
  125. * <tt>mediaType</tt> in the specified <tt>store</tt>.
  126. */
  127. function _getLocalTrack(store, mediaType: MEDIA_TYPE) {
  128. return getLocalTrack(store.getState()['features/base/tracks'], mediaType);
  129. }
  130. /**
  131. * Mutes or unmutes a local track with a specific media type.
  132. *
  133. * @param {Store} store - The redux store in which the specified action is
  134. * dispatched.
  135. * @param {Action} action - The redux action dispatched in the specified store.
  136. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  137. * which is being muted or unmuted.
  138. * @private
  139. * @returns {void}
  140. */
  141. function _setMuted(store, { muted }, mediaType: MEDIA_TYPE) {
  142. const localTrack = _getLocalTrack(store, mediaType);
  143. localTrack && store.dispatch(setTrackMuted(localTrack.jitsiTrack, muted));
  144. }
  145. /**
  146. * Intercepts the action <tt>TRACK_UPDATED</tt> in order to synchronize the
  147. * muted states of the local tracks of features/base/tracks with the muted
  148. * states of features/base/media.
  149. *
  150. * @param {Store} store - The redux store in which the specified <tt>action</tt>
  151. * is being dispatched.
  152. * @param {Dispatch} next - The redux dispatch function to dispatch the
  153. * specified <tt>action</tt> to the specified <tt>store</tt>.
  154. * @param {Action} action - The redux action <tt>TRACK_UPDATED</tt> which is
  155. * being dispatched in the specified <tt>store</tt>.
  156. * @private
  157. * @returns {Object} The new state that is the result of the reduction of the
  158. * specified <tt>action</tt>.
  159. */
  160. function _trackUpdated(store, next, action) {
  161. // Determine the muted state of the local track before the update.
  162. const track = action.track;
  163. let mediaType;
  164. let oldMuted;
  165. if ('muted' in track) {
  166. // XXX The return value of JitsiTrack.getType() is of type MEDIA_TYPE
  167. // that happens to be compatible with the type MEDIA_TYPE defined by
  168. // jitsi-meet.
  169. mediaType = track.jitsiTrack.getType();
  170. const localTrack = _getLocalTrack(store, mediaType);
  171. if (localTrack) {
  172. oldMuted = localTrack.muted;
  173. }
  174. }
  175. const result = next(action);
  176. if (typeof oldMuted !== 'undefined') {
  177. // Determine the muted state of the local track after the update. If the
  178. // muted states before and after the update differ, then the respective
  179. // media state should by synchronized.
  180. const localTrack = _getLocalTrack(store, mediaType);
  181. if (localTrack) {
  182. const newMuted = localTrack.muted;
  183. if (oldMuted !== newMuted) {
  184. switch (mediaType) {
  185. case MEDIA_TYPE.AUDIO:
  186. store.dispatch(setAudioMuted(newMuted));
  187. break;
  188. case MEDIA_TYPE.VIDEO:
  189. store.dispatch(setVideoMuted(newMuted));
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. return result;
  196. }