2021-12-08 21:20:26 +00:00
|
|
|
import { batch } from 'react-redux';
|
|
|
|
|
2022-09-23 07:48:20 +00:00
|
|
|
import { IStore } from '../../app/types';
|
2022-04-28 08:34:23 +00:00
|
|
|
import { _RESET_BREAKOUT_ROOMS } from '../../breakout-rooms/actionTypes';
|
2020-06-19 07:03:26 +00:00
|
|
|
import { isPrejoinPageVisible } from '../../prejoin/functions';
|
2021-12-08 21:20:26 +00:00
|
|
|
import { getCurrentConference } from '../conference/functions';
|
2022-09-23 07:48:20 +00:00
|
|
|
import { getMultipleVideoSendingSupportFeatureFlag } from '../config/functions.any';
|
2016-10-05 14:36:59 +00:00
|
|
|
import {
|
2017-01-11 18:14:00 +00:00
|
|
|
SET_AUDIO_MUTED,
|
|
|
|
SET_CAMERA_FACING_MODE,
|
2022-09-27 07:10:28 +00:00
|
|
|
SET_SCREENSHARE_MUTED,
|
2017-01-11 18:14:00 +00:00
|
|
|
SET_VIDEO_MUTED,
|
2022-09-27 07:10:28 +00:00
|
|
|
TOGGLE_CAMERA_FACING_MODE
|
2022-09-23 07:48:20 +00:00
|
|
|
} from '../media/actionTypes';
|
2022-11-01 12:36:32 +00:00
|
|
|
import { toggleCameraFacingMode } from '../media/actions';
|
2022-09-23 07:48:20 +00:00
|
|
|
import {
|
|
|
|
CAMERA_FACING_MODE,
|
|
|
|
MEDIA_TYPE,
|
2022-09-27 07:10:28 +00:00
|
|
|
MediaType,
|
2022-09-23 07:48:20 +00:00
|
|
|
SCREENSHARE_MUTISM_AUTHORITY,
|
2022-11-01 12:36:32 +00:00
|
|
|
VIDEO_MUTISM_AUTHORITY
|
2022-09-23 07:48:20 +00:00
|
|
|
} from '../media/constants';
|
|
|
|
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
|
|
|
import StateListenerRegistry from '../redux/StateListenerRegistry';
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2018-03-07 00:28:19 +00:00
|
|
|
import {
|
|
|
|
TRACK_UPDATED
|
|
|
|
} from './actionTypes';
|
2020-05-20 10:57:03 +00:00
|
|
|
import {
|
|
|
|
createLocalTracksA,
|
2021-12-10 10:50:12 +00:00
|
|
|
destroyLocalTracks,
|
2022-03-15 17:24:49 +00:00
|
|
|
trackMuteUnmuteFailed,
|
2022-09-27 07:10:28 +00:00
|
|
|
trackRemoved
|
2020-05-20 10:57:03 +00:00
|
|
|
} from './actions';
|
2019-07-10 11:02:27 +00:00
|
|
|
import {
|
|
|
|
getLocalTrack,
|
|
|
|
isUserInteractionRequiredForUnmute,
|
|
|
|
setTrackMuted
|
|
|
|
} from './functions';
|
2021-01-12 09:13:20 +00:00
|
|
|
import './subscriber';
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
2017-02-27 22:45:53 +00: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 14:36:59 +00:00
|
|
|
*
|
2017-07-31 16:27:34 +00:00
|
|
|
* @param {Store} store - The redux store.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2017-01-11 18:14:00 +00:00
|
|
|
case SET_AUDIO_MUTED:
|
2019-07-10 11:02:27 +00:00
|
|
|
if (!action.muted
|
|
|
|
&& isUserInteractionRequiredForUnmute(store.getState())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:14:00 +00:00
|
|
|
_setMuted(store, action, MEDIA_TYPE.AUDIO);
|
2016-10-05 14:36:59 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-22 09:25:58 +00:00
|
|
|
case SET_CAMERA_FACING_MODE: {
|
2017-07-18 21:41:39 +00: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 09:25:58 +00:00
|
|
|
const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
|
2017-07-18 21:41:39 +00:00
|
|
|
let jitsiTrack;
|
2017-02-22 09:25:58 +00:00
|
|
|
|
2017-07-18 21:41:39 +00:00
|
|
|
if (localTrack
|
|
|
|
&& (jitsiTrack = localTrack.jitsiTrack)
|
|
|
|
&& jitsiTrack.getCameraFacingMode()
|
|
|
|
!== action.cameraFacingMode) {
|
|
|
|
store.dispatch(toggleCameraFacingMode());
|
2017-02-22 09:25:58 +00:00
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
break;
|
2017-02-22 09:25:58 +00:00
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2022-03-15 17:24:49 +00:00
|
|
|
case SET_SCREENSHARE_MUTED:
|
2022-12-07 21:41:50 +00:00
|
|
|
_setMuted(store, action, MEDIA_TYPE.SCREENSHARE);
|
2022-03-15 17:24:49 +00:00
|
|
|
break;
|
|
|
|
|
2017-01-17 14:32:20 +00:00
|
|
|
case SET_VIDEO_MUTED:
|
2019-07-10 11:02:27 +00:00
|
|
|
if (!action.muted
|
|
|
|
&& isUserInteractionRequiredForUnmute(store.getState())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-07 21:37:12 +00:00
|
|
|
_setMuted(store, action, MEDIA_TYPE.VIDEO);
|
2016-10-05 14:36:59 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-03 08:57:01 +00:00
|
|
|
case TOGGLE_CAMERA_FACING_MODE: {
|
|
|
|
const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
|
|
|
|
let jitsiTrack;
|
2017-04-05 09:03:16 +00: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 08:57:01 +00:00
|
|
|
|
|
|
|
// Don't mirror the video of the back/environment-facing camera.
|
2017-04-05 09:03:16 +00:00
|
|
|
const mirror
|
|
|
|
= jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
|
|
|
|
|
2017-04-03 08:57:01 +00:00
|
|
|
store.dispatch({
|
|
|
|
type: TRACK_UPDATED,
|
|
|
|
track: {
|
|
|
|
jitsiTrack,
|
2017-04-05 09:03:16 +00:00
|
|
|
mirror
|
2017-04-03 08:57:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
2021-12-08 21:20:26 +00:00
|
|
|
/**
|
|
|
|
* Set up state change listener to perform maintenance tasks when the conference
|
2021-12-09 17:46:27 +00:00
|
|
|
* is left or failed, remove all tracks from the store.
|
2021-12-08 21:20:26 +00:00
|
|
|
*/
|
|
|
|
StateListenerRegistry.register(
|
|
|
|
state => getCurrentConference(state),
|
|
|
|
(conference, { dispatch, getState }, prevConference) => {
|
2022-04-14 19:35:41 +00:00
|
|
|
const { authRequired, error } = getState()['features/base/conference'];
|
2021-12-23 17:57:05 +00:00
|
|
|
|
|
|
|
// conference keep flipping while we are authenticating, skip clearing while we are in that process
|
2022-04-14 19:35:41 +00:00
|
|
|
if (prevConference && !conference && !authRequired && !error) {
|
2021-12-23 17:57:05 +00:00
|
|
|
|
2021-12-09 17:46:27 +00:00
|
|
|
// Clear all tracks.
|
2021-12-10 10:50:12 +00:00
|
|
|
const remoteTracks = getState()['features/base/tracks'].filter(t => !t.local);
|
2021-12-08 21:20:26 +00:00
|
|
|
|
|
|
|
batch(() => {
|
2021-12-10 10:50:12 +00:00
|
|
|
dispatch(destroyLocalTracks());
|
|
|
|
for (const track of remoteTracks) {
|
2021-12-08 21:20:26 +00:00
|
|
|
dispatch(trackRemoved(track.jitsiTrack));
|
|
|
|
}
|
2022-04-28 08:34:23 +00:00
|
|
|
dispatch({ type: _RESET_BREAKOUT_ROOMS });
|
2021-12-08 21:20:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-25 16:43:15 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Gets the local track associated with a specific {@code MEDIA_TYPE} in a
|
2017-07-31 16:27:34 +00:00
|
|
|
* specific redux store.
|
2016-10-25 16:43:15 +00:00
|
|
|
*
|
2017-07-31 16:27:34 +00:00
|
|
|
* @param {Store} store - The redux store from which the local track associated
|
2017-10-01 06:35:19 +00: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 20:04:25 +00: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 16:43:15 +00:00
|
|
|
* @private
|
2017-10-01 06:35:19 +00:00
|
|
|
* @returns {Track} The local {@code Track} associated with the specified
|
|
|
|
* {@code mediaType} in the specified {@code store}.
|
2016-10-25 16:43:15 +00:00
|
|
|
*/
|
2018-04-06 20:04:25 +00:00
|
|
|
function _getLocalTrack(
|
2022-09-23 07:48:20 +00:00
|
|
|
{ getState }: { getState: Function; },
|
|
|
|
mediaType: MediaType,
|
|
|
|
includePending = false) {
|
2018-04-06 20:04:25 +00:00
|
|
|
return (
|
|
|
|
getLocalTrack(
|
|
|
|
getState()['features/base/tracks'],
|
|
|
|
mediaType,
|
|
|
|
includePending));
|
2016-10-25 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Mutes or unmutes a local track with a specific media type.
|
|
|
|
*
|
2017-07-31 16:27:34 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified action is
|
2016-10-05 14:36:59 +00:00
|
|
|
* dispatched.
|
2017-07-31 16:27:34 +00:00
|
|
|
* @param {Action} action - The redux action dispatched in the specified store.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
|
|
|
|
* which is being muted or unmuted.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-23 07:48:20 +00:00
|
|
|
async function _setMuted(store: IStore, { ensureTrack, authority, muted }: {
|
|
|
|
authority: number; ensureTrack: boolean; muted: boolean; }, mediaType: MediaType) {
|
2022-03-15 17:24:49 +00:00
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const localTrack = _getLocalTrack(store, mediaType, /* includePending */ true);
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
if (mediaType === MEDIA_TYPE.SCREENSHARE
|
2022-04-29 14:32:16 +00:00
|
|
|
&& getMultipleVideoSendingSupportFeatureFlag(state)
|
2022-03-15 17:24:49 +00:00
|
|
|
&& !muted) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-08-14 13:27:24 +00:00
|
|
|
if (localTrack) {
|
2022-03-15 17:24:49 +00: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 20:04:25 +00:00
|
|
|
const { jitsiTrack } = localTrack;
|
2022-03-15 17:24:49 +00:00
|
|
|
const isAudioOnly = (mediaType === MEDIA_TYPE.VIDEO && authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY)
|
|
|
|
|| (mediaType === MEDIA_TYPE.SCREENSHARE && authority === SCREENSHARE_MUTISM_AUTHORITY.AUDIO_ONLY);
|
|
|
|
|
|
|
|
// Screenshare cannot be unmuted using the video mute button unless it is muted by audioOnly in the legacy
|
|
|
|
// screensharing mode.
|
2022-04-29 14:32:16 +00:00
|
|
|
if (jitsiTrack && (
|
|
|
|
jitsiTrack.videoType !== 'desktop' || isAudioOnly || getMultipleVideoSendingSupportFeatureFlag(state))
|
|
|
|
) {
|
2022-03-15 17:24:49 +00:00
|
|
|
setTrackMuted(jitsiTrack, muted, state).catch(() => dispatch(trackMuteUnmuteFailed(localTrack, muted)));
|
|
|
|
}
|
|
|
|
} else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(state))) {
|
2017-08-14 13:27:24 +00:00
|
|
|
// FIXME: This only runs on mobile now because web has its own way of
|
|
|
|
// creating local tracks. Adjust the check once they are unified.
|
2022-03-15 17:24:49 +00:00
|
|
|
dispatch(createLocalTracksA({ devices: [ mediaType ] }));
|
2017-08-14 13:27:24 +00:00
|
|
|
}
|
2016-10-25 16:43:15 +00:00
|
|
|
}
|