@voltkit-rtc/client 0.7.3 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/README.md +244 -2
- package/dist/cjs/adaptive.d.ts +104 -0
- package/dist/cjs/adaptive.d.ts.map +1 -0
- package/dist/cjs/adaptive.js +263 -0
- package/dist/cjs/adaptive.js.map +1 -0
- package/dist/cjs/devices.d.ts +42 -0
- package/dist/cjs/devices.d.ts.map +1 -0
- package/dist/cjs/devices.js +56 -0
- package/dist/cjs/devices.js.map +1 -0
- package/dist/cjs/diagnostics.d.ts +65 -0
- package/dist/cjs/diagnostics.d.ts.map +1 -0
- package/dist/cjs/diagnostics.js +69 -0
- package/dist/cjs/diagnostics.js.map +1 -0
- package/dist/cjs/fallback.d.ts +81 -0
- package/dist/cjs/fallback.d.ts.map +1 -0
- package/dist/cjs/fallback.js +148 -0
- package/dist/cjs/fallback.js.map +1 -0
- package/dist/cjs/health.d.ts +70 -0
- package/dist/cjs/health.d.ts.map +1 -0
- package/dist/cjs/health.js +116 -0
- package/dist/cjs/health.js.map +1 -0
- package/dist/cjs/index.d.ts +339 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1098 -26
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/link.d.ts +99 -0
- package/dist/cjs/link.d.ts.map +1 -0
- package/dist/cjs/link.js +219 -0
- package/dist/cjs/link.js.map +1 -0
- package/dist/cjs/preflight.d.ts +79 -0
- package/dist/cjs/preflight.d.ts.map +1 -0
- package/dist/cjs/preflight.js +159 -0
- package/dist/cjs/preflight.js.map +1 -0
- package/dist/cjs/speakers.d.ts +34 -0
- package/dist/cjs/speakers.d.ts.map +1 -0
- package/dist/cjs/speakers.js +71 -0
- package/dist/cjs/speakers.js.map +1 -0
- package/dist/esm/adaptive.d.ts +104 -0
- package/dist/esm/adaptive.d.ts.map +1 -0
- package/dist/esm/adaptive.js +256 -0
- package/dist/esm/adaptive.js.map +1 -0
- package/dist/esm/devices.d.ts +42 -0
- package/dist/esm/devices.d.ts.map +1 -0
- package/dist/esm/devices.js +50 -0
- package/dist/esm/devices.js.map +1 -0
- package/dist/esm/diagnostics.d.ts +65 -0
- package/dist/esm/diagnostics.d.ts.map +1 -0
- package/dist/esm/diagnostics.js +65 -0
- package/dist/esm/diagnostics.js.map +1 -0
- package/dist/esm/fallback.d.ts +81 -0
- package/dist/esm/fallback.d.ts.map +1 -0
- package/dist/esm/fallback.js +142 -0
- package/dist/esm/fallback.js.map +1 -0
- package/dist/esm/health.d.ts +70 -0
- package/dist/esm/health.d.ts.map +1 -0
- package/dist/esm/health.js +111 -0
- package/dist/esm/health.js.map +1 -0
- package/dist/esm/index.d.ts +339 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1096 -26
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/link.d.ts +99 -0
- package/dist/esm/link.d.ts.map +1 -0
- package/dist/esm/link.js +214 -0
- package/dist/esm/link.js.map +1 -0
- package/dist/esm/preflight.d.ts +79 -0
- package/dist/esm/preflight.d.ts.map +1 -0
- package/dist/esm/preflight.js +153 -0
- package/dist/esm/preflight.js.map +1 -0
- package/dist/esm/speakers.d.ts +34 -0
- package/dist/esm/speakers.d.ts.map +1 -0
- package/dist/esm/speakers.js +67 -0
- package/dist/esm/speakers.js.map +1 -0
- package/package.json +1 -1
- package/src/adaptive.ts +295 -0
- package/src/devices.ts +70 -0
- package/src/diagnostics.ts +94 -0
- package/src/fallback.ts +169 -0
- package/src/health.ts +127 -0
- package/src/index.ts +1097 -27
- package/src/link.ts +255 -0
- package/src/preflight.ts +191 -0
- package/src/speakers.ts +66 -0
package/dist/cjs/link.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Keeping the connection alive between drops (docs/implementation-progress/RTC_PERF_PROGRESS.md G1, G2).
|
|
4
|
+
*
|
|
5
|
+
* Two failures the socket's own close never reports:
|
|
6
|
+
*
|
|
7
|
+
* - **The media path dies while the socket lives** (Wi-Fi to mobile data, a NAT rebinding, UDP blocked mid-call).
|
|
8
|
+
* The video freezes for good. Twilio and LiveKit restart ICE on the same transports first -- new ICE credentials
|
|
9
|
+
* from the server, the same producers and consumers -- and rejoin only when that does not bring the path back.
|
|
10
|
+
* {@link MediaPathWatch} does the same: a transport `disconnected` for {@link DISCONNECTED_GRACE_MS} or `failed`
|
|
11
|
+
* restarts ICE; not `connected` within {@link RESTART_TIMEOUT_MS} of starting recovery (including the request), or a server without `restartIce`, is a
|
|
12
|
+
* full reconnect.
|
|
13
|
+
* - **The socket dies silently**: the network went away and TCP has not noticed yet, which can take minutes. The
|
|
14
|
+
* server pings, but a browser cannot see those. LiveKit pings from the client and reconnects when no pong comes.
|
|
15
|
+
* {@link SocketLiveness} sends `ping` after {@link IDLE_MS} with nothing received, and calls the socket dead when
|
|
16
|
+
* nothing at all arrives within {@link PING_TIMEOUT_MS} of it. Any frame counts, so an older server's
|
|
17
|
+
* `unknown-method` reply proves the socket as well as a pong.
|
|
18
|
+
*
|
|
19
|
+
* Pure: timers and the clock are injectable, so the decisions are unit-tested on their own.
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.SocketLiveness = exports.MediaPathWatch = exports.LIVENESS_TICK_MS = exports.PING_TIMEOUT_MS = exports.IDLE_MS = exports.RESTART_TIMEOUT_MS = exports.DISCONNECTED_GRACE_MS = void 0;
|
|
23
|
+
/** A transport `disconnected` this long is restarted (it often recovers by itself within a second or two). */
|
|
24
|
+
exports.DISCONNECTED_GRACE_MS = 3000;
|
|
25
|
+
/** The whole ICE recovery, including the request, has this long before the session rejoins. */
|
|
26
|
+
exports.RESTART_TIMEOUT_MS = 10_000;
|
|
27
|
+
/** With nothing received for this long, the client pings. */
|
|
28
|
+
exports.IDLE_MS = 15_000;
|
|
29
|
+
/** A ping with nothing at all received for this long after it: the socket is dead. */
|
|
30
|
+
exports.PING_TIMEOUT_MS = 8000;
|
|
31
|
+
/** How often liveness is checked. Background tabs may run it less often; the decisions use the clock, not counts. */
|
|
32
|
+
exports.LIVENESS_TICK_MS = 5000;
|
|
33
|
+
/** Watches each media transport's connection state and recovers a dead path. */
|
|
34
|
+
class MediaPathWatch {
|
|
35
|
+
host;
|
|
36
|
+
entries = new Map();
|
|
37
|
+
setTimer;
|
|
38
|
+
clearTimer;
|
|
39
|
+
constructor(host) {
|
|
40
|
+
this.host = host;
|
|
41
|
+
this.setTimer = host.setTimer ?? ((fn, ms) => setTimeout(fn, ms));
|
|
42
|
+
this.clearTimer = host.clearTimer ?? ((h) => clearTimeout(h));
|
|
43
|
+
}
|
|
44
|
+
/** A transport's connection state changed (mediasoup-client `connectionstatechange`). */
|
|
45
|
+
update(transportId, state) {
|
|
46
|
+
let entry = this.entries.get(transportId);
|
|
47
|
+
if (!entry) {
|
|
48
|
+
entry = { state, timer: null, restarting: false, lost: false, revision: 0 };
|
|
49
|
+
this.entries.set(transportId, entry);
|
|
50
|
+
}
|
|
51
|
+
entry.state = state;
|
|
52
|
+
switch (state) {
|
|
53
|
+
case 'connected':
|
|
54
|
+
entry.revision++;
|
|
55
|
+
this.cancel(entry);
|
|
56
|
+
entry.restarting = false;
|
|
57
|
+
this.setLost(transportId, entry, false);
|
|
58
|
+
return;
|
|
59
|
+
case 'closed':
|
|
60
|
+
this.forget(transportId);
|
|
61
|
+
return;
|
|
62
|
+
case 'disconnected':
|
|
63
|
+
// A restart is already waiting for `connected`; its own deadline decides.
|
|
64
|
+
if (entry.restarting || entry.timer !== null)
|
|
65
|
+
return;
|
|
66
|
+
entry.timer = this.setTimer(() => {
|
|
67
|
+
entry.timer = null;
|
|
68
|
+
if (entry.state === 'disconnected' || entry.state === 'failed')
|
|
69
|
+
void this.restart(transportId, entry);
|
|
70
|
+
}, exports.DISCONNECTED_GRACE_MS);
|
|
71
|
+
return;
|
|
72
|
+
case 'failed':
|
|
73
|
+
if (entry.restarting)
|
|
74
|
+
return; // duplicate notification; the deadline decides
|
|
75
|
+
this.cancel(entry);
|
|
76
|
+
void this.restart(transportId, entry);
|
|
77
|
+
return;
|
|
78
|
+
default:
|
|
79
|
+
// new / connecting: a fresh transport, or one mid-restart.
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** The transport went away with its session (teardown): no more decisions for it. */
|
|
84
|
+
forget(transportId) {
|
|
85
|
+
const entry = this.entries.get(transportId);
|
|
86
|
+
if (!entry)
|
|
87
|
+
return;
|
|
88
|
+
this.cancel(entry);
|
|
89
|
+
this.entries.delete(transportId);
|
|
90
|
+
if (entry.lost)
|
|
91
|
+
this.host.onPathChange?.(transportId, false);
|
|
92
|
+
}
|
|
93
|
+
/** Every transport went away (teardown, close). */
|
|
94
|
+
reset() {
|
|
95
|
+
for (const id of [...this.entries.keys()])
|
|
96
|
+
this.forget(id);
|
|
97
|
+
}
|
|
98
|
+
/** For tests and the network-change nudge: is this transport being recovered now? */
|
|
99
|
+
restartingNow(transportId) {
|
|
100
|
+
return this.entries.get(transportId)?.restarting === true;
|
|
101
|
+
}
|
|
102
|
+
async restart(transportId, entry) {
|
|
103
|
+
if (!this.host.active() || this.entries.get(transportId) !== entry)
|
|
104
|
+
return;
|
|
105
|
+
entry.restarting = true;
|
|
106
|
+
const revision = ++entry.revision;
|
|
107
|
+
// Include both the RPC and the platform call in the deadline: neither is
|
|
108
|
+
// guaranteed to settle when a network/interface disappears.
|
|
109
|
+
this.cancel(entry);
|
|
110
|
+
entry.timer = this.setTimer(() => {
|
|
111
|
+
entry.timer = null;
|
|
112
|
+
if (!entry.restarting || this.entries.get(transportId) !== entry)
|
|
113
|
+
return;
|
|
114
|
+
entry.revision++;
|
|
115
|
+
entry.restarting = false;
|
|
116
|
+
if (this.host.active())
|
|
117
|
+
this.host.reconnect('media-timeout');
|
|
118
|
+
}, exports.RESTART_TIMEOUT_MS);
|
|
119
|
+
this.setLost(transportId, entry, true);
|
|
120
|
+
let outcome;
|
|
121
|
+
try {
|
|
122
|
+
outcome = await this.host.restartIce(transportId);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
outcome = 'unsupported';
|
|
126
|
+
}
|
|
127
|
+
if (this.entries.get(transportId) !== entry || entry.revision !== revision)
|
|
128
|
+
return;
|
|
129
|
+
if (outcome === 'skipped') {
|
|
130
|
+
this.cancel(entry);
|
|
131
|
+
entry.restarting = false;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (outcome === 'unsupported') {
|
|
135
|
+
this.cancel(entry);
|
|
136
|
+
entry.restarting = false;
|
|
137
|
+
if (this.host.active())
|
|
138
|
+
this.host.reconnect('media-failed');
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// `connected` clears the deadline, including when it arrives during the await.
|
|
142
|
+
}
|
|
143
|
+
setLost(transportId, entry, lost) {
|
|
144
|
+
if (entry.lost === lost)
|
|
145
|
+
return;
|
|
146
|
+
entry.lost = lost;
|
|
147
|
+
this.host.onPathChange?.(transportId, lost);
|
|
148
|
+
}
|
|
149
|
+
cancel(entry) {
|
|
150
|
+
if (entry.timer !== null)
|
|
151
|
+
this.clearTimer(entry.timer);
|
|
152
|
+
entry.timer = null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.MediaPathWatch = MediaPathWatch;
|
|
156
|
+
/** Notices a socket that died without closing. */
|
|
157
|
+
class SocketLiveness {
|
|
158
|
+
host;
|
|
159
|
+
lastReceived = 0;
|
|
160
|
+
pingSentAt = null;
|
|
161
|
+
timer = null;
|
|
162
|
+
now;
|
|
163
|
+
constructor(host) {
|
|
164
|
+
this.host = host;
|
|
165
|
+
this.now = host.now ?? Date.now;
|
|
166
|
+
}
|
|
167
|
+
/** Start watching a newly opened socket. */
|
|
168
|
+
start() {
|
|
169
|
+
this.stop();
|
|
170
|
+
this.lastReceived = this.now();
|
|
171
|
+
this.pingSentAt = null;
|
|
172
|
+
const every = this.host.setInterval ?? ((fn, ms) => setInterval(fn, ms));
|
|
173
|
+
this.timer = every(() => this.check(), exports.LIVENESS_TICK_MS);
|
|
174
|
+
// A Node/SSR process with no other work need not stay alive for a browser
|
|
175
|
+
// connection watch. Browser timers are numbers and have no unref.
|
|
176
|
+
this.timer?.unref?.();
|
|
177
|
+
}
|
|
178
|
+
stop() {
|
|
179
|
+
if (this.timer !== null)
|
|
180
|
+
(this.host.clearInterval ?? ((h) => clearInterval(h)))(this.timer);
|
|
181
|
+
this.timer = null;
|
|
182
|
+
this.pingSentAt = null;
|
|
183
|
+
}
|
|
184
|
+
get running() {
|
|
185
|
+
return this.timer !== null;
|
|
186
|
+
}
|
|
187
|
+
/** Any frame arrived on the socket. */
|
|
188
|
+
received() {
|
|
189
|
+
this.lastReceived = this.now();
|
|
190
|
+
this.pingSentAt = null;
|
|
191
|
+
}
|
|
192
|
+
/** Check now instead of at the next tick (the browser said the network changed). */
|
|
193
|
+
checkNow() {
|
|
194
|
+
if (this.timer === null || !this.host.active())
|
|
195
|
+
return;
|
|
196
|
+
if (this.pingSentAt === null) {
|
|
197
|
+
this.pingSentAt = this.now();
|
|
198
|
+
this.host.ping();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
check() {
|
|
202
|
+
if (this.timer === null || !this.host.active())
|
|
203
|
+
return;
|
|
204
|
+
const now = this.now();
|
|
205
|
+
if (this.pingSentAt !== null) {
|
|
206
|
+
if (now - this.pingSentAt >= exports.PING_TIMEOUT_MS) {
|
|
207
|
+
this.stop();
|
|
208
|
+
this.host.dead();
|
|
209
|
+
}
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (now - this.lastReceived >= exports.IDLE_MS) {
|
|
213
|
+
this.pingSentAt = now;
|
|
214
|
+
this.host.ping();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.SocketLiveness = SocketLiveness;
|
|
219
|
+
//# sourceMappingURL=link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CA8DC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pre-call check (docs/implementation-progress/RTC_PERF_PROGRESS.md L1).
|
|
3
|
+
*
|
|
4
|
+
* - Twilio's `runPreflight` (read from its code) joins a test room, publishes synthetic audio and video and reports
|
|
5
|
+
* timings, the connection path, round trip, jitter and loss.
|
|
6
|
+
* - LiveKit's `ConnectionCheck` runs separate checks -- WebSocket, WebRTC, TURN, publishing audio and video, UDP
|
|
7
|
+
* against TCP -- each with messages and warnings.
|
|
8
|
+
* - Agora's `startLastmileProbeTest` measures the last mile before a call.
|
|
9
|
+
*
|
|
10
|
+
* This joins the room the token names (mint one for a test room nobody else is in: the session is real, and billed
|
|
11
|
+
* like any other for its few seconds), publishes what it is given -- or a tone and a picture it makes -- and reports
|
|
12
|
+
* what it saw. The orchestration takes a small target interface, so it is tested without a browser.
|
|
13
|
+
*/
|
|
14
|
+
import { type NetworkQualityLevel } from './network.js';
|
|
15
|
+
import type { StatsEntry } from './stats.js';
|
|
16
|
+
import type { MediaWarningCode } from './health.js';
|
|
17
|
+
export interface PreflightOptions {
|
|
18
|
+
/** What to publish (cloned; your tracks are left running). Without it, a tone and a moving picture are made. */
|
|
19
|
+
media?: MediaStream;
|
|
20
|
+
/** How long media is measured once it flows. Default 5 s. */
|
|
21
|
+
durationMs?: number;
|
|
22
|
+
}
|
|
23
|
+
export type PreflightStepName = 'join' | 'media';
|
|
24
|
+
export interface PreflightStep {
|
|
25
|
+
name: PreflightStepName;
|
|
26
|
+
ok: boolean;
|
|
27
|
+
/** How long it took, in ms. */
|
|
28
|
+
ms: number | null;
|
|
29
|
+
error?: string;
|
|
30
|
+
}
|
|
31
|
+
export interface PreflightReport {
|
|
32
|
+
/** Joined, and media reached the server. */
|
|
33
|
+
ok: boolean;
|
|
34
|
+
steps: PreflightStep[];
|
|
35
|
+
/** How media travelled: UDP is best; TCP or a relay means a firewall in the way (it works, with more delay). */
|
|
36
|
+
path: {
|
|
37
|
+
protocol: 'udp' | 'tcp' | null;
|
|
38
|
+
relayed: boolean;
|
|
39
|
+
} | null;
|
|
40
|
+
rttMs: number | null;
|
|
41
|
+
lossPct: number | null;
|
|
42
|
+
/** The upload the browser estimated it could use, in kbps. */
|
|
43
|
+
uploadKbps: number | null;
|
|
44
|
+
level: NetworkQualityLevel | null;
|
|
45
|
+
/** Camera and microphone problems seen during the test (see the mediaWarning event). */
|
|
46
|
+
warnings: MediaWarningCode[];
|
|
47
|
+
}
|
|
48
|
+
/** What a pre-call check needs from a client. */
|
|
49
|
+
export interface PreflightTarget {
|
|
50
|
+
connect(): Promise<unknown>;
|
|
51
|
+
publish(stream: MediaStream): Promise<void>;
|
|
52
|
+
/** The send transport's connection state, or null before there is one. */
|
|
53
|
+
sendState(): string | null;
|
|
54
|
+
/** The send transport's statistics, or null without one. */
|
|
55
|
+
sendStats(): Promise<Iterable<StatsEntry> | null>;
|
|
56
|
+
onWarning(listener: (code: MediaWarningCode, active: boolean) => void): () => void;
|
|
57
|
+
close(): void;
|
|
58
|
+
}
|
|
59
|
+
export interface PreflightEnv {
|
|
60
|
+
now(): number;
|
|
61
|
+
sleep(ms: number): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
/** Media has this long to connect once published. */
|
|
64
|
+
export declare const MEDIA_CONNECT_TIMEOUT_MS = 10000;
|
|
65
|
+
/** Statistics are read this often while measuring. */
|
|
66
|
+
export declare const PREFLIGHT_SAMPLE_MS = 1000;
|
|
67
|
+
/** The selected candidate pair's protocol, and whether either end is a relay. */
|
|
68
|
+
export declare function connectionPath(report: Iterable<StatsEntry>): PreflightReport['path'];
|
|
69
|
+
/** Run the check on `target`, closing it at the end whatever happens. */
|
|
70
|
+
export declare function runPreflight(target: PreflightTarget, stream: MediaStream, options?: PreflightOptions, env?: PreflightEnv): Promise<PreflightReport>;
|
|
71
|
+
/**
|
|
72
|
+
* A tone and a moving picture to publish when the app gives none (Twilio's preflight makes synthetic media too).
|
|
73
|
+
* Returns the stream and a function that stops everything it made. Browser only.
|
|
74
|
+
*/
|
|
75
|
+
export declare function syntheticMedia(): {
|
|
76
|
+
stream: MediaStream;
|
|
77
|
+
stop: () => void;
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=preflight.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preflight.d.ts","sourceRoot":"","sources":["../../src/preflight.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAwC,KAAK,mBAAmB,EAAwB,MAAM,cAAc,CAAC;AACpH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,WAAW,gBAAgB;IAC/B,gHAAgH;IAChH,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,6DAA6D;IAC7D,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,OAAO,CAAC;AAEjD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,iBAAiB,CAAC;IACxB,EAAE,EAAE,OAAO,CAAC;IACZ,+BAA+B;IAC/B,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,4CAA4C;IAC5C,EAAE,EAAE,OAAO,CAAC;IACZ,KAAK,EAAE,aAAa,EAAE,CAAC;IACvB,gHAAgH;IAChH,IAAI,EAAE;QAAE,QAAQ,EAAE,KAAK,GAAG,KAAK,GAAG,IAAI,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI,CAAC;IAClE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,8DAA8D;IAC9D,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC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-0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PREFLIGHT_SAMPLE_MS = exports.MEDIA_CONNECT_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.connectionPath = connectionPath;
|
|
5
|
+
exports.runPreflight = runPreflight;
|
|
6
|
+
exports.syntheticMedia = syntheticMedia;
|
|
7
|
+
/**
|
|
8
|
+
* Pre-call check (docs/implementation-progress/RTC_PERF_PROGRESS.md L1).
|
|
9
|
+
*
|
|
10
|
+
* - Twilio's `runPreflight` (read from its code) joins a test room, publishes synthetic audio and video and reports
|
|
11
|
+
* timings, the connection path, round trip, jitter and loss.
|
|
12
|
+
* - LiveKit's `ConnectionCheck` runs separate checks -- WebSocket, WebRTC, TURN, publishing audio and video, UDP
|
|
13
|
+
* against TCP -- each with messages and warnings.
|
|
14
|
+
* - Agora's `startLastmileProbeTest` measures the last mile before a call.
|
|
15
|
+
*
|
|
16
|
+
* This joins the room the token names (mint one for a test room nobody else is in: the session is real, and billed
|
|
17
|
+
* like any other for its few seconds), publishes what it is given -- or a tone and a picture it makes -- and reports
|
|
18
|
+
* what it saw. The orchestration takes a small target interface, so it is tested without a browser.
|
|
19
|
+
*/
|
|
20
|
+
const network_js_1 = require("./network.js");
|
|
21
|
+
const realEnv = { now: () => Date.now(), sleep: (ms) => new Promise((r) => setTimeout(r, ms)) };
|
|
22
|
+
/** Media has this long to connect once published. */
|
|
23
|
+
exports.MEDIA_CONNECT_TIMEOUT_MS = 10_000;
|
|
24
|
+
/** Statistics are read this often while measuring. */
|
|
25
|
+
exports.PREFLIGHT_SAMPLE_MS = 1000;
|
|
26
|
+
/** The selected candidate pair's protocol, and whether either end is a relay. */
|
|
27
|
+
function connectionPath(report) {
|
|
28
|
+
const entries = [...report];
|
|
29
|
+
const byId = new Map();
|
|
30
|
+
for (const e of entries)
|
|
31
|
+
if (typeof e.id === 'string')
|
|
32
|
+
byId.set(e.id, e);
|
|
33
|
+
let pair;
|
|
34
|
+
for (const t of entries.filter((e) => e.type === 'transport')) {
|
|
35
|
+
if (typeof t.selectedCandidatePairId === 'string')
|
|
36
|
+
pair = byId.get(t.selectedCandidatePairId);
|
|
37
|
+
if (pair)
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
pair ??= entries.find((e) => e.type === 'candidate-pair' && e.nominated === true && e.state === 'succeeded');
|
|
41
|
+
if (!pair)
|
|
42
|
+
return null;
|
|
43
|
+
const local = typeof pair.localCandidateId === 'string' ? byId.get(pair.localCandidateId) : undefined;
|
|
44
|
+
const remote = typeof pair.remoteCandidateId === 'string' ? byId.get(pair.remoteCandidateId) : undefined;
|
|
45
|
+
const protocol = local?.protocol === 'udp' || local?.protocol === 'tcp' ? local.protocol : null;
|
|
46
|
+
const relayed = local?.candidateType === 'relay' || remote?.candidateType === 'relay';
|
|
47
|
+
return { protocol, relayed };
|
|
48
|
+
}
|
|
49
|
+
/** Run the check on `target`, closing it at the end whatever happens. */
|
|
50
|
+
async function runPreflight(target, stream, options = {}, env = realEnv) {
|
|
51
|
+
const report = { ok: false, steps: [], path: null, rttMs: null, lossPct: null, uploadKbps: null, level: null, warnings: [] };
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
const stopWarnings = target.onWarning((code, active) => { if (active)
|
|
54
|
+
seen.add(code); });
|
|
55
|
+
try {
|
|
56
|
+
let started = env.now();
|
|
57
|
+
try {
|
|
58
|
+
await target.connect();
|
|
59
|
+
report.steps.push({ name: 'join', ok: true, ms: env.now() - started });
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
report.steps.push({ name: 'join', ok: false, ms: null, error: String(err?.message ?? err) });
|
|
63
|
+
return report;
|
|
64
|
+
}
|
|
65
|
+
started = env.now();
|
|
66
|
+
try {
|
|
67
|
+
await target.publish(stream);
|
|
68
|
+
while (target.sendState() !== 'connected') {
|
|
69
|
+
const state = target.sendState();
|
|
70
|
+
if (state === 'failed' || state === 'closed')
|
|
71
|
+
throw new Error(`media connection ${state}`);
|
|
72
|
+
if (env.now() - started > exports.MEDIA_CONNECT_TIMEOUT_MS)
|
|
73
|
+
throw new Error('media did not connect in time (a firewall blocking UDP and TCP?)');
|
|
74
|
+
await env.sleep(100);
|
|
75
|
+
}
|
|
76
|
+
report.steps.push({ name: 'media', ok: true, ms: env.now() - started });
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
report.steps.push({ name: 'media', ok: false, ms: null, error: String(err?.message ?? err) });
|
|
80
|
+
return report;
|
|
81
|
+
}
|
|
82
|
+
// Measure: the worst round trip and loss over the run, the last upload estimate.
|
|
83
|
+
const duration = options.durationMs ?? 5000;
|
|
84
|
+
let previous = new Map();
|
|
85
|
+
const until = env.now() + duration;
|
|
86
|
+
while (env.now() < until) {
|
|
87
|
+
await env.sleep(exports.PREFLIGHT_SAMPLE_MS);
|
|
88
|
+
const stats = await target.sendStats().catch(() => null);
|
|
89
|
+
if (!stats)
|
|
90
|
+
continue;
|
|
91
|
+
const entries = [...stats];
|
|
92
|
+
report.path = connectionPath(entries) ?? report.path;
|
|
93
|
+
const now = new Map([['send', (0, network_js_1.readTransportStats)(entries)]]);
|
|
94
|
+
const q = (0, network_js_1.networkQualityOf)(now, previous, true);
|
|
95
|
+
previous = now;
|
|
96
|
+
if (q.rttMs !== null)
|
|
97
|
+
report.rttMs = Math.max(report.rttMs ?? 0, q.rttMs);
|
|
98
|
+
if (q.lossPct !== null)
|
|
99
|
+
report.lossPct = Math.max(report.lossPct ?? 0, q.lossPct);
|
|
100
|
+
if (q.availableKbps !== null)
|
|
101
|
+
report.uploadKbps = q.availableKbps;
|
|
102
|
+
report.level = q.level;
|
|
103
|
+
}
|
|
104
|
+
report.ok = true;
|
|
105
|
+
return report;
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
report.warnings = [...seen];
|
|
109
|
+
stopWarnings();
|
|
110
|
+
target.close();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A tone and a moving picture to publish when the app gives none (Twilio's preflight makes synthetic media too).
|
|
115
|
+
* Returns the stream and a function that stops everything it made. Browser only.
|
|
116
|
+
*/
|
|
117
|
+
function syntheticMedia() {
|
|
118
|
+
const tracks = [];
|
|
119
|
+
const stops = [];
|
|
120
|
+
const AudioCtx = globalThis.AudioContext;
|
|
121
|
+
if (AudioCtx) {
|
|
122
|
+
const ctx = new AudioCtx();
|
|
123
|
+
const osc = ctx.createOscillator();
|
|
124
|
+
const gain = ctx.createGain();
|
|
125
|
+
gain.gain.value = 0.05;
|
|
126
|
+
const dest = ctx.createMediaStreamDestination();
|
|
127
|
+
osc.connect(gain).connect(dest);
|
|
128
|
+
osc.start();
|
|
129
|
+
tracks.push(...dest.stream.getAudioTracks());
|
|
130
|
+
stops.push(() => { osc.stop(); void ctx.close(); });
|
|
131
|
+
}
|
|
132
|
+
if (typeof document !== 'undefined') {
|
|
133
|
+
const canvas = document.createElement('canvas');
|
|
134
|
+
canvas.width = 640;
|
|
135
|
+
canvas.height = 360;
|
|
136
|
+
const g = canvas.getContext('2d');
|
|
137
|
+
let frame = 0;
|
|
138
|
+
const draw = () => {
|
|
139
|
+
if (!g)
|
|
140
|
+
return;
|
|
141
|
+
frame += 1;
|
|
142
|
+
g.fillStyle = `hsl(${frame % 360}, 60%, 40%)`;
|
|
143
|
+
g.fillRect(0, 0, canvas.width, canvas.height);
|
|
144
|
+
g.fillStyle = '#fff';
|
|
145
|
+
g.fillRect((frame * 8) % canvas.width, 150, 60, 60);
|
|
146
|
+
};
|
|
147
|
+
draw();
|
|
148
|
+
const timer = setInterval(draw, 1000 / 30);
|
|
149
|
+
const captured = canvas.captureStream?.(30);
|
|
150
|
+
if (captured)
|
|
151
|
+
tracks.push(...captured.getVideoTracks());
|
|
152
|
+
stops.push(() => clearInterval(timer));
|
|
153
|
+
}
|
|
154
|
+
const stream = new MediaStream(tracks);
|
|
155
|
+
return { stream, stop: () => { for (const t of tracks)
|
|
156
|
+
t.stop(); for (const s of stops)
|
|
157
|
+
s(); } };
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=preflight.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preflight.js","sourceRoot":"","sources":["../../src/preflight.ts"],"names":[],"mappings":";;;AA0EA,wCAgBC;AAGD,oCAsDC;AAMD,wCAqCC;AA9LD;;;;;;;;;;;;GAYG;AACH,6CAAoH;AAqDpH,MAAM,OAAO,GAAiB,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC;AAE9G,qDAAqD;AACxC,QAAA,wBAAwB,GAAG,MAAM,CAAC;AAC/C,sDAAsD;AACzC,QAAA,mBAAmB,GAAG,IAAI,CAAC;AAExC,iFAAiF;AACjF,SAAgB,cAAc,CAAC,MAA4B;IACzD,MAAM,OAAO,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;IAC5B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAsB,CAAC;IAC3C,KAAK,MAAM,CAAC,IAAI,OAAO;QAAE,IAAI,OAAO,CAAC,CAAC,EAAE,KAAK,QAAQ;YAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IACzE,IAAI,IAA4B,CAAC;IACjC,KAAK,MAAM,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,WAAW,CAAC,EAAE,CAAC;QAC9D,IAAI,OAAO,CAAC,CAAC,uBAAuB,KAAK,QAAQ;YAAE,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,uBAAuB,CAAC,CAAC;QAC9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C;QACtD,CAAC,CAAC;QACF,IAAI,EAAE,CAAC;QACP,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;QAC3C,MAAM,QAAQ,GAAI,MAA+E,CAAC,aAAa,EAAE,CAAC,EAAE,CAAC,CAAC;QACtH,IAAI,QAAQ;YAAE,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,cAAc,EAAE,CAAC,CAAC;QACxD,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;IACzC,CAAC;IACD,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAC;IACvC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,KAAK,MAAM,CAAC,IAAI,MAAM;YAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,IAAI,KAAK;YAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AACnG,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audio levels and active speakers (docs/implementation-progress/RTC_PERF_PROGRESS.md J1).
|
|
3
|
+
*
|
|
4
|
+
* - Agora `volume-indicator` and Tencent TRTC `AUDIO_VOLUME` report every participant's volume on an interval.
|
|
5
|
+
* - Amazon Chime's `DefaultActiveSpeakerPolicy` (read from its code) ranks speakers: a volume above a silence
|
|
6
|
+
* threshold counts as speaking (1, else 0); each sample the speaker's score becomes `score × 0.9 + speaking × 0.1`,
|
|
7
|
+
* and everyone else's drops by `0.2 × speaking` (the takeover); scores under 0.01 are 0. Its detector updates every
|
|
8
|
+
* 200 ms. Sampled together here, so two people speaking at once both stay; the takeover applies to the silent.
|
|
9
|
+
* - LiveKit's server counts a participant as speaking above -35 dBov.
|
|
10
|
+
*
|
|
11
|
+
* Levels are WebRTC's `audioLevel`: linear, 0 = silence, 1 = 0 dBov. -35 dBov is 10^(-35/20) ≈ 0.0178.
|
|
12
|
+
* Pure: the ranking takes levels and gives the order.
|
|
13
|
+
*/
|
|
14
|
+
/** Above this level a participant is speaking (LiveKit's -35 dBov). */
|
|
15
|
+
export declare const SPEAKING_LEVEL = 0.0178;
|
|
16
|
+
/** How often levels are read while someone listens (Chime's detector: 200 ms). */
|
|
17
|
+
export declare const AUDIO_LEVEL_INTERVAL_MS = 250;
|
|
18
|
+
export declare const SPEAKER_WEIGHT = 0.9;
|
|
19
|
+
export declare const TAKEOVER_RATE = 0.2;
|
|
20
|
+
export declare const SCORE_CUTOFF = 0.01;
|
|
21
|
+
/** Chime's active speaker ranking: who has been speaking recently, the most recent and steady first. */
|
|
22
|
+
export declare class ActiveSpeakers {
|
|
23
|
+
private readonly scores;
|
|
24
|
+
private last;
|
|
25
|
+
/**
|
|
26
|
+
* One sample: each identity's loudest level now (0..1). Identities missing from it count as silent. Returns the new
|
|
27
|
+
* order when it changed, else null.
|
|
28
|
+
*/
|
|
29
|
+
update(levels: ReadonlyMap<string, number>): string[] | null;
|
|
30
|
+
/** Someone left: forget them. Returns the new order when it changed. */
|
|
31
|
+
remove(identity: string): string[] | null;
|
|
32
|
+
reset(): void;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=speakers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"speakers.d.ts","sourceRoot":"","sources":["../../src/speakers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,uEAAuE;AACvE,eAAO,MAAM,cAAc,SAAS,CAAC;AACrC,kFAAkF;AAClF,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAE3C,eAAO,MAAM,cAAc,MAAM,CAAC;AAClC,eAAO,MAAM,aAAa,MAAM,CAAC;AACjC,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC,wGAAwG;AACxG,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6B;IACpD,OAAO,CAAC,IAAI,CAAgB;IAE5B;;;OAGG;IACH,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,EAAE,GAAG,IAAI;IAoB5D,wEAAwE;IACxE,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI;IAQzC,KAAK,IAAI,IAAI;CAId"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Audio levels and active speakers (docs/implementation-progress/RTC_PERF_PROGRESS.md J1).
|
|
4
|
+
*
|
|
5
|
+
* - Agora `volume-indicator` and Tencent TRTC `AUDIO_VOLUME` report every participant's volume on an interval.
|
|
6
|
+
* - Amazon Chime's `DefaultActiveSpeakerPolicy` (read from its code) ranks speakers: a volume above a silence
|
|
7
|
+
* threshold counts as speaking (1, else 0); each sample the speaker's score becomes `score × 0.9 + speaking × 0.1`,
|
|
8
|
+
* and everyone else's drops by `0.2 × speaking` (the takeover); scores under 0.01 are 0. Its detector updates every
|
|
9
|
+
* 200 ms. Sampled together here, so two people speaking at once both stay; the takeover applies to the silent.
|
|
10
|
+
* - LiveKit's server counts a participant as speaking above -35 dBov.
|
|
11
|
+
*
|
|
12
|
+
* Levels are WebRTC's `audioLevel`: linear, 0 = silence, 1 = 0 dBov. -35 dBov is 10^(-35/20) ≈ 0.0178.
|
|
13
|
+
* Pure: the ranking takes levels and gives the order.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.ActiveSpeakers = exports.SCORE_CUTOFF = exports.TAKEOVER_RATE = exports.SPEAKER_WEIGHT = exports.AUDIO_LEVEL_INTERVAL_MS = exports.SPEAKING_LEVEL = void 0;
|
|
17
|
+
/** Above this level a participant is speaking (LiveKit's -35 dBov). */
|
|
18
|
+
exports.SPEAKING_LEVEL = 0.0178;
|
|
19
|
+
/** How often levels are read while someone listens (Chime's detector: 200 ms). */
|
|
20
|
+
exports.AUDIO_LEVEL_INTERVAL_MS = 250;
|
|
21
|
+
exports.SPEAKER_WEIGHT = 0.9;
|
|
22
|
+
exports.TAKEOVER_RATE = 0.2;
|
|
23
|
+
exports.SCORE_CUTOFF = 0.01;
|
|
24
|
+
/** Chime's active speaker ranking: who has been speaking recently, the most recent and steady first. */
|
|
25
|
+
class ActiveSpeakers {
|
|
26
|
+
scores = new Map();
|
|
27
|
+
last = [];
|
|
28
|
+
/**
|
|
29
|
+
* One sample: each identity's loudest level now (0..1). Identities missing from it count as silent. Returns the new
|
|
30
|
+
* order when it changed, else null.
|
|
31
|
+
*/
|
|
32
|
+
update(levels) {
|
|
33
|
+
for (const id of levels.keys())
|
|
34
|
+
if (!this.scores.has(id))
|
|
35
|
+
this.scores.set(id, 0);
|
|
36
|
+
// Chime updates one attendee at a time as volumes arrive; here everyone is sampled together, so people speaking
|
|
37
|
+
// at the same moment do not take over from each other -- only the silent ones lose ground.
|
|
38
|
+
const speakingNow = new Set([...levels].filter(([, level]) => level > exports.SPEAKING_LEVEL).map(([id]) => id));
|
|
39
|
+
for (const id of this.scores.keys()) {
|
|
40
|
+
const speaking = speakingNow.has(id) ? 1 : 0;
|
|
41
|
+
let score = (this.scores.get(id) ?? 0) * exports.SPEAKER_WEIGHT + speaking * (1 - exports.SPEAKER_WEIGHT);
|
|
42
|
+
if (!speaking && speakingNow.size > 0)
|
|
43
|
+
score = Math.max(score - exports.TAKEOVER_RATE, 0);
|
|
44
|
+
this.scores.set(id, score);
|
|
45
|
+
}
|
|
46
|
+
const order = [...this.scores.entries()]
|
|
47
|
+
.filter(([, score]) => score >= exports.SCORE_CUTOFF)
|
|
48
|
+
.sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))
|
|
49
|
+
.map(([id]) => id);
|
|
50
|
+
if (order.length === this.last.length && order.every((id, i) => id === this.last[i]))
|
|
51
|
+
return null;
|
|
52
|
+
this.last = order;
|
|
53
|
+
return order;
|
|
54
|
+
}
|
|
55
|
+
/** Someone left: forget them. Returns the new order when it changed. */
|
|
56
|
+
remove(identity) {
|
|
57
|
+
if (!this.scores.delete(identity))
|
|
58
|
+
return null;
|
|
59
|
+
const order = this.last.filter((id) => id !== identity);
|
|
60
|
+
if (order.length === this.last.length)
|
|
61
|
+
return null;
|
|
62
|
+
this.last = order;
|
|
63
|
+
return order;
|
|
64
|
+
}
|
|
65
|
+
reset() {
|
|
66
|
+
this.scores.clear();
|
|
67
|
+
this.last = [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.ActiveSpeakers = ActiveSpeakers;
|
|
71
|
+
//# sourceMappingURL=speakers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"speakers.js","sourceRoot":"","sources":["../../src/speakers.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;AAEH,uEAAuE;AAC1D,QAAA,cAAc,GAAG,MAAM,CAAC;AACrC,kFAAkF;AACrE,QAAA,uBAAuB,GAAG,GAAG,CAAC;AAE9B,QAAA,cAAc,GAAG,GAAG,CAAC;AACrB,QAAA,aAAa,GAAG,GAAG,CAAC;AACpB,QAAA,YAAY,GAAG,IAAI,CAAC;AAEjC,wGAAwG;AACxG,MAAa,cAAc;IACR,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC5C,IAAI,GAAa,EAAE,CAAC;IAE5B;;;OAGG;IACH,MAAM,CAAC,MAAmC;QACxC,KAAK,MAAM,EAAE,IAAI,MAAM,CAAC,IAAI,EAAE;YAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;gBAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QACjF,gHAAgH;QAChH,2FAA2F;QAC3F,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,CAAC,C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