From 40c9f583fac6f239aaaf6eac364e90e148e757c2 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Mon, 31 Jul 2017 12:34:18 +0200 Subject: [PATCH] ref(tracks): remove duplication in mute/unmute --- react/features/base/media/middleware.js | 15 ++------------ react/features/base/tracks/functions.js | 25 +++++++++++++++++++----- react/features/base/tracks/middleware.js | 12 +----------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/react/features/base/media/middleware.js b/react/features/base/media/middleware.js index b12ce0a36..2bfa2c1c4 100644 --- a/react/features/base/media/middleware.js +++ b/react/features/base/media/middleware.js @@ -6,7 +6,7 @@ import { MiddlewareRegistry } from '../redux'; import { setTrackMuted, TRACK_ADDED } from '../tracks'; 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. @@ -104,17 +104,6 @@ function _syncTrackMutedState(store, track) { // fired before track gets to state. if (track.muted !== muted) { track.muted = muted; - setTrackMuted(track.jitsiTrack, muted) - .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)); - }); + setTrackMuted(track.jitsiTrack, muted, store); } } diff --git a/react/features/base/tracks/functions.js b/react/features/base/tracks/functions.js index b0b3ba28b..2ebcd7136 100644 --- a/react/features/base/tracks/functions.js +++ b/react/features/base/tracks/functions.js @@ -1,7 +1,7 @@ /* global APP */ 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); @@ -159,20 +159,35 @@ export function getTracksByMediaType(tracks, mediaType) { /** * Mutes or unmutes a specific JitsiLocalTrack. If the muted state of * the specified track is already in accord with the specified - * muted value, then does nothing. + * muted 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 JitsiLocalTrack to mute or * unmute. * @param {boolean} muted - If the specified track is to be muted, then * true; otherwise, false. - * @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) { return Promise.resolve(); } 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)); + }); } diff --git a/react/features/base/tracks/middleware.js b/react/features/base/tracks/middleware.js index d44c63987..b61b0f736 100644 --- a/react/features/base/tracks/middleware.js +++ b/react/features/base/tracks/middleware.js @@ -161,17 +161,7 @@ function _setMuted(store, action, mediaType: MEDIA_TYPE) { const localTrack = _getLocalTrack(store, mediaType); if (localTrack) { - setTrackMuted(localTrack.jitsiTrack, action.muted) - .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)); - }); + setTrackMuted(localTrack.jitsiTrack, action.muted, store); } }