-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathProcessor.ts
More file actions
450 lines (350 loc) · 12.8 KB
/
Processor.ts
File metadata and controls
450 lines (350 loc) · 12.8 KB
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
import { Emitter } from '@rocket.chat/emitter';
import { LocalStream } from './LocalStream';
import { RemoteStream } from './RemoteStream';
import type { IWebRTCProcessor, WebRTCInternalStateMap, WebRTCProcessorConfig, WebRTCProcessorEvents } from '../../../definition';
import type { ServiceStateValue } from '../../../definition/services/IServiceProcessor';
import { getExternalWaiter, type PromiseWaiterData } from '../../utils/getExternalWaiter';
export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
public readonly emitter: Emitter<WebRTCProcessorEvents>;
private peer: RTCPeerConnection;
private iceGatheringFinished = false;
private iceGatheringTimedOut = false;
private localStream: LocalStream;
private localMediaStream: MediaStream;
private localMediaStreamInitialized = false;
private remoteStream: RemoteStream;
private remoteMediaStream: MediaStream;
private iceGatheringWaiters: Set<PromiseWaiterData>;
private inputTrack: MediaStreamTrack | null;
private _muted = false;
public get muted(): boolean {
return this._muted;
}
private _held = false;
public get held(): boolean {
return this._held;
}
private stopped = false;
private iceCandidateCount = 0;
private lastSetLocalDescription: string | null = null;
private addedEmptyTransceiver = false;
constructor(private readonly config: WebRTCProcessorConfig) {
this.localMediaStream = new MediaStream();
this.remoteMediaStream = new MediaStream();
this.iceGatheringWaiters = new Set();
this.inputTrack = config.inputTrack;
this.peer = new RTCPeerConnection(config.rtc);
this.localStream = new LocalStream(this.localMediaStream, this.peer, this.config.logger);
this.remoteStream = new RemoteStream(this.remoteMediaStream, this.peer, this.config.logger);
this.emitter = new Emitter();
this.registerPeerEvents();
}
public getRemoteMediaStream() {
return this.remoteMediaStream;
}
public async setInputTrack(newInputTrack: MediaStreamTrack | null): Promise<void> {
this.config.logger?.debug('MediaCallWebRTCProcessor.setInputTrack');
if (newInputTrack && newInputTrack.kind !== 'audio') {
throw new Error('Unsupported track kind');
}
this.inputTrack = newInputTrack;
await this.loadInputTrack();
}
public async createOffer({ iceRestart }: { iceRestart?: boolean }): Promise<{ sdp: RTCSessionDescriptionInit }> {
this.config.logger?.debug('MediaCallWebRTCProcessor.createOffer');
if (this.stopped) {
throw new Error('WebRTC Processor has already been stopped.');
}
await this.initializeLocalMediaStream();
if (!this.addedEmptyTransceiver) {
// If there's no audio transceivers yet, add a new one; since it's an offer, the track can be set later
const transceivers = this.peer
.getTransceivers()
.filter((transceiver) => transceiver.sender.track?.kind === 'audio' || transceiver.receiver.track?.kind === 'audio');
if (!transceivers.length) {
this.peer.addTransceiver('audio', { direction: 'sendrecv' });
this.addedEmptyTransceiver = true;
}
}
if (iceRestart) {
this.restartIce();
}
const offer = await this.peer.createOffer();
if (this.lastSetLocalDescription && offer.sdp !== this.lastSetLocalDescription && !iceRestart) {
this.startNewNegotiation();
}
this.lastSetLocalDescription = offer.sdp || null;
await this.peer.setLocalDescription(offer);
return this.getLocalDescription();
}
public setMuted(muted: boolean): void {
if (this.stopped) {
return;
}
this._muted = muted;
this.localStream.setEnabled(!muted && !this._held);
}
public setHeld(held: boolean): void {
if (this.stopped) {
return;
}
this._held = held;
this.localStream.setEnabled(!held && !this._muted);
this.remoteStream.setEnabled(!held);
}
public stop(): void {
this.config.logger?.debug('MediaCallWebRTCProcessor.stop');
this.stopped = true;
// Stop only the remote stream; the track of the local stream may still be in use by another call so it's up to the session to stop it.
this.remoteStream.stopAudio();
this.unregisterPeerEvents();
this.peer.close();
}
public startNewNegotiation(): void {
this.iceGatheringFinished = false;
this.clearIceGatheringWaiters(new Error('new-negotiation'));
this.iceCandidateCount = 0;
}
public async createAnswer({ sdp }: { sdp: RTCSessionDescriptionInit }): Promise<{ sdp: RTCSessionDescriptionInit }> {
this.config.logger?.debug('MediaCallWebRTCProcessor.createAnswer');
if (this.stopped) {
throw new Error('WebRTC Processor has already been stopped.');
}
if (sdp.type !== 'offer') {
throw new Error('invalid-webrtc-offer');
}
if (!this.inputTrack) {
throw new Error('no-input-track');
}
await this.initializeLocalMediaStream();
const transceivers = this.peer
.getTransceivers()
.filter((transceiver) => transceiver.sender.track?.kind === 'audio' || transceiver.receiver.track?.kind === 'audio');
if (!transceivers.length) {
throw new Error('no-audio-transceiver');
}
if (this.peer.remoteDescription?.sdp !== sdp.sdp) {
this.startNewNegotiation();
await this.peer.setRemoteDescription(sdp);
}
const answer = await this.peer.createAnswer();
this.lastSetLocalDescription = answer.sdp || null;
await this.peer.setLocalDescription(answer);
return this.getLocalDescription();
}
public async setRemoteAnswer({ sdp }: { sdp: RTCSessionDescriptionInit }): Promise<void> {
this.config.logger?.debug('MediaCallWebRTCProcessor.setRemoteAnswer');
if (this.stopped) {
return;
}
if (sdp.type === 'offer') {
throw new Error('invalid-answer');
}
await this.peer.setRemoteDescription(sdp);
}
public getInternalState<K extends keyof WebRTCInternalStateMap>(stateName: K): ServiceStateValue<WebRTCInternalStateMap, K> {
switch (stateName) {
case 'signaling':
return this.peer.signalingState;
case 'connection':
return this.peer.connectionState;
case 'iceConnection':
return this.peer.iceConnectionState;
case 'iceGathering':
return this.peer.iceGatheringState;
case 'iceUntrickler':
if (this.iceGatheringTimedOut) {
return 'timeout';
}
return this.iceGatheringWaiters.size > 0 ? 'waiting' : 'not-waiting';
}
}
private changeInternalState(stateName: keyof WebRTCInternalStateMap): void {
this.config.logger?.debug('MediaCallWebRTCProcessor.changeInternalState', stateName);
this.emitter.emit('internalStateChange', stateName);
}
private async getLocalDescription(): Promise<{ sdp: RTCSessionDescriptionInit }> {
this.config.logger?.debug('MediaCallWebRTCProcessor.getLocalDescription');
if (this.stopped) {
throw new Error('WebRTC Processor has already been stopped.');
}
await this.waitForIceGathering();
const sdp = this.peer.localDescription;
if (!sdp) {
throw new Error('no-local-sdp');
}
this.config.logger?.debug('MediaCallWebRTCProcessor.getLocalDescription - ice candidates: ', this.iceCandidateCount);
// If we don't have any ice candidate, trigger a service error.
if (this.iceCandidateCount === 0) {
this.emitter.emit('internalError', { critical: true, error: 'no-ice-candidates' });
}
return {
sdp,
};
}
private async waitForIceGathering(): Promise<void> {
this.config.logger?.debug('MediaCallWebRTCProcessor.waitForIceGathering');
if (this.iceGatheringFinished || this.stopped) {
return;
}
this.iceGatheringTimedOut = false;
const iceGatheringData = getExternalWaiter({
timeout: this.config.iceGatheringTimeout,
timeoutFn: () => {
if (!this.iceGatheringWaiters.has(iceGatheringData)) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.waitForIceGathering.timeout', this.iceCandidateCount);
this.clearIceGatheringData(iceGatheringData);
this.iceGatheringTimedOut = true;
this.changeInternalState('iceUntrickler');
},
});
this.iceGatheringWaiters.add(iceGatheringData);
this.changeInternalState('iceUntrickler');
await iceGatheringData.promise;
// always wait a little extra to ensure all relevant events have been fired
// 30ms is low enough that it won't be noticeable by users, but is also enough time to process any local stuff
await new Promise((resolve) => setTimeout(resolve, 30));
}
private registerPeerEvents() {
const { peer } = this;
peer.ontrack = (event) => this.onTrack(event);
peer.onicecandidate = (event) => this.onIceCandidate(event);
peer.onicecandidateerror = (event) => this.onIceCandidateError(event);
peer.onconnectionstatechange = () => this.onConnectionStateChange();
peer.oniceconnectionstatechange = () => this.onIceConnectionStateChange();
peer.onnegotiationneeded = () => this.onNegotiationNeeded();
peer.onicegatheringstatechange = () => this.onIceGatheringStateChange();
peer.onsignalingstatechange = () => this.onSignalingStateChange();
}
private unregisterPeerEvents() {
try {
const { peer } = this;
peer.ontrack = null as any;
peer.onicecandidate = null as any;
peer.onicecandidateerror = null as any;
peer.onconnectionstatechange = null as any;
peer.oniceconnectionstatechange = null as any;
peer.onnegotiationneeded = null as any;
peer.onicegatheringstatechange = null as any;
peer.onsignalingstatechange = null as any;
} catch {
// suppress exceptions here
}
}
private restartIce() {
this.config.logger?.debug('MediaCallWebRTCProcessor.restartIce');
this.startNewNegotiation();
this.peer.restartIce();
}
private onIceCandidate(event: RTCPeerConnectionIceEvent) {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onIceCandidate', event.candidate);
this.iceCandidateCount++;
}
private onIceCandidateError(event: RTCPeerConnectionIceErrorEvent) {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onIceCandidateError');
this.config.logger?.error(event);
this.emitter.emit('internalError', { critical: false, error: 'ice-candidate-error', errorDetails: JSON.stringify(event) });
}
private onNegotiationNeeded() {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onNegotiationNeeded');
this.emitter.emit('negotiationNeeded');
}
private onTrack(event: RTCTrackEvent): void {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onTrack', event.track.kind);
// Received a remote stream
this.remoteStream.setTrack(event.track);
}
private onConnectionStateChange() {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onConnectionStateChange');
this.changeInternalState('connection');
}
private onIceConnectionStateChange() {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onIceConnectionStateChange');
this.changeInternalState('iceConnection');
}
private onSignalingStateChange() {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onSignalingStateChange');
this.changeInternalState('signaling');
}
private onIceGatheringStateChange() {
if (this.stopped) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.onIceGatheringStateChange');
if (this.peer.iceGatheringState === 'complete') {
this.onIceGatheringComplete();
}
this.changeInternalState('iceGathering');
}
private async initializeLocalMediaStream(): Promise<void> {
if (this.localMediaStreamInitialized) {
return;
}
this.config.logger?.debug('MediaCallWebRTCProcessor.initializeLocalMediaStream');
await this.loadInputTrack();
}
private async loadInputTrack(): Promise<void> {
this.config.logger?.debug('MediaCallWebRTCProcessor.loadInputTrack');
this.localMediaStreamInitialized = true;
await this.localStream.setTrack(this.inputTrack);
}
private onIceGatheringComplete() {
this.config.logger?.debug('MediaCallWebRTCProcessor.onIceGatheringComplete');
this.iceGatheringFinished = true;
this.clearIceGatheringWaiters();
}
private clearIceGatheringData(iceGatheringData: PromiseWaiterData, error?: Error) {
this.config.logger?.debug('MediaCallWebRTCProcessor.clearIceGatheringData');
if (this.iceGatheringWaiters.has(iceGatheringData)) {
this.iceGatheringWaiters.delete(iceGatheringData);
}
if (iceGatheringData.timeout) {
clearTimeout(iceGatheringData.timeout);
}
if (error) {
if (iceGatheringData.promiseReject) {
iceGatheringData.promiseReject(error);
}
return;
}
if (iceGatheringData.promiseResolve) {
iceGatheringData.promiseResolve();
}
}
private clearIceGatheringWaiters(error?: Error) {
this.config.logger?.debug('MediaCallWebRTCProcessor.clearIceGatheringWaiters');
this.iceGatheringTimedOut = false;
if (!this.iceGatheringWaiters.size) {
return;
}
const waiters = Array.from(this.iceGatheringWaiters.values());
this.iceGatheringWaiters.clear();
for (const iceGatheringData of waiters) {
this.clearIceGatheringData(iceGatheringData, error);
}
this.changeInternalState('iceUntrickler');
}
}