Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 { hideNotification } from '../../notifications';
  12. import { MiddlewareRegistry } from '../redux';
  13. import UIEvents from '../../../../service/UI/UIEvents';
  14. import {
  15. createLocalTracksA,
  16. showNoDataFromSourceVideoError,
  17. trackNoDataFromSourceNotificationInfoChanged
  18. } from './actions';
  19. import {
  20. TOGGLE_SCREENSHARING,
  21. TRACK_NO_DATA_FROM_SOURCE,
  22. TRACK_REMOVED,
  23. TRACK_UPDATED
  24. } from './actionTypes';
  25. import { getLocalTrack, getTrackByJitsiTrack, setTrackMuted } from './functions';
  26. declare var APP: Object;
  27. /**
  28. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  29. * respectively, creates/destroys local media tracks. Also listens to
  30. * media-related actions and performs corresponding operations with tracks.
  31. *
  32. * @param {Store} store - The redux store.
  33. * @returns {Function}
  34. */
  35. MiddlewareRegistry.register(store => next => action => {
  36. switch (action.type) {
  37. case TRACK_NO_DATA_FROM_SOURCE: {
  38. const result = next(action);
  39. _handleNoDataFromSourceErrors(store, action);
  40. return result;
  41. }
  42. case TRACK_REMOVED: {
  43. _removeNoDataFromSourceNotification(store, action.track);
  44. break;
  45. }
  46. case SET_AUDIO_MUTED:
  47. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  48. break;
  49. case SET_CAMERA_FACING_MODE: {
  50. // XXX The camera facing mode of a MediaStreamTrack can be specified
  51. // only at initialization time and then it can only be toggled. So in
  52. // order to set the camera facing mode, one may destroy the track and
  53. // then initialize a new instance with the new camera facing mode. But
  54. // that is inefficient on mobile at least so the following relies on the
  55. // fact that there are 2 camera facing modes and merely toggles between
  56. // them to (hopefully) get the camera in the specified state.
  57. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  58. let jitsiTrack;
  59. if (localTrack
  60. && (jitsiTrack = localTrack.jitsiTrack)
  61. && jitsiTrack.getCameraFacingMode()
  62. !== action.cameraFacingMode) {
  63. store.dispatch(toggleCameraFacingMode());
  64. }
  65. break;
  66. }
  67. case SET_VIDEO_MUTED:
  68. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  69. break;
  70. case TOGGLE_CAMERA_FACING_MODE: {
  71. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  72. let jitsiTrack;
  73. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  74. // XXX MediaStreamTrack._switchCamera is a custom function
  75. // implemented in react-native-webrtc for video which switches
  76. // between the cameras via a native WebRTC library implementation
  77. // without making any changes to the track.
  78. jitsiTrack._switchCamera();
  79. // Don't mirror the video of the back/environment-facing camera.
  80. const mirror
  81. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  82. store.dispatch({
  83. type: TRACK_UPDATED,
  84. track: {
  85. jitsiTrack,
  86. mirror
  87. }
  88. });
  89. }
  90. break;
  91. }
  92. case TOGGLE_SCREENSHARING:
  93. if (typeof APP === 'object') {
  94. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING);
  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(participantID, jitsiTrack.videoType);
  112. } else if (jitsiTrack.isLocal()) {
  113. APP.conference.setAudioMuteStatus(muted);
  114. } else {
  115. APP.UI.setAudioMuted(participantID, muted);
  116. }
  117. }
  118. }
  119. return next(action);
  120. });
  121. /**
  122. * Handles no data from source errors.
  123. *
  124. * @param {Store} store - The redux store in which the specified action is
  125. * dispatched.
  126. * @param {Action} action - The redux action dispatched in the specified store.
  127. * @private
  128. * @returns {void}
  129. */
  130. function _handleNoDataFromSourceErrors(store, action) {
  131. const { getState, dispatch } = store;
  132. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  133. if (!track || !track.local) {
  134. return;
  135. }
  136. const { jitsiTrack } = track;
  137. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  138. _removeNoDataFromSourceNotification(store, action.track);
  139. }
  140. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  141. const { noDataFromSourceNotificationInfo = {} } = track;
  142. if (track.isReceivingData) {
  143. if (noDataFromSourceNotificationInfo.timeout) {
  144. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  145. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  146. }
  147. // try to remove the notification if there is one.
  148. _removeNoDataFromSourceNotification(store, action.track);
  149. } else {
  150. if (noDataFromSourceNotificationInfo.timeout) {
  151. return;
  152. }
  153. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  154. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  155. }
  156. }
  157. }
  158. /**
  159. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  160. * specific redux store.
  161. *
  162. * @param {Store} store - The redux store from which the local track associated
  163. * with the specified {@code mediaType} is to be retrieved.
  164. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  165. * be retrieved from the specified {@code store}.
  166. * @param {boolean} [includePending] - Indicates whether a local track is to be
  167. * returned if it is still pending. A local track is pending if
  168. * {@code getUserMedia} is still executing to create it and, consequently, its
  169. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  170. * track is not returned.
  171. * @private
  172. * @returns {Track} The local {@code Track} associated with the specified
  173. * {@code mediaType} in the specified {@code store}.
  174. */
  175. function _getLocalTrack(
  176. { getState }: { getState: Function },
  177. mediaType: MEDIA_TYPE,
  178. includePending: boolean = false) {
  179. return (
  180. getLocalTrack(
  181. getState()['features/base/tracks'],
  182. mediaType,
  183. includePending));
  184. }
  185. /**
  186. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  187. *
  188. * @param {Store} store - The redux store.
  189. * @param {Track} track - The redux action dispatched in the specified store.
  190. * @returns {void}
  191. */
  192. function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
  193. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  194. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  195. if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
  196. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  197. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  198. }
  199. }
  200. /**
  201. * Mutes or unmutes a local track with a specific media type.
  202. *
  203. * @param {Store} store - The redux store in which the specified action is
  204. * dispatched.
  205. * @param {Action} action - The redux action dispatched in the specified store.
  206. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  207. * which is being muted or unmuted.
  208. * @private
  209. * @returns {void}
  210. */
  211. function _setMuted(store, { ensureTrack, muted }, mediaType: MEDIA_TYPE) {
  212. const localTrack
  213. = _getLocalTrack(store, mediaType, /* includePending */ true);
  214. if (localTrack) {
  215. // The `jitsiTrack` property will have a value only for a localTrack for
  216. // which `getUserMedia` has already completed. If there's no
  217. // `jitsiTrack`, then the `muted` state will be applied once the
  218. // `jitsiTrack` is created.
  219. const { jitsiTrack } = localTrack;
  220. jitsiTrack && setTrackMuted(jitsiTrack, muted);
  221. } else if (!muted && ensureTrack && typeof APP === 'undefined') {
  222. // FIXME: This only runs on mobile now because web has its own way of
  223. // creating local tracks. Adjust the check once they are unified.
  224. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  225. }
  226. }