Run a virtual iPhone on an Apple Silicon Mac.
Swift
Start libcamfix's preview-layer scan with a vcam session
Jacky069d530177ec
diff --git a/Research/Guest/virtual_camera_transport.md b/Research/Guest/virtual_camera_transport.mdindex bceae26d..d27c2fdb 100644--- a/Research/Guest/virtual_camera_transport.md+++ b/Research/Guest/virtual_camera_transport.md@@ -108,3 +108,33 @@ serialises patchers. Kill-and-relaunch stress (killing `cameracaptured`with Camera.app auto-reconnecting, the same collision the crashes camefrom) is the validation.++## Idle cost (2026-10-09)++With no camera client, both camera hooks used to keep timers running for+the life of their process:++- `libvcamcaptured` armed its 30 Hz viewfinder and sink drive when it+ installed its hooks, three seconds after `cameracaptured` started. Each tick+ copied an empty stream list and an empty sink table and returned, which woke+ the daemon 27,000–28,000 times in 15 idle minutes (30.2/s, 2–3 s of CPU) on+ an iPhone 27.0 guest. The drive now runs only while it has a consumer+ (`VCamCaptured/Frames/VCamDriveGate.{h,c}`): `-[FigCameraViewfinderStream+ openWithDestination:]` and every new `BWImageQueueSinkNode` or+ `BWRemoteQueueSinkNode` start it, and the first tick that finds no open+ stream and no live sink stops it. A consumer that appears during a tick+ keeps it running; start and stop run under the gate's lock. The log shows+ `viewfinder drive started (30 Hz, start #N)` and `viewfinder drive stopped`.+ `make -C VPhoneGuestComponents test-vcam-drive-gate` covers the gate.+- `libcamfix` loads into every app-path process that has AVFoundation, which+ on a fresh guest includes SpringBoard, PosterBoard, AccessibilityUIServer,+ InputUI and most widget extensions. Each armed the 1 Hz preview-layer scan+ on its main queue as soon as AVFCapture loaded, walking the whole layer tree+ every second. The scan now starts with the first vcam-bound session that+ reaches `-[AVCaptureSession _setRunning:YES]`; the preview-layer+ initializer hooks still adopt layers directly. If `_setRunning:` cannot be+ hooked, the scan starts at install as before.++After both changes `cameracaptured` took about one wakeup a second in the same+window, and AccessibilityUIServer and InputUI dropped from about 900 each to+under 10. The numbers are in [`service_trimming.md`](service_trimming.md#idle-cost-of-vphones-own-components).diff --git a/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.m b/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.mindex a0fcde38..2ab46347 100644--- a/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.m+++ b/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.m@@ -186,8 +186,8 @@ void cfx_install_preview_layer_hooks(void) {//// Fallback: scan UIApplication's windows for any AVCaptureVideoPreviewLayer// (or subclass) and adopt them. Covers a private layer class that never-// goes through the hooks above. Runs once a second; cheap if no windows-// match.+// goes through the hooks above. Runs once a second once a vcam session runs;+// cheap if no windows match.static void cfx_walk_layers(CALayer *layer, NSMutableArray *out) {if (!layer) return;@@ -239,17 +239,21 @@ static void cfx_scan_preview_layers(void) {}static dispatch_source_t cfx_scan_timer = NULL;+// Started by the first running vcam session (Session/CamFixSessionGuards.m).+// Callers run on any thread; the timer is created once.void cfx_start_scan_timer(void) {- if (cfx_scan_timer) return;- dispatch_queue_t q = dispatch_get_main_queue();- cfx_scan_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);- dispatch_source_set_timer(cfx_scan_timer,+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ cfx_scan_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,+ dispatch_get_main_queue());+ dispatch_source_set_timer(cfx_scan_timer,dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),1000000000ull,100000000ull);- dispatch_source_set_event_handler(cfx_scan_timer, ^{- @autoreleasepool { cfx_scan_preview_layers(); }+ dispatch_source_set_event_handler(cfx_scan_timer, ^{+ @autoreleasepool { cfx_scan_preview_layers(); }+ });+ dispatch_resume(cfx_scan_timer);+ cfxlog(@"scan timer armed (1 Hz)");});- dispatch_resume(cfx_scan_timer);- cfxlog(@"scan timer armed (1 Hz)");}diff --git a/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.m b/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.mindex 14b2df50..f60026f5 100644--- a/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.m+++ b/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.m@@ -63,6 +63,9 @@ static void cfx_session_setRunning_hook(id self, SEL _cmd, BOOL running) {if (running && cfx_session_is_for_vcam(self)) {cfx_log_session_start(self);cfx_track_vcam_session(self);+ // Only a process with a running vcam session can have a preview layer+ // to find.+ cfx_start_scan_timer();}if (!running && cfx_session_is_for_vcam(self)) {cfxlog(@"[session _setRunning:NO] suppressed for vcam session %p", self);@@ -97,6 +100,9 @@ void cfx_install_session_guards(void) {cfxlog(@"installed _setRunning: hook");} else {cfxlog(@"_setRunning: not in objc table");+ // Without the hook nothing would start the scan: keep the old+ // always-on fallback.+ cfx_start_scan_timer();}if (m2) {cfx_orig_setInterrupted = method_setImplementation(m2, (IMP)cfx_session_setInterrupted_hook);diff --git a/VPhoneGuestComponents/CamFix/libcamfix.m b/VPhoneGuestComponents/CamFix/libcamfix.mindex 1a3b3b84..4c092c7b 100644--- a/VPhoneGuestComponents/CamFix/libcamfix.m+++ b/VPhoneGuestComponents/CamFix/libcamfix.m@@ -82,7 +82,11 @@ static void cfx_install_all_hooks(void) {cfx_install_photo_representation_hooks();cfx_install_capturerequest_stubs();cfx_install_remote_queue_tap();- cfx_start_scan_timer();+ // The 1 Hz preview layer scan starts with the first running vcam session+ // (Session/CamFixSessionGuards.m), not here: this hook also loads into+ // SpringBoard, PosterBoard and every widget extension that links+ // AVFoundation, and an always-on scan woke each of their main threads+ // once a second to walk its whole layer tree.});}