|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+/* global __filename, module, require */
|
|
|
2
|
+var logger = require('jitsi-meet-logger').getLogger(__filename);
|
|
|
3
|
+var MediaType = require('../../service/RTC/MediaType');
|
|
|
4
|
+var RTCBrowserType = require('../RTC/RTCBrowserType');
|
|
|
5
|
+var RTCEvents = require('../../service/RTC/RTCEvents');
|
|
|
6
|
+
|
|
|
7
|
+import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
|
|
|
8
|
+import * as JitsiTrackEvents from '../../JitsiTrackEvents';
|
|
|
9
|
+
|
|
|
10
|
+/**
|
|
|
11
|
+ * How long we're going to wait after the RTC video track muted event for
|
|
|
12
|
+ * the corresponding signalling mute event, before the connection interrupted
|
|
|
13
|
+ * is fired.
|
|
|
14
|
+ *
|
|
|
15
|
+ * @type {number} amount of time in milliseconds
|
|
|
16
|
+ */
|
|
|
17
|
+const RTC_MUTE_TIMEOUT = 1000;
|
|
|
18
|
+
|
|
|
19
|
+/**
|
|
|
20
|
+ * Class is responsible for emitting
|
|
|
21
|
+ * JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED events.
|
|
|
22
|
+ *
|
|
|
23
|
+ * @constructor
|
|
|
24
|
+ * @param rtc {RTC} the RTC service instance
|
|
|
25
|
+ * @param conference {JitsiConference} parent conference instance
|
|
|
26
|
+ */
|
|
|
27
|
+function ParticipantConnectionStatus(rtc, conference) {
|
|
|
28
|
+ this.rtc = rtc;
|
|
|
29
|
+ this.conference = conference;
|
|
|
30
|
+ /**
|
|
|
31
|
+ * A map of the "endpoint ID"(which corresponds to the resource part of MUC
|
|
|
32
|
+ * JID(nickname)) to the timeout callback IDs scheduled using
|
|
|
33
|
+ * window.setTimeout.
|
|
|
34
|
+ * @type {Object.<string, number>}
|
|
|
35
|
+ */
|
|
|
36
|
+ this.trackTimers = {};
|
|
|
37
|
+}
|
|
|
38
|
+
|
|
|
39
|
+/**
|
|
|
40
|
+ * Initializes <tt>ParticipantConnectionStatus</tt> and bind required event
|
|
|
41
|
+ * listeners.
|
|
|
42
|
+ */
|
|
|
43
|
+ParticipantConnectionStatus.prototype.init = function() {
|
|
|
44
|
+
|
|
|
45
|
+ this._onEndpointConnStatusChanged
|
|
|
46
|
+ = this.onEndpointConnStatusChanged.bind(this);
|
|
|
47
|
+
|
|
|
48
|
+ this.rtc.addListener(
|
|
|
49
|
+ RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
|
|
|
50
|
+ this._onEndpointConnStatusChanged);
|
|
|
51
|
+
|
|
|
52
|
+ // On some browsers MediaStreamTrack trigger "onmute"/"onunmute"
|
|
|
53
|
+ // events for video type tracks when they stop receiving data which is
|
|
|
54
|
+ // often a sign that remote user is having connectivity issues
|
|
|
55
|
+ if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
|
|
|
56
|
+
|
|
|
57
|
+ this._onTrackRtcMuted = this.onTrackRtcMuted.bind(this);
|
|
|
58
|
+ this.rtc.addListener(
|
|
|
59
|
+ RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
|
|
|
60
|
+
|
|
|
61
|
+ this._onTrackRtcUnmuted = this.onTrackRtcUnmuted.bind(this);
|
|
|
62
|
+ this.rtc.addListener(
|
|
|
63
|
+ RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
|
|
|
64
|
+
|
|
|
65
|
+ // Track added/removed listeners are used to bind "mute"/"unmute"
|
|
|
66
|
+ // event handlers
|
|
|
67
|
+ this._onRemoteTrackAdded = this.onRemoteTrackAdded.bind(this);
|
|
|
68
|
+ this.conference.on(
|
|
|
69
|
+ JitsiConferenceEvents.TRACK_ADDED, this._onRemoteTrackAdded);
|
|
|
70
|
+ this._onRemoteTrackRemoved = this.onRemoteTrackRemoved.bind(this);
|
|
|
71
|
+ this.conference.on(
|
|
|
72
|
+ JitsiConferenceEvents.TRACK_REMOVED, this._onRemoteTrackRemoved);
|
|
|
73
|
+
|
|
|
74
|
+ // Listened which will be bound to JitsiRemoteTrack to listen for
|
|
|
75
|
+ // signalling mute/unmute events.
|
|
|
76
|
+ this._onSignallingMuteChanged = this.onSignallingMuteChanged.bind(this);
|
|
|
77
|
+ }
|
|
|
78
|
+};
|
|
|
79
|
+
|
|
|
80
|
+/**
|
|
|
81
|
+ * Removes all event listeners and disposes of all resources held by this
|
|
|
82
|
+ * instance.
|
|
|
83
|
+ */
|
|
|
84
|
+ParticipantConnectionStatus.prototype.dispose = function () {
|
|
|
85
|
+
|
|
|
86
|
+ this.rtc.removeListener(
|
|
|
87
|
+ RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
|
|
|
88
|
+ this._onEndpointConnStatusChanged);
|
|
|
89
|
+
|
|
|
90
|
+ if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
|
|
|
91
|
+ this.rtc.removeListener(
|
|
|
92
|
+ RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
|
|
|
93
|
+ this.rtc.removeListener(
|
|
|
94
|
+ RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
|
|
|
95
|
+ this.conference.off(
|
|
|
96
|
+ JitsiConferenceEvents.TRACK_ADDED, this._onRemoteTrackAdded);
|
|
|
97
|
+ this.conference.off(
|
|
|
98
|
+ JitsiConferenceEvents.TRACK_REMOVED, this._onRemoteTrackRemoved);
|
|
|
99
|
+ }
|
|
|
100
|
+
|
|
|
101
|
+ Object.keys(this.trackTimers).forEach(function (participantId) {
|
|
|
102
|
+ this.clearTimeout(participantId);
|
|
|
103
|
+ }.bind(this));
|
|
|
104
|
+};
|
|
|
105
|
+
|
|
|
106
|
+/**
|
|
|
107
|
+ * Checks whether given <tt>JitsiParticipant</tt> has any muted video
|
|
|
108
|
+ * <tt>MediaStreamTrack</tt>s.
|
|
|
109
|
+ *
|
|
|
110
|
+ * @param {JitsiParticipant} participant to be checked for muted video tracks
|
|
|
111
|
+ *
|
|
|
112
|
+ * @return {boolean} <tt>true</tt> if given <tt>participant</tt> contains any
|
|
|
113
|
+ * video <tt>MediaStreamTrack</tt>s muted according to their 'muted' field.
|
|
|
114
|
+ */
|
|
|
115
|
+var hasRtcMutedVideoTrack = function (participant) {
|
|
|
116
|
+ return participant.getTracks().some(function(jitsiTrack) {
|
|
|
117
|
+ var rtcTrack = jitsiTrack.getTrack();
|
|
|
118
|
+ return jitsiTrack.getType() === MediaType.VIDEO
|
|
|
119
|
+ && rtcTrack && rtcTrack.muted === true;
|
|
|
120
|
+ });
|
|
|
121
|
+};
|
|
|
122
|
+
|
|
|
123
|
+/**
|
|
|
124
|
+ * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
|
|
|
125
|
+ * notification over the data channel from the bridge about endpoint's
|
|
|
126
|
+ * connection status update.
|
|
|
127
|
+ * @param endpointId {string} the endpoint ID(MUC nickname/resource JID)
|
|
|
128
|
+ * @param isActive {boolean} true if the connection is OK or false otherwise
|
|
|
129
|
+ */
|
|
|
130
|
+ParticipantConnectionStatus.prototype.onEndpointConnStatusChanged
|
|
|
131
|
+= function(endpointId, isActive) {
|
|
|
132
|
+
|
|
|
133
|
+ logger.debug(
|
|
|
134
|
+ 'Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED('
|
|
|
135
|
+ + Date.now() +'): ' + endpointId + ': ' + isActive);
|
|
|
136
|
+
|
|
|
137
|
+ // Filter out events for the local JID for now
|
|
|
138
|
+ if (endpointId !== this.conference.myUserId()) {
|
|
|
139
|
+ var participant = this.conference.getParticipantById(endpointId);
|
|
|
140
|
+ // Delay the 'active' event until the video track gets RTC unmuted event
|
|
|
141
|
+ if (isActive
|
|
|
142
|
+ && RTCBrowserType.isVideoMuteOnConnInterruptedSupported()
|
|
|
143
|
+ && participant
|
|
|
144
|
+ && hasRtcMutedVideoTrack(participant)
|
|
|
145
|
+ && !participant.isVideoMuted()) {
|
|
|
146
|
+ logger.debug(
|
|
|
147
|
+ 'Ignoring RTCEvents.ENDPOINT_CONN_STATUS_CHANGED -'
|
|
|
148
|
+ + ' will wait for unmute event');
|
|
|
149
|
+ } else {
|
|
|
150
|
+ this._changeConnectionStatus(endpointId, isActive);
|
|
|
151
|
+ }
|
|
|
152
|
+ }
|
|
|
153
|
+};
|
|
|
154
|
+
|
|
|
155
|
+ParticipantConnectionStatus.prototype._changeConnectionStatus
|
|
|
156
|
+= function (endpointId, newStatus) {
|
|
|
157
|
+ var participant = this.conference.getParticipantById(endpointId);
|
|
|
158
|
+ if (!participant) {
|
|
|
159
|
+ // This will happen when participant exits the conference with broken
|
|
|
160
|
+ // ICE connection and we join after that. The bridge keeps sending
|
|
|
161
|
+ // that notification until the conference does not expire.
|
|
|
162
|
+ logger.warn(
|
|
|
163
|
+ 'Missed participant connection status update - ' +
|
|
|
164
|
+ 'no participant for endpoint: ' + endpointId);
|
|
|
165
|
+ return;
|
|
|
166
|
+ }
|
|
|
167
|
+ if (participant.isConnectionActive() !== newStatus) {
|
|
|
168
|
+ participant._setIsConnectionActive(newStatus);
|
|
|
169
|
+ logger.debug(
|
|
|
170
|
+ 'Emit endpoint conn status(' + Date.now() + '): ',
|
|
|
171
|
+ endpointId, newStatus);
|
|
|
172
|
+ this.conference.eventEmitter.emit(
|
|
|
173
|
+ JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
|
|
|
174
|
+ endpointId, newStatus);
|
|
|
175
|
+ }
|
|
|
176
|
+};
|
|
|
177
|
+
|
|
|
178
|
+/**
|
|
|
179
|
+ * Reset the postponed "connection interrupted" event which was previously
|
|
|
180
|
+ * scheduled as a timeout on RTC 'onmute' event.
|
|
|
181
|
+ *
|
|
|
182
|
+ * @param participantId the participant for which the "connection interrupted"
|
|
|
183
|
+ * timeout was scheduled
|
|
|
184
|
+ */
|
|
|
185
|
+ParticipantConnectionStatus.prototype.clearTimeout = function (participantId) {
|
|
|
186
|
+ if (this.trackTimers[participantId]) {
|
|
|
187
|
+ window.clearTimeout(this.trackTimers[participantId]);
|
|
|
188
|
+ this.trackTimers[participantId] = null;
|
|
|
189
|
+ }
|
|
|
190
|
+};
|
|
|
191
|
+
|
|
|
192
|
+/**
|
|
|
193
|
+ * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
|
|
|
194
|
+ * a new one is added to the conference.
|
|
|
195
|
+ *
|
|
|
196
|
+ * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
|
|
|
197
|
+ * the conference.
|
|
|
198
|
+ */
|
|
|
199
|
+ParticipantConnectionStatus.prototype.onRemoteTrackAdded
|
|
|
200
|
+= function(remoteTrack) {
|
|
|
201
|
+ if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
|
|
|
202
|
+
|
|
|
203
|
+ logger.debug(
|
|
|
204
|
+ 'Detector on remote track added: ', remoteTrack.getParticipantId());
|
|
|
205
|
+
|
|
|
206
|
+ remoteTrack.on(
|
|
|
207
|
+ JitsiTrackEvents.TRACK_MUTE_CHANGED,
|
|
|
208
|
+ this._onSignallingMuteChanged);
|
|
|
209
|
+ }
|
|
|
210
|
+};
|
|
|
211
|
+
|
|
|
212
|
+/**
|
|
|
213
|
+ * Removes all event listeners bound to the remote video track and clears any
|
|
|
214
|
+ * related timeouts.
|
|
|
215
|
+ *
|
|
|
216
|
+ * @param {JitsiRemoteTrack} remoteTrack the remote track which is being removed
|
|
|
217
|
+ * from the conference.
|
|
|
218
|
+ */
|
|
|
219
|
+ParticipantConnectionStatus.prototype.onRemoteTrackRemoved
|
|
|
220
|
+= function(remoteTrack) {
|
|
|
221
|
+ if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
|
|
|
222
|
+ logger.debug(
|
|
|
223
|
+ 'Detector on remote track removed: ',
|
|
|
224
|
+ remoteTrack.getParticipantId());
|
|
|
225
|
+ remoteTrack.off(
|
|
|
226
|
+ JitsiTrackEvents.TRACK_MUTE_CHANGED,
|
|
|
227
|
+ this._onSignallingMuteChanged);
|
|
|
228
|
+ this.clearTimeout(remoteTrack.getParticipantId());
|
|
|
229
|
+ }
|
|
|
230
|
+};
|
|
|
231
|
+
|
|
|
232
|
+/**
|
|
|
233
|
+ * Handles RTC 'onmute' event for the video track.
|
|
|
234
|
+ *
|
|
|
235
|
+ * @param track {JitsiRemoteTrack} the video track for which 'onmute' event will
|
|
|
236
|
+ * be processed.
|
|
|
237
|
+ */
|
|
|
238
|
+ParticipantConnectionStatus.prototype.onTrackRtcMuted = function(track) {
|
|
|
239
|
+ var participantId = track.getParticipantId();
|
|
|
240
|
+ var participant = this.conference.getParticipantById(participantId);
|
|
|
241
|
+ logger.debug('Detector track RTC muted: ', participantId);
|
|
|
242
|
+ if (!participant) {
|
|
|
243
|
+ logger.error('No participant for id: ' + participantId);
|
|
|
244
|
+ return;
|
|
|
245
|
+ }
|
|
|
246
|
+ if (!participant.isVideoMuted()) {
|
|
|
247
|
+ // If the user is not muted according to the signalling we'll give it
|
|
|
248
|
+ // some time, before the connection interrupted event is triggered.
|
|
|
249
|
+ this.trackTimers[participantId] = window.setTimeout(function () {
|
|
|
250
|
+ if (!track.isMuted() && participant.isConnectionActive()) {
|
|
|
251
|
+ logger.info(
|
|
|
252
|
+ 'Connection interrupted through the RTC mute: '
|
|
|
253
|
+ + participantId, Date.now());
|
|
|
254
|
+ this._changeConnectionStatus(participantId, false);
|
|
|
255
|
+ }
|
|
|
256
|
+ this.clearTimeout(participantId);
|
|
|
257
|
+ }.bind(this), RTC_MUTE_TIMEOUT);
|
|
|
258
|
+ }
|
|
|
259
|
+};
|
|
|
260
|
+
|
|
|
261
|
+/**
|
|
|
262
|
+ * Handles RTC 'onunmute' event for the video track.
|
|
|
263
|
+ *
|
|
|
264
|
+ * @param track {JitsiRemoteTrack} the video track for which 'onunmute' event
|
|
|
265
|
+ * will be processed.
|
|
|
266
|
+ */
|
|
|
267
|
+ParticipantConnectionStatus.prototype.onTrackRtcUnmuted = function(track) {
|
|
|
268
|
+ logger.debug('Detector track RTC unmuted: ', track);
|
|
|
269
|
+ var participantId = track.getParticipantId();
|
|
|
270
|
+ if (!track.isMuted() &&
|
|
|
271
|
+ !this.conference.getParticipantById(participantId)
|
|
|
272
|
+ .isConnectionActive()) {
|
|
|
273
|
+ logger.info(
|
|
|
274
|
+ 'Detector connection restored through the RTC unmute: '
|
|
|
275
|
+ + participantId, Date.now());
|
|
|
276
|
+ this._changeConnectionStatus(participantId, true);
|
|
|
277
|
+ }
|
|
|
278
|
+ this.clearTimeout(participantId);
|
|
|
279
|
+};
|
|
|
280
|
+
|
|
|
281
|
+/**
|
|
|
282
|
+ * Here the signalling "mute"/"unmute" events are processed.
|
|
|
283
|
+ *
|
|
|
284
|
+ * @param track {JitsiRemoteTrack} the remote video track for which
|
|
|
285
|
+ * the signalling mute/unmute event will be processed.
|
|
|
286
|
+ */
|
|
|
287
|
+ParticipantConnectionStatus.prototype.onSignallingMuteChanged
|
|
|
288
|
+= function (track) {
|
|
|
289
|
+ logger.debug(
|
|
|
290
|
+ 'Detector on track signalling mute changed: ', track, track.isMuted());
|
|
|
291
|
+ var isMuted = track.isMuted();
|
|
|
292
|
+ var participantId = track.getParticipantId();
|
|
|
293
|
+ var participant = this.conference.getParticipantById(participantId);
|
|
|
294
|
+ if (!participant) {
|
|
|
295
|
+ logger.error('No participant for id: ' + participantId);
|
|
|
296
|
+ return;
|
|
|
297
|
+ }
|
|
|
298
|
+ var isConnectionActive = participant.isConnectionActive();
|
|
|
299
|
+ if (isMuted && isConnectionActive && this.trackTimers[participantId]) {
|
|
|
300
|
+ logger.debug(
|
|
|
301
|
+ 'Signalling got in sync - cancelling task for: ' + participantId);
|
|
|
302
|
+ this.clearTimeout(participantId);
|
|
|
303
|
+ }
|
|
|
304
|
+};
|
|
|
305
|
+
|
|
|
306
|
+module.exports = ParticipantConnectionStatus;
|