Sfoglia il codice sorgente

address code review

tags/v0.0.2
Andrei Gavrilescu 5 anni fa
parent
commit
279dd99f99

+ 9
- 0
modules/RTC/JitsiTrack.js Vedi File

@@ -207,6 +207,15 @@ export default class JitsiTrack extends EventEmitter {
207 207
         throw new Error('Not implemented by subclass');
208 208
     }
209 209
 
210
+    /**
211
+     * Check whether this is a local audio track.
212
+     *
213
+     * @return {boolean} -  true if track represents a local audio track, false otherwise.
214
+     */
215
+    isLocalAudioTrack() {
216
+        return this.isAudioTrack() && this.isLocal();
217
+    }
218
+
210 219
     /**
211 220
      * Returns the WebRTC MediaStream instance.
212 221
      */

+ 15
- 47
modules/detection/NoAudioSignalDetection.js Vedi File

@@ -4,7 +4,7 @@ import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
4 4
 // potential abnormalities and for a better use experience i.e. don't generate event the instant
5 5
 // an audio track is added to the tcr.
6 6
 // Potential improvement - add this as a configurable parameter.
7
-const SILENCE_PERIOD_SEC = 4;
7
+const SILENCE_PERIOD_MS = 4000;
8 8
 
9 9
 /**
10 10
  * Detect if there is no audio input on the current TraceAblePeerConnection selected track. The no audio
@@ -19,43 +19,20 @@ export default class NoAudioSignalDetection {
19 19
     constructor(conference, callback) {
20 20
         this._conference = conference;
21 21
         this._callback = callback;
22
-        this._firstSilentSignalDate = null;
22
+        this._timeoutTrigger = null;
23 23
 
24 24
         conference.statistics.addAudioLevelListener(this._audioLevel.bind(this));
25 25
         conference.on(JitsiConferenceEvents.TRACK_ADDED, this._trackAdded.bind(this));
26 26
     }
27 27
 
28 28
     /**
29
-     * Checks if the configured period in which no audio was received has elapsed.
30
-     *
31
-     * @returns {boolean}
29
+     * Clear the timeout state.
32 30
      */
33
-    _hasSilencePeriodElapsed() {
34
-        const currentDate = new Date();
35
-        const elapsedSec = (currentDate.getTime() - this._firstSilentSignalDate.getTime()) / 1000;
36
-
37
-        if (elapsedSec > SILENCE_PERIOD_SEC) {
38
-            return true;
39
-        }
40
-
41
-        return false;
31
+    _clearTriggerTimeout() {
32
+        clearTimeout(this._timeoutTrigger);
33
+        this._timeoutTrigger = null;
42 34
     }
43 35
 
44
-    /**
45
-     * Trigger the set callback for no audio input if expected conditions are met.
46
-     */
47
-    _triggerNoAudioCallback() {
48
-        // In case this is the first time 0 audio level was detected initialize the interval check start
49
-        // date
50
-        if (!this._firstSilentSignalDate) {
51
-            this._firstSilentSignalDate = new Date();
52
-
53
-        // If the configured interval has elapsed trigger the callback
54
-        } else if (this._hasSilencePeriodElapsed()) {
55
-            this._eventFired = true;
56
-            this._callback();
57
-        }
58
-    }
59 36
 
60 37
     /**
61 38
      * Receives audio level events for all send and receive streams on the current TraceablePeerConnection.
@@ -88,25 +65,16 @@ export default class NoAudioSignalDetection {
88 65
             return;
89 66
         }
90 67
 
91
-        if (audioLevel === 0) {
92
-            this._triggerNoAudioCallback();
93
-        } else {
94
-            // Reset the period start date in order to check for consistent silence over the configured
95
-            // time interval.
96
-            this._firstSilentSignalDate = null;
68
+        if (audioLevel === 0 && !this._timeoutTrigger) {
69
+            this._timeoutTrigger = setTimeout(() => {
70
+                this._eventFired = true;
71
+                this._callback();
72
+            }, SILENCE_PERIOD_MS);
73
+        } else if (audioLevel !== 0 && this._timeoutTrigger) {
74
+            this._clearTriggerTimeout();
97 75
         }
98 76
     }
99 77
 
100
-    /**
101
-     * Determines if a specific JitsiTrack is a local audio track.
102
-     *
103
-     * @param {JitsiTrack} track - The JitsiTrack to be checked whether it represents a local audio track.
104
-     * @return {boolean} -  true if track represents a local audio track, false otherwise.
105
-     */
106
-    _isLocalAudioTrack(track) {
107
-        return track.isAudioTrack() && track.isLocal();
108
-    }
109
-
110 78
     /**
111 79
      * Notifies NoAudioSignalDetection that a JitsiTrack was added to the associated JitsiConference.
112 80
      * Only take into account local audio tracks.
@@ -114,11 +82,11 @@ export default class NoAudioSignalDetection {
114 82
      * @param {JitsiTrack} track - The added JitsiTrack.
115 83
      */
116 84
     _trackAdded(track) {
117
-        if (this._isLocalAudioTrack(track)) {
85
+        if (track.isLocalAudioTrack()) {
118 86
             // Reset state for the new track.
119
-            this._firstSilentSignalDate = null;
120 87
             this._audioTrack = track;
121 88
             this._eventFired = false;
89
+            this._clearTriggerTimeout();
122 90
         }
123 91
     }
124 92
 }

+ 5
- 0
modules/detection/TrackVADEmitter.js Vedi File

@@ -68,6 +68,11 @@ export default class TrackVADEmitter extends EventEmitter {
68 68
      *
69 69
      * @param {string} micDeviceId - Target microphone device id.
70 70
      * @param {number} procNodeSampleRate - Sample rate of the proc node.
71
+     * @param {Object} vadProcessor -Module that calculates the voice activity score for a certain audio PCM sample.
72
+     * The processor needs to implement the following functions:
73
+     * - <tt>getSampleLength()</tt> - Returns the sample size accepted by getSampleLength.
74
+     * - <tt>getRequiredPCMFrequency()</tt> - Returns the PCM frequency at which the processor operates.
75
+     * - <tt>calculateAudioFrameVAD(pcmSample)</tt> - Process a 32 float pcm sample of getSampleLength size.
71 76
      * @returns {Promise<TrackVADEmitter>} - Promise resolving in a new instance of TrackVADEmitter.
72 77
      */
73 78
     static create(micDeviceId, procNodeSampleRate, vadProcessor) {

+ 8
- 15
modules/detection/VADTalkMutedDetection.js Vedi File

@@ -79,6 +79,9 @@ export default class VADTalkMutedDetection extends EventEmitter {
79 79
          */
80 80
         this._vadInitTracker = Promise.resolve();
81 81
 
82
+
83
+        this._processVADScore = this._processVADScore.bind(this);
84
+
82 85
         /**
83 86
          * {@link JitsiConference} bindings.
84 87
          */
@@ -116,7 +119,7 @@ export default class VADTalkMutedDetection extends EventEmitter {
116 119
      * @returns {void}
117 120
      */
118 121
     _startVADEmitter() {
119
-        this._vadEmitter.on(VAD_SCORE_PUBLISHED, this._processVADScore.bind(this));
122
+        this._vadEmitter.on(VAD_SCORE_PUBLISHED, this._processVADScore);
120 123
         this._vadEmitter.start();
121 124
     }
122 125
 
@@ -125,7 +128,7 @@ export default class VADTalkMutedDetection extends EventEmitter {
125 128
      * @returns {void}
126 129
      */
127 130
     _stopVADEmitter() {
128
-        this._vadEmitter.removeAllListeners(VAD_SCORE_PUBLISHED);
131
+        this._vadEmitter.removeListener(VAD_SCORE_PUBLISHED, this._processVADScore);
129 132
         this._vadEmitter.stop();
130 133
     }
131 134
 
@@ -198,16 +201,6 @@ export default class VADTalkMutedDetection extends EventEmitter {
198 201
         }
199 202
     }
200 203
 
201
-    /**
202
-     * Determines whether a specific {@link JitsiTrack} represents a local audio track.
203
-     *
204
-     * @param {JitsiTrack} track - The track to be checked whether it represents a local audio track.
205
-     * @return {boolean} - true if the specified track represents a local audio track; otherwise, false.
206
-     */
207
-    _isLocalAudioTrack(track) {
208
-        return track.isAudioTrack() && track.isLocal();
209
-    }
210
-
211 204
     /**
212 205
      * Reset the processing context, clear buffer, cancel the timeout trigger.
213 206
      *
@@ -227,7 +220,7 @@ export default class VADTalkMutedDetection extends EventEmitter {
227 220
      * @listens TRACK_ADDED
228 221
      */
229 222
     _trackAdded(track) {
230
-        if (this._isLocalAudioTrack(track)) {
223
+        if (track.isLocalAudioTrack()) {
231 224
             // Keep a track promise so we take into account successive TRACK_ADD events being generated so that we
232 225
             // destroy/create the processing context in the proper order.
233 226
             this._vadInitTracker
@@ -256,7 +249,7 @@ export default class VADTalkMutedDetection extends EventEmitter {
256 249
      * @listens TRACK_MUTE_CHANGED
257 250
      */
258 251
     _trackMuteChanged(track) {
259
-        if (this._isLocalAudioTrack(track)) {
252
+        if (track.isLocalAudioTrack()) {
260 253
             // On a mute toggle reset the state.
261 254
             this._vadInitTracker.then(() => {
262 255
 
@@ -281,7 +274,7 @@ export default class VADTalkMutedDetection extends EventEmitter {
281 274
      * @listens TRACK_REMOVED
282 275
      */
283 276
     _trackRemoved(track) {
284
-        if (this._isLocalAudioTrack(track)) {
277
+        if (track.isLocalAudioTrack()) {
285 278
             // Use the promise to make sure operations are in sequence.
286 279
             this._vadInitTracker.then(() => {
287 280
                 logger.debug('Removing track from VAD detection - ', track.getTrackLabel());

Loading…
Annulla
Salva