ref(tracks): remove duplication in mute/unmute

This commit is contained in:
paweldomas 2017-07-31 12:34:18 +02:00 committed by Lyubo Marinov
parent 3e84d8b3b6
commit 40c9f583fa
3 changed files with 23 additions and 29 deletions

View File

@ -6,7 +6,7 @@ import { MiddlewareRegistry } from '../redux';
import { setTrackMuted, TRACK_ADDED } from '../tracks'; import { setTrackMuted, TRACK_ADDED } from '../tracks';
import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions'; import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
import { CAMERA_FACING_MODE, MEDIA_TYPE } from './constants'; import { CAMERA_FACING_MODE } from './constants';
/** /**
* Implements the entry point of the middleware of the feature base/media. * Implements the entry point of the middleware of the feature base/media.
@ -104,17 +104,6 @@ function _syncTrackMutedState(store, track) {
// fired before track gets to state. // fired before track gets to state.
if (track.muted !== muted) { if (track.muted !== muted) {
track.muted = muted; track.muted = muted;
setTrackMuted(track.jitsiTrack, muted) setTrackMuted(track.jitsiTrack, muted, store);
.catch(error => {
console.error(`setTrackMuted(${muted}) failed`, error);
const setMuted
= track.mediaType === MEDIA_TYPE.AUDIO
? setAudioMuted
: setVideoMuted;
// Failed to sync muted state - dispatch rollback action
store.dispatch(setMuted(!muted));
});
} }
} }

View File

@ -1,7 +1,7 @@
/* global APP */ /* global APP */
import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet'; import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
import { MEDIA_TYPE } from '../media'; import { MEDIA_TYPE, setAudioMuted, setVideoMuted } from '../media';
const logger = require('jitsi-meet-logger').getLogger(__filename); const logger = require('jitsi-meet-logger').getLogger(__filename);
@ -159,20 +159,35 @@ export function getTracksByMediaType(tracks, mediaType) {
/** /**
* Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
* the specified <tt>track</tt> is already in accord with the specified * the specified <tt>track</tt> is already in accord with the specified
* <tt>muted</tt> value, then does nothing. * <tt>muted</tt> value, then does nothing. In case mute/unmute operation fails
* (JitsiLocalTrack Promise is rejected) a rollback action will be dispatched on
* the given store. For example if the mute operation fails then the 'unmute'
* action will be dispatched to rollback to the previous state in base/media.
* *
* @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
* unmute. * unmute.
* @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
* <tt>true</tt>; otherwise, <tt>false</tt>. * <tt>true</tt>; otherwise, <tt>false</tt>.
* @returns {Promise} * @param {Store} store - The redux store in the context of which the function
* is to execute and which will be used to dispatch the rollback action in case
* mute/unmute fails.
* @returns {Promise}
*/ */
export function setTrackMuted(track, muted) { export function setTrackMuted(track, muted, { dispatch }) {
if (track.isMuted() === muted) { if (track.isMuted() === muted) {
return Promise.resolve(); return Promise.resolve();
} }
const f = muted ? 'mute' : 'unmute'; const f = muted ? 'mute' : 'unmute';
return track[f](); return track[f]().catch(error => {
console.error(`set track ${f} failed`, error);
const setMuted
= track.mediaType === MEDIA_TYPE.AUDIO
? setAudioMuted
: setVideoMuted;
dispatch(setMuted(!muted));
});
} }

View File

@ -161,17 +161,7 @@ function _setMuted(store, action, mediaType: MEDIA_TYPE) {
const localTrack = _getLocalTrack(store, mediaType); const localTrack = _getLocalTrack(store, mediaType);
if (localTrack) { if (localTrack) {
setTrackMuted(localTrack.jitsiTrack, action.muted) setTrackMuted(localTrack.jitsiTrack, action.muted, store);
.catch(error => {
console.error(`setTrackMuted(${action.muted}) failed`, error);
const setMuted
= mediaType === MEDIA_TYPE.AUDIO
? setAudioMuted : setVideoMuted;
// Failed to modify muted state - dispatch rollback action
store.dispatch(setMuted(!action.muted));
});
} }
} }