Browse Source

address code review

tags/v0.0.2
Andrei Gavrilescu 5 years ago
parent
commit
279dd99f99

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

207
         throw new Error('Not implemented by subclass');
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
      * Returns the WebRTC MediaStream instance.
220
      * Returns the WebRTC MediaStream instance.
212
      */
221
      */

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

4
 // potential abnormalities and for a better use experience i.e. don't generate event the instant
4
 // potential abnormalities and for a better use experience i.e. don't generate event the instant
5
 // an audio track is added to the tcr.
5
 // an audio track is added to the tcr.
6
 // Potential improvement - add this as a configurable parameter.
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
  * Detect if there is no audio input on the current TraceAblePeerConnection selected track. The no audio
10
  * Detect if there is no audio input on the current TraceAblePeerConnection selected track. The no audio
19
     constructor(conference, callback) {
19
     constructor(conference, callback) {
20
         this._conference = conference;
20
         this._conference = conference;
21
         this._callback = callback;
21
         this._callback = callback;
22
-        this._firstSilentSignalDate = null;
22
+        this._timeoutTrigger = null;
23
 
23
 
24
         conference.statistics.addAudioLevelListener(this._audioLevel.bind(this));
24
         conference.statistics.addAudioLevelListener(this._audioLevel.bind(this));
25
         conference.on(JitsiConferenceEvents.TRACK_ADDED, this._trackAdded.bind(this));
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
      * Receives audio level events for all send and receive streams on the current TraceablePeerConnection.
38
      * Receives audio level events for all send and receive streams on the current TraceablePeerConnection.
88
             return;
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
      * Notifies NoAudioSignalDetection that a JitsiTrack was added to the associated JitsiConference.
79
      * Notifies NoAudioSignalDetection that a JitsiTrack was added to the associated JitsiConference.
112
      * Only take into account local audio tracks.
80
      * Only take into account local audio tracks.
114
      * @param {JitsiTrack} track - The added JitsiTrack.
82
      * @param {JitsiTrack} track - The added JitsiTrack.
115
      */
83
      */
116
     _trackAdded(track) {
84
     _trackAdded(track) {
117
-        if (this._isLocalAudioTrack(track)) {
85
+        if (track.isLocalAudioTrack()) {
118
             // Reset state for the new track.
86
             // Reset state for the new track.
119
-            this._firstSilentSignalDate = null;
120
             this._audioTrack = track;
87
             this._audioTrack = track;
121
             this._eventFired = false;
88
             this._eventFired = false;
89
+            this._clearTriggerTimeout();
122
         }
90
         }
123
     }
91
     }
124
 }
92
 }

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

68
      *
68
      *
69
      * @param {string} micDeviceId - Target microphone device id.
69
      * @param {string} micDeviceId - Target microphone device id.
70
      * @param {number} procNodeSampleRate - Sample rate of the proc node.
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
      * @returns {Promise<TrackVADEmitter>} - Promise resolving in a new instance of TrackVADEmitter.
76
      * @returns {Promise<TrackVADEmitter>} - Promise resolving in a new instance of TrackVADEmitter.
72
      */
77
      */
73
     static create(micDeviceId, procNodeSampleRate, vadProcessor) {
78
     static create(micDeviceId, procNodeSampleRate, vadProcessor) {

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

79
          */
79
          */
80
         this._vadInitTracker = Promise.resolve();
80
         this._vadInitTracker = Promise.resolve();
81
 
81
 
82
+
83
+        this._processVADScore = this._processVADScore.bind(this);
84
+
82
         /**
85
         /**
83
          * {@link JitsiConference} bindings.
86
          * {@link JitsiConference} bindings.
84
          */
87
          */
116
      * @returns {void}
119
      * @returns {void}
117
      */
120
      */
118
     _startVADEmitter() {
121
     _startVADEmitter() {
119
-        this._vadEmitter.on(VAD_SCORE_PUBLISHED, this._processVADScore.bind(this));
122
+        this._vadEmitter.on(VAD_SCORE_PUBLISHED, this._processVADScore);
120
         this._vadEmitter.start();
123
         this._vadEmitter.start();
121
     }
124
     }
122
 
125
 
125
      * @returns {void}
128
      * @returns {void}
126
      */
129
      */
127
     _stopVADEmitter() {
130
     _stopVADEmitter() {
128
-        this._vadEmitter.removeAllListeners(VAD_SCORE_PUBLISHED);
131
+        this._vadEmitter.removeListener(VAD_SCORE_PUBLISHED, this._processVADScore);
129
         this._vadEmitter.stop();
132
         this._vadEmitter.stop();
130
     }
133
     }
131
 
134
 
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
      * Reset the processing context, clear buffer, cancel the timeout trigger.
205
      * Reset the processing context, clear buffer, cancel the timeout trigger.
213
      *
206
      *
227
      * @listens TRACK_ADDED
220
      * @listens TRACK_ADDED
228
      */
221
      */
229
     _trackAdded(track) {
222
     _trackAdded(track) {
230
-        if (this._isLocalAudioTrack(track)) {
223
+        if (track.isLocalAudioTrack()) {
231
             // Keep a track promise so we take into account successive TRACK_ADD events being generated so that we
224
             // Keep a track promise so we take into account successive TRACK_ADD events being generated so that we
232
             // destroy/create the processing context in the proper order.
225
             // destroy/create the processing context in the proper order.
233
             this._vadInitTracker
226
             this._vadInitTracker
256
      * @listens TRACK_MUTE_CHANGED
249
      * @listens TRACK_MUTE_CHANGED
257
      */
250
      */
258
     _trackMuteChanged(track) {
251
     _trackMuteChanged(track) {
259
-        if (this._isLocalAudioTrack(track)) {
252
+        if (track.isLocalAudioTrack()) {
260
             // On a mute toggle reset the state.
253
             // On a mute toggle reset the state.
261
             this._vadInitTracker.then(() => {
254
             this._vadInitTracker.then(() => {
262
 
255
 
281
      * @listens TRACK_REMOVED
274
      * @listens TRACK_REMOVED
282
      */
275
      */
283
     _trackRemoved(track) {
276
     _trackRemoved(track) {
284
-        if (this._isLocalAudioTrack(track)) {
277
+        if (track.isLocalAudioTrack()) {
285
             // Use the promise to make sure operations are in sequence.
278
             // Use the promise to make sure operations are in sequence.
286
             this._vadInitTracker.then(() => {
279
             this._vadInitTracker.then(() => {
287
                 logger.debug('Removing track from VAD detection - ', track.getTrackLabel());
280
                 logger.debug('Removing track from VAD detection - ', track.getTrackLabel());

Loading…
Cancel
Save