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

middleware.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 { hideNotification } from '../../notifications';
  12. import { MiddlewareRegistry } from '../redux';
  13. import UIEvents from '../../../../service/UI/UIEvents';
  14. import {
  15. createLocalTracksA,
  16. showNoDataFromSourceVideoError,
  17. trackNoDataFromSourceNotificationInfoChanged
  18. } from './actions';
  19. import {
  20. TOGGLE_SCREENSHARING,
  21. TRACK_NO_DATA_FROM_SOURCE,
  22. TRACK_REMOVED,
  23. TRACK_UPDATED
  24. } from './actionTypes';
  25. import {
  26. getLocalTrack,
  27. getTrackByJitsiTrack,
  28. isUserInteractionRequiredForUnmute,
  29. setTrackMuted
  30. } from './functions';
  31. declare var APP: Object;
  32. /**
  33. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  34. * respectively, creates/destroys local media tracks. Also listens to
  35. * media-related actions and performs corresponding operations with tracks.
  36. *
  37. * @param {Store} store - The redux store.
  38. * @returns {Function}
  39. */
  40. MiddlewareRegistry.register(store => next => action => {
  41. switch (action.type) {
  42. case TRACK_NO_DATA_FROM_SOURCE: {
  43. const result = next(action);
  44. _handleNoDataFromSourceErrors(store, action);
  45. return result;
  46. }
  47. case TRACK_REMOVED: {
  48. _removeNoDataFromSourceNotification(store, action.track);
  49. break;
  50. }
  51. case SET_AUDIO_MUTED:
  52. if (!action.muted
  53. && isUserInteractionRequiredForUnmute(store.getState())) {
  54. return;
  55. }
  56. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  57. break;
  58. case SET_CAMERA_FACING_MODE: {
  59. // XXX The camera facing mode of a MediaStreamTrack can be specified
  60. // only at initialization time and then it can only be toggled. So in
  61. // order to set the camera facing mode, one may destroy the track and
  62. // then initialize a new instance with the new camera facing mode. But
  63. // that is inefficient on mobile at least so the following relies on the
  64. // fact that there are 2 camera facing modes and merely toggles between
  65. // them to (hopefully) get the camera in the specified state.
  66. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  67. let jitsiTrack;
  68. if (localTrack
  69. && (jitsiTrack = localTrack.jitsiTrack)
  70. && jitsiTrack.getCameraFacingMode()
  71. !== action.cameraFacingMode) {
  72. store.dispatch(toggleCameraFacingMode());
  73. }
  74. break;
  75. }
  76. case SET_VIDEO_MUTED:
  77. if (!action.muted
  78. && isUserInteractionRequiredForUnmute(store.getState())) {
  79. return;
  80. }
  81. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  82. break;
  83. case TOGGLE_CAMERA_FACING_MODE: {
  84. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  85. let jitsiTrack;
  86. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  87. // XXX MediaStreamTrack._switchCamera is a custom function
  88. // implemented in react-native-webrtc for video which switches
  89. // between the cameras via a native WebRTC library implementation
  90. // without making any changes to the track.
  91. jitsiTrack._switchCamera();
  92. // Don't mirror the video of the back/environment-facing camera.
  93. const mirror
  94. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  95. store.dispatch({
  96. type: TRACK_UPDATED,
  97. track: {
  98. jitsiTrack,
  99. mirror
  100. }
  101. });
  102. }
  103. break;
  104. }
  105. case TOGGLE_SCREENSHARING:
  106. if (typeof APP === 'object') {
  107. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING);
  108. }
  109. break;
  110. case TRACK_UPDATED:
  111. // TODO Remove the following calls to APP.UI once components interested
  112. // in track mute changes are moved into React and/or redux.
  113. if (typeof APP !== 'undefined') {
  114. const { jitsiTrack } = action.track;
  115. const muted = jitsiTrack.isMuted();
  116. const participantID = jitsiTrack.getParticipantId();
  117. const isVideoTrack = jitsiTrack.isVideoTrack();
  118. if (isVideoTrack) {
  119. if (jitsiTrack.isLocal()) {
  120. APP.conference.setVideoMuteStatus(muted);
  121. } else {
  122. APP.UI.setVideoMuted(participantID, muted);
  123. }
  124. APP.UI.onPeerVideoTypeChanged(participantID, jitsiTrack.videoType);
  125. } else if (jitsiTrack.isLocal()) {
  126. APP.conference.setAudioMuteStatus(muted);
  127. } else {
  128. APP.UI.setAudioMuted(participantID, muted);
  129. }
  130. }
  131. }
  132. return next(action);
  133. });
  134. /**
  135. * Handles no data from source errors.
  136. *
  137. * @param {Store} store - The redux store in which the specified action is
  138. * dispatched.
  139. * @param {Action} action - The redux action dispatched in the specified store.
  140. * @private
  141. * @returns {void}
  142. */
  143. function _handleNoDataFromSourceErrors(store, action) {
  144. const { getState, dispatch } = store;
  145. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  146. if (!track || !track.local) {
  147. return;
  148. }
  149. const { jitsiTrack } = track;
  150. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  151. _removeNoDataFromSourceNotification(store, action.track);
  152. }
  153. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  154. const { noDataFromSourceNotificationInfo = {} } = track;
  155. if (track.isReceivingData) {
  156. if (noDataFromSourceNotificationInfo.timeout) {
  157. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  158. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  159. }
  160. // try to remove the notification if there is one.
  161. _removeNoDataFromSourceNotification(store, action.track);
  162. } else {
  163. if (noDataFromSourceNotificationInfo.timeout) {
  164. return;
  165. }
  166. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  167. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  168. }
  169. }
  170. }
  171. /**
  172. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  173. * specific redux store.
  174. *
  175. * @param {Store} store - The redux store from which the local track associated
  176. * with the specified {@code mediaType} is to be retrieved.
  177. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  178. * be retrieved from the specified {@code store}.
  179. * @param {boolean} [includePending] - Indicates whether a local track is to be
  180. * returned if it is still pending. A local track is pending if
  181. * {@code getUserMedia} is still executing to create it and, consequently, its
  182. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  183. * track is not returned.
  184. * @private
  185. * @returns {Track} The local {@code Track} associated with the specified
  186. * {@code mediaType} in the specified {@code store}.
  187. */
  188. function _getLocalTrack(
  189. { getState }: { getState: Function },
  190. mediaType: MEDIA_TYPE,
  191. includePending: boolean = false) {
  192. return (
  193. getLocalTrack(
  194. getState()['features/base/tracks'],
  195. mediaType,
  196. includePending));
  197. }
  198. /**
  199. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  200. *
  201. * @param {Store} store - The redux store.
  202. * @param {Track} track - The redux action dispatched in the specified store.
  203. * @returns {void}
  204. */
  205. function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
  206. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  207. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  208. if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
  209. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  210. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  211. }
  212. }
  213. /**
  214. * Mutes or unmutes a local track with a specific media type.
  215. *
  216. * @param {Store} store - The redux store in which the specified action is
  217. * dispatched.
  218. * @param {Action} action - The redux action dispatched in the specified store.
  219. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  220. * which is being muted or unmuted.
  221. * @private
  222. * @returns {void}
  223. */
  224. function _setMuted(store, { ensureTrack, muted }, mediaType: MEDIA_TYPE) {
  225. const localTrack
  226. = _getLocalTrack(store, mediaType, /* includePending */ true);
  227. if (localTrack) {
  228. // The `jitsiTrack` property will have a value only for a localTrack for
  229. // which `getUserMedia` has already completed. If there's no
  230. // `jitsiTrack`, then the `muted` state will be applied once the
  231. // `jitsiTrack` is created.
  232. const { jitsiTrack } = localTrack;
  233. jitsiTrack && setTrackMuted(jitsiTrack, muted);
  234. } else if (!muted && ensureTrack && typeof APP === 'undefined') {
  235. // FIXME: This only runs on mobile now because web has its own way of
  236. // creating local tracks. Adjust the check once they are unified.
  237. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  238. }
  239. }