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

middleware.js 6.7KB

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