Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. setAudioMuted,
  9. setVideoMuted,
  10. TOGGLE_CAMERA_FACING_MODE,
  11. toggleCameraFacingMode
  12. } from '../media';
  13. import { MiddlewareRegistry } from '../redux';
  14. import { setTrackMuted } from './actions';
  15. import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
  16. import { getLocalTrack } from './functions';
  17. declare var APP: Object;
  18. /**
  19. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  20. * respectively, creates/destroys local media tracks. Also listens to
  21. * media-related actions and performs corresponding operations with tracks.
  22. *
  23. * @param {Store} store - The redux store.
  24. * @returns {Function}
  25. */
  26. MiddlewareRegistry.register(store => next => action => {
  27. switch (action.type) {
  28. case SET_AUDIO_MUTED:
  29. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  30. break;
  31. case SET_CAMERA_FACING_MODE: {
  32. // XXX The camera facing mode of a MediaStreamTrack can be specified
  33. // only at initialization time and then it can only be toggled. So in
  34. // order to set the camera facing mode, one may destroy the track and
  35. // then initialize a new instance with the new camera facing mode. But
  36. // that is inefficient on mobile at least so the following relies on the
  37. // fact that there are 2 camera facing modes and merely toggles between
  38. // them to (hopefully) get the camera in the specified state.
  39. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  40. let jitsiTrack;
  41. if (localTrack
  42. && (jitsiTrack = localTrack.jitsiTrack)
  43. && jitsiTrack.getCameraFacingMode()
  44. !== action.cameraFacingMode) {
  45. store.dispatch(toggleCameraFacingMode());
  46. }
  47. break;
  48. }
  49. case SET_VIDEO_MUTED:
  50. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  51. break;
  52. case TOGGLE_CAMERA_FACING_MODE: {
  53. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  54. let jitsiTrack;
  55. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  56. // XXX MediaStreamTrack._switchCamera is a custom function
  57. // implemented in react-native-webrtc for video which switches
  58. // between the cameras via a native WebRTC library implementation
  59. // without making any changes to the track.
  60. jitsiTrack._switchCamera();
  61. // Don't mirror the video of the back/environment-facing camera.
  62. const mirror
  63. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  64. store.dispatch({
  65. type: TRACK_UPDATED,
  66. track: {
  67. jitsiTrack,
  68. mirror
  69. }
  70. });
  71. }
  72. break;
  73. }
  74. case TRACK_ADDED:
  75. // TODO Remove this middleware case once all UI interested in new tracks
  76. // being added are converted to react and listening for store changes.
  77. if (typeof APP !== 'undefined' && !action.track.local) {
  78. APP.UI.addRemoteStream(action.track.jitsiTrack);
  79. }
  80. break;
  81. case TRACK_REMOVED:
  82. // TODO Remove this middleware case once all UI interested in tracks
  83. // being removed are converted to react and listening for store changes.
  84. if (typeof APP !== 'undefined' && !action.track.local) {
  85. APP.UI.removeRemoteStream(action.track.jitsiTrack);
  86. }
  87. break;
  88. case TRACK_UPDATED:
  89. // TODO Remove the below calls to APP.UI once components interested in
  90. // track mute changes are moved into react.
  91. if (typeof APP !== 'undefined') {
  92. const { jitsiTrack } = action.track;
  93. const isMuted = jitsiTrack.isMuted();
  94. const participantID = jitsiTrack.getParticipantId();
  95. const isVideoTrack = jitsiTrack.isVideoTrack();
  96. if (jitsiTrack.isLocal()) {
  97. if (isVideoTrack) {
  98. APP.conference.videoMuted = isMuted;
  99. } else {
  100. APP.conference.audioMuted = isMuted;
  101. }
  102. }
  103. if (isVideoTrack) {
  104. APP.UI.setVideoMuted(participantID, isMuted);
  105. APP.UI.onPeerVideoTypeChanged(
  106. participantID, jitsiTrack.videoType);
  107. } else {
  108. APP.UI.setAudioMuted(participantID, isMuted);
  109. }
  110. // XXX This function synchronizes track states with media states.
  111. // This is not required in React, because media is the source of
  112. // truth, synchronization should always happen in the media -> track
  113. // direction. The old web, however, does the opposite, hence the
  114. // need for this.
  115. return _trackUpdated(store, next, action);
  116. }
  117. }
  118. return next(action);
  119. });
  120. /**
  121. * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
  122. * specific redux store.
  123. *
  124. * @param {Store} store - The redux store from which the local track associated
  125. * with the specified <tt>mediaType</tt> is to be retrieved.
  126. * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
  127. * be retrieved from the specified <tt>store</tt>.
  128. * @private
  129. * @returns {Track} The local <tt>Track</tt> associated with the specified
  130. * <tt>mediaType</tt> in the specified <tt>store</tt>.
  131. */
  132. function _getLocalTrack(store, mediaType: MEDIA_TYPE) {
  133. return getLocalTrack(store.getState()['features/base/tracks'], mediaType);
  134. }
  135. /**
  136. * Mutes or unmutes a local track with a specific media type.
  137. *
  138. * @param {Store} store - The redux store in which the specified action is
  139. * dispatched.
  140. * @param {Action} action - The redux action dispatched in the specified store.
  141. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  142. * which is being muted or unmuted.
  143. * @private
  144. * @returns {void}
  145. */
  146. function _setMuted(store, { muted }, mediaType: MEDIA_TYPE) {
  147. const localTrack = _getLocalTrack(store, mediaType);
  148. localTrack && store.dispatch(setTrackMuted(localTrack.jitsiTrack, muted));
  149. }
  150. /**
  151. * Intercepts the action <tt>TRACK_UPDATED</tt> in order to synchronize the
  152. * muted states of the local tracks of features/base/tracks with the muted
  153. * states of features/base/media.
  154. *
  155. * @param {Store} store - The redux store in which the specified <tt>action</tt>
  156. * is being dispatched.
  157. * @param {Dispatch} next - The redux dispatch function to dispatch the
  158. * specified <tt>action</tt> to the specified <tt>store</tt>.
  159. * @param {Action} action - The redux action <tt>TRACK_UPDATED</tt> which is
  160. * being dispatched in the specified <tt>store</tt>.
  161. * @private
  162. * @returns {Object} The new state that is the result of the reduction of the
  163. * specified <tt>action</tt>.
  164. */
  165. function _trackUpdated(store, next, action) {
  166. // Determine the muted state of the local track before the update.
  167. const track = action.track;
  168. let mediaType;
  169. let oldMuted;
  170. if ('muted' in track) {
  171. // XXX The return value of JitsiTrack.getType() is of type MEDIA_TYPE
  172. // that happens to be compatible with the type MEDIA_TYPE defined by
  173. // jitsi-meet.
  174. mediaType = track.jitsiTrack.getType();
  175. const localTrack = _getLocalTrack(store, mediaType);
  176. if (localTrack) {
  177. oldMuted = localTrack.muted;
  178. }
  179. }
  180. const result = next(action);
  181. if (typeof oldMuted !== 'undefined') {
  182. // Determine the muted state of the local track after the update. If the
  183. // muted states before and after the update differ, then the respective
  184. // media state should by synchronized.
  185. const localTrack = _getLocalTrack(store, mediaType);
  186. if (localTrack) {
  187. const newMuted = localTrack.muted;
  188. if (oldMuted !== newMuted) {
  189. switch (mediaType) {
  190. case MEDIA_TYPE.AUDIO:
  191. store.dispatch(setAudioMuted(newMuted));
  192. break;
  193. case MEDIA_TYPE.VIDEO:
  194. store.dispatch(setVideoMuted(newMuted));
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. return result;
  201. }