2017-02-03 18:51:12 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
import { CONFERENCE_LEFT } from '../conference';
|
|
|
|
import { MiddlewareRegistry } from '../redux';
|
|
|
|
import { setTrackMuted, TRACK_ADDED } from '../tracks';
|
|
|
|
|
|
|
|
import {
|
2017-01-11 18:14:00 +00:00
|
|
|
setAudioMuted,
|
|
|
|
setCameraFacingMode,
|
|
|
|
setVideoMuted
|
2016-10-05 14:36:59 +00:00
|
|
|
} from './actions';
|
|
|
|
import { CAMERA_FACING_MODE } from './constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware that captures CONFERENCE_LEFT action and restores initial state
|
|
|
|
* for media devices. Also captures TRACK_ADDED to sync 'muted' state.
|
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case CONFERENCE_LEFT:
|
2017-01-28 23:28:13 +00:00
|
|
|
_resetInitialMediaState(store);
|
2016-10-05 14:36:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TRACK_ADDED:
|
2017-01-28 23:28:13 +00:00
|
|
|
action.track.local && _syncTrackMutedState(store, action.track);
|
2016-10-05 14:36:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets initial media state.
|
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
2017-01-28 23:28:13 +00:00
|
|
|
* @private
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-28 23:28:13 +00:00
|
|
|
function _resetInitialMediaState(store) {
|
2016-10-05 14:36:59 +00:00
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState()['features/base/media'];
|
|
|
|
|
2017-01-11 18:14:00 +00:00
|
|
|
state.audio.muted && dispatch(setAudioMuted(false));
|
2016-10-25 16:43:15 +00:00
|
|
|
(state.video.facingMode !== CAMERA_FACING_MODE.USER)
|
2017-01-11 18:14:00 +00:00
|
|
|
&& dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
|
|
|
|
state.video.muted && dispatch(setVideoMuted(false));
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Syncs muted state of local media track with muted state from media state.
|
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
|
|
|
* @param {Track} track - Local media track.
|
2017-01-28 23:28:13 +00:00
|
|
|
* @private
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-28 23:28:13 +00:00
|
|
|
function _syncTrackMutedState(store, track) {
|
2016-10-05 14:36:59 +00:00
|
|
|
const state = store.getState()['features/base/media'];
|
|
|
|
const muted = state[track.mediaType].muted;
|
|
|
|
|
|
|
|
// XXX If muted state of track when it was added is different from our media
|
|
|
|
// muted state, we need to mute track and explicitly modify 'muted' property
|
|
|
|
// on track. This is because though TRACK_ADDED action was dispatched it's
|
|
|
|
// not yet in Redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
|
|
|
|
// fired before track gets to state.
|
|
|
|
if (track.muted !== muted) {
|
|
|
|
track.muted = muted;
|
|
|
|
setTrackMuted(track.jitsiTrack, muted);
|
|
|
|
}
|
|
|
|
}
|