@coasys/ad4m 0.13.1-staging.1 → 0.13.1-staging.2
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/lib/index.cjs +3789 -1192
- package/lib/index.js +3783 -1193
- package/lib/src/generated/api/AgentsByDidsRequest.d.ts +3 -0
- package/lib/src/generated/api/CallSessionInfo.d.ts +23 -0
- package/lib/src/generated/api/DiscardBatchRequest.d.ts +3 -0
- package/lib/src/generated/api/Events.d.ts +13 -3
- package/lib/src/generated/api/Events.js +1 -0
- package/lib/src/generated/api/FlowStateChangedEvent.d.ts +38 -0
- package/lib/src/generated/api/FlowStateEntry.d.ts +18 -0
- package/lib/src/generated/api/FlowStateEntry.js +2 -0
- package/lib/src/generated/api/IceServer.d.ts +18 -0
- package/lib/src/generated/api/IceServer.js +2 -0
- package/lib/src/generated/api/OnlineAgent.d.ts +4 -0
- package/lib/src/generated/api/PerspectiveDiscardBatchParams.d.ts +4 -0
- package/lib/src/generated/api/PerspectiveDiscardBatchParams.js +2 -0
- package/lib/src/generated/api/PerspectiveFlowFiredParams.d.ts +8 -0
- package/lib/src/generated/api/PerspectiveFlowFiredParams.js +2 -0
- package/lib/src/generated/api/PerspectiveFlowStatesParams.d.ts +11 -0
- package/lib/src/generated/api/PerspectiveFlowStatesParams.js +2 -0
- package/lib/src/generated/api/PerspectiveGetAllShaclParams.d.ts +7 -0
- package/lib/src/generated/api/PerspectiveGetAllShaclParams.js +2 -0
- package/lib/src/generated/api/PerspectiveModelQueryParams.d.ts +6 -1
- package/lib/src/generated/api/PerspectiveRemoveSubjectParams.d.ts +6 -0
- package/lib/src/generated/api/PerspectiveRemoveSubjectParams.js +2 -0
- package/lib/src/generated/api/PerspectiveResynced.d.ts +8 -0
- package/lib/src/generated/api/PerspectiveResynced.js +2 -0
- package/lib/src/generated/api/PerspectiveSubscribed.d.ts +10 -0
- package/lib/src/generated/api/PerspectiveSubscribed.js +2 -0
- package/lib/src/generated/api/PerspectiveUpdateSubjectParams.d.ts +10 -0
- package/lib/src/generated/api/PerspectiveUpdateSubjectParams.js +2 -0
- package/lib/src/generated/api/QuerySubscriptionUpdate.d.ts +19 -0
- package/lib/src/generated/api/QuerySubscriptionUpdate.js +2 -0
- package/lib/src/generated/api/QuerySubscriptionUpdateEvent.d.ts +7 -0
- package/lib/src/generated/api/QuerySubscriptionUpdateEvent.js +1 -0
- package/lib/src/generated/api/QueryUpdatesLagged.d.ts +7 -0
- package/lib/src/generated/api/QueryUpdatesLagged.js +2 -0
- package/lib/src/generated/api/RemoveSubjectRequest.d.ts +9 -0
- package/lib/src/generated/api/RemoveSubjectRequest.js +2 -0
- package/lib/src/generated/api/ResyncSubscriptionRequest.d.ts +3 -0
- package/lib/src/generated/api/ResyncSubscriptionRequest.js +2 -0
- package/lib/src/generated/api/RpcMethods.d.ts +134 -14
- package/lib/src/generated/api/RpcMethods.js +12 -0
- package/lib/src/generated/api/SfuAddIceCandidateParams.d.ts +8 -0
- package/lib/src/generated/api/SfuAddIceCandidateParams.js +2 -0
- package/lib/src/generated/api/SfuAnswerServerOfferParams.d.ts +8 -0
- package/lib/src/generated/api/SfuAnswerServerOfferParams.js +2 -0
- package/lib/src/generated/api/SfuCallJoinParams.d.ts +14 -0
- package/lib/src/generated/api/SfuCallJoinParams.js +2 -0
- package/lib/src/generated/api/SfuCallRenegotiationOffer.d.ts +30 -0
- package/lib/src/generated/api/SfuCallRenegotiationOffer.js +1 -0
- package/lib/src/generated/api/SfuCascadePipe.d.ts +4 -0
- package/lib/src/generated/api/SfuCascadePipe.js +2 -0
- package/lib/src/generated/api/SfuCascadeStatus.d.ts +8 -0
- package/lib/src/generated/api/SfuCascadeStatus.js +1 -0
- package/lib/src/generated/api/SfuConfig.d.ts +52 -0
- package/lib/src/generated/api/SfuConfig.js +1 -0
- package/lib/src/generated/api/SfuDataMessage.d.ts +24 -0
- package/lib/src/generated/api/SfuDataMessage.js +2 -0
- package/lib/src/generated/api/SfuEnsureMembershipParams.d.ts +4 -0
- package/lib/src/generated/api/SfuEnsureMembershipParams.js +2 -0
- package/lib/src/generated/api/SfuMigrateEvent.d.ts +21 -0
- package/lib/src/generated/api/SfuMigrateEvent.js +2 -0
- package/lib/src/generated/api/SfuNeighbourhoodParams.d.ts +3 -0
- package/lib/src/generated/api/SfuNeighbourhoodParams.js +2 -0
- package/lib/src/generated/api/SfuParticipantQualityPreference.d.ts +7 -0
- package/lib/src/generated/api/SfuParticipantQualityPreference.js +2 -0
- package/lib/src/generated/api/SfuPerspectiveParams.d.ts +6 -0
- package/lib/src/generated/api/SfuPerspectiveParams.js +2 -0
- package/lib/src/generated/api/SfuQualityPreferenceParams.d.ts +8 -0
- package/lib/src/generated/api/SfuQualityPreferenceParams.js +2 -0
- package/lib/src/generated/api/SfuRoomInfo.d.ts +11 -0
- package/lib/src/generated/api/SfuRoomInfo.js +1 -0
- package/lib/src/generated/api/SfuRoomParams.d.ts +4 -0
- package/lib/src/generated/api/SfuRoomParams.js +2 -0
- package/lib/src/generated/api/SfuRoomParticipantInfo.d.ts +11 -0
- package/lib/src/generated/api/SfuRoomParticipantInfo.js +2 -0
- package/lib/src/generated/api/SfuRoomRoster.d.ts +13 -0
- package/lib/src/generated/api/SfuRoomRoster.js +2 -0
- package/lib/src/generated/api/SfuSendDataParams.d.ts +10 -0
- package/lib/src/generated/api/SfuSendDataParams.js +2 -0
- package/lib/src/generated/api/SfuSetConfigParams.d.ts +5 -0
- package/lib/src/generated/api/SfuSetConfigParams.js +1 -0
- package/lib/src/generated/api/SfuStatus.d.ts +24 -0
- package/lib/src/generated/api/SfuStatus.js +2 -0
- package/lib/src/generated/api/TrackMapEntry.d.ts +17 -0
- package/lib/src/generated/api/TrackMapEntry.js +2 -0
- package/lib/src/generated/api/UpdateSubjectRequest.d.ts +12 -0
- package/lib/src/generated/api/UpdateSubjectRequest.js +2 -0
- package/lib/src/generated/api/index.d.ts +3 -0
- package/lib/src/index.d.ts +7 -0
- package/lib/src/index.js +4 -0
- package/lib/src/model/Ad4mModel.d.ts +84 -0
- package/lib/src/model/Ad4mModel.js +234 -4
- package/lib/src/model/ModelQueryBuilder.d.ts +16 -35
- package/lib/src/model/ModelQueryBuilder.js +88 -270
- package/lib/src/model/decorators.d.ts +32 -0
- package/lib/src/model/decorators.js +40 -1
- package/lib/src/model/shacl-gen.d.ts +3 -1
- package/lib/src/model/shacl-gen.js +13 -1
- package/lib/src/model/types.d.ts +7 -0
- package/lib/src/neighbourhood/MeshManager.d.ts +384 -0
- package/lib/src/neighbourhood/MeshManager.js +1144 -0
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +91 -0
- package/lib/src/neighbourhood/NeighbourhoodClient.js +196 -0
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +55 -1
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +170 -3
- package/lib/src/neighbourhood/Session.d.ts +105 -0
- package/lib/src/neighbourhood/Session.js +412 -0
- package/lib/src/neighbourhood/SfuManager.d.ts +174 -0
- package/lib/src/neighbourhood/SfuManager.js +619 -0
- package/lib/src/neighbourhood/SfuTypes.d.ts +35 -0
- package/lib/src/neighbourhood/SfuTypes.js +8 -0
- package/lib/src/perspectives/FlowInstance.d.ts +63 -24
- package/lib/src/perspectives/FlowInstance.js +74 -31
- package/lib/src/perspectives/FlowModels.d.ts +0 -16
- package/lib/src/perspectives/FlowModels.js +8 -16
- package/lib/src/perspectives/LiveQuery.d.ts +49 -0
- package/lib/src/perspectives/LiveQuery.js +169 -0
- package/lib/src/perspectives/PerspectiveClient.d.ts +24 -15
- package/lib/src/perspectives/PerspectiveClient.js +33 -36
- package/lib/src/perspectives/PerspectiveProxy.d.ts +67 -167
- package/lib/src/perspectives/PerspectiveProxy.js +121 -475
- package/lib/src/perspectives/TextContent.d.ts +31 -0
- package/lib/src/perspectives/TextContent.js +52 -0
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +23 -0
- package/lib/src/perspectives/WakerSubscriptionManager.js +58 -17
- package/lib/src/shacl/SHACLFlow.d.ts +7 -4
- package/lib/src/shacl/SHACLFlow.js +7 -4
- package/lib/src/shacl/SHACLShape.d.ts +9 -0
- package/lib/src/shacl/SHACLShape.js +37 -0
- package/package.json +2 -2
- package/pnpm-workspace.yaml +4 -1
- package/src/Ad4mClient.test.ts +89 -17
- package/src/generated/api/AgentsByDidsRequest.ts +3 -0
- package/src/generated/api/CallSessionInfo.ts +20 -0
- package/src/generated/api/DiscardBatchRequest.ts +3 -0
- package/src/generated/api/Events.ts +14 -3
- package/src/generated/api/FlowStateChangedEvent.ts +36 -0
- package/src/generated/api/FlowStateEntry.ts +18 -0
- package/src/generated/api/IceServer.ts +19 -0
- package/src/generated/api/OnlineAgent.ts +6 -1
- package/src/generated/api/PerspectiveDiscardBatchParams.ts +3 -0
- package/src/generated/api/PerspectiveFlowFiredParams.ts +7 -0
- package/src/generated/api/PerspectiveFlowStatesParams.ts +11 -0
- package/src/generated/api/PerspectiveGetAllShaclParams.ts +6 -0
- package/src/generated/api/PerspectiveModelQueryParams.ts +5 -1
- package/src/generated/api/PerspectiveRemoveSubjectParams.ts +3 -0
- package/src/generated/api/PerspectiveResynced.ts +7 -0
- package/src/generated/api/PerspectiveSubscribed.ts +8 -0
- package/src/generated/api/PerspectiveUpdateSubjectParams.ts +7 -0
- package/src/generated/api/QuerySubscriptionUpdate.ts +10 -0
- package/src/generated/api/QuerySubscriptionUpdateEvent.ts +9 -0
- package/src/generated/api/QueryUpdatesLagged.ts +7 -0
- package/src/generated/api/RemoveSubjectRequest.ts +7 -0
- package/src/generated/api/ResyncSubscriptionRequest.ts +3 -0
- package/src/generated/api/RpcMethods.ts +68 -8
- package/src/generated/api/SfuAddIceCandidateParams.ts +7 -0
- package/src/generated/api/SfuAnswerServerOfferParams.ts +7 -0
- package/src/generated/api/SfuCallJoinParams.ts +13 -0
- package/src/generated/api/SfuCallRenegotiationOffer.ts +29 -0
- package/src/generated/api/SfuCascadePipe.ts +3 -0
- package/src/generated/api/SfuCascadeStatus.ts +7 -0
- package/src/generated/api/SfuConfig.ts +53 -0
- package/src/generated/api/SfuDataMessage.ts +23 -0
- package/src/generated/api/SfuEnsureMembershipParams.ts +3 -0
- package/src/generated/api/SfuMigrateEvent.ts +20 -0
- package/src/generated/api/{KeepAliveQueryRequest.ts → SfuNeighbourhoodParams.ts} +1 -1
- package/src/generated/api/SfuParticipantQualityPreference.ts +6 -0
- package/src/generated/api/{PerspectiveSubscribeQueryResult.ts → SfuPerspectiveParams.ts} +4 -1
- package/src/generated/api/SfuQualityPreferenceParams.ts +7 -0
- package/src/generated/api/SfuRoomInfo.ts +7 -0
- package/src/generated/api/SfuRoomParams.ts +3 -0
- package/src/generated/api/SfuRoomParticipantInfo.ts +8 -0
- package/src/generated/api/SfuRoomRoster.ts +12 -0
- package/src/generated/api/SfuSendDataParams.ts +7 -0
- package/src/generated/api/SfuSetConfigParams.ts +4 -0
- package/src/generated/api/SfuStatus.ts +24 -0
- package/src/generated/api/TrackMapEntry.ts +18 -0
- package/src/generated/api/UpdateSubjectRequest.ts +10 -0
- package/src/generated/api/index.ts +3 -0
- package/src/index.ts +7 -0
- package/src/model/Ad4mModel.test.ts +255 -231
- package/src/model/Ad4mModel.ts +271 -4
- package/src/model/ModelQueryBuilder.ts +103 -310
- package/src/model/abstract-classes.test.ts +225 -0
- package/src/model/decorators.ts +81 -0
- package/src/model/shacl-gen.ts +13 -0
- package/src/model/types.ts +7 -0
- package/src/neighbourhood/MeshManager.test.ts +2640 -0
- package/src/neighbourhood/MeshManager.ts +1350 -0
- package/src/neighbourhood/NeighbourhoodClient.ts +271 -0
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +23 -0
- package/src/neighbourhood/NeighbourhoodProxy.ts +247 -1
- package/src/neighbourhood/Session.test.ts +800 -0
- package/src/neighbourhood/Session.ts +525 -0
- package/src/neighbourhood/SfuManager.test.ts +677 -0
- package/src/neighbourhood/SfuManager.ts +803 -0
- package/src/neighbourhood/SfuTypes.ts +40 -0
- package/src/perspectives/FlowInstance.ts +108 -31
- package/src/perspectives/FlowModels.ts +8 -18
- package/src/perspectives/LiveQuery.test.ts +197 -0
- package/src/perspectives/LiveQuery.ts +189 -0
- package/src/perspectives/PerspectiveClient.ts +51 -51
- package/src/perspectives/PerspectiveProxy.test.ts +85 -401
- package/src/perspectives/PerspectiveProxy.ts +126 -536
- package/src/perspectives/TextContent.test.ts +123 -0
- package/src/perspectives/TextContent.ts +36 -0
- package/src/perspectives/WakerSubscriptionManager.test.ts +88 -6
- package/src/perspectives/WakerSubscriptionManager.ts +69 -18
- package/src/shacl/SHACLFlow.ts +7 -4
- package/src/shacl/SHACLShape.test.ts +2 -1
- package/src/shacl/SHACLShape.ts +49 -0
- package/lib/src/generated/api/KeepAliveQueryRequest.d.ts +0 -3
- package/lib/src/generated/api/PerspectiveModelSubscribeResult.d.ts +0 -7
- package/lib/src/generated/api/PerspectiveQuerySubscriptionFilter.d.ts +0 -6
- package/lib/src/generated/api/PerspectiveSubscribeQueryResult.d.ts +0 -4
- package/src/generated/api/PerspectiveModelSubscribeResult.ts +0 -6
- package/src/generated/api/PerspectiveQuerySubscriptionFilter.ts +0 -3
- /package/lib/src/generated/api/{KeepAliveQueryRequest.js → AgentsByDidsRequest.js} +0 -0
- /package/lib/src/generated/api/{PerspectiveModelSubscribeResult.js → CallSessionInfo.js} +0 -0
- /package/lib/src/generated/api/{PerspectiveQuerySubscriptionFilter.js → DiscardBatchRequest.js} +0 -0
- /package/lib/src/generated/api/{PerspectiveSubscribeQueryResult.js → FlowStateChangedEvent.js} +0 -0
|
@@ -0,0 +1,803 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SFU (Selective Forwarding Unit) client manager.
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic WebRTC client for the AD4M executor's embedded
|
|
5
|
+
* SFU. Handles topology resolution, SDP negotiation, server-pushed
|
|
6
|
+
* renegotiation, simulcast quality preferences, and reconnecting after
|
|
7
|
+
* a failed connection.
|
|
8
|
+
*
|
|
9
|
+
* The client reaches one SFU: its own executor's. Multi-node calls work
|
|
10
|
+
* through cascade pipes between executors, so the client never follows
|
|
11
|
+
* a redirect or a rebalance to another node — it has no way to reach one.
|
|
12
|
+
*
|
|
13
|
+
* Applications (Flux, WE, or any other) wrap this class with their
|
|
14
|
+
* own reactive store and UI layer. The manager itself only touches
|
|
15
|
+
* Web APIs (RTCPeerConnection, MediaStream) and the AD4M
|
|
16
|
+
* neighbourhood RPC surface.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { NeighbourhoodProxy } from "./NeighbourhoodProxy"
|
|
20
|
+
import type {
|
|
21
|
+
CallSessionInfo,
|
|
22
|
+
SfuCallRenegotiationOffer,
|
|
23
|
+
SfuConfig,
|
|
24
|
+
SfuDataMessage,
|
|
25
|
+
SfuQualityPreference,
|
|
26
|
+
SfuRoomRoster,
|
|
27
|
+
} from "./SfuTypes"
|
|
28
|
+
|
|
29
|
+
// ── Public types ──────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
/** Minimal interface for the neighbourhood client methods the SFU
|
|
32
|
+
* manager calls. NeighbourhoodClient satisfies this directly —
|
|
33
|
+
* pass the client through without an adapter. */
|
|
34
|
+
export interface SfuNeighbourhoodApi {
|
|
35
|
+
sfuCallJoin(
|
|
36
|
+
neighbourhoodUrl: string,
|
|
37
|
+
roomName: string,
|
|
38
|
+
sdpOffer: string,
|
|
39
|
+
): Promise<CallSessionInfo>
|
|
40
|
+
sfuCallLeave(
|
|
41
|
+
neighbourhoodUrl: string,
|
|
42
|
+
roomName: string,
|
|
43
|
+
): Promise<boolean>
|
|
44
|
+
sfuCallSetQualityPreference(
|
|
45
|
+
neighbourhoodUrl: string,
|
|
46
|
+
roomName: string,
|
|
47
|
+
preference: SfuQualityPreference,
|
|
48
|
+
): Promise<boolean>
|
|
49
|
+
sfuCallAnswerServerOffer(
|
|
50
|
+
neighbourhoodUrl: string,
|
|
51
|
+
roomName: string,
|
|
52
|
+
sdpAnswer: string,
|
|
53
|
+
): Promise<boolean>
|
|
54
|
+
subscribeSfuCallRenegotiationOffer(
|
|
55
|
+
targetDid: string,
|
|
56
|
+
callback: (event: SfuCallRenegotiationOffer) => void,
|
|
57
|
+
): () => void
|
|
58
|
+
sfuAddIceCandidate(
|
|
59
|
+
neighbourhoodUrl: string,
|
|
60
|
+
roomName: string,
|
|
61
|
+
candidate: string,
|
|
62
|
+
): Promise<boolean>
|
|
63
|
+
sfuSendData(
|
|
64
|
+
neighbourhoodUrl: string,
|
|
65
|
+
roomName: string,
|
|
66
|
+
channelLabel: string,
|
|
67
|
+
data: string,
|
|
68
|
+
binary?: boolean,
|
|
69
|
+
): Promise<boolean>
|
|
70
|
+
subscribeSfuDataChannel(
|
|
71
|
+
callback: (message: SfuDataMessage) => void,
|
|
72
|
+
): () => void
|
|
73
|
+
subscribeSfuRoomRoster(
|
|
74
|
+
callback: (roster: SfuRoomRoster) => void,
|
|
75
|
+
): () => void
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type SfuTopology = "sfu" | "mesh" | "cascaded"
|
|
79
|
+
|
|
80
|
+
export interface SfuCallState {
|
|
81
|
+
topology: SfuTopology
|
|
82
|
+
roomId: string
|
|
83
|
+
participantId: string | null
|
|
84
|
+
sfuPeerDid: string | null
|
|
85
|
+
participants: Map<string, SfuParticipantState>
|
|
86
|
+
localStream: MediaStream | null
|
|
87
|
+
peerConnection: RTCPeerConnection | null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface SfuParticipantState {
|
|
91
|
+
did: string
|
|
92
|
+
stream: MediaStream
|
|
93
|
+
hasAudio: boolean
|
|
94
|
+
hasVideo: boolean
|
|
95
|
+
isActiveSpeaker: boolean
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type SfuEvent =
|
|
99
|
+
| "topology-changed"
|
|
100
|
+
| "participant-joined"
|
|
101
|
+
| "participant-left"
|
|
102
|
+
| "active-speaker"
|
|
103
|
+
| "stream-added"
|
|
104
|
+
| "stream-removed"
|
|
105
|
+
| "error"
|
|
106
|
+
|
|
107
|
+
export type SfuEventCallback = (...args: any[]) => void
|
|
108
|
+
|
|
109
|
+
/** ICE server configuration for SFU peer connections. */
|
|
110
|
+
export interface SfuIceConfig {
|
|
111
|
+
/** Used as given, before `sfuConfig.iceServers`. */
|
|
112
|
+
servers?: RTCIceServer[]
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ── Constants ─────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
const DEFAULT_ICE_SERVERS: RTCIceServer[] = [
|
|
118
|
+
{ urls: "stun:stun.l.google.com:19302" },
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
// ── Topology resolution ───────────────────────────────────────────
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Determine the optimal call topology from the SFU config and the
|
|
125
|
+
* current participant count. Returns the topology, the DID of the
|
|
126
|
+
* SFU peer to connect to (if any), and the full config.
|
|
127
|
+
*/
|
|
128
|
+
export async function resolveTopology(
|
|
129
|
+
neighbourhood: NeighbourhoodProxy,
|
|
130
|
+
neighbourhoodUrl: string,
|
|
131
|
+
participantCount: number,
|
|
132
|
+
): Promise<{ topology: SfuTopology; sfuPeer: string | null; config: SfuConfig }> {
|
|
133
|
+
const config = await neighbourhood.sfuConfig(neighbourhoodUrl)
|
|
134
|
+
|
|
135
|
+
if (config.mode === "cascaded") {
|
|
136
|
+
const sfuPeers: string[] = await neighbourhood.sfuPeers(neighbourhoodUrl)
|
|
137
|
+
if (sfuPeers.length > 1) {
|
|
138
|
+
return { topology: "cascaded", sfuPeer: sfuPeers[0], config }
|
|
139
|
+
}
|
|
140
|
+
if (sfuPeers.length === 1) {
|
|
141
|
+
return { topology: "sfu", sfuPeer: sfuPeers[0], config }
|
|
142
|
+
}
|
|
143
|
+
return { topology: "mesh", sfuPeer: null, config }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (config.mode === "mesh") {
|
|
147
|
+
return { topology: "mesh", sfuPeer: null, config }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const sfuPeer = await neighbourhood.sfuPeer(neighbourhoodUrl)
|
|
151
|
+
|
|
152
|
+
if (!sfuPeer) {
|
|
153
|
+
if (participantCount <= config.maxMeshParticipants) {
|
|
154
|
+
return { topology: "mesh", sfuPeer: null, config }
|
|
155
|
+
}
|
|
156
|
+
console.warn(
|
|
157
|
+
`SFU peer unavailable and ${participantCount} participants ` +
|
|
158
|
+
`exceeds mesh limit (${config.maxMeshParticipants}). ` +
|
|
159
|
+
`Attempting mesh anyway.`,
|
|
160
|
+
)
|
|
161
|
+
return { topology: "mesh", sfuPeer: null, config }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (participantCount <= config.maxMeshParticipants) {
|
|
165
|
+
return { topology: "mesh", sfuPeer, config }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return { topology: "sfu", sfuPeer, config }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ── SFU Manager ───────────────────────────────────────────────────
|
|
172
|
+
|
|
173
|
+
export class SfuManager {
|
|
174
|
+
private neighbourhood: SfuNeighbourhoodApi
|
|
175
|
+
private neighbourhoodUrl: string
|
|
176
|
+
private roomId: string
|
|
177
|
+
private agentDid: string
|
|
178
|
+
private state: SfuCallState
|
|
179
|
+
private callbacks: Map<SfuEvent, SfuEventCallback[]> = new Map()
|
|
180
|
+
private iceServers: RTCIceServer[]
|
|
181
|
+
/** Reconnects since ICE last reached connected — not since a join: a
|
|
182
|
+
* join can succeed while the media path never comes up. */
|
|
183
|
+
private reconnectAttempts: number = 0
|
|
184
|
+
private static readonly MAX_RECONNECTS = 3
|
|
185
|
+
/** How long one rebuild keeps retrying a join the executor cannot take yet. */
|
|
186
|
+
private static readonly REJOIN_WINDOW_MS = 120_000
|
|
187
|
+
/** How long ICE may stay disconnected before a rebuild: the executor's `ICE_DISCONNECT_GRACE`. */
|
|
188
|
+
private static readonly DISCONNECT_GRACE_MS = 10_000
|
|
189
|
+
/** A rebuild is running: a second failure signal joins it rather than starting another. */
|
|
190
|
+
private reconnecting = false
|
|
191
|
+
/** Bumped by `leave()`, so a reconnect in flight stops before rejoining. */
|
|
192
|
+
private generation: number = 0
|
|
193
|
+
/** Which participant each received media section carries, from the server's mappings. */
|
|
194
|
+
private midToParticipant: Map<string, string> = new Map()
|
|
195
|
+
/** DIDs the roster removed and has not listed since. */
|
|
196
|
+
private departed: Set<string> = new Set()
|
|
197
|
+
/** The sender of each kind of local media: declared at join, so a later camera or screen needs no renegotiation. */
|
|
198
|
+
private senders: Map<"audio" | "video", RTCRtpSender> = new Map()
|
|
199
|
+
/** Tracks `replaceTrack` set since the user's join, for a rebuilt connection to send. */
|
|
200
|
+
private replacedTracks: Map<"audio" | "video", MediaStreamTrack | null> = new Map()
|
|
201
|
+
/** The last quality preference set, for a rebuilt connection to restore. */
|
|
202
|
+
private qualityPreference: SfuQualityPreference | null = null
|
|
203
|
+
/** Server offers waiting to be applied, in arrival order, one at a time. */
|
|
204
|
+
private offerQueue: Promise<void> = Promise.resolve()
|
|
205
|
+
private renegotiationUnsubscribe: (() => void) | null = null
|
|
206
|
+
private rosterUnsubscribe: (() => void) | null = null
|
|
207
|
+
private dataChannelUnsubscribe: (() => void) | null = null
|
|
208
|
+
private disconnectedTimer: ReturnType<typeof setTimeout> | null = null
|
|
209
|
+
|
|
210
|
+
constructor(
|
|
211
|
+
neighbourhood: SfuNeighbourhoodApi,
|
|
212
|
+
roomId: string,
|
|
213
|
+
agentDid: string,
|
|
214
|
+
neighbourhoodUrl?: string,
|
|
215
|
+
iceConfig?: SfuIceConfig,
|
|
216
|
+
sfuConfig?: SfuConfig,
|
|
217
|
+
) {
|
|
218
|
+
this.neighbourhood = neighbourhood
|
|
219
|
+
this.neighbourhoodUrl = neighbourhoodUrl || ""
|
|
220
|
+
this.roomId = roomId
|
|
221
|
+
this.agentDid = agentDid
|
|
222
|
+
this.state = {
|
|
223
|
+
topology: "sfu",
|
|
224
|
+
roomId,
|
|
225
|
+
participantId: null,
|
|
226
|
+
sfuPeerDid: null,
|
|
227
|
+
participants: new Map(),
|
|
228
|
+
localStream: null,
|
|
229
|
+
peerConnection: null,
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Resolution order for ICE servers:
|
|
233
|
+
// 1. iceConfig.servers — the app's (Session's `iceServers`)
|
|
234
|
+
// 2. sfuConfig.iceServers
|
|
235
|
+
// 3. DEFAULT_ICE_SERVERS — public STUN fallback
|
|
236
|
+
let servers: RTCIceServer[] = []
|
|
237
|
+
if (iceConfig?.servers && iceConfig.servers.length > 0) {
|
|
238
|
+
servers = iceConfig.servers
|
|
239
|
+
} else if (sfuConfig?.iceServers && sfuConfig.iceServers.length > 0) {
|
|
240
|
+
servers = sfuConfig.iceServers.map((s) => ({
|
|
241
|
+
urls: s.urls,
|
|
242
|
+
username: s.username,
|
|
243
|
+
credential: s.credential,
|
|
244
|
+
}))
|
|
245
|
+
}
|
|
246
|
+
this.iceServers = servers.length > 0 ? servers : DEFAULT_ICE_SERVERS
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Rebuild the call after its connection failed: leave, then join again
|
|
251
|
+
* on this executor. Leaving first matters: the executor still holds this
|
|
252
|
+
* DID's old session until it notices the connection died.
|
|
253
|
+
*
|
|
254
|
+
* Two budgets. A rebuild whose join works but whose connection fails
|
|
255
|
+
* again counts once against `MAX_RECONNECTS`, reset when ICE connects.
|
|
256
|
+
* Within one rebuild, a join that fails because the executor cannot be
|
|
257
|
+
* reached — a restart, an outage — is retried with backoff for up to
|
|
258
|
+
* `REJOIN_WINDOW_MS`, so the call comes back on its own once it can.
|
|
259
|
+
*/
|
|
260
|
+
private async reconnect(): Promise<void> {
|
|
261
|
+
const stream = this.state.localStream
|
|
262
|
+
if (!stream || this.reconnecting) return
|
|
263
|
+
const generation = this.generation
|
|
264
|
+
if (++this.reconnectAttempts > SfuManager.MAX_RECONNECTS) {
|
|
265
|
+
this.emit("error", new Error("SFU reconnect attempts exhausted"))
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
console.info(
|
|
269
|
+
`SFU: connection failed, reconnecting (${this.reconnectAttempts}/${SfuManager.MAX_RECONNECTS})`,
|
|
270
|
+
)
|
|
271
|
+
this.reconnecting = true
|
|
272
|
+
try {
|
|
273
|
+
const deadline = Date.now() + SfuManager.REJOIN_WINDOW_MS
|
|
274
|
+
for (let delay = 1000; ; delay = Math.min(delay * 2, 10_000)) {
|
|
275
|
+
this.releaseServerEvents()
|
|
276
|
+
if (this.state.peerConnection) {
|
|
277
|
+
this.state.peerConnection.close()
|
|
278
|
+
this.state.peerConnection = null
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
await this.neighbourhood.sfuCallLeave(this.neighbourhoodUrl, this.roomId)
|
|
282
|
+
} catch {
|
|
283
|
+
/* best-effort: the executor frees a dead session on its own */
|
|
284
|
+
}
|
|
285
|
+
if (generation !== this.generation) return // left meanwhile
|
|
286
|
+
this.resetParticipants()
|
|
287
|
+
try {
|
|
288
|
+
await this.join(stream)
|
|
289
|
+
if (this.qualityPreference) await this.setQualityPreference(this.qualityPreference)
|
|
290
|
+
return
|
|
291
|
+
} catch (e) {
|
|
292
|
+
if (generation !== this.generation) return
|
|
293
|
+
if (Date.now() + delay > deadline) {
|
|
294
|
+
console.error("SFU reconnect failed:", e)
|
|
295
|
+
this.emit("error", e)
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
console.warn(`SFU: rejoin failed, retrying in ${delay} ms:`, e)
|
|
299
|
+
await new Promise((resolve) => setTimeout(resolve, delay))
|
|
300
|
+
if (generation !== this.generation) return
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
} finally {
|
|
304
|
+
this.reconnecting = false
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
on(event: SfuEvent, callback: SfuEventCallback): void {
|
|
309
|
+
if (!this.callbacks.has(event)) {
|
|
310
|
+
this.callbacks.set(event, [])
|
|
311
|
+
}
|
|
312
|
+
this.callbacks.get(event)!.push(callback)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
off(event: SfuEvent, callback?: SfuEventCallback): void {
|
|
316
|
+
if (!callback) {
|
|
317
|
+
this.callbacks.delete(event)
|
|
318
|
+
return
|
|
319
|
+
}
|
|
320
|
+
const cbs = this.callbacks.get(event)
|
|
321
|
+
if (cbs) {
|
|
322
|
+
const idx = cbs.indexOf(callback)
|
|
323
|
+
if (idx !== -1) cbs.splice(idx, 1)
|
|
324
|
+
if (cbs.length === 0) this.callbacks.delete(event)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private emit(event: SfuEvent, ...args: any[]): void {
|
|
329
|
+
const cbs = this.callbacks.get(event)
|
|
330
|
+
if (cbs) {
|
|
331
|
+
for (const cb of cbs) {
|
|
332
|
+
try {
|
|
333
|
+
cb(...args)
|
|
334
|
+
} catch (e) {
|
|
335
|
+
console.error(
|
|
336
|
+
`SFU event handler error (${event}):`,
|
|
337
|
+
e,
|
|
338
|
+
)
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Join the SFU call with simulcast support. */
|
|
345
|
+
async join(localStream: MediaStream): Promise<void> {
|
|
346
|
+
this.state.localStream = localStream
|
|
347
|
+
|
|
348
|
+
const pc = new RTCPeerConnection({ iceServers: this.iceServers })
|
|
349
|
+
this.state.peerConnection = pc
|
|
350
|
+
|
|
351
|
+
// ICE state monitoring for reconnecting.
|
|
352
|
+
// "disconnected" is often transient (network blip, route change):
|
|
353
|
+
// the connection gets the same grace the server gives it before
|
|
354
|
+
// dropping the peer, and a rebuild only follows once the server
|
|
355
|
+
// would have dropped it anyway. "failed" reconnects at once.
|
|
356
|
+
pc.oniceconnectionstatechange = () => {
|
|
357
|
+
if (this.state.peerConnection !== pc) return // superseded
|
|
358
|
+
if (pc.iceConnectionState === "failed") {
|
|
359
|
+
if (this.disconnectedTimer) {
|
|
360
|
+
clearTimeout(this.disconnectedTimer)
|
|
361
|
+
this.disconnectedTimer = null
|
|
362
|
+
}
|
|
363
|
+
this.reconnect()
|
|
364
|
+
} else if (pc.iceConnectionState === "disconnected") {
|
|
365
|
+
if (!this.disconnectedTimer) {
|
|
366
|
+
this.disconnectedTimer = setTimeout(() => {
|
|
367
|
+
this.disconnectedTimer = null
|
|
368
|
+
// Only reconnect if this pc is still the active one
|
|
369
|
+
// and still disconnected.
|
|
370
|
+
if (
|
|
371
|
+
this.state.peerConnection === pc &&
|
|
372
|
+
pc.iceConnectionState === "disconnected"
|
|
373
|
+
) {
|
|
374
|
+
this.reconnect()
|
|
375
|
+
}
|
|
376
|
+
}, SfuManager.DISCONNECT_GRACE_MS)
|
|
377
|
+
}
|
|
378
|
+
} else {
|
|
379
|
+
// Any other state (connected, completed, checking) cancels
|
|
380
|
+
// the pending disconnected timer; a working path ends a run
|
|
381
|
+
// of reconnects.
|
|
382
|
+
if (
|
|
383
|
+
pc.iceConnectionState === "connected" ||
|
|
384
|
+
pc.iceConnectionState === "completed"
|
|
385
|
+
) {
|
|
386
|
+
this.reconnectAttempts = 0
|
|
387
|
+
}
|
|
388
|
+
if (this.disconnectedTimer) {
|
|
389
|
+
clearTimeout(this.disconnectedTimer)
|
|
390
|
+
this.disconnectedTimer = null
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Both kinds of local media, send-only, declared now whether or not a
|
|
396
|
+
// track exists yet: the SFU only ever receives on these sections, and a
|
|
397
|
+
// camera or screen started later replaces an empty track with no
|
|
398
|
+
// renegotiation. Video goes out as three simulcast layers.
|
|
399
|
+
this.senders.clear()
|
|
400
|
+
for (const kind of ["audio", "video"] as const) {
|
|
401
|
+
const track = this.replacedTracks.has(kind)
|
|
402
|
+
? this.replacedTracks.get(kind)
|
|
403
|
+
: localStream.getTracks().find((t) => t.kind === kind)
|
|
404
|
+
const transceiver = pc.addTransceiver(track ?? kind, {
|
|
405
|
+
direction: "sendonly",
|
|
406
|
+
streams: [localStream],
|
|
407
|
+
...(kind === "video"
|
|
408
|
+
? {
|
|
409
|
+
sendEncodings: [
|
|
410
|
+
{ rid: "high", maxBitrate: 1500000 },
|
|
411
|
+
{ rid: "medium", maxBitrate: 500000, scaleResolutionDownBy: 2 },
|
|
412
|
+
{ rid: "low", maxBitrate: 150000, scaleResolutionDownBy: 4 },
|
|
413
|
+
],
|
|
414
|
+
}
|
|
415
|
+
: {}),
|
|
416
|
+
})
|
|
417
|
+
this.senders.set(kind, transceiver.sender)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Incoming media belongs to the participant the server mapped its
|
|
421
|
+
// section to. A track on a section with no mapping is not someone's
|
|
422
|
+
// media, so it is left alone rather than attributed by guesswork.
|
|
423
|
+
pc.ontrack = (event: RTCTrackEvent) => {
|
|
424
|
+
if (this.state.peerConnection !== pc) return
|
|
425
|
+
const mid = event.transceiver?.mid
|
|
426
|
+
const did = mid ? this.midToParticipant.get(mid) : undefined
|
|
427
|
+
// Someone the roster said left stays gone: a late offer can still name them.
|
|
428
|
+
if (!did || did === this.agentDid || this.departed.has(did)) return
|
|
429
|
+
const participant = this.ensureParticipant(did)
|
|
430
|
+
participant.stream.addTrack(event.track)
|
|
431
|
+
this.refreshKinds(participant)
|
|
432
|
+
this.emit("stream-added", participant.stream, event.track)
|
|
433
|
+
event.track.addEventListener("ended", () => {
|
|
434
|
+
participant.stream.removeTrack(event.track)
|
|
435
|
+
this.refreshKinds(participant)
|
|
436
|
+
this.emit("stream-removed", participant.stream, event.track)
|
|
437
|
+
})
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// ── Trickle ICE ──────────────────────────────────────────────
|
|
441
|
+
//
|
|
442
|
+
// Buffer candidates gathered before callJoin returns (the
|
|
443
|
+
// server needs the participant registered first). After
|
|
444
|
+
// callJoin completes, flush the buffer and trickle live.
|
|
445
|
+
|
|
446
|
+
const pendingCandidates: string[] = []
|
|
447
|
+
let joinComplete = false
|
|
448
|
+
|
|
449
|
+
pc.onicecandidate = (event) => {
|
|
450
|
+
if (!event.candidate) return
|
|
451
|
+
if (joinComplete) {
|
|
452
|
+
this.neighbourhood.sfuAddIceCandidate(
|
|
453
|
+
this.neighbourhoodUrl,
|
|
454
|
+
this.roomId,
|
|
455
|
+
event.candidate.candidate,
|
|
456
|
+
).catch((err) => {
|
|
457
|
+
console.error(
|
|
458
|
+
"SFU: failed to trickle ICE candidate:",
|
|
459
|
+
err,
|
|
460
|
+
)
|
|
461
|
+
})
|
|
462
|
+
} else {
|
|
463
|
+
pendingCandidates.push(event.candidate.candidate)
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Create offer and send immediately — no ICE gathering wait
|
|
468
|
+
const offer = await pc.createOffer()
|
|
469
|
+
await pc.setLocalDescription(offer)
|
|
470
|
+
|
|
471
|
+
const sdpOffer = JSON.stringify(pc.localDescription)
|
|
472
|
+
// Server offers can arrive before the join's own answer is applied;
|
|
473
|
+
// they wait for it, then apply in order.
|
|
474
|
+
let markJoined!: () => void
|
|
475
|
+
const joined = new Promise<void>((resolve) => { markJoined = resolve })
|
|
476
|
+
this.subscribeServerEvents(pc, joined)
|
|
477
|
+
let session: CallSessionInfo
|
|
478
|
+
try {
|
|
479
|
+
session = await this.neighbourhood.sfuCallJoin(
|
|
480
|
+
this.neighbourhoodUrl,
|
|
481
|
+
this.roomId,
|
|
482
|
+
sdpOffer,
|
|
483
|
+
)
|
|
484
|
+
} catch (err) {
|
|
485
|
+
// Settled either way: offers queued behind this join reach
|
|
486
|
+
// `applyServerOffer`, which drops them for the abandoned connection.
|
|
487
|
+
markJoined()
|
|
488
|
+
this.abandon(pc)
|
|
489
|
+
throw err
|
|
490
|
+
}
|
|
491
|
+
this.state.participantId = session.participantId
|
|
492
|
+
|
|
493
|
+
try {
|
|
494
|
+
const answer = JSON.parse(session.sdpAnswer)
|
|
495
|
+
await pc.setRemoteDescription(new RTCSessionDescription(answer))
|
|
496
|
+
} catch (err) {
|
|
497
|
+
markJoined()
|
|
498
|
+
this.abandon(pc)
|
|
499
|
+
throw err
|
|
500
|
+
}
|
|
501
|
+
markJoined()
|
|
502
|
+
|
|
503
|
+
// Who was in the room when this join landed (`participantId:did`).
|
|
504
|
+
// The roster event keeps it current from here.
|
|
505
|
+
this.applyRoster(session.streamMapping.map((entry) => entry.slice(entry.indexOf(":") + 1)), false)
|
|
506
|
+
|
|
507
|
+
// Flush buffered trickle ICE candidates
|
|
508
|
+
joinComplete = true
|
|
509
|
+
for (const candidate of pendingCandidates) {
|
|
510
|
+
this.neighbourhood.sfuAddIceCandidate(
|
|
511
|
+
this.neighbourhoodUrl,
|
|
512
|
+
this.roomId,
|
|
513
|
+
candidate,
|
|
514
|
+
).catch((err) => {
|
|
515
|
+
console.error(
|
|
516
|
+
"SFU: failed to trickle buffered ICE candidate:",
|
|
517
|
+
err,
|
|
518
|
+
)
|
|
519
|
+
})
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** Drop the server-event subscriptions of the current join. */
|
|
524
|
+
private releaseServerEvents(): void {
|
|
525
|
+
this.renegotiationUnsubscribe?.()
|
|
526
|
+
this.renegotiationUnsubscribe = null
|
|
527
|
+
this.rosterUnsubscribe?.()
|
|
528
|
+
this.rosterUnsubscribe = null
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Undo a join that failed part way: its subscription and its connection. */
|
|
532
|
+
private abandon(pc: RTCPeerConnection): void {
|
|
533
|
+
this.releaseServerEvents()
|
|
534
|
+
pc.close()
|
|
535
|
+
if (this.state.peerConnection === pc) this.state.peerConnection = null
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Subscribe to the server's events for this join: renegotiation offers
|
|
540
|
+
* and the room roster. `join` calls this before `sfuCallJoin`: the
|
|
541
|
+
* executor sends a socket only the events it watches, and a call
|
|
542
|
+
* carries the pending watch ahead of itself, so no event the join
|
|
543
|
+
* causes is lost. Offers wait for `joined` and then apply one at a
|
|
544
|
+
* time, in arrival order; one that arrives for a connection already
|
|
545
|
+
* replaced is dropped.
|
|
546
|
+
*/
|
|
547
|
+
private subscribeServerEvents(pc: RTCPeerConnection, joined: Promise<void>): void {
|
|
548
|
+
// Join may run more than once (a reconnect). The prior
|
|
549
|
+
// subscriptions go after the new ones exist, so the event socket
|
|
550
|
+
// never drops to zero listeners in between.
|
|
551
|
+
const previous = [this.renegotiationUnsubscribe, this.rosterUnsubscribe]
|
|
552
|
+
const ours = (e: { neighbourhoodUrl: string; roomName: string }) =>
|
|
553
|
+
e.neighbourhoodUrl === this.neighbourhoodUrl && e.roomName === this.roomId
|
|
554
|
+
|
|
555
|
+
this.renegotiationUnsubscribe = this.neighbourhood.subscribeSfuCallRenegotiationOffer(
|
|
556
|
+
this.agentDid,
|
|
557
|
+
(event) => {
|
|
558
|
+
if (!ours(event)) return
|
|
559
|
+
this.offerQueue = this.offerQueue
|
|
560
|
+
.then(() => joined)
|
|
561
|
+
.then(() => this.applyServerOffer(pc, event))
|
|
562
|
+
},
|
|
563
|
+
)
|
|
564
|
+
this.rosterUnsubscribe = this.neighbourhood.subscribeSfuRoomRoster((roster) => {
|
|
565
|
+
if (!ours(roster) || this.state.peerConnection !== pc) return
|
|
566
|
+
void joined.then(() => {
|
|
567
|
+
if (this.state.peerConnection === pc) this.applyRoster(roster.participants, true)
|
|
568
|
+
})
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
for (const unsubscribe of previous) unsubscribe?.()
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** Apply one server offer to `pc` and answer it, unless `pc` is no longer the call's. */
|
|
575
|
+
private async applyServerOffer(pc: RTCPeerConnection, event: SfuCallRenegotiationOffer): Promise<void> {
|
|
576
|
+
if (this.state.peerConnection !== pc) return
|
|
577
|
+
// The mapping first: the tracks this offer adds arrive while it is applied.
|
|
578
|
+
for (const entry of event.trackMapping ?? []) {
|
|
579
|
+
this.midToParticipant.set(entry.mid, entry.agentDid)
|
|
580
|
+
}
|
|
581
|
+
try {
|
|
582
|
+
await pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(event.sdpOffer)))
|
|
583
|
+
await pc.setLocalDescription(await pc.createAnswer())
|
|
584
|
+
await this.neighbourhood.sfuCallAnswerServerOffer(
|
|
585
|
+
this.neighbourhoodUrl,
|
|
586
|
+
event.roomName,
|
|
587
|
+
JSON.stringify(pc.localDescription),
|
|
588
|
+
)
|
|
589
|
+
} catch (err) {
|
|
590
|
+
if (this.state.peerConnection !== pc) return
|
|
591
|
+
// A renegotiation that fails leaves this connection out of step
|
|
592
|
+
// with the server — or the server has already dropped it. Either
|
|
593
|
+
// way the call is rebuilt; `reconnect` reports an error only if
|
|
594
|
+
// that fails too.
|
|
595
|
+
console.warn("SFU: renegotiation failed, rebuilding the connection:", err)
|
|
596
|
+
void this.reconnect()
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Make the participants match `dids`, the room's roster: everyone new
|
|
602
|
+
* joins, and — when `complete`, as the roster event is — everyone
|
|
603
|
+
* missing leaves. The join's own roster can lag the room, so it only adds.
|
|
604
|
+
*/
|
|
605
|
+
private applyRoster(dids: string[], complete: boolean): void {
|
|
606
|
+
const present = new Set(dids.filter((did) => did !== this.agentDid))
|
|
607
|
+
for (const did of present) {
|
|
608
|
+
this.departed.delete(did)
|
|
609
|
+
this.ensureParticipant(did)
|
|
610
|
+
}
|
|
611
|
+
if (!complete) return
|
|
612
|
+
for (const [did, participant] of [...this.state.participants]) {
|
|
613
|
+
if (present.has(did)) continue
|
|
614
|
+
this.state.participants.delete(did)
|
|
615
|
+
this.departed.add(did)
|
|
616
|
+
for (const [mid, owner] of this.midToParticipant) {
|
|
617
|
+
if (owner === did) this.midToParticipant.delete(mid)
|
|
618
|
+
}
|
|
619
|
+
this.emit("participant-left", participant)
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** The participant for `did`, joining them to the call if they are new. */
|
|
624
|
+
private ensureParticipant(did: string): SfuParticipantState {
|
|
625
|
+
let participant = this.state.participants.get(did)
|
|
626
|
+
if (!participant) {
|
|
627
|
+
participant = {
|
|
628
|
+
did,
|
|
629
|
+
stream: new MediaStream(),
|
|
630
|
+
hasAudio: false,
|
|
631
|
+
hasVideo: false,
|
|
632
|
+
isActiveSpeaker: false,
|
|
633
|
+
}
|
|
634
|
+
this.state.participants.set(did, participant)
|
|
635
|
+
this.emit("participant-joined", participant)
|
|
636
|
+
}
|
|
637
|
+
return participant
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
private refreshKinds(participant: SfuParticipantState): void {
|
|
641
|
+
participant.hasAudio = participant.stream.getAudioTracks().length > 0
|
|
642
|
+
participant.hasVideo = participant.stream.getVideoTracks().length > 0
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
async leave(): Promise<void> {
|
|
646
|
+
this.generation++
|
|
647
|
+
this.releaseServerEvents()
|
|
648
|
+
if (this.disconnectedTimer) {
|
|
649
|
+
clearTimeout(this.disconnectedTimer)
|
|
650
|
+
this.disconnectedTimer = null
|
|
651
|
+
}
|
|
652
|
+
this.senders.clear()
|
|
653
|
+
this.replacedTracks.clear()
|
|
654
|
+
this.qualityPreference = null
|
|
655
|
+
if (this.dataChannelUnsubscribe) {
|
|
656
|
+
try {
|
|
657
|
+
this.dataChannelUnsubscribe()
|
|
658
|
+
} catch {
|
|
659
|
+
/* swallow */
|
|
660
|
+
}
|
|
661
|
+
this.dataChannelUnsubscribe = null
|
|
662
|
+
}
|
|
663
|
+
if (this.state.peerConnection) {
|
|
664
|
+
this.state.peerConnection.close()
|
|
665
|
+
this.state.peerConnection = null
|
|
666
|
+
}
|
|
667
|
+
try {
|
|
668
|
+
await this.neighbourhood.sfuCallLeave(
|
|
669
|
+
this.neighbourhoodUrl,
|
|
670
|
+
this.roomId,
|
|
671
|
+
)
|
|
672
|
+
} catch (e) {
|
|
673
|
+
console.error("Error leaving SFU call:", e)
|
|
674
|
+
}
|
|
675
|
+
this.resetParticipants()
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** Forget the call's participants, telling listeners each one left. */
|
|
679
|
+
private resetParticipants(): void {
|
|
680
|
+
for (const [, participant] of this.state.participants) {
|
|
681
|
+
this.emit("participant-left", participant)
|
|
682
|
+
}
|
|
683
|
+
this.state.participants.clear()
|
|
684
|
+
this.state.participantId = null
|
|
685
|
+
this.midToParticipant.clear()
|
|
686
|
+
this.departed.clear()
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
async setQualityPreference(
|
|
690
|
+
preference: SfuQualityPreference,
|
|
691
|
+
): Promise<void> {
|
|
692
|
+
this.qualityPreference = preference
|
|
693
|
+
if (!this.state.participantId) {
|
|
694
|
+
console.warn(
|
|
695
|
+
"Cannot set quality preference: not connected to SFU",
|
|
696
|
+
)
|
|
697
|
+
return
|
|
698
|
+
}
|
|
699
|
+
try {
|
|
700
|
+
await this.neighbourhood.sfuCallSetQualityPreference(
|
|
701
|
+
this.neighbourhoodUrl,
|
|
702
|
+
this.roomId,
|
|
703
|
+
preference,
|
|
704
|
+
)
|
|
705
|
+
} catch (e) {
|
|
706
|
+
console.error("Failed to set quality preference:", e)
|
|
707
|
+
this.emit("error", e)
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
getState(): Readonly<SfuCallState> {
|
|
712
|
+
return this.state
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
getParticipants(): SfuParticipantState[] {
|
|
716
|
+
return Array.from(this.state.participants.values())
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// ── Track replacement ──────────────────────────────────────────
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Replace the outbound track of a given kind on the SFU peer connection.
|
|
723
|
+
*
|
|
724
|
+
* Both kinds were declared at join, so this is always
|
|
725
|
+
* `RTCRtpSender.replaceTrack` — no renegotiation. Pass `null` to stop
|
|
726
|
+
* sending that kind.
|
|
727
|
+
*/
|
|
728
|
+
async replaceTrack(kind: "audio" | "video", track: MediaStreamTrack | null): Promise<void> {
|
|
729
|
+
this.replacedTracks.set(kind, track)
|
|
730
|
+
// Between connections (a rebuild) the next join sends it.
|
|
731
|
+
if (this.state.peerConnection) await this.senders.get(kind)?.replaceTrack(track)
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// ── Data channel relay ──────────────────────────────────────────
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Send data to all other participants in the room via the SFU.
|
|
738
|
+
* Text payloads pass as-is; binary payloads should arrive
|
|
739
|
+
* base64-encoded with `binary: true`.
|
|
740
|
+
*/
|
|
741
|
+
async sendData(
|
|
742
|
+
channelLabel: string,
|
|
743
|
+
data: string,
|
|
744
|
+
binary: boolean = false,
|
|
745
|
+
): Promise<void> {
|
|
746
|
+
if (!this.state.participantId) {
|
|
747
|
+
console.warn("Cannot send data: not connected to SFU")
|
|
748
|
+
return
|
|
749
|
+
}
|
|
750
|
+
try {
|
|
751
|
+
await this.neighbourhood.sfuSendData(
|
|
752
|
+
this.neighbourhoodUrl,
|
|
753
|
+
this.roomId,
|
|
754
|
+
channelLabel,
|
|
755
|
+
data,
|
|
756
|
+
binary,
|
|
757
|
+
)
|
|
758
|
+
} catch (e) {
|
|
759
|
+
console.error("SFU: sendData failed:", e)
|
|
760
|
+
this.emit("error", e)
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Subscribe to data channel messages from other participants.
|
|
766
|
+
* Returns an unsubscribe function. Only one subscription per
|
|
767
|
+
* manager instance — a second call replaces the first.
|
|
768
|
+
*/
|
|
769
|
+
subscribeDataChannel(
|
|
770
|
+
callback: (message: SfuDataMessage) => void,
|
|
771
|
+
): () => void {
|
|
772
|
+
// Tear down any existing subscription
|
|
773
|
+
if (this.dataChannelUnsubscribe) {
|
|
774
|
+
try {
|
|
775
|
+
this.dataChannelUnsubscribe()
|
|
776
|
+
} catch {
|
|
777
|
+
/* swallow */
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
this.dataChannelUnsubscribe =
|
|
781
|
+
this.neighbourhood.subscribeSfuDataChannel(callback)
|
|
782
|
+
return () => {
|
|
783
|
+
if (this.dataChannelUnsubscribe) {
|
|
784
|
+
try {
|
|
785
|
+
this.dataChannelUnsubscribe()
|
|
786
|
+
} catch {
|
|
787
|
+
/* swallow */
|
|
788
|
+
}
|
|
789
|
+
this.dataChannelUnsubscribe = null
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
async destroy(): Promise<void> {
|
|
795
|
+
try {
|
|
796
|
+
await this.leave()
|
|
797
|
+
} catch (e) {
|
|
798
|
+
console.error("Error during SFU destroy:", e)
|
|
799
|
+
}
|
|
800
|
+
this.callbacks.clear()
|
|
801
|
+
this.midToParticipant.clear()
|
|
802
|
+
}
|
|
803
|
+
}
|