2018-03-07 00:28:19 +00:00
|
|
|
// @flow
|
2017-02-03 18:51:12 +00:00
|
|
|
|
2020-05-20 10:57:03 +00:00
|
|
|
import UIEvents from '../../../../service/UI/UIEvents';
|
|
|
|
import { hideNotification } from '../../notifications';
|
2020-06-19 07:03:26 +00:00
|
|
|
import { isPrejoinPageVisible } from '../../prejoin/functions';
|
|
|
|
import { getAvailableDevices } from '../devices/actions';
|
2016-10-05 14:36:59 +00:00
|
|
|
import {
|
2017-04-05 09:03:16 +00:00
|
|
|
CAMERA_FACING_MODE,
|
2016-10-05 14:36:59 +00:00
|
|
|
MEDIA_TYPE,
|
2017-01-11 18:14:00 +00:00
|
|
|
SET_AUDIO_MUTED,
|
|
|
|
SET_CAMERA_FACING_MODE,
|
|
|
|
SET_VIDEO_MUTED,
|
2019-11-26 10:57:03 +00:00
|
|
|
VIDEO_MUTISM_AUTHORITY,
|
2017-07-18 21:41:39 +00:00
|
|
|
TOGGLE_CAMERA_FACING_MODE,
|
|
|
|
toggleCameraFacingMode
|
2016-10-05 14:36:59 +00:00
|
|
|
} from '../media';
|
|
|
|
import { MiddlewareRegistry } from '../redux';
|
|
|
|
|
2018-03-07 00:28:19 +00:00
|
|
|
import {
|
2020-06-19 07:03:26 +00:00
|
|
|
TRACK_ADDED,
|
2018-03-07 00:28:19 +00:00
|
|
|
TOGGLE_SCREENSHARING,
|
2019-06-12 21:05:41 +00:00
|
|
|
TRACK_NO_DATA_FROM_SOURCE,
|
|
|
|
TRACK_REMOVED,
|
2018-03-07 00:28:19 +00:00
|
|
|
TRACK_UPDATED
|
|
|
|
} from './actionTypes';
|
2020-05-20 10:57:03 +00:00
|
|
|
import {
|
|
|
|
createLocalTracksA,
|
|
|
|
showNoDataFromSourceVideoError,
|
|
|
|
trackNoDataFromSourceNotificationInfoChanged
|
|
|
|
} from './actions';
|
2019-07-10 11:02:27 +00:00
|
|
|
import {
|
|
|
|
getLocalTrack,
|
|
|
|
getTrackByJitsiTrack,
|
|
|
|
isUserInteractionRequiredForUnmute,
|
|
|
|
setTrackMuted
|
|
|
|
} from './functions';
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-06-20 16:30:54 +00:00
|
|
|
declare var APP: Object;
|
|
|
|
|
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) {
|
2020-06-19 07:03:26 +00:00
|
|
|
case TRACK_ADDED: {
|
|
|
|
// The devices list needs to be refreshed when no initial video permissions
|
|
|
|
// were granted and a local video track is added by umuting the video.
|
|
|
|
if (action.track.local) {
|
|
|
|
store.dispatch(getAvailableDevices());
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2019-06-12 21:05:41 +00:00
|
|
|
case TRACK_NO_DATA_FROM_SOURCE: {
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
_handleNoDataFromSourceErrors(store, action);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
case TRACK_REMOVED: {
|
|
|
|
_removeNoDataFromSourceNotification(store, action.track);
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:57:03 +00:00
|
|
|
_setMuted(store, action, action.mediaType);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-07 00:28:19 +00:00
|
|
|
case TOGGLE_SCREENSHARING:
|
|
|
|
if (typeof APP === 'object') {
|
|
|
|
APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-10-25 16:43:15 +00:00
|
|
|
case TRACK_UPDATED:
|
2017-08-04 21:06:42 +00:00
|
|
|
// TODO Remove the following calls to APP.UI once components interested
|
|
|
|
// in track mute changes are moved into React and/or redux.
|
2017-06-20 20:09:34 +00:00
|
|
|
if (typeof APP !== 'undefined') {
|
2020-04-14 22:41:30 +00:00
|
|
|
const result = next(action);
|
|
|
|
|
2020-06-29 07:45:58 +00:00
|
|
|
if (isPrejoinPageVisible(store.getState())) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-20 16:30:54 +00:00
|
|
|
const { jitsiTrack } = action.track;
|
2017-08-04 21:06:42 +00:00
|
|
|
const muted = jitsiTrack.isMuted();
|
2017-06-20 16:30:54 +00:00
|
|
|
const participantID = jitsiTrack.getParticipantId();
|
2019-11-26 10:57:03 +00:00
|
|
|
const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
|
2017-06-20 20:09:34 +00:00
|
|
|
|
2017-08-18 11:30:30 +00:00
|
|
|
if (isVideoTrack) {
|
2019-12-04 20:28:42 +00:00
|
|
|
if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
|
|
|
|
APP.conference.mutePresenter(muted);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we change the video mute state only for camera tracks.
|
|
|
|
if (jitsiTrack.isLocal() && jitsiTrack.videoType !== 'desktop') {
|
2017-08-04 08:15:11 +00:00
|
|
|
APP.conference.setVideoMuteStatus(muted);
|
2017-06-20 20:09:34 +00:00
|
|
|
} else {
|
2017-08-18 11:30:30 +00:00
|
|
|
APP.UI.setVideoMuted(participantID, muted);
|
2017-06-20 20:09:34 +00:00
|
|
|
}
|
2019-07-02 13:06:52 +00:00
|
|
|
APP.UI.onPeerVideoTypeChanged(participantID, jitsiTrack.videoType);
|
2017-08-18 11:30:30 +00:00
|
|
|
} else if (jitsiTrack.isLocal()) {
|
|
|
|
APP.conference.setAudioMuteStatus(muted);
|
2017-06-20 16:30:54 +00:00
|
|
|
} else {
|
2017-08-04 21:06:42 +00:00
|
|
|
APP.UI.setAudioMuted(participantID, muted);
|
2017-06-20 16:30:54 +00:00
|
|
|
}
|
2020-04-14 22:41:30 +00:00
|
|
|
|
|
|
|
return result;
|
2017-06-20 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
2019-06-12 21:05:41 +00:00
|
|
|
/**
|
|
|
|
* Handles no data from source errors.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified action is
|
|
|
|
* dispatched.
|
|
|
|
* @param {Action} action - The redux action dispatched in the specified store.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _handleNoDataFromSourceErrors(store, action) {
|
|
|
|
const { getState, dispatch } = store;
|
|
|
|
|
|
|
|
const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
|
|
|
|
|
|
|
|
if (!track || !track.local) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { jitsiTrack } = track;
|
|
|
|
|
|
|
|
if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
|
|
|
|
_removeNoDataFromSourceNotification(store, action.track);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track.mediaType === MEDIA_TYPE.VIDEO) {
|
|
|
|
const { noDataFromSourceNotificationInfo = {} } = track;
|
|
|
|
|
|
|
|
if (track.isReceivingData) {
|
|
|
|
if (noDataFromSourceNotificationInfo.timeout) {
|
|
|
|
clearTimeout(noDataFromSourceNotificationInfo.timeout);
|
|
|
|
dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to remove the notification if there is one.
|
|
|
|
_removeNoDataFromSourceNotification(store, action.track);
|
|
|
|
} else {
|
|
|
|
if (noDataFromSourceNotificationInfo.timeout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
|
|
|
|
|
|
|
|
dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
{ getState }: { getState: Function },
|
|
|
|
mediaType: MEDIA_TYPE,
|
|
|
|
includePending: boolean = false) {
|
|
|
|
return (
|
|
|
|
getLocalTrack(
|
|
|
|
getState()['features/base/tracks'],
|
|
|
|
mediaType,
|
|
|
|
includePending));
|
2016-10-25 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 21:05:41 +00:00
|
|
|
/**
|
|
|
|
* Removes the no data from source notification associated with the JitsiTrack if displayed.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @param {Track} track - The redux action dispatched in the specified store.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
|
|
|
|
const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
|
|
|
|
const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
|
|
|
|
|
|
|
|
if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
|
|
|
|
dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
|
|
|
|
dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
*/
|
2019-11-26 10:57:03 +00:00
|
|
|
function _setMuted(store, { ensureTrack, authority, muted }, mediaType: MEDIA_TYPE) {
|
2018-04-06 20:04:25 +00:00
|
|
|
const localTrack
|
|
|
|
= _getLocalTrack(store, mediaType, /* includePending */ true);
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-08-14 13:27:24 +00:00
|
|
|
if (localTrack) {
|
2018-04-06 20:04:25 +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.
|
|
|
|
const { jitsiTrack } = localTrack;
|
2019-11-26 10:57:03 +00:00
|
|
|
const isAudioOnly = authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY;
|
2018-04-06 20:04:25 +00:00
|
|
|
|
2019-11-26 10:57:03 +00:00
|
|
|
// screenshare cannot be muted or unmuted using the video mute button
|
|
|
|
// anymore, unless it is muted by audioOnly.
|
|
|
|
jitsiTrack && (jitsiTrack.videoType !== 'desktop' || isAudioOnly)
|
|
|
|
&& setTrackMuted(jitsiTrack, muted);
|
2020-06-19 07:03:26 +00:00
|
|
|
} else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(store.getState()))) {
|
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.
|
|
|
|
store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
|
|
|
|
}
|
2016-10-25 16:43:15 +00:00
|
|
|
}
|