@fuzdev/fuz_app 0.178.0 → 0.180.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/dist/actions/CLAUDE.md +106 -0
- package/dist/actions/connection_registry.d.ts +18 -0
- package/dist/actions/connection_registry.d.ts.map +1 -1
- package/dist/actions/connection_registry.js +23 -0
- package/dist/actions/room_action_specs.d.ts +733 -0
- package/dist/actions/room_action_specs.d.ts.map +1 -0
- package/dist/actions/room_action_specs.js +506 -0
- package/dist/actions/room_actions.d.ts +276 -0
- package/dist/actions/room_actions.d.ts.map +1 -0
- package/dist/actions/room_actions.js +394 -0
- package/dist/actions/room_client.svelte.d.ts +399 -0
- package/dist/actions/room_client.svelte.d.ts.map +1 -0
- package/dist/actions/room_client.svelte.js +942 -0
- package/dist/actions/room_registry.d.ts +454 -0
- package/dist/actions/room_registry.d.ts.map +1 -0
- package/dist/actions/room_registry.js +927 -0
- package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
- package/dist/auth/role_grant_queries.d.ts +17 -0
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +30 -0
- package/dist/rtc/rtc_mesh.svelte.d.ts +102 -0
- package/dist/rtc/rtc_mesh.svelte.d.ts.map +1 -0
- package/dist/rtc/rtc_mesh.svelte.js +232 -0
- package/dist/rtc/rtc_peer.svelte.d.ts +180 -0
- package/dist/rtc/rtc_peer.svelte.d.ts.map +1 -0
- package/dist/rtc/rtc_peer.svelte.js +397 -0
- package/dist/rtc/rtc_signal.d.ts +74 -0
- package/dist/rtc/rtc_signal.d.ts.map +1 -0
- package/dist/rtc/rtc_signal.js +87 -0
- package/dist/testing/CLAUDE.md +67 -13
- package/dist/testing/cross_backend/action_manifest.d.ts +34 -2
- package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
- package/dist/testing/cross_backend/action_manifest.js +33 -6
- package/dist/testing/cross_backend/capabilities.d.ts +22 -4
- package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
- package/dist/testing/cross_backend/capabilities.js +4 -1
- package/dist/testing/cross_backend/full_spine_mount.d.ts +31 -6
- package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
- package/dist/testing/cross_backend/full_spine_mount.js +45 -13
- package/dist/testing/cross_backend/rooms_ws.d.ts +20 -0
- package/dist/testing/cross_backend/rooms_ws.d.ts.map +1 -0
- package/dist/testing/cross_backend/rooms_ws.js +534 -0
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +4 -1
- package/dist/testing/cross_backend/spine_suites.d.ts +5 -2
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +24 -5
- package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.js +2 -1
- package/dist/testing/cross_backend/test_room_authorize.d.ts +47 -0
- package/dist/testing/cross_backend/test_room_authorize.d.ts.map +1 -0
- package/dist/testing/cross_backend/test_room_authorize.js +58 -0
- package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_backdoor.js +10 -4
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +85 -1
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +57 -3
- package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/ts_spine_backend_config.js +5 -2
- package/dist/ui/AccountSessions.svelte.d.ts.map +1 -1
- package/dist/ui/AdminAccounts.svelte.d.ts.map +1 -1
- package/dist/ui/AdminAuditLog.svelte.d.ts.map +1 -1
- package/dist/ui/AdminInvites.svelte.d.ts.map +1 -1
- package/dist/ui/AdminOverview.svelte.d.ts.map +1 -1
- package/dist/ui/AdminRoleGrantHistory.svelte.d.ts.map +1 -1
- package/dist/ui/AdminSessions.svelte.d.ts.map +1 -1
- package/dist/ui/AdminSettings.svelte.d.ts.map +1 -1
- package/dist/ui/AdminSurface.svelte.d.ts.map +1 -1
- package/dist/ui/BootstrapForm.svelte.d.ts.map +1 -1
- package/dist/ui/ConfirmButton.svelte.d.ts.map +1 -1
- package/dist/ui/Datatable.svelte.d.ts.map +1 -1
- package/dist/ui/LoginForm.svelte.d.ts.map +1 -1
- package/dist/ui/LogoutButton.svelte.d.ts.map +1 -1
- package/dist/ui/OpenSignupToggle.svelte.d.ts.map +1 -1
- package/dist/ui/PopoverButton.svelte.d.ts.map +1 -1
- package/dist/ui/RoleGrantOfferForm.svelte.d.ts.map +1 -1
- package/dist/ui/RoleGrantOfferHistory.svelte.d.ts.map +1 -1
- package/dist/ui/RoleGrantOfferInbox.svelte.d.ts.map +1 -1
- package/dist/ui/SignupForm.svelte.d.ts.map +1 -1
- package/dist/ui/SurfaceExplorer.svelte.d.ts.map +1 -1
- package/package.json +19 -19
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One WebRTC connection to one remote member: `RtcPeer`, over an injected
|
|
3
|
+
* `RTCPeerConnection`, exchanging signals through a `send` function and an
|
|
4
|
+
* `on_signal` entry rather than any transport, so two peers cross-wire in a
|
|
5
|
+
* test and a room carries them in the app (`rtc/rtc_mesh.svelte.ts`).
|
|
6
|
+
*
|
|
7
|
+
* ## Perfect negotiation
|
|
8
|
+
*
|
|
9
|
+
* Either end may need to renegotiate at any time (an ICE restart, say), so the
|
|
10
|
+
* two follow the perfect negotiation pattern: each sends an offer whenever its
|
|
11
|
+
* connection says negotiation is needed, and when two offers cross, the
|
|
12
|
+
* **polite** end rolls its own back and answers the other's, while the
|
|
13
|
+
* **impolite** end ignores the offer it receives. Which end is polite is a
|
|
14
|
+
* pure function of the two member ids (`rtc_peer_is_polite`), so both agree
|
|
15
|
+
* without a word: the lower id is polite.
|
|
16
|
+
*
|
|
17
|
+
* Candidates that arrive before the remote description is set are queued and
|
|
18
|
+
* added once it is — a candidate is meaningless without one, and the
|
|
19
|
+
* connection refuses it. Candidates that arrive while the impolite end is
|
|
20
|
+
* ignoring an offer may belong to it and fail to add, queued or not; that
|
|
21
|
+
* failure is expected and dropped.
|
|
22
|
+
*
|
|
23
|
+
* Inbound signals are handled one at a time, in the order they arrive. A
|
|
24
|
+
* caller that delivers them over a channel that can reorder (concurrently
|
|
25
|
+
* dispatched requests) serializes its sends per peer, as `RtcMesh` does.
|
|
26
|
+
* Since no signal is handled while another's remote answer is being applied,
|
|
27
|
+
* the standard pattern's "setting a remote answer" flag would never read true
|
|
28
|
+
* here, and there is none.
|
|
29
|
+
*
|
|
30
|
+
* Once `close` is called, nothing more reaches the connection, and a failure
|
|
31
|
+
* of work in flight at the close is expected and not recorded.
|
|
32
|
+
*
|
|
33
|
+
* ## Fixed transceiver slots
|
|
34
|
+
*
|
|
35
|
+
* The media a peer carries is a fixed list of slots (`RtcSlot`), each one
|
|
36
|
+
* `sendrecv` transceiver of one kind: `RTC_VOICE_SLOTS` is the microphone's
|
|
37
|
+
* audio; camera, screen, and screen audio are further slots of the same shape,
|
|
38
|
+
* added to the list when they land. Every slot exists from the start, so
|
|
39
|
+
* turning a source on or off is `set_track` — a `replaceTrack` on the slot's
|
|
40
|
+
* sender, with no renegotiation — and a slot with no track sends nothing.
|
|
41
|
+
*
|
|
42
|
+
* The impolite end creates the transceivers, in slot order, and its first
|
|
43
|
+
* offer carries them; the polite end adopts the transceivers that offer
|
|
44
|
+
* creates on its side, by position, sets them `sendrecv`, and answers. Both
|
|
45
|
+
* ends creating their own would give each side two transceivers per slot,
|
|
46
|
+
* since a transceiver made by `addTransceiver` is never matched to a remote
|
|
47
|
+
* offer's media section. So both ends must use the same slot list.
|
|
48
|
+
*
|
|
49
|
+
* Two ends whose lists differ degrade rather than fail: a slot the offer has
|
|
50
|
+
* no media section for stays empty (no transceiver, its track kept unsent), a
|
|
51
|
+
* media section past the polite end's slots stays `recvonly` and unnamed, and
|
|
52
|
+
* a section whose kind differs from its slot's is logged and skipped, that
|
|
53
|
+
* slot staying empty. The slot list is protocol: changing its shape (the
|
|
54
|
+
* slots, their order, or their kinds) bumps `RTC_SIGNAL_VERSION`
|
|
55
|
+
* (`rtc/rtc_signal.ts`), so mismatched ends refuse each other's signals
|
|
56
|
+
* instead of degrading.
|
|
57
|
+
*
|
|
58
|
+
* Remote media is `remote_tracks`, by slot name: the receiver's track for each
|
|
59
|
+
* slot once the far end's description reaches it (muted while the far end
|
|
60
|
+
* sends nothing on it).
|
|
61
|
+
*
|
|
62
|
+
* ## ICE restart
|
|
63
|
+
*
|
|
64
|
+
* When ICE fails, the peer restarts it (`restartIce`, which renegotiates with
|
|
65
|
+
* new credentials through the same pattern). A connection that stays
|
|
66
|
+
* `disconnected` past `ice_disconnected_restart_ms` is restarted too, since a
|
|
67
|
+
* disconnect is often a network change that ICE alone won't recover from.
|
|
68
|
+
*
|
|
69
|
+
* @module
|
|
70
|
+
*/
|
|
71
|
+
import { to_error_message } from '@fuzdev/fuz_util/error.ts';
|
|
72
|
+
/**
|
|
73
|
+
* The slots of a voice call: the microphone's audio. A call with video adds
|
|
74
|
+
* slots after it (camera, screen, screen audio), in an order both ends share.
|
|
75
|
+
*/
|
|
76
|
+
export const RTC_VOICE_SLOTS = [{ name: 'mic', kind: 'audio' }];
|
|
77
|
+
/**
|
|
78
|
+
* Default `RtcPeerOptions.ice_disconnected_restart_ms`: long enough for a
|
|
79
|
+
* brief network blip to recover on its own.
|
|
80
|
+
*/
|
|
81
|
+
export const RTC_ICE_DISCONNECTED_RESTART_MS = 5_000;
|
|
82
|
+
/**
|
|
83
|
+
* Whether the local end is the polite one of a pair: the lower member id is
|
|
84
|
+
* polite. Both ends compute it alike, so they always disagree on who yields.
|
|
85
|
+
*
|
|
86
|
+
* @param local_member_id - this end's member id
|
|
87
|
+
* @param remote_member_id - the far end's
|
|
88
|
+
* @returns whether this end yields when offers cross
|
|
89
|
+
*/
|
|
90
|
+
export const rtc_peer_is_polite = (local_member_id, remote_member_id) => local_member_id < remote_member_id;
|
|
91
|
+
/** `RtcPeerDeps` over the browser's own globals, each read when it's called. */
|
|
92
|
+
export const create_rtc_peer_deps = (log) => ({
|
|
93
|
+
create_peer_connection: (configuration) => new RTCPeerConnection(configuration),
|
|
94
|
+
set_timeout: (callback, ms) => setTimeout(callback, ms),
|
|
95
|
+
clear_timeout: (handle) => clearTimeout(handle),
|
|
96
|
+
log
|
|
97
|
+
});
|
|
98
|
+
/** One connection to one remote member. See the module doc. */
|
|
99
|
+
export class RtcPeer {
|
|
100
|
+
remote_member_id;
|
|
101
|
+
polite;
|
|
102
|
+
slots;
|
|
103
|
+
/** The connection's `connectionState`. */
|
|
104
|
+
connection_state = $state.raw('new');
|
|
105
|
+
/** The connection's `iceConnectionState`. */
|
|
106
|
+
ice_connection_state = $state.raw('new');
|
|
107
|
+
/** The connection's `signalingState`. */
|
|
108
|
+
signaling_state = $state.raw('stable');
|
|
109
|
+
/** The far end's track for each slot, by slot name, `null` until its description reaches it. */
|
|
110
|
+
remote_tracks = $state.raw({});
|
|
111
|
+
/** Whether `close` was called. */
|
|
112
|
+
closed = $state.raw(false);
|
|
113
|
+
/** The last negotiation failure, for diagnostics. */
|
|
114
|
+
last_error = $state.raw(null);
|
|
115
|
+
#deps;
|
|
116
|
+
#pc;
|
|
117
|
+
#send;
|
|
118
|
+
#listeners = new AbortController();
|
|
119
|
+
#ice_disconnected_restart_ms;
|
|
120
|
+
/** Each slot's transceiver, `null` on the polite end until the first offer brings it. */
|
|
121
|
+
#transceivers;
|
|
122
|
+
/** Each slot's local track, applied to its transceiver whenever there is one. */
|
|
123
|
+
#local_tracks;
|
|
124
|
+
#making_offer = false;
|
|
125
|
+
#ignore_offer = false;
|
|
126
|
+
/** Candidates waiting for a remote description, each with whether an offer was being ignored as it arrived. */
|
|
127
|
+
#pending_candidates = [];
|
|
128
|
+
#inbound = Promise.resolve();
|
|
129
|
+
#disconnected_timer = null;
|
|
130
|
+
constructor(deps, options) {
|
|
131
|
+
this.#deps = deps;
|
|
132
|
+
this.remote_member_id = options.remote_member_id;
|
|
133
|
+
this.polite = options.polite;
|
|
134
|
+
this.slots = options.slots ?? RTC_VOICE_SLOTS;
|
|
135
|
+
this.#send = options.send;
|
|
136
|
+
this.#ice_disconnected_restart_ms =
|
|
137
|
+
options.ice_disconnected_restart_ms === undefined
|
|
138
|
+
? RTC_ICE_DISCONNECTED_RESTART_MS
|
|
139
|
+
: options.ice_disconnected_restart_ms;
|
|
140
|
+
this.#transceivers = this.slots.map(() => null);
|
|
141
|
+
this.#local_tracks = this.slots.map(() => null);
|
|
142
|
+
this.remote_tracks = this.#no_remote_tracks();
|
|
143
|
+
const pc = deps.create_peer_connection({ iceServers: [...(options.ice_servers ?? [])] });
|
|
144
|
+
this.#pc = pc;
|
|
145
|
+
const signal = this.#listeners.signal;
|
|
146
|
+
pc.addEventListener('negotiationneeded', () => void this.#negotiate(), { signal });
|
|
147
|
+
pc.addEventListener('icecandidate', (e) => this.#on_ice_candidate(e), { signal });
|
|
148
|
+
pc.addEventListener('track', (e) => this.#on_track(e), { signal });
|
|
149
|
+
pc.addEventListener('connectionstatechange', () => (this.connection_state = pc.connectionState), { signal });
|
|
150
|
+
pc.addEventListener('signalingstatechange', () => (this.signaling_state = pc.signalingState), {
|
|
151
|
+
signal
|
|
152
|
+
});
|
|
153
|
+
pc.addEventListener('iceconnectionstatechange', () => this.#on_ice_state(), { signal });
|
|
154
|
+
// the impolite end creates the slots; its first offer carries them
|
|
155
|
+
if (!this.polite) {
|
|
156
|
+
try {
|
|
157
|
+
this.slots.forEach((slot, i) => {
|
|
158
|
+
this.#transceivers[i] = pc.addTransceiver(slot.kind, { direction: 'sendrecv' });
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
// a peer that can't be made leaves no connection open behind it
|
|
163
|
+
this.#listeners.abort();
|
|
164
|
+
pc.close();
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Take a signal from the far end. Signals are handled one at a time, in
|
|
171
|
+
* call order; a failure is logged and kept in `last_error`, never thrown.
|
|
172
|
+
*
|
|
173
|
+
* @returns resolves once this signal is handled
|
|
174
|
+
*/
|
|
175
|
+
on_signal(signal) {
|
|
176
|
+
const handled = this.#inbound.then(() => this.#handle(signal));
|
|
177
|
+
this.#inbound = handled;
|
|
178
|
+
return handled;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Send `track` in `slot` (`null` sends nothing) — a `replaceTrack`, never a
|
|
182
|
+
* renegotiation. On the polite end before the first offer, the track is
|
|
183
|
+
* kept and sent once the slot's transceiver arrives.
|
|
184
|
+
*
|
|
185
|
+
* @param slot - the slot's name
|
|
186
|
+
* @param track - the track, of the slot's kind, or `null`
|
|
187
|
+
* @throws Error when no slot has that name
|
|
188
|
+
*/
|
|
189
|
+
async set_track(slot, track) {
|
|
190
|
+
const index = this.slots.findIndex((s) => s.name === slot);
|
|
191
|
+
if (index === -1)
|
|
192
|
+
throw new Error(`RtcPeer: no slot named '${slot}'`);
|
|
193
|
+
this.#local_tracks[index] = track;
|
|
194
|
+
const transceiver = this.#transceivers[index];
|
|
195
|
+
if (!transceiver || this.closed)
|
|
196
|
+
return;
|
|
197
|
+
await transceiver.sender.replaceTrack(track);
|
|
198
|
+
}
|
|
199
|
+
/** Restart ICE: a renegotiation with new credentials. */
|
|
200
|
+
restart_ice() {
|
|
201
|
+
if (this.closed)
|
|
202
|
+
return;
|
|
203
|
+
this.#clear_disconnected_timer();
|
|
204
|
+
this.#deps.log?.debug('[rtc_peer] ICE restart:', this.remote_member_id);
|
|
205
|
+
this.#pc.restartIce();
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Close the connection. Idempotent; local tracks are the caller's and stay
|
|
209
|
+
* live.
|
|
210
|
+
*
|
|
211
|
+
* @mutates this - sets `closed`, clears `remote_tracks`
|
|
212
|
+
*/
|
|
213
|
+
close() {
|
|
214
|
+
if (this.closed)
|
|
215
|
+
return;
|
|
216
|
+
this.closed = true;
|
|
217
|
+
this.#listeners.abort();
|
|
218
|
+
this.#clear_disconnected_timer();
|
|
219
|
+
this.#pending_candidates = [];
|
|
220
|
+
this.#pc.close();
|
|
221
|
+
this.connection_state = 'closed';
|
|
222
|
+
this.signaling_state = 'closed';
|
|
223
|
+
this.remote_tracks = this.#no_remote_tracks();
|
|
224
|
+
}
|
|
225
|
+
/** Every slot's remote track, none received. */
|
|
226
|
+
#no_remote_tracks() {
|
|
227
|
+
return Object.fromEntries(this.slots.map((s) => [s.name, null]));
|
|
228
|
+
}
|
|
229
|
+
async #negotiate() {
|
|
230
|
+
if (this.closed)
|
|
231
|
+
return;
|
|
232
|
+
try {
|
|
233
|
+
this.#making_offer = true;
|
|
234
|
+
await this.#pc.setLocalDescription();
|
|
235
|
+
if (this.closed)
|
|
236
|
+
return;
|
|
237
|
+
this.#send_local_description();
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
this.#fail('offer', error);
|
|
241
|
+
}
|
|
242
|
+
finally {
|
|
243
|
+
this.#making_offer = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async #handle(signal) {
|
|
247
|
+
if (this.closed)
|
|
248
|
+
return;
|
|
249
|
+
const pc = this.#pc;
|
|
250
|
+
try {
|
|
251
|
+
if ('description' in signal) {
|
|
252
|
+
const { description } = signal;
|
|
253
|
+
const ready_for_offer = !this.#making_offer && pc.signalingState === 'stable';
|
|
254
|
+
const collision = description.type === 'offer' && !ready_for_offer;
|
|
255
|
+
this.#ignore_offer = !this.polite && collision;
|
|
256
|
+
if (this.#ignore_offer) {
|
|
257
|
+
this.#deps.log?.debug('[rtc_peer] ignored a crossing offer:', this.remote_member_id);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
// on the polite end a crossing offer rolls its own back implicitly
|
|
261
|
+
await pc.setRemoteDescription(description);
|
|
262
|
+
if (this.closed)
|
|
263
|
+
return;
|
|
264
|
+
if (description.type === 'offer') {
|
|
265
|
+
this.#adopt_transceivers();
|
|
266
|
+
await pc.setLocalDescription();
|
|
267
|
+
if (this.closed)
|
|
268
|
+
return;
|
|
269
|
+
this.#send_local_description();
|
|
270
|
+
}
|
|
271
|
+
await this.#flush_candidates();
|
|
272
|
+
}
|
|
273
|
+
else if (pc.remoteDescription === null) {
|
|
274
|
+
this.#pending_candidates.push({
|
|
275
|
+
candidate: signal.candidate,
|
|
276
|
+
ignoring: this.#ignore_offer
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
await this.#add_candidate(signal.candidate, this.#ignore_offer);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
this.#fail('signal', error);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/** On the polite end, take the slots' transceivers from the offer just applied. */
|
|
288
|
+
#adopt_transceivers() {
|
|
289
|
+
const transceivers = this.#pc.getTransceivers();
|
|
290
|
+
this.slots.forEach((slot, i) => {
|
|
291
|
+
if (this.#transceivers[i])
|
|
292
|
+
return;
|
|
293
|
+
const transceiver = transceivers[i];
|
|
294
|
+
if (!transceiver)
|
|
295
|
+
return;
|
|
296
|
+
if (transceiver.receiver.track.kind !== slot.kind) {
|
|
297
|
+
this.#deps.log?.warn(`[rtc_peer] the offer's media section ${i} is ${transceiver.receiver.track.kind}, slot '${slot.name}' is ${slot.kind}`);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
transceiver.direction = 'sendrecv';
|
|
301
|
+
this.#transceivers[i] = transceiver;
|
|
302
|
+
const track = this.#local_tracks[i] ?? null;
|
|
303
|
+
if (track) {
|
|
304
|
+
transceiver.sender.replaceTrack(track).catch((error) => {
|
|
305
|
+
this.#fail('replace_track', error);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
#send_local_description() {
|
|
311
|
+
const description = this.#pc.localDescription;
|
|
312
|
+
if (!description)
|
|
313
|
+
return;
|
|
314
|
+
const { type, sdp } = description;
|
|
315
|
+
if (type !== 'offer' && type !== 'answer')
|
|
316
|
+
return;
|
|
317
|
+
this.#send({ description: { type, sdp } });
|
|
318
|
+
}
|
|
319
|
+
async #flush_candidates() {
|
|
320
|
+
const queued = this.#pending_candidates;
|
|
321
|
+
this.#pending_candidates = [];
|
|
322
|
+
for (const { candidate, ignoring } of queued) {
|
|
323
|
+
if (this.closed)
|
|
324
|
+
return;
|
|
325
|
+
await this.#add_candidate(candidate, ignoring);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* @param ignoring - whether an offer was being ignored as the candidate
|
|
330
|
+
* arrived; one for that offer can't be added, and needn't be
|
|
331
|
+
*/
|
|
332
|
+
async #add_candidate(candidate, ignoring) {
|
|
333
|
+
try {
|
|
334
|
+
await this.#pc.addIceCandidate(candidate);
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
if (!ignoring)
|
|
338
|
+
this.#fail('candidate', error);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
#on_ice_candidate(event) {
|
|
342
|
+
if (this.closed || !event.candidate)
|
|
343
|
+
return;
|
|
344
|
+
const json = event.candidate.toJSON();
|
|
345
|
+
if (typeof json.candidate !== 'string')
|
|
346
|
+
return;
|
|
347
|
+
this.#send({
|
|
348
|
+
candidate: {
|
|
349
|
+
candidate: json.candidate,
|
|
350
|
+
sdpMid: json.sdpMid,
|
|
351
|
+
sdpMLineIndex: json.sdpMLineIndex,
|
|
352
|
+
usernameFragment: json.usernameFragment
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
#on_track(event) {
|
|
357
|
+
const index = this.#pc.getTransceivers().indexOf(event.transceiver);
|
|
358
|
+
const slot = this.slots[index];
|
|
359
|
+
if (!slot)
|
|
360
|
+
return;
|
|
361
|
+
this.remote_tracks = { ...this.remote_tracks, [slot.name]: event.track };
|
|
362
|
+
}
|
|
363
|
+
#on_ice_state() {
|
|
364
|
+
const state = this.#pc.iceConnectionState;
|
|
365
|
+
this.ice_connection_state = state;
|
|
366
|
+
if (state === 'failed') {
|
|
367
|
+
this.restart_ice();
|
|
368
|
+
}
|
|
369
|
+
else if (state === 'disconnected') {
|
|
370
|
+
const ms = this.#ice_disconnected_restart_ms;
|
|
371
|
+
if (ms !== null && this.#disconnected_timer === null) {
|
|
372
|
+
this.#disconnected_timer = this.#deps.set_timeout(() => {
|
|
373
|
+
this.#disconnected_timer = null;
|
|
374
|
+
if (this.#pc.iceConnectionState === 'disconnected')
|
|
375
|
+
this.restart_ice();
|
|
376
|
+
}, ms);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
this.#clear_disconnected_timer();
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
#clear_disconnected_timer() {
|
|
384
|
+
if (this.#disconnected_timer === null)
|
|
385
|
+
return;
|
|
386
|
+
this.#deps.clear_timeout(this.#disconnected_timer);
|
|
387
|
+
this.#disconnected_timer = null;
|
|
388
|
+
}
|
|
389
|
+
#fail(stage, error) {
|
|
390
|
+
// work in flight at the close fails against the closed connection, as expected
|
|
391
|
+
if (this.closed)
|
|
392
|
+
return;
|
|
393
|
+
const message = to_error_message(error);
|
|
394
|
+
this.last_error = `${stage}: ${message}`;
|
|
395
|
+
this.#deps.log?.warn(`[rtc_peer] ${stage} failed:`, this.remote_member_id, message);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebRTC signal envelope: what `RtcPeer` sends its far end, carried by
|
|
3
|
+
* `RtcMesh` as a room payload (`room_send`'s `payload`, a string the server
|
|
4
|
+
* relays without reading).
|
|
5
|
+
*
|
|
6
|
+
* A signal is one of two things, the two that perfect negotiation exchanges:
|
|
7
|
+
* a session description (an offer or an answer) or a trickled ICE candidate.
|
|
8
|
+
* On the wire it is the JSON text of one object whose `rtc` key is the
|
|
9
|
+
* envelope's version, `RTC_SIGNAL_VERSION`, beside exactly one of
|
|
10
|
+
* `description` or `candidate`:
|
|
11
|
+
*
|
|
12
|
+
* ```json
|
|
13
|
+
* {"rtc":1,"description":{"type":"offer","sdp":"v=0\r\n…"}}
|
|
14
|
+
* {"rtc":1,"candidate":{"candidate":"candidate:…","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"…"}}
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* `candidate` is `RTCIceCandidate.toJSON()`. The end of candidates is never
|
|
18
|
+
* sent: trickle works without it. The `rtc` key also tells a signal apart from
|
|
19
|
+
* the room's other payloads (a room's text, say): `decode_rtc_signal` answers
|
|
20
|
+
* `not_rtc_signal` for any payload without it, so a room's other listeners can
|
|
21
|
+
* read their own. A newer envelope bumps the version, and an older client
|
|
22
|
+
* refuses it as `unsupported_version` rather than misreading it.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
import type { Result } from '@fuzdev/fuz_util/result.ts';
|
|
27
|
+
import { type Infer } from '@fuzdev/fuz_schema/schema.ts';
|
|
28
|
+
/** The envelope's version, its `rtc` key. */
|
|
29
|
+
export declare const RTC_SIGNAL_VERSION = 1;
|
|
30
|
+
/** A session description as a signal carries it: an offer or an answer. */
|
|
31
|
+
export declare const RtcSessionDescription: import("@fuzdev/fuz_schema/schema.ts").ObjectSchema<{
|
|
32
|
+
type: import("@fuzdev/fuz_schema/schema.ts").OneOfSchema<readonly ["offer", "answer"]>;
|
|
33
|
+
sdp: import("@fuzdev/fuz_schema/schema.ts").StringSchema;
|
|
34
|
+
}>;
|
|
35
|
+
export type RtcSessionDescription = Infer<typeof RtcSessionDescription>;
|
|
36
|
+
/** A trickled ICE candidate as a signal carries it: `RTCIceCandidate.toJSON()`. */
|
|
37
|
+
export declare const RtcIceCandidate: import("@fuzdev/fuz_schema/schema.ts").ObjectSchema<{
|
|
38
|
+
candidate: import("@fuzdev/fuz_schema/schema.ts").StringSchema;
|
|
39
|
+
sdpMid: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").StringSchema>>;
|
|
40
|
+
sdpMLineIndex: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<number, import("@fuzdev/fuz_schema/schema.ts").NumberSchema>>>;
|
|
41
|
+
usernameFragment: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").StringSchema>>;
|
|
42
|
+
}>;
|
|
43
|
+
export type RtcIceCandidate = Infer<typeof RtcIceCandidate>;
|
|
44
|
+
/** One signal between two peers: a session description, or an ICE candidate. */
|
|
45
|
+
export type RtcSignal = {
|
|
46
|
+
description: RtcSessionDescription;
|
|
47
|
+
} | {
|
|
48
|
+
candidate: RtcIceCandidate;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Why a payload is no signal: `not_rtc_signal` (no `rtc` key, or not JSON — a
|
|
52
|
+
* payload for some other reader), `unsupported_version` (another envelope
|
|
53
|
+
* version), `invalid` (an `rtc` envelope that doesn't parse).
|
|
54
|
+
*/
|
|
55
|
+
export type RtcSignalDecodeError = 'not_rtc_signal' | 'unsupported_version' | 'invalid';
|
|
56
|
+
/**
|
|
57
|
+
* Encode a signal as a room payload.
|
|
58
|
+
*
|
|
59
|
+
* @param signal - the signal
|
|
60
|
+
* @returns its envelope's JSON text
|
|
61
|
+
*/
|
|
62
|
+
export declare const encode_rtc_signal: (signal: RtcSignal) => string;
|
|
63
|
+
/**
|
|
64
|
+
* Decode a room payload as a signal.
|
|
65
|
+
*
|
|
66
|
+
* @param payload - a `room_message` payload
|
|
67
|
+
* @returns the signal, or why the payload is none
|
|
68
|
+
*/
|
|
69
|
+
export declare const decode_rtc_signal: (payload: string) => Result<{
|
|
70
|
+
value: RtcSignal;
|
|
71
|
+
}, {
|
|
72
|
+
reason: RtcSignalDecodeError;
|
|
73
|
+
}>;
|
|
74
|
+
//# sourceMappingURL=rtc_signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rtc_signal.d.ts","sourceRoot":"","sources":["../../src/lib/rtc/rtc_signal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAYN,KAAK,KAAK,EACV,MAAM,8BAA8B,CAAC;AAEtC,6CAA6C;AAC7C,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC,2EAA2E;AAC3E,eAAO,MAAM,qBAAqB;;;EAGhC,CAAC;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAExE,mFAAmF;AACnF,eAAO,MAAM,eAAe;;;;;EAK1B,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAE5D,gFAAgF;AAChF,MAAM,MAAM,SAAS,GAAG;IAAE,WAAW,EAAE,qBAAqB,CAAA;CAAE,GAAG;IAAE,SAAS,EAAE,eAAe,CAAA;CAAE,CAAC;AAYhG;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,gBAAgB,GAAG,qBAAqB,GAAG,SAAS,CAAC;AAExF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,SAAS,KAAG,MAKpD,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAC7B,SAAS,MAAM,KACb,MAAM,CAAC;IAAE,KAAK,EAAE,SAAS,CAAA;CAAE,EAAE;IAAE,MAAM,EAAE,oBAAoB,CAAA;CAAE,CAmB/D,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WebRTC signal envelope: what `RtcPeer` sends its far end, carried by
|
|
3
|
+
* `RtcMesh` as a room payload (`room_send`'s `payload`, a string the server
|
|
4
|
+
* relays without reading).
|
|
5
|
+
*
|
|
6
|
+
* A signal is one of two things, the two that perfect negotiation exchanges:
|
|
7
|
+
* a session description (an offer or an answer) or a trickled ICE candidate.
|
|
8
|
+
* On the wire it is the JSON text of one object whose `rtc` key is the
|
|
9
|
+
* envelope's version, `RTC_SIGNAL_VERSION`, beside exactly one of
|
|
10
|
+
* `description` or `candidate`:
|
|
11
|
+
*
|
|
12
|
+
* ```json
|
|
13
|
+
* {"rtc":1,"description":{"type":"offer","sdp":"v=0\r\n…"}}
|
|
14
|
+
* {"rtc":1,"candidate":{"candidate":"candidate:…","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"…"}}
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* `candidate` is `RTCIceCandidate.toJSON()`. The end of candidates is never
|
|
18
|
+
* sent: trickle works without it. The `rtc` key also tells a signal apart from
|
|
19
|
+
* the room's other payloads (a room's text, say): `decode_rtc_signal` answers
|
|
20
|
+
* `not_rtc_signal` for any payload without it, so a room's other listeners can
|
|
21
|
+
* read their own. A newer envelope bumps the version, and an older client
|
|
22
|
+
* refuses it as `unsupported_version` rather than misreading it.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
import { literal, nullable, number, object, one_of, optional, parse, pipe, safe_integer, min_value, string } from '@fuzdev/fuz_schema/schema.ts';
|
|
27
|
+
/** The envelope's version, its `rtc` key. */
|
|
28
|
+
export const RTC_SIGNAL_VERSION = 1;
|
|
29
|
+
/** A session description as a signal carries it: an offer or an answer. */
|
|
30
|
+
export const RtcSessionDescription = object({
|
|
31
|
+
type: one_of(['offer', 'answer']),
|
|
32
|
+
sdp: string
|
|
33
|
+
});
|
|
34
|
+
/** A trickled ICE candidate as a signal carries it: `RTCIceCandidate.toJSON()`. */
|
|
35
|
+
export const RtcIceCandidate = object({
|
|
36
|
+
candidate: string,
|
|
37
|
+
sdpMid: optional(nullable(string)),
|
|
38
|
+
sdpMLineIndex: optional(nullable(pipe(number, safe_integer, min_value(0)))),
|
|
39
|
+
usernameFragment: optional(nullable(string))
|
|
40
|
+
});
|
|
41
|
+
const RtcSignalDescriptionEnvelope = object({
|
|
42
|
+
rtc: literal(RTC_SIGNAL_VERSION),
|
|
43
|
+
description: RtcSessionDescription
|
|
44
|
+
});
|
|
45
|
+
const RtcSignalCandidateEnvelope = object({
|
|
46
|
+
rtc: literal(RTC_SIGNAL_VERSION),
|
|
47
|
+
candidate: RtcIceCandidate
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* Encode a signal as a room payload.
|
|
51
|
+
*
|
|
52
|
+
* @param signal - the signal
|
|
53
|
+
* @returns its envelope's JSON text
|
|
54
|
+
*/
|
|
55
|
+
export const encode_rtc_signal = (signal) => JSON.stringify('description' in signal
|
|
56
|
+
? { rtc: RTC_SIGNAL_VERSION, description: signal.description }
|
|
57
|
+
: { rtc: RTC_SIGNAL_VERSION, candidate: signal.candidate });
|
|
58
|
+
/**
|
|
59
|
+
* Decode a room payload as a signal.
|
|
60
|
+
*
|
|
61
|
+
* @param payload - a `room_message` payload
|
|
62
|
+
* @returns the signal, or why the payload is none
|
|
63
|
+
*/
|
|
64
|
+
export const decode_rtc_signal = (payload) => {
|
|
65
|
+
let json;
|
|
66
|
+
try {
|
|
67
|
+
json = JSON.parse(payload);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return { ok: false, reason: 'not_rtc_signal' };
|
|
71
|
+
}
|
|
72
|
+
if (json === null || typeof json !== 'object' || Array.isArray(json) || !('rtc' in json)) {
|
|
73
|
+
return { ok: false, reason: 'not_rtc_signal' };
|
|
74
|
+
}
|
|
75
|
+
if (json.rtc !== RTC_SIGNAL_VERSION)
|
|
76
|
+
return { ok: false, reason: 'unsupported_version' };
|
|
77
|
+
if ('description' in json) {
|
|
78
|
+
const parsed = parse(RtcSignalDescriptionEnvelope, json);
|
|
79
|
+
if (!parsed.ok)
|
|
80
|
+
return { ok: false, reason: 'invalid' };
|
|
81
|
+
return { ok: true, value: { description: parsed.value.description } };
|
|
82
|
+
}
|
|
83
|
+
const parsed = parse(RtcSignalCandidateEnvelope, json);
|
|
84
|
+
if (!parsed.ok)
|
|
85
|
+
return { ok: false, reason: 'invalid' };
|
|
86
|
+
return { ok: true, value: { candidate: parsed.value.candidate } };
|
|
87
|
+
};
|