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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_ADDED,
  17. TRACK_REMOVED,
  18. TRACK_UPDATED
  19. } from './actionTypes';
  20. import { getLocalTrack, setTrackMuted } from './functions';
  21. declare var APP: Object;
  22. /**
  23. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  24. * respectively, creates/destroys local media tracks. Also listens to
  25. * media-related actions and performs corresponding operations with tracks.
  26. *
  27. * @param {Store} store - The redux store.
  28. * @returns {Function}
  29. */
  30. MiddlewareRegistry.register(store => next => action => {
  31. switch (action.type) {
  32. case SET_AUDIO_MUTED:
  33. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  34. break;
  35. case SET_CAMERA_FACING_MODE: {
  36. // XXX The camera facing mode of a MediaStreamTrack can be specified
  37. // only at initialization time and then it can only be toggled. So in
  38. // order to set the camera facing mode, one may destroy the track and
  39. // then initialize a new instance with the new camera facing mode. But
  40. // that is inefficient on mobile at least so the following relies on the
  41. // fact that there are 2 camera facing modes and merely toggles between
  42. // them to (hopefully) get the camera in the specified state.
  43. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  44. let jitsiTrack;
  45. if (localTrack
  46. && (jitsiTrack = localTrack.jitsiTrack)
  47. && jitsiTrack.getCameraFacingMode()
  48. !== action.cameraFacingMode) {
  49. store.dispatch(toggleCameraFacingMode());
  50. }
  51. break;
  52. }
  53. case SET_VIDEO_MUTED:
  54. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  55. break;
  56. case TOGGLE_CAMERA_FACING_MODE: {
  57. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  58. let jitsiTrack;
  59. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  60. // XXX MediaStreamTrack._switchCamera is a custom function
  61. // implemented in react-native-webrtc for video which switches
  62. // between the cameras via a native WebRTC library implementation
  63. // without making any changes to the track.
  64. jitsiTrack._switchCamera();
  65. // Don't mirror the video of the back/environment-facing camera.
  66. const mirror
  67. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  68. store.dispatch({
  69. type: TRACK_UPDATED,
  70. track: {
  71. jitsiTrack,
  72. mirror
  73. }
  74. });
  75. }
  76. break;
  77. }
  78. case TOGGLE_SCREENSHARING:
  79. if (typeof APP === 'object') {
  80. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING);
  81. }
  82. break;
  83. case TRACK_ADDED:
  84. // TODO Remove this middleware case once all UI interested in new tracks
  85. // being added are converted to react and listening for store changes.
  86. if (typeof APP !== 'undefined' && !action.track.local) {
  87. APP.UI.addRemoteStream(action.track.jitsiTrack);
  88. }
  89. break;
  90. case TRACK_REMOVED:
  91. // TODO Remove this middleware case once all UI interested in tracks
  92. // being removed are converted to react and listening for store changes.
  93. if (typeof APP !== 'undefined' && !action.track.local) {
  94. APP.UI.removeRemoteStream(action.track.jitsiTrack);
  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. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  125. * specific redux store.
  126. *
  127. * @param {Store} store - The redux store from which the local track associated
  128. * with the specified {@code mediaType} is to be retrieved.
  129. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  130. * be retrieved from the specified {@code store}.
  131. * @private
  132. * @returns {Track} The local {@code Track} associated with the specified
  133. * {@code mediaType} in the specified {@code store}.
  134. */
  135. function _getLocalTrack({ getState }, mediaType: MEDIA_TYPE) {
  136. return getLocalTrack(getState()['features/base/tracks'], mediaType);
  137. }
  138. /**
  139. * Mutes or unmutes a local track with a specific media type.
  140. *
  141. * @param {Store} store - The redux store in which the specified action is
  142. * dispatched.
  143. * @param {Action} action - The redux action dispatched in the specified store.
  144. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  145. * which is being muted or unmuted.
  146. * @private
  147. * @returns {void}
  148. */
  149. function _setMuted(store, { ensureTrack, muted }, mediaType: MEDIA_TYPE) {
  150. const localTrack = _getLocalTrack(store, mediaType);
  151. if (localTrack) {
  152. setTrackMuted(localTrack.jitsiTrack, muted);
  153. } else if (!muted && ensureTrack && typeof APP === 'undefined') {
  154. // FIXME: This only runs on mobile now because web has its own way of
  155. // creating local tracks. Adjust the check once they are unified.
  156. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  157. }
  158. }