Browse Source

[RN] startWithAudioMuted & startWithVideoMuted

master
Saúl Ibarra Corretgé 8 years ago
parent
commit
f1c9e57b43
1 changed files with 66 additions and 30 deletions
  1. 66
    30
      react/features/base/media/middleware.js

+ 66
- 30
react/features/base/media/middleware.js View File

@@ -1,61 +1,95 @@
1 1
 /* @flow */
2 2
 
3
-import { CONFERENCE_LEFT } from '../conference';
3
+import { SET_ROOM } from '../conference';
4
+import { parseURLParams } from '../config';
4 5
 import { MiddlewareRegistry } from '../redux';
5 6
 import { setTrackMuted, TRACK_ADDED } from '../tracks';
6 7
 
7
-import {
8
-    setAudioMuted,
9
-    setCameraFacingMode,
10
-    setVideoMuted
11
-} from './actions';
8
+import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
12 9
 import { CAMERA_FACING_MODE, MEDIA_TYPE } from './constants';
13 10
 
14 11
 /**
15
- * Middleware that captures CONFERENCE_LEFT action and restores initial state
16
- * for media devices. Also captures TRACK_ADDED to sync 'muted' state.
12
+ * Implements the entry point of the middleware of the feature base/media.
17 13
  *
18
- * @param {Store} store - Redux store.
14
+ * @param {Store} store - The redux store.
19 15
  * @returns {Function}
20 16
  */
21 17
 MiddlewareRegistry.register(store => next => action => {
22
-    const result = next(action);
23
-
24 18
     switch (action.type) {
25
-    case CONFERENCE_LEFT:
26
-        _resetInitialMediaState(store);
27
-        break;
19
+    case SET_ROOM:
20
+        return _setRoom(store, next, action);
21
+
22
+    case TRACK_ADDED: {
23
+        const result = next(action);
28 24
 
29
-    case TRACK_ADDED:
30 25
         action.track.local && _syncTrackMutedState(store, action.track);
31
-        break;
26
+
27
+        return result;
28
+    }
32 29
     }
33 30
 
34
-    return result;
31
+    return next(action);
35 32
 });
36 33
 
37 34
 /**
38
- * Resets initial media state.
35
+ * Notifies the feature base/media that the action {@link SET_ROOM} is being
36
+ * dispatched within a specific redux {@code store}.
39 37
  *
40
- * @param {Store} store - Redux store.
38
+ * @param {Store} store - The redux store in which the specified {@code action}
39
+ * is being dispatched.
40
+ * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
41
+ * specified {@code action} to the specified {@code store}.
42
+ * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
43
+ * dispatched in the specified {@code store}.
41 44
  * @private
42
- * @returns {void}
45
+ * @returns {Object} The new state that is the result of the reduction of the
46
+ * specified {@code action}.
43 47
  */
44
-function _resetInitialMediaState(store) {
45
-    const { dispatch, getState } = store;
46
-    const state = getState()['features/base/media'];
48
+function _setRoom({ dispatch, getState }, next, action) {
49
+    const state = getState();
50
+    let audioMuted;
51
+    let videoMuted;
52
+
53
+    if (action.room) {
54
+        // The Jitsi Meet client may override the Jitsi Meet deployment on the
55
+        // subject of startWithAudioMuted and/or startWithVideoMuted in the
56
+        // (location) URL.
57
+        const urlParams
58
+            = parseURLParams(state['features/base/connection'].locationURL);
59
+
60
+        audioMuted = urlParams['config.startWithAudioMuted'];
61
+        videoMuted = urlParams['config.startWithVideoMuted'];
62
+    }
63
+
64
+    // Of course, the Jitsi Meet deployment may define startWithAudioMuted
65
+    // and/or startWithVideoMuted through config.js which should be respected if
66
+    // the client did not override it.
67
+    const config = state['features/base/config'];
68
+
69
+    typeof audioMuted === 'undefined'
70
+        && (audioMuted = config.startWithAudioMuted);
71
+    typeof videoMuted === 'undefined'
72
+        && (videoMuted = config.startWithVideoMuted);
73
+
74
+    // Apply startWithAudioMuted and startWithVideoMuted.
75
+    const { audio, video } = state['features/base/media'];
47 76
 
48
-    state.audio.muted && dispatch(setAudioMuted(false));
49
-    (state.video.facingMode !== CAMERA_FACING_MODE.USER)
77
+    audioMuted = Boolean(audioMuted);
78
+    videoMuted = Boolean(videoMuted);
79
+
80
+    (audio.muted !== audioMuted) && dispatch(setAudioMuted(audioMuted));
81
+    (video.facingMode !== CAMERA_FACING_MODE.USER)
50 82
         && dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
51
-    state.video.muted && dispatch(setVideoMuted(false));
83
+    (video.muted !== videoMuted) && dispatch(setVideoMuted(videoMuted));
84
+
85
+    return next(action);
52 86
 }
53 87
 
54 88
 /**
55 89
  * Syncs muted state of local media track with muted state from media state.
56 90
  *
57
- * @param {Store} store - Redux store.
58
- * @param {Track} track - Local media track.
91
+ * @param {Store} store - The redux store.
92
+ * @param {Track} track - The local media track.
59 93
  * @private
60 94
  * @returns {void}
61 95
  */
@@ -66,16 +100,18 @@ function _syncTrackMutedState(store, track) {
66 100
     // XXX If muted state of track when it was added is different from our media
67 101
     // muted state, we need to mute track and explicitly modify 'muted' property
68 102
     // on track. This is because though TRACK_ADDED action was dispatched it's
69
-    // not yet in Redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
103
+    // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
70 104
     // fired before track gets to state.
71 105
     if (track.muted !== muted) {
72 106
         track.muted = muted;
73 107
         setTrackMuted(track.jitsiTrack, muted)
74 108
             .catch(error => {
75 109
                 console.error(`setTrackMuted(${muted}) failed`, error);
110
+
76 111
                 const setMuted
77 112
                     = track.mediaType === MEDIA_TYPE.AUDIO
78
-                        ? setAudioMuted : setVideoMuted;
113
+                        ? setAudioMuted
114
+                        : setVideoMuted;
79 115
 
80 116
                 // Failed to sync muted state - dispatch rollback action
81 117
                 store.dispatch(setMuted(!muted));

Loading…
Cancel
Save