Skip to content

feat: record the screen to an MP4 file with iph record - #11

Merged
HyunSeob merged 2 commits into
HyunSeob:mainfrom
Kofktu:feat/record-annotate
Nov 20, 2026
Merged

HyunSeob merged 2 commits into
HyunSeob:mainfrom
Kofktu:feat/record-annotate

Conversation

@Kofktu

@Kofktu Kofktu commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

E2E evidence on a real phone needs more than a plain screenshot: a video of the flow. It comes from the phone without extra tools. The screen-sharing call iph already opens for input can also carry the screen as HEVC over RTP.

What changed

  • iph record start|stop|status [-o file.mp4] [--display N], also as MCP iph_record and script record(). The recorder opens its own video-only display session, so input sessions keep working and are opened and closed as before. The HEVC stream is written to an MP4 with shiguredo_mp4 (zero dependencies, sans I/O), with no ffmpeg or other runtime dependency. Frames go to <file>.part as they come; stop hands the sample table to the muxer and writes the index it returns at the end of the file. A frame with no picture (its slices lost, or only parameter sets or SEI) is not written.
  • Each session stops only its own streams: the phone's stop request accepts a stream's RemoteSSRC in identifiers. stopAll is a fallback used only when nothing but that session's streams runs. Session ids are kept in <udid>.media, and housekeeping stops streams a killed iph left behind.
  • stop returns {path, duration_s, frames, keyframes, lost_packets, bytes, width, height}. The phone sends one keyframe per stream, so a lost packet damages the rest of the file: stop then adds first_loss_s and a packets_lost warning. A recording ends on its own when the video stops, the connection resets, the script that started it ends, or after record_max_secs (1800 s by default).
  • A geometry check refuses a display with display_mismatch when its video and the accessibility screen do not have the same shape, so a phone with two displays does not record the other one.
  • Default file names carry milliseconds, so two recordings started in the same second do not overwrite each other.

Scope

  • Portrait only, like the rest of iph. An unfolded iPhone Duo is not supported by iph itself.
  • No batch step: a recording spans requests, and a failing batch would leave it running.
  • Record over USB. Over Wi-Fi, 2 of 9 short recordings lost packets in testing.
  • No version bump. The crate needs Rust 1.93, so the README's Rust note moves from 1.88 to 1.93.
  • iph screenshot --annotate, left in from testing, was removed during review.

Tradeoffs

  • A transport error or command timeout during a recording ends it. The tunnel is rebuilt so later commands recover, and the result says why the recording ended. Keeping the tunnel would keep the recording but can leave every later command failing on a broken tunnel. Only a closed accessibility connection keeps the recording.
  • Leftover streams are stopped by housekeeping, not when the next session opens. One bounded attempt per tick keeps commands from waiting on a stuck leftover; the cost is that a new session can start while a leftover from a killed iph is still streaming, for a few seconds. The resident process also tries once more on its way out (up to 20 s), so a recording ended by a reset does not keep the phone encoding for the hour of its RTCP timeout when no command follows.

Verification

  • cargo test --locked passes, including the RTP and 24 kHz timing, the MP4 read back with shiguredo_mp4's demuxer, frames without a picture, loss accounting before and after the first keyframe, the stop decisions (including a status without the session list), and the geometry guard. Clippy: no new warning.
  • After the switch to shiguredo_mp4, on an iPhone17,1 (iOS 27.2.1) over Wi-Fi: 4 recordings of 15 to 19 s with swipes during the recording lost no packet; stop frames equalled ffprobe -count_frames (1044, 946, 815, 453), ffmpeg decoded without errors, AVFoundation decoded every frame, and creation_time was the recording time.
  • The runs below on the iPhone Duo were made with the earlier hand-written muxer and were not repeated. The review fixes for a status without the session list, the reap on exit and millisecond file names are covered by unit tests only.
  • On an iPhone19,4 (folded, cover display) with iOS 27.2.1 over USB, from macOS:
    • Recordings of 10 to 60 s: stop frames always equal ffprobe -count_frames, ffmpeg decodes without errors, and AVFoundation plays them.
    • Taps, swipes and two-finger pinches during a recording all landed. With input_idle_secs = 0 (input opens and closes on every command) the recording kept going, and the phone stopped only each input's audio.
    • A closed accessibility connection kept the recording. A reset ended it with its cause and a complete file. After kill -9, the next command worked and the leftover streams were stopped.
    • A script that started a recording and threw ended it with a valid file. record_max_secs = 8 ended a recording at 8.8 s.
  • On an iPhone17,1 over Wi-Fi: 3 clean 10 s recordings, and the losses that did happen were reported with a first_loss_s that matched where ffmpeg starts failing to decode.

🤖 Generated with Claude Code

@Kofktu
Kofktu force-pushed the feat/record-annotate branch from 3229f72 to 054b758 Compare November 19, 2026 10:30
@HyunSeob

Copy link
Copy Markdown
Owner

Thanks for another super thorough PR — the real-device measurements, failure-mode handling, and unit tests are great as always.

iph record looks like a genuinely useful addition to iph. Being able to capture MP4 video directly alongside normal tap/read commands without requiring ffmpeg (and working on both macOS and Windows) fits the project's single-binary philosophy really well, and separating stream teardown per RemoteSSRC via MediaControl is a very clean solution to the stopAll limitation.

Before diving into a line-by-line review, I'd love to align on a few higher-level questions:

  1. Intent & use case for iph screenshot --annotate:

    • Could you share a bit more about how you're using --annotate in practice?
    • The description frames it around PRs and bug reports, where outlining/numbering every interactive element on screen can feel a bit visually noisy compared to a clean screenshot (and iph's core idea is avoiding screenshot/SoM loops in favor of the accessibility tree).
    • On the other hand, if it's mainly useful as an agent fallback for screens with unlabeled icon buttons (matching visual icons to ids), that's a different story — I'd like to understand the primary workflow you have in mind before deciding whether it belongs in core.
  2. Splitting the PR:

    • Since iph record and iph screenshot --annotate are independent features (only sharing src/geometry.rs) and the diff is ~3.4k lines, would you be open to splitting them into separate PRs? That would let us review and land iph record cleanly first while we discuss --annotate.
  3. Hand-rolled codecs vs. pure-Rust crates:

    • Avoiding external runtime dependencies like ffmpeg definitely matches iph's philosophy.
    • For the in-process pieces, though, did you consider pure-Rust crates — e.g. png (if we keep --annotate) instead of a custom PNG codec in src/png.rs, or a zero-dep crate like shiguredo_mp4 for the MP4/SPS side — vs. maintaining custom parsers/muxers long-term? Curious how you weighed the tradeoff.

@Kofktu
Kofktu force-pushed the feat/record-annotate branch from 054b758 to c75d985 Compare November 19, 2026 15:07
@Kofktu Kofktu changed the title feat: record the screen with iph record and number elements with screenshot --annotate feat: record the screen to an MP4 file with iph record Nov 19, 2026
@Kofktu

Kofktu commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful read, and for asking before going line by line.

  1. --annotate: there isn't a real workflow behind it. It went in while I was testing the recorder and stayed in the branch when it shouldn't have, so I've removed it from this PR.

  2. Split: done. This PR is now iph record only (2 commits: the feature and its docs). The MCP tool count, README sections and SKILL.md were trimmed to match, and flate2, src/png.rs and src/annotate.rs are no longer in the diff. While splitting I ran a review pass over the record part and folded in three fixes:

    • when the media server's status has no sessions list (a shape not seen yet), a session's streams are stopped with stopAll as before, instead of being taken as already stopped;
    • the resident process tries once more to stop leftover streams on its way out, so a recording ended by a reset doesn't keep the phone encoding for the RTCP timeout when no command follows;
    • default file names carry milliseconds, so two recordings started in the same second (e.g. from a script, which can't pick a path) don't overwrite each other.

    These three are covered by unit tests only; the device runs in the description were made before them.

  3. Hand-rolled MP4/SPS vs. crates: honestly, I didn't weigh this properly; I went for "no new dependency" and stopped there. Looking at shiguredo_mp4 now, it fits better than I expected: zero dependencies, sans-I/O (Mp4FileMuxer leaves writing to the caller and puts moov at the end, which matches the .part file + index-at-stop design), and it has parse_sps and build_hvc1_box. That would replace src/record/mp4.rs and the SPS / hvcC half of src/record/hevc.rs; the RTP depacketizer and loss accounting would stay. It is Apache-2.0 and needs Rust 1.93. If you'd prefer that, I'm happy to switch in this PR and re-run the device checks (frame counts vs. ffprobe, AVFoundation playback) before you review. png is moot now that --annotate is out.

@HyunSeob

Copy link
Copy Markdown
Owner

Thanks for trimming --annotate so cleanly and catching those three edge cases along the way.

Yes, switching to shiguredo_mp4 sounds great — zero-dep sans-I/O fits the .part design nicely and saves us from maintaining custom MP4/SPS code long-term. Bumping the Rust version note in the READMEs from 1.88 to 1.93 is totally fine.

Two small things to fold in while you're in there:

  • Please rebase onto latest main (PR feat: capture the phone's log to a file with iph syslog #9 just landed, so there are a few conflicts).
  • In Track::take, skip writing a sample if au.sample() is empty (e.g. when a lost packet drops all fragments of an AU, or an AU only carries parameter sets / AUD), so we never emit a 0-byte sample into the MP4.

Once that's in and re-tested on a device, I'll do a full pass!

Kofktu and others added 2 commits November 20, 2026 09:59
`iph record start|stop|status` writes the phone's screen to an MP4 while
other commands keep running, so an E2E run can keep video evidence of
what happened. Taps, swipes and reads work during a recording.

The recorder opens a screen-sharing session of its own with the phone,
the video of one display, next to the input session's audio. The device
library stops streams only with `stopAll`, which would let every input
retry, idle close or sweep of an abandoned session end the recording.
The phone also takes a list of streams to stop, each named by the
`RemoteSSRC` of its stream configuration (sent as XPC uint64; it refuses
int64 and strings), so `device::media::MediaControl` speaks the display
service's requests itself and each session stops only its own streams.
Seen on an iPhone Duo (iOS 27.2.1): stopping a recording's video by its
SSRC left the input's audio running and taps working, and an input
session dropped and swept mid-recording left the video running. A phone
that refuses the list, or still lists the stream afterwards, is told to
stop all streams instead.

A recording ends with a reason in `ended_reason`, and the file is
completed with what came, when the connection to the phone is closed,
or reset after a connection error, an accessibility service that would
not open, or a command that ran out of time (not when only the
accessibility connection or the input session is made again), when the
phone stops sending the video (none for 2.5 s and the phone no
longer lists the stream: a still screen can be silent that long), when
the phone changes the video's VPS, SPS or PPS (the file has one hvcC),
or after `record_max_secs` (default 1800 s: at the phone's cap of
6 Mbit/s, 30 minutes is up to 1.4 GB, and a recording nobody stopped
ends within the daemon's own idle time). Its report waits for `stop`,
or the next `start` returns it under `previous`; the daemon does not
exit while it records, nor for its idle time after a recording ended
with nobody told. A recording a script started and did not stop ends
with the script, also on a throw, and the script's result has its
report under `recording`.

The phone sends HEVC over RTP (payload type 100) with a 24 kHz clock and
the parameter sets in band. It sends one keyframe per stream, so `start`
waits for it, and a packet lost later damages the picture until the end
while players still open the file. Losses before that keyframe damage
nothing and are not counted. `stop` and `status` report `lost_packets`,
and after a loss `first_loss_s` (where the damage starts in the file)
and a `packets_lost` warning; the result stays `ok`. Frames go to
`<file>.part` as they come and only the sample table stays in memory;
`stop` appends the index and renames the file, and a start given up on
the way leaves no file. On a still screen the phone may send few frames,
so the last frame lasts until `stop`. A frame with no picture (its slices
lost, or only parameter sets or SEI) is not written, and the frame
before it lasts in its place. The MP4 boxes and the hvcC come from
`shiguredo_mp4` (no dependencies, sans I/O): `stop` hands it the sample
table and writes the index it returns at the end of the file.

Without `--display`, displays 1, 0 and 2 are tried and the first whose
video has the shape of the screen being read is kept. The new geometry
check requires the horizontal and vertical pixels-per-point ratios to
agree within 1%, else `display_mismatch`. On a folded iPhone Duo,
displays 0 and 1 both show the cover screen and display 2 sends nothing.

A killed daemon leaves its streams running on the phone for their RTCP
timeout, an hour; minutes after a `kill -9` the media server still listed
them. The input session and the recorder write their client session id
to `<udid>.media` in the state directory, removed once its streams are
stopped. The daemon's housekeeping stops one left id at a time, skipping
the ids still in use, for at most 3 s with the session held and then 30 s
apart after a failure, never on a request's way or inside an open; the daemon's exit tries once
more, for up to 20 s. It
stops only the streams the media server lists under such an id, so a
stream another client started (Xcode's screen view, another Mac) is never
stopped.

The same command is the MCP tool `iph_record` (one tool with an action,
as `iph_dnd`) and the script function `record()`, which is not an input
and cannot choose the file. Batch is left out: a recording spans
requests, and a failing batch would leave it running.

Measured on an iPhone Duo (iOS 27.2.1, folded, USB): 1408x2048 at 50 to
52 fps while the screen moves, about 0.5 Mbit/s, no lost packets. On an
iPhone 16 Pro over Wi-Fi, 2 of 9 short recordings lost packets (9 and
51). After the switch to `shiguredo_mp4`, 3 recordings of 15 to 19 s on
the iPhone 16 Pro (iOS 27.2.1) over Wi-Fi lost no packet, their frame
counts matched `ffprobe -count_frames`, and ffmpeg and AVFoundation
decoded every frame.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
README.md and README.ko.md get a section on recordings: that a recording
has its own screen-sharing session next to the input's, why to record
over USB (one keyframe per stream), what a still screen does to the
frame count, how the display is chosen, when a recording ends on its own
(`record_max_secs`, a lost connection or video) and where its report
goes, what a script's end does to a recording it started, and what is
left when the resident process stops or is killed. The command table,
the config example, the MCP tool list, batch (no recording step) and the
known limits follow: over Wi-Fi 2 of 9 short recordings lost packets in
testing, and a loss damages the rest of the file, reported as
`first_loss_s` and a `packets_lost` warning.

skills/iph/SKILL.md tells agents when to keep this evidence, to record
over USB and stop every recording, and that a script's leftover
recording ends with the script.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Kofktu
Kofktu force-pushed the feat/record-annotate branch from c75d985 to 0acbfb5 Compare November 20, 2026 00:59
@Kofktu

Kofktu commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! All three are in, and the PR is mergeable again.

  • Rebase: on top of main with feat: capture the phone's log to a file with iph syslog #9. The conflicts were all "keep both": syslog and record sit side by side in the daemon, scripts and MCP, and a reset now ends a running capture and a running recording with the same cause. The MCP tool count is 21 everywhere in the README and site (main had 19 in two places and 20 in one).
  • shiguredo_mp4: the hand-written box writer, SPS parser and hvcC builder are gone (src/record/{mp4,hevc}.rs, about 200 lines fewer). Frames still go to <file>.part as they arrive; at stop the sample table is handed to Mp4FileMuxer and the boxes it returns are written, with moov at the end. The README's Rust note is now 1.93.
  • Empty samples in Track::take: I made the check a bit stricter than au.sample().is_empty(). A frame is skipped when it has no VCL NAL, so a frame that lost its slice but kept an SEI is not written either. A test covers a parameter-sets-only frame, an SEI-only frame and a frame whose slice lost a fragment.

A review pass over the new writer found three small things, also fixed. The crate's default creation_time is 1970, so it is now the recording time. The file is cut right after moov in case a failed write left bytes past it. A failed stop removes the .part file instead of leaving it.

Re-tested on a device: an iPhone 16 Pro (iOS 27.2.1) over Wi-Fi, 4 recordings of 15 to 19 s with swipes during the recording. No packets were lost. stop frames equalled ffprobe -count_frames (1044, 946, 815, 453). ffmpeg decoded them without errors and AVFoundation decoded every frame. The iPhone Duo / USB runs in the description were not repeated, since it isn't connected right now.

@HyunSeob HyunSeob left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great — switching to shiguredo_mp4 cleaned up the MP4/HEVC layer nicely, and the rebase and has_picture() guard all look solid. Thanks for the thorough work on this, merging now!

@HyunSeob
HyunSeob merged commit 7422253 into HyunSeob:main Nov 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants