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.

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