Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

middleware.js 10KB

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