/
mockrtc-server-peer.ts
346 lines (278 loc) · 13 KB
/
mockrtc-server-peer.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
/*
* SPDX-FileCopyrightText: 2022 Tim Perry <tim@httptoolkit.tech>
* SPDX-License-Identifier: Apache-2.0
*/
import { randomUUID } from 'crypto';
import { EventEmitter } from "events";
import now = require("performance-now");
import {
MockRTCPeer,
MockRTCPeerOptions,
MockRTCSession,
MockRTCAnswerParams,
MockRTCOfferParams,
MockRTCExternalAnswerParams,
MockRTCExternalOfferParams,
OfferOptions,
AnswerOptions
} from "../mockrtc-peer";
import { HandlerStep } from '../handling/handler-steps';
import { RTCConnection } from '../webrtc/rtc-connection';
import { MockRTCConnection } from '../webrtc/mockrtc-connection';
import { DataChannelStream } from '../webrtc/datachannel-stream';
import { MediaTrackStream } from '../webrtc/mediatrack-stream';
import { TimingEvents } from '../mockrtc';
export class MockRTCServerPeer implements MockRTCPeer {
readonly peerId = randomUUID();
private debug: boolean = false;
// A list of all currently open connections managed by this peer
private readonly connections: { [id: string]: RTCConnection } = {};
// A subset of the connections: external connections with no assigned internal connection
private readonly unassignedExternalConnections: { [id: string]: RTCConnection } = {};
constructor(
private getHandlerSteps: (conn: RTCConnection) =>
(HandlerStep[] | Promise<HandlerStep[]>),
private options: MockRTCPeerOptions & { peerId?: string } = {},
private eventEmitter: EventEmitter
) {
this.debug = !!options.debug;
if (options.peerId) this.peerId = options.peerId;
}
private trackConnection(conn: RTCConnection) {
this.connections[conn.id] = conn;
conn.once('connection-closed', () => {
delete this.connections[conn.id];
});
if (conn instanceof MockRTCConnection) {
// Here we listen to the various internal connection events, and convert them into
// their corresponding public-API events.
conn.once('connection-connected', () => {
const timingEvents: TimingEvents = {
startTime: Date.now(),
connectTimestamp: now()
}
const connectionEventParams = {
peerId: this.peerId,
sessionId: conn.id,
timingEvents
};
const selectedCandidates = conn.getSelectedCandidates()!;
this.eventEmitter.emit('peer-connected', {
...connectionEventParams,
localSessionDescription: conn.getLocalDescription(),
remoteSessionDescription: conn.getRemoteDescription(),
metadata: conn.metadata,
selectedLocalCandidate: selectedCandidates.local,
selectedRemoteCandidate: selectedCandidates.remote
});
conn.once('external-connection-attached', (externalConn: RTCConnection) => {
timingEvents.externalAttachTimestamp = now();
const selectedExternalCandidates = externalConn.getSelectedCandidates()!;
this.eventEmitter.emit('external-peer-attached', {
...connectionEventParams,
externalConnection: {
sessionId: externalConn.id,
localSessionDescription: externalConn.getLocalDescription(),
remoteSessionDescription: externalConn.getRemoteDescription(),
selectedLocalCandidate: selectedExternalCandidates.local,
selectedRemoteCandidate: selectedExternalCandidates.remote
}
});
});
const emitChannelEvents = (channelStream: DataChannelStream) => {
const channelEventParams = {
...connectionEventParams,
channelId: channelStream.id,
};
const announceOpen = () => {
this.eventEmitter.emit('data-channel-opened', {
...channelEventParams,
channelLabel: channelStream.label,
channelProtocol: channelStream.protocol,
eventTimestamp: now()
});
};
if (channelStream.isOpen) announceOpen();
else channelStream.on('channel-open', announceOpen);
const emitMessage = (direction: 'sent' | 'received') => (data: Buffer | string) => {
const isBinary = Buffer.isBuffer(data);
const content: Buffer = isBinary
? data
: Buffer.from(data, 'utf8');
this.eventEmitter.emit(`data-channel-message-${direction}`, {
...channelEventParams,
direction,
content,
isBinary,
eventTimestamp: now()
});
};
channelStream.on('read-data', emitMessage('received'));
channelStream.on('wrote-data', emitMessage('sent'));
channelStream.on('close', () => this.eventEmitter.emit('data-channel-closed', {
...channelEventParams,
eventTimestamp: now()
}));
}
conn.on('channel-created', emitChannelEvents);
// Due to race conditions somewhere (?) presumably in node-datachannel, channels can
// be created before the 'connected' event fires, so we need to handle already
// existing channels here too:
conn.channels.forEach(emitChannelEvents);
const emitTrackEvents = (mediaTrack: MediaTrackStream) => {
const trackEventParams = {
...connectionEventParams,
trackMid: mediaTrack.mid
};
const announceOpen = () => {
this.eventEmitter.emit('media-track-opened', {
...trackEventParams,
trackType: mediaTrack.type,
trackDirection: mediaTrack.direction,
eventTimestamp: now()
});
};
if (mediaTrack.isOpen) announceOpen();
else mediaTrack.on('track-open', announceOpen);
let previousBytesSent = 0;
let previousBytesReceived = 0;
const statsInterval = setInterval(() => {
if (
previousBytesSent === mediaTrack.totalBytesSent &&
previousBytesReceived === mediaTrack.totalBytesReceived
) return; // Skip zero-change events to limit traffic noise
this.eventEmitter.emit('media-track-stats', {
...trackEventParams,
totalBytesSent: mediaTrack.totalBytesSent,
totalBytesReceived: mediaTrack.totalBytesReceived,
eventTimestamp: now()
});
previousBytesSent = mediaTrack.totalBytesSent;
previousBytesReceived = mediaTrack.totalBytesReceived;
}, 1000);
mediaTrack.on('close', () => {
clearInterval(statsInterval);
this.eventEmitter.emit('media-track-closed', {
...trackEventParams,
eventTimestamp: now()
});
});
}
conn.on('track-created', emitTrackEvents);
// Due to race conditions somewhere (?) presumably in node-datachannel, tracks can
// be created before the 'connected' event fires, so we need to handle already
// existing tracks here too:
conn.mediaTracks.forEach(emitTrackEvents);
conn.once('connection-closed', () => {
timingEvents.disconnectTimestamp = now();
this.eventEmitter.emit('peer-disconnected', { ...connectionEventParams });
});
});
}
}
private getExternalConnection = (id: string) => {
const externalConn = this.unassignedExternalConnections[id];
if (!externalConn) throw new Error(`Attempted to connect unknown external conn ${id}`);
delete this.unassignedExternalConnections[id];
return externalConn;
}
async createExternalOffer(options: OfferOptions = {}): Promise<MockRTCExternalOfferParams> {
if (this.debug) console.log(`Creating external peer offer for ${this.peerId}`);
const externalConn = new RTCConnection();
this.unassignedExternalConnections[externalConn.id] = externalConn;
this.trackConnection(externalConn);
return {
id: externalConn.id,
offer: await externalConn.sessionApi.createOffer(options),
session: externalConn.sessionApi,
setAnswer: async (answer: RTCSessionDescriptionInit) => {
if (this.debug) console.log(`Accepting answer for external peer offer for ${this.peerId}`);
externalConn.sessionApi.completeOffer(answer);
}
};
}
async answerExternalOffer(
offer: RTCSessionDescriptionInit,
options?: AnswerOptions
): Promise<MockRTCExternalAnswerParams> {
if (this.debug) console.log(`Answering offer with external peer for ${this.peerId}`);
const externalConn = new RTCConnection();
this.unassignedExternalConnections[externalConn.id] = externalConn;
this.trackConnection(externalConn);
return {
id: externalConn.id,
answer: await externalConn.sessionApi.answerOffer(offer, options),
session: externalConn.sessionApi
};
}
private createConnection() {
const conn = new MockRTCConnection(this.getExternalConnection);
this.trackConnection(conn);
this.handleConnection(conn).catch((error) => {
console.error("Error handling WebRTC connection:", error);
conn.close().catch(() => {});
});
if (this.options.recordMessages) {
const logChannelMessages = (channel: DataChannelStream) => {
const channelLabel = channel.label;
const messageLog = (this.messages[channelLabel] ??= []);
channel.on('read-data', d => {
messageLog.push(d);
});
};
conn.channels.forEach(logChannelMessages);
conn.on('channel-created', logChannelMessages);
}
return conn;
}
async createOffer(options: OfferOptions = {}): Promise<MockRTCOfferParams> {
if (this.debug) console.log(`Creating mock offer for ${this.peerId}`);
const conn = this.createConnection();
return {
offer: await conn.sessionApi.createOffer(options),
session: conn.sessionApi,
setAnswer: async (answer) => {
conn.sessionApi.completeOffer(answer);
}
}
}
async answerOffer(offer: RTCSessionDescriptionInit, options: AnswerOptions = {}): Promise<MockRTCAnswerParams> {
if (this.debug) console.log(`Answering offer for mocking for ${this.peerId}`);
const conn = this.createConnection();
return {
answer: await conn.sessionApi.answerOffer(offer, options),
session: conn.sessionApi
};
}
getSession(id: string): MockRTCSession {
return this.connections[id].sessionApi;
}
private async handleConnection(conn: MockRTCConnection) {
await conn.waitUntilConnected();
const handlerSteps = await this.getHandlerSteps(conn);
for (const step of handlerSteps) {
await step.handle(conn);
}
await conn.close();
}
async close() {
await Promise.all(
Object.values(this.connections).map(c =>
c.close()
)
);
}
private messages: { [channelName: string]: Array<string | Buffer> } = {};
async getAllMessages() {
if (!this.options.recordMessages) {
throw new Error("Can't query messages, as recordMessages was not enabled");
}
return Object.values(this.messages).flat();
}
async getMessagesOnChannel(channelName: string) {
if (!this.options.recordMessages) {
throw new Error("Can't query messages, as recordMessages was not enabled");
}
return this.messages[channelName].flat();
}
}