Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.js 8.1KB

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