@capyclaw/rsp-suite 0.0.0-stage → 0.1.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/README.md +84 -2
- package/cordis.patch.yml +8 -0
- package/dsh-suite.json +81 -0
- package/package.json +39 -3
- package/skill/README.md +7 -0
- package/skill/contract/rsp-contract.json +389 -0
- package/skill/cordis.patch.yml +4 -0
- package/skill/package.json +33 -0
- package/skill/plugin.mjs +15 -0
- package/skill/skills/remote-screen/SKILL.md +120 -0
- package/workbench/contract/capabilities.json +171 -0
- package/workbench/lib/client.mjs +454 -0
- package/workbench/lib/descriptors.mjs +28 -0
- package/workbench/lib/host.mjs +111 -0
- package/workbench/lib/manager.mjs +333 -0
- package/workbench/lib/session-mode.mjs +68 -0
- package/workbench/lib/tools.mjs +385 -0
- package/workbench/lib/workbench.mjs +165 -0
- package/workbench/package.json +47 -0
- package/workbench/plugin.mjs +2 -0
- package/workbench/scripts/build-contract.mjs +41 -0
- package/workbench/scripts/drive.mjs +66 -0
- package/workbench/test/client.test.mjs +218 -0
- package/workbench/test/frames-prune.test.mjs +49 -0
- package/workbench/test/h264.test.mjs +101 -0
- package/workbench/test/live-persistent-smoke.mjs +29 -0
- package/workbench/test/live-smoke.mjs +109 -0
- package/workbench/test/mock-server.mjs +194 -0
- package/workbench/test/persistence.test.mjs +177 -0
- package/workbench/test/persistent.test.mjs +160 -0
- package/workbench/test/session-mode.test.mjs +118 -0
- package/workbench/test/workbench.test.mjs +385 -0
- package/workbench/ui/index.html +475 -0
- package/workbench/verify-e2e.mjs +44 -0
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>RSP 远程屏幕工作台</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root { color-scheme: dark; --bg:#0b1018; --panel:#141c2b; --line:#232f45; --ink:#e6ebf5; --dim:#8b9ab8; --accent:#4c7dff; }
|
|
9
|
+
* { box-sizing: border-box; }
|
|
10
|
+
html, body { height: 100%; }
|
|
11
|
+
body { margin: 0; font: 13px/1.5 system-ui, -apple-system, "Segoe UI", "PingFang SC", sans-serif; background: var(--bg); color: var(--ink); display: flex; flex-direction: column; overflow: hidden; }
|
|
12
|
+
header { flex: none; display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); background: #0e1521; }
|
|
13
|
+
header h1 { font-size: 14px; margin: 0; font-weight: 600; white-space: nowrap; }
|
|
14
|
+
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12px; border: 1px solid transparent; white-space: nowrap; }
|
|
15
|
+
.pill i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; display: inline-block; }
|
|
16
|
+
.on { background: #10331f; color: #57d98a; border-color: #1d4d2f; }
|
|
17
|
+
.off { background: #33161d; color: #ff8a9b; border-color: #4d1f29; }
|
|
18
|
+
.warn { background: #33280f; color: #ffc46b; border-color: #4d3a14; }
|
|
19
|
+
.spacer { flex: 1; }
|
|
20
|
+
.meta { color: var(--dim); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
21
|
+
button { font: inherit; border-radius: 7px; border: 1px solid var(--line); background: #1b2436; color: #cdd7ee; padding: 5px 10px; cursor: pointer; white-space: nowrap; }
|
|
22
|
+
button:hover { border-color: #33425f; background: #1f2a3f; }
|
|
23
|
+
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
|
|
24
|
+
main { flex: 1; min-height: 0; display: flex; }
|
|
25
|
+
.stage { position: relative; flex: 1; min-width: 0; background: #05080d; overflow: hidden; }
|
|
26
|
+
.stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
|
|
27
|
+
#screen { z-index: 2; }
|
|
28
|
+
#snapshot { z-index: 1; }
|
|
29
|
+
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; z-index: 3; background: #05080d; }
|
|
30
|
+
.placeholder { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #5b6884; text-align: center; padding: 30px; font-size: 13px; line-height: 1.8; }
|
|
31
|
+
.overlay { position: absolute; left: 10px; bottom: 10px; z-index: 3; display: flex; gap: 8px; align-items: center; padding: 4px 10px; border-radius: 8px; background: rgba(8,12,20,.72); border: 1px solid #1e2940; font-size: 12px; color: #9fb0d0; backdrop-filter: blur(6px); }
|
|
32
|
+
.overlay b { color: #e6ebf5; font-weight: 600; }
|
|
33
|
+
aside { flex: none; width: 300px; border-left: 1px solid var(--line); background: #0e1521; overflow: auto; padding: 12px; }
|
|
34
|
+
aside section { margin-bottom: 14px; }
|
|
35
|
+
aside h2 { font-size: 11px; margin: 0 0 8px; color: var(--dim); text-transform: uppercase; letter-spacing: .06em; }
|
|
36
|
+
.kv { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; border-bottom: 1px dashed #1d2740; font-size: 12px; }
|
|
37
|
+
.kv:last-child { border-bottom: none; }
|
|
38
|
+
.kv .k { color: var(--dim); flex: none; }
|
|
39
|
+
.kv .v { text-align: right; word-break: break-all; }
|
|
40
|
+
label { display: block; font-size: 11px; color: var(--dim); margin: 8px 0 3px; }
|
|
41
|
+
input, select { width: 100%; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line); background: #0b1018; color: var(--ink); font: inherit; }
|
|
42
|
+
input:focus, select:focus { outline: none; border-color: var(--accent); }
|
|
43
|
+
.row { display: flex; gap: 6px; }
|
|
44
|
+
.row > * { flex: 1; min-width: 0; }
|
|
45
|
+
.btns { display: flex; gap: 6px; margin-top: 10px; }
|
|
46
|
+
.btns button { flex: 1; }
|
|
47
|
+
.msg { margin-top: 8px; font-size: 12px; min-height: 16px; color: var(--dim); }
|
|
48
|
+
.msg.err { color: #ff8a9b; }
|
|
49
|
+
.msg.ok { color: #57d98a; }
|
|
50
|
+
details > summary { cursor: pointer; color: var(--dim); font-size: 12px; }
|
|
51
|
+
@media (max-width: 900px) { aside { display: none; } }
|
|
52
|
+
</style>
|
|
53
|
+
</head>
|
|
54
|
+
<body>
|
|
55
|
+
<header>
|
|
56
|
+
<h1>RSP 远程屏幕工作台</h1>
|
|
57
|
+
<span id="pill" class="pill off"><i></i><span id="pillText">未连接</span></span>
|
|
58
|
+
<span id="target" class="meta"></span>
|
|
59
|
+
<span class="spacer"></span>
|
|
60
|
+
<button id="fullscreen" title="全屏查看">全屏</button>
|
|
61
|
+
<button id="resync" title="重新拉取画面流">重连画面</button>
|
|
62
|
+
</header>
|
|
63
|
+
<main>
|
|
64
|
+
<div class="stage" id="stage">
|
|
65
|
+
<div class="placeholder" id="placeholder">未连接被控端。<br/>连接后这里显示实时画面;画面只读,操控由 AI 经 rsp_* 工具完成。</div>
|
|
66
|
+
<img id="snapshot" alt="" style="display:none" />
|
|
67
|
+
<img id="screen" alt="remote screen" style="display:none" />
|
|
68
|
+
<canvas id="video" style="display:none"></canvas>
|
|
69
|
+
<div class="overlay" id="overlay" style="display:none">
|
|
70
|
+
<span>更新于 <b id="age">—</b></span>
|
|
71
|
+
<span id="dims">—</span>
|
|
72
|
+
<span id="liveTag" class="pill on" style="padding:0 6px"><i></i><span id="liveText">实时</span></span>
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
<aside>
|
|
76
|
+
<section>
|
|
77
|
+
<h2>状态</h2>
|
|
78
|
+
<div id="status"><div class="kv"><span class="k">尚未连接</span></div></div>
|
|
79
|
+
</section>
|
|
80
|
+
<section>
|
|
81
|
+
<h2>记住的目标(跨会话共享)</h2>
|
|
82
|
+
<select id="remembered"><option value="">(暂无记住的目标)</option></select>
|
|
83
|
+
<div class="btns"><button id="connectRemembered">连接选中目标</button></div>
|
|
84
|
+
<div class="msg">令牌掩码显示;连接信息持久化在本机,会话下次动作自动重连。</div>
|
|
85
|
+
</section>
|
|
86
|
+
<section>
|
|
87
|
+
<h2>观看会话</h2>
|
|
88
|
+
<select id="watchSession"><option value="">默认(最近活动的会话)</option></select>
|
|
89
|
+
<div class="msg">每个会话独立连接;嵌入主区时自动跟随当前会话,也可手动切换观看别的会话。</div>
|
|
90
|
+
</section>
|
|
91
|
+
<section>
|
|
92
|
+
<details id="connDetails">
|
|
93
|
+
<summary>连接 / 切换被控端</summary>
|
|
94
|
+
<label>基址 URL</label>
|
|
95
|
+
<input id="url" placeholder="http://192.168.1.5:8765" />
|
|
96
|
+
<div class="row">
|
|
97
|
+
<div><label>Host</label><input id="host" placeholder="192.168.1.5" /></div>
|
|
98
|
+
<div><label>Port</label><input id="port" placeholder="8765" /></div>
|
|
99
|
+
</div>
|
|
100
|
+
<label>访问令牌 token</label>
|
|
101
|
+
<input id="token" type="password" placeholder="bearer 令牌" />
|
|
102
|
+
<label>别名(可选)</label>
|
|
103
|
+
<input id="name" placeholder="我的电脑" />
|
|
104
|
+
<div class="btns">
|
|
105
|
+
<button id="connect" class="primary">连接</button>
|
|
106
|
+
<button id="disconnect">断开</button>
|
|
107
|
+
</div>
|
|
108
|
+
<div id="msg" class="msg"></div>
|
|
109
|
+
</details>
|
|
110
|
+
</section>
|
|
111
|
+
<section>
|
|
112
|
+
<h2>说明</h2>
|
|
113
|
+
<div class="msg">画面经服务端代理,令牌不下发到浏览器。被控端在画面无变化时不上报新帧:静止画面下显示的是最后一帧,左上角“更新于 N 秒前”即静止时长。</div>
|
|
114
|
+
</section>
|
|
115
|
+
</aside>
|
|
116
|
+
</main>
|
|
117
|
+
<script>
|
|
118
|
+
const $ = (id) => document.getElementById(id)
|
|
119
|
+
const msg = (t, kind) => { const m = $('msg'); if (!m) return; m.textContent = t || ''; m.className = 'msg ' + (kind || '') }
|
|
120
|
+
let streaming = false
|
|
121
|
+
let streamToken = 0
|
|
122
|
+
let lastFrameAt = 0
|
|
123
|
+
let lastFrameCount = -1
|
|
124
|
+
let retryTimer = null
|
|
125
|
+
let snapshotUrl = null
|
|
126
|
+
let h264Live = false // true once the H.264 canvas has painted a real frame
|
|
127
|
+
let isConnected = false
|
|
128
|
+
/* 与 service-host 的会话段规则一致:/s/<sid>(sid 字母数字开头,可含 . _ ~ -)。
|
|
129
|
+
GUI 主区给每个会话嵌的地址都带这一段——不认领它,所有会话的查看器就都看同一个桶。
|
|
130
|
+
注意:SESSION_RE 必须先于 watchSid 初始化声明,否则 sidFromUrl() 立即执行时 const 还在 TDZ。 */
|
|
131
|
+
const SESSION_RE = /\/s\/([A-Za-z0-9][A-Za-z0-9._~-]{0,127})(?=\/|$)/
|
|
132
|
+
function sidFromUrl() {
|
|
133
|
+
const m = SESSION_RE.exec(location.pathname)
|
|
134
|
+
if (m) return m[1]
|
|
135
|
+
try { return new URLSearchParams(location.search).get('session') || null } catch { return null }
|
|
136
|
+
}
|
|
137
|
+
let watchSid = sidFromUrl() // 观看/操作的会话桶;嵌入时从自身 URL 的 /s/<sid>/ 段认领,null=服务端默认
|
|
138
|
+
const sessQ = () => watchSid ? '&session=' + encodeURIComponent(watchSid) : ''
|
|
139
|
+
const statusQ = () => watchSid ? '?session=' + encodeURIComponent(watchSid) : ''
|
|
140
|
+
|
|
141
|
+
/* portal-hud 换会话时不重载 iframe,改发 postMessage 就地换桶;回 ack 免它 600ms 后兜底硬重载。 */
|
|
142
|
+
window.addEventListener('message', (ev) => {
|
|
143
|
+
const d = ev.data
|
|
144
|
+
if (!d || d.type !== 'capyclaw:workbench:session') return
|
|
145
|
+
if (ev.origin !== location.origin) return
|
|
146
|
+
try { ev.source && ev.source.postMessage({ type: 'capyclaw:workbench:session-ack', sid: d.sid }, ev.origin) } catch {}
|
|
147
|
+
const sid = typeof d.sid === 'string' && d.sid ? d.sid : null
|
|
148
|
+
if (sid === watchSid) return
|
|
149
|
+
watchSid = sid
|
|
150
|
+
resetStage()
|
|
151
|
+
refresh()
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
/* Single source of truth for which stage layer is visible. A layer is only made
|
|
155
|
+
visible when it actually holds content, so transitions never flash blank. */
|
|
156
|
+
function showLayer(name) {
|
|
157
|
+
for (const id of ['video', 'screen', 'snapshot']) $(id).style.display = (id === name) ? 'block' : 'none'
|
|
158
|
+
$('placeholder').style.display = (name === 'placeholder') ? 'flex' : 'none'
|
|
159
|
+
$('overlay').style.display = (name === 'placeholder') ? 'none' : 'flex'
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function jfetch(route, opts) {
|
|
163
|
+
const res = await fetch(route, opts)
|
|
164
|
+
let data = null
|
|
165
|
+
try { data = await res.json() } catch {}
|
|
166
|
+
if (!res.ok) throw new Error((data && (data.error || data.code)) || ('HTTP ' + res.status))
|
|
167
|
+
return data
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function setPill(kind, text) { $('pill').className = 'pill ' + kind; $('pillText').textContent = text }
|
|
171
|
+
|
|
172
|
+
/* 静态快照层:保证任何时刻都有图可看(含静止画面/刚打开时)。 */
|
|
173
|
+
async function refreshSnapshot() {
|
|
174
|
+
try {
|
|
175
|
+
const r = await fetch('api/frame?t=' + Date.now() + sessQ())
|
|
176
|
+
if (!r.ok) return
|
|
177
|
+
const blob = await r.blob()
|
|
178
|
+
const img = $('snapshot')
|
|
179
|
+
const old = snapshotUrl
|
|
180
|
+
snapshotUrl = URL.createObjectURL(blob)
|
|
181
|
+
img.onload = () => {
|
|
182
|
+
if (old) URL.revokeObjectURL(old)
|
|
183
|
+
// only reveal the static layer when no live layer is on top
|
|
184
|
+
if (!h264Live && !streaming) showLayer('snapshot')
|
|
185
|
+
}
|
|
186
|
+
img.src = snapshotUrl
|
|
187
|
+
} catch {}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/* 实时流层:叠加在快照之上;有变化时由被控端推帧覆盖。 */
|
|
191
|
+
function startStream() {
|
|
192
|
+
if (streaming) return
|
|
193
|
+
streaming = true
|
|
194
|
+
const my = ++streamToken
|
|
195
|
+
const img = $('screen')
|
|
196
|
+
img.onload = () => { if (my === streamToken && !h264Live) showLayer('screen') }
|
|
197
|
+
img.onerror = () => {
|
|
198
|
+
if (my !== streamToken) return
|
|
199
|
+
img.onerror = null
|
|
200
|
+
if (!streaming) return
|
|
201
|
+
clearTimeout(retryTimer)
|
|
202
|
+
retryTimer = setTimeout(() => { if (streaming && my === streamToken) { streaming = false; startStream() } }, 1500)
|
|
203
|
+
}
|
|
204
|
+
img.src = 'api/screen.mjpg?t=' + Date.now() + '-' + my + sessQ()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function stopStream() {
|
|
208
|
+
streaming = false
|
|
209
|
+
streamToken++
|
|
210
|
+
clearTimeout(retryTimer)
|
|
211
|
+
const img = $('screen')
|
|
212
|
+
img.onerror = null
|
|
213
|
+
img.onload = null
|
|
214
|
+
img.removeAttribute('src') // abort the MJPEG connection; canvas/snapshot hold the picture
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* ---- 实时 H.264 层:fetch chunked + WebCodecs 硬解 + canvas rAF(ToDesk 级低延迟) ---- */
|
|
218
|
+
let h264 = null // {abort, decoder, ...}
|
|
219
|
+
let h264Fps = 0
|
|
220
|
+
function stopH264() {
|
|
221
|
+
if (!h264) return
|
|
222
|
+
try { h264.abort.abort() } catch {}
|
|
223
|
+
try { h264.decoder.close() } catch {}
|
|
224
|
+
h264 = null
|
|
225
|
+
h264Live = false
|
|
226
|
+
// keep the last decoded frame on the canvas as a freeze-frame; bring a live
|
|
227
|
+
// fallback up underneath and let it hide the canvas once it has content.
|
|
228
|
+
if (isConnected) { startStream(); refreshSnapshot() }
|
|
229
|
+
}
|
|
230
|
+
function nalBody(nal) { // 去掉 Annex-B 起始码
|
|
231
|
+
if (nal[0] === 0 && nal[1] === 0 && nal[2] === 0 && nal[3] === 1) return nal.subarray(4)
|
|
232
|
+
if (nal[0] === 0 && nal[1] === 0 && nal[2] === 1) return nal.subarray(3)
|
|
233
|
+
return nal
|
|
234
|
+
}
|
|
235
|
+
function nalType(nal) { return nalBody(nal)[0] & 0x1f }
|
|
236
|
+
function buildAvcC(sps, pps) {
|
|
237
|
+
const s = nalBody(sps), p = nalBody(pps)
|
|
238
|
+
const len = (n) => [(n >> 8) & 255, n & 255]
|
|
239
|
+
return new Uint8Array([1, s[1], s[2], s[3], 0xff, 0xe1, ...len(s.length), ...s, 1, ...len(p.length), ...p])
|
|
240
|
+
}
|
|
241
|
+
const hex2 = (n) => n.toString(16).padStart(2, '0')
|
|
242
|
+
async function startH264() {
|
|
243
|
+
if (h264) return
|
|
244
|
+
if (!('VideoDecoder' in window)) return false // 无 WebCodecs → 回退 JPEG
|
|
245
|
+
const abort = new AbortController()
|
|
246
|
+
const canvas = $('video')
|
|
247
|
+
const ctx = canvas.getContext('2d')
|
|
248
|
+
let pending = null, raf = 0, sps = null, pps = null, configured = false, ts = 0, decoded = 0, fpsT = performance.now()
|
|
249
|
+
const decoder = new VideoDecoder({
|
|
250
|
+
output: (frame) => {
|
|
251
|
+
if (canvas.width !== frame.displayWidth || canvas.height !== frame.displayHeight) {
|
|
252
|
+
canvas.width = frame.displayWidth; canvas.height = frame.displayHeight
|
|
253
|
+
}
|
|
254
|
+
if (pending) pending.close()
|
|
255
|
+
pending = frame
|
|
256
|
+
if (!raf) raf = requestAnimationFrame(() => { raf = 0; if (pending) { ctx.drawImage(pending, 0, 0); pending.close(); pending = null; if (!h264Live) { h264Live = true; showLayer('video') } } })
|
|
257
|
+
decoded++
|
|
258
|
+
const now = performance.now()
|
|
259
|
+
if (now - fpsT >= 1000) { h264Fps = Math.round(decoded * 1000 / (now - fpsT)); decoded = 0; fpsT = now }
|
|
260
|
+
lastFrameAt = Date.now()
|
|
261
|
+
$('dims').textContent = canvas.width + '×' + canvas.height
|
|
262
|
+
},
|
|
263
|
+
error: () => { stopH264() },
|
|
264
|
+
})
|
|
265
|
+
h264 = { abort, decoder }
|
|
266
|
+
try {
|
|
267
|
+
const res = await fetch('api/screen.h264?t=' + Date.now() + sessQ(), { signal: abort.signal })
|
|
268
|
+
if (!res.ok || !res.body) { stopH264(); return false }
|
|
269
|
+
const reader = res.body.getReader()
|
|
270
|
+
let buf = new Uint8Array(0)
|
|
271
|
+
const concat = (a, b) => { const m = new Uint8Array(a.length + b.length); m.set(a); m.set(b, a.length); return m }
|
|
272
|
+
const push = (nal) => {
|
|
273
|
+
const t = nalType(nal)
|
|
274
|
+
if (t === 7) { sps = nal; return }
|
|
275
|
+
if (t === 8) { pps = nal; return }
|
|
276
|
+
if (t !== 1 && t !== 5) return
|
|
277
|
+
if (!configured) {
|
|
278
|
+
if (!sps || !pps) return
|
|
279
|
+
const s = nalBody(sps)
|
|
280
|
+
decoder.configure({ codec: 'avc1.' + hex2(s[1]) + hex2(s[2]) + hex2(s[3]), description: buildAvcC(sps, pps), optimizeForLatency: true })
|
|
281
|
+
configured = true
|
|
282
|
+
}
|
|
283
|
+
const body = nalBody(nal)
|
|
284
|
+
const data = new Uint8Array(4 + body.length)
|
|
285
|
+
new DataView(data.buffer).setUint32(0, body.length)
|
|
286
|
+
data.set(body, 4)
|
|
287
|
+
ts += 33333
|
|
288
|
+
decoder.decode(new EncodedVideoChunk({ type: t === 5 ? 'key' : 'delta', timestamp: ts, data }))
|
|
289
|
+
}
|
|
290
|
+
for (;;) {
|
|
291
|
+
const { done, value } = await reader.read()
|
|
292
|
+
if (done) break
|
|
293
|
+
buf = concat(buf, value)
|
|
294
|
+
// 一个 NAL 在「看到下一个起始码」时才算完整;尾部不完整 NAL 留到下次 read
|
|
295
|
+
const sc = findSC(buf, 0)
|
|
296
|
+
if (sc === -1) { buf = buf.subarray(Math.max(0, buf.length - 3)); continue }
|
|
297
|
+
if (sc > 0) buf = buf.subarray(sc)
|
|
298
|
+
for (;;) {
|
|
299
|
+
const next = findSC(buf, 3)
|
|
300
|
+
if (next === -1) break
|
|
301
|
+
push(buf.subarray(0, next))
|
|
302
|
+
buf = buf.subarray(next)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} catch { /* abort/网络 → 回退 */ }
|
|
306
|
+
if (h264 && h264.abort === abort) stopH264()
|
|
307
|
+
return true
|
|
308
|
+
}
|
|
309
|
+
function findSC(b, from) {
|
|
310
|
+
for (let i = from; i + 3 < b.length; i++) {
|
|
311
|
+
if (b[i] === 0 && b[i + 1] === 0 && (b[i + 2] === 1 || (b[i + 2] === 0 && b[i + 3] === 1))) return i
|
|
312
|
+
}
|
|
313
|
+
return -1
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function fmtAge(ms) {
|
|
317
|
+
if (!ms || ms < 0) return '—'
|
|
318
|
+
if (ms < 1000) return (Math.round(ms / 100) / 10) + ' 秒前'
|
|
319
|
+
if (ms < 60000) return Math.round(ms / 1000) + ' 秒前'
|
|
320
|
+
return Math.round(ms / 60000) + ' 分钟前'
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// 目标名/基址来自 rsp_connect 参数(调用者可控),插 innerHTML 前必须转义,防存储型 XSS。
|
|
324
|
+
function esc(s) {
|
|
325
|
+
return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]))
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function fillSelect(sel, options, keepValue) {
|
|
329
|
+
const cur = keepValue !== undefined ? keepValue : sel.value
|
|
330
|
+
sel.innerHTML = options.map(([v, label]) => '<option value="' + String(v).replace(/"/g, '"') + '">' + esc(label) + '</option>').join('')
|
|
331
|
+
if ([...sel.options].some((o) => o.value === cur)) sel.value = cur
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function renderMemory(s) {
|
|
335
|
+
const rem = s.remembered || { targets: [], sessions: {} }
|
|
336
|
+
const opts = [['', '(选择记住的目标)']]
|
|
337
|
+
for (const t of rem.targets || []) opts.push([t.name, (rem.active === t.name ? '★ ' : '') + t.name + ' · ' + t.baseUrl])
|
|
338
|
+
fillSelect($('remembered'), opts)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async function refreshSessions() {
|
|
342
|
+
try {
|
|
343
|
+
const r = await jfetch('api/sessions')
|
|
344
|
+
const opts = [['', '默认(最近活动的会话)']]
|
|
345
|
+
const list = r.sessions || []
|
|
346
|
+
// 嵌入认领的会话可能还没连接(不在列表里):也要出现在下拉中,标明「本会话」
|
|
347
|
+
if (watchSid && !list.some((s) => s.session === watchSid)) {
|
|
348
|
+
opts.push([watchSid, '◌ ' + String(watchSid).slice(0, 8) + ' · 本会话(未连接)'])
|
|
349
|
+
}
|
|
350
|
+
for (const s of list) {
|
|
351
|
+
const label = (s.connected ? '● ' : '○ ') + String(s.session).slice(0, 8) + ' · ' + (s.target || s.baseUrl || '—')
|
|
352
|
+
opts.push([s.session, s.session === watchSid ? label + '(本会话)' : label])
|
|
353
|
+
}
|
|
354
|
+
fillSelect($('watchSession'), opts, watchSid || '')
|
|
355
|
+
} catch {}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function renderStatus(s) {
|
|
359
|
+
const c = s.client || {}
|
|
360
|
+
const connected = !!s.connected
|
|
361
|
+
setPill(connected ? 'on' : 'off', connected ? '已连接' : '未连接')
|
|
362
|
+
$('target').textContent = connected && s.target ? ((s.target.name ? s.target.name + ' · ' : '') + s.target.baseUrl) : ''
|
|
363
|
+
$('connDetails').open = !connected
|
|
364
|
+
const rows = [
|
|
365
|
+
['目标', s.target ? (s.target.name || '—') : '—'],
|
|
366
|
+
['基址', s.target ? s.target.baseUrl : '—'],
|
|
367
|
+
['令牌', s.target ? s.target.token : '—'],
|
|
368
|
+
['画面通道', c.screenState || '—'],
|
|
369
|
+
['实时通道', c.h264 ? ('H.264 · ' + h264Fps + ' fps') : (c.h264State && c.h264State !== 'idle' ? c.h264State : 'JPEG')],
|
|
370
|
+
['操控通道', (c.controlState || '—') + (c.controlMode ? ' / ' + c.controlMode : '')],
|
|
371
|
+
['逻辑尺寸', c.logical ? (c.logical.width + '×' + c.logical.height) : '—'],
|
|
372
|
+
['最近帧', c.lastFrame ? (c.lastFrame.width + '×' + c.lastFrame.height) : '—'],
|
|
373
|
+
['帧数', c.frameCount ?? '—'],
|
|
374
|
+
['引擎就绪', c.engine ? String(c.engine.ready) : '—'],
|
|
375
|
+
['辅助功能', c.engine ? String(c.engine.axTrusted) : '—'],
|
|
376
|
+
['屏幕录制', c.engine ? String(c.engine.screenCapture) : '—'],
|
|
377
|
+
]
|
|
378
|
+
$('status').innerHTML = rows.map(([k, v]) => '<div class="kv"><span class="k">' + esc(k) + '</span><span class="v">' + esc(v) + '</span></div>').join('')
|
|
379
|
+
renderMemory(s)
|
|
380
|
+
const fc = c.frameCount ?? -1
|
|
381
|
+
if (c.lastFrame && c.lastFrame.at) lastFrameAt = c.lastFrame.at
|
|
382
|
+
$('dims').textContent = c.lastFrame ? (c.lastFrame.width + '×' + c.lastFrame.height) : '—'
|
|
383
|
+
|
|
384
|
+
isConnected = connected
|
|
385
|
+
if (!connected) { if (streaming) stopStream(); stopH264(); showLayer('placeholder'); return }
|
|
386
|
+
if (c.h264) {
|
|
387
|
+
// 实时 H.264 优先:画布真正出帧后才收掉 MJPEG/快照层,避免切换空白闪
|
|
388
|
+
startH264()
|
|
389
|
+
if (h264Live) { if (streaming) stopStream() }
|
|
390
|
+
else {
|
|
391
|
+
if (!streaming) startStream()
|
|
392
|
+
if (fc !== lastFrameCount) { lastFrameCount = fc; refreshSnapshot() }
|
|
393
|
+
}
|
|
394
|
+
return
|
|
395
|
+
}
|
|
396
|
+
stopH264()
|
|
397
|
+
if (!streaming) startStream()
|
|
398
|
+
// 帧数变化(或有首帧)时刷新快照层,兜住 MJPEG 静止时拿不到首帧的情况
|
|
399
|
+
if (fc !== lastFrameCount) { lastFrameCount = fc; refreshSnapshot() }
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function tickAge() {
|
|
403
|
+
const age = lastFrameAt ? Date.now() - lastFrameAt : 0
|
|
404
|
+
$('age').textContent = fmtAge(age)
|
|
405
|
+
const tag = $('liveTag')
|
|
406
|
+
if (h264) { tag.className = 'pill on'; $('liveText').textContent = 'H.264 ' + h264Fps + 'fps'; return }
|
|
407
|
+
if (streaming && age > 3000) { tag.className = 'pill warn'; $('liveText').textContent = '静止' }
|
|
408
|
+
else if (streaming) { tag.className = 'pill on'; $('liveText').textContent = '实时' }
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async function refresh() {
|
|
412
|
+
try { renderStatus(await jfetch('api/status' + statusQ())) } catch {}
|
|
413
|
+
refreshSessions()
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function resetStage() {
|
|
417
|
+
stopStream(); stopH264(); h264Live = false; lastFrameCount = -1; lastFrameAt = 0
|
|
418
|
+
showLayer('placeholder')
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
$('connectRemembered').onclick = async () => {
|
|
422
|
+
const name = $('remembered').value
|
|
423
|
+
if (!name) { msg('先在下拉中选择一个记住的目标', 'err'); return }
|
|
424
|
+
msg('连接中…')
|
|
425
|
+
const body = { name }
|
|
426
|
+
if (watchSid) body.session = watchSid
|
|
427
|
+
try {
|
|
428
|
+
const r = await jfetch('api/connect', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
429
|
+
msg('已连接 ' + (r.info && r.info.protocol), 'ok')
|
|
430
|
+
resetStage()
|
|
431
|
+
renderStatus(r.status)
|
|
432
|
+
} catch (e) { msg(e.message, 'err') }
|
|
433
|
+
}
|
|
434
|
+
$('watchSession').onchange = () => {
|
|
435
|
+
watchSid = $('watchSession').value || null
|
|
436
|
+
resetStage()
|
|
437
|
+
refresh()
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
$('connect').onclick = async () => {
|
|
441
|
+
msg('连接中…')
|
|
442
|
+
const body = {}
|
|
443
|
+
if ($('url').value.trim()) body.url = $('url').value.trim()
|
|
444
|
+
if ($('host').value.trim()) body.host = $('host').value.trim()
|
|
445
|
+
if ($('port').value.trim()) body.port = Number($('port').value.trim())
|
|
446
|
+
if ($('token').value) body.token = $('token').value
|
|
447
|
+
if ($('name').value.trim()) body.name = $('name').value.trim()
|
|
448
|
+
if (watchSid) body.session = watchSid
|
|
449
|
+
try {
|
|
450
|
+
const r = await jfetch('api/connect', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
451
|
+
msg('已连接 ' + (r.info && r.info.protocol), 'ok')
|
|
452
|
+
$('token').value = ''
|
|
453
|
+
resetStage()
|
|
454
|
+
renderStatus(r.status)
|
|
455
|
+
} catch (e) { msg(e.message, 'err') }
|
|
456
|
+
}
|
|
457
|
+
$('disconnect').onclick = async () => {
|
|
458
|
+
try { await jfetch('api/disconnect', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(watchSid ? { session: watchSid } : {}) }); msg('已断开', 'ok') } catch (e) { msg(e.message, 'err') }
|
|
459
|
+
resetStage()
|
|
460
|
+
refresh()
|
|
461
|
+
}
|
|
462
|
+
$('resync').onclick = () => { const was = streaming; stopStream(); stopH264(); lastFrameCount = -1; if (was) { startStream(); refreshSnapshot() } refresh() }
|
|
463
|
+
$('fullscreen').onclick = () => {
|
|
464
|
+
const el = $('stage')
|
|
465
|
+
if (document.fullscreenElement) document.exitFullscreen()
|
|
466
|
+
else if (el.requestFullscreen) el.requestFullscreen()
|
|
467
|
+
else if (el.webkitRequestFullscreen) el.webkitRequestFullscreen()
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
refresh()
|
|
471
|
+
setInterval(refresh, 1500)
|
|
472
|
+
setInterval(tickAge, 250)
|
|
473
|
+
</script>
|
|
474
|
+
</body>
|
|
475
|
+
</html>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// 端到端验证(经宿主代理):读 /api/screen.h264 同时制造屏幕变化,统计帧率。
|
|
2
|
+
// 用法: RSP_TOKEN=<token> node verify-e2e.mjs [base]
|
|
3
|
+
const base = process.argv[2] || 'http://127.0.0.1:3398/capyclaw/rsp'
|
|
4
|
+
const token = process.env.RSP_TOKEN
|
|
5
|
+
const mac = 'http://192.168.1.38:8765'
|
|
6
|
+
const res = await fetch(base + '/api/screen.h264')
|
|
7
|
+
console.log('proxy', res.status, res.headers.get('x-rsp-format'))
|
|
8
|
+
if (res.status !== 200) process.exit(1)
|
|
9
|
+
const reader = res.body.getReader()
|
|
10
|
+
let buf = Buffer.alloc(0), frames = 0, bytes = 0, nals = 0
|
|
11
|
+
const gaps = []; let last = 0
|
|
12
|
+
const findSC = (b, from) => { for (let i = from; i + 3 < b.length; i++) { if (b[i] === 0 && b[i + 1] === 0 && (b[i + 2] === 1 || (b[i + 2] === 0 && b[i + 3] === 1))) return i } return -1 }
|
|
13
|
+
const nalType = (n) => { let i = 0; if (n[0] === 0 && n[1] === 0 && n[2] === 0 && n[3] === 1) i = 4; else if (n[0] === 0 && n[1] === 0 && n[2] === 1) i = 3; return n[i] & 0x1f }
|
|
14
|
+
const move = (i) => {
|
|
15
|
+
const a = i / 8
|
|
16
|
+
const x = 960 + Math.round(200 * Math.cos(a)), y = 540 + Math.round(200 * Math.sin(a))
|
|
17
|
+
return fetch(`${mac}/control?token=${token}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ cmd: 'mouse', action: 'move', x, y }) }).catch(() => {})
|
|
18
|
+
}
|
|
19
|
+
const t0 = Date.now()
|
|
20
|
+
let i = 0
|
|
21
|
+
const mover = setInterval(() => move(i++), 40)
|
|
22
|
+
const timer = setTimeout(() => reader.cancel().catch(() => {}), 6000)
|
|
23
|
+
try {
|
|
24
|
+
while (true) {
|
|
25
|
+
const { done, value } = await reader.read()
|
|
26
|
+
if (done) break
|
|
27
|
+
bytes += value.length
|
|
28
|
+
buf = Buffer.concat([buf, Buffer.from(value)])
|
|
29
|
+
let idx
|
|
30
|
+
while ((idx = findSC(buf, 0)) !== -1) {
|
|
31
|
+
const next = findSC(buf, idx + 3)
|
|
32
|
+
if (next === -1) break
|
|
33
|
+
const nal = buf.subarray(idx, next); buf = buf.subarray(next)
|
|
34
|
+
nals++
|
|
35
|
+
const t = nalType(nal)
|
|
36
|
+
if (t === 1 || t === 5) { const now = Date.now(); if (last) gaps.push(now - last); last = now; frames++ }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch { /* cancel */ }
|
|
40
|
+
clearTimeout(timer); clearInterval(mover)
|
|
41
|
+
const dur = (Date.now() - t0) / 1000
|
|
42
|
+
gaps.sort((a, b) => a - b)
|
|
43
|
+
const p = (q) => gaps.length ? gaps[Math.floor(q * (gaps.length - 1))] : -1
|
|
44
|
+
console.log(JSON.stringify({ dur: +dur.toFixed(1), nals, frames, fps: +(frames / dur).toFixed(1), kbps: Math.round(bytes * 8 / dur / 1000), gapP50: p(0.5), gapP90: p(0.9) }))
|