Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools
Rust ai-agentautomationclaudeclaude-codedockeriosiphonelinuxmcpmcp-servermodel-context-protocolrustwebdriveragent
fix(viewer): a picture that was slow to start is tried again (#109)
Qinghe Lic88bc56b1f8c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0bc0a51..934ded8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,7 @@ All notable changes to this project are documented here. The format is based on### Fixed+- A control page whose H.264 or the phone's own video didn't show a first frame within 9 s (the viewer or daemon restarting, the encoder starting up) used MJPEG until it was reloaded; it now tries again after 30 s, 1 min and 2 min. A picture the viewer said isn't available still isn't tried again.- A swipe on the control page through WDA no longer waits for the list to stop before answering (reading the element tree again and again, up to 2.5 s): the person at the page sees it. On an iPad mini 5 a swipe took 3.1 s, now 1.6–2.3 s. Agents (MCP, CLI) still wait, so their next tap doesn't land on a moving list.- The way Home and locking work on each device is kept in `state.json` instead of only in memory. After the daemon restarted, or another device was controlled meanwhile, the first Home on a device where IOHID's Home doesn't work tried it, waited 2 s for the home screen and pressed again: 4.4 s instead of 0.8 on an iPad mini 5.diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.mdindex 0454519..230ef3f 100644--- a/CHANGELOG.zh-CN.md+++ b/CHANGELOG.zh-CN.md@@ -8,6 +8,7 @@### 修复+- 控制台页面的 H.264 或手机自己的视频 9 秒内没出第一帧时(控制台服务或守护进程正在重启、编码器正在启动),原来一直用 MJPEG 直到刷新页面;现在 30 秒、1 分钟、2 分钟后各再试一次。控制台服务明确说不可用的画面仍然不再试。- 控制台上经 WDA 的滑动不再等列表停下才返回(原来要反复读元素树,最多 2.5 秒):页面前的人看得见。iPad mini 5 上一次滑动从 3.1 秒降到 1.6–2.3 秒。Agent(MCP、命令行)仍然等,免得下一次点击落在还在滚的列表上。- 每台设备按 Home 和锁屏该用哪种方式,现在记在 `state.json` 里,不再只放在内存。原来守护进程重启或中途控制过别的设备后,在 IOHID 的 Home 不起作用的设备上,第一次按 Home 会先用 IOHID 按,等 2 秒没回到主屏幕再补按一次:iPad mini 5 上要 4.4 秒,平时 0.8 秒。diff --git a/apps/iphone-control/src/viewer.html b/apps/iphone-control/src/viewer.htmlindex 6e9c905..100584d 100644--- a/apps/iphone-control/src/viewer.html+++ b/apps/iphone-control/src/viewer.html@@ -2029,7 +2029,7 @@ function applyInfo(v) {highlight(null); marks.replaceChildren(); hoverDot = null;if (changed) {S.daemon = null; S.dstatus = null; S.apps = []; S.appsState = 'idle'; appNames.map = null; appNames.loading = false;- T.warm = 0; V.noDevice = false;+ T.warm = 0; V.noDevice = false; V.devRetries = 0;}}const reconnect = feedId && (changed || S.display?.id !== display?.id);@@ -2323,16 +2323,28 @@ function connect() {}firstFrameTimer = setTimeout(() => {if (epoch !== feedEpoch) return;- if (V.on && !V.got) {- if (dev) V.noDevice = true; else V.off = true;- connect();- } else feedError();+ if (V.on && !V.got) { giveUpVideo(dev, false); connect(); }+ else feedError();}, 9000);renderFeed(); renderChips();}/* ---------- H.264 画面:服务端用 ffmpeg 编码(硬件优先),这里用 WebCodecs 解码(浏览器自己选硬件)画到 canvas。* 消息:1 字节类型 + 4 字节长度 + 内容。0 是解码配置(JSON),1 关键帧,2 其他帧。* 这一页从没出过画面就失败(没有编码器、浏览器解不了)就改用 MJPEG,之后不再试 ---------- */+/* 放弃这一路画面,改用下一种。服务端说走不通(404:没有编码器、手机不支持)就不再试;只是第一帧迟迟不来+ * (viewer 或守护进程正在重启、编码器正在启动)的,过一阵再试:之前这样一次就永远停在 MJPEG。最多再试几次,+ * 真走不通的不会一直断画面 */+const VIDEO_RETRY_MS = [30000, 60000, 120000];+function giveUpVideo(dev, permanent) {+ if (dev) V.noDevice = true; else V.off = true;+ if (permanent) return;+ const n = dev ? (V.devRetries = (V.devRetries || 0) + 1) : (V.offRetries = (V.offRetries || 0) + 1);+ if (n > VIDEO_RETRY_MS.length) return;+ setTimeout(() => {+ if (dev) V.noDevice = false; else V.off = false;+ if (S.mode === 'live') connect();+ }, VIDEO_RETRY_MS[n - 1]);+}function stopVideo() {if (V.ctrl) V.ctrl.abort();V.ctrl = null; V.on = false; V.got = false; V.rate = null; V.stream = ''; V.recv = 0; V.acked = 0; V.acking = 0; V.reported = 0;@@ -2347,7 +2359,7 @@ async function startVideo(q, dev) {const got = V.got;stopVideo();// 这一路从没出过画面就走不通:手机的视频走不通改用本机编码,本机编码也不行改用 MJPEG,之后不再试- if (permanent && !got) { if (dev) V.noDevice = true; else V.off = true; connect(); return; }+ if (permanent && !got) { giveUpVideo(dev, true); connect(); return; }feedError();};const onFrame = f => {diff --git a/tests/viewer/video-retry.test.mjs b/tests/viewer/video-retry.test.mjsnew file mode 100644index 0000000..102e0a6--- /dev/null+++ b/tests/viewer/video-retry.test.mjs@@ -0,0 +1,45 @@+import assert from 'node:assert/strict';+import { readFileSync } from 'node:fs';+import { test } from 'node:test';+import { runInNewContext } from 'node:vm';++const html = readFileSync(new URL('../../apps/iphone-control/src/viewer.html', import.meta.url), 'utf8');+const src = html.match(/^const VIDEO_RETRY_MS = .*$/m)[0] + '\n' + html.match(/^function giveUpVideo\([^]*?^}/m)[0];++function load() {+ const timers = [], ctx = { V: {}, S: { mode: 'live' }, connects: 0 };+ ctx.setTimeout = (f, ms) => timers.push({ f, ms });+ ctx.connect = () => { ctx.connects++; };+ runInNewContext(src + '\nthis.giveUpVideo = giveUpVideo;', ctx);+ return { ctx, timers };+}++test('a picture that never answered is tried again, a few times at most', () => {+ const { ctx, timers } = load();+ for (const ms of [30000, 60000, 120000]) {+ ctx.giveUpVideo(false, false);+ assert.equal(ctx.V.off, true, 'MJPEG meanwhile');+ const t = timers.shift();+ assert.equal(t.ms, ms);+ t.f();+ assert.equal(ctx.V.off, false, 'H.264 again');+ }+ const before = ctx.connects;+ ctx.giveUpVideo(false, false);+ assert.equal(timers.length, 0, 'no more after three');+ assert.equal(ctx.V.off, true);+ assert.equal(ctx.connects, before);+});++test('a picture the viewer refused is not tried again; the phone\'s own video counts its own tries', () => {+ const { ctx, timers } = load();+ ctx.giveUpVideo(false, true);+ assert.equal(ctx.V.off, true);+ assert.equal(timers.length, 0);+ ctx.giveUpVideo(true, false);+ assert.equal(ctx.V.noDevice, true);+ assert.equal(timers[0].ms, 30000, 'its first retry');+ timers[0].f();+ assert.equal(ctx.V.noDevice, false);+ assert.equal(ctx.connects, 1);+});