2017-02-03 18:51:12 +00:00
|
|
|
/* @flow */
|
|
|
|
|
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,
|
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';
|
|
|
|
|
2017-08-14 13:27:24 +00:00
|
|
|
import { createLocalTracksA } from './actions';
|
2017-06-20 16:30:54 +00:00
|
|
|
import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
|
2017-08-18 11:30:30 +00:00
|
|
|
import { getLocalTrack, 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) {
|
2017-01-11 18:14:00 +00:00
|
|
|
case SET_AUDIO_MUTED:
|
|
|
|
_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:
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2017-06-20 16:30:54 +00:00
|
|
|
case TRACK_ADDED:
|
|
|
|
// TODO Remove this middleware case once all UI interested in new tracks
|
|
|
|
// being added are converted to react and listening for store changes.
|
|
|
|
if (typeof APP !== 'undefined' && !action.track.local) {
|
|
|
|
APP.UI.addRemoteStream(action.track.jitsiTrack);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRACK_REMOVED:
|
|
|
|
// TODO Remove this middleware case once all UI interested in tracks
|
|
|
|
// being removed are converted to react and listening for store changes.
|
|
|
|
if (typeof APP !== 'undefined' && !action.track.local) {
|
|
|
|
APP.UI.removeRemoteStream(action.track.jitsiTrack);
|
|
|
|
}
|
|
|
|
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') {
|
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();
|
2017-06-20 20:09:34 +00:00
|
|
|
const isVideoTrack = jitsiTrack.isVideoTrack();
|
|
|
|
|
2017-08-18 11:30:30 +00:00
|
|
|
if (isVideoTrack) {
|
|
|
|
if (jitsiTrack.isLocal()) {
|
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
|
|
|
}
|
2017-06-20 16:30:54 +00:00
|
|
|
APP.UI.onPeerVideoTypeChanged(
|
2017-08-04 21:06:42 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
2016-10-25 16:43:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> 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
|
2016-10-25 16:43:15 +00:00
|
|
|
* with the specified <tt>mediaType</tt> is to be retrieved.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
|
|
|
|
* be retrieved from the specified <tt>store</tt>.
|
|
|
|
* @private
|
|
|
|
* @returns {Track} The local <tt>Track</tt> associated with the specified
|
|
|
|
* <tt>mediaType</tt> in the specified <tt>store</tt>.
|
|
|
|
*/
|
2017-08-04 21:06:42 +00:00
|
|
|
function _getLocalTrack({ getState }, mediaType: MEDIA_TYPE) {
|
|
|
|
return getLocalTrack(getState()['features/base/tracks'], mediaType);
|
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}
|
|
|
|
*/
|
2017-08-14 13:27:24 +00:00
|
|
|
function _setMuted(store, { ensureTrack, muted }, mediaType: MEDIA_TYPE) {
|
2016-10-25 16:43:15 +00:00
|
|
|
const localTrack = _getLocalTrack(store, mediaType);
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-08-14 13:27:24 +00:00
|
|
|
if (localTrack) {
|
|
|
|
setTrackMuted(localTrack.jitsiTrack, muted);
|
|
|
|
} else if (!muted && ensureTrack && typeof APP === 'undefined') {
|
|
|
|
// 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
|
|
|
}
|