Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ParticipantAnalyzer.js « analyzers « webrtc « utils « src - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 390aadd978ecfc18f8f89c91e0ddde01c8b7ecb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
 *
 * @copyright Copyright (c) 2020, Daniel Calviño Sánchez (danxuliu@gmail.com)
 *
 * @license AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import EmitterMixin from '../../EmitterMixin'

import {
	PEER_DIRECTION,
	PeerConnectionAnalyzer,
} from './PeerConnectionAnalyzer'

/**
 * Analyzer for the quality of the connections of a Participant.
 *
 * After a ParticipantAnalyzer is created the participant to analyze must be set
 * using any of the "setXXXParticipant" methods; "setSenderReceiverParticipant"
 * is meant to be used when there is no HPB, while "setSenderParticipant" and
 * "setReceiverParticipant" are meant to be used when there is an HPB for the
 * local and remote participants respectively.
 *
 * When the quality of the connections change different events will be triggered
 * depending on the case:
 * - 'change:senderConnectionQualityAudio'
 * - 'change:senderConnectionQualityVideo'
 * - 'change:senderConnectionQualityScreen'
 * - 'change:receiverConnectionQualityAudio'
 * - 'change:receiverConnectionQualityVideo'
 * - 'change:receiverConnectionQualityScreen'
 *
 * The reported values are based on CONNECTION_QUALITY values of
 * PeerConnectionAnalyzer.
 *
 * Note that the connections will be analyzed only when the corresponding media
 * is enabled so, for example, if a sender participant has muted but still has
 * the video enabled only the video quality will be analyzed until the audio is
 * unmuted again. This is done not only because the connection quality of media
 * is less relevant when the media is disabled, but also because the connection
 * stats provided by the browser and used for the analysis are less reliable in
 * that case.
 *
 * Once the ParticipantAnalyzer is no longer needed "destroy()" must be called
 * to stop the analysis.
 */
function ParticipantAnalyzer() {
	this._superEmitterMixin()

	this._localMediaModel = null
	this._localCallParticipantModel = null
	this._callParticipantModel = null

	this._peer = null
	this._screenPeer = null

	this._senderPeerConnectionAnalyzer = null
	this._receiverPeerConnectionAnalyzer = null
	this._senderScreenPeerConnectionAnalyzer = null
	this._receiverScreenPeerConnectionAnalyzer = null

	this._handlePeerChangeBound = this._handlePeerChange.bind(this)
	this._handleScreenPeerChangeBound = this._handleScreenPeerChange.bind(this)
	this._handleSenderAudioEnabledChangeBound = this._handleSenderAudioEnabledChange.bind(this)
	this._handleSenderVideoEnabledChangeBound = this._handleSenderVideoEnabledChange.bind(this)
	this._handleReceiverAudioAvailableChangeBound = this._handleReceiverAudioAvailableChange.bind(this)
	this._handleReceiverVideoAvailableChangeBound = this._handleReceiverVideoAvailableChange.bind(this)
	this._handleConnectionQualityAudioChangeBound = this._handleConnectionQualityAudioChange.bind(this)
	this._handleConnectionQualityVideoChangeBound = this._handleConnectionQualityVideoChange.bind(this)
	this._handleConnectionQualityScreenChangeBound = this._handleConnectionQualityScreenChange.bind(this)
}
ParticipantAnalyzer.prototype = {

	destroy() {
		if (this._localCallParticipantModel) {
			this._localCallParticipantModel.off('change:peer', this._handlePeerChangeBound)
			this._localCallParticipantModel.off('change:screenPeer', this._handleScreenPeerChangeBound)
		}

		if (this._callParticipantModel) {
			this._callParticipantModel.off('change:peer', this._handlePeerChangeBound)
			this._callParticipantModel.off('change:screenPeer', this._handleScreenPeerChangeBound)
		}

		this._stopListeningToAudioVideoChanges()
		this._stopListeningToScreenChanges()

		this._localMediaModel = null
		this._localCallParticipantModel = null
		this._callParticipantModel = null

		this._peer = null
		this._screenPeer = null

		this._senderPeerConnectionAnalyzer = null
		this._receiverPeerConnectionAnalyzer = null
		this._senderScreenPeerConnectionAnalyzer = null
		this._receiverScreenPeerConnectionAnalyzer = null
	},

	setSenderParticipant(localMediaModel, localCallParticipantModel) {
		this.destroy()

		this._localMediaModel = localMediaModel
		this._localCallParticipantModel = localCallParticipantModel

		if (this._localCallParticipantModel) {
			this._senderPeerConnectionAnalyzer = new PeerConnectionAnalyzer()
			this._senderScreenPeerConnectionAnalyzer = new PeerConnectionAnalyzer()

			this._localCallParticipantModel.on('change:peer', this._handlePeerChangeBound)
			this._handlePeerChange(this._localCallParticipantModel, this._localCallParticipantModel.get('peer'))

			this._localCallParticipantModel.on('change:screenPeer', this._handleScreenPeerChangeBound)
			this._handleScreenPeerChange(this._localCallParticipantModel, this._localCallParticipantModel.get('screenPeer'))
		}
	},

	setReceiverParticipant(callParticipantModel) {
		this.destroy()

		this._callParticipantModel = callParticipantModel

		if (this._callParticipantModel) {
			this._receiverPeerConnectionAnalyzer = new PeerConnectionAnalyzer()
			this._receiverScreenPeerConnectionAnalyzer = new PeerConnectionAnalyzer()

			this._callParticipantModel.on('change:peer', this._handlePeerChangeBound)
			this._handlePeerChange(this._callParticipantModel, this._callParticipantModel.get('peer'))

			this._callParticipantModel.on('change:screenPeer', this._handleScreenPeerChangeBound)
			this._handleScreenPeerChange(this._callParticipantModel, this._callParticipantModel.get('screenPeer'))
		}
	},

	setSenderReceiverParticipant(localMediaModel, callParticipantModel) {
		this.destroy()

		this._localMediaModel = localMediaModel
		this._callParticipantModel = callParticipantModel

		if (this._callParticipantModel) {
			this._senderPeerConnectionAnalyzer = new PeerConnectionAnalyzer()
			this._receiverPeerConnectionAnalyzer = new PeerConnectionAnalyzer()
			this._senderScreenPeerConnectionAnalyzer = new PeerConnectionAnalyzer()
			this._receiverScreenPeerConnectionAnalyzer = new PeerConnectionAnalyzer()

			this._callParticipantModel.on('change:peer', this._handlePeerChangeBound)
			this._handlePeerChange(this._callParticipantModel, this._callParticipantModel.get('peer'))

			this._callParticipantModel.on('change:screenPeer', this._handleScreenPeerChangeBound)
			this._handleScreenPeerChange(this._callParticipantModel, this._callParticipantModel.get('screenPeer'))
		}
	},

	_handlePeerChange(model, peer) {
		if (this._peer) {
			this._stopListeningToAudioVideoChanges()
		}

		this._peer = peer

		if (peer) {
			this._startListeningToAudioVideoChanges()
		}
	},

	_handleScreenPeerChange(model, screenPeer) {
		if (this._screenPeer) {
			this._stopListeningToScreenChanges()
		}

		this._screenPeer = screenPeer

		if (screenPeer) {
			this._startListeningToScreenChanges()
		}
	},

	_startListeningToAudioVideoChanges() {
		if (this._localMediaModel) {
			this._senderPeerConnectionAnalyzer.setPeerConnection(this._peer.pc, PEER_DIRECTION.SENDER)

			this._senderPeerConnectionAnalyzer.on('change:connectionQualityAudio', this._handleConnectionQualityAudioChangeBound)
			this._senderPeerConnectionAnalyzer.on('change:connectionQualityVideo', this._handleConnectionQualityVideoChangeBound)

			this._localMediaModel.on('change:audioEnabled', this._handleSenderAudioEnabledChangeBound)
			this._localMediaModel.on('change:videoEnabled', this._handleSenderVideoEnabledChangeBound)

			this._handleSenderAudioEnabledChange(this._localMediaModel, this._localMediaModel.get('audioEnabled'))
			this._handleSenderVideoEnabledChange(this._localMediaModel, this._localMediaModel.get('videoEnabled'))
		}

		if (this._callParticipantModel) {
			this._receiverPeerConnectionAnalyzer.setPeerConnection(this._peer.pc, PEER_DIRECTION.RECEIVER)

			this._receiverPeerConnectionAnalyzer.on('change:connectionQualityAudio', this._handleConnectionQualityAudioChangeBound)
			this._receiverPeerConnectionAnalyzer.on('change:connectionQualityVideo', this._handleConnectionQualityVideoChangeBound)

			this._callParticipantModel.on('change:audioAvailable', this._handleReceiverAudioAvailableChangeBound)
			this._callParticipantModel.on('change:videoAvailable', this._handleReceiverVideoAvailableChangeBound)

			this._handleReceiverAudioAvailableChange(this._localMediaModel, this._callParticipantModel.get('audioAvailable'))
			this._handleReceiverVideoAvailableChange(this._localMediaModel, this._callParticipantModel.get('videoAvailable'))
		}
	},

	_startListeningToScreenChanges() {
		if (this._localMediaModel) {
			this._senderScreenPeerConnectionAnalyzer.setPeerConnection(this._screenPeer.pc, PEER_DIRECTION.SENDER)

			this._senderScreenPeerConnectionAnalyzer.on('change:connectionQualityVideo', this._handleConnectionQualityScreenChangeBound)

			this._senderScreenPeerConnectionAnalyzer.setAnalysisEnabledAudio(false)
			this._senderScreenPeerConnectionAnalyzer.setAnalysisEnabledVideo(true)
		}

		if (this._callParticipantModel) {
			this._receiverScreenPeerConnectionAnalyzer.setPeerConnection(this._screenPeer.pc, PEER_DIRECTION.RECEIVER)

			this._receiverScreenPeerConnectionAnalyzer.on('change:connectionQualityVideo', this._handleConnectionQualityScreenChangeBound)

			this._receiverScreenPeerConnectionAnalyzer.setAnalysisEnabledAudio(false)
			this._receiverScreenPeerConnectionAnalyzer.setAnalysisEnabledVideo(true)
		}
	},

	_stopListeningToAudioVideoChanges() {
		if (this._localMediaModel) {
			this._senderPeerConnectionAnalyzer.setPeerConnection(null)

			this._senderPeerConnectionAnalyzer.off('change:connectionQualityAudio', this._handleConnectionQualityAudioChangeBound)
			this._senderPeerConnectionAnalyzer.off('change:connectionQualityVideo', this._handleConnectionQualityVideoChangeBound)

			this._localMediaModel.off('change:audioEnabled', this._handleSenderAudioEnabledChangeBound)
			this._localMediaModel.off('change:videoEnabled', this._handleSenderVideoEnabledChangeBound)
		}

		if (this._callParticipantModel) {
			this._receiverPeerConnectionAnalyzer.setPeerConnection(null)

			this._receiverPeerConnectionAnalyzer.off('change:connectionQualityAudio', this._handleConnectionQualityAudioChangeBound)
			this._receiverPeerConnectionAnalyzer.off('change:connectionQualityVideo', this._handleConnectionQualityVideoChangeBound)

			this._callParticipantModel.off('change:audioAvailable', this._handleReceiverAudioAvailableChangeBound)
			this._callParticipantModel.off('change:videoAvailable', this._handleReceiverVideoAvailableChangeBound)
		}
	},

	_stopListeningToScreenChanges() {
		if (this._localMediaModel) {
			this._senderScreenPeerConnectionAnalyzer.setPeerConnection(null)

			this._senderPeerConnectionAnalyzer.off('change:connectionQualityVideo', this._handleConnectionQualityScreenChangeBound)
		}

		if (this._callParticipantModel) {
			this._receiverScreenPeerConnectionAnalyzer.setPeerConnection(null)

			this._receiverPeerConnectionAnalyzer.off('change:connectionQualityVideo', this._handleConnectionQualityScreenChangeBound)
		}
	},

	_handleConnectionQualityAudioChange(peerConnectionAnalyzer, connectionQualityAudio) {
		if (peerConnectionAnalyzer === this._senderPeerConnectionAnalyzer) {
			this._trigger('change:senderConnectionQualityAudio', [connectionQualityAudio])
		} else if (peerConnectionAnalyzer === this._receiverPeerConnectionAnalyzer) {
			this._trigger('change:receiverConnectionQualityAudio', [connectionQualityAudio])
		}
	},

	_handleConnectionQualityVideoChange(peerConnectionAnalyzer, connectionQualityVideo) {
		if (peerConnectionAnalyzer === this._senderPeerConnectionAnalyzer) {
			this._trigger('change:senderConnectionQualityVideo', [connectionQualityVideo])
		} else if (peerConnectionAnalyzer === this._receiverPeerConnectionAnalyzer) {
			this._trigger('change:receiverConnectionQualityVideo', [connectionQualityVideo])
		}
	},

	_handleConnectionQualityScreenChange(peerConnectionAnalyzer, connectionQualityScreen) {
		if (peerConnectionAnalyzer === this._senderScreenPeerConnectionAnalyzer) {
			this._trigger('change:senderConnectionQualityScreen', [connectionQualityScreen])
		} else if (peerConnectionAnalyzer === this._receiverScreenPeerConnectionAnalyzer) {
			this._trigger('change:receiverConnectionQualityScreen', [connectionQualityScreen])
		}
	},

	_handleSenderAudioEnabledChange(localMediaModel, audioEnabled) {
		this._senderPeerConnectionAnalyzer.setAnalysisEnabledAudio(audioEnabled)
	},

	_handleSenderVideoEnabledChange(localMediaModel, videoEnabled) {
		this._senderPeerConnectionAnalyzer.setAnalysisEnabledVideo(videoEnabled)
	},

	_handleReceiverAudioAvailableChange(callParticipantModel, audioAvailable) {
		this._receiverPeerConnectionAnalyzer.setAnalysisEnabledAudio(audioAvailable)
	},

	_handleReceiverVideoAvailableChange(callParticipantModel, videoAvailable) {
		this._receiverPeerConnectionAnalyzer.setAnalysisEnabledVideo(videoAvailable)
	},

}

EmitterMixin.apply(ParticipantAnalyzer.prototype)

export {
	ParticipantAnalyzer,
}