-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathindex.js
498 lines (421 loc) · 15.5 KB
/
index.js
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
*
* @copyright Copyright (c) 2019, 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 Axios from '@nextcloud/axios'
import { getCapabilities } from '@nextcloud/capabilities'
import { PARTICIPANT, PRIVACY, VIRTUAL_BACKGROUND } from '../../constants.js'
import BrowserStorage from '../../services/BrowserStorage.js'
import { fetchSignalingSettings } from '../../services/signalingService.js'
import store from '../../store/index.js'
import CancelableRequest from '../cancelableRequest.js'
import Signaling from '../signaling.js'
import SignalingTypingHandler from '../SignalingTypingHandler.js'
import CallAnalyzer from './analyzers/CallAnalyzer.js'
import MediaDevicesManager from './MediaDevicesManager.js'
import CallParticipantCollection from './models/CallParticipantCollection.js'
import LocalCallParticipantModel from './models/LocalCallParticipantModel.js'
import LocalMediaModel from './models/LocalMediaModel.js'
import SentVideoQualityThrottler from './SentVideoQualityThrottler.js'
import './shims/MediaStream.js'
import './shims/MediaStreamTrack.js'
import SpeakingStatusHandler from './SpeakingStatusHandler.js'
import initWebRtc from './webrtc.js'
let webRtc = null
const callParticipantCollection = new CallParticipantCollection()
const localCallParticipantModel = new LocalCallParticipantModel()
const localMediaModel = new LocalMediaModel()
const mediaDevicesManager = new MediaDevicesManager()
let callAnalyzer = null
let sentVideoQualityThrottler = null
let speakingStatusHandler = null
// This does not really belongs here, as it is unrelated to WebRTC, but it is
// included here for the time being until signaling and WebRTC are split.
const enableTypingIndicators = getCapabilities()?.spreed?.config?.chat?.['typing-privacy'] === PRIVACY.PUBLIC
const signalingTypingHandler = enableTypingIndicators ? new SignalingTypingHandler(store) : null
let cancelFetchSignalingSettings = null
let signaling = null
let tokensInSignaling = {}
/**
* @param {string} token The token of the conversation to get the signaling settings for
* @param {object} options The additional options for the request
*/
async function getSignalingSettings(token, options) {
// If getSignalingSettings is called again while a previous one was still
// being executed the previous one is cancelled.
if (cancelFetchSignalingSettings) {
cancelFetchSignalingSettings('canceled')
cancelFetchSignalingSettings = null
}
const { request, cancel } = CancelableRequest(fetchSignalingSettings)
cancelFetchSignalingSettings = cancel
let settings = null
try {
const response = await request({ token }, options)
settings = response.data.ocs.data
settings.token = token
cancelFetchSignalingSettings = null
} catch (exception) {
if (Axios.isCancel(exception)) {
console.debug('Getting the signaling settings for ' + token + ' was cancelled by a newer getSignalingSettings')
} else {
console.warn('Failed to get the signaling settings for ' + token)
}
}
return settings
}
/**
* @param {string} token The token of the conversation to get the signaling settings for
* @param {string} random A string of at least 32 characters
* @param {string} checksum The SHA-256 HMAC of random with the secret of the
* recording server
*/
async function signalingGetSettingsForRecording(token, random, checksum) {
const options = {
headers: {
'Talk-Recording-Random': random,
'Talk-Recording-Checksum': checksum,
},
}
return getSignalingSettings(token, options)
}
/**
* @param {string} token The token of the conversation to connect to
*/
async function connectSignaling(token) {
const settings = await getSignalingSettings(token)
if (!settings) {
return
}
if (signaling && signaling.settings.server !== settings.server) {
if (webRtc) {
webRtc.disconnect()
webRtc = null
}
signaling.disconnect()
signaling = null
tokensInSignaling = {}
}
if (!signaling) {
signaling = Signaling.createConnection(settings)
signaling.on('updateSettings', async function() {
const settings = await getSignalingSettings(token)
console.debug('Received updated settings', settings)
signaling.setSettings(settings)
})
signalingTypingHandler?.setSignaling(signaling)
}
tokensInSignaling[token] = true
}
let pendingJoinCallToken = null
let startedCall = null
let failedToStartCall = null
/**
* @param {object} signaling The signaling object
* @param {object} configuration Media to connect with
* @param {boolean} silent Whether the call should trigger a notifications and
* sound for other participants or not
* @param {boolean} recordingConsent Whether the participant gave his consent to be recorded
*/
function startCall(signaling, configuration, silent, recordingConsent) {
let flags = PARTICIPANT.CALL_FLAG.IN_CALL
if (configuration) {
if (configuration.audio) {
flags |= PARTICIPANT.CALL_FLAG.WITH_AUDIO
}
if (configuration.video && signaling.getSendVideoIfAvailable()) {
flags |= PARTICIPANT.CALL_FLAG.WITH_VIDEO
}
}
signaling.joinCall(pendingJoinCallToken, flags, silent, recordingConsent).then(() => {
startedCall(flags)
}).catch(error => {
failedToStartCall(error)
})
}
/**
*
*/
function setupWebRtc() {
if (webRtc) {
return
}
webRtc = initWebRtc(signaling, callParticipantCollection, localCallParticipantModel)
localCallParticipantModel.setWebRtc(webRtc)
localMediaModel.setWebRtc(webRtc)
signaling.on('sessionId', sessionId => {
localCallParticipantModel.setPeerId(sessionId)
})
}
/**
* Join the given conversation on the respective signaling server with the given sessionId
*
* @param {string} token Conversation to join
* @param {string} sessionId Session id to join with
* @return {Promise<void>}
*/
async function signalingJoinConversation(token, sessionId) {
await connectSignaling(token)
if (tokensInSignaling[token]) {
await signaling.joinRoom(token, sessionId)
}
}
/**
* Join the call of the given conversation
*
* @param {string} token Conversation to join the call
* @param {number} flags Bitwise combination of PARTICIPANT.CALL_FLAG
* @param {boolean} silent Whether the call should trigger a notifications and
* sound for other participants or not
* @param {boolean} recordingConsent Whether the participant gave his consent to be recorded
* @return {Promise<void>} Resolved with the actual flags based on the
* available media
*/
async function signalingJoinCall(token, flags, silent, recordingConsent) {
if (tokensInSignaling[token]) {
pendingJoinCallToken = token
setupWebRtc()
sentVideoQualityThrottler = new SentVideoQualityThrottler(localMediaModel, callParticipantCollection, webRtc.webrtc._videoTrackConstrainer)
speakingStatusHandler = new SpeakingStatusHandler(store, localMediaModel, localCallParticipantModel, callParticipantCollection)
if (signaling.hasFeature('mcu')) {
callAnalyzer = new CallAnalyzer(localMediaModel, localCallParticipantModel, callParticipantCollection)
} else {
callAnalyzer = new CallAnalyzer(localMediaModel, null, callParticipantCollection)
}
const _signaling = signaling
return new Promise((resolve, reject) => {
startedCall = resolve
failedToStartCall = reject
// The previous state might be wiped after the media is started, so
// it should be saved now.
const enableAudio = !BrowserStorage.getItem('audioDisabled_' + token)
const enableVideo = !BrowserStorage.getItem('videoDisabled_' + token)
const enableVirtualBackground = !!BrowserStorage.getItem('virtualBackgroundEnabled_' + token)
const virtualBackgroundType = BrowserStorage.getItem('virtualBackgroundType_' + token)
const virtualBackgroundBlurStrength = BrowserStorage.getItem('virtualBackgroundBlurStrength_' + token)
const virtualBackgroundUrl = BrowserStorage.getItem('virtualBackgroundUrl_' + token)
if (enableAudio) {
localMediaModel.enableAudio()
} else {
localMediaModel.disableAudio()
}
if (enableVideo) {
localMediaModel.enableVideo()
} else {
localMediaModel.disableVideo()
}
if (enableVirtualBackground) {
localMediaModel.enableVirtualBackground()
} else {
localMediaModel.disableVirtualBackground()
}
if (virtualBackgroundType === VIRTUAL_BACKGROUND.BACKGROUND_TYPE.IMAGE) {
localMediaModel.setVirtualBackgroundImage(virtualBackgroundUrl)
} else if (virtualBackgroundType === VIRTUAL_BACKGROUND.BACKGROUND_TYPE.VIDEO) {
localMediaModel.setVirtualBackgroundVideo(virtualBackgroundUrl)
} else {
localMediaModel.setVirtualBackgroundBlur(virtualBackgroundBlurStrength)
}
const startCallOnceLocalMediaStarted = (configuration) => {
webRtc.off('localMediaStarted', startCallOnceLocalMediaStarted)
webRtc.off('localMediaError', startCallOnceLocalMediaError)
startCall(_signaling, configuration, silent, recordingConsent)
}
const startCallOnceLocalMediaError = () => {
webRtc.off('localMediaStarted', startCallOnceLocalMediaStarted)
webRtc.off('localMediaError', startCallOnceLocalMediaError)
startCall(_signaling, null, silent, recordingConsent)
}
// ".once" can not be used, as both handlers need to be removed when
// just one of them is executed.
webRtc.on('localMediaStarted', startCallOnceLocalMediaStarted)
webRtc.on('localMediaError', startCallOnceLocalMediaError)
webRtc.startMedia(token, flags)
})
}
}
/**
* Wait until the signaling connection succeed.
*
* If the authentication fails an error is thrown and the waiting aborted.
*
* @param {object} signaling the signaling to check its connection.
*/
async function signalingIsConnected(signaling) {
let signalingConnectionSucceeded
let signalingConnectionFailed
const signalingConnection = new Promise((resolve, reject) => {
signalingConnectionSucceeded = resolve
signalingConnectionFailed = reject
})
const signalingConnectionSucceededOnConnect = () => {
signaling.off('connect', signalingConnectionSucceededOnConnect)
signaling.off('error', signalingConnectionFailedOnInvalidToken)
signalingConnectionSucceeded()
}
const signalingConnectionFailedOnInvalidToken = (error) => {
if (error.code !== 'invalid_token') {
return
}
signaling.off('connect', signalingConnectionSucceededOnConnect)
signaling.off('error', signalingConnectionFailedOnInvalidToken)
signalingConnectionFailed(new Error('Authentication failed for signaling server: ' + signaling.settings.server))
}
signaling.on('connect', signalingConnectionSucceededOnConnect)
signaling.on('error', signalingConnectionFailedOnInvalidToken)
await signalingConnection
}
/**
* Join the call of the given conversation for recording with an internal
* client.
*
* The authentication parameters for the internal client must contain:
* - random: string of at least 32 characters
* - token: the SHA-256 HMAC of random with the internal secret of the signaling
* server
* - backend: the URL of the Nextcloud server that the conversation belongs to
*
* @param {string} token Conversation to join the call
* @param {object} settings the settings used to create the signaling connection
* @param {object} internalClientAuthParams the authentication parameters for
* the internal client
* @return {Promise<void>} Resolved with the actual flags based on the
* available media
*/
async function signalingJoinCallForRecording(token, settings, internalClientAuthParams) {
mediaDevicesManager.set('audioInputId', null)
mediaDevicesManager.set('videoInputId', null)
settings.helloAuthParams.internal = internalClientAuthParams
signaling = Signaling.createConnection(settings)
await signalingIsConnected(signaling)
// The default call flags for internal clients include audio, so they must
// be downgraded to just "in call" to prevent other participants from trying
// to connect to the recording participant.
// This must be done before joining the room to ensure that other
// participants will see the correct flags from the beginning.
signaling.doSend({
type: 'internal',
internal: {
type: 'incall',
incall: {
incall: PARTICIPANT.CALL_FLAG.IN_CALL,
},
},
})
// No Nextcloud session ID is needed to join the room with an internal
// client.
await signaling.joinRoom(token)
pendingJoinCallToken = token
setupWebRtc()
const _signaling = signaling
return new Promise((resolve, reject) => {
startedCall = resolve
failedToStartCall = reject
const silent = true
localMediaModel.disableAudio()
localMediaModel.disableVideo()
localMediaModel.disableVirtualBackground()
const startCallOnceLocalMediaStarted = (configuration) => {
webRtc.off('localMediaStarted', startCallOnceLocalMediaStarted)
webRtc.off('localMediaError', startCallOnceLocalMediaError)
startCall(_signaling, configuration, silent)
}
const startCallOnceLocalMediaError = () => {
webRtc.off('localMediaStarted', startCallOnceLocalMediaStarted)
webRtc.off('localMediaError', startCallOnceLocalMediaError)
startCall(_signaling, null, silent)
}
// ".once" can not be used, as both handlers need to be removed when
// just one of them is executed.
webRtc.on('localMediaStarted', startCallOnceLocalMediaStarted)
webRtc.on('localMediaError', startCallOnceLocalMediaError)
webRtc.startMedia(token, PARTICIPANT.CALL_FLAG.IN_CALL)
})
}
/**
* Leave the call of the given conversation
*
* @param {string} token Conversation to leave the call
* @param {boolean} all Whether to end the meeting for all
* @return {Promise<void>}
*/
async function signalingLeaveCall(token, all = false) {
sentVideoQualityThrottler.destroy()
sentVideoQualityThrottler = null
speakingStatusHandler.destroy()
speakingStatusHandler = null
callAnalyzer.destroy()
callAnalyzer = null
if (tokensInSignaling[token]) {
await signaling.leaveCall(token, false, all)
}
}
/**
* Leave the given conversation on the respective signaling server
*
* @param {string} token Conversation to leave
* @return {Promise<void>}
*/
async function signalingLeaveConversation(token) {
if (tokensInSignaling[token]) {
await signaling.leaveRoom(token)
}
}
/**
* Immediately kill the signaling connection synchronously
* This should be called only in the unload handler
*/
function signalingKill() {
if (signaling) {
signaling.disconnect()
}
}
/**
* Send a message through signaling
*
* @param {object} data message payload
* @return {Promise<void>}
*/
async function signalingSendCallMessage(data) {
await signaling.sendCallMessage(data)
}
/**
* Sets whether the current participant is typing.
*
* @param {boolean} typing whether the current participant is typing.
*/
function signalingSetTyping(typing) {
signalingTypingHandler?.setTyping(typing)
}
export {
callParticipantCollection,
localCallParticipantModel,
localMediaModel,
mediaDevicesManager,
callAnalyzer,
signalingGetSettingsForRecording,
signalingJoinConversation,
signalingJoinCall,
signalingJoinCallForRecording,
signalingLeaveCall,
signalingLeaveConversation,
signalingKill,
signalingSendCallMessage,
signalingSetTyping,
}