2021-12-08 22:20:26 +01:00
|
|
|
import { batch } from 'react-redux';
|
|
|
|
|
|
2022-09-23 10:48:20 +03:00
|
|
|
import { IStore } from '../../app/types';
|
2022-04-28 11:34:23 +03:00
|
|
|
import { _RESET_BREAKOUT_ROOMS } from '../../breakout-rooms/actionTypes';
|
2021-12-08 22:20:26 +01:00
|
|
|
import { getCurrentConference } from '../conference/functions';
|
2016-10-05 09:36:59 -05:00
|
|
|
import {
|
2017-01-11 12:14:00 -06:00
|
|
|
SET_CAMERA_FACING_MODE,
|
2022-09-27 10:10:28 +03:00
|
|
|
SET_SCREENSHARE_MUTED,
|
2017-01-11 12:14:00 -06:00
|
|
|
SET_VIDEO_MUTED,
|
2022-09-27 10:10:28 +03:00
|
|
|
TOGGLE_CAMERA_FACING_MODE
|
2022-09-23 10:48:20 +03:00
|
|
|
} from '../media/actionTypes';
|
2023-05-18 14:16:37 -05:00
|
|
|
import { gumPending, toggleCameraFacingMode } from '../media/actions';
|
2022-09-23 10:48:20 +03:00
|
|
|
import {
|
|
|
|
|
CAMERA_FACING_MODE,
|
|
|
|
|
MEDIA_TYPE,
|
2024-01-09 12:07:29 -05:00
|
|
|
MediaType
|
2022-09-23 10:48:20 +03:00
|
|
|
} from '../media/constants';
|
2023-05-18 14:16:37 -05:00
|
|
|
import { IGUMPendingState } from '../media/types';
|
2022-09-23 10:48:20 +03:00
|
|
|
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
|
|
|
|
import StateListenerRegistry from '../redux/StateListenerRegistry';
|
2016-10-05 09:36:59 -05:00
|
|
|
|
2018-03-06 16:28:19 -08:00
|
|
|
import {
|
|
|
|
|
TRACK_UPDATED
|
|
|
|
|
} from './actionTypes';
|
2020-05-20 12:57:03 +02:00
|
|
|
import {
|
|
|
|
|
createLocalTracksA,
|
2021-12-10 11:50:12 +01:00
|
|
|
destroyLocalTracks,
|
2022-03-15 13:24:49 -04:00
|
|
|
trackMuteUnmuteFailed,
|
2022-09-27 10:10:28 +03:00
|
|
|
trackRemoved
|
2020-05-20 12:57:03 +02:00
|
|
|
} from './actions';
|
2019-07-10 04:02:27 -07:00
|
|
|
import {
|
|
|
|
|
getLocalTrack,
|
|
|
|
|
isUserInteractionRequiredForUnmute,
|
|
|
|
|
setTrackMuted
|
|
|
|
|
} from './functions';
|
2021-01-12 11:13:20 +02:00
|
|
|
|
2016-10-05 09:36:59 -05:00
|
|
|
/**
|
2017-02-27 16:45:53 -06:00
|
|
|
* Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
|
|
|
|
|
* respectively, creates/destroys local media tracks. Also listens to
|
|
|
|
|
* media-related actions and performs corresponding operations with tracks.
|
2016-10-05 09:36:59 -05:00
|
|
|
*
|
2017-07-31 11:27:34 -05:00
|
|
|
* @param {Store} store - The redux store.
|
2016-10-05 09:36:59 -05:00
|
|
|
* @returns {Function}
|
|
|
|
|
*/
|
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
|
switch (action.type) {
|
2017-02-22 10:25:58 +01:00
|
|
|
case SET_CAMERA_FACING_MODE: {
|
2017-07-18 16:41:39 -05:00
|
|
|
// XXX The camera facing mode of a MediaStreamTrack can be specified
|
|
|
|
|
// only at initialization time and then it can only be toggled. So in
|
|
|
|
|
// order to set the camera facing mode, one may destroy the track and
|
|
|
|
|
// then initialize a new instance with the new camera facing mode. But
|
|
|
|
|
// that is inefficient on mobile at least so the following relies on the
|
|
|
|
|
// fact that there are 2 camera facing modes and merely toggles between
|
|
|
|
|
// them to (hopefully) get the camera in the specified state.
|
2017-02-22 10:25:58 +01:00
|
|
|
const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
|
2017-07-18 16:41:39 -05:00
|
|
|
let jitsiTrack;
|
2017-02-22 10:25:58 +01:00
|
|
|
|
2017-07-18 16:41:39 -05:00
|
|
|
if (localTrack
|
|
|
|
|
&& (jitsiTrack = localTrack.jitsiTrack)
|
|
|
|
|
&& jitsiTrack.getCameraFacingMode()
|
|
|
|
|
!== action.cameraFacingMode) {
|
|
|
|
|
store.dispatch(toggleCameraFacingMode());
|
2017-02-22 10:25:58 +01:00
|
|
|
}
|
2016-10-05 09:36:59 -05:00
|
|
|
break;
|
2017-02-22 10:25:58 +01:00
|
|
|
}
|
2016-10-05 09:36:59 -05:00
|
|
|
|
2022-03-15 13:24:49 -04:00
|
|
|
case SET_SCREENSHARE_MUTED:
|
2022-12-07 15:41:50 -06:00
|
|
|
_setMuted(store, action, MEDIA_TYPE.SCREENSHARE);
|
2022-03-15 13:24:49 -04:00
|
|
|
break;
|
|
|
|
|
|
2017-01-17 08:32:20 -06:00
|
|
|
case SET_VIDEO_MUTED:
|
2019-07-10 04:02:27 -07:00
|
|
|
if (!action.muted
|
|
|
|
|
&& isUserInteractionRequiredForUnmute(store.getState())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 15:37:12 -06:00
|
|
|
_setMuted(store, action, MEDIA_TYPE.VIDEO);
|
2016-10-05 09:36:59 -05:00
|
|
|
break;
|
|
|
|
|
|
2017-04-03 10:57:01 +02:00
|
|
|
case TOGGLE_CAMERA_FACING_MODE: {
|
|
|
|
|
const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
|
|
|
|
|
let jitsiTrack;
|
2017-04-05 11:03:16 +02:00
|
|
|
|
|
|
|
|
if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
|
|
|
|
|
// XXX MediaStreamTrack._switchCamera is a custom function
|
|
|
|
|
// implemented in react-native-webrtc for video which switches
|
|
|
|
|
// between the cameras via a native WebRTC library implementation
|
|
|
|
|
// without making any changes to the track.
|
|
|
|
|
jitsiTrack._switchCamera();
|
2017-04-03 10:57:01 +02:00
|
|
|
|
|
|
|
|
// Don't mirror the video of the back/environment-facing camera.
|
2017-04-05 11:03:16 +02:00
|
|
|
const mirror
|
|
|
|
|
= jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
|
|
|
|
|
|
2017-04-03 10:57:01 +02:00
|
|
|
store.dispatch({
|
|
|
|
|
type: TRACK_UPDATED,
|
|
|
|
|
track: {
|
|
|
|
|
jitsiTrack,
|
2017-04-05 11:03:16 +02:00
|
|
|
mirror
|
2017-04-03 10:57:01 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-05 09:36:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
|
});
|
|
|
|
|
|
2021-12-08 22:20:26 +01:00
|
|
|
/**
|
|
|
|
|
* Set up state change listener to perform maintenance tasks when the conference
|
2021-12-09 11:46:27 -06:00
|
|
|
* is left or failed, remove all tracks from the store.
|
2021-12-08 22:20:26 +01:00
|
|
|
*/
|
|
|
|
|
StateListenerRegistry.register(
|
|
|
|
|
state => getCurrentConference(state),
|
|
|
|
|
(conference, { dispatch, getState }, prevConference) => {
|
2022-04-14 14:35:41 -05:00
|
|
|
const { authRequired, error } = getState()['features/base/conference'];
|
2021-12-23 11:57:05 -06:00
|
|
|
|
|
|
|
|
// conference keep flipping while we are authenticating, skip clearing while we are in that process
|
2022-04-14 14:35:41 -05:00
|
|
|
if (prevConference && !conference && !authRequired && !error) {
|
2021-12-23 11:57:05 -06:00
|
|
|
|
2021-12-09 11:46:27 -06:00
|
|
|
// Clear all tracks.
|
2021-12-10 11:50:12 +01:00
|
|
|
const remoteTracks = getState()['features/base/tracks'].filter(t => !t.local);
|
2021-12-08 22:20:26 +01:00
|
|
|
|
|
|
|
|
batch(() => {
|
2021-12-10 11:50:12 +01:00
|
|
|
dispatch(destroyLocalTracks());
|
|
|
|
|
for (const track of remoteTracks) {
|
2021-12-08 22:20:26 +01:00
|
|
|
dispatch(trackRemoved(track.jitsiTrack));
|
|
|
|
|
}
|
2022-04-28 11:34:23 +03:00
|
|
|
dispatch({ type: _RESET_BREAKOUT_ROOMS });
|
2021-12-08 22:20:26 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-25 11:43:15 -05:00
|
|
|
/**
|
2017-10-01 01:35:19 -05:00
|
|
|
* Gets the local track associated with a specific {@code MEDIA_TYPE} in a
|
2017-07-31 11:27:34 -05:00
|
|
|
* specific redux store.
|
2016-10-25 11:43:15 -05:00
|
|
|
*
|
2017-07-31 11:27:34 -05:00
|
|
|
* @param {Store} store - The redux store from which the local track associated
|
2017-10-01 01:35:19 -05:00
|
|
|
* with the specified {@code mediaType} is to be retrieved.
|
|
|
|
|
* @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
|
|
|
|
|
* be retrieved from the specified {@code store}.
|
2018-04-06 15:04:25 -05:00
|
|
|
* @param {boolean} [includePending] - Indicates whether a local track is to be
|
|
|
|
|
* returned if it is still pending. A local track is pending if
|
|
|
|
|
* {@code getUserMedia} is still executing to create it and, consequently, its
|
|
|
|
|
* {@code jitsiTrack} property is {@code undefined}. By default a pending local
|
|
|
|
|
* track is not returned.
|
2016-10-25 11:43:15 -05:00
|
|
|
* @private
|
2017-10-01 01:35:19 -05:00
|
|
|
* @returns {Track} The local {@code Track} associated with the specified
|
|
|
|
|
* {@code mediaType} in the specified {@code store}.
|
2016-10-25 11:43:15 -05:00
|
|
|
*/
|
2018-04-06 15:04:25 -05:00
|
|
|
function _getLocalTrack(
|
2023-05-22 09:54:11 +03:00
|
|
|
{ getState }: { getState: IStore['getState']; },
|
2022-09-23 10:48:20 +03:00
|
|
|
mediaType: MediaType,
|
|
|
|
|
includePending = false) {
|
2018-04-06 15:04:25 -05:00
|
|
|
return (
|
|
|
|
|
getLocalTrack(
|
|
|
|
|
getState()['features/base/tracks'],
|
|
|
|
|
mediaType,
|
|
|
|
|
includePending));
|
2016-10-25 11:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
2016-10-05 09:36:59 -05:00
|
|
|
/**
|
|
|
|
|
* Mutes or unmutes a local track with a specific media type.
|
|
|
|
|
*
|
2017-07-31 11:27:34 -05:00
|
|
|
* @param {Store} store - The redux store in which the specified action is
|
2016-10-05 09:36:59 -05:00
|
|
|
* dispatched.
|
2017-07-31 11:27:34 -05:00
|
|
|
* @param {Action} action - The redux action dispatched in the specified store.
|
2016-10-05 09:36:59 -05:00
|
|
|
* @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
|
|
|
|
|
* which is being muted or unmuted.
|
|
|
|
|
* @private
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2024-07-10 09:54:41 +03:00
|
|
|
function _setMuted(store: IStore, { ensureTrack, muted }: {
|
2024-01-09 12:07:29 -05:00
|
|
|
ensureTrack: boolean; muted: boolean; }, mediaType: MediaType) {
|
2022-03-15 13:24:49 -04:00
|
|
|
const { dispatch, getState } = store;
|
|
|
|
|
const localTrack = _getLocalTrack(store, mediaType, /* includePending */ true);
|
|
|
|
|
const state = getState();
|
|
|
|
|
|
2017-08-14 15:27:24 +02:00
|
|
|
if (localTrack) {
|
2022-03-15 13:24:49 -04:00
|
|
|
// The `jitsiTrack` property will have a value only for a localTrack for which `getUserMedia` has already
|
|
|
|
|
// completed. If there's no `jitsiTrack`, then the `muted` state will be applied once the `jitsiTrack` is
|
|
|
|
|
// created.
|
2018-04-06 15:04:25 -05:00
|
|
|
const { jitsiTrack } = localTrack;
|
2024-01-09 12:07:29 -05:00
|
|
|
|
|
|
|
|
if (jitsiTrack) {
|
2023-05-18 14:16:37 -05:00
|
|
|
setTrackMuted(jitsiTrack, muted, state, dispatch)
|
|
|
|
|
.catch(() => dispatch(trackMuteUnmuteFailed(localTrack, muted)));
|
2022-03-15 13:24:49 -04:00
|
|
|
}
|
2025-06-09 23:44:24 +03:00
|
|
|
} else if (!muted && ensureTrack) {
|
2025-06-30 10:59:28 +02:00
|
|
|
// TODO(saghul): reconcile these 2 types.
|
|
|
|
|
const createMediaType = mediaType === MEDIA_TYPE.SCREENSHARE ? 'desktop' : mediaType;
|
|
|
|
|
|
2023-05-18 14:16:37 -05:00
|
|
|
typeof APP !== 'undefined' && dispatch(gumPending([ mediaType ], IGUMPendingState.PENDING_UNMUTE));
|
2025-06-30 10:59:28 +02:00
|
|
|
dispatch(createLocalTracksA({ devices: [ createMediaType ] })).then(() => {
|
2023-05-18 14:16:37 -05:00
|
|
|
typeof APP !== 'undefined' && dispatch(gumPending([ mediaType ], IGUMPendingState.NONE));
|
|
|
|
|
});
|
2017-08-14 15:27:24 +02:00
|
|
|
}
|
2016-10-25 11:43:15 -05:00
|
|
|
}
|