|
@@ -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
|
}
|