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

middleware.js 9.1KB

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