Skip to content

fix(ios): forward Apple Watch audio chunks without holding the whole recording - #21072

Merged
aryanorastar merged 3 commits into
BasedHardware:mainfrom
aryanorastar:fix/20479-watch-chunks-flat-memory
Oct 10, 2026
Merged

aryanorastar merged 3 commits into
BasedHardware:mainfrom
aryanorastar:fix/20479-watch-chunks-flat-memory

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

What changed and why

Part of #20479: an 11-hour Apple Watch recording ended with almost nothing saved.

What happens today. While the Watch records, the iPhone app forwards each 1.5 s chunk to Flutter's capture path, which is the real audio path. It also keeps every chunk in AppDelegate.audioChunks until the last one arrives. Then it stitches them into one buffer and sends it to Flutter's onAudioData, whose handler is empty (apple_watch_bridge.dart), so that copy is thrown away. The stored copy grows by about 115 MB per recorded hour (16 kHz, 16-bit mono), roughly 1.3 GB at 11 hours, plus a second full copy at the end. iOS kills a background app long before that.

What this PR does. The iPhone keeps nothing:

  • Each chunk goes to Flutter as it arrives, through a stateless WatchAudioChunkRelay.
  • The stitch-and-send step and the audioChunks store are deleted.
  • The Watch's empty end-of-recording marker is dropped instead of forwarded.

Flutter's chunk handler (watch_transport.dart) ignores isLast and only forwards bytes, so the audio Flutter receives is unchanged.

Not in this PR:

  • Restart after a relaunch. Once iOS has relaunched the app, isRecordingActive is false and later chunks are dropped. Turning recording back on when a stray chunk arrives would create phantom recordings: queued background chunks can arrive after Stop. That needs a session id from the Watch.
  • File-based transfer from the Watch. Saving each recording to a file on the Watch and sending it with transferFile is a separate change.

Product invariants affected

none

How it was verified

End to end on simulators: a watchOS 27 Watch simulator paired with an iPhone 16 Pro simulator (iOS 26.5), and the Watch app installed as a dependent app.

  • Both sides reported reachable: YES, paired: YES, appInstalled: YES.
  • The iPhone app logged each incoming chunk message while the Watch recorded.

Memory A/B:

  • One 10-minute Watch recording per build, main vs this branch, both run the same way: app opened from the Home Screen, 388 chunks each.
  • Measured phys_footprint (footprint -f bytes) every 15 s. RSS can't show this in a simulator: its mic records silence, macOS compresses the all-zero chunks out of RSS, and both builds look flat.
Build Start After 10 min Shape
main 309 MB 357 MB (+48) stops releasing after 4.5 min, then climbs steadily
this branch 300 MB 319 MB (+19) falls back to its starting level at 6.5 min

Both curves rise and fall as Flutter's normal capture work runs, so the difference is in the trend, not every sample.

Not verified:

  • A real paired Watch and iPhone over a multi-hour recording; a simulator can't reproduce the 11-hour case.
  • Background termination by iOS itself.

Tests

app/ios/test/watch_audio_chunk_relay_test.rb, wired into .github/checks-manifest.yaml as ios-watch-audio-chunk-relay:

  • A swiftc harness compiles the real WatchAudioChunkRelay.swift, plays a 40-chunk Watch session, and checks:
    • every chunk is forwarded as it arrives, byte for byte, behind the 3 header bytes Flutter strips;
    • nothing is held back;
    • the empty end marker yields nothing.
  • A second test checks that AppDelegate routes chunks through the relay and has no chunk store.

With main's AppDelegate.swift restored, the second test fails ("Watch chunks must go through the relay"). On this branch both pass.

The iPhone app built and ran on the simulator with the new file in both app targets.

UI evidence

UI-Evidence: none -- native memory fix with no UI change

Failure class (fixes)

Failure-Class: none

🤖 Generated with Claude Code

View guided diff

aryanorastar and others added 3 commits October 7, 2026 09:10
…recording

The iPhone kept every Watch chunk in `audioChunks` until the last one, then
stitched them into one buffer for `onAudioData`, whose Flutter handler is
empty. Flutter's only consumer of Watch audio is the per-chunk `onAudioChunk`
stream, so the stored copy was never used, yet it grew about 115 MB per
recorded hour plus a second full copy at the end. A long session could get
the app killed in the background; after relaunch `isRecordingActive` is
false and every later chunk is dropped (BasedHardware#20479).

Each chunk now goes straight to Flutter and nothing is kept. The final chunk
is an empty end marker and is skipped as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Moves the chunk-to-Flutter step into a stateless WatchAudioChunkRelay so a
swiftc harness can prove every chunk is forwarded as it arrives and the empty
end marker is dropped, and asserts AppDelegate keeps no chunk store (BasedHardware#20479).
Adds the manifest check and the iOS changelog fragment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution ios labels Oct 9, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Reviewed against the merge-base diff (3bb93db..1b8e07e).

The core claim checks out. app/ios/Runner/AppDelegate.swift drops the audioChunks dictionary and reassembleAndSendAudioData(), which previously accumulated every Watch chunk (~115 MB per recorded hour) and then re-sent the whole stitched buffer via onAudioData. I verified onAudioData is a dead consumer: its Dart handler in app/lib/services/bridges/apple_watch_bridge.dart:26 is an empty body, the Android side is a stub, and the Pigeon-generated plumbing has no other caller. The real audio path is onAudioChunk -> AppleWatchFlutterBridge.onChunk -> WatchTransport._audioControllers (app/lib/services/devices/transports/watch_transport.dart), which forwards bytes only and ignores isLast - so deleting the stitched copy loses no audio.

Per-file notes:

  • app/ios/Runner/WatchAudioChunkRelay.swift - stateless enum, prepends the 3 header bytes, returns nil for the empty end marker. Previously that marker went out as a 3-byte all-header chunk; downstream only forwards bytes after stripping the header, so dropping it is behavior-neutral.
  • app/ios/Runner/AppDelegate.swift - clean deletion; the startRecording reset of audioChunks/nextExpectedChunkIndex goes away with the store it reset. sampleRate is now only a passthrough to onAudioChunk, which already carried it.
  • app/ios/test/watch_audio_chunk_relay_test.rb - good contract test: compiles the real relay with swiftc and asserts every chunk forwards byte-identical with zero held back, plus a static assertion that AppDelegate keeps no chunk store.
  • .github/checks-manifest.yaml - new ios-watch-audio-chunk-relay entry matches the shape and triggers of sibling entries (e.g. ios-flutter-launch-engine-guard), correctly listing all five touched files.
  • app/ios/Runner.xcodeproj/project.pbxproj - adds the file to both build targets; the hand-minted A204790x hex ID matches the synthetic-ID convention already used in this file, cosmetic only.
  • app/changelog/unreleased/20261009-watch-recording-memory.json - accurate, user-facing, iOS-scoped.

Caveat: I could not execute the ruby test on this review host (Linux, no swiftc); it is declared as a macOS-lane check consistent with its siblings and Mobile Release Eligibility passed on the PR head, but it hasn't been observed running in CI on this diff. The out-of-scope items (relaunch session handling, transferFile) are reasonable deferrals.

Nice fix - clear root-cause analysis, verified on simulators with a memory A/B, and a test that pins the memory invariant. Leaving for human maintainer review before merge.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@aryanorastar
aryanorastar merged commit c3561a1 into BasedHardware:main Oct 10, 2026
48 of 49 checks passed
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Thanks for checking the audio path end to end. On the caveat: ios-watch-audio-chunk-relay passes on macOS in make preflight, and with main's AppDelegate.swift restored it fails as intended ("Watch chunks must go through the relay"). It's in the macOS lane, which this fork PR reaches only once ci:full is added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ios positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants