/
rtc-connection.ts
514 lines (423 loc) · 20.1 KB
/
rtc-connection.ts
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* SPDX-FileCopyrightText: 2022 Tim Perry <tim@httptoolkit.tech>
* SPDX-License-Identifier: Apache-2.0
*/
import * as _ from 'lodash';
import { randomUUID } from 'crypto';
import { EventEmitter } from 'events';
import * as SDP from 'sdp-transform';
import {
PeerConnection as NDCPeerConnection,
Track as NDCTrack,
DataChannel as NDCDataChannel,
Direction as NDCDirection
} from 'node-datachannel';
// Node-DataChannel is now an ES6 module, but we're not - work around
// that with a synchronous import wrapper:
const importSync = require('import-sync');
const NodeDataChannel = importSync('node-datachannel') as
typeof import('node-datachannel');
import type { MockRTCSessionDescription } from '../mockrtc';
import {
ConnectionMetadata,
MockRTCSession,
AnswerOptions,
OfferOptions
} from '../mockrtc-peer';
import { DataChannelStream } from './datachannel-stream';
import { MediaTrackStream } from './mediatrack-stream';
export type ParsedSDP = {
parsedSdp: SDP.SessionDescription;
};
/**
* An RTC connection is a single connection. This base class defines the raw connection management and
* tracking logic for a generic connection. The MockRTCConnection subclass extends this and adds
* logic to support control channels, proxying and other MockRTC-specific additions.
*/
export class RTCConnection extends EventEmitter {
readonly id = randomUUID();
// Set to null when the connection is closed, as otherwise calling any method (including checking
// the connection state) will segfault the process.
private rawConn: NDCPeerConnection | null
= new NodeDataChannel.PeerConnection("MockRTCConnection", { iceServers: [], forceMediaTransport: true });
private remoteDescription: RTCSessionDescriptionInit & ParsedSDP | undefined;
private localDescription: MockRTCSessionDescription & ParsedSDP | undefined;
private _connectionMetadata: ConnectionMetadata = {};
public get metadata() {
return this._connectionMetadata;
}
private readonly trackedChannels: Array<{ stream: DataChannelStream, isLocal: boolean }> = [];
get channels(): ReadonlyArray<DataChannelStream> {
return this.trackedChannels
.map(channel => channel.stream);
}
get localChannels(): ReadonlyArray<DataChannelStream> {
return this.trackedChannels
.filter(channel => channel.isLocal)
.map(channel => channel.stream);
}
get remoteChannels(): ReadonlyArray<DataChannelStream> {
return this.trackedChannels
.filter(channel => !channel.isLocal)
.map(channel => channel.stream);
}
private readonly trackedMediaTracks: Array<{ stream: MediaTrackStream, isLocal: boolean }> = [];
get mediaTracks(): ReadonlyArray<MediaTrackStream> {
return this.trackedMediaTracks
.map(track => track.stream);
}
get localMediaTracks(): ReadonlyArray<MediaTrackStream> {
return this.trackedMediaTracks
.filter(track => track.isLocal)
.map(track => track.stream);
}
get remoteMediaTracks(): ReadonlyArray<MediaTrackStream> {
return this.trackedMediaTracks
.filter(track => !track.isLocal)
.map(track => track.stream);
}
constructor() {
super();
this.rawConn!.onDataChannel((channel) => {
if (!this.rawConn) return; // https://github.com/murat-dogan/node-datachannel/issues/103
this.trackNewChannel(channel, { isLocal: false });
});
this.rawConn!.onTrack((track: NDCTrack) => {
if (!this.rawConn) return; // https://github.com/murat-dogan/node-datachannel/issues/103
this.trackNewMediaTrack(track, { isLocal: false });
});
// Important to remember that only node-dc only allows one listener per event. To handle that,
// we reemit important events here to use normal node event methods instead:
this.rawConn!.onStateChange((state) => {
this.emit('connection-state-changed', state);
});
this.on('connection-state-changed', (state) => {
if (state === 'connected') {
this.emit('connection-connected');
} else if (state === 'closed' || state === 'disconnected') {
this.emit('connection-closed');
this.remoteDescription = undefined;
this.localDescription = undefined;
}
});
}
createDataChannel(label: string) {
if (!this.rawConn) throw new Error("Can't create data channel after connection is closed");
const channel = this.rawConn.createDataChannel(label);
return this.trackNewChannel(channel, { isLocal: true });
}
protected trackNewChannel(channel: NDCDataChannel, options: { isLocal: boolean }) {
const channelStream = new DataChannelStream(channel);
this.trackedChannels.push({ stream: channelStream, isLocal: options.isLocal });
channelStream.on('close', () => {
const channelIndex = this.trackedChannels.findIndex(c => c.stream === channelStream);
if (channelIndex !== -1) {
this.trackedChannels.splice(channelIndex, 1);
}
});
channelStream.on('error', (error) => {
console.error('Channel error:', error);
});
this.emit('channel-created', channelStream);
this.emit(`${options.isLocal ? 'local' : 'remote'}-channel-created`, channelStream);
channelStream.once('channel-open', () => {
this.emit('channel-open', channelStream);
this.emit(`${options.isLocal ? 'local' : 'remote'}-channel-open`, channelStream);
});
return channelStream;
}
protected trackNewMediaTrack(track: NDCTrack, options: { isLocal: boolean }) {
const trackStream = new MediaTrackStream(track);
this.trackedMediaTracks.push({ stream: trackStream, isLocal: options.isLocal });
trackStream.on('close', () => {
const trackIndex = this.trackedMediaTracks.findIndex(c => c.stream === trackStream);
if (trackIndex !== -1) {
this.trackedMediaTracks.splice(trackIndex, 1);
}
});
trackStream.on('error', (error) => {
console.error('Media track error:', error);
});
this.emit('track-created', trackStream);
this.emit(`${options.isLocal ? 'local' : 'remote'}-track-created`, trackStream);
trackStream.once('track-open', () => {
this.emit('track-open', trackStream);
this.emit(`${options.isLocal ? 'local' : 'remote'}-track-open`, trackStream);
});
return trackStream;
}
setRemoteDescription(description: RTCSessionDescriptionInit) {
if (!this.rawConn) throw new Error("Can't set remote description after connection is closed");
this.remoteDescription = {
...description,
parsedSdp: SDP.parse(description.sdp ?? '')
};
const { type: offerType, sdp: offerSdp } = description;
if (!offerSdp) throw new Error("Cannot set MockRTC peer description without providing an SDP");
this.rawConn.setRemoteDescription(offerSdp, offerType[0].toUpperCase() + offerType.slice(1) as any);
}
/**
* Gets the local description for this connection, waiting until gathering is complete to provide a
* full result. Because this waits for gathering, it will not resolve if no DataChannel, other
* tracks or remote description have been provided beforehand.
*/
async buildLocalDescription(): Promise<MockRTCSessionDescription> {
if (!this.rawConn) throw new Error("Can't get local description after connection is closed");
let setupChannel: NDCDataChannel | undefined;
if (this.rawConn.gatheringState() === 'new') {
// We can't create an offer until we have something to negotiate, but we don't want to
// negotiate ourselves when we don't really know what's being negotiated here. To work
// around that, we create a channel to trigger gathering & get an offer, and then we
// remove it before the offer is delivered, so it's never visible remotely.
setupChannel = this.rawConn.createDataChannel('mockrtc.setup-channel');
}
await new Promise<void>((resolve) => {
this.rawConn!.onGatheringStateChange((state) => {
if (state === 'complete') resolve();
});
// Handle race conditions where gathering has already completed
if (this.rawConn!.gatheringState() === 'complete') resolve();
});
if (!this.rawConn) throw new Error("Connection was closed while building local description");
const sessionDescription = this.rawConn.localDescription() as MockRTCSessionDescription;
setupChannel?.close(); // Close the temporary setup channel, if we created one
this.localDescription = {
...sessionDescription,
parsedSdp: SDP.parse(sessionDescription.sdp ?? '')
};
return sessionDescription;
}
getRemoteDescription() {
if (!this.rawConn) throw new Error("Can't get remote description after connection is closed");
return this.remoteDescription;
}
getLocalDescription() {
if (!this.rawConn) throw new Error("Can't get local description after connection is closed");
return this.localDescription;
}
getSelectedCandidates() {
if (!this.rawConn) throw new Error("Can't get selected candidates after connection is closed");
const candidates = this.rawConn.getSelectedCandidatePair();
if (!candidates) return undefined;
// Rename transportType -> protocol, to better match the browser WebRTC APIs
return {
local: {
..._.omit(candidates.local, 'transportType'),
type: candidates.local.type as RTCIceCandidateType,
protocol: candidates.local.transportType.toLowerCase()
},
remote: {
..._.omit(candidates.remote, 'transportType'),
type: candidates.remote.type as RTCIceCandidateType,
protocol: candidates.remote.transportType.toLowerCase()
}
};
}
async getMirroredLocalOffer(
sdpToMirror: string,
options: { addDataStream?: boolean } = {}
): Promise<MockRTCSessionDescription> {
if (!this.rawConn) throw new Error("Can't get local description after connection is closed");
const offerToMirror = SDP.parse(sdpToMirror);
const mediaStreamsToMirror = offerToMirror.media.filter(media => media.type !== 'application');
const shouldMirrorDataStream = offerToMirror.media.some(media => media.type === 'application');
mediaStreamsToMirror.forEach((mediaToMirror) => {
// Skip media tracks that we already have
if (this.mediaTracks.find(({ mid }) => mid === mediaToMirror.mid!)) return;
const mid = mediaToMirror.mid!.toString();
const direction = sdpDirectionToNDCDirection(mediaToMirror.direction);
const media = mediaToMirror.type === 'video'
? new NodeDataChannel.Video(mid, direction)
: new NodeDataChannel.Audio(mid, direction)
// Copy SSRC data (awkward translation between per-attr and full-value structures)
const ssrcs = mediaToMirror.ssrcs?.reduce((ssrcs, kv) => {
ssrcs[kv.id] ||= {};
ssrcs[kv.id][kv.attribute] = kv.value;
return ssrcs;
}, {} as { [id: string]: { [attr: string]: string | undefined } }) ?? {};
Object.keys(ssrcs).forEach((id) => {
const ssrcAttrs = ssrcs[id];
const [msid, trackId] = ssrcAttrs.msid?.split(' ') ?? [];
if (!msid) {
media.addSSRC(
parseInt(id, 10),
ssrcAttrs['cname']
);
} else {
media.addSSRC(
parseInt(id, 10),
ssrcAttrs['cname'],
msid,
trackId
);
}
});
const track = this.rawConn!.addTrack(media);
this.trackNewMediaTrack(track, { isLocal: true });
});
let setupChannel: NDCDataChannel | undefined;
const channelRequiredForDescription = this.rawConn.gatheringState() === 'new' &&
!mediaStreamsToMirror.length;
if (shouldMirrorDataStream || channelRequiredForDescription || options.addDataStream) {
// See getLocalDescription() above: if we want a description and we have no media, we
// need to make a stub channel to allow us to negotiate _something_.
// In addition, we might actually have data channels to mirror. In that case, we need
// to create a temporary data channel to force that negotiation (which will be closed
// again shortly, so that it never actually gets created).
setupChannel = this.rawConn.createDataChannel('mockrtc.setup-channel');
}
this.rawConn.setLocalDescription(NodeDataChannel.DescriptionType.Offer);
await new Promise<void>((resolve) => {
this.rawConn!.onGatheringStateChange((state) => {
if (state === 'complete') resolve();
});
// Handle race conditions where gathering has already completed
if (this.rawConn!.gatheringState() === 'complete') resolve();
});
if (!this.rawConn) throw new Error("Connection was closed while building the local description");
const localDesc = this.rawConn.localDescription()!;
setupChannel?.close(); // Close the temporary setup channel, if we created one
const offerSDP = SDP.parse(localDesc.sdp);
mirrorMediaParams(offerToMirror, offerSDP);
localDesc.sdp = SDP.write(offerSDP);
this.localDescription = {
...localDesc as MockRTCSessionDescription,
parsedSdp: offerSDP
};
return this.localDescription;
}
async getMirroredLocalAnswer(sdpToMirror: string): Promise<MockRTCSessionDescription> {
const localDesc = this.rawConn!.localDescription()!;
const answerToMirror = SDP.parse(sdpToMirror);
const answerSDP = SDP.parse(localDesc.sdp!);
mirrorMediaParams(answerToMirror, answerSDP);
localDesc.sdp = SDP.write(answerSDP);
this.localDescription = {
...localDesc as MockRTCSessionDescription,
parsedSdp: answerSDP
};
return this.localDescription;
}
waitUntilConnected() {
return new Promise<void>((resolve, reject) => {
if (!this.rawConn) throw new Error("Connection closed while/before waiting until connected");
this.on('connection-state-changed', (state) => {
if (state === 'connected') resolve();
if (state === 'failed') {
reject(new Error("Connection failed while waiting for connection"));
}
});
if (this.rawConn.state() === 'connected') resolve();
if (this.rawConn.state() === 'failed') {
reject(new Error("Connection failed while waiting for connection"));
}
});
}
readonly sessionApi: MockRTCSession = {
sessionId: this.id, // The session id is actually just the connection id, shhh don't tell anyone.
createOffer: async (options: OfferOptions = {}): Promise<MockRTCSessionDescription> => {
if (options.connectionMetadata) {
this._connectionMetadata = {
...this._connectionMetadata,
...options.connectionMetadata
};
}
if (options.mirrorSDP) {
return this.getMirroredLocalOffer(options.mirrorSDP, {
addDataStream: !!options.addDataStream
});
} else {
return this.buildLocalDescription();
}
},
completeOffer: async (answer: MockRTCSessionDescription): Promise<void> => {
this.setRemoteDescription(answer);
},
answerOffer: async (
offer: MockRTCSessionDescription,
options: AnswerOptions = {}
): Promise<MockRTCSessionDescription> => {
if (options.connectionMetadata) {
this._connectionMetadata = {
...this._connectionMetadata,
...options.connectionMetadata
};
}
this.setRemoteDescription(offer);
if (options.mirrorSDP) {
return this.getMirroredLocalAnswer(options.mirrorSDP);
} else {
return this.buildLocalDescription();
}
}
};
async close() {
if (!this.rawConn) return; // Already closed
const { rawConn } = this;
this.rawConn = null; // Drop the reference, so nothing tries to use it after close
this.remoteDescription = undefined;
this.localDescription = undefined;
if (rawConn.state() === 'closed') return;
rawConn.destroy();
this.emit('connection-closed');
}
}
function sdpDirectionToNDCDirection(direction: SDP.SharedAttributes['direction']): NDCDirection {
if (direction === 'inactive') return NodeDataChannel.Direction.Inactive;
else if (direction?.length === 8) {
return direction[0].toUpperCase() +
direction.slice(1, 4) +
direction[4].toUpperCase() +
direction.slice(5) as NodeDataChannel.Direction;
} else {
return NodeDataChannel.Direction.Unknown;
}
};
/**
* Takes two parsed descriptions (typically a real description we want to mock, and our own current
* self-generated description) and modifies the target description sure that the media params for
* each stream in the source description match.
*
* In theory, this should guarantee that RTP packets generated by the source and forwarded through
* the target's connection can be interpreted by somebody connected to the target.
*/
function mirrorMediaParams(source: SDP.SessionDescription, target: SDP.SessionDescription) {
target.msidSemantic = source.msidSemantic;
const sourceMediaStreams = source.media.filter(m => m.type !== 'application');
sourceMediaStreams.forEach((sourceMedia) => {
const targetMedia = target.media
.find((targetMedia) => targetMedia.mid === sourceMedia.mid);
if (!targetMedia) {
throw new Error(
`Missing mid ${sourceMedia.mid} in target when mirroring media params`
);
}
if (sourceMedia.type !== targetMedia.type) {
throw new Error(
`Unexpected media type (${
targetMedia.type
}) for mid ${
targetMedia.mid
} when mirroring media params`
);
}
// Copy all the semantic parameters of the RTP & RTCP streams themselves, so that RTP packets
// can be forwarded correctly, but without copying the fingerprint or similar, so we can still
// act as a MitM to intercept the packets:
targetMedia.msid = sourceMedia.msid;
targetMedia.protocol = sourceMedia.protocol;
targetMedia.ext = sourceMedia.ext;
targetMedia.payloads = sourceMedia.payloads;
targetMedia.rtp = sourceMedia.rtp;
targetMedia.fmtp = sourceMedia.fmtp;
targetMedia.rtcp = sourceMedia.rtcp;
targetMedia.rtcpFb = sourceMedia.rtcpFb;
targetMedia.ssrcGroups = sourceMedia.ssrcGroups;
// SSRC info is especially important here: this is used to map RTP SSRCs to track mids, so if
// this is incorrect, the recipient track will not receive the data we're sending.
// Although in some cases we do already have some SSRC info here, for offers where we've already
// defined the tracks ourselves, libdatachannel doesn't support all params and it's best to copy
// the full definition itself directly to make sure they match:
targetMedia.ssrcs = sourceMedia.ssrcs;
});
}