@raydiculousx/ts-sdk 0.0.0-stage → 1.0.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 +7 -0
- package/LICENSE +55 -0
- package/NOTICE +14 -0
- package/README.md +91 -3
- package/dist/THIRD_PARTY_NOTICES.txt +466 -0
- package/dist/host/src/bootstrap/client.d.ts +37 -0
- package/dist/host/src/bootstrap/client.js +215 -0
- package/dist/host/src/bootstrap/host.d.ts +79 -0
- package/dist/host/src/bootstrap/host.js +214 -0
- package/dist/host/src/bootstrap/protocol.d.ts +20 -0
- package/dist/host/src/bootstrap/protocol.js +72 -0
- package/dist/host/src/dev.d.ts +30 -0
- package/dist/host/src/dev.js +20 -0
- package/dist/host/src/entry/app.d.ts +10 -0
- package/dist/host/src/entry/app.js +9 -0
- package/dist/host/src/entry/auto.d.ts +16 -0
- package/dist/host/src/entry/auto.js +18747 -0
- package/dist/host/src/standalone/account-api.d.ts +32 -0
- package/dist/host/src/standalone/account-api.js +176 -0
- package/dist/host/src/standalone/connection-slot.d.ts +12 -0
- package/dist/host/src/standalone/connection-slot.js +28 -0
- package/dist/host/src/standalone/device-picker.d.ts +6 -0
- package/dist/host/src/standalone/device-picker.js +19 -0
- package/dist/host/src/standalone/locale.d.ts +4 -0
- package/dist/host/src/standalone/locale.js +35 -0
- package/dist/host/src/standalone/mount-host.d.ts +155 -0
- package/dist/host/src/standalone/mount-host.js +396 -0
- package/dist/host/src/standalone/mount-surface.d.ts +12 -0
- package/dist/host/src/standalone/mount-surface.js +69 -0
- package/dist/host/src/standalone/robot-setup.d.ts +61 -0
- package/dist/host/src/standalone/robot-setup.js +160 -0
- package/dist/lib/client/action-ownership.d.ts +31 -0
- package/dist/lib/client/action-ownership.js +182 -0
- package/dist/lib/client/direct.d.ts +149 -0
- package/dist/lib/client/direct.js +560 -0
- package/dist/lib/client/raydiculousx.d.ts +541 -0
- package/dist/lib/client/raydiculousx.js +694 -0
- package/dist/lib/client/runtime.d.ts +165 -0
- package/dist/lib/client/runtime.js +1 -0
- package/dist/lib/core/index.d.ts +16 -0
- package/dist/lib/core/index.js +9 -0
- package/dist/lib/core/rpc/command-channel.d.ts +345 -0
- package/dist/lib/core/rpc/command-channel.js +697 -0
- package/dist/lib/core/rpc/commands.d.ts +354 -0
- package/dist/lib/core/rpc/commands.js +134 -0
- package/dist/lib/core/rpc/request-policy.d.ts +29 -0
- package/dist/lib/core/rpc/request-policy.js +35 -0
- package/dist/lib/core/rpc/robot-api.d.ts +184 -0
- package/dist/lib/core/rpc/robot-api.js +34 -0
- package/dist/lib/core/signaling/ice-servers.d.ts +10 -0
- package/dist/lib/core/signaling/ice-servers.js +58 -0
- package/dist/lib/core/signaling/peer-negotiator.d.ts +164 -0
- package/dist/lib/core/signaling/peer-negotiator.js +366 -0
- package/dist/lib/core/signaling/signaling-client.d.ts +204 -0
- package/dist/lib/core/signaling/signaling-client.js +473 -0
- package/dist/lib/core/signaling/signaling-contract.d.ts +164 -0
- package/dist/lib/core/signaling/signaling-contract.js +35 -0
- package/dist/lib/core/signaling/signaling.d.ts +172 -0
- package/dist/lib/core/signaling/signaling.js +381 -0
- package/dist/lib/media/attach-video.d.ts +3 -0
- package/dist/lib/media/attach-video.js +243 -0
- package/dist/lib/media/direct-audio.d.ts +36 -0
- package/dist/lib/media/direct-audio.js +94 -0
- package/dist/lib/media/index.d.ts +5 -0
- package/dist/lib/media/index.js +3 -0
- package/dist/lib/media/pcm-types.d.ts +33 -0
- package/dist/lib/media/pcm-types.js +1 -0
- package/dist/lib/media/robot-audio.d.ts +64 -0
- package/dist/lib/media/robot-audio.js +82 -0
- package/dist/lib/media/robot-pcm-output.d.ts +16 -0
- package/dist/lib/media/robot-pcm-output.js +101 -0
- package/dist/lib/robot/action-upload.d.ts +32 -0
- package/dist/lib/robot/action-upload.js +143 -0
- package/dist/lib/robot/robot-commands.d.ts +148 -0
- package/dist/lib/robot/robot-commands.js +164 -0
- package/dist/lib/robot/robot-responses.d.ts +42 -0
- package/dist/lib/robot/robot-responses.js +226 -0
- package/dist/lib/robot/robot-tof.d.ts +61 -0
- package/dist/lib/robot/robot-tof.js +25 -0
- package/dist/lib/robot/robot-types.d.ts +293 -0
- package/dist/lib/robot/robot-types.js +17 -0
- package/dist/lib/shared/errors.d.ts +57 -0
- package/dist/lib/shared/errors.js +45 -0
- package/dist/lib/shared/events.d.ts +11 -0
- package/dist/lib/shared/events.js +40 -0
- package/dist/lib/shared/types.d.ts +26 -0
- package/dist/lib/shared/types.js +1 -0
- package/dist/lib/shared/validation.d.ts +29 -0
- package/dist/lib/shared/validation.js +87 -0
- package/dist/lib/version.d.ts +2 -0
- package/dist/lib/version.js +3 -0
- package/dist/raydiculousx-sdk.d.ts +26 -0
- package/dist/raydiculousx-sdk.js +17 -0
- package/docs/API.md +1106 -0
- package/docs/DIRECT_APPS.md +83 -0
- package/docs/HOST_HANDOFF.md +59 -0
- package/docs/HOST_PROTOCOL.md +43 -0
- package/docs/RELEASING.md +33 -0
- package/docs/assets/raydiculousx-mark.png +0 -0
- package/examples/README.md +10 -0
- package/examples/application-template/README.md +60 -0
- package/examples/application-template/index.html +12 -0
- package/examples/application-template/package.json +21 -0
- package/examples/application-template/src/app.config.ts +8 -0
- package/examples/application-template/src/app.ts +81 -0
- package/examples/application-template/src/dispatch.ts +29 -0
- package/examples/application-template/src/embed.ts +39 -0
- package/examples/application-template/src/env.d.ts +10 -0
- package/examples/application-template/src/host-origin.ts +9 -0
- package/examples/application-template/src/locale.ts +90 -0
- package/examples/application-template/src/styles.css +31 -0
- package/examples/application-template/tsconfig.json +24 -0
- package/examples/application-template/vite.config.mjs +16 -0
- package/examples/sdk-diagnostics/README.md +199 -0
- package/examples/sdk-diagnostics/assets/THIRD_PARTY_NOTICES.txt +567 -0
- package/examples/sdk-diagnostics/assets/auto-CnsixhmH.js +9 -0
- package/examples/sdk-diagnostics/assets/console-BjvPHSNK.css +1 -0
- package/examples/sdk-diagnostics/assets/console-BrwFsb65.js +9 -0
- package/examples/sdk-diagnostics/assets/favicon-BZQEit-G.ico +0 -0
- package/examples/sdk-diagnostics/assets/index-DXSJUMrS.js +2 -0
- package/examples/sdk-diagnostics/assets/messages-C1UCo5nT.js +1 -0
- package/examples/sdk-diagnostics/assets/robot-x3-D4wBvBN8.webp +0 -0
- package/examples/sdk-diagnostics/assets/styles-D3TrErcf.css +1 -0
- package/examples/sdk-diagnostics/index.html +15 -0
- package/host/README.md +34 -0
- package/package.json +80 -4
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebRTC 协商器,负责当前页面的连接资源。
|
|
3
|
+
*
|
|
4
|
+
* 边界(严格):
|
|
5
|
+
* - 只做 RTCPeerConnection 建立、SDP 协商、ICE 交换
|
|
6
|
+
* - 建成后交出可用句柄,仍负责连接资源的关闭
|
|
7
|
+
* - 不发送任何业务指令,不解析 telemetry
|
|
8
|
+
* - 不碰 DOM
|
|
9
|
+
*/
|
|
10
|
+
/* ───────────────────────────── 主体 ───────────────────────────── */
|
|
11
|
+
/**
|
|
12
|
+
* 管理一个 RTCPeerConnection、SDP/ICE 交换、数据通道及可选静音占位轨道。
|
|
13
|
+
* @remarks 不发送机器人命令。异步方法没有额外超时或 AbortSignal,调用方通过 close 释放资源。
|
|
14
|
+
*/
|
|
15
|
+
export class PeerNegotiator {
|
|
16
|
+
pc;
|
|
17
|
+
control = null;
|
|
18
|
+
telemetry = null;
|
|
19
|
+
blob = null;
|
|
20
|
+
tof = null;
|
|
21
|
+
remoteStream = null;
|
|
22
|
+
handlers = new Map();
|
|
23
|
+
/** 候选可能在 answer 之前到达,必须缓冲 */
|
|
24
|
+
pendingCandidates = [];
|
|
25
|
+
remoteDescSet = false;
|
|
26
|
+
audioCtx = null;
|
|
27
|
+
placeholderTracks = [];
|
|
28
|
+
closed = false;
|
|
29
|
+
establishedEmitted = false;
|
|
30
|
+
sn;
|
|
31
|
+
deviceName = null;
|
|
32
|
+
media;
|
|
33
|
+
/**
|
|
34
|
+
* 创建浏览器 WebRTC 资源并准备所需通道与占位音轨。
|
|
35
|
+
* @param opts - 目标设备、ICE 服务器及媒体配置,见 {@link NegotiatorOptions}。
|
|
36
|
+
* @throws 浏览器无法创建 WebRTC 或数据通道时同步抛出。
|
|
37
|
+
* @remarks 由调用方调用 createOffer 或 applyOffer 开始协商。
|
|
38
|
+
*/
|
|
39
|
+
constructor(opts) {
|
|
40
|
+
this.sn = opts.sn;
|
|
41
|
+
this.media = {
|
|
42
|
+
receiveVideo: opts.media?.receiveVideo ?? false,
|
|
43
|
+
receiveAudio: opts.media?.receiveAudio ?? false,
|
|
44
|
+
addSilentAudio: opts.media?.addSilentAudio ?? false,
|
|
45
|
+
};
|
|
46
|
+
this.pc = new RTCPeerConnection({
|
|
47
|
+
iceServers: opts.iceServers,
|
|
48
|
+
bundlePolicy: 'max-bundle',
|
|
49
|
+
});
|
|
50
|
+
this.wire();
|
|
51
|
+
/**
|
|
52
|
+
* 由浏览器连接所有者创建 DataChannel(webrtc-control.md §2.2)
|
|
53
|
+
*
|
|
54
|
+
* control 可靠有序 双向:JSON-RPC 指令、响应、robot.fault
|
|
55
|
+
* telemetry 不可靠无序 单向:robot.state 状态推送
|
|
56
|
+
* blob 可靠有序 双向:自定义动作上传。旧设备可独立关闭它。
|
|
57
|
+
* tof 不可靠无序 单向:订阅后接收头部 ToF 深度帧。
|
|
58
|
+
*
|
|
59
|
+
* 必须是 offer 侧创建:answer 方无法新增 m=application 行。
|
|
60
|
+
*/
|
|
61
|
+
if (opts.createChannels ?? true) {
|
|
62
|
+
this.control = this.pc.createDataChannel('control', { ordered: true });
|
|
63
|
+
this.bindControl(this.control);
|
|
64
|
+
this.telemetry = this.pc.createDataChannel('telemetry', {
|
|
65
|
+
ordered: false,
|
|
66
|
+
maxRetransmits: 0,
|
|
67
|
+
});
|
|
68
|
+
this.bindTelemetry(this.telemetry);
|
|
69
|
+
this.blob = this.pc.createDataChannel('blob', { ordered: true });
|
|
70
|
+
this.tof = this.pc.createDataChannel('tof', { ordered: false, maxRetransmits: 0 });
|
|
71
|
+
}
|
|
72
|
+
if (this.media.addSilentAudio) {
|
|
73
|
+
this.addSilentAudioTrack();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/* ───────────── 事件订阅 ───────────── */
|
|
77
|
+
/**
|
|
78
|
+
* 订阅协商事件。
|
|
79
|
+
* @param key - 要订阅的事件,见 {@link NegotiatorEvents}。
|
|
80
|
+
* @param handler - 接收该事件载荷的回调。
|
|
81
|
+
* @returns 可重复调用的退订函数。
|
|
82
|
+
* @remarks 同步回调异常会被隔离,异步拒绝由回调自行处理。
|
|
83
|
+
*/
|
|
84
|
+
on(key, handler) {
|
|
85
|
+
let set = this.handlers.get(key);
|
|
86
|
+
if (!set) {
|
|
87
|
+
set = new Set();
|
|
88
|
+
this.handlers.set(key, set);
|
|
89
|
+
}
|
|
90
|
+
const h = handler;
|
|
91
|
+
set.add(h);
|
|
92
|
+
return () => {
|
|
93
|
+
set.delete(h);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
emit(key, payload) {
|
|
97
|
+
const set = this.handlers.get(key);
|
|
98
|
+
if (!set)
|
|
99
|
+
return;
|
|
100
|
+
for (const h of set) {
|
|
101
|
+
try {
|
|
102
|
+
h(payload);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
console.error(`[negotiator] handler for "${key}" threw`, err);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/* ───────────── 装配 ───────────── */
|
|
110
|
+
wire() {
|
|
111
|
+
const pc = this.pc;
|
|
112
|
+
pc.onicecandidate = (e) => {
|
|
113
|
+
if (e.candidate)
|
|
114
|
+
this.emit('iceCandidate', e.candidate.toJSON());
|
|
115
|
+
};
|
|
116
|
+
pc.oniceconnectionstatechange = () => {
|
|
117
|
+
this.emit('iceState', pc.iceConnectionState);
|
|
118
|
+
if (pc.iceConnectionState === 'failed') {
|
|
119
|
+
this.emit('failed', { reason: 'ice_failed' });
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
pc.ontrack = (e) => {
|
|
123
|
+
if (e.streams[0]) {
|
|
124
|
+
this.remoteStream = e.streams[0];
|
|
125
|
+
this.tryEmitEstablished();
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* 对端若也创建了通道,在此接收。
|
|
130
|
+
*
|
|
131
|
+
* 正常情况由浏览器连接所有者创建通道(webrtc-control.md §2.2)。
|
|
132
|
+
* 这里仅作兜底,避免设备端重复建通道时丢失。
|
|
133
|
+
*/
|
|
134
|
+
pc.ondatachannel = (e) => {
|
|
135
|
+
const dc = e.channel;
|
|
136
|
+
if (dc.label === 'control') {
|
|
137
|
+
console.warn('[negotiator] 收到对端创建的 control 通道,覆盖本地实例');
|
|
138
|
+
this.control = dc;
|
|
139
|
+
this.bindControl(dc);
|
|
140
|
+
}
|
|
141
|
+
else if (dc.label === 'telemetry') {
|
|
142
|
+
console.warn('[negotiator] 收到对端创建的 telemetry 通道,覆盖本地实例');
|
|
143
|
+
this.telemetry = dc;
|
|
144
|
+
this.bindTelemetry(dc);
|
|
145
|
+
}
|
|
146
|
+
else if (dc.label === 'tof' && !this.tof && !this.establishedEmitted) {
|
|
147
|
+
this.tof = dc;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
console.debug('[negotiator] 未知数据通道', dc.label);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
pc.onconnectionstatechange = () => {
|
|
154
|
+
if (pc.connectionState === 'connected') {
|
|
155
|
+
this.tryEmitEstablished();
|
|
156
|
+
}
|
|
157
|
+
else if (pc.connectionState === 'failed' && !this.closed) {
|
|
158
|
+
this.emit('failed', { reason: 'connection_failed' });
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
bindControl(dc) {
|
|
163
|
+
dc.onopen = () => {
|
|
164
|
+
console.debug('[negotiator] control 通道 open');
|
|
165
|
+
this.tryEmitEstablished();
|
|
166
|
+
};
|
|
167
|
+
dc.onclose = () => {
|
|
168
|
+
console.warn('[negotiator] control 通道 closed');
|
|
169
|
+
if (!this.closed)
|
|
170
|
+
this.emit('failed', { reason: 'control_closed' });
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
bindTelemetry(dc) {
|
|
174
|
+
dc.onopen = () => {
|
|
175
|
+
console.debug('[negotiator] telemetry 通道 open');
|
|
176
|
+
this.tryEmitEstablished();
|
|
177
|
+
};
|
|
178
|
+
dc.onclose = () => {
|
|
179
|
+
// telemetry 断开不致命:机器人端可能只在有状态变化时才推送
|
|
180
|
+
console.warn('[negotiator] telemetry 通道 closed');
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 添加静音占位音轨,为后续 replaceTrack 预留音频发送器。
|
|
185
|
+
* SDK 接入真实音轨时检查协商方向为 sendonly 或 sendrecv,并核验设备音频输出能力。
|
|
186
|
+
*/
|
|
187
|
+
addSilentAudioTrack() {
|
|
188
|
+
try {
|
|
189
|
+
const ctx = new AudioContext();
|
|
190
|
+
const dst = ctx.createMediaStreamDestination();
|
|
191
|
+
const osc = ctx.createOscillator();
|
|
192
|
+
const gain = ctx.createGain();
|
|
193
|
+
gain.gain.value = 0;
|
|
194
|
+
osc.connect(gain).connect(dst);
|
|
195
|
+
osc.start();
|
|
196
|
+
dst.stream.getAudioTracks().forEach((t) => {
|
|
197
|
+
t.enabled = false;
|
|
198
|
+
this.placeholderTracks.push(t);
|
|
199
|
+
this.pc.addTrack(t, dst.stream);
|
|
200
|
+
});
|
|
201
|
+
this.audioCtx = ctx;
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
// AudioContext 需要用户手势,失败不阻塞协商
|
|
205
|
+
console.warn('[negotiator] 静音音轨添加失败', err);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/* ───────────── 协商 §5.2 ───────────── */
|
|
209
|
+
/**
|
|
210
|
+
* 生成并设置本地 offer。
|
|
211
|
+
*
|
|
212
|
+
* 控制和遥测 DataChannel 共用 m=application;媒体行由音轨和 media 选项决定。
|
|
213
|
+
* 仅在配置音轨或接收选项时请求相应媒体,设备以 answer 确定最终方向。
|
|
214
|
+
* @returns 已设置为本地描述的 offer;此时尚未收到远端 answer。
|
|
215
|
+
* @throws 浏览器生成或应用 SDP 失败时拒绝,不自动重试。
|
|
216
|
+
*/
|
|
217
|
+
async createOffer() {
|
|
218
|
+
const offer = await this.pc.createOffer({
|
|
219
|
+
offerToReceiveVideo: this.media.receiveVideo,
|
|
220
|
+
offerToReceiveAudio: this.media.receiveAudio,
|
|
221
|
+
});
|
|
222
|
+
await this.pc.setLocalDescription(offer);
|
|
223
|
+
return offer;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 应用远端 answer 并尝试加入此前缓冲的 ICE 候选。
|
|
227
|
+
* @param sdp - 远端返回的 SDP answer。
|
|
228
|
+
* @param fromName - 可选设备名,非空时保存在之后交出的连接句柄中。
|
|
229
|
+
* @returns 远端描述应用和候选处理结束后完成,连接建立由 established 事件确认。
|
|
230
|
+
* @throws 浏览器设置远端描述失败时拒绝 Promise;单个 ICE 候选失败仅记录警告。
|
|
231
|
+
*/
|
|
232
|
+
async applyAnswer(sdp, fromName) {
|
|
233
|
+
if (fromName)
|
|
234
|
+
this.deviceName = fromName;
|
|
235
|
+
await this.pc.setRemoteDescription(sdp);
|
|
236
|
+
this.remoteDescSet = true;
|
|
237
|
+
await this.flushPendingCandidates();
|
|
238
|
+
this.tryEmitEstablished();
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* 应用远端 offer,处理缓冲候选并生成本地 answer。
|
|
242
|
+
* @param sdp - 远端发送的 SDP offer。
|
|
243
|
+
* @returns 已设置为本地描述的 answer,需由调用方通过信令发送。
|
|
244
|
+
* @throws 浏览器设置描述或生成 answer 失败时拒绝 Promise;单个 ICE 候选失败仅记录警告。
|
|
245
|
+
*/
|
|
246
|
+
async applyOffer(sdp) {
|
|
247
|
+
await this.pc.setRemoteDescription(sdp);
|
|
248
|
+
this.remoteDescSet = true;
|
|
249
|
+
await this.flushPendingCandidates();
|
|
250
|
+
const answer = await this.pc.createAnswer();
|
|
251
|
+
await this.pc.setLocalDescription(answer);
|
|
252
|
+
return answer;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* 处理远端 ICE 候选。
|
|
256
|
+
* @param candidate - 远端候选;candidate 字段为空时直接忽略。
|
|
257
|
+
* @returns 忽略、缓冲或尝试添加候选后完成。
|
|
258
|
+
* @remarks 远端描述尚未设置时先缓冲。浏览器拒绝候选时仅记录警告,关闭会清空缓冲。
|
|
259
|
+
*/
|
|
260
|
+
async addIceCandidate(candidate) {
|
|
261
|
+
if (!candidate.candidate)
|
|
262
|
+
return;
|
|
263
|
+
if (!this.remoteDescSet) {
|
|
264
|
+
this.pendingCandidates.push(candidate);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
await this.pc.addIceCandidate(candidate);
|
|
269
|
+
}
|
|
270
|
+
catch (err) {
|
|
271
|
+
console.warn('[negotiator] addIceCandidate 失败', err);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async flushPendingCandidates() {
|
|
275
|
+
const queue = this.pendingCandidates;
|
|
276
|
+
this.pendingCandidates = [];
|
|
277
|
+
for (const c of queue) {
|
|
278
|
+
try {
|
|
279
|
+
await this.pc.addIceCandidate(c);
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
console.warn('[negotiator] 缓冲候选应用失败', err);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/* ───────────── 交付 ───────────── */
|
|
287
|
+
/**
|
|
288
|
+
* 就绪判定:PC connected 且 control / telemetry 两条通道都 open
|
|
289
|
+
*
|
|
290
|
+
* 判定为「两条通道都 open」而非「任一通道 open」:
|
|
291
|
+
* control 承载指令,telemetry 承载状态推送。
|
|
292
|
+
* 若 telemetry 未就绪就交出句柄,上层会拿不到机器人状态。
|
|
293
|
+
*/
|
|
294
|
+
tryEmitEstablished() {
|
|
295
|
+
if (this.establishedEmitted)
|
|
296
|
+
return;
|
|
297
|
+
if (this.pc.connectionState !== 'connected')
|
|
298
|
+
return;
|
|
299
|
+
if (this.control?.readyState !== 'open')
|
|
300
|
+
return;
|
|
301
|
+
if (this.telemetry?.readyState !== 'open')
|
|
302
|
+
return;
|
|
303
|
+
this.establishedEmitted = true;
|
|
304
|
+
console.info('[negotiator] 连接就绪', `sn=${this.sn}`, `control=${this.control.readyState}`, `telemetry=${this.telemetry.readyState}`);
|
|
305
|
+
this.emit('established', {
|
|
306
|
+
pc: this.pc,
|
|
307
|
+
control: this.control,
|
|
308
|
+
telemetry: this.telemetry,
|
|
309
|
+
blob: this.blob,
|
|
310
|
+
tof: this.tof,
|
|
311
|
+
remoteStream: this.remoteStream,
|
|
312
|
+
sn: this.sn,
|
|
313
|
+
deviceName: this.deviceName,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* 检查协商器是否曾交出已建立的连接。
|
|
318
|
+
* @returns 已发布过 established 时为 true,调用 close 后保留该标记。
|
|
319
|
+
* @remarks 当前连通性可通过 {@link PeerNegotiator.getConnectionState} 查询。
|
|
320
|
+
*/
|
|
321
|
+
isReady() {
|
|
322
|
+
return this.establishedEmitted;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* 读取当前 WebRTC 连接状态。
|
|
326
|
+
* @returns 浏览器的 RTCPeerConnection.connectionState,执行器状态需另行查询。
|
|
327
|
+
*/
|
|
328
|
+
getConnectionState() {
|
|
329
|
+
return this.pc.connectionState;
|
|
330
|
+
}
|
|
331
|
+
/* ───────────── 关闭 ───────────── */
|
|
332
|
+
/**
|
|
333
|
+
* 幂等关闭数据通道和 WebRTC,移除发送端、停止本类占位音轨、清空候选及事件订阅。
|
|
334
|
+
* @remarks 不发送停车请求,不停止调用方替换进来的音轨;AudioContext 关闭在后台尝试,
|
|
335
|
+
* 同步返回不表示媒体资源已完成释放或远端已处理断线。
|
|
336
|
+
*/
|
|
337
|
+
close() {
|
|
338
|
+
if (this.closed)
|
|
339
|
+
return;
|
|
340
|
+
this.closed = true;
|
|
341
|
+
this.control?.close();
|
|
342
|
+
this.telemetry?.close();
|
|
343
|
+
this.blob?.close();
|
|
344
|
+
this.tof?.close();
|
|
345
|
+
this.blob = null;
|
|
346
|
+
this.tof = null;
|
|
347
|
+
this.control = null;
|
|
348
|
+
this.telemetry = null;
|
|
349
|
+
this.pc.getSenders().forEach((s) => {
|
|
350
|
+
try {
|
|
351
|
+
this.pc.removeTrack(s);
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
/* ignore */
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
this.pc.close();
|
|
358
|
+
for (const track of this.placeholderTracks)
|
|
359
|
+
track.stop();
|
|
360
|
+
this.placeholderTracks = [];
|
|
361
|
+
this.pendingCandidates = [];
|
|
362
|
+
this.handlers.clear();
|
|
363
|
+
void this.audioCtx?.close().catch(() => { });
|
|
364
|
+
this.audioCtx = null;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 信令客户端,与平台信令服务交换发现、SDP 和 ICE 消息。
|
|
3
|
+
*
|
|
4
|
+
* 边界:
|
|
5
|
+
* - 只管 WSS 收发、requestId 配对、心跳、重连
|
|
6
|
+
* - 不碰 WebRTC,不碰 DOM
|
|
7
|
+
*/
|
|
8
|
+
import { type AnyFrame, type AuthOkPayload, type DeviceListPayload, type DeviceStatusPayload, type DownFrame, type ErrorPayload, type IcePayload, type SdpPayload, type SendFrameParams } from './signaling-contract.ts';
|
|
9
|
+
/** 信令客户端事件;数据不回放,事件内容由服务端帧提供。 */
|
|
10
|
+
export interface SignalingEvents {
|
|
11
|
+
/** 鉴权回执,收到后可开始发消息 */
|
|
12
|
+
authOk: AuthOkPayload;
|
|
13
|
+
/** 设备上下线广播 */
|
|
14
|
+
deviceStatus: DeviceStatusPayload;
|
|
15
|
+
/** 对端转发的 SDP offer(用户侧一般不收,设备侧收) */
|
|
16
|
+
offer: {
|
|
17
|
+
/** SDP 提议。 */
|
|
18
|
+
payload: SdpPayload;
|
|
19
|
+
/** 服务端注入的发送方身份,可能缺省。 */
|
|
20
|
+
from: AnyFrame['from'];
|
|
21
|
+
};
|
|
22
|
+
/** 对端转发的 SDP answer */
|
|
23
|
+
answer: {
|
|
24
|
+
/** SDP 应答。 */
|
|
25
|
+
payload: SdpPayload;
|
|
26
|
+
/** 服务端注入的发送方身份,可能缺省。 */
|
|
27
|
+
from: AnyFrame['from'];
|
|
28
|
+
};
|
|
29
|
+
/** 对端转发的 ICE 候选 */
|
|
30
|
+
ice: {
|
|
31
|
+
/** 远端 ICE 候选。 */
|
|
32
|
+
payload: IcePayload;
|
|
33
|
+
/** 服务端注入的发送方身份,可能缺省。 */
|
|
34
|
+
from: AnyFrame['from'];
|
|
35
|
+
};
|
|
36
|
+
/** 服务端 command 帧的原样通知;直连 SDK 不把它作为业务命令回退通道。 */
|
|
37
|
+
command: {
|
|
38
|
+
/** 未解释的服务端载荷。 */
|
|
39
|
+
payload: unknown;
|
|
40
|
+
/** 服务端注入的发送方身份,可能缺省。 */
|
|
41
|
+
from: AnyFrame['from'];
|
|
42
|
+
};
|
|
43
|
+
/** 服务端错误 */
|
|
44
|
+
error: ErrorPayload;
|
|
45
|
+
/** WebSocket 已打开,不等同于收到 authOk。 */
|
|
46
|
+
open: void;
|
|
47
|
+
/** 非主动关闭路径的 WebSocket 关闭事件。 */
|
|
48
|
+
close: {
|
|
49
|
+
/** 浏览器 WebSocket 关闭码。 */
|
|
50
|
+
code: number;
|
|
51
|
+
/** 浏览器或服务端提供的关闭说明。 */
|
|
52
|
+
reason: string;
|
|
53
|
+
/** 客户端是否将安排信令重连,不表示机器人会自动重新协商。 */
|
|
54
|
+
willRetry: boolean;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
type EventKey = keyof SignalingEvents;
|
|
58
|
+
/**
|
|
59
|
+
* 接收指定信令事件。
|
|
60
|
+
* @param payload - 与事件类型对应的 {@link SignalingEvents} 载荷。
|
|
61
|
+
*/
|
|
62
|
+
type Handler<K extends EventKey> = (payload: SignalingEvents[K]) => void;
|
|
63
|
+
/** 底层 WebSocket 信令客户端配置。 */
|
|
64
|
+
export interface SignalingOptions {
|
|
65
|
+
/**
|
|
66
|
+
* 信令地址。
|
|
67
|
+
*
|
|
68
|
+
* 走网关是 ws(s)://<网关>/campus-websocket/ws
|
|
69
|
+
* 直连内网是 ws://<IP>:8082/ws(服务端会剥离网关前缀)
|
|
70
|
+
*/
|
|
71
|
+
url: string;
|
|
72
|
+
/**
|
|
73
|
+
* 平台提供的信令登录凭据,作为 WebSocket query 的 jti 参数发送。
|
|
74
|
+
* 本客户端不解析内容;不得把它写入日志或公开业务状态。
|
|
75
|
+
*/
|
|
76
|
+
jti: string;
|
|
77
|
+
/** 设备侧才需要;用户侧不传 */
|
|
78
|
+
sn?: string;
|
|
79
|
+
/** 是否在非主动、非鉴权失败关闭后重连信令,默认 true;不会重放业务请求或重建 WebRTC。 */
|
|
80
|
+
autoReconnect?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/** WebSocket 生命周期状态;open 只表示连接打开,不保证鉴权已完成。 */
|
|
83
|
+
export type ConnectionStatus = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed';
|
|
84
|
+
/**
|
|
85
|
+
* 管理一个 WebSocket 的信令收发、请求关联、30 秒心跳及可选重连。
|
|
86
|
+
* @remarks 自动重连按 1/2/4/8/16/30 秒基准延迟并加入 ±25% 抖动;不负责机器人业务命令。
|
|
87
|
+
*/
|
|
88
|
+
export declare class SignalingClient {
|
|
89
|
+
private ws;
|
|
90
|
+
private readonly opts;
|
|
91
|
+
private handlers;
|
|
92
|
+
private readonly frameHandlers;
|
|
93
|
+
private status;
|
|
94
|
+
private attempt;
|
|
95
|
+
private heartbeatTimer;
|
|
96
|
+
private reconnectTimer;
|
|
97
|
+
private manualClose;
|
|
98
|
+
private closing;
|
|
99
|
+
private cancelConnect;
|
|
100
|
+
/** requestId → 等待回执 */
|
|
101
|
+
private pending;
|
|
102
|
+
/** requestId → 期望的回执 type,用于区分同名回执 */
|
|
103
|
+
private expectType;
|
|
104
|
+
/**
|
|
105
|
+
* 保存信令连接配置。
|
|
106
|
+
* @param options - 地址、认证信息及自动重连选项,见 {@link SignalingOptions}。
|
|
107
|
+
* @remarks 默认启用自动重连。调用 connectSignaling 时才会创建 WebSocket。
|
|
108
|
+
*/
|
|
109
|
+
constructor(options: SignalingOptions);
|
|
110
|
+
/**
|
|
111
|
+
* 订阅信令客户端事件。
|
|
112
|
+
* @param key - 要订阅的事件,见 {@link SignalingEvents}。
|
|
113
|
+
* @param handler - 接收该事件载荷的回调。
|
|
114
|
+
* @returns 可重复调用的退订函数。
|
|
115
|
+
* @remarks 同步回调异常会被隔离,异步拒绝由回调自行处理。
|
|
116
|
+
*/
|
|
117
|
+
on<K extends EventKey>(key: K, handler: Handler<K>): () => void;
|
|
118
|
+
/**
|
|
119
|
+
* 按消息类型订阅完整下行帧。
|
|
120
|
+
* @param type - 要匹配的下行帧类型。
|
|
121
|
+
* @param handler - 同步接收 {@link DownFrame} 的回调。
|
|
122
|
+
* @returns 可重复调用的退订函数。
|
|
123
|
+
* @remarks 历史帧不回放,单个回调的同步异常会被隔离。
|
|
124
|
+
*/
|
|
125
|
+
onFrame(type: string, handler: (frame: DownFrame) => void): () => void;
|
|
126
|
+
private emit;
|
|
127
|
+
/**
|
|
128
|
+
* 读取当前 WebSocket 状态。
|
|
129
|
+
* @returns 当前状态,见 {@link ConnectionStatus};open 只表示 socket 已打开,鉴权状态需另行确认。
|
|
130
|
+
*/
|
|
131
|
+
getStatus(): ConnectionStatus;
|
|
132
|
+
/**
|
|
133
|
+
* 建立并认证信令连接。
|
|
134
|
+
* @returns 收到 auth.ok 后解析为 {@link AuthOkPayload}。
|
|
135
|
+
* @throws URL 构造失败时同步抛出;已有连接、正在关闭、浏览器无法创建 WebSocket 或握手未完成便关闭时拒绝 Promise。
|
|
136
|
+
* @remarks 本方法没有鉴权等待超时或 AbortSignal。可调用 {@link SignalingClient.close} 结束等待。
|
|
137
|
+
* 关闭码 1008、4001、4003 被视为鉴权失败且不自动重连,其他非主动关闭按配置处理。
|
|
138
|
+
*/
|
|
139
|
+
connectSignaling(): Promise<AuthOkPayload>;
|
|
140
|
+
/**
|
|
141
|
+
* 停止重连、心跳与在途请求,并等待当前 WebSocket 的 close 事件。
|
|
142
|
+
* @param timeoutMs - 等待浏览器连接关闭的毫秒上限,默认 3000,范围为 1~30000 的整数。
|
|
143
|
+
* @returns 没有活动 socket 或浏览器已确认关闭时完成。
|
|
144
|
+
* @throws 超时、超时参数非法或浏览器关闭失败时拒绝 Promise。
|
|
145
|
+
* @remarks 正在关闭时复用同一次等待。超时后保留未关闭的 socket,旧连接关闭前不能重新 connectSignaling。
|
|
146
|
+
*/
|
|
147
|
+
close(timeoutMs?: number): Promise<void>;
|
|
148
|
+
private scheduleReconnect;
|
|
149
|
+
private clearReconnectTimer;
|
|
150
|
+
private startHeartbeat;
|
|
151
|
+
private stopHeartbeat;
|
|
152
|
+
private send;
|
|
153
|
+
/**
|
|
154
|
+
* 单向发送自定义信令帧。
|
|
155
|
+
* @param type - 上行帧类型。
|
|
156
|
+
* @param frame - 关联标识、目标序列号及载荷,见 {@link SendFrameParams}。
|
|
157
|
+
* @returns 提交给 WebSocket 时返回 true,WebSocket 未打开时返回 false。
|
|
158
|
+
* @throws 序列化失败、整帧超过 65536 个 UTF-8 字节或浏览器发送失败时同步抛出。
|
|
159
|
+
* @remarks 不登记请求或排队,重连后不重发。
|
|
160
|
+
*/
|
|
161
|
+
sendFrame(type: string, { requestId, targetSN, payload }: SendFrameParams): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* 发送一条信令请求并等待对应类型的关联回执。
|
|
164
|
+
* @param type - 上行信令消息类型。
|
|
165
|
+
* @param payload - 消息载荷,undefined 时序列化会省略该字段。
|
|
166
|
+
* @param expectType - 期望的回执类型,例如 device.list.result。
|
|
167
|
+
* @param timeoutMs - 本地等待上限,单位 ms,默认 10000;调用方应提供正的有限值,本层未额外校验范围。
|
|
168
|
+
* @returns 关联回执的 payload;T 由调用方指定,返回值不包含信令信封。
|
|
169
|
+
* @throws 未连接、服务端错误或等待超时会拒绝;序列化和浏览器发送异常也可能导致拒绝。
|
|
170
|
+
* @remarks 不提供独立取消信号,不自动重发;close 或连接关闭会拒绝在途请求。
|
|
171
|
+
*/
|
|
172
|
+
request<T = unknown>(type: string, payload: unknown, expectType: string, timeoutMs?: number): Promise<T>;
|
|
173
|
+
/**
|
|
174
|
+
* 查询用户侧设备列表。
|
|
175
|
+
* @param timeoutMs - 本地等待上限,单位 ms,默认 10000;调用方应提供正的有限值。
|
|
176
|
+
* @returns 对应 device.list.result 的 {@link DeviceListPayload}。
|
|
177
|
+
* @throws 未连接、服务端错误、关闭连接或等待超时时拒绝 Promise,见 {@link SignalingClient.request}。
|
|
178
|
+
*/
|
|
179
|
+
listDevices(timeoutMs?: number): Promise<DeviceListPayload>;
|
|
180
|
+
/**
|
|
181
|
+
* 向目标设备发送 SDP offer。
|
|
182
|
+
* @param targetSN - 目标设备序列号。
|
|
183
|
+
* @param sdp - 本地 offer 的 SDP 文本。
|
|
184
|
+
* @param sdpType - 固定为 offer,省略时补 offer。
|
|
185
|
+
* @returns 已提交给浏览器 WebSocket 时返回 true,连接未打开时返回 false。
|
|
186
|
+
* @throws 序列化或 WebSocket 发送失败时同步抛出。
|
|
187
|
+
* @remarks 不等待服务端或设备确认。
|
|
188
|
+
*/
|
|
189
|
+
sendOffer(targetSN: string, sdp: string, sdpType?: 'offer'): boolean;
|
|
190
|
+
/**
|
|
191
|
+
* 向目标设备发送 ICE 候选。
|
|
192
|
+
* @param targetSN - 目标设备序列号。
|
|
193
|
+
* @param ice - 待发送的候选,candidate 缺省时补空字符串,媒体索引和标识缺省时补 null。
|
|
194
|
+
* @returns 已提交给浏览器 WebSocket 时返回 true,连接未打开时返回 false。
|
|
195
|
+
* @throws 序列化或 WebSocket 发送失败时同步抛出。
|
|
196
|
+
* @remarks 不等待设备收到候选的确认。
|
|
197
|
+
*/
|
|
198
|
+
sendIce(targetSN: string, ice: RTCIceCandidateInit): boolean;
|
|
199
|
+
private handleMessage;
|
|
200
|
+
private dispatchMessage;
|
|
201
|
+
private dispatch;
|
|
202
|
+
private rejectAllPending;
|
|
203
|
+
}
|
|
204
|
+
export {};
|