You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 6.4KB

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