2017-02-03 18:51:12 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-02-27 22:45:53 +00:00
|
|
|
import { LIB_DID_DISPOSE, LIB_DID_INIT } from '../lib-jitsi-meet';
|
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-04-03 08:57:01 +00:00
|
|
|
TOGGLE_CAMERA_FACING_MODE,
|
2017-01-11 18:14:00 +00:00
|
|
|
setAudioMuted,
|
|
|
|
setVideoMuted
|
2016-10-05 14:36:59 +00:00
|
|
|
} from '../media';
|
|
|
|
import { MiddlewareRegistry } from '../redux';
|
|
|
|
|
2017-02-22 09:25:58 +00:00
|
|
|
import {
|
|
|
|
_disposeAndRemoveTracks,
|
|
|
|
createLocalTracks,
|
|
|
|
destroyLocalTracks
|
|
|
|
} from './actions';
|
2016-10-25 16:43:15 +00:00
|
|
|
import { TRACK_UPDATED } from './actionTypes';
|
2017-01-17 14:32:20 +00:00
|
|
|
import { getLocalTrack, setTrackMuted } from './functions';
|
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
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2017-02-27 22:45:53 +00:00
|
|
|
case LIB_DID_DISPOSE:
|
|
|
|
store.dispatch(destroyLocalTracks());
|
2017-01-17 14:32:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-27 22:45:53 +00:00
|
|
|
case LIB_DID_INIT:
|
|
|
|
store.dispatch(createLocalTracks());
|
2017-01-17 14:32:20 +00:00
|
|
|
break;
|
|
|
|
|
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: {
|
|
|
|
// XXX Destroy the local video track before creating a new one or
|
|
|
|
// react-native-webrtc may be slow or get stuck when opening a (video)
|
|
|
|
// capturer twice.
|
|
|
|
const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
|
|
|
|
|
|
|
|
if (localTrack) {
|
|
|
|
store.dispatch(_disposeAndRemoveTracks([ localTrack.jitsiTrack ]));
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
store.dispatch(
|
|
|
|
createLocalTracks({
|
|
|
|
devices: [ MEDIA_TYPE.VIDEO ],
|
|
|
|
facingMode: action.cameraFacingMode
|
|
|
|
})
|
|
|
|
);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-25 16:43:15 +00:00
|
|
|
case TRACK_UPDATED:
|
|
|
|
return _trackUpdated(store, next, action);
|
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
|
|
|
|
* specific Redux store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store from which the local track associated
|
|
|
|
* 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-02-03 18:51:12 +00:00
|
|
|
function _getLocalTrack(store, mediaType: MEDIA_TYPE) {
|
2016-10-25 16:43:15 +00:00
|
|
|
return getLocalTrack(store.getState()['features/base/tracks'], mediaType);
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Mutes or unmutes a local track with a specific media type.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store in which the specified action is
|
|
|
|
* dispatched.
|
|
|
|
* @param {Action} action - The Redux action dispatched in the specified store.
|
|
|
|
* @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
|
|
|
|
* which is being muted or unmuted.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-03 18:51:12 +00:00
|
|
|
function _setMuted(store, action, mediaType: MEDIA_TYPE) {
|
2016-10-25 16:43:15 +00:00
|
|
|
const localTrack = _getLocalTrack(store, mediaType);
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
localTrack && setTrackMuted(localTrack.jitsiTrack, action.muted);
|
|
|
|
}
|
2016-10-25 16:43:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts the action <tt>TRACK_UPDATED</tt> in order to synchronize the
|
|
|
|
* muted states of the local tracks of features/base/tracks with the muted
|
|
|
|
* states of features/base/media.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store in which the specified <tt>action</tt>
|
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The Redux dispatch function to dispatch the
|
|
|
|
* specified <tt>action</tt> to the specified <tt>store</tt>.
|
|
|
|
* @param {Action} action - The Redux action <tt>TRACK_UPDATED</tt> which is
|
|
|
|
* being dispatched in the specified <tt>store</tt>.
|
|
|
|
* @private
|
2016-12-05 15:14:50 +00:00
|
|
|
* @returns {Object} The new state that is the result of the reduction of the
|
|
|
|
* specified <tt>action</tt>.
|
2016-10-25 16:43:15 +00:00
|
|
|
*/
|
|
|
|
function _trackUpdated(store, next, action) {
|
|
|
|
// Determine the muted state of the local track before the update.
|
|
|
|
const track = action.track;
|
|
|
|
let mediaType;
|
|
|
|
let oldMuted;
|
|
|
|
|
|
|
|
if ('muted' in track) {
|
|
|
|
// XXX The return value of JitsiTrack.getType() is of type MEDIA_TYPE
|
|
|
|
// that happens to be compatible with the type MEDIA_TYPE defined by
|
2017-05-08 14:43:55 +00:00
|
|
|
// jitsi-meet.
|
2016-10-25 16:43:15 +00:00
|
|
|
mediaType = track.jitsiTrack.getType();
|
|
|
|
|
|
|
|
const localTrack = _getLocalTrack(store, mediaType);
|
|
|
|
|
|
|
|
if (localTrack) {
|
|
|
|
oldMuted = localTrack.muted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
if (typeof oldMuted !== 'undefined') {
|
|
|
|
// Determine the muted state of the local track after the update. If the
|
|
|
|
// muted states before and after the update differ, then the respective
|
|
|
|
// media state should by synchronized.
|
|
|
|
const localTrack = _getLocalTrack(store, mediaType);
|
|
|
|
|
|
|
|
if (localTrack) {
|
|
|
|
const newMuted = localTrack.muted;
|
|
|
|
|
|
|
|
if (oldMuted !== newMuted) {
|
|
|
|
switch (mediaType) {
|
|
|
|
case MEDIA_TYPE.AUDIO:
|
2017-01-11 18:14:00 +00:00
|
|
|
store.dispatch(setAudioMuted(newMuted));
|
2016-10-25 16:43:15 +00:00
|
|
|
break;
|
|
|
|
case MEDIA_TYPE.VIDEO:
|
2017-01-11 18:14:00 +00:00
|
|
|
store.dispatch(setVideoMuted(newMuted));
|
2016-10-25 16:43:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|