Run a virtual iPhone on an Apple Silicon Mac.
Swift
Split the camera hooks into compilation units and fix their leaks
Lakrcd013c246f64
diff --git a/VPhoneGuestComponents/CamFix/CamFixPrivate.h b/VPhoneGuestComponents/CamFix/CamFixPrivate.hnew file mode 100644index 00000000..22b5a3c6--- /dev/null+++ b/VPhoneGuestComponents/CamFix/CamFixPrivate.h@@ -0,0 +1,74 @@+// Private interface shared by libcamfix's compilation units. Nothing here+// is dylib API: every declaration is hidden, so the export table stays the+// same as when libcamfix was one file.++#ifndef CAMFIX_PRIVATE_H+#define CAMFIX_PRIVATE_H++#import <AVFoundation/AVFoundation.h>+#import <Foundation/Foundation.h>+#import <ImageIO/ImageIO.h>+#import <IOSurface/IOSurfaceRef.h>+#include <pthread.h>+#include <stdio.h>+#include <string.h>+#include <unistd.h>+#include <objc/runtime.h>+#include <objc/message.h>+#include "VCamFrameProtocol.h"+#include "vcam_dataplane.h"++#define CFX_HIDDEN __attribute__((visibility("hidden")))++#define VCAM_UID @"vphone:vcam:0"++// MARK: - support (CamFixSupport.m)++CFX_HIDDEN void cfxlog(NSString *fmt, ...);+CFX_HIDDEN BOOL cfx_session_is_for_vcam(AVCaptureSession *session);+CFX_HIDDEN BOOL cfx_output_is_for_vcam(id self);++// MARK: - frame source (Frame/CamFixFrameSource.m)++CFX_HIDDEN CMSampleBufferRef cfx_build_cmsb(void);+CFX_HIDDEN CGImageRef cfx_build_cgimage_from_shm(void) CF_RETURNS_RETAINED;+CFX_HIDDEN NSData *cfx_jpeg_from_cgimage(CGImageRef img);+CFX_HIDDEN IOSurfaceRef cfx_build_iosurface_from_shm(uint32_t *outW, uint32_t *outH) CF_RETURNS_RETAINED;++// MARK: - device (Device/CamFixActiveFormat.m)++CFX_HIDDEN void cfx_install_setActiveFormat_hook(void);++// MARK: - photos (Photo/)++CFX_HIDDEN id cfx_build_avcapturephoto_with_request(+ IOSurfaceRef surf,+ uint32_t w,+ uint32_t h,+ id captureRequest);+CFX_HIDDEN NSData *cfx_stamp_photo(id photo);+CFX_HIDDEN void cfx_install_photo_representation_hooks(void);+CFX_HIDDEN void cfx_install_capturePhoto_hook(void);+CFX_HIDDEN void cfx_install_moment_capture_hooks(void);+CFX_HIDDEN void cfx_install_capturerequest_stubs(void);++// MARK: - preview (Preview/)++CFX_HIDDEN BOOL cfx_preview_has_layers(void);+CFX_HIDDEN void cfx_preview_start_timer(void);+CFX_HIDDEN void cfx_install_preview_layer_hooks(void);+CFX_HIDDEN void cfx_start_scan_timer(void);+CFX_HIDDEN CMSampleBufferRef cfx_tap_fetch_fresh(void) CF_RETURNS_RETAINED;+CFX_HIDDEN void cfx_tap_clear(void);+CFX_HIDDEN BOOL cfx_graph_is_delivering(void);+CFX_HIDDEN void cfx_install_remote_queue_tap(void);++// MARK: - sessions (Session/)++CFX_HIDDEN void cfx_track_vcam_session(id session);+CFX_HIDDEN void cfx_deliver_video_frames_once(void);+CFX_HIDDEN void cfx_install_session_guards(void);+CFX_HIDDEN void cfx_install_input_diagnostics(void);+CFX_HIDDEN void cfx_install_session_state_lies(void);++#endifdiff --git a/VPhoneGuestComponents/CamFix/CamFixSupport.m b/VPhoneGuestComponents/CamFix/CamFixSupport.mnew file mode 100644index 00000000..de29e676--- /dev/null+++ b/VPhoneGuestComponents/CamFix/CamFixSupport.m@@ -0,0 +1,51 @@+// Logging and the "is this bound to our virtual camera" probes every hook+// shares.++#import "CamFixPrivate.h"++// MARK: - logging++void cfxlog(NSString *fmt, ...) {+ va_list ap; va_start(ap, fmt);+ NSString *line = [[NSString alloc] initWithFormat:fmt arguments:ap];+ va_end(ap);+ FILE *fp = fopen(VPHONE_VCAM_APP_LOG_PATH, "a");+ if (fp) {+ fprintf(fp, "[camfix:%d] %s\n", getpid(), line.UTF8String ?: "?");+ fclose(fp);+ } else {+ NSLog(@"camfix: %@", line);+ }+}++// MARK: - vcam binding probes++BOOL cfx_session_is_for_vcam(AVCaptureSession *session) {+ @try {+ for (AVCaptureInput *inp in session.inputs) {+ if ([inp isKindOfClass:[AVCaptureDeviceInput class]]) {+ AVCaptureDevice *d = ((AVCaptureDeviceInput *)inp).device;+ if ([d.uniqueID isEqualToString:VCAM_UID]) return YES;+ }+ }+ } @catch (NSException *e) {}+ return NO;+}++BOOL cfx_output_is_for_vcam(id self) {+ @try {+ NSArray *conns = [self valueForKey:@"connections"];+ for (AVCaptureConnection *conn in conns) {+ for (AVCaptureInputPort *port in conn.inputPorts) {+ id input = port.input;+ if ([input isKindOfClass:[AVCaptureDeviceInput class]]) {+ AVCaptureDevice *d = ((AVCaptureDeviceInput *)input).device;+ if ([d.uniqueID isEqualToString:VCAM_UID]) return YES;+ }+ }+ }+ } @catch (NSException *e) {+ cfxlog(@"connection probe exception: %@", e);+ }+ return NO;+}diff --git a/VPhoneGuestComponents/CamFix/Device/CamFixActiveFormat.m b/VPhoneGuestComponents/CamFix/Device/CamFixActiveFormat.mnew file mode 100644index 00000000..3b85dd76--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Device/CamFixActiveFormat.m@@ -0,0 +1,49 @@+// -[AVCaptureFigVideoDevice _setActiveFormat:...]: suppress the crash when+// the device is our virtual camera and the format argument is nil.++#import "CamFixPrivate.h"++// MARK: - _setActiveFormat: nil-format guard++static IMP cfx_orig_setActiveFormat = NULL;++// Signature: void(*)(id self, SEL _cmd, AVCaptureDeviceFormat *fmt,+// BOOL resetZoomAndFrameRates, NSString *preset)+static void cfx_setActiveFormat_hook(id self, SEL _cmd, id fmt,+ BOOL resetZoomAndFrameRates,+ NSString *preset) {+ if (!fmt) {+ NSString *uid = nil;+ @try { uid = [self valueForKey:@"uniqueID"]; } @catch (NSException *e) {}+ cfxlog(@"[setActiveFormat:nil] device.uid=%@ preset=%@", uid, preset);+ if ([uid isEqualToString:VCAM_UID]) {+ // Substitute the first available format from the device's -formats list.+ NSArray *fmts = nil;+ @try { fmts = [self valueForKey:@"formats"]; } @catch (NSException *e) {}+ if (fmts.count > 0) {+ fmt = fmts.firstObject;+ cfxlog(@"[setActiveFormat:] substituted first format: %@", fmt);+ } else {+ cfxlog(@"[setActiveFormat:] device.formats is empty — letting AVF throw");+ }+ }+ }+ typedef void (*OrigFn)(id, SEL, id, BOOL, NSString *);+ ((OrigFn)cfx_orig_setActiveFormat)(self, _cmd, fmt, resetZoomAndFrameRates, preset);+}++void cfx_install_setActiveFormat_hook(void) {+ Class cls = NSClassFromString(@"AVCaptureFigVideoDevice");+ if (!cls) { cfxlog(@"AVCaptureFigVideoDevice missing"); return; }+ SEL sel = NSSelectorFromString(+ @"_setActiveFormat:resetVideoZoomFactorAndMinMaxFrameDurations:sessionPreset:");+ Method m = class_getInstanceMethod(cls, sel);+ if (!m) {+ cfxlog(@"_setActiveFormat: method not in objc table");+ return;+ }+ cfx_orig_setActiveFormat =+ method_setImplementation(m, (IMP)cfx_setActiveFormat_hook);+ cfxlog(@"installed _setActiveFormat: hook (orig=%p)",+ cfx_orig_setActiveFormat);+}diff --git a/VPhoneGuestComponents/CamFix/Frame/CamFixFrameSource.m b/VPhoneGuestComponents/CamFix/Frame/CamFixFrameSource.mnew file mode 100644index 00000000..9c645174--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Frame/CamFixFrameSource.m@@ -0,0 +1,195 @@+// The vphoned shared frame, read in this process and turned into the+// CoreMedia, CoreGraphics and IOSurface objects the hooks hand to clients.+// Every conversion goes through the shared data plane.++#import "CamFixPrivate.h"+#include <fcntl.h>+#include <sys/mman.h>+#include <sys/stat.h>++// MARK: - shm reader++// Mapped once, lazily, by whichever of the preview queue and the photo+// queues gets here first; the lock keeps a second caller from mapping the+// file again. The mapping outlives the descriptor.+static const uint8_t *cfx_shm_base = NULL;+static size_t cfx_shm_size = 0;+static pthread_mutex_t cfx_shm_lock = PTHREAD_MUTEX_INITIALIZER;++static BOOL cfx_shm_open(void) {+ pthread_mutex_lock(&cfx_shm_lock);+ BOOL mapped = cfx_shm_base != NULL;+ if (!mapped) {+ int fd = open(VPHONE_VCAM_SHM_PATH, O_RDONLY | O_CLOEXEC);+ struct stat st;+ if (fd < 0) {+ cfxlog(@"shm open failed: %s (errno=%d)", VPHONE_VCAM_SHM_PATH, errno);+ } else if (fstat(fd, &st) == 0) {+ void *p = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0);+ if (p != MAP_FAILED) {+ cfx_shm_size = (size_t)st.st_size;+ cfx_shm_base = (const uint8_t *)p;+ mapped = YES;+ cfxlog(@"shm mapped %s size=%zu", VPHONE_VCAM_SHM_PATH, cfx_shm_size);+ }+ }+ if (fd >= 0) close(fd);+ }+ pthread_mutex_unlock(&cfx_shm_lock);+ return mapped;+}++// One owner for "map the frame and check it is usable": opens the shm,+// rejects a zeroed header, and rejects pixel data that runs past the+// mapping. Planar 4:2:0 frames carry a chroma plane after luma, and the+// data plane reads it, so it counts toward the checked length. On success+// fills *frame with the header fields and a pointer into the mapping.+// Returns NO when the frame cannot be read.+static BOOL cfx_shm_frame(vcc_frame_desc_t *frame) {+ if (!cfx_shm_open()) return NO;+ const vphone_vcam_shm_header_t *hdr = (const vphone_vcam_shm_header_t *)cfx_shm_base;+ uint32_t w = hdr->width, h = hdr->height, bpr = hdr->bytes_per_row;+ uint32_t fmt = hdr->pixel_format;+ if (!w || !h || !bpr) {+ cfxlog(@"shm header zeros");+ return NO;+ }+ size_t len = (size_t)bpr * h;+ if (vcc_is_planar_yuv(fmt)) len += (size_t)(2u * (w / 2)) * (h / 2);+ if ((size_t)VPHONE_VCAM_SHM_HEADER_SIZE + len > cfx_shm_size) {+ cfxlog(@"shm: pixel range exceeds mapping");+ return NO;+ }+ memset(frame, 0, sizeof(*frame));+ frame->width = w;+ frame->height = h;+ frame->bytes_per_row = bpr;+ frame->pixel_format = fmt;+ frame->timestamp_ns = hdr->timestamp_ns;+ frame->frame_index = hdr->frame_index;+ frame->pixels = cfx_shm_base + VPHONE_VCAM_SHM_HEADER_SIZE;+ frame->pixels_length = len;+ return YES;+}++static void cfx_cg_release_data(void *info, const void *data, size_t size) {+ (void)info; (void)size;+ free((void *)data);+}++// Snapshot the shm frame and decode it to tightly packed BGRA. Every RGB+// consumer (CGImage, JPEG, IOSurface) goes through here, so the wire pixel+// format is honored instead of assumed. Returns a malloc'd buffer of+// w * 4 * h bytes; the caller frees it.+static uint8_t *cfx_bgra_from_shm(uint32_t *outW, uint32_t *outH) {+ vcc_frame_desc_t frame;+ if (!cfx_shm_frame(&frame)) return NULL;+ uint8_t *bgra = NULL;+ uint32_t bpr = 0;+ if (vcc_bgra_bytes_from_frame(&frame, &bgra, &bpr) != 0) return NULL;+ *outW = frame.width;+ *outH = frame.height;+ return bgra;+}++// MARK: - sample buffers++// Photo and video delivery are BGRA. The shared data plane builds the+// CVPixelBuffer, format description and camera attachments and converts+// the wire format when needed. Timing is stream-local (host epoch-ns PTS+// values lose precision past 2^53 in downstream float conversions) and+// advances per buffer, so builds from the preview queue and the photo+// paths serialize on one lock.+static pthread_mutex_t cfx_cmsb_lock = PTHREAD_MUTEX_INITIALIZER;+static vcc_timing_state_t cfx_cmsb_timing;+static BOOL cfx_cmsb_timing_ready = NO;++CMSampleBufferRef cfx_build_cmsb(void) {+ vcc_frame_desc_t frame;+ if (!cfx_shm_frame(&frame)) return NULL;+ uint8_t *pixels = malloc(frame.pixels_length);+ if (!pixels) return NULL;+ memcpy(pixels, frame.pixels, frame.pixels_length);+ frame.pixels = pixels;++ pthread_mutex_lock(&cfx_cmsb_lock);+ if (!cfx_cmsb_timing_ready) {+ vcc_timing_init(&cfx_cmsb_timing);+ cfx_cmsb_timing_ready = YES;+ }+ CMSampleBufferRef sb = vcc_cmsb_from_frame(&frame, VCC_FMT_BGRA, &cfx_cmsb_timing);+ pthread_mutex_unlock(&cfx_cmsb_lock);+ free(pixels);+ if (!sb) cfxlog(@"build_cmsb: shared data plane returned NULL");+ return sb;+}++// MARK: - JPEG + CGImage + IOSurface builders++NSData *cfx_jpeg_from_cgimage(CGImageRef img) {+ if (!img) return nil;+ NSMutableData *data = [NSMutableData data];+ CGImageDestinationRef dest = CGImageDestinationCreateWithData(+ (CFMutableDataRef)data,+ (CFStringRef)@"public.jpeg",+ 1,+ NULL);+ if (!dest) return nil;+ CGImageDestinationAddImage(dest, img, NULL);+ BOOL ok = CGImageDestinationFinalize(dest);+ CFRelease(dest);+ return ok ? data : nil;+}++CGImageRef cfx_build_cgimage_from_shm(void) {+ uint32_t w = 0, h = 0;+ uint8_t *bgra = cfx_bgra_from_shm(&w, &h);+ if (!bgra) return NULL;+ size_t len = (size_t)w * 4 * h;+ CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();+ CGDataProviderRef dp = CGDataProviderCreateWithData(+ NULL,+ bgra,+ len,+ cfx_cg_release_data);+ CGImageRef img = CGImageCreate(+ w,+ h,+ 8,+ 32,+ w * 4,+ cs,+ (CGBitmapInfo)(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst),+ dp,+ NULL,+ false,+ kCGRenderingIntentDefault);+ CGDataProviderRelease(dp);+ CGColorSpaceRelease(cs);+ return img;+}++IOSurfaceRef cfx_build_iosurface_from_shm(uint32_t *outW, uint32_t *outH) {+ uint32_t w = 0, h = 0;+ uint8_t *bgra = cfx_bgra_from_shm(&w, &h);+ if (!bgra) return NULL;+ size_t len = (size_t)w * 4 * h;+ NSDictionary *props = @{+ (NSString *)kIOSurfaceWidth: @(w),+ (NSString *)kIOSurfaceHeight: @(h),+ (NSString *)kIOSurfacePixelFormat: @(kCVPixelFormatType_32BGRA),+ (NSString *)kIOSurfaceBytesPerElement: @(4),+ (NSString *)kIOSurfaceBytesPerRow: @(w * 4),+ (NSString *)kIOSurfaceAllocSize: @(len),+ };+ IOSurfaceRef surf = IOSurfaceCreate((CFDictionaryRef)props);+ if (!surf) { free(bgra); return NULL; }+ IOSurfaceLock(surf, 0, NULL);+ void *base = IOSurfaceGetBaseAddress(surf);+ if (base) memcpy(base, bgra, len);+ IOSurfaceUnlock(surf, 0, NULL);+ free(bgra);+ if (outW) *outW = w;+ if (outH) *outH = h;+ return surf;+}diff --git a/VPhoneGuestComponents/CamFix/Photo/CamFixCapturePhoto.m b/VPhoneGuestComponents/CamFix/Photo/CamFixCapturePhoto.mnew file mode 100644index 00000000..44fe7ea3--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Photo/CamFixCapturePhoto.m@@ -0,0 +1,89 @@+// -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:]: when the+// photo output is bound to our virtual camera, deliver a photo built from+// the shm frame instead of calling into the daemon's still pipeline.++#import "CamFixPrivate.h"++// MARK: - capturePhoto swizzle (modern + deprecated delegate paths)++static IMP cfx_orig_capturePhoto = NULL;++static void cfx_deliver_capturePhoto(id output, id delegate) {+ // Modern path used by any AVF client: build a real AVCapturePhoto from+ // the shm frame, tag it with our JPEG + CGImage so fileDataRepresentation+ // / CGImageRepresentation return our bytes, fire the modern delegate+ // didFinishProcessingPhoto:error:.+ // Falls back to the deprecated CMSampleBuffer delegate if the client+ // opts into it (only test harnesses do; production AVF clients implement+ // the modern method).+ SEL oldSel = NSSelectorFromString(+ @"captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:");+ if ([delegate respondsToSelector:oldSel]) {+ CMSampleBufferRef sbuf = cfx_build_cmsb();+ if (!sbuf) { cfxlog(@"[capturePhoto] build_cmsb returned NULL"); return; }+ cfxlog(@"[capturePhoto] dispatching deprecated didFinishProcessingPhotoSampleBuffer:");+ ((void (*)(id, SEL, id, CMSampleBufferRef, CMSampleBufferRef, id, id, id))objc_msgSend)(+ delegate,+ oldSel,+ output,+ sbuf,+ NULL,+ (id)nil,+ (id)nil,+ (id)nil);+ CFRelease(sbuf);+ return;+ }++ SEL S5 = @selector(captureOutput:didFinishProcessingPhoto:error:);+ if (![delegate respondsToSelector:S5]) {+ cfxlog(@"[capturePhoto] delegate implements neither modern nor deprecated method");+ return;+ }++ uint32_t w = 0, h = 0;+ IOSurfaceRef surf = cfx_build_iosurface_from_shm(&w, &h);+ if (!surf) { cfxlog(@"[capturePhoto] no IOSurface"); return; }++ // captureRequest = nil (no CAMCaptureEngine outside Camera.app). The+ // AVCapturePhoto init handles nil safely — objc_msgSend on nil returns 0+ // for the resolvedSettings / unresolvedSettings calls it makes during init.+ id photo = cfx_build_avcapturephoto_with_request(surf, w, h, nil);+ CFRelease(surf);+ if (!photo) { cfxlog(@"[capturePhoto] no AVCapturePhoto"); return; }++ NSData *jpeg = cfx_stamp_photo(photo);+ cfxlog(@"[capturePhoto] firing didFinishProcessingPhoto: with vcam photo (%lu bytes jpeg)",+ (unsigned long)jpeg.length);+ ((void (*)(id, SEL, id, id, id))objc_msgSend)(delegate, S5, output, photo, nil);+}++static void cfx_capturePhoto_hook(id self, SEL _cmd, id settings, id delegate) {+ cfxlog(@"[capturePhoto] self=%p settings=%@ delegate=%@",+ self, settings, delegate);+ BOOL forVcam = cfx_output_is_for_vcam(self);+ cfxlog(@"forVcam=%d", forVcam);++ if (!forVcam) {+ typedef void (*OrigFn)(id, SEL, id, id);+ ((OrigFn)cfx_orig_capturePhoto)(self, _cmd, settings, delegate);+ return;+ }+ __strong id retainedSelf = self;+ __strong id retainedDelegate = delegate;+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{+ @autoreleasepool {+ cfx_deliver_capturePhoto(retainedSelf, retainedDelegate);+ }+ });+}++void cfx_install_capturePhoto_hook(void) {+ Class cls = NSClassFromString(@"AVCapturePhotoOutput");+ if (!cls) { cfxlog(@"AVCapturePhotoOutput missing"); return; }+ SEL sel = @selector(capturePhotoWithSettings:delegate:);+ Method m = class_getInstanceMethod(cls, sel);+ if (!m) { cfxlog(@"capturePhotoWithSettings:delegate: not found"); return; }+ cfx_orig_capturePhoto = method_setImplementation(m, (IMP)cfx_capturePhoto_hook);+ cfxlog(@"installed capturePhoto hook (orig=%p)", cfx_orig_capturePhoto);+}diff --git a/VPhoneGuestComponents/CamFix/Photo/CamFixMomentCapture.m b/VPhoneGuestComponents/CamFix/Photo/CamFixMomentCapture.mnew file mode 100644index 00000000..e35c6864--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Photo/CamFixMomentCapture.m@@ -0,0 +1,385 @@+// Camera.app's shutter path: the moment-capture trio on AVCapturePhotoOutput+// and the CAMStillImageCaptureRequest stubs its capture engine reads.++#import "CamFixPrivate.h"++// MARK: - beginMomentCapture / commitMomentCapture hooks+//+// Camera.app uses -[AVCapturePhotoOutput beginMomentCaptureWithSettings:+// delegate:] (Live Photo path) when the user taps the shutter. The original+// implementation throws NSInvalidArgumentException because our daemon's+// photo XPC isn't backed by a real still pipeline.+//+// For our virtual camera, SKIP the original entirely. Just async-deliver+// a CMSampleBuffer to the delegate. We never call orig — so AVF's state+// stays clean and subsequent shutter taps don't compound corruption.++static IMP cfx_orig_beginMomentCapture = NULL;+static IMP cfx_orig_commitMomentCapture = NULL;+static IMP cfx_orig_cancelMomentCapture = NULL;++// Build a minimal AVCaptureResolvedPhotoSettings with just _uniqueID set.+// Bypasses the 32-arg +resolvedSettingsWith… factory (which crashes on+// nil dict args). CAMCaptureEngine's didFinishProcessingPhoto: only reads+// uniqueID off the resolvedSettings to match the pending request.++static id cfx_build_minimal_resolved(int64_t uid) {+ Class outerCls = NSClassFromString(@"AVCaptureResolvedPhotoSettings");+ Class innerCls = NSClassFromString(@"AVCaptureResolvedPhotoSettingsInternal");+ if (!outerCls || !innerCls) return nil;++ id outer = class_createInstance(outerCls, 0);+ id inner = class_createInstance(innerCls, 0);+ if (!outer || !inner) return nil;++ // uniqueID:q (int64_t)+ Ivar uidIvar = class_getInstanceVariable(innerCls, "uniqueID");+ if (uidIvar) {+ *(int64_t *)((char *)(__bridge void *)inner + ivar_getOffset(uidIvar)) = uid;+ }++ // Set photo + preview dimensions so dimension accessors return our size.+ // (Zero structs are technically safe but feed our real dims for realism.)+ struct cfx_dims2 { int32_t w, h; };+ struct cfx_dims2 photoDim = {1280, 720};+ const char *dimNames[] = {"photoDimensions", "previewDimensions", NULL};+ for (int i = 0; dimNames[i]; i++) {+ Ivar iv = class_getInstanceVariable(innerCls, dimNames[i]);+ if (iv) {+ *(struct cfx_dims2 *)((char *)(__bridge void *)inner + ivar_getOffset(iv))+ = photoDim;+ }+ }++ // NSArray-typed ivars should be empty arrays, not nil, so AVF callers+ // can safely send -count etc. The strong-default setter retains for an+ // MRC ivar and follows the declared ownership for an ARC one, so the+ // owner's dealloc balances it either way — a manual retain here would+ // leak once per resolved settings when the ivar is ARC strong.+ const char *arrayNames[] = {"photoManifest", "digitalFlashUserInterfaceRGBEstimate", NULL};+ for (int i = 0; arrayNames[i]; i++) {+ Ivar iv = class_getInstanceVariable(innerCls, arrayNames[i]);+ if (iv) object_setIvarWithStrongDefault(inner, iv, @[]);+ }++ // Wire outer._internal = inner; outer's dealloc releases it.+ Ivar internalIvar = class_getInstanceVariable(outerCls, "_internal");+ if (internalIvar) object_setIvarWithStrongDefault(outer, internalIvar, inner);++ return outer;+}++// We attach the uid via objc_setAssociatedObject before AVCapturePhoto's+// init calls -resolvedSettings on the captureRequest; the stub reads it.+static const void *CFX_ASSOC_UID_KEY = &CFX_ASSOC_UID_KEY;++// MARK: - CAMStillImageCaptureRequest stubs++static id cfx_stub_resolvedSettings(id self, SEL _cmd) {+ (void)_cmd;+ NSNumber *uidObj = objc_getAssociatedObject(self, CFX_ASSOC_UID_KEY);+ int64_t uid = uidObj.longLongValue;+ id r = cfx_build_minimal_resolved(uid);+ // Log pointer only — calling -description on a half-built obj crashes.+ cfxlog(@"[stub resolvedSettings] uid=%lld -> %p", uid, r);+ return r;+}++static id cfx_stub_unresolvedSettings(id self, SEL _cmd) {+ (void)self; (void)_cmd;+ return nil;+}++static BOOL cfx_stub_lensStabSupported(id self, SEL _cmd) {+ (void)self; (void)_cmd;+ return NO;+}++void cfx_install_capturerequest_stubs(void) {+ Class cls = NSClassFromString(@"CAMStillImageCaptureRequest");+ if (!cls) { cfxlog(@"CAMStillImageCaptureRequest missing"); return; }+ if (class_addMethod(+ cls,+ NSSelectorFromString(@"resolvedSettings"),+ (IMP)cfx_stub_resolvedSettings,+ "@@:")) {+ cfxlog(@"stubbed resolvedSettings on CAMStillImageCaptureRequest");+ }+ if (class_addMethod(+ cls,+ NSSelectorFromString(@"unresolvedSettings"),+ (IMP)cfx_stub_unresolvedSettings,+ "@@:")) {+ cfxlog(@"stubbed unresolvedSettings on CAMStillImageCaptureRequest");+ }+ if (class_addMethod(+ cls,+ NSSelectorFromString(@"lensStabilizationSupported"),+ (IMP)cfx_stub_lensStabSupported,+ "B@:")) {+ cfxlog(@"stubbed lensStabilizationSupported on CAMStillImageCaptureRequest");+ }+}++// MARK: - drive the capture state machine++static void cfx_drive_capture(id output, id delegate, id settings) {+ // Extract uniqueID from AVMomentCaptureSettings.+ int64_t uid = 0;+ @try {+ NSNumber *n = [settings valueForKey:@"uniqueID"];+ uid = n.longLongValue;+ } @catch (NSException *e) {}+ cfxlog(@"[drive] uid=%lld settings=%@", uid, [settings class]);++ // Look up the real captureRequest from CAMCaptureEngine's internal+ // registry FIRST. When the user tapped the shutter, CAMCaptureEngine+ // registered a CAMCaptureRequestInfo keyed by uid (the+ // _resultsQueueRegisteredStillImageRequests ivar). Its `request` property+ // is the AVCaptureRequest we need to pass as the `captureRequest:` arg+ // to the AVCapturePhoto init — without it, CAMCaptureEngine's+ // didFinishProcessing handler can't match the photo to a pending+ // request and drops it.+ id captureRequest = nil;+ @try {+ id reqDict = [delegate valueForKey:@"_resultsQueueRegisteredStillImageRequests"];+ if ([reqDict isKindOfClass:[NSDictionary class]]) {+ id info = ((NSDictionary *)reqDict)[@(uid)];+ if (info) {+ id stillReq = [info valueForKey:@"request"];+ cfxlog(@"[drive] CAMStillImageCaptureRequest=%p for uid=%lld",+ stillReq, uid);+ // Tag the captureRequest with the uid so the resolvedSettings stub+ // (which has no parameter context) can recover it.+ objc_setAssociatedObject(stillReq, CFX_ASSOC_UID_KEY, @(uid),+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ captureRequest = stillReq;+ } else {+ cfxlog(@"[drive] no CAMCaptureRequestInfo for uid=%lld; keys=%@",+ uid, ((NSDictionary *)reqDict).allKeys);+ }+ }+ } @catch (NSException *e) {+ cfxlog(@"[drive] registry lookup threw: %@", e);+ }++ // Build IOSurface from shm.+ uint32_t w = 0, h = 0;+ IOSurfaceRef surf = cfx_build_iosurface_from_shm(&w, &h);+ if (!surf || !w || !h) {+ cfxlog(@"[drive] no IOSurface — falling back to error finish");+ NSError *err = [NSError errorWithDomain:AVFoundationErrorDomain+ code:-11800+ userInfo:@{NSLocalizedDescriptionKey:@"Unable to take the photo. Try again."}];+ SEL finishSel = @selector(captureOutput:didFinishCaptureForResolvedSettings:error:);+ if ([delegate respondsToSelector:finishSel]) {+ ((void (*)(id, SEL, id, id, id))objc_msgSend)(delegate, finishSel, output, nil, err);+ }+ if (surf) CFRelease(surf);+ return;+ }+ cfxlog(@"[drive] IOSurface %ux%u built", w, h);++ // Build AVCapturePhoto WITH the real captureRequest.+ id photo = cfx_build_avcapturephoto_with_request(surf, w, h, captureRequest);+ CFRelease(surf); // photo retains it+ if (!photo) {+ cfxlog(@"[drive] no AVCapturePhoto — abort");+ return;+ }++ // Stamp it with our JPEG so fileDataRepresentation returns our bytes.+ NSData *jpeg = cfx_stamp_photo(photo);+ cfxlog(@"[drive] photo stamped jpeg=%lu bytes", (unsigned long)jpeg.length);++ // Fire the FULL standard delegate sequence. CAMCaptureEngine tracks+ // a _receivedCallbacks set per uid; until ALL expected callbacks+ // arrive, the request stays in+ // _resultsQueueRegisteredStillImageRequests and the engine refuses+ // to start new captures (manifests as "shutter stops working after+ // N taps"). We now have a real resolvedSettings, so we can fire the+ // will/did pre-callbacks too.+ id resolvedForFinish = nil;+ @try {+ resolvedForFinish = [photo valueForKey:@"resolvedSettings"];+ } @catch (NSException *e) {+ cfxlog(@"[drive] photo.resolvedSettings threw: %@", e);+ }+ SEL S1 = @selector(captureOutput:willBeginCaptureBeforeResolvingSettingsForUniqueID:);+ SEL S2 = @selector(captureOutput:willBeginCaptureForResolvedSettings:);+ SEL S3 = @selector(captureOutput:willCapturePhotoForResolvedSettings:);+ SEL S4 = @selector(captureOutput:didCapturePhotoForResolvedSettings:);+ SEL S5 = @selector(captureOutput:didFinishProcessingPhoto:error:);+ SEL S6 = @selector(captureOutput:didFinishCaptureForResolvedSettings:error:);+ SEL Sfinish = NSSelectorFromString(@"_didFinishStillImageCaptureForUniqueID:error:");+ if ([delegate respondsToSelector:S1]) {+ cfxlog(@"[drive] -> willBeginCaptureBefore… uid=%lld", uid);+ ((void (*)(id, SEL, id, int64_t))objc_msgSend)(delegate, S1, output, uid);+ }+ if (resolvedForFinish && [delegate respondsToSelector:S2]) {+ cfxlog(@"[drive] -> willBeginCaptureForResolvedSettings:");+ ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, S2, output, resolvedForFinish);+ }+ if (resolvedForFinish && [delegate respondsToSelector:S3]) {+ cfxlog(@"[drive] -> willCapturePhotoForResolvedSettings:");+ ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, S3, output, resolvedForFinish);+ }+ if (resolvedForFinish && [delegate respondsToSelector:S4]) {+ cfxlog(@"[drive] -> didCapturePhotoForResolvedSettings:");+ ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, S4, output, resolvedForFinish);+ }+ if ([delegate respondsToSelector:S5]) {+ cfxlog(@"[drive] -> didFinishProcessingPhoto:error:");+ ((void (*)(id, SEL, id, id, id))objc_msgSend)(delegate, S5, output, photo, nil);+ }+ if ([delegate respondsToSelector:S6]) {+ cfxlog(@"[drive] -> didFinishCapture resolved=%p", resolvedForFinish);+ ((void (*)(id, SEL, id, id, id))objc_msgSend)(+ delegate, S6, output, resolvedForFinish, nil);+ }+ if ([delegate respondsToSelector:Sfinish]) {+ cfxlog(@"[drive] -> _didFinishStillImageCaptureForUniqueID:%lld", uid);+ ((void (*)(id, SEL, int64_t, id))objc_msgSend)(delegate, Sfinish, uid, nil);+ }++ // Signal that the output is ready for the NEXT capture request. AVF's+ // photo output is a 2-deep pipeline — Camera.app's shutter stays+ // disabled until this fires for each in-flight capture.+ SEL Sready = NSSelectorFromString(@"captureOutput:readyForResponsiveRequestAfterResolvedSettings:");+ if (resolvedForFinish && [delegate respondsToSelector:Sready]) {+ cfxlog(@"[drive] -> readyForResponsiveRequestAfterResolvedSettings:");+ ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, Sready, output, resolvedForFinish);+ }++ cfxlog(@"[drive] sequence complete");+}++// We stash (delegate, settings) on the AVCapturePhotoOutput at begin time,+// keyed by the settings' uniqueID, so the commit hook can drive the capture+// once CAMCaptureEngine has registered the request in its internal dict.+// The photo output is a two-deep pipeline, so a second begin can arrive+// before the first commit; keying keeps each commit on its own settings.+// Commit and cancel take the entry out, so the output does not keep its+// delegate (which owns the output) alive after the capture.+static const void *CFX_ASSOC_PENDING_KEY = &CFX_ASSOC_PENDING_KEY;++static NSNumber *cfx_settings_uid(id settings) {+ @try {+ return @([[settings valueForKey:@"uniqueID"] longLongValue]);+ } @catch (NSException *e) {+ return @0;+ }+}++static void cfx_pending_put(id output, id settings, id delegate) {+ @synchronized(output) {+ NSMutableDictionary *pending = objc_getAssociatedObject(output, CFX_ASSOC_PENDING_KEY);+ if (!pending) {+ pending = [NSMutableDictionary dictionary];+ objc_setAssociatedObject(output, CFX_ASSOC_PENDING_KEY, pending,+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ }+ pending[cfx_settings_uid(settings)] = @[ delegate, settings ?: [NSNull null] ];+ }+}++// Takes the entry for uid out. A commit falls back to the only pending+// entry when the uid does not match, which is what the output-wide stash+// did before; a cancel removes an exact match only.+static NSArray *cfx_pending_take(id output, intptr_t uid, BOOL fallBackToOnly) {+ @synchronized(output) {+ NSMutableDictionary *pending = objc_getAssociatedObject(output, CFX_ASSOC_PENDING_KEY);+ NSNumber *key = @((long long)uid);+ NSArray *entry = pending[key];+ if (!entry && fallBackToOnly && pending.count == 1) {+ key = pending.allKeys.firstObject;+ entry = pending[key];+ }+ if (entry) [pending removeObjectForKey:key];+ return entry;+ }+}++static void cfx_beginMomentCapture_hook(id self, SEL _cmd, id settings, id delegate) {+ BOOL forVcam = cfx_output_is_for_vcam(self);+ cfxlog(@"[beginMomentCapture] forVcam=%d delegate=%@", forVcam, delegate);+ if (!forVcam) {+ typedef void (*OrigFn)(id, SEL, id, id);+ ((OrigFn)cfx_orig_beginMomentCapture)(self, _cmd, settings, delegate);+ return;+ }+ // SKIP orig — it throws for our session. Just stash the delegate ++ // settings; commit will drive the photo delivery once CAMCaptureEngine+ // has registered the captureRequest internally.+ if (delegate) cfx_pending_put(self, settings, delegate);+}++// NOTE: uniqueID is a pointer-sized integer (not an NSUUID *). Declaring+// it as `id` would cause ARC to retain it on entry, dereferencing the+// integer-as-isa and segfaulting. Use intptr_t to skip the retain.++static void cfx_commitMomentCapture_hook(id self, SEL _cmd, intptr_t uniqueID) {+ BOOL forVcam = cfx_output_is_for_vcam(self);+ cfxlog(@"[commitMomentCapture] forVcam=%d uniqueID=%ld", forVcam, (long)uniqueID);+ if (!forVcam) {+ typedef void (*OrigFn)(id, SEL, intptr_t);+ ((OrigFn)cfx_orig_commitMomentCapture)(self, _cmd, uniqueID);+ return;+ }+ // SKIP orig (would throw) and drive the synthesized photo delivery+ // here, where CAMCaptureEngine has already registered its+ // CAMCaptureRequestInfo for this uid.+ NSArray *entry = cfx_pending_take(self, uniqueID, YES);+ if (!entry) {+ cfxlog(@"[commit] no stashed delegate — was begin called?");+ return;+ }+ __strong id retainedSelf = self;+ __strong id retainedDelegate = entry[0];+ __strong id retainedSettings = entry[1] == [NSNull null] ? nil : entry[1];+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{+ @autoreleasepool {+ cfx_drive_capture(retainedSelf, retainedDelegate, retainedSettings);+ }+ });+}++static void cfx_cancelMomentCapture_hook(id self, SEL _cmd, intptr_t uniqueID) {+ BOOL forVcam = cfx_output_is_for_vcam(self);+ cfxlog(@"[cancelMomentCapture] forVcam=%d uniqueID=0x%lx", forVcam, (long)uniqueID);+ if (!forVcam) {+ typedef void (*OrigFn)(id, SEL, intptr_t);+ ((OrigFn)cfx_orig_cancelMomentCapture)(self, _cmd, uniqueID);+ return;+ }+ // Orig throws (the begin was a no-op so there is no live moment to+ // cancel). CAMCaptureEngine drives this when it decides the capture is+ // stale; just drop what begin stashed.+ (void)cfx_pending_take(self, uniqueID, NO);+}++void cfx_install_moment_capture_hooks(void) {+ Class cls = NSClassFromString(@"AVCapturePhotoOutput");+ if (!cls) return;+ SEL b = @selector(beginMomentCaptureWithSettings:delegate:);+ SEL c = NSSelectorFromString(@"commitMomentCaptureToPhotoWithUniqueID:");+ SEL x = NSSelectorFromString(@"cancelMomentCaptureWithUniqueID:");+ Method mb = class_getInstanceMethod(cls, b);+ Method mc = class_getInstanceMethod(cls, c);+ Method mx = class_getInstanceMethod(cls, x);+ if (mb) {+ cfx_orig_beginMomentCapture =+ method_setImplementation(mb, (IMP)cfx_beginMomentCapture_hook);+ cfxlog(@"installed beginMomentCapture hook");+ }+ if (mc) {+ cfx_orig_commitMomentCapture =+ method_setImplementation(mc, (IMP)cfx_commitMomentCapture_hook);+ cfxlog(@"installed commitMomentCapture hook");+ }+ if (mx) {+ cfx_orig_cancelMomentCapture =+ method_setImplementation(mx, (IMP)cfx_cancelMomentCapture_hook);+ cfxlog(@"installed cancelMomentCapture hook");+ }+}diff --git a/VPhoneGuestComponents/CamFix/Photo/CamFixPhotoSynthesis.m b/VPhoneGuestComponents/CamFix/Photo/CamFixPhotoSynthesis.mnew file mode 100644index 00000000..45943785--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Photo/CamFixPhotoSynthesis.m@@ -0,0 +1,254 @@+// AVCapturePhoto objects built from the shm frame, and the+// fileDataRepresentation / CGImageRepresentation hooks that hand back the+// bytes stamped onto them.++#import "CamFixPrivate.h"++// Associated-object keys the representation hooks use to recognize "our"+// photos without a global dict.+static const void *CFX_ASSOC_JPEG_KEY = &CFX_ASSOC_JPEG_KEY;+static const void *CFX_ASSOC_CGIMG_KEY = &CFX_ASSOC_CGIMG_KEY;++// Tag a synthesized photo with our JPEG + CGImage so fileDataRepresentation+// / CGImageRepresentation return our bytes. Returns the JPEG so the caller+// can log its size.+NSData *cfx_stamp_photo(id photo) {+ // One frame for both representations, so they show the same picture.+ CGImageRef cgImg = cfx_build_cgimage_from_shm();+ NSData *jpeg = cfx_jpeg_from_cgimage(cgImg);+ if (jpeg) objc_setAssociatedObject(+ photo,+ CFX_ASSOC_JPEG_KEY,+ jpeg,+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ if (cgImg) {+ objc_setAssociatedObject(+ photo,+ CFX_ASSOC_CGIMG_KEY,+ (__bridge id)cgImg,+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ CGImageRelease(cgImg);+ }+ return jpeg;+}++// MARK: - Version-agnostic invocation helpers+//+// AVCapturePhoto's designated init and AVCaptureResolvedPhotoSettings'+// factory each take 25+ arguments. The selectors are gigantic strings and+// Apple has been known to add/remove arguments between iOS revisions+// (which silently moves every later arg's positional index). To avoid+// rewriting every libcamfix release against a fixed signature, the+// builders below look up the selector by PREFIX at runtime and fill+// arguments by LABEL — the actual selector component names like+// "photoSurface" or "uniqueID" — so a future arg insertion just gets+// nil/zero in that slot without breaking the rest.++// Walk class_copyMethodList for `cls` (or its metaclass when looking for+// class methods) and return the selector with the most colons that starts+// with `prefix`. Returns NULL if nothing matches.+static SEL cfx_find_selector_by_prefix(+ Class cls,+ NSString *prefix,+ BOOL classMethod) {+ if (!cls || !prefix.length) return NULL;+ Class c = classMethod ? object_getClass(cls) : cls;+ unsigned int n = 0;+ Method *methods = class_copyMethodList(c, &n);+ if (!methods) return NULL;+ SEL best = NULL;+ NSUInteger bestArgs = 0;+ for (unsigned int i = 0; i < n; i++) {+ SEL s = method_getName(methods[i]);+ NSString *name = NSStringFromSelector(s);+ if (![name hasPrefix:prefix]) continue;+ NSUInteger colons = 0;+ for (NSUInteger j = 0; j < name.length; j++) {+ if ([name characterAtIndex:j] == ':') colons++;+ }+ if (colons > bestArgs) {+ bestArgs = colons;+ best = s;+ }+ }+ free(methods);+ return best;+}++// Normalize the first selector component to a canonical label. Apple+// init/factory selectors start with "initWithX:" / "resolvedSettingsWithX:";+// the helper-side label dictionary keys are just "X" (camelCase). Strips+// the prefix and lowercases the first character of the remainder.+static NSString *cfx_normalize_first_label(NSString *label) {+ for (NSString *prefix in @[@"initWith", @"resolvedSettingsWith"]) {+ if ([label hasPrefix:prefix] && label.length > prefix.length) {+ NSString *rest = [label substringFromIndex:prefix.length];+ return [[[rest substringToIndex:1] lowercaseString]+ stringByAppendingString:[rest substringFromIndex:1]];+ }+ }+ return label;+}++// Resolver block: called once per method argument with the canonical label+// (e.g. "photoSurface") and the runtime type encoding. The block decides+// whether and what to write into `outBuf` (already zeroed). If the block+// doesn't recognize the label, leave outBuf alone — nil for objects,+// zeroed bytes for structs/primitives.+typedef void (^cfx_arg_resolver_t)(+ NSString *label,+ const char *typeEnc,+ void *outBuf);++// Build + invoke `[target selector]` with arguments filled by `resolver`.+// Works whether `target` is an alloc'd instance (for inits) or a Class+// (for class-method factories). Returns the result if it's an object+// return type, nil otherwise.+static id cfx_invoke_with_labeled_args(+ id target,+ SEL selector,+ cfx_arg_resolver_t resolver) {+ NSMethodSignature *sig = nil;+ @try { sig = [target methodSignatureForSelector:selector]; }+ @catch (NSException *e) { return nil; }+ if (!sig) return nil;++ NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];+ [inv setTarget:target];+ [inv setSelector:selector];++ NSArray<NSString *> *parts =+ [NSStringFromSelector(selector) componentsSeparatedByString:@":"];+ NSUInteger argCount = sig.numberOfArguments - 2; // -2 for self + _cmd+ for (NSUInteger i = 0; i < argCount; i++) {+ NSString *raw = i < parts.count ? parts[i] : @"";+ NSString *label = (i == 0) ? cfx_normalize_first_label(raw) : raw;++ const char *typeEnc = [sig getArgumentTypeAtIndex:i + 2];+ NSUInteger argSize = 0, argAlign = 0;+ NSGetSizeAndAlignment(typeEnc, &argSize, &argAlign);+ if (argSize == 0) continue;++ // alloca on arm64 returns a 16-byte-aligned buffer; argSize <= 64 in+ // practice for everything Apple uses here (CMTimeRange is 48 bytes).+ void *buf = alloca(argSize);+ memset(buf, 0, argSize);+ if (resolver) resolver(label, typeEnc, buf);+ [inv setArgument:buf atIndex:i + 2];+ }++ @try {+ [inv invoke];+ } @catch (NSException *e) {+ cfxlog(@"invoke %@ threw: %@", NSStringFromSelector(selector), e);+ return nil;+ }++ if (sig.methodReturnLength > 0 &&+ sig.methodReturnType[0] == '@') {+ __unsafe_unretained id result = nil;+ [inv getReturnValue:&result];+ return result;+ }+ return nil;+}++// MARK: - AVCapturePhoto / Resolved settings synthesis++id cfx_build_avcapturephoto_with_request(IOSurfaceRef surf,+ uint32_t w, uint32_t h,+ id captureRequest) {+ Class cls = NSClassFromString(@"AVCapturePhoto");+ if (!cls) return nil;+ id alloc_obj = [cls alloc];+ if (!alloc_obj) return nil;++ // Discover the designated init at runtime. Apple may add/remove args+ // between iOS releases; we anchor on the leading components and accept+ // whatever the longest matching selector is.+ SEL initSel = cfx_find_selector_by_prefix(cls, @"initWithTimestamp:",+ /*classMethod*/NO);+ if (!initSel) { cfxlog(@"photo: no init selector found"); return nil; }++ CMTime ts = CMClockGetTime(CMClockGetHostTimeClock());+ CGSize sz = CGSizeMake((CGFloat)w, (CGFloat)h);+ NSString *fileType = @"public.jpeg";+ NSDictionary *meta = @{};+ NSString *deviceType = @"AVCaptureDeviceTypeBuiltInWideAngleCamera";++ id result = cfx_invoke_with_labeled_args(+ alloc_obj, initSel,+ ^(NSString *label, const char *typeEnc, void *out) {+ if ([label isEqualToString:@"timestamp"]) {+ *(CMTime *)out = ts;+ } else if ([label isEqualToString:@"photoSurface"]) {+ *(IOSurfaceRef *)out = surf;+ } else if ([label isEqualToString:@"photoSurfaceSize"]) {+ *(CGSize *)out = sz;+ } else if ([label isEqualToString:@"processedFileType"]) {+ *(__unsafe_unretained id *)out = fileType;+ } else if ([label isEqualToString:@"metadata"]) {+ *(__unsafe_unretained id *)out = meta;+ } else if ([label isEqualToString:@"captureRequest"]) {+ *(__unsafe_unretained id *)out = captureRequest;+ } else if ([label isEqualToString:@"sequenceCount"]+ || [label isEqualToString:@"photoCount"]) {+ *(NSInteger *)out = 1;+ } else if ([label isEqualToString:@"sourceDeviceType"]) {+ *(__unsafe_unretained id *)out = deviceType;+ }+ // Every other arg (preview/thumbnail/depth/portrait/hair/skin/+ // teeth/glasses/constantColor surfaces + their metadata dicts,+ // bracketSettings, expectedPhotoProcessingFlags, etc.) stays+ // zero/nil — works for both Camera.app (with non-nil+ // captureRequest) and standalone AVF clients (nil).+ });+ if (result) {+ cfxlog(@"AVCapturePhoto built: %p (captureRequest=%p, sel=%@)",+ result,+ captureRequest,+ NSStringFromSelector(initSel));+ }+ return result;+}++// MARK: - fileDataRepresentation / CGImageRepresentation hooks++static IMP cfx_orig_fileDataRep = NULL;+static IMP cfx_orig_cgImageRep = NULL;++static NSData *cfx_fileDataRep_hook(id self, SEL _cmd) {+ NSData *ours = objc_getAssociatedObject(self, CFX_ASSOC_JPEG_KEY);+ if (ours) {+ cfxlog(@"[fileDataRep] returning vcam jpeg (%lu bytes)", (unsigned long)ours.length);+ return ours;+ }+ typedef NSData *(*OrigFn)(id, SEL);+ return ((OrigFn)cfx_orig_fileDataRep)(self, _cmd);+}++static CGImageRef cfx_cgImageRep_hook(id self, SEL _cmd) {+ id wrap = objc_getAssociatedObject(self, CFX_ASSOC_CGIMG_KEY);+ if (wrap) {+ CGImageRef img = (__bridge CGImageRef)wrap;+ cfxlog(@"[cgImageRep] returning vcam CGImage");+ return img;+ }+ typedef CGImageRef (*OrigFn)(id, SEL);+ return ((OrigFn)cfx_orig_cgImageRep)(self, _cmd);+}++void cfx_install_photo_representation_hooks(void) {+ Class cls = NSClassFromString(@"AVCapturePhoto");+ if (!cls) return;+ Method m1 = class_getInstanceMethod(cls, @selector(fileDataRepresentation));+ Method m2 = class_getInstanceMethod(cls, @selector(CGImageRepresentation));+ if (m1) {+ cfx_orig_fileDataRep = method_setImplementation(m1, (IMP)cfx_fileDataRep_hook);+ cfxlog(@"installed fileDataRepresentation hook");+ }+ if (m2) {+ cfx_orig_cgImageRep = method_setImplementation(m2, (IMP)cfx_cgImageRep_hook);+ cfxlog(@"installed CGImageRepresentation hook");+ }+}diff --git a/VPhoneGuestComponents/CamFix/Preview/CamFixGraphTap.m b/VPhoneGuestComponents/CamFix/Preview/CamFixGraphTap.mnew file mode 100644index 00000000..dd987fce--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Preview/CamFixGraphTap.m@@ -0,0 +1,83 @@+// The client-side tail of the capture graph, tapped so the preview can show+// the sample the graph delivered and video delivery knows when to stand+// down.++#import "CamFixPrivate.h"++// MARK: - preview fed from the client's capture graph+//+// A real camera has one path: capture graph -> sample buffer -> preview+// surface. While a vcam preview layer is live, tap the client-side sink node+// that receives the capture graph's sample buffers and feed the preview from+// that sample — the same frame, timestamp, pixel format and camera metadata+// every other consumer sees. The shm reader is the bootstrap fallback for+// the window before the daemon builds the graph (preview-only sessions).++static pthread_mutex_t cfx_tap_lock = PTHREAD_MUTEX_INITIALIZER;+static CMSampleBufferRef cfx_tap_latest = NULL; // retained, guarded+static CFAbsoluteTime cfx_tap_latest_at = 0; // guarded+#define CFX_TAP_STALE_SECONDS 0.5++static void cfx_tap_store(CMSampleBufferRef sb) {+ CFRetain(sb);+ pthread_mutex_lock(&cfx_tap_lock);+ CMSampleBufferRef old = cfx_tap_latest;+ cfx_tap_latest = sb;+ cfx_tap_latest_at = CFAbsoluteTimeGetCurrent();+ pthread_mutex_unlock(&cfx_tap_lock);+ if (old) CFRelease(old);+}++// Returns a retained latest sample, or NULL if none is fresh.+CMSampleBufferRef cfx_tap_fetch_fresh(void) {+ CMSampleBufferRef out = NULL;+ pthread_mutex_lock(&cfx_tap_lock);+ if (cfx_tap_latest &&+ CFAbsoluteTimeGetCurrent() - cfx_tap_latest_at < CFX_TAP_STALE_SECONDS) {+ out = (CMSampleBufferRef)CFRetain(cfx_tap_latest);+ }+ pthread_mutex_unlock(&cfx_tap_lock);+ return out;+}++void cfx_tap_clear(void) {+ pthread_mutex_lock(&cfx_tap_lock);+ CMSampleBufferRef old = cfx_tap_latest;+ cfx_tap_latest = NULL;+ pthread_mutex_unlock(&cfx_tap_lock);+ if (old) CFRelease(old);+}++// When the capture graph last delivered a sample to this process. Video+// delivery is the fallback for sessions whose graph never runs; once the+// graph delivers, the data outputs are fed natively and the fallback must+// stop, or delegates would see two interleaved streams.+static _Atomic double cfx_graph_last_at = 0;++BOOL cfx_graph_is_delivering(void) {+ return CFAbsoluteTimeGetCurrent() - cfx_graph_last_at < CFX_TAP_STALE_SECONDS;+}++static IMP cfx_orig_rqsn_render = NULL;++static void cfx_rqsn_render_tap(id self, SEL _cmd, CMSampleBufferRef sb, id input) {+ if (sb) cfx_graph_last_at = CFAbsoluteTimeGetCurrent();+ // Hold buffers only while a vcam preview is being pumped — never retain+ // graph buffers in clients without preview layers.+ if (sb && cfx_preview_has_layers()) cfx_tap_store(sb);+ typedef void (*Fn)(id, SEL, CMSampleBufferRef, id);+ ((Fn)cfx_orig_rqsn_render)(self, _cmd, sb, input);+}++void cfx_install_remote_queue_tap(void) {+ // Client-side tail of the capture graph. In this VM the synthetic source+ // is the only camera, so samples arriving here while a vcam preview is+ // tracked are ours by construction.+ Class cls = NSClassFromString(@"BWRemoteQueueSinkNode");+ if (!cls) { cfxlog(@"[tap] BWRemoteQueueSinkNode missing"); return; }+ SEL sel = @selector(renderSampleBuffer:forInput:);+ Method m = class_getInstanceMethod(cls, sel);+ if (!m) { cfxlog(@"[tap] renderSampleBuffer:forInput: missing"); return; }+ cfx_orig_rqsn_render = method_setImplementation(m, (IMP)cfx_rqsn_render_tap);+ cfxlog(@"[tap] installed BWRemoteQueueSinkNode tap (orig=%p)", cfx_orig_rqsn_render);+}diff --git a/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.m b/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.mnew file mode 100644index 00000000..a0fcde38--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Preview/CamFixPreviewLayers.m@@ -0,0 +1,255 @@+// AVCaptureVideoPreviewLayer.contents pump and the layer discovery that+// feeds it.++#import "CamFixPrivate.h"++// MARK: - AVCaptureVideoPreviewLayer.contents pump+//+// AVCaptureVideoPreviewLayer is a CALayer subclass that normally displays+// the camera's preview via internal IOSurface plumbing fed by the daemon.+// For our virtual camera, no daemon-side preview pipeline is built, so the+// layer stays black.+//+// Workaround: capture each AVCaptureVideoPreviewLayer instance bound to+// our virtual camera, and pump CGImage frames into its `contents` property+// at 30 Hz from a background timer. CALayer rendering picks the image up+// without needing the underlying AVF preview infrastructure.++// Weak. Written from the layer hooks (any thread) and the main-queue scan,+// read from the preview queue and the graph's render thread, so every+// access holds the lock.+static NSHashTable *cfx_preview_layers = nil;+static pthread_mutex_t cfx_preview_layers_lock = PTHREAD_MUTEX_INITIALIZER;+static dispatch_source_t cfx_preview_timer = NULL;+static IMP cfx_orig_pv_initWithSession = NULL;+static IMP cfx_orig_pv_initWithSessionMakeConnection = NULL;++// Adds the layers and returns how many were new.+static NSUInteger cfx_preview_add_layers(NSArray *layers) {+ pthread_mutex_lock(&cfx_preview_layers_lock);+ if (!cfx_preview_layers) cfx_preview_layers = [NSHashTable weakObjectsHashTable];+ NSUInteger before = cfx_preview_layers.count;+ for (id layer in layers) [cfx_preview_layers addObject:layer];+ NSUInteger added = cfx_preview_layers.count - before;+ pthread_mutex_unlock(&cfx_preview_layers_lock);+ return added;+}++static NSArray *cfx_preview_layers_snapshot(void) {+ pthread_mutex_lock(&cfx_preview_layers_lock);+ NSArray *layers = cfx_preview_layers.allObjects ?: @[];+ pthread_mutex_unlock(&cfx_preview_layers_lock);+ return layers;+}++BOOL cfx_preview_has_layers(void) {+ pthread_mutex_lock(&cfx_preview_layers_lock);+ BOOL any = cfx_preview_layers.count > 0;+ pthread_mutex_unlock(&cfx_preview_layers_lock);+ return any;+}++static void cfx_pump_preview_once(void) {+ NSArray *layers = cfx_preview_layers_snapshot();+ if (layers.count == 0) {+ // Nothing to show: drop the graph sample the tap may still hold.+ cfx_tap_clear();+ return;+ }+ CGImageRef img = NULL;+ // Preferred: the sample the capture graph just delivered.+ CMSampleBufferRef sb = cfx_tap_fetch_fresh();+ if (sb) {+ CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sb);+ if (pb) img = vcc_cgimage_from_pixel_buffer(pb);+ CFRelease(sb);+ }+ // Bootstrap fallback: no graph yet, straight from the shm frame.+ if (!img) img = cfx_build_cgimage_from_shm();+ if (!img) return;+ dispatch_async(dispatch_get_main_queue(), ^{+ for (CALayer *layer in layers) {+ layer.contents = (__bridge id)img;+ layer.contentsGravity = kCAGravityResizeAspectFill;+ }+ CGImageRelease(img);+ });+}++// Callers run on any thread; the timer is created once.+void cfx_preview_start_timer(void) {+ static dispatch_once_t once;+ dispatch_once(&once, ^{+ dispatch_queue_t q = dispatch_queue_create("com.vphone.camfix.preview", DISPATCH_QUEUE_SERIAL);+ cfx_preview_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);+ dispatch_source_set_timer(+ cfx_preview_timer,+ dispatch_time(DISPATCH_TIME_NOW, 0),+ 33333333ull,+ 2000000ull);+ dispatch_source_set_event_handler(cfx_preview_timer, ^{+ @autoreleasepool {+ cfx_pump_preview_once();+ cfx_deliver_video_frames_once();+ }+ });+ dispatch_resume(cfx_preview_timer);+ cfxlog(@"preview pump armed (30 Hz)");+ });+}++static void cfx_adopt_preview_layer(id layer) {+ cfx_preview_add_layers(@[ layer ]);+ cfx_preview_start_timer();+}++// MARK: - preview layer hooks++__attribute__((ns_returns_retained))+static id cfx_pv_initWithSession_hook(id self, SEL _cmd, AVCaptureSession *session) {+ typedef id (*Fn)(id, SEL, AVCaptureSession *);+ id ret = ((Fn)cfx_orig_pv_initWithSession)(self, _cmd, session);+ BOOL forVcam = (session != nil) && cfx_session_is_for_vcam(session);+ cfxlog(@"[PVLayer initWithSession:%p] ret=%p forVcam=%d cls=%@",+ session,+ ret,+ forVcam,+ NSStringFromClass([ret class]));+ if (ret && forVcam) cfx_adopt_preview_layer(ret);+ return ret;+}++__attribute__((ns_returns_retained))+static id cfx_pv_initWithSessionMakeConnection_hook(+ id self,+ SEL _cmd,+ AVCaptureSession *session,+ BOOL makeConnection) {+ typedef id (*Fn)(id, SEL, AVCaptureSession *, BOOL);+ id ret = ((Fn)cfx_orig_pv_initWithSessionMakeConnection)(+ self,+ _cmd,+ session,+ makeConnection);+ BOOL forVcam = (session != nil) && cfx_session_is_for_vcam(session);+ cfxlog(@"[PVLayer _initWithSession:%p makeConnection:%d] ret=%p forVcam=%d cls=%@",+ session,+ makeConnection,+ ret,+ forVcam,+ NSStringFromClass([ret class]));+ if (ret && forVcam) cfx_adopt_preview_layer(ret);+ return ret;+}++static IMP cfx_orig_pv_setSession = NULL;+static void cfx_pv_setSession_hook(id self, SEL _cmd, AVCaptureSession *session) {+ typedef void (*Fn)(id, SEL, AVCaptureSession *);+ ((Fn)cfx_orig_pv_setSession)(self, _cmd, session);+ BOOL forVcam = (session != nil) && cfx_session_is_for_vcam(session);+ cfxlog(@"[PVLayer setSession:%p] self=%p forVcam=%d cls=%@",+ session,+ self,+ forVcam,+ NSStringFromClass([self class]));+ if (session && forVcam) cfx_adopt_preview_layer(self);+}++void cfx_install_preview_layer_hooks(void) {+ Class cls = NSClassFromString(@"AVCaptureVideoPreviewLayer");+ if (!cls) { cfxlog(@"AVCaptureVideoPreviewLayer missing"); return; }+ SEL s1 = @selector(initWithSession:);+ SEL s2 = NSSelectorFromString(@"_initWithSession:makeConnection:");+ SEL s3 = @selector(setSession:);+ Method m1 = class_getInstanceMethod(cls, s1);+ Method m2 = class_getInstanceMethod(cls, s2);+ Method m3 = class_getInstanceMethod(cls, s3);+ if (m1) {+ cfx_orig_pv_initWithSession =+ method_setImplementation(m1, (IMP)cfx_pv_initWithSession_hook);+ }+ if (m2) {+ cfx_orig_pv_initWithSessionMakeConnection =+ method_setImplementation(m2, (IMP)cfx_pv_initWithSessionMakeConnection_hook);+ }+ if (m3) {+ cfx_orig_pv_setSession =+ method_setImplementation(m3, (IMP)cfx_pv_setSession_hook);+ }+ cfxlog(@"installed PVLayer hooks (init=%p initMC=%p setSession=%p)",+ cfx_orig_pv_initWithSession,+ cfx_orig_pv_initWithSessionMakeConnection,+ cfx_orig_pv_setSession);+}++// MARK: - preview layer scan+//+// 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.++static void cfx_walk_layers(CALayer *layer, NSMutableArray *out) {+ if (!layer) return;+ Class avcvpl = NSClassFromString(@"AVCaptureVideoPreviewLayer");+ if (avcvpl && [layer isKindOfClass:avcvpl]) {+ [out addObject:layer];+ }+ for (CALayer *sub in layer.sublayers) {+ cfx_walk_layers(sub, out);+ }+}++static void cfx_scan_preview_layers(void) {+ Class UIApp = NSClassFromString(@"UIApplication");+ if (!UIApp) return;+ id app = [UIApp performSelector:@selector(sharedApplication)];+ if (!app) return;+ NSArray *windows = nil;+ @try {+ // iOS 13+: connectedScenes → UIWindowScene → windows+ NSSet *scenes = [app valueForKey:@"connectedScenes"];+ NSMutableArray *all = [NSMutableArray array];+ for (id scene in scenes) {+ @try {+ NSArray *w = [scene valueForKey:@"windows"];+ if (w) [all addObjectsFromArray:w];+ } @catch (NSException *e) {}+ }+ if (all.count > 0) windows = all;+ } @catch (NSException *e) {}+ if (!windows.count) {+ @try { windows = [app valueForKey:@"windows"]; } @catch (NSException *e) {}+ }+ if (!windows.count) return;++ NSMutableArray *found = [NSMutableArray array];+ for (id w in windows) {+ @try {+ CALayer *root = [w valueForKey:@"layer"];+ cfx_walk_layers(root, found);+ } @catch (NSException *e) {}+ }+ if (found.count == 0) return;+ NSUInteger added = cfx_preview_add_layers(found);+ if (added > 0) {+ cfxlog(@"[scan] adopted %lu preview layer(s)", (unsigned long)added);+ cfx_preview_start_timer();+ }+}++static dispatch_source_t cfx_scan_timer = NULL;+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,+ 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_resume(cfx_scan_timer);+ cfxlog(@"scan timer armed (1 Hz)");+}diff --git a/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.m b/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.mnew file mode 100644index 00000000..14b2df50--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Session/CamFixSessionGuards.m@@ -0,0 +1,208 @@+// AVCaptureSession state guards and getter lies that keep a vcam-bound+// session live, plus the session start and device input diagnostics.++#import "CamFixPrivate.h"++// MARK: - AVCaptureSession state guards+//+// After ~4-5s of no real sample buffer flow on the AVCaptureSession's+// preview connection, Camera.app's session transitions to+// "interrupted" / not-running and disables the shutter. We bypass the+// daemon's preview pipeline (we feed CALayer.contents directly), so+// no sample buffers ever flow.+//+// Two guards on AVCaptureSession for our vcam-bound sessions:+// 1. -[AVCaptureSession _setRunning:NO] → swallow (stay running)+// 2. -[AVCaptureSession _setInterrupted:YES withReason:interruptor:] → swallow++static IMP cfx_orig_setRunning = NULL;+static IMP cfx_orig_setInterrupted = NULL;++// Diagnoses why a vcam session never reaches the daemon's graph builder:+// logs what the client thinks its session looks like at start time.+static void cfx_log_session_start(AVCaptureSession *session) {+ @try {+ NSMutableArray *lines = [NSMutableArray array];+ [lines addObject:[NSString stringWithFormat:@"inputs=%lu",+ (unsigned long)session.inputs.count]];+ for (AVCaptureInput *inp in session.inputs) {+ NSString *uid = nil;+ if ([inp isKindOfClass:[AVCaptureDeviceInput class]]) {+ uid = ((AVCaptureDeviceInput *)inp).device.uniqueID;+ }+ [lines addObject:[NSString stringWithFormat:@"input %@ cls=%@ ports=%lu",+ uid ?: @"?",+ NSStringFromClass([inp class]),+ (unsigned long)inp.ports.count]];+ }+ for (AVCaptureOutput *outp in session.outputs) {+ BOOL anyEnabled = NO;+ NSUInteger videoConns = 0;+ for (AVCaptureConnection *c in outp.connections) {+ if (c.enabled || c.active) anyEnabled = YES;+ for (AVCaptureInputPort *port in c.inputPorts) {+ if ([port.mediaType isEqualToString:AVMediaTypeVideo]) {+ videoConns++;+ break;+ }+ }+ }+ [lines addObject:[NSString stringWithFormat:@"output %@ conns=%lu video=%lu anyEnabled/Active=%d",+ NSStringFromClass([outp class]),+ (unsigned long)outp.connections.count,+ (unsigned long)videoConns,+ anyEnabled]];+ }+ cfxlog(@"[session start] %p %@", session, [lines componentsJoinedByString:@" | "]);+ } @catch (NSException *e) {+ cfxlog(@"[session start] dump exception: %@", e);+ }+}++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);+ }+ if (!running && cfx_session_is_for_vcam(self)) {+ cfxlog(@"[session _setRunning:NO] suppressed for vcam session %p", self);+ return;+ }+ typedef void (*OrigFn)(id, SEL, BOOL);+ ((OrigFn)cfx_orig_setRunning)(self, _cmd, running);+}++static void cfx_session_setInterrupted_hook(id self, SEL _cmd,+ BOOL interrupted,+ long reason,+ id interruptor) {+ if (interrupted && cfx_session_is_for_vcam(self)) {+ cfxlog(@"[session _setInterrupted:YES reason=%ld] suppressed for vcam session %p",+ reason, self);+ return;+ }+ typedef void (*OrigFn)(id, SEL, BOOL, long, id);+ ((OrigFn)cfx_orig_setInterrupted)(self, _cmd, interrupted, reason, interruptor);+}++void cfx_install_session_guards(void) {+ Class cls = NSClassFromString(@"AVCaptureSession");+ if (!cls) { cfxlog(@"AVCaptureSession missing"); return; }+ SEL s1 = NSSelectorFromString(@"_setRunning:");+ SEL s2 = NSSelectorFromString(@"_setInterrupted:withReason:interruptor:");+ Method m1 = class_getInstanceMethod(cls, s1);+ Method m2 = class_getInstanceMethod(cls, s2);+ if (m1) {+ cfx_orig_setRunning = method_setImplementation(m1, (IMP)cfx_session_setRunning_hook);+ cfxlog(@"installed _setRunning: hook");+ } else {+ cfxlog(@"_setRunning: not in objc table");+ }+ if (m2) {+ cfx_orig_setInterrupted = method_setImplementation(m2, (IMP)cfx_session_setInterrupted_hook);+ cfxlog(@"installed _setInterrupted: hook");+ } else {+ cfxlog(@"_setInterrupted: not in objc table");+ }+}++// MARK: - device input / port diagnostics+//+// A vcam session that never reaches startRunning usually dies at addInput:+// because the client-side device object has no ports (streams missing from+// the remote source copy). Log the port count at input creation and the+// app's own canAddInput: checks.++static IMP cfx_orig_dvinput_init = NULL;+static IMP cfx_orig_canAddInput = NULL;++__attribute__((ns_returns_retained))+static id cfx_dvinput_init_hook(id self, SEL _cmd, AVCaptureDevice *device, NSError **err) {+ typedef id (*Fn)(id, SEL, AVCaptureDevice *, NSError **);+ id ret = ((Fn)cfx_orig_dvinput_init)(self, _cmd, device, err);+ if ([device.uniqueID isEqualToString:VCAM_UID]) {+ cfxlog(@"[DeviceInput init] device=%@ ports=%lu err=%@",+ device.uniqueID,+ (unsigned long)((AVCaptureDeviceInput *)ret).ports.count,+ err && *err ? *err : nil);+ }+ return ret;+}++static BOOL cfx_canAddInput_hook(id self, SEL _cmd, AVCaptureInput *input) {+ typedef BOOL (*Fn)(id, SEL, AVCaptureInput *);+ BOOL ret = ((Fn)cfx_orig_canAddInput)(self, _cmd, input);+ if ([input isKindOfClass:[AVCaptureDeviceInput class]] &&+ [((AVCaptureDeviceInput *)input).device.uniqueID isEqualToString:VCAM_UID]) {+ cfxlog(@"[canAddInput:%@] -> %d", VCAM_UID, ret);+ }+ return ret;+}++void cfx_install_input_diagnostics(void) {+ Class cls = NSClassFromString(@"AVCaptureDeviceInput");+ Method m = cls ? class_getInstanceMethod(cls, @selector(initWithDevice:error:)) : NULL;+ if (m) {+ cfx_orig_dvinput_init = method_setImplementation(m, (IMP)cfx_dvinput_init_hook);+ cfxlog(@"installed DeviceInput init diag");+ }+ Class sessCls = NSClassFromString(@"AVCaptureSession");+ Method m2 = sessCls ? class_getInstanceMethod(sessCls, @selector(canAddInput:)) : NULL;+ if (m2) {+ cfx_orig_canAddInput = method_setImplementation(m2, (IMP)cfx_canAddInput_hook);+ cfxlog(@"installed canAddInput diag");+ }+}++// MARK: - AVCaptureSession state GETTER lies+//+// If Camera.app polls -isRunning / -isInterrupted in its UI loop and+// reacts to a transition (no-frames timer or similar), we can lie to+// keep the UI in "live preview" mode.++static IMP cfx_orig_isRunning = NULL;+static IMP cfx_orig_isInterrupted = NULL;+static int cfx_isRunning_logged = 0;+static int cfx_isInterrupted_logged = 0;++static BOOL cfx_session_isRunning_hook(id self, SEL _cmd) {+ typedef BOOL (*Fn)(id, SEL);+ BOOL real = ((Fn)cfx_orig_isRunning)(self, _cmd);+ if (cfx_session_is_for_vcam(self)) {+ cfx_track_vcam_session(self);+ if (cfx_isRunning_logged < 3) {+ cfxlog(@"[isRunning] real=%d -> forcing YES (session=%p)", real, self);+ cfx_isRunning_logged++;+ }+ return YES;+ }+ return real;+}++static BOOL cfx_session_isInterrupted_hook(id self, SEL _cmd) {+ typedef BOOL (*Fn)(id, SEL);+ BOOL real = ((Fn)cfx_orig_isInterrupted)(self, _cmd);+ if (cfx_session_is_for_vcam(self) && real) {+ if (cfx_isInterrupted_logged < 3) {+ cfxlog(@"[isInterrupted] real=%d -> forcing NO (session=%p)", real, self);+ cfx_isInterrupted_logged++;+ }+ return NO;+ }+ return real;+}++void cfx_install_session_state_lies(void) {+ Class cls = NSClassFromString(@"AVCaptureSession");+ if (!cls) return;+ Method r = class_getInstanceMethod(cls, @selector(isRunning));+ Method i = class_getInstanceMethod(cls, @selector(isInterrupted));+ if (r) {+ cfx_orig_isRunning = method_setImplementation(r, (IMP)cfx_session_isRunning_hook);+ cfxlog(@"installed isRunning lie");+ }+ if (i) {+ cfx_orig_isInterrupted = method_setImplementation(i, (IMP)cfx_session_isInterrupted_hook);+ cfxlog(@"installed isInterrupted lie");+ }+}diff --git a/VPhoneGuestComponents/CamFix/Session/CamFixVideoDelivery.m b/VPhoneGuestComponents/CamFix/Session/CamFixVideoDelivery.mnew file mode 100644index 00000000..934cd364--- /dev/null+++ b/VPhoneGuestComponents/CamFix/Session/CamFixVideoDelivery.m@@ -0,0 +1,91 @@+// Frames delivered straight to AVCaptureVideoDataOutput delegates of vcam+// sessions whose capture graph never runs.++#import "CamFixPrivate.h"++// MARK: - client-side video delivery for vcam sessions+//+// When a session's capture graph never runs, its AVCaptureVideoDataOutput+// delegates starve. Deliver the shared-layer sample directly to the+// delegate on the output's own callback queue — the same pattern as the+// photo delivery path. In this VM the synthetic source is the only camera,+// so any vcam session's data output is ours. Stops while the graph+// delivers (see cfx_graph_is_delivering).++static NSHashTable *cfx_vcam_sessions = nil; // weak, guarded+static pthread_mutex_t cfx_vcam_sessions_lock = PTHREAD_MUTEX_INITIALIZER;+static uint64_t cfx_video_deliver_count = 0;+static _Atomic uint64_t cfx_video_deliver_ok = 0;+static _Atomic int cfx_video_deliver_logged = 0;++// Also arms the 30 Hz pump: a session with a data output and no preview+// layer would otherwise never start it.+void cfx_track_vcam_session(id session) {+ if (!session) return;+ BOOL added = NO;+ pthread_mutex_lock(&cfx_vcam_sessions_lock);+ if (!cfx_vcam_sessions) cfx_vcam_sessions = [NSHashTable weakObjectsHashTable];+ if (![cfx_vcam_sessions containsObject:session]) {+ [cfx_vcam_sessions addObject:session];+ added = YES;+ }+ pthread_mutex_unlock(&cfx_vcam_sessions_lock);+ if (!added) return;+ cfxlog(@"[video delivery] tracking session %p", session);+ dispatch_async(dispatch_get_main_queue(), ^{ cfx_preview_start_timer(); });+}++void cfx_deliver_video_frames_once(void) {+ pthread_mutex_lock(&cfx_vcam_sessions_lock);+ NSArray *sessions = cfx_vcam_sessions.allObjects;+ pthread_mutex_unlock(&cfx_vcam_sessions_lock);+ if (sessions.count == 0 || cfx_graph_is_delivering()) return;+ Class dataOutCls = NSClassFromString(@"AVCaptureVideoDataOutput");+ if (!dataOutCls) return;++ for (id sess in sessions) {+ NSArray *outputs = nil;+ @try { outputs = [sess valueForKey:@"outputs"]; } @catch (NSException *e) { continue; }+ for (id out in outputs) {+ if (![out isKindOfClass:dataOutCls]) continue;+ id delegate = nil;+ dispatch_queue_t q = nil;+ @try { delegate = [out valueForKey:@"sampleBufferDelegate"]; } @catch (NSException *e) {}+ @try { q = [out valueForKey:@"sampleBufferCallbackQueue"]; } @catch (NSException *e) {}+ if (!delegate) continue;+ NSArray *conns = nil;+ @try { conns = [out valueForKey:@"connections"]; } @catch (NSException *e) {}+ for (id conn in conns) {+ BOOL enabled = NO;+ @try { enabled = [[conn valueForKey:@"enabled"] boolValue]; } @catch (NSException *e) {}+ if (!enabled) continue;+ CMSampleBufferRef sb = cfx_build_cmsb();+ if (!sb) continue;+ cfx_video_deliver_count++;+ dispatch_async(q ?: dispatch_get_main_queue(), ^{+ @autoreleasepool {+ @try {+ ((void (*)(id, SEL, id, CMSampleBufferRef, id))objc_msgSend)(+ delegate,+ @selector(captureOutput:didOutputSampleBuffer:fromConnection:),+ out,+ sb,+ conn);+ cfx_video_deliver_ok++;+ } @catch (NSException *e) {+ if (cfx_video_deliver_logged++ < 8) {+ cfxlog(@"[video delivery] delegate exception: %@", e);+ }+ }+ CFRelease(sb);+ }+ });+ }+ }+ }+ if (cfx_video_deliver_count && (cfx_video_deliver_count % 90) == 1) {+ cfxlog(@"[video delivery] attempted=%llu ok=%llu",+ (unsigned long long)cfx_video_deliver_count,+ (unsigned long long)cfx_video_deliver_ok);+ }+}diff --git a/VPhoneGuestComponents/CamFix/libcamfix.m b/VPhoneGuestComponents/CamFix/libcamfix.mindex 3b75d3a2..1a3b3b84 100644--- a/VPhoneGuestComponents/CamFix/libcamfix.m+++ b/VPhoneGuestComponents/CamFix/libcamfix.m@@ -10,6 +10,7 @@* Camera.app's session-preset → format lookup returns nil for our synth* because no preset matches our published formats exactly. Substitute* the device's first supported format instead.+ * (Device/CamFixActiveFormat.m)** 2. -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:]: when the* photo output is bound to our virtual camera, read@@ -20,1602 +21,42 @@* client that implements nothing else. If this hook fails, the capture* path falls back to the original AVF code path (which would re-throw /* error out).+ * (Photo/CamFixCapturePhoto.m)** 3. The moment-capture trio (begin / commit / cancelMomentCapture):* Camera.app's shutter path. For our virtual camera the original is* deliberately never called — it throws — so AVF's state stays clean and* subsequent shutter taps don't compound corruption.+ * (Photo/CamFixMomentCapture.m)** 4. AVCaptureSession's _setRunning: / _setInterrupted: guards and its* isRunning / isInterrupted getters: keep a vcam-bound session live* although no sample buffers ever flow through it.+ * (Session/CamFixSessionGuards.m)** 5. The AVCaptureVideoPreviewLayer pump and its scan timer: push CGImage* frames into CALayer.contents, since nothing feeds the layer otherwise.+ * (Preview/CamFixPreviewLayers.m, fed by Preview/CamFixGraphTap.m)** 6. AVCapturePhoto's -fileDataRepresentation / -CGImageRepresentation: hand* back the bytes stamped onto the photos we synthesized.+ * (Photo/CamFixPhotoSynthesis.m)** 7. CAMStillImageCaptureRequest stubs for the accessors Camera.app's* capture engine reads off a request.+ * (Photo/CamFixMomentCapture.m)*- * Diagnostics go to the shared camera media directory, written by cfxlog.+ * 8. AVCaptureVideoDataOutput delivery for vcam sessions whose capture graph+ * never runs. (Session/CamFixVideoDelivery.m)+ *+ * Frames come from the vphoned shared frame file through the shared data+ * plane (Frame/CamFixFrameSource.m). Diagnostics go to the shared camera+ * media directory, written by cfxlog.*/-#import <AVFoundation/AVFoundation.h>-#import <Foundation/Foundation.h>-#import <ImageIO/ImageIO.h>-#import <IOSurface/IOSurfaceRef.h>-#include <pthread.h>-#include <stdio.h>-#include <sys/mman.h>-#include <sys/stat.h>-#include <fcntl.h>-#include <unistd.h>-#include <objc/runtime.h>-#include <objc/message.h>+#import "CamFixPrivate.h"#include <mach-o/dyld.h>#include <mach-o/loader.h>-#include <string.h>-#include "VCamFrameProtocol.h"-#include "vcam_dataplane.h"--#define VCAM_UID @"vphone:vcam:0"--static void cfxlog(NSString *fmt, ...) {- va_list ap; va_start(ap, fmt);- NSString *line = [[NSString alloc] initWithFormat:fmt arguments:ap];- va_end(ap);- FILE *fp = fopen(VPHONE_VCAM_APP_LOG_PATH, "a");- if (fp) {- fprintf(fp, "[camfix:%d] %s\n", getpid(), line.UTF8String ?: "?");- fclose(fp);- } else {- NSLog(@"camfix: %@", line);- }-}--// MARK: - shm reader--static int cfx_shm_fd = -1;-static const uint8_t *cfx_shm_base = NULL;-static size_t cfx_shm_size = 0;--static BOOL cfx_shm_open(void) {- if (cfx_shm_base) return YES;- cfx_shm_fd = open(VPHONE_VCAM_SHM_PATH, O_RDONLY);- if (cfx_shm_fd < 0) {- cfxlog(@"shm open failed: %s (errno=%d)", VPHONE_VCAM_SHM_PATH, errno);- return NO;- }- struct stat st;- if (fstat(cfx_shm_fd, &st) < 0) {- close(cfx_shm_fd); cfx_shm_fd = -1; return NO;- }- cfx_shm_size = (size_t)st.st_size;- void *p = mmap(NULL, cfx_shm_size, PROT_READ, MAP_SHARED, cfx_shm_fd, 0);- if (p == MAP_FAILED) {- close(cfx_shm_fd); cfx_shm_fd = -1; return NO;- }- cfx_shm_base = (const uint8_t *)p;- cfxlog(@"shm mapped %s size=%zu", VPHONE_VCAM_SHM_PATH, cfx_shm_size);- return YES;-}--// One owner for "map the frame and check it is usable": opens the shm,-// rejects a zeroed header, and rejects pixel data that runs past the-// mapping. Planar 4:2:0 frames carry a chroma plane after luma, and the-// data plane reads it, so it counts toward the checked length. On success-// fills *frame with the header fields and a pointer into the mapping.-// Returns NO when the frame cannot be read.-static BOOL cfx_shm_frame(vcc_frame_desc_t *frame) {- if (!cfx_shm_open()) return NO;- const vphone_vcam_shm_header_t *hdr = (const vphone_vcam_shm_header_t *)cfx_shm_base;- uint32_t w = hdr->width, h = hdr->height, bpr = hdr->bytes_per_row;- uint32_t fmt = hdr->pixel_format;- if (!w || !h || !bpr) {- cfxlog(@"shm header zeros");- return NO;- }- size_t len = (size_t)bpr * h;- if (vcc_is_planar_yuv(fmt)) len += (size_t)(2u * (w / 2)) * (h / 2);- if ((size_t)VPHONE_VCAM_SHM_HEADER_SIZE + len > cfx_shm_size) {- cfxlog(@"shm: pixel range exceeds mapping");- return NO;- }- memset(frame, 0, sizeof(*frame));- frame->width = w;- frame->height = h;- frame->bytes_per_row = bpr;- frame->pixel_format = fmt;- frame->timestamp_ns = hdr->timestamp_ns;- frame->frame_index = hdr->frame_index;- frame->pixels = cfx_shm_base + VPHONE_VCAM_SHM_HEADER_SIZE;- frame->pixels_length = len;- return YES;-}--static void cfx_cg_release_data(void *info, const void *data, size_t size) {- (void)info; (void)size;- free((void *)data);-}--// Snapshot the shm frame and decode it to tightly packed BGRA. Every RGB-// consumer (CGImage, JPEG, IOSurface) goes through here, so the wire pixel-// format is honored instead of assumed. Returns a malloc'd buffer of-// w * 4 * h bytes; the caller frees it.-static uint8_t *cfx_bgra_from_shm(uint32_t *outW, uint32_t *outH) {- vcc_frame_desc_t frame;- if (!cfx_shm_frame(&frame)) return NULL;- uint8_t *bgra = NULL;- uint32_t bpr = 0;- if (vcc_bgra_bytes_from_frame(&frame, &bgra, &bpr) != 0) return NULL;- *outW = frame.width;- *outH = frame.height;- return bgra;-}--// Photo and video delivery are BGRA. The shared data plane builds the-// CVPixelBuffer, format description and camera attachments and converts-// the wire format when needed. Timing is stream-local (host epoch-ns PTS-// values lose precision past 2^53 in downstream float conversions) and-// advances per buffer, so builds from the preview queue and the photo-// paths serialize on one lock.-static pthread_mutex_t cfx_cmsb_lock = PTHREAD_MUTEX_INITIALIZER;-static vcc_timing_state_t cfx_cmsb_timing;-static BOOL cfx_cmsb_timing_ready = NO;--static CMSampleBufferRef cfx_build_cmsb(void) {- vcc_frame_desc_t frame;- if (!cfx_shm_frame(&frame)) return NULL;- uint8_t *pixels = malloc(frame.pixels_length);- if (!pixels) return NULL;- memcpy(pixels, frame.pixels, frame.pixels_length);- frame.pixels = pixels;-- pthread_mutex_lock(&cfx_cmsb_lock);- if (!cfx_cmsb_timing_ready) {- vcc_timing_init(&cfx_cmsb_timing);- cfx_cmsb_timing_ready = YES;- }- CMSampleBufferRef sb = vcc_cmsb_from_frame(&frame, VCC_FMT_BGRA, &cfx_cmsb_timing);- pthread_mutex_unlock(&cfx_cmsb_lock);- free(pixels);- if (!sb) cfxlog(@"build_cmsb: shared data plane returned NULL");- return sb;-}--// MARK: - _setActiveFormat: nil-format guard--static IMP cfx_orig_setActiveFormat = NULL;--// Signature: void(*)(id self, SEL _cmd, AVCaptureDeviceFormat *fmt,-// BOOL resetZoomAndFrameRates, NSString *preset)-static void cfx_setActiveFormat_hook(id self, SEL _cmd, id fmt,- BOOL resetZoomAndFrameRates,- NSString *preset) {- if (!fmt) {- NSString *uid = nil;- @try { uid = [self valueForKey:@"uniqueID"]; } @catch (NSException *e) {}- cfxlog(@"[setActiveFormat:nil] device.uid=%@ preset=%@", uid, preset);- if ([uid isEqualToString:VCAM_UID]) {- // Substitute the first available format from the device's -formats list.- NSArray *fmts = nil;- @try { fmts = [self valueForKey:@"formats"]; } @catch (NSException *e) {}- if (fmts.count > 0) {- fmt = fmts.firstObject;- cfxlog(@"[setActiveFormat:] substituted first format: %@", fmt);- } else {- cfxlog(@"[setActiveFormat:] device.formats is empty — letting AVF throw");- }- }- }- typedef void (*OrigFn)(id, SEL, id, BOOL, NSString *);- ((OrigFn)cfx_orig_setActiveFormat)(self, _cmd, fmt, resetZoomAndFrameRates, preset);-}--static void cfx_install_setActiveFormat_hook(void) {- Class cls = NSClassFromString(@"AVCaptureFigVideoDevice");- if (!cls) { cfxlog(@"AVCaptureFigVideoDevice missing"); return; }- SEL sel = NSSelectorFromString(- @"_setActiveFormat:resetVideoZoomFactorAndMinMaxFrameDurations:sessionPreset:");- Method m = class_getInstanceMethod(cls, sel);- if (!m) {- cfxlog(@"_setActiveFormat: method not in objc table");- return;- }- cfx_orig_setActiveFormat =- method_setImplementation(m, (IMP)cfx_setActiveFormat_hook);- cfxlog(@"installed _setActiveFormat: hook (orig=%p)",- cfx_orig_setActiveFormat);-}--// MARK: - capturePhoto swizzle (modern + deprecated delegate paths)--static IMP cfx_orig_capturePhoto = NULL;--// Forward decls: helpers defined further down — shared with the-// moment-capture (Camera.app) delivery path.-static IOSurfaceRef cfx_build_iosurface_from_shm(uint32_t *outW, uint32_t *outH) CF_RETURNS_RETAINED;-static CGImageRef cfx_build_cgimage_from_shm(void) CF_RETURNS_RETAINED;-static NSData *cfx_build_jpeg_from_shm(void);-static id cfx_build_avcapturephoto_with_request(- IOSurfaceRef surf,- uint32_t w,- uint32_t h,- id captureRequest);-static BOOL cfx_output_is_for_vcam(id self);--// Associated-object keys (used by fileDataRepresentation /-// CGImageRepresentation hooks to recognize "our" photos). Defined here-// so cfx_deliver_capturePhoto can reference them before the hooks that-// also use them are declared further down.-static const void *CFX_ASSOC_JPEG_KEY = &CFX_ASSOC_JPEG_KEY;-static const void *CFX_ASSOC_CGIMG_KEY = &CFX_ASSOC_CGIMG_KEY;--// Tag a synthesized photo with our JPEG + CGImage so fileDataRepresentation-// / CGImageRepresentation return our bytes. Returns the JPEG so the caller-// can log its size.-static NSData *cfx_stamp_photo(id photo) {- NSData *jpeg = cfx_build_jpeg_from_shm();- CGImageRef cgImg = cfx_build_cgimage_from_shm();- if (jpeg) objc_setAssociatedObject(- photo,- CFX_ASSOC_JPEG_KEY,- jpeg,- OBJC_ASSOCIATION_RETAIN_NONATOMIC);- if (cgImg) {- objc_setAssociatedObject(- photo,- CFX_ASSOC_CGIMG_KEY,- (__bridge id)cgImg,- OBJC_ASSOCIATION_RETAIN_NONATOMIC);- CGImageRelease(cgImg);- }- return jpeg;-}--static void cfx_deliver_capturePhoto(id output, id delegate) {- // Modern path used by any AVF client: build a real AVCapturePhoto from- // the shm frame, tag it with our JPEG + CGImage so fileDataRepresentation- // / CGImageRepresentation return our bytes, fire the modern delegate- // didFinishProcessingPhoto:error:.- // Falls back to the deprecated CMSampleBuffer delegate if the client- // opts into it (only test harnesses do; production AVF clients implement- // the modern method).- SEL oldSel = NSSelectorFromString(- @"captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:");- if ([delegate respondsToSelector:oldSel]) {- CMSampleBufferRef sbuf = cfx_build_cmsb();- if (!sbuf) { cfxlog(@"[capturePhoto] build_cmsb returned NULL"); return; }- cfxlog(@"[capturePhoto] dispatching deprecated didFinishProcessingPhotoSampleBuffer:");- ((void (*)(id, SEL, id, CMSampleBufferRef, CMSampleBufferRef, id, id, id))objc_msgSend)(- delegate,- oldSel,- output,- sbuf,- NULL,- (id)nil,- (id)nil,- (id)nil);- CFRelease(sbuf);- return;- }-- SEL S5 = @selector(captureOutput:didFinishProcessingPhoto:error:);- if (![delegate respondsToSelector:S5]) {- cfxlog(@"[capturePhoto] delegate implements neither modern nor deprecated method");- return;- }-- uint32_t w = 0, h = 0;- IOSurfaceRef surf = cfx_build_iosurface_from_shm(&w, &h);- if (!surf) { cfxlog(@"[capturePhoto] no IOSurface"); return; }-- // captureRequest = nil (no CAMCaptureEngine outside Camera.app). The- // AVCapturePhoto init handles nil safely — objc_msgSend on nil returns 0- // for the resolvedSettings / unresolvedSettings calls it makes during init.- id photo = cfx_build_avcapturephoto_with_request(surf, w, h, nil);- CFRelease(surf);- if (!photo) { cfxlog(@"[capturePhoto] no AVCapturePhoto"); return; }-- NSData *jpeg = cfx_stamp_photo(photo);- cfxlog(@"[capturePhoto] firing didFinishProcessingPhoto: with vcam photo (%lu bytes jpeg)",- (unsigned long)jpeg.length);- ((void (*)(id, SEL, id, id, id))objc_msgSend)(delegate, S5, output, photo, nil);-}--static void cfx_capturePhoto_hook(id self, SEL _cmd, id settings, id delegate) {- cfxlog(@"[capturePhoto] self=%p settings=%@ delegate=%@",- self, settings, delegate);- BOOL forVcam = cfx_output_is_for_vcam(self);- cfxlog(@"forVcam=%d", forVcam);-- if (!forVcam) {- typedef void (*OrigFn)(id, SEL, id, id);- ((OrigFn)cfx_orig_capturePhoto)(self, _cmd, settings, delegate);- return;- }- __strong id retainedSelf = self;- __strong id retainedDelegate = delegate;- dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{- @autoreleasepool {- cfx_deliver_capturePhoto(retainedSelf, retainedDelegate);- }- });-}--static void cfx_install_capturePhoto_hook(void) {- Class cls = NSClassFromString(@"AVCapturePhotoOutput");- if (!cls) { cfxlog(@"AVCapturePhotoOutput missing"); return; }- SEL sel = @selector(capturePhotoWithSettings:delegate:);- Method m = class_getInstanceMethod(cls, sel);- if (!m) { cfxlog(@"capturePhotoWithSettings:delegate: not found"); return; }- cfx_orig_capturePhoto = method_setImplementation(m, (IMP)cfx_capturePhoto_hook);- cfxlog(@"installed capturePhoto hook (orig=%p)", cfx_orig_capturePhoto);-}--// MARK: - AVCaptureVideoPreviewLayer.contents pump-//-// AVCaptureVideoPreviewLayer is a CALayer subclass that normally displays-// the camera's preview via internal IOSurface plumbing fed by the daemon.-// For our virtual camera, no daemon-side preview pipeline is built, so the-// layer stays black.-//-// Workaround: capture each AVCaptureVideoPreviewLayer instance bound to-// our virtual camera, and pump CGImage frames into its `contents` property-// at 30 Hz from a background timer. CALayer rendering picks the image up-// without needing the underlying AVF preview infrastructure.--static NSHashTable *cfx_preview_layers = nil;-static dispatch_source_t cfx_preview_timer = NULL;--// MARK: - preview fed from the client's capture graph-//-// A real camera has one path: capture graph -> sample buffer -> preview-// surface. While a vcam preview layer is live, tap the client-side sink node-// that receives the capture graph's sample buffers and feed the preview from-// that sample — the same frame, timestamp, pixel format and camera metadata-// every other consumer sees. The shm reader is the bootstrap fallback for-// the window before the daemon builds the graph (preview-only sessions).--static pthread_mutex_t cfx_tap_lock = PTHREAD_MUTEX_INITIALIZER;-static CMSampleBufferRef cfx_tap_latest = NULL; // retained, guarded-static CFAbsoluteTime cfx_tap_latest_at = 0; // guarded-#define CFX_TAP_STALE_SECONDS 0.5--static void cfx_tap_store(CMSampleBufferRef sb) {- CFRetain(sb);- pthread_mutex_lock(&cfx_tap_lock);- CMSampleBufferRef old = cfx_tap_latest;- cfx_tap_latest = sb;- cfx_tap_latest_at = CFAbsoluteTimeGetCurrent();- pthread_mutex_unlock(&cfx_tap_lock);- if (old) CFRelease(old);-}--// Returns a retained latest sample, or NULL if none is fresh.-static CMSampleBufferRef cfx_tap_fetch_fresh(void) {- CMSampleBufferRef out = NULL;- pthread_mutex_lock(&cfx_tap_lock);- if (cfx_tap_latest &&- CFAbsoluteTimeGetCurrent() - cfx_tap_latest_at < CFX_TAP_STALE_SECONDS) {- out = (CMSampleBufferRef)CFRetain(cfx_tap_latest);- }- pthread_mutex_unlock(&cfx_tap_lock);- return out;-}--// When the capture graph last delivered a sample to this process. Video-// delivery below is the fallback for sessions whose graph never runs; once-// the graph delivers, the data outputs are fed natively and the fallback-// must stop, or delegates would see two interleaved streams.-static _Atomic double cfx_graph_last_at = 0;--static BOOL cfx_graph_is_delivering(void) {- return CFAbsoluteTimeGetCurrent() - cfx_graph_last_at < CFX_TAP_STALE_SECONDS;-}--static IMP cfx_orig_rqsn_render = NULL;--static void cfx_rqsn_render_tap(id self, SEL _cmd, CMSampleBufferRef sb, id input) {- if (sb) cfx_graph_last_at = CFAbsoluteTimeGetCurrent();- // Hold buffers only while a vcam preview is being pumped — never retain- // graph buffers in clients without preview layers.- if (sb && cfx_preview_layers.count > 0) cfx_tap_store(sb);- typedef void (*Fn)(id, SEL, CMSampleBufferRef, id);- ((Fn)cfx_orig_rqsn_render)(self, _cmd, sb, input);-}--static void cfx_install_remote_queue_tap(void) {- // Client-side tail of the capture graph. In this VM the synthetic source- // is the only camera, so samples arriving here while a vcam preview is- // tracked are ours by construction.- Class cls = NSClassFromString(@"BWRemoteQueueSinkNode");- if (!cls) { cfxlog(@"[tap] BWRemoteQueueSinkNode missing"); return; }- SEL sel = @selector(renderSampleBuffer:forInput:);- Method m = class_getInstanceMethod(cls, sel);- if (!m) { cfxlog(@"[tap] renderSampleBuffer:forInput: missing"); return; }- cfx_orig_rqsn_render = method_setImplementation(m, (IMP)cfx_rqsn_render_tap);- cfxlog(@"[tap] installed BWRemoteQueueSinkNode tap (orig=%p)", cfx_orig_rqsn_render);-}--static IMP cfx_orig_pv_initWithSession = NULL;-static IMP cfx_orig_pv_initWithSessionMakeConnection = NULL;--static void cfx_pump_preview_once(void) {- if (!cfx_preview_layers || cfx_preview_layers.count == 0) return;- CGImageRef img = NULL;- // Preferred: the sample the capture graph just delivered.- CMSampleBufferRef sb = cfx_tap_fetch_fresh();- if (sb) {- CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sb);- if (pb) img = vcc_cgimage_from_pixel_buffer(pb);- CFRelease(sb);- }- // Bootstrap fallback: no graph yet, straight from the shm frame.- if (!img) img = cfx_build_cgimage_from_shm();- if (!img) return;- dispatch_async(dispatch_get_main_queue(), ^{- for (CALayer *layer in cfx_preview_layers) {- layer.contents = (__bridge id)img;- layer.contentsGravity = kCAGravityResizeAspectFill;- }- CGImageRelease(img);- });-}--// MARK: - client-side video delivery for vcam sessions-//-// When a session's capture graph never runs, its AVCaptureVideoDataOutput-// delegates starve. Deliver the shared-layer sample directly to the-// delegate on the output's own callback queue — the same pattern as the-// photo delivery path. In this VM the synthetic source is the only camera,-// so any vcam session's data output is ours. Stops while the graph-// delivers (see cfx_graph_is_delivering).--static NSHashTable *cfx_vcam_sessions = nil; // weak, guarded-static pthread_mutex_t cfx_vcam_sessions_lock = PTHREAD_MUTEX_INITIALIZER;-static uint64_t cfx_video_deliver_count = 0;-static _Atomic uint64_t cfx_video_deliver_ok = 0;-static _Atomic int cfx_video_deliver_logged = 0;--static void cfx_preview_start_timer(void);--// Also arms the 30 Hz pump: a session with a data output and no preview-// layer would otherwise never start it.-static void cfx_track_vcam_session(id session) {- if (!session) return;- BOOL added = NO;- pthread_mutex_lock(&cfx_vcam_sessions_lock);- if (!cfx_vcam_sessions) cfx_vcam_sessions = [NSHashTable weakObjectsHashTable];- if (![cfx_vcam_sessions containsObject:session]) {- [cfx_vcam_sessions addObject:session];- added = YES;- }- pthread_mutex_unlock(&cfx_vcam_sessions_lock);- if (!added) return;- cfxlog(@"[video delivery] tracking session %p", session);- dispatch_async(dispatch_get_main_queue(), ^{ cfx_preview_start_timer(); });-}--static void cfx_deliver_video_frames_once(void) {- pthread_mutex_lock(&cfx_vcam_sessions_lock);- NSArray *sessions = cfx_vcam_sessions.allObjects;- pthread_mutex_unlock(&cfx_vcam_sessions_lock);- if (sessions.count == 0 || cfx_graph_is_delivering()) return;- Class dataOutCls = NSClassFromString(@"AVCaptureVideoDataOutput");- if (!dataOutCls) return;-- for (id sess in sessions) {- NSArray *outputs = nil;- @try { outputs = [sess valueForKey:@"outputs"]; } @catch (NSException *e) { continue; }- for (id out in outputs) {- if (![out isKindOfClass:dataOutCls]) continue;- id delegate = nil;- dispatch_queue_t q = nil;- @try { delegate = [out valueForKey:@"sampleBufferDelegate"]; } @catch (NSException *e) {}- @try { q = [out valueForKey:@"sampleBufferCallbackQueue"]; } @catch (NSException *e) {}- if (!delegate) continue;- NSArray *conns = nil;- @try { conns = [out valueForKey:@"connections"]; } @catch (NSException *e) {}- for (id conn in conns) {- BOOL enabled = NO;- @try { enabled = [[conn valueForKey:@"enabled"] boolValue]; } @catch (NSException *e) {}- if (!enabled) continue;- CMSampleBufferRef sb = cfx_build_cmsb();- if (!sb) continue;- cfx_video_deliver_count++;- dispatch_async(q ?: dispatch_get_main_queue(), ^{- @autoreleasepool {- @try {- ((void (*)(id, SEL, id, CMSampleBufferRef, id))objc_msgSend)(- delegate,- @selector(captureOutput:didOutputSampleBuffer:fromConnection:),- out,- sb,- conn);- cfx_video_deliver_ok++;- } @catch (NSException *e) {- if (cfx_video_deliver_logged++ < 8) {- cfxlog(@"[video delivery] delegate exception: %@", e);- }- }- CFRelease(sb);- }- });- }- }- }- if (cfx_video_deliver_count && (cfx_video_deliver_count % 90) == 1) {- cfxlog(@"[video delivery] attempted=%llu ok=%llu",- (unsigned long long)cfx_video_deliver_count,- (unsigned long long)cfx_video_deliver_ok);- }-}--static void cfx_preview_start_timer(void) {- if (cfx_preview_timer) return;- dispatch_queue_t q = dispatch_queue_create("com.vphone.camfix.preview", DISPATCH_QUEUE_SERIAL);- cfx_preview_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);- dispatch_source_set_timer(- cfx_preview_timer,- dispatch_time(DISPATCH_TIME_NOW, 0),- 33333333ull,- 2000000ull);- dispatch_source_set_event_handler(cfx_preview_timer, ^{- @autoreleasepool {- cfx_pump_preview_once();- cfx_deliver_video_frames_once();- }- });- dispatch_resume(cfx_preview_timer);- cfxlog(@"preview pump armed (30 Hz)");-}--static void cfx_adopt_preview_layer(id layer) {- if (!cfx_preview_layers) cfx_preview_layers = [NSHashTable weakObjectsHashTable];- [cfx_preview_layers addObject:layer];- cfx_preview_start_timer();-}--static BOOL cfx_session_is_for_vcam(AVCaptureSession *session) {- @try {- for (AVCaptureInput *inp in session.inputs) {- if ([inp isKindOfClass:[AVCaptureDeviceInput class]]) {- AVCaptureDevice *d = ((AVCaptureDeviceInput *)inp).device;- if ([d.uniqueID isEqualToString:VCAM_UID]) return YES;- }- }- } @catch (NSException *e) {}- return NO;-}--__attribute__((ns_returns_retained))-static id cfx_pv_initWithSession_hook(id self, SEL _cmd, AVCaptureSession *session) {- typedef id (*Fn)(id, SEL, AVCaptureSession *);- id ret = ((Fn)cfx_orig_pv_initWithSession)(self, _cmd, session);- BOOL forVcam = (session != nil) && cfx_session_is_for_vcam(session);- cfxlog(@"[PVLayer initWithSession:%p] ret=%p forVcam=%d cls=%@",- session,- ret,- forVcam,- NSStringFromClass([ret class]));- if (ret && forVcam) cfx_adopt_preview_layer(ret);- return ret;-}--__attribute__((ns_returns_retained))-static id cfx_pv_initWithSessionMakeConnection_hook(- id self,- SEL _cmd,- AVCaptureSession *session,- BOOL makeConnection) {- typedef id (*Fn)(id, SEL, AVCaptureSession *, BOOL);- id ret = ((Fn)cfx_orig_pv_initWithSessionMakeConnection)(- self,- _cmd,- session,- makeConnection);- BOOL forVcam = (session != nil) && cfx_session_is_for_vcam(session);- cfxlog(@"[PVLayer _initWithSession:%p makeConnection:%d] ret=%p forVcam=%d cls=%@",- session,- makeConnection,- ret,- forVcam,- NSStringFromClass([ret class]));- if (ret && forVcam) cfx_adopt_preview_layer(ret);- return ret;-}--static IMP cfx_orig_pv_setSession = NULL;-static void cfx_pv_setSession_hook(id self, SEL _cmd, AVCaptureSession *session) {- typedef void (*Fn)(id, SEL, AVCaptureSession *);- ((Fn)cfx_orig_pv_setSession)(self, _cmd, session);- BOOL forVcam = (session != nil) && cfx_session_is_for_vcam(session);- cfxlog(@"[PVLayer setSession:%p] self=%p forVcam=%d cls=%@",- session,- self,- forVcam,- NSStringFromClass([self class]));- if (session && forVcam) cfx_adopt_preview_layer(self);-}--// Diagnostic: every CALayer subclass that has setSession: should fire here.-// If neither the AVCaptureVideoPreviewLayer hooks fire nor any subclass's-// setSession: shows up here, Camera.app must be using a fully private-// layer class we'll need to detect by walking the layer hierarchy.--// Fallback: scan UIApplication's windows for any AVCaptureVideoPreviewLayer-// (or subclass) and adopt them. Runs once a second; cheap if no windows-// match.--static void cfx_walk_layers(CALayer *layer, NSMutableArray *out) {- if (!layer) return;- Class avcvpl = NSClassFromString(@"AVCaptureVideoPreviewLayer");- if (avcvpl && [layer isKindOfClass:avcvpl]) {- [out addObject:layer];- }- for (CALayer *sub in layer.sublayers) {- cfx_walk_layers(sub, out);- }-}--static void cfx_scan_preview_layers(void) {- Class UIApp = NSClassFromString(@"UIApplication");- if (!UIApp) return;- id app = [UIApp performSelector:@selector(sharedApplication)];- if (!app) return;- NSArray *windows = nil;- @try {- // iOS 13+: connectedScenes → UIWindowScene → windows- NSSet *scenes = [app valueForKey:@"connectedScenes"];- NSMutableArray *all = [NSMutableArray array];- for (id scene in scenes) {- @try {- NSArray *w = [scene valueForKey:@"windows"];- if (w) [all addObjectsFromArray:w];- } @catch (NSException *e) {}- }- if (all.count > 0) windows = all;- } @catch (NSException *e) {}- if (!windows.count) {- @try { windows = [app valueForKey:@"windows"]; } @catch (NSException *e) {}- }- if (!windows.count) return;-- NSMutableArray *found = [NSMutableArray array];- for (id w in windows) {- @try {- CALayer *root = [w valueForKey:@"layer"];- cfx_walk_layers(root, found);- } @catch (NSException *e) {}- }- if (found.count == 0) return;- if (!cfx_preview_layers) cfx_preview_layers = [NSHashTable weakObjectsHashTable];- NSUInteger before = cfx_preview_layers.count;- for (CALayer *layer in found) [cfx_preview_layers addObject:layer];- NSUInteger after = cfx_preview_layers.count;- if (after > before) {- cfxlog(@"[scan] adopted %lu preview layer(s) (total=%lu)",- (unsigned long)(after - before),- (unsigned long)after);- cfx_preview_start_timer();- }-}--static dispatch_source_t cfx_scan_timer = NULL;-static 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,- 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_resume(cfx_scan_timer);- cfxlog(@"scan timer armed (1 Hz)");-}--static void cfx_install_preview_layer_hooks(void) {- Class cls = NSClassFromString(@"AVCaptureVideoPreviewLayer");- if (!cls) { cfxlog(@"AVCaptureVideoPreviewLayer missing"); return; }- SEL s1 = @selector(initWithSession:);- SEL s2 = NSSelectorFromString(@"_initWithSession:makeConnection:");- SEL s3 = @selector(setSession:);- Method m1 = class_getInstanceMethod(cls, s1);- Method m2 = class_getInstanceMethod(cls, s2);- Method m3 = class_getInstanceMethod(cls, s3);- if (m1) {- cfx_orig_pv_initWithSession =- method_setImplementation(m1, (IMP)cfx_pv_initWithSession_hook);- }- if (m2) {- cfx_orig_pv_initWithSessionMakeConnection =- method_setImplementation(m2, (IMP)cfx_pv_initWithSessionMakeConnection_hook);- }- if (m3) {- cfx_orig_pv_setSession =- method_setImplementation(m3, (IMP)cfx_pv_setSession_hook);- }- cfxlog(@"installed PVLayer hooks (init=%p initMC=%p setSession=%p)",- cfx_orig_pv_initWithSession,- cfx_orig_pv_initWithSessionMakeConnection,- cfx_orig_pv_setSession);-}--// MARK: - beginMomentCapture / commitMomentCapture hooks-//-// Camera.app uses -[AVCapturePhotoOutput beginMomentCaptureWithSettings:-// delegate:] (Live Photo path) when the user taps the shutter. The original-// implementation throws NSInvalidArgumentException because our daemon's-// photo XPC isn't backed by a real still pipeline.-//-// For our virtual camera, SKIP the original entirely. Just async-deliver-// a CMSampleBuffer to the delegate. We never call orig — so AVF's state-// stays clean and subsequent shutter taps don't compound corruption.--static IMP cfx_orig_beginMomentCapture = NULL;-static IMP cfx_orig_commitMomentCapture = NULL;-static IMP cfx_orig_cancelMomentCapture = NULL;--// Build a minimal AVCaptureResolvedPhotoSettings with just _uniqueID set.-// Bypasses the 32-arg +resolvedSettingsWith… factory (which crashes on-// nil dict args). CAMCaptureEngine's didFinishProcessingPhoto: only reads-// uniqueID off the resolvedSettings to match the pending request.--static id cfx_build_minimal_resolved(int64_t uid) {- Class outerCls = NSClassFromString(@"AVCaptureResolvedPhotoSettings");- Class innerCls = NSClassFromString(@"AVCaptureResolvedPhotoSettingsInternal");- if (!outerCls || !innerCls) return nil;-- id outer = class_createInstance(outerCls, 0);- id inner = class_createInstance(innerCls, 0);- if (!outer || !inner) return nil;-- // uniqueID:q (int64_t)- Ivar uidIvar = class_getInstanceVariable(innerCls, "uniqueID");- if (uidIvar) {- *(int64_t *)((char *)(__bridge void *)inner + ivar_getOffset(uidIvar)) = uid;- }-- // Set photo + preview dimensions so dimension accessors return our size.- // (Zero structs are technically safe but feed our real dims for realism.)- struct cfx_dims2 { int32_t w, h; };- struct cfx_dims2 photoDim = {1280, 720};- const char *dimNames[] = {"photoDimensions", "previewDimensions", NULL};- for (int i = 0; dimNames[i]; i++) {- Ivar iv = class_getInstanceVariable(innerCls, dimNames[i]);- if (iv) {- *(struct cfx_dims2 *)((char *)(__bridge void *)inner + ivar_getOffset(iv))- = photoDim;- }- }-- // NSArray-typed ivars should be empty arrays, not nil, so AVF callers- // can safely send -count etc. Manually retain so the ivars survive- // past our autorelease pool / ARC scope exit; outer's dealloc will- // balance with a release.- const char *arrayNames[] = {"photoManifest", "digitalFlashUserInterfaceRGBEstimate", NULL};- for (int i = 0; arrayNames[i]; i++) {- Ivar iv = class_getInstanceVariable(innerCls, arrayNames[i]);- if (iv) {- NSArray *empty = @[];- CFRetain((__bridge CFTypeRef)empty);- object_setIvar(inner, iv, empty);- }- }-- // Wire outer._internal = inner. CFRetain inner so ARC can't drop it- // before outer's dealloc gets a chance to release it.- Ivar internalIvar = class_getInstanceVariable(outerCls, "_internal");- if (internalIvar) {- CFRetain((__bridge CFTypeRef)inner);- object_setIvar(outer, internalIvar, inner);- }-- return outer;-}--// We attach the uid via objc_setAssociatedObject before AVCapturePhoto's-// init calls -resolvedSettings on the captureRequest; the stub reads it.-static const void *CFX_ASSOC_UID_KEY = &CFX_ASSOC_UID_KEY;--static id cfx_stub_resolvedSettings(id self, SEL _cmd) {- (void)_cmd;- NSNumber *uidObj = objc_getAssociatedObject(self, CFX_ASSOC_UID_KEY);- int64_t uid = uidObj.longLongValue;- id r = cfx_build_minimal_resolved(uid);- // Log pointer only — calling -description on a half-built obj crashes.- cfxlog(@"[stub resolvedSettings] uid=%lld -> %p", uid, r);- return r;-}--static id cfx_stub_unresolvedSettings(id self, SEL _cmd) {- (void)self; (void)_cmd;- return nil;-}--static BOOL cfx_stub_lensStabSupported(id self, SEL _cmd) {- (void)self; (void)_cmd;- return NO;-}--static void cfx_install_capturerequest_stubs(void) {- Class cls = NSClassFromString(@"CAMStillImageCaptureRequest");- if (!cls) { cfxlog(@"CAMStillImageCaptureRequest missing"); return; }- if (class_addMethod(- cls,- NSSelectorFromString(@"resolvedSettings"),- (IMP)cfx_stub_resolvedSettings,- "@@:")) {- cfxlog(@"stubbed resolvedSettings on CAMStillImageCaptureRequest");- }- if (class_addMethod(- cls,- NSSelectorFromString(@"unresolvedSettings"),- (IMP)cfx_stub_unresolvedSettings,- "@@:")) {- cfxlog(@"stubbed unresolvedSettings on CAMStillImageCaptureRequest");- }- if (class_addMethod(- cls,- NSSelectorFromString(@"lensStabilizationSupported"),- (IMP)cfx_stub_lensStabSupported,- "B@:")) {- cfxlog(@"stubbed lensStabilizationSupported on CAMStillImageCaptureRequest");- }-}--static BOOL cfx_output_is_for_vcam(id self) {- @try {- NSArray *conns = [self valueForKey:@"connections"];- for (AVCaptureConnection *conn in conns) {- for (AVCaptureInputPort *port in conn.inputPorts) {- id input = port.input;- if ([input isKindOfClass:[AVCaptureDeviceInput class]]) {- AVCaptureDevice *d = ((AVCaptureDeviceInput *)input).device;- if ([d.uniqueID isEqualToString:VCAM_UID]) return YES;- }- }- }- } @catch (NSException *e) {- cfxlog(@"connection probe exception: %@", e);- }- return NO;-}--// MARK: - JPEG + IOSurface builders--static NSData *cfx_build_jpeg_from_shm(void) {- CGImageRef img = cfx_build_cgimage_from_shm();- if (!img) return nil;-- NSMutableData *data = [NSMutableData data];- CGImageDestinationRef dest = CGImageDestinationCreateWithData(- (CFMutableDataRef)data,- (CFStringRef)@"public.jpeg",- 1,- NULL);- if (!dest) { CGImageRelease(img); return nil; }- CGImageDestinationAddImage(dest, img, NULL);- BOOL ok = CGImageDestinationFinalize(dest);- CFRelease(dest);- CGImageRelease(img);- if (!ok) return nil;- return data;-}--static CGImageRef cfx_build_cgimage_from_shm(void) CF_RETURNS_RETAINED {- uint32_t w = 0, h = 0;- uint8_t *bgra = cfx_bgra_from_shm(&w, &h);- if (!bgra) return NULL;- size_t len = (size_t)w * 4 * h;- CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();- CGDataProviderRef dp = CGDataProviderCreateWithData(- NULL,- bgra,- len,- cfx_cg_release_data);- CGImageRef img = CGImageCreate(- w,- h,- 8,- 32,- w * 4,- cs,- (CGBitmapInfo)(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst),- dp,- NULL,- false,- kCGRenderingIntentDefault);- CGDataProviderRelease(dp);- CGColorSpaceRelease(cs);- return img;-}--static IOSurfaceRef cfx_build_iosurface_from_shm(uint32_t *outW, uint32_t *outH) CF_RETURNS_RETAINED {- uint32_t w = 0, h = 0;- uint8_t *bgra = cfx_bgra_from_shm(&w, &h);- if (!bgra) return NULL;- size_t len = (size_t)w * 4 * h;- NSDictionary *props = @{- (NSString *)kIOSurfaceWidth: @(w),- (NSString *)kIOSurfaceHeight: @(h),- (NSString *)kIOSurfacePixelFormat: @(kCVPixelFormatType_32BGRA),- (NSString *)kIOSurfaceBytesPerElement: @(4),- (NSString *)kIOSurfaceBytesPerRow: @(w * 4),- (NSString *)kIOSurfaceAllocSize: @(len),- };- IOSurfaceRef surf = IOSurfaceCreate((CFDictionaryRef)props);- if (!surf) { free(bgra); return NULL; }- IOSurfaceLock(surf, 0, NULL);- void *base = IOSurfaceGetBaseAddress(surf);- if (base) memcpy(base, bgra, len);- IOSurfaceUnlock(surf, 0, NULL);- free(bgra);- if (outW) *outW = w;- if (outH) *outH = h;- return surf;-}--// MARK: - Version-agnostic invocation helpers-//-// AVCapturePhoto's designated init and AVCaptureResolvedPhotoSettings'-// factory each take 25+ arguments. The selectors are gigantic strings and-// Apple has been known to add/remove arguments between iOS revisions-// (which silently moves every later arg's positional index). To avoid-// rewriting every libcamfix release against a fixed signature, the-// builders below look up the selector by PREFIX at runtime and fill-// arguments by LABEL — the actual selector component names like-// "photoSurface" or "uniqueID" — so a future arg insertion just gets-// nil/zero in that slot without breaking the rest.--// Walk class_copyMethodList for `cls` (or its metaclass when looking for-// class methods) and return the selector with the most colons that starts-// with `prefix`. Returns NULL if nothing matches.-static SEL cfx_find_selector_by_prefix(- Class cls,- NSString *prefix,- BOOL classMethod) {- if (!cls || !prefix.length) return NULL;- Class c = classMethod ? object_getClass(cls) : cls;- unsigned int n = 0;- Method *methods = class_copyMethodList(c, &n);- if (!methods) return NULL;- SEL best = NULL;- NSUInteger bestArgs = 0;- for (unsigned int i = 0; i < n; i++) {- SEL s = method_getName(methods[i]);- NSString *name = NSStringFromSelector(s);- if (![name hasPrefix:prefix]) continue;- NSUInteger colons = 0;- for (NSUInteger j = 0; j < name.length; j++) {- if ([name characterAtIndex:j] == ':') colons++;- }- if (colons > bestArgs) {- bestArgs = colons;- best = s;- }- }- free(methods);- return best;-}--// Normalize the first selector component to a canonical label. Apple-// init/factory selectors start with "initWithX:" / "resolvedSettingsWithX:";-// the helper-side label dictionary keys are just "X" (camelCase). Strips-// the prefix and lowercases the first character of the remainder.-static NSString *cfx_normalize_first_label(NSString *label) {- for (NSString *prefix in @[@"initWith", @"resolvedSettingsWith"]) {- if ([label hasPrefix:prefix] && label.length > prefix.length) {- NSString *rest = [label substringFromIndex:prefix.length];- return [[[rest substringToIndex:1] lowercaseString]- stringByAppendingString:[rest substringFromIndex:1]];- }- }- return label;-}--// Resolver block: called once per method argument with the canonical label-// (e.g. "photoSurface") and the runtime type encoding. The block decides-// whether and what to write into `outBuf` (already zeroed). If the block-// doesn't recognize the label, leave outBuf alone — nil for objects,-// zeroed bytes for structs/primitives.-typedef void (^cfx_arg_resolver_t)(- NSString *label,- const char *typeEnc,- void *outBuf);--// Build + invoke `[target selector]` with arguments filled by `resolver`.-// Works whether `target` is an alloc'd instance (for inits) or a Class-// (for class-method factories). Returns the result if it's an object-// return type, nil otherwise.-static id cfx_invoke_with_labeled_args(- id target,- SEL selector,- cfx_arg_resolver_t resolver) {- NSMethodSignature *sig = nil;- @try { sig = [target methodSignatureForSelector:selector]; }- @catch (NSException *e) { return nil; }- if (!sig) return nil;-- NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];- [inv setTarget:target];- [inv setSelector:selector];-- NSArray<NSString *> *parts =- [NSStringFromSelector(selector) componentsSeparatedByString:@":"];- NSUInteger argCount = sig.numberOfArguments - 2; // -2 for self + _cmd- for (NSUInteger i = 0; i < argCount; i++) {- NSString *raw = i < parts.count ? parts[i] : @"";- NSString *label = (i == 0) ? cfx_normalize_first_label(raw) : raw;-- const char *typeEnc = [sig getArgumentTypeAtIndex:i + 2];- NSUInteger argSize = 0, argAlign = 0;- NSGetSizeAndAlignment(typeEnc, &argSize, &argAlign);- if (argSize == 0) continue;-- // alloca on arm64 returns a 16-byte-aligned buffer; argSize <= 64 in- // practice for everything Apple uses here (CMTimeRange is 48 bytes).- void *buf = alloca(argSize);- memset(buf, 0, argSize);- if (resolver) resolver(label, typeEnc, buf);- [inv setArgument:buf atIndex:i + 2];- }-- @try {- [inv invoke];- } @catch (NSException *e) {- cfxlog(@"invoke %@ threw: %@", NSStringFromSelector(selector), e);- return nil;- }-- if (sig.methodReturnLength > 0 &&- sig.methodReturnType[0] == '@') {- __unsafe_unretained id result = nil;- [inv getReturnValue:&result];- return result;- }- return nil;-}--// MARK: - AVCapturePhoto / Resolved settings synthesis--static id cfx_build_avcapturephoto_with_request(IOSurfaceRef surf,- uint32_t w, uint32_t h,- id captureRequest) {- Class cls = NSClassFromString(@"AVCapturePhoto");- if (!cls) return nil;- id alloc_obj = [cls alloc];- if (!alloc_obj) return nil;-- // Discover the designated init at runtime. Apple may add/remove args- // between iOS releases; we anchor on the leading components and accept- // whatever the longest matching selector is.- SEL initSel = cfx_find_selector_by_prefix(cls, @"initWithTimestamp:",- /*classMethod*/NO);- if (!initSel) { cfxlog(@"photo: no init selector found"); return nil; }-- CMTime ts = CMClockGetTime(CMClockGetHostTimeClock());- CGSize sz = CGSizeMake((CGFloat)w, (CGFloat)h);- NSString *fileType = @"public.jpeg";- NSDictionary *meta = @{};- NSString *deviceType = @"AVCaptureDeviceTypeBuiltInWideAngleCamera";-- id result = cfx_invoke_with_labeled_args(- alloc_obj, initSel,- ^(NSString *label, const char *typeEnc, void *out) {- if ([label isEqualToString:@"timestamp"]) {- *(CMTime *)out = ts;- } else if ([label isEqualToString:@"photoSurface"]) {- *(IOSurfaceRef *)out = surf;- } else if ([label isEqualToString:@"photoSurfaceSize"]) {- *(CGSize *)out = sz;- } else if ([label isEqualToString:@"processedFileType"]) {- *(__unsafe_unretained id *)out = fileType;- } else if ([label isEqualToString:@"metadata"]) {- *(__unsafe_unretained id *)out = meta;- } else if ([label isEqualToString:@"captureRequest"]) {- *(__unsafe_unretained id *)out = captureRequest;- } else if ([label isEqualToString:@"sequenceCount"]- || [label isEqualToString:@"photoCount"]) {- *(NSInteger *)out = 1;- } else if ([label isEqualToString:@"sourceDeviceType"]) {- *(__unsafe_unretained id *)out = deviceType;- }- // Every other arg (preview/thumbnail/depth/portrait/hair/skin/- // teeth/glasses/constantColor surfaces + their metadata dicts,- // bracketSettings, expectedPhotoProcessingFlags, etc.) stays- // zero/nil — works for both Camera.app (with non-nil- // captureRequest) and standalone AVF clients (nil).- });- if (result) {- cfxlog(@"AVCapturePhoto built: %p (captureRequest=%p, sel=%@)",- result,- captureRequest,- NSStringFromSelector(initSel));- }- return result;-}--// MARK: - fileDataRepresentation / CGImageRepresentation hooks-//-// We tag synthesized photos via objc_setAssociatedObject so the hook-// recognizes "ours" without a global dict. Keys are defined earlier in-// the file alongside cfx_deliver_capturePhoto's forward decls.--static IMP cfx_orig_fileDataRep = NULL;-static IMP cfx_orig_cgImageRep = NULL;--static NSData *cfx_fileDataRep_hook(id self, SEL _cmd) {- NSData *ours = objc_getAssociatedObject(self, CFX_ASSOC_JPEG_KEY);- if (ours) {- cfxlog(@"[fileDataRep] returning vcam jpeg (%lu bytes)", (unsigned long)ours.length);- return ours;- }- typedef NSData *(*OrigFn)(id, SEL);- return ((OrigFn)cfx_orig_fileDataRep)(self, _cmd);-}--static CGImageRef cfx_cgImageRep_hook(id self, SEL _cmd) {- id wrap = objc_getAssociatedObject(self, CFX_ASSOC_CGIMG_KEY);- if (wrap) {- CGImageRef img = (__bridge CGImageRef)wrap;- cfxlog(@"[cgImageRep] returning vcam CGImage");- return img;- }- typedef CGImageRef (*OrigFn)(id, SEL);- return ((OrigFn)cfx_orig_cgImageRep)(self, _cmd);-}--static void cfx_install_photo_representation_hooks(void) {- Class cls = NSClassFromString(@"AVCapturePhoto");- if (!cls) return;- Method m1 = class_getInstanceMethod(cls, @selector(fileDataRepresentation));- Method m2 = class_getInstanceMethod(cls, @selector(CGImageRepresentation));- if (m1) {- cfx_orig_fileDataRep = method_setImplementation(m1, (IMP)cfx_fileDataRep_hook);- cfxlog(@"installed fileDataRepresentation hook");- }- if (m2) {- cfx_orig_cgImageRep = method_setImplementation(m2, (IMP)cfx_cgImageRep_hook);- cfxlog(@"installed CGImageRepresentation hook");- }-}--// MARK: - drive the capture state machine--static void cfx_drive_capture(id output, id delegate, id settings) {- // Extract uniqueID from AVMomentCaptureSettings.- int64_t uid = 0;- @try {- NSNumber *n = [settings valueForKey:@"uniqueID"];- uid = n.longLongValue;- } @catch (NSException *e) {}- cfxlog(@"[drive] uid=%lld settings=%@", uid, [settings class]);-- // Look up the real captureRequest from CAMCaptureEngine's internal- // registry FIRST. When the user tapped the shutter, CAMCaptureEngine- // registered a CAMCaptureRequestInfo keyed by uid (the- // _resultsQueueRegisteredStillImageRequests ivar). Its `request` property- // is the AVCaptureRequest we need to pass as the `captureRequest:` arg- // to the AVCapturePhoto init — without it, CAMCaptureEngine's- // didFinishProcessing handler can't match the photo to a pending- // request and drops it.- id captureRequest = nil;- @try {- id reqDict = [delegate valueForKey:@"_resultsQueueRegisteredStillImageRequests"];- if ([reqDict isKindOfClass:[NSDictionary class]]) {- id info = ((NSDictionary *)reqDict)[@(uid)];- if (info) {- id stillReq = [info valueForKey:@"request"];- cfxlog(@"[drive] CAMStillImageCaptureRequest=%p for uid=%lld",- stillReq, uid);- // Tag the captureRequest with the uid so the resolvedSettings stub- // (which has no parameter context) can recover it.- objc_setAssociatedObject(stillReq, CFX_ASSOC_UID_KEY, @(uid),- OBJC_ASSOCIATION_RETAIN_NONATOMIC);- captureRequest = stillReq;- } else {- cfxlog(@"[drive] no CAMCaptureRequestInfo for uid=%lld; keys=%@",- uid, ((NSDictionary *)reqDict).allKeys);- }- }- } @catch (NSException *e) {- cfxlog(@"[drive] registry lookup threw: %@", e);- }-- // Build IOSurface from shm.- uint32_t w = 0, h = 0;- IOSurfaceRef surf = cfx_build_iosurface_from_shm(&w, &h);- if (!surf || !w || !h) {- cfxlog(@"[drive] no IOSurface — falling back to error finish");- NSError *err = [NSError errorWithDomain:AVFoundationErrorDomain- code:-11800- userInfo:@{NSLocalizedDescriptionKey:@"Unable to take the photo. Try again."}];- SEL finishSel = @selector(captureOutput:didFinishCaptureForResolvedSettings:error:);- if ([delegate respondsToSelector:finishSel]) {- ((void (*)(id, SEL, id, id, id))objc_msgSend)(delegate, finishSel, output, nil, err);- }- if (surf) CFRelease(surf);- return;- }- cfxlog(@"[drive] IOSurface %ux%u built", w, h);-- // Build AVCapturePhoto WITH the real captureRequest.- id photo = cfx_build_avcapturephoto_with_request(surf, w, h, captureRequest);- CFRelease(surf); // photo retains it- if (!photo) {- cfxlog(@"[drive] no AVCapturePhoto — abort");- return;- }-- // Stamp it with our JPEG so fileDataRepresentation returns our bytes.- NSData *jpeg = cfx_stamp_photo(photo);- cfxlog(@"[drive] photo stamped jpeg=%lu bytes", (unsigned long)jpeg.length);-- // Fire the FULL standard delegate sequence. CAMCaptureEngine tracks- // a _receivedCallbacks set per uid; until ALL expected callbacks- // arrive, the request stays in- // _resultsQueueRegisteredStillImageRequests and the engine refuses- // to start new captures (manifests as "shutter stops working after- // N taps"). We now have a real resolvedSettings, so we can fire the- // will/did pre-callbacks too.- id resolvedForFinish = nil;- @try {- resolvedForFinish = [photo valueForKey:@"resolvedSettings"];- } @catch (NSException *e) {- cfxlog(@"[drive] photo.resolvedSettings threw: %@", e);- }- SEL S1 = @selector(captureOutput:willBeginCaptureBeforeResolvingSettingsForUniqueID:);- SEL S2 = @selector(captureOutput:willBeginCaptureForResolvedSettings:);- SEL S3 = @selector(captureOutput:willCapturePhotoForResolvedSettings:);- SEL S4 = @selector(captureOutput:didCapturePhotoForResolvedSettings:);- SEL S5 = @selector(captureOutput:didFinishProcessingPhoto:error:);- SEL S6 = @selector(captureOutput:didFinishCaptureForResolvedSettings:error:);- SEL Sfinish = NSSelectorFromString(@"_didFinishStillImageCaptureForUniqueID:error:");- if ([delegate respondsToSelector:S1]) {- cfxlog(@"[drive] -> willBeginCaptureBefore… uid=%lld", uid);- ((void (*)(id, SEL, id, int64_t))objc_msgSend)(delegate, S1, output, uid);- }- if (resolvedForFinish && [delegate respondsToSelector:S2]) {- cfxlog(@"[drive] -> willBeginCaptureForResolvedSettings:");- ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, S2, output, resolvedForFinish);- }- if (resolvedForFinish && [delegate respondsToSelector:S3]) {- cfxlog(@"[drive] -> willCapturePhotoForResolvedSettings:");- ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, S3, output, resolvedForFinish);- }- if (resolvedForFinish && [delegate respondsToSelector:S4]) {- cfxlog(@"[drive] -> didCapturePhotoForResolvedSettings:");- ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, S4, output, resolvedForFinish);- }- if ([delegate respondsToSelector:S5]) {- cfxlog(@"[drive] -> didFinishProcessingPhoto:error:");- ((void (*)(id, SEL, id, id, id))objc_msgSend)(delegate, S5, output, photo, nil);- }- if ([delegate respondsToSelector:S6]) {- cfxlog(@"[drive] -> didFinishCapture resolved=%p", resolvedForFinish);- ((void (*)(id, SEL, id, id, id))objc_msgSend)(- delegate, S6, output, resolvedForFinish, nil);- }- if ([delegate respondsToSelector:Sfinish]) {- cfxlog(@"[drive] -> _didFinishStillImageCaptureForUniqueID:%lld", uid);- ((void (*)(id, SEL, int64_t, id))objc_msgSend)(delegate, Sfinish, uid, nil);- }-- // Signal that the output is ready for the NEXT capture request. AVF's- // photo output is a 2-deep pipeline — Camera.app's shutter stays- // disabled until this fires for each in-flight capture.- SEL Sready = NSSelectorFromString(@"captureOutput:readyForResponsiveRequestAfterResolvedSettings:");- if (resolvedForFinish && [delegate respondsToSelector:Sready]) {- cfxlog(@"[drive] -> readyForResponsiveRequestAfterResolvedSettings:");- ((void (*)(id, SEL, id, id))objc_msgSend)(delegate, Sready, output, resolvedForFinish);- }-- cfxlog(@"[drive] sequence complete");-}--// We stash (delegate, settings) on the AVCapturePhotoOutput at begin time-// so the commit hook can drive the capture once CAMCaptureEngine has-// registered the request in its internal dict.-static const void *CFX_ASSOC_DELEGATE_KEY = &CFX_ASSOC_DELEGATE_KEY;-static const void *CFX_ASSOC_SETTINGS_KEY = &CFX_ASSOC_SETTINGS_KEY;--static void cfx_beginMomentCapture_hook(id self, SEL _cmd, id settings, id delegate) {- BOOL forVcam = cfx_output_is_for_vcam(self);- cfxlog(@"[beginMomentCapture] forVcam=%d delegate=%@", forVcam, delegate);- if (!forVcam) {- typedef void (*OrigFn)(id, SEL, id, id);- ((OrigFn)cfx_orig_beginMomentCapture)(self, _cmd, settings, delegate);- return;- }- // SKIP orig — it throws for our session. Just stash the delegate +- // settings; commit will drive the photo delivery once CAMCaptureEngine- // has registered the captureRequest internally.- objc_setAssociatedObject(self, CFX_ASSOC_DELEGATE_KEY, delegate,- OBJC_ASSOCIATION_RETAIN_NONATOMIC);- objc_setAssociatedObject(self, CFX_ASSOC_SETTINGS_KEY, settings,- OBJC_ASSOCIATION_RETAIN_NONATOMIC);-}--// NOTE: uniqueID is a pointer-sized integer (not an NSUUID *). Declaring-// it as `id` would cause ARC to retain it on entry, dereferencing the-// integer-as-isa and segfaulting. Use intptr_t to skip the retain.--static void cfx_commitMomentCapture_hook(id self, SEL _cmd, intptr_t uniqueID) {- BOOL forVcam = cfx_output_is_for_vcam(self);- cfxlog(@"[commitMomentCapture] forVcam=%d uniqueID=%ld", forVcam, (long)uniqueID);- if (!forVcam) {- typedef void (*OrigFn)(id, SEL, intptr_t);- ((OrigFn)cfx_orig_commitMomentCapture)(self, _cmd, uniqueID);- return;- }- // SKIP orig (would throw) and drive the synthesized photo delivery- // here, where CAMCaptureEngine has already registered its- // CAMCaptureRequestInfo for this uid.- id delegate = objc_getAssociatedObject(self, CFX_ASSOC_DELEGATE_KEY);- id settings = objc_getAssociatedObject(self, CFX_ASSOC_SETTINGS_KEY);- if (!delegate) {- cfxlog(@"[commit] no stashed delegate — was begin called?");- return;- }- __strong id retainedSelf = self;- __strong id retainedDelegate = delegate;- __strong id retainedSettings = settings;- dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{- @autoreleasepool {- cfx_drive_capture(retainedSelf, retainedDelegate, retainedSettings);- }- });-}--static void cfx_cancelMomentCapture_hook(id self, SEL _cmd, intptr_t uniqueID) {- BOOL forVcam = cfx_output_is_for_vcam(self);- cfxlog(@"[cancelMomentCapture] forVcam=%d uniqueID=0x%lx", forVcam, (long)uniqueID);- if (!forVcam) {- typedef void (*OrigFn)(id, SEL, intptr_t);- ((OrigFn)cfx_orig_cancelMomentCapture)(self, _cmd, uniqueID);- return;- }- // No-op for vcam — orig throws (the begin was a no-op so there is no- // live moment to cancel). CAMCaptureEngine drives this when it decides- // the capture is stale.-}--static void cfx_install_moment_capture_hooks(void) {- Class cls = NSClassFromString(@"AVCapturePhotoOutput");- if (!cls) return;- SEL b = @selector(beginMomentCaptureWithSettings:delegate:);- SEL c = NSSelectorFromString(@"commitMomentCaptureToPhotoWithUniqueID:");- SEL x = NSSelectorFromString(@"cancelMomentCaptureWithUniqueID:");- Method mb = class_getInstanceMethod(cls, b);- Method mc = class_getInstanceMethod(cls, c);- Method mx = class_getInstanceMethod(cls, x);- if (mb) {- cfx_orig_beginMomentCapture =- method_setImplementation(mb, (IMP)cfx_beginMomentCapture_hook);- cfxlog(@"installed beginMomentCapture hook");- }- if (mc) {- cfx_orig_commitMomentCapture =- method_setImplementation(mc, (IMP)cfx_commitMomentCapture_hook);- cfxlog(@"installed commitMomentCapture hook");- }- if (mx) {- cfx_orig_cancelMomentCapture =- method_setImplementation(mx, (IMP)cfx_cancelMomentCapture_hook);- cfxlog(@"installed cancelMomentCapture hook");- }-}--// MARK: - AVCaptureSession state guards-//-// After ~4-5s of no real sample buffer flow on the AVCaptureSession's-// preview connection, Camera.app's session transitions to-// "interrupted" / not-running and disables the shutter. We bypass the-// daemon's preview pipeline (we feed CALayer.contents directly), so-// no sample buffers ever flow.-//-// Two guards on AVCaptureSession for our vcam-bound sessions:-// 1. -[AVCaptureSession _setRunning:NO] → swallow (stay running)-// 2. -[AVCaptureSession _setInterrupted:YES withReason:interruptor:] → swallow--static IMP cfx_orig_setRunning = NULL;-static IMP cfx_orig_setInterrupted = NULL;--// Diagnoses why a vcam session never reaches the daemon's graph builder:-// logs what the client thinks its session looks like at start time.-static void cfx_log_session_start(AVCaptureSession *session) {- @try {- NSMutableArray *lines = [NSMutableArray array];- [lines addObject:[NSString stringWithFormat:@"inputs=%lu",- (unsigned long)session.inputs.count]];- for (AVCaptureInput *inp in session.inputs) {- NSString *uid = nil;- if ([inp isKindOfClass:[AVCaptureDeviceInput class]]) {- uid = ((AVCaptureDeviceInput *)inp).device.uniqueID;- }- [lines addObject:[NSString stringWithFormat:@"input %@ cls=%@ ports=%lu",- uid ?: @"?",- NSStringFromClass([inp class]),- (unsigned long)inp.ports.count]];- }- for (AVCaptureOutput *outp in session.outputs) {- BOOL anyEnabled = NO;- NSUInteger videoConns = 0;- for (AVCaptureConnection *c in outp.connections) {- if (c.enabled || c.active) anyEnabled = YES;- for (AVCaptureInputPort *port in c.inputPorts) {- if ([port.mediaType isEqualToString:AVMediaTypeVideo]) {- videoConns++;- break;- }- }- }- [lines addObject:[NSString stringWithFormat:@"output %@ conns=%lu video=%lu anyEnabled/Active=%d",- NSStringFromClass([outp class]),- (unsigned long)outp.connections.count,- (unsigned long)videoConns,- anyEnabled]];- }- cfxlog(@"[session start] %p %@", session, [lines componentsJoinedByString:@" | "]);- } @catch (NSException *e) {- cfxlog(@"[session start] dump exception: %@", e);- }-}--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);- }- if (!running && cfx_session_is_for_vcam(self)) {- cfxlog(@"[session _setRunning:NO] suppressed for vcam session %p", self);- return;- }- typedef void (*OrigFn)(id, SEL, BOOL);- ((OrigFn)cfx_orig_setRunning)(self, _cmd, running);-}--static void cfx_session_setInterrupted_hook(id self, SEL _cmd,- BOOL interrupted,- long reason,- id interruptor) {- if (interrupted && cfx_session_is_for_vcam(self)) {- cfxlog(@"[session _setInterrupted:YES reason=%ld] suppressed for vcam session %p",- reason, self);- return;- }- typedef void (*OrigFn)(id, SEL, BOOL, long, id);- ((OrigFn)cfx_orig_setInterrupted)(self, _cmd, interrupted, reason, interruptor);-}--// MARK: - device input / port diagnostics-//-// A vcam session that never reaches startRunning usually dies at addInput:-// because the client-side device object has no ports (streams missing from-// the remote source copy). Log the port count at input creation and the-// app's own canAddInput: checks.--static IMP cfx_orig_dvinput_init = NULL;-static IMP cfx_orig_canAddInput = NULL;--__attribute__((ns_returns_retained))-static id cfx_dvinput_init_hook(id self, SEL _cmd, AVCaptureDevice *device, NSError **err) {- typedef id (*Fn)(id, SEL, AVCaptureDevice *, NSError **);- id ret = ((Fn)cfx_orig_dvinput_init)(self, _cmd, device, err);- if ([device.uniqueID isEqualToString:VCAM_UID]) {- cfxlog(@"[DeviceInput init] device=%@ ports=%lu err=%@",- device.uniqueID,- (unsigned long)((AVCaptureDeviceInput *)ret).ports.count,- err && *err ? *err : nil);- }- return ret;-}--static BOOL cfx_canAddInput_hook(id self, SEL _cmd, AVCaptureInput *input) {- typedef BOOL (*Fn)(id, SEL, AVCaptureInput *);- BOOL ret = ((Fn)cfx_orig_canAddInput)(self, _cmd, input);- if ([input isKindOfClass:[AVCaptureDeviceInput class]] &&- [((AVCaptureDeviceInput *)input).device.uniqueID isEqualToString:VCAM_UID]) {- cfxlog(@"[canAddInput:%@] -> %d", VCAM_UID, ret);- }- return ret;-}--static void cfx_install_input_diagnostics(void) {- Class cls = NSClassFromString(@"AVCaptureDeviceInput");- Method m = cls ? class_getInstanceMethod(cls, @selector(initWithDevice:error:)) : NULL;- if (m) {- cfx_orig_dvinput_init = method_setImplementation(m, (IMP)cfx_dvinput_init_hook);- cfxlog(@"installed DeviceInput init diag");- }- Class sessCls = NSClassFromString(@"AVCaptureSession");- Method m2 = sessCls ? class_getInstanceMethod(sessCls, @selector(canAddInput:)) : NULL;- if (m2) {- cfx_orig_canAddInput = method_setImplementation(m2, (IMP)cfx_canAddInput_hook);- cfxlog(@"installed canAddInput diag");- }-}--static void cfx_install_session_guards(void) {- Class cls = NSClassFromString(@"AVCaptureSession");- if (!cls) { cfxlog(@"AVCaptureSession missing"); return; }- SEL s1 = NSSelectorFromString(@"_setRunning:");- SEL s2 = NSSelectorFromString(@"_setInterrupted:withReason:interruptor:");- Method m1 = class_getInstanceMethod(cls, s1);- Method m2 = class_getInstanceMethod(cls, s2);- if (m1) {- cfx_orig_setRunning = method_setImplementation(m1, (IMP)cfx_session_setRunning_hook);- cfxlog(@"installed _setRunning: hook");- } else {- cfxlog(@"_setRunning: not in objc table");- }- if (m2) {- cfx_orig_setInterrupted = method_setImplementation(m2, (IMP)cfx_session_setInterrupted_hook);- cfxlog(@"installed _setInterrupted: hook");- } else {- cfxlog(@"_setInterrupted: not in objc table");- }-}--// MARK: - AVCaptureSession state GETTER lies-//-// If Camera.app polls -isRunning / -isInterrupted in its UI loop and-// reacts to a transition (no-frames timer or similar), we can lie to-// keep the UI in "live preview" mode.--static IMP cfx_orig_isRunning = NULL;-static IMP cfx_orig_isInterrupted = NULL;-static int cfx_isRunning_logged = 0;-static int cfx_isInterrupted_logged = 0;--static BOOL cfx_session_isRunning_hook(id self, SEL _cmd) {- typedef BOOL (*Fn)(id, SEL);- BOOL real = ((Fn)cfx_orig_isRunning)(self, _cmd);- if (cfx_session_is_for_vcam(self)) {- cfx_track_vcam_session(self);- if (cfx_isRunning_logged < 3) {- cfxlog(@"[isRunning] real=%d -> forcing YES (session=%p)", real, self);- cfx_isRunning_logged++;- }- return YES;- }- return real;-}--static BOOL cfx_session_isInterrupted_hook(id self, SEL _cmd) {- typedef BOOL (*Fn)(id, SEL);- BOOL real = ((Fn)cfx_orig_isInterrupted)(self, _cmd);- if (cfx_session_is_for_vcam(self) && real) {- if (cfx_isInterrupted_logged < 3) {- cfxlog(@"[isInterrupted] real=%d -> forcing NO (session=%p)", real, self);- cfx_isInterrupted_logged++;- }- return NO;- }- return real;-}--static void cfx_install_session_state_lies(void) {- Class cls = NSClassFromString(@"AVCaptureSession");- if (!cls) return;- Method r = class_getInstanceMethod(cls, @selector(isRunning));- Method i = class_getInstanceMethod(cls, @selector(isInterrupted));- if (r) {- cfx_orig_isRunning = method_setImplementation(r, (IMP)cfx_session_isRunning_hook);- cfxlog(@"installed isRunning lie");- }- if (i) {- cfx_orig_isInterrupted = method_setImplementation(i, (IMP)cfx_session_isInterrupted_hook);- cfxlog(@"installed isInterrupted lie");- }-}// When libcamfix is loaded as an LC_LOAD_DYLIB dependency of AVFoundation// (via the DSC patch cfw_patch_avf_load_dylib.py), our constructor firesdiff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefileindex b564a9b4..e76fc1b8 100644--- a/VPhoneGuestComponents/Makefile+++ b/VPhoneGuestComponents/Makefile@@ -96,8 +96,9 @@ test-loader-links:test-vcam-dataplane:@mkdir -p $(OUT)xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -Wno-unused-parameter \+ $(addprefix -I,$(VCAM_SHARED_DIRS)) \-o $(OUT)/VCamDataPlaneTests \- Tests/VCamDataPlaneTests.c VCamDataPlane/vcam_dataplane.c \+ Tests/VCamDataPlaneTests.c $(call find_sources,$(VCAM_SHARED_DIRS)) \-framework CoreMedia -framework CoreVideo -framework CoreFoundation \-framework CoreGraphics@$(OUT)/VCamDataPlaneTestsdiff --git a/VPhoneGuestComponents/Tests/VCamDataPlaneTests.c b/VPhoneGuestComponents/Tests/VCamDataPlaneTests.cindex ad372122..ff2d2b72 100644--- a/VPhoneGuestComponents/Tests/VCamDataPlaneTests.c+++ b/VPhoneGuestComponents/Tests/VCamDataPlaneTests.c@@ -24,7 +24,7 @@* Build/run: make -C VPhoneGuestComponents test-vcam-dataplane*/-#include "../VCamDataPlane/vcam_dataplane.h"+#include "vcam_dataplane.h"#include <math.h>#include <stdio.h>diff --git a/VPhoneGuestComponents/VCamCaptured/VCamFrameReceiver.inc b/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrameReceiver.msimilarity index 89%rename from VPhoneGuestComponents/VCamCaptured/VCamFrameReceiver.increname to VPhoneGuestComponents/VCamCaptured/Frames/VCamFrameReceiver.mindex c966661d..aa420008 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamFrameReceiver.inc+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrameReceiver.m@@ -1,10 +1,19 @@-// MARK: - shared-frame reader+#include <notify.h>+#include <stdatomic.h>+#include <sys/mman.h>+#include <sys/stat.h>++#include "VCamFrames.h"+#include "vcam_dataplane.h"-// The latest frame copied out of the vphoned-published shm. Mutated only-// from the reader callback thread; readers (AVF stream production, next-// stage) take the lock for the brief duration of a copy.-// (struct vcc_latest_frame_s is forward-declared above as vcc_latest_frame_t-// for use by the viewfinder injection code.)+// MARK: - shared-frame reader+//+// cameracaptured's sandbox blocks AF_VSOCK socket creation, so the frame+// receiver lives in vphoned (root, has vsock perms). vphoned writes+// frames into a shared mmap and posts a Darwin notification; we map the+// file read-only, subscribe to the notification, and copy out the latest+// frame on each fire. The layout is VCamFrameProtocol.h, shared with+// vphoned_vcam.h.vcc_latest_frame_t vcc_latest_frame = {.lock = PTHREAD_MUTEX_INITIALIZER,@@ -110,7 +119,7 @@ static int vcc_shm_read_latest(void) {return 1;}-static void vcc_start_frame_receiver(void) {+void vcc_start_frame_receiver(void) {// Subscribe before mapping: cameracaptured may start before vphoned has// created the file. The first frame notification retries the map.int token = -1;diff --git a/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.h b/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.hnew file mode 100644index 00000000..1db1ee47--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.h@@ -0,0 +1,35 @@+#ifndef VCAM_FRAMES_H+#define VCAM_FRAMES_H++// The latest frame vphoned published, and the CMSampleBuffers built from+// it for every sink this dylib drives.++#include "VCamCapturedPrivate.h"++#pragma GCC visibility push(hidden)++// The latest frame copied out of the vphoned-published shm. Written only by+// the frame receiver's queue (VCamFrameReceiver.m); readers take the lock+// for the brief duration of a copy.+typedef struct vcc_latest_frame_s {+ pthread_mutex_t lock;+ uint32_t width;+ uint32_t height;+ uint32_t bytes_per_row;+ uint32_t pixel_format;+ uint64_t timestamp_ns;+ uint64_t frame_index;+ uint8_t *pixels;+ size_t pixels_capacity;+ size_t pixels_length;+} vcc_latest_frame_t;++extern vcc_latest_frame_t vcc_latest_frame;++// A CMSampleBuffer wrapping the latest frame in fmt_out (VCC_FMT_*), or+// NULL when no frame has arrived. Caller must CFRelease.+CMSampleBufferRef vcc_build_cmsb_from_shm_fmt(uint32_t fmt_out) CF_RETURNS_RETAINED;++#pragma GCC visibility pop++#endifdiff --git a/VPhoneGuestComponents/VCamCaptured/VCamViewfinderHooks.inc b/VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.msimilarity index 80%rename from VPhoneGuestComponents/VCamCaptured/VCamViewfinderHooks.increname to VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.mindex 17b269b9..6e67e708 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamViewfinderHooks.inc+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.m@@ -1,3 +1,9 @@+#include <stdatomic.h>++#include "VCamFrames.h"+#include "VCamHooks.h"+#include "vcam_dataplane.h"+// MARK: - viewfinder stream injection//// Camera.app's preview path goes through FigCameraViewfinderStream, NOT@@ -8,26 +14,14 @@// capture each instance, and drive enqueueVideoSampleBuffer: ourselves on// a 30 Hz timer, wrapping vcc_latest_frame pixels in a fresh CMSampleBuffer.-// Forward declaration — full definition is later in the file under the-// "shared-frame reader" section.-typedef struct vcc_latest_frame_s {- pthread_mutex_t lock;- uint32_t width;- uint32_t height;- uint32_t bytes_per_row;- uint32_t pixel_format;- uint64_t timestamp_ns;- uint64_t frame_index;- uint8_t *pixels;- size_t pixels_capacity;- size_t pixels_length;-} vcc_latest_frame_t;-extern vcc_latest_frame_t vcc_latest_frame;-static IMP vcc_vfs_init_orig = NULL;static IMP vcc_vfs_open_orig = NULL;static IMP vcc_vfs_close_orig = NULL;-static NSMutableArray *vcc_vf_streams = nil; // strong refs+// Strong refs, from open to close. The open/close hooks run on daemon+// threads while the drive timer reads on its own queue, so every access+// holds the lock.+static NSMutableArray *vcc_vf_streams = nil;+static pthread_mutex_t vcc_vf_streams_lock = PTHREAD_MUTEX_INITIALIZER;static dispatch_source_t vcc_vf_timer = NULL;static dispatch_queue_t vcc_vf_q = NULL;static uint64_t vcc_vf_enqueue_count = 0;@@ -35,8 +29,6 @@ static uint64_t vcc_vf_enqueue_success = 0;static uint64_t vcc_sink_drive_count = 0;static uint64_t vcc_sink_drive_ok = 0;-static CMSampleBufferRef vcc_build_cmsb_from_shm_fmt(uint32_t fmt_out);-// Deliver the latest shm frame to every captured video sink — the// client-graph tail. Driving them with our samples is what delivers frames// to third-party AVCaptureVideoDataOutput clients, the same way the@@ -96,15 +88,19 @@ static void vcc_vfs_open_hook(id self, SEL _cmd, id dest) {vcc_log(@" [VFS open] self=%p dest=%@", self, dest);VccVfsOpenFn orig = (VccVfsOpenFn)vcc_vfs_open_orig;orig(self, _cmd, dest);+ pthread_mutex_lock(&vcc_vf_streams_lock);if (!vcc_vf_streams) vcc_vf_streams = [NSMutableArray array];[vcc_vf_streams addObject:self];- vcc_log(@" [VFS open] captured stream %p (total=%lu)",- self, (unsigned long)vcc_vf_streams.count);+ NSUInteger total = vcc_vf_streams.count;+ pthread_mutex_unlock(&vcc_vf_streams_lock);+ vcc_log(@" [VFS open] captured stream %p (total=%lu)", self, (unsigned long)total);}static void vcc_vfs_close_hook(id self, SEL _cmd) {vcc_log(@" [VFS close] self=%p", self);- if (vcc_vf_streams) [vcc_vf_streams removeObject:self];+ pthread_mutex_lock(&vcc_vf_streams_lock);+ [vcc_vf_streams removeObject:self];+ pthread_mutex_unlock(&vcc_vf_streams_lock);VccVfsCloseFn orig = (VccVfsCloseFn)vcc_vfs_close_orig;orig(self, _cmd);}@@ -126,7 +122,7 @@ static void vcc_delivery_timing_init(void) {vcc_timing_init(&vcc_delivery_timing);}-static CMSampleBufferRef vcc_build_cmsb_from_shm_fmt(uint32_t fmt_out) {+CMSampleBufferRef vcc_build_cmsb_from_shm_fmt(uint32_t fmt_out) {pthread_mutex_lock(&vcc_delivery_lock);pthread_once(&vcc_delivery_timing_once, vcc_delivery_timing_init);@@ -173,11 +169,14 @@ static CMSampleBufferRef vcc_build_cmsb_from_shm(void) {}static void vcc_vf_drive_once(void) {- if (!vcc_vf_streams || vcc_vf_streams.count == 0) return;+ pthread_mutex_lock(&vcc_vf_streams_lock);+ NSArray *streams = vcc_vf_streams ? [vcc_vf_streams copy] : @[];+ pthread_mutex_unlock(&vcc_vf_streams_lock);+ if (streams.count == 0) return;CMSampleBufferRef cmsb = vcc_build_cmsb_from_shm();if (!cmsb) return;SEL sel = NSSelectorFromString(@"enqueueVideoSampleBuffer:");- for (id stream in [vcc_vf_streams copy]) {+ for (id stream in streams) {int ret = ((int (*)(id, SEL, CMSampleBufferRef))objc_msgSend)(stream, sel, cmsb);vcc_vf_enqueue_count++;@@ -192,7 +191,7 @@ static void vcc_vf_drive_once(void) {CFRelease(cmsb);}-static void vcc_install_viewfinder_hooks(void) {+void vcc_install_viewfinder_hooks(void) {Class cls = NSClassFromString(@"FigCameraViewfinderStream");if (!cls) {vcc_log(@" VF hook: class missing");@@ -232,28 +231,3 @@ static void vcc_install_viewfinder_hooks(void) {vcc_log(@" viewfinder drive timer armed (30 Hz, delivering '420v' "@"+ camera metadata)");}--static void vcc_install_sink_observation(void) {- Class iqsn = NSClassFromString(@"BWImageQueueSinkNode");- Class rqsn = NSClassFromString(@"BWRemoteQueueSinkNode");- if (!iqsn || !rqsn) {- vcc_log(@" sink obs: classes missing iqsn=%p rqsn=%p", iqsn, rqsn);- return;- }-- SEL iqsnInit = NSSelectorFromString(- @"initWithHFRSupport:ispJitterCompensationEnabled:"- @"clientAuditToken:sinkID:");- SEL rqsnInit = NSSelectorFromString(- @"initWithMediaType:clientAuditToken:sinkID:cameraInfoByPortType:");- SEL renderSel = @selector(renderSampleBuffer:forInput:);-- vcc_swizzle_method(iqsn, iqsnInit, (IMP)vcc_iqsn_init_hook,- &vcc_iqsn_init_orig);- vcc_swizzle_method(rqsn, rqsnInit, (IMP)vcc_rqsn_init_hook,- &vcc_rqsn_init_orig);- vcc_swizzle_method(iqsn, renderSel, (IMP)vcc_iqsn_render_hook,- &vcc_iqsn_render_orig);- vcc_swizzle_method(rqsn, renderSel, (IMP)vcc_rqsn_render_hook,- &vcc_rqsn_render_orig);-}diff --git a/VPhoneGuestComponents/VCamCaptured/Hooks/VCamHooks.h b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamHooks.hnew file mode 100644index 00000000..9765abcf--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamHooks.h@@ -0,0 +1,16 @@+#ifndef VCAM_HOOKS_H+#define VCAM_HOOKS_H++// Observation and delivery hooks on the daemon's capture graph classes.++#include "VCamCapturedPrivate.h"++#pragma GCC visibility push(hidden)++// Every video sink node (BWImageQueueSinkNode / BWRemoteQueueSinkNode)+// created so far, as an immutable snapshot the viewfinder drive delivers to.+NSArray *vcc_driven_sinks_snapshot(void);++#pragma GCC visibility pop++#endifdiff --git a/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSessionHooks.m b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSessionHooks.mnew file mode 100644index 00000000..090d5267--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSessionHooks.m@@ -0,0 +1,196 @@+#include <ptrauth.h>++#include "VCamHooks.h"+#include "VCamImage.h"++// MARK: - BWFigCaptureSession graph callback observation+//+// AVCaptureSession.startRunning → cameracaptured graph build → start → these+// delegate callbacks fire on BWFigCaptureSession. If start fails (error != 0),+// the daemon reports failure back over XPC and client.isRunning stays NO.+// Hook these to see exactly where in the graph startup the failure occurs.++static IMP vcc_sess_didFinishStarting_orig = NULL;+static IMP vcc_sess_didStartSourceNode_orig = NULL;+static IMP vcc_sess_didPrepare_orig = NULL;++typedef void (*VccSessDidFinishStartingFn)(id self, SEL _cmd, id graph, int error);+typedef void (*VccSessDidStartSourceFn)(id self, SEL _cmd, id graph, id node, int error);+typedef void (*VccSessDidPrepareFn)(id self, SEL _cmd, id graph);++static void vcc_sess_didFinishStarting_hook(id self,+ SEL _cmd,+ id graph,+ int error) {+ vcc_log(@" [Sess graph:didFinishStartingWithError:] self=%p graph=%p err=%d",+ self, graph, error);+ VccSessDidFinishStartingFn orig =+ (VccSessDidFinishStartingFn)vcc_sess_didFinishStarting_orig;+ orig(self, _cmd, graph, error);+}++static void vcc_sess_didStartSourceNode_hook(id self,+ SEL _cmd,+ id graph,+ id node,+ int error) {+ vcc_log(@" [Sess graph:didStartSourceNode:error:] self=%p graph=%p node=%@ err=%d",+ self, graph, node, error);+ VccSessDidStartSourceFn orig =+ (VccSessDidStartSourceFn)vcc_sess_didStartSourceNode_orig;+ orig(self, _cmd, graph, node, error);+}++static void vcc_sess_didPrepare_hook(id self, SEL _cmd, id graph) {+ vcc_log(@" [Sess graphDidPrepareNodes:] self=%p graph=%p", self, graph);+ VccSessDidPrepareFn orig =+ (VccSessDidPrepareFn)vcc_sess_didPrepare_orig;+ orig(self, _cmd, graph);+}++void vcc_install_session_graph_observation(void) {+ Class cls = NSClassFromString(@"BWFigCaptureSession");+ if (!cls) {+ vcc_log(@" session-graph obs: class missing");+ return;+ }+ vcc_swizzle_method(cls,+ @selector(graph:didFinishStartingWithError:),+ (IMP)vcc_sess_didFinishStarting_hook,+ &vcc_sess_didFinishStarting_orig);+ vcc_swizzle_method(cls,+ @selector(graph:didStartSourceNode:error:),+ (IMP)vcc_sess_didStartSourceNode_hook,+ &vcc_sess_didStartSourceNode_orig);+ vcc_swizzle_method(cls,+ @selector(graphDidPrepareNodes:),+ (IMP)vcc_sess_didPrepare_hook,+ &vcc_sess_didPrepare_orig);+}++// MARK: - BWFigCaptureSession init capture + pipelines extraction+//+// To splice our manual sink into a real session, we need a handle to its+// FigCaptureSessionPipelines instance. Hook the session's init and, post-orig,+// reach into the _pipelines ivar via KVC. Store globally so the next-stage+// injector has a live reference.++static IMP vcc_sess_initWithFigSess_orig = NULL;+typedef id (*VccSessInitWithFigSessFn)(id self, SEL _cmd, void *figSess);++static id vcc_captured_bw_session = nil; // strong ref to the BWFigCaptureSession+static id vcc_captured_pipelines = nil; // its _pipelines ivar value++__attribute__((ns_returns_retained))+static id vcc_sess_initWithFigSess_hook(id self, SEL _cmd, void *figSess) {+ VccSessInitWithFigSessFn orig =+ (VccSessInitWithFigSessFn)vcc_sess_initWithFigSess_orig;+ id ret = orig(self, _cmd, figSess);+ if (ret) {+ vcc_captured_bw_session = ret;+ @try {+ vcc_captured_pipelines = [ret valueForKey:@"pipelines"];+ } @catch (NSException *e) {+ @try {+ vcc_captured_pipelines = [ret valueForKey:@"_pipelines"];+ } @catch (NSException *e2) {+ vcc_captured_pipelines = nil;+ }+ }+ vcc_log(@" [BWFigCaptureSession init] -> self=%p figSess=%p _pipelines=%p (class=%@)",+ ret, figSess, vcc_captured_pipelines,+ NSStringFromClass([vcc_captured_pipelines class]));+ }+ return ret;+}++void vcc_install_session_init_capture(void) {+ Class cls = NSClassFromString(@"BWFigCaptureSession");+ if (!cls) {+ vcc_log(@" session-init capture: class missing");+ return;+ }+ vcc_swizzle_method(cls,+ @selector(initWithFigCaptureSession:),+ (IMP)vcc_sess_initWithFigSess_hook,+ &vcc_sess_initWithFigSess_orig);+}++// MARK: - graph-build error suppressors+//+// captureSession_buildGraphWithConfiguration's per-source iteration bails+// to OSStatus -12783 along three known paths on our synth source:+//+// (1) -[FigCaptureCameraSourcePipeline requiresMasterClock] returns YES,+// and the post-iteration check (OR of all pipelines' returns) bails+// on a virtual source that has no real ISP/HW clock.+// (2) _cs_addObjectToStreamsAttributes returns -12783 when an inherited+// ivar slot is NULL (called from _FigVideoCaptureSourcesActivate-+// AndCreateDevices during session activation).+// (3) -[BWFigVideoCaptureStream initWithCaptureStream:…] sets *errOut+// = -12783 in its validation-failure path.+//+// (1) is neutralized by rewriting the function's prologue to `mov w0, #0; ret`.+// The function's address is resolved from CMCapture's LC_SYMTAB by name —+// no hardcoded VMA.+//+// (2)+(3) are neutralized by scanning CMCapture's __text for the specific+// MOVN encodings the compiler used to prepare -12783 in w20 / w8, and+// rewriting each to `MOVZ w?, #0`. -12783 is a capture-specific OSStatus+// emitted only by the daemon; over-application is benign because the+// daemon's only consumer in the VM is our synth.++void vcc_install_csp_requires_master_clock_hook(void) {+ vcc_image_t img;+ if (vcc_image_resolve(&img, "FigCaptureSourceServerStart") != 0) {+ vcc_log(@" graph-build patches: image resolve failed");+ return;+ }++ // (1) Resolve -[FigCaptureCameraSourcePipeline requiresMasterClock] via+ // LC_SYMTAB. The function is called via direct C dispatch (not in the+ // objc method table on observed builds, so swizzling won't work), so+ // we rewrite its prologue in place. The PAC-signed address from the+ // symtab is stripped before the write.+ static const char *const kReqMCSyms[] = {+ "_-[FigCaptureCameraSourcePipeline requiresMasterClock]",+ "-[FigCaptureCameraSourcePipeline requiresMasterClock]",+ NULL,+ };+ uintptr_t reqMC = 0;+ for (unsigned i = 0; kReqMCSyms[i]; i++) {+ reqMC = vcc_lookup_lc_symtab(&img, kReqMCSyms[i]);+ if (reqMC) {+ vcc_log(@" requiresMasterClock @ 0x%lx (sym=%s)",+ (unsigned long)reqMC, kReqMCSyms[i]);+ break;+ }+ }+ if (reqMC) {+ uintptr_t target = (uintptr_t)ptrauth_strip(+ (void *)reqMC, ptrauth_key_function_pointer);+ uint32_t insn0 = ((const uint32_t *)target)[0];+ uint32_t insn1 = ((const uint32_t *)target)[1];+ // Sanity anchor: the second prologue insn should be pacibsp on every+ // arm64e build that signs return addresses. Refuse the patch if it+ // isn't — we'd rather skip than rewrite an unrelated function.+ if (insn1 != 0xD503237Fu) {+ vcc_log(@" requiresMasterClock @ 0x%lx: insn1=0x%08x != pacibsp; skip",+ (unsigned long)target, insn1);+ } else {+ int ok1 = vcc_patch_word(target, insn0, 0x52800000u); // mov w0, #0+ int ok2 = vcc_patch_word(target + 4, insn1, 0xd65f03c0u); // ret+ vcc_log(@" requiresMasterClock prologue patch @ 0x%lx: %d/%d",+ (unsigned long)target, ok1, ok2);+ }+ } else {+ vcc_log(@" requiresMasterClock symbol not in LC_SYMTAB; patch skipped");+ }++ // (2) `mov w20, #-12783` (MOVN encoding 0x12863dd4) -> `mov w20, #0`.+ vcc_scan_and_patch(&img, 0x12863dd4u, 0x52800014u,+ "mov w20, #-12783 -> #0");+ // (3) `mov w8, #-12783` (MOVN encoding 0x12863dc8) -> `mov w8, #0`.+ vcc_scan_and_patch(&img, 0x12863dc8u, 0x52800008u,+ "mov w8, #-12783 -> #0");+}diff --git a/VPhoneGuestComponents/VCamCaptured/VCamCaptureObservation.inc b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.msimilarity index 74%rename from VPhoneGuestComponents/VCamCaptured/VCamCaptureObservation.increname to VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.mindex d5ee5895..db6afe25 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamCaptureObservation.inc+++ b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.m@@ -1,3 +1,5 @@+#include "VCamHooks.h"+// MARK: - frame-sender endpoint observation//// AVF capture clients (Camera.app, AVCaptureSession-using apps) tell the@@ -54,7 +56,7 @@ static BOOL vcc_add_endpoint_hook(id self,return ok;}-static void vcc_install_endpoint_hook(void) {+void vcc_install_endpoint_hook(void) {Class cls = NSClassFromString(@"CMCaptureFrameSenderEndpointsServerSideSingleton");if (!cls) {@@ -122,7 +124,7 @@ static void vcc_dump_class(const char *name) {free(clist);}-static void vcc_dump_sink_node_methods(void) {+void vcc_dump_sink_node_methods(void) {vcc_log(@"---- sink-node method dump ----");vcc_dump_class("BWSinkNode");vcc_dump_class("BWImageQueueSinkNode");@@ -157,25 +159,26 @@ static void vcc_dump_sink_node_methods(void) {// sample-buffer contents.// Video sink nodes (BWImageQueueSinkNode / BWRemoteQueueSinkNode) created-// for any client session. Strong refs: they keep each node valid and its-// client transport pinned for the lifetime of the daemon — one leak per-// session start, the same tradeoff the synth streams already make. The-// viewfinder queue drives them (vcc_drive_sinks_once) while the init hooks-// append from graph-build threads, so every access holds the array's lock.-static NSMutableArray *vcc_driven_sinks = nil;+// for any client session. Weak refs: the capture graph owns each node, so a+// node drops out of the table when its session tears down, and the drive+// loop never feeds a dead graph or keeps one alive. The snapshot holds+// strong refs for the length of one drive tick. The viewfinder queue drives+// them (vcc_drive_sinks_once) while the init hooks add from graph-build+// threads, so every access holds the lock.+static NSHashTable *vcc_driven_sinks = nil;static pthread_mutex_t vcc_driven_sinks_lock = PTHREAD_MUTEX_INITIALIZER;static void vcc_track_driven_sink(id sink) {if (!sink) return;pthread_mutex_lock(&vcc_driven_sinks_lock);- if (!vcc_driven_sinks) vcc_driven_sinks = [NSMutableArray array];- if (![vcc_driven_sinks containsObject:sink]) [vcc_driven_sinks addObject:sink];+ if (!vcc_driven_sinks) vcc_driven_sinks = [NSHashTable weakObjectsHashTable];+ [vcc_driven_sinks addObject:sink];pthread_mutex_unlock(&vcc_driven_sinks_lock);}-static NSArray *vcc_driven_sinks_snapshot(void) {+NSArray *vcc_driven_sinks_snapshot(void) {pthread_mutex_lock(&vcc_driven_sinks_lock);- NSArray *snapshot = vcc_driven_sinks ? [vcc_driven_sinks copy] : @[];+ NSArray *snapshot = vcc_driven_sinks.allObjects ?: @[];pthread_mutex_unlock(&vcc_driven_sinks_lock);return snapshot;}@@ -275,67 +278,27 @@ static void vcc_rqsn_render_hook(orig(self, _cmd, cmsb, input);}-static void vcc_swizzle_method(Class cls, SEL sel, IMP newImp, IMP *outOrig) {- Method m = class_getInstanceMethod(cls, sel);- if (!m) {- vcc_log(@" swizzle: -[%s %@] missing", class_getName(cls),- NSStringFromSelector(sel));+void vcc_install_sink_observation(void) {+ Class iqsn = NSClassFromString(@"BWImageQueueSinkNode");+ Class rqsn = NSClassFromString(@"BWRemoteQueueSinkNode");+ if (!iqsn || !rqsn) {+ vcc_log(@" sink obs: classes missing iqsn=%p rqsn=%p", iqsn, rqsn);return;}- *outOrig = method_setImplementation(m, newImp);- vcc_log(@" swizzled -[%s %@] (orig imp=%p)", class_getName(cls),- NSStringFromSelector(sel), *outOrig);-}--// Extra observation: hook -[BWFigCaptureDeviceVendor copyDeviceWithID:forClient:informClientWhenDeviceAvailableAgain:error:]-// to see when an AVF client asks for our device. If this fires, we know the-// session is at least requesting a device for our synth. If it doesn't fire,-// the client never even reached the device-vendor stage (gated upstream).--static IMP vcc_copy_device_orig = NULL;--// Type encoding observed on iOS 26.5 cameracaptured:-// @40@0:8@16i24B28^i32-// => id (*)(id self, SEL _cmd, NSString *deviceID, int clientPID,-// BOOL informClient, int *err)-// The earlier disabled stub mis-typed clientPID as `id`, causing ARC to-// emit objc_retain on an integer register and crash inside the hook-// prologue.--typedef id (*VccCopyDeviceFn)(id self,- SEL _cmd,- NSString *deviceID,- int clientPID,- BOOL informClient,- int *err);-static Class vcc_synth_device_class = Nil;--// Forward decls.-static void vcc_init_synth_device_class(void);-__attribute__((ns_returns_retained))-static id vcc_make_synth_device(NSString *deviceID);--static id vcc_copy_device_hook(id self,- SEL _cmd,- NSString *deviceID,- int clientPID,- BOOL informClient,- int *err) {- vcc_log(@" [copyDeviceWithID] deviceID=%@ clientPID=%d inform=%d",- deviceID, clientPID, informClient);-- if ([deviceID isEqualToString:kVccSynthDeviceID]) {- id synth = vcc_make_synth_device(deviceID);- if (err) *err = 0;- vcc_log(@" [copyDeviceWithID] -> synth %p (class=%s err=0)",- synth, object_getClassName(synth));- return synth;- }-- VccCopyDeviceFn orig = (VccCopyDeviceFn)vcc_copy_device_orig;- id ret = orig(self, _cmd, deviceID, clientPID, informClient, err);- int errval = err ? *err : 0;- vcc_log(@" [copyDeviceWithID] returned %p (err=%d)", ret, errval);- return ret;+ SEL iqsnInit = NSSelectorFromString(+ @"initWithHFRSupport:ispJitterCompensationEnabled:"+ @"clientAuditToken:sinkID:");+ SEL rqsnInit = NSSelectorFromString(+ @"initWithMediaType:clientAuditToken:sinkID:cameraInfoByPortType:");+ SEL renderSel = @selector(renderSampleBuffer:forInput:);++ vcc_swizzle_method(iqsn, iqsnInit, (IMP)vcc_iqsn_init_hook,+ &vcc_iqsn_init_orig);+ vcc_swizzle_method(rqsn, rqsnInit, (IMP)vcc_rqsn_init_hook,+ &vcc_rqsn_init_orig);+ vcc_swizzle_method(iqsn, renderSel, (IMP)vcc_iqsn_render_hook,+ &vcc_iqsn_render_orig);+ vcc_swizzle_method(rqsn, renderSel, (IMP)vcc_rqsn_render_hook,+ &vcc_rqsn_render_orig);}diff --git a/VPhoneGuestComponents/VCamCaptured/VCamSessionHooks.inc b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamStillObservation.msimilarity index 60%rename from VPhoneGuestComponents/VCamCaptured/VCamSessionHooks.increname to VPhoneGuestComponents/VCamCaptured/Hooks/VCamStillObservation.mindex 315c2ff1..a60c3fdd 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamSessionHooks.inc+++ b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamStillObservation.m@@ -1,3 +1,5 @@+#include "VCamHooks.h"+// MARK: - BWStillImageSampleBufferSinkNode observation + injection//// AVCapturePhotoOutput attaches a sampleBufferAvailableHandler block to the@@ -48,7 +50,7 @@ static void vcc_still_render_hook(id self,orig(self, _cmd, sb, input);}-static void vcc_install_still_sink_observation(void) {+void vcc_install_still_sink_observation(void) {Class cls = NSClassFromString(@"BWStillImageSampleBufferSinkNode");if (!cls) {vcc_log(@" still-sink obs: class missing");@@ -66,119 +68,6 @@ static void vcc_install_still_sink_observation(void) {(IMP)vcc_still_render_hook, &vcc_still_render_orig);}-// MARK: - BWFigCaptureSession graph callback observation-//-// AVCaptureSession.startRunning → cameracaptured graph build → start → these-// delegate callbacks fire on BWFigCaptureSession. If start fails (error != 0),-// the daemon reports failure back over XPC and client.isRunning stays NO.-// Hook these to see exactly where in the graph startup the failure occurs.--static IMP vcc_sess_didFinishStarting_orig = NULL;-static IMP vcc_sess_didStartSourceNode_orig = NULL;-static IMP vcc_sess_didPrepare_orig = NULL;--typedef void (*VccSessDidFinishStartingFn)(id self, SEL _cmd, id graph, int error);-typedef void (*VccSessDidStartSourceFn)(id self, SEL _cmd, id graph, id node, int error);-typedef void (*VccSessDidPrepareFn)(id self, SEL _cmd, id graph);--static void vcc_sess_didFinishStarting_hook(id self,- SEL _cmd,- id graph,- int error) {- vcc_log(@" [Sess graph:didFinishStartingWithError:] self=%p graph=%p err=%d",- self, graph, error);- VccSessDidFinishStartingFn orig =- (VccSessDidFinishStartingFn)vcc_sess_didFinishStarting_orig;- orig(self, _cmd, graph, error);-}--static void vcc_sess_didStartSourceNode_hook(id self,- SEL _cmd,- id graph,- id node,- int error) {- vcc_log(@" [Sess graph:didStartSourceNode:error:] self=%p graph=%p node=%@ err=%d",- self, graph, node, error);- VccSessDidStartSourceFn orig =- (VccSessDidStartSourceFn)vcc_sess_didStartSourceNode_orig;- orig(self, _cmd, graph, node, error);-}--static void vcc_sess_didPrepare_hook(id self, SEL _cmd, id graph) {- vcc_log(@" [Sess graphDidPrepareNodes:] self=%p graph=%p", self, graph);- VccSessDidPrepareFn orig =- (VccSessDidPrepareFn)vcc_sess_didPrepare_orig;- orig(self, _cmd, graph);-}--static void vcc_install_session_graph_observation(void) {- Class cls = NSClassFromString(@"BWFigCaptureSession");- if (!cls) {- vcc_log(@" session-graph obs: class missing");- return;- }- vcc_swizzle_method(cls,- @selector(graph:didFinishStartingWithError:),- (IMP)vcc_sess_didFinishStarting_hook,- &vcc_sess_didFinishStarting_orig);- vcc_swizzle_method(cls,- @selector(graph:didStartSourceNode:error:),- (IMP)vcc_sess_didStartSourceNode_hook,- &vcc_sess_didStartSourceNode_orig);- vcc_swizzle_method(cls,- @selector(graphDidPrepareNodes:),- (IMP)vcc_sess_didPrepare_hook,- &vcc_sess_didPrepare_orig);-}--// MARK: - BWFigCaptureSession init capture + pipelines extraction-//-// To splice our manual sink into a real session, we need a handle to its-// FigCaptureSessionPipelines instance. Hook the session's init and, post-orig,-// reach into the _pipelines ivar via KVC. Store globally so the next-stage-// injector has a live reference.--static IMP vcc_sess_initWithFigSess_orig = NULL;-typedef id (*VccSessInitWithFigSessFn)(id self, SEL _cmd, void *figSess);--static id vcc_captured_bw_session = nil; // strong ref to the BWFigCaptureSession-static id vcc_captured_pipelines = nil; // its _pipelines ivar value--__attribute__((ns_returns_retained))-static id vcc_sess_initWithFigSess_hook(id self, SEL _cmd, void *figSess) {- VccSessInitWithFigSessFn orig =- (VccSessInitWithFigSessFn)vcc_sess_initWithFigSess_orig;- id ret = orig(self, _cmd, figSess);- if (ret) {- vcc_captured_bw_session = ret;- @try {- vcc_captured_pipelines = [ret valueForKey:@"pipelines"];- } @catch (NSException *e) {- @try {- vcc_captured_pipelines = [ret valueForKey:@"_pipelines"];- } @catch (NSException *e2) {- vcc_captured_pipelines = nil;- }- }- vcc_log(@" [BWFigCaptureSession init] -> self=%p figSess=%p _pipelines=%p (class=%@)",- ret, figSess, vcc_captured_pipelines,- NSStringFromClass([vcc_captured_pipelines class]));- }- return ret;-}--static void vcc_install_session_init_capture(void) {- Class cls = NSClassFromString(@"BWFigCaptureSession");- if (!cls) {- vcc_log(@" session-init capture: class missing");- return;- }- vcc_swizzle_method(cls,- @selector(initWithFigCaptureSession:),- (IMP)vcc_sess_initWithFigSess_hook,- &vcc_sess_initWithFigSess_orig);-}-// MARK: - BWFigCaptureSession stillImageCoordinator observation//// AVCapturePhotoOutput.capturePhoto → daemon-side BWStillImageCaptureCoordinator@@ -255,7 +144,7 @@ static void vcc_sess_didCapturePhoto_hook(id self,orig(self, _cmd, coord, settings);}-static void vcc_install_still_coordinator_observation(void) {+void vcc_install_still_coordinator_observation(void) {Class cls = NSClassFromString(@"BWFigCaptureSession");if (!cls) {vcc_log(@" still-coord obs: BWFigCaptureSession missing");@@ -304,7 +193,7 @@ static void vcc_still_coord_enqueue_hook(id self,orig(self, _cmd, settings, serviceIfNecessary);}-static void vcc_install_still_coord_node_observation(void) {+void vcc_install_still_coord_node_observation(void) {Class cls = NSClassFromString(@"BWStillImageCoordinatorNode");if (!cls) {vcc_log(@" still-coord-node obs: class missing");@@ -361,7 +250,7 @@ static id vcc_still_pipe_init_hook(id self,return ret;}-static void vcc_install_still_pipeline_observation(void) {+void vcc_install_still_pipeline_observation(void) {Class cls = NSClassFromString(@"FigCaptureStillImageSinkPipeline");if (!cls) {vcc_log(@" still-pipe obs: class missing");@@ -394,7 +283,7 @@ static void vcc_pipelines_addStill_hook(id self, SEL _cmd, id storage) {orig(self, _cmd, storage);}-static void vcc_install_pipelines_addStill_observation(void) {+void vcc_install_pipelines_addStill_observation(void) {Class cls = NSClassFromString(@"FigCaptureSessionPipelines");if (!cls) {vcc_log(@" pipelines obs: class missing");@@ -405,82 +294,3 @@ static void vcc_install_pipelines_addStill_observation(void) {(IMP)vcc_pipelines_addStill_hook,&vcc_pipelines_addStill_orig);}--// MARK: - graph-build error suppressors-//-// captureSession_buildGraphWithConfiguration's per-source iteration bails-// to OSStatus -12783 along three known paths on our synth source:-//-// (1) -[FigCaptureCameraSourcePipeline requiresMasterClock] returns YES,-// and the post-iteration check (OR of all pipelines' returns) bails-// on a virtual source that has no real ISP/HW clock.-// (2) _cs_addObjectToStreamsAttributes returns -12783 when an inherited-// ivar slot is NULL (called from _FigVideoCaptureSourcesActivate--// AndCreateDevices during session activation).-// (3) -[BWFigVideoCaptureStream initWithCaptureStream:…] sets *errOut-// = -12783 in its validation-failure path.-//-// (1) is neutralized by rewriting the function's prologue to `mov w0, #0; ret`.-// The function's address is resolved from CMCapture's LC_SYMTAB by name —-// no hardcoded VMA.-//-// (2)+(3) are neutralized by scanning CMCapture's __text for the specific-// MOVN encodings the compiler used to prepare -12783 in w20 / w8, and-// rewriting each to `MOVZ w?, #0`. -12783 is a capture-specific OSStatus-// emitted only by the daemon; over-application is benign because the-// daemon's only consumer in the VM is our synth.--static void vcc_install_csp_requires_master_clock_hook(void) {- vcc_image_t img;- if (vcc_image_resolve(&img, "FigCaptureSourceServerStart") != 0) {- vcc_log(@" graph-build patches: image resolve failed");- return;- }-- // (1) Resolve -[FigCaptureCameraSourcePipeline requiresMasterClock] via- // LC_SYMTAB. The function is called via direct C dispatch (not in the- // objc method table on observed builds, so swizzling won't work), so- // we rewrite its prologue in place. The PAC-signed address from the- // symtab is stripped before the write.- static const char *const kReqMCSyms[] = {- "_-[FigCaptureCameraSourcePipeline requiresMasterClock]",- "-[FigCaptureCameraSourcePipeline requiresMasterClock]",- NULL,- };- uintptr_t reqMC = 0;- for (unsigned i = 0; kReqMCSyms[i]; i++) {- reqMC = vcc_lookup_lc_symtab(&img, kReqMCSyms[i]);- if (reqMC) {- vcc_log(@" requiresMasterClock @ 0x%lx (sym=%s)",- (unsigned long)reqMC, kReqMCSyms[i]);- break;- }- }- if (reqMC) {- uintptr_t target = (uintptr_t)ptrauth_strip(- (void *)reqMC, ptrauth_key_function_pointer);- uint32_t insn0 = ((const uint32_t *)target)[0];- uint32_t insn1 = ((const uint32_t *)target)[1];- // Sanity anchor: the second prologue insn should be pacibsp on every- // arm64e build that signs return addresses. Refuse the patch if it- // isn't — we'd rather skip than rewrite an unrelated function.- if (insn1 != 0xD503237Fu) {- vcc_log(@" requiresMasterClock @ 0x%lx: insn1=0x%08x != pacibsp; skip",- (unsigned long)target, insn1);- } else {- int ok1 = vcc_patch_word(target, insn0, 0x52800000u); // mov w0, #0- int ok2 = vcc_patch_word(target + 4, insn1, 0xd65f03c0u); // ret- vcc_log(@" requiresMasterClock prologue patch @ 0x%lx: %d/%d",- (unsigned long)target, ok1, ok2);- }- } else {- vcc_log(@" requiresMasterClock symbol not in LC_SYMTAB; patch skipped");- }-- // (2) `mov w20, #-12783` (MOVN encoding 0x12863dd4) -> `mov w20, #0`.- vcc_scan_and_patch(&img, 0x12863dd4u, 0x52800014u,- "mov w20, #-12783 -> #0");- // (3) `mov w8, #-12783` (MOVN encoding 0x12863dc8) -> `mov w8, #0`.- vcc_scan_and_patch(&img, 0x12863dc8u, 0x52800008u,- "mov w8, #-12783 -> #0");-}diff --git a/VPhoneGuestComponents/VCamCaptured/VCamStillSink.inc b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamStillSink.msimilarity index 98%rename from VPhoneGuestComponents/VCamCaptured/VCamStillSink.increname to VPhoneGuestComponents/VCamCaptured/Hooks/VCamStillSink.mindex ab9dba14..9f3f4de8 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamStillSink.inc+++ b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamStillSink.m@@ -1,3 +1,7 @@+#include "VCamFrames.h"+#include "VCamHooks.h"+#include "vcam_dataplane.h"+// MARK: - Sink node injection — manually-constructed BWStillImageSampleBufferSinkNode//// The daemon's session graph for our synth source builds without a still-image@@ -11,7 +15,7 @@static id vcc_synth_still_sink = nil;-static void vcc_construct_still_sink(void) {+void vcc_construct_still_sink(void) {Class cls = NSClassFromString(@"BWStillImageSampleBufferSinkNode");if (!cls) {vcc_log(@" manual still-sink: class missing");@@ -135,7 +139,7 @@ static void vcc_construct_still_sink(void) {// synth sink directly with a synthetic CMSampleBuffer built from// vcc_latest_frame. If our manually-installed handler fires, we have// proven the synthetic-handler approach works at the API level.-static void vcc_drive_still_sink_once(void) {+void vcc_drive_still_sink_once(void) {if (!vcc_synth_still_sink) return;// Photo sinks receive BGRA (the still pipeline JPEG-encodes directly);// delivered format matches the BGRA FigCaptureSourceVideoFormat.@@ -254,7 +258,7 @@ static id vcc_parsed_cfg_init_hook(id self,return ret;}-static void vcc_install_parsed_cfg_observation(void) {+void vcc_install_parsed_cfg_observation(void) {Class cls = NSClassFromString(@"FigCaptureSessionParsedConfiguration");if (!cls) {vcc_log(@" parsed-cfg obs: class missing");diff --git a/VPhoneGuestComponents/VCamCaptured/Image/VCamCodePatch.m b/VPhoneGuestComponents/VCamCaptured/Image/VCamCodePatch.mnew file mode 100644index 00000000..88547787--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Image/VCamCodePatch.m@@ -0,0 +1,182 @@+#include <libkern/OSCacheControl.h>+#include <mach/mach.h>++#include "VCamImage.h"++// MARK: - in-place code patching++// Patch two consecutive instructions starting at `pc` to NOP. iOS __TEXT+// is W^X-enforced + TXM-validated. Try in order:+// (a) vm_protect with VM_PROT_COPY: kernel COWs the page into an anon+// mapping and grants RW. Standard iOS-hooker recipe (libhooker etc).+// (b) vm_allocate scratch + memcpy + vm_remap(OVERWRITE|FIXED) overlay.+// Either way, scratch_writable -> patch -> set RX -> icache flush.+int vcc_patch_two_nops(uintptr_t pc) {+ uintptr_t page_size = (uintptr_t)getpagesize();+ uintptr_t page_start = pc & ~(page_size - 1);+ uintptr_t end = pc + 8;+ uintptr_t page_end =+ ((end + page_size - 1) & ~(page_size - 1));+ vm_size_t span = (vm_size_t)(page_end - page_start);+ mach_port_t self_task = mach_task_self();++ // (a) vm_protect with VM_PROT_COPY (= 0x10) to force COW.+ kern_return_t kr = vm_protect(+ self_task,+ (vm_address_t)page_start,+ span,+ FALSE,+ VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);+ if (kr == KERN_SUCCESS) {+ uint32_t nop = 0xD503201Fu;+ ((uint32_t *)pc)[0] = nop;+ ((uint32_t *)pc)[1] = nop;+ kr = vm_protect(+ self_task,+ (vm_address_t)page_start,+ span,+ FALSE,+ VM_PROT_READ | VM_PROT_EXECUTE);+ if (kr == KERN_SUCCESS) {+ sys_icache_invalidate((void *)pc, 8);+ vcc_log(@" vm_protect+COPY patch OK @ 0x%lx",+ (unsigned long)pc);+ return 1;+ }+ vcc_log(@" vm_protect restore RX failed: %d (page=0x%lx)",+ kr,+ (unsigned long)page_start);+ // Continue to try (b).+ } else {+ vcc_log(@" vm_protect+COPY failed: %d", kr);+ }++ // (b) Scratch allocation + vm_remap with FIXED|OVERWRITE.+ vm_address_t scratch = 0;+ kr = vm_allocate(self_task, &scratch, span, VM_FLAGS_ANYWHERE);+ if (kr != KERN_SUCCESS) {+ vcc_log(@" vm_allocate failed: %d", kr);+ return 0;+ }+ memcpy((void *)scratch, (const void *)page_start, span);+ uint32_t nop = 0xD503201Fu;+ uintptr_t scratch_pc = scratch + (pc - page_start);+ ((uint32_t *)scratch_pc)[0] = nop;+ ((uint32_t *)scratch_pc)[1] = nop;+ kr = vm_protect(+ self_task,+ scratch,+ span,+ FALSE,+ VM_PROT_READ | VM_PROT_EXECUTE);+ if (kr != KERN_SUCCESS) {+ vcc_log(@" vm_protect RX scratch failed: %d", kr);+ vm_deallocate(self_task, scratch, span);+ return 0;+ }+ vm_address_t target = (vm_address_t)page_start;+ vm_prot_t cur_prot = 0, max_prot = 0;+ kr = vm_remap(+ self_task,+ &target,+ span,+ 0,+ VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,+ self_task,+ scratch,+ FALSE,+ &cur_prot,+ &max_prot,+ VM_INHERIT_NONE);+ if (kr != KERN_SUCCESS) {+ vcc_log(@" vm_remap FIXED|OVERWRITE failed: %d (cur=0x%x max=0x%x)",+ kr,+ cur_prot,+ max_prot);+ vm_deallocate(self_task, scratch, span);+ return 0;+ }+ sys_icache_invalidate((void *)pc, 8);+ vcc_log(@" vm_remap OK: page=0x%lx span=%zu (cur=0x%x max=0x%x)",+ (unsigned long)page_start,+ (size_t)span,+ cur_prot,+ max_prot);+ return 1;+}++// Patch a single 32-bit ARM64 instruction word at `pc` to `new_word`.+// Uses the same vm_protect(VM_PROT_COPY) → write → vm_protect(RX) →+// icache flush dance as vcc_patch_two_nops. Verifies the original word+// matches `expected_word` before writing so an iOS version skew doesn't+// silently corrupt the wrong code. Returns 1 on success.+int vcc_patch_word(uintptr_t pc,+ uint32_t expected_word,+ uint32_t new_word) {+ uint32_t cur = ((const uint32_t *)pc)[0];+ if (cur != expected_word) {+ vcc_log(@" patch_word @ 0x%lx: expected 0x%08x, found 0x%08x — skip",+ (unsigned long)pc,+ expected_word,+ cur);+ return 0;+ }+ uintptr_t page_size = (uintptr_t)getpagesize();+ uintptr_t page_start = pc & ~(page_size - 1);+ uintptr_t end = pc + 4;+ uintptr_t page_end = ((end + page_size - 1) & ~(page_size - 1));+ vm_size_t span = (vm_size_t)(page_end - page_start);+ mach_port_t self_task = mach_task_self();++ kern_return_t kr = vm_protect(+ self_task,+ (vm_address_t)page_start,+ span,+ FALSE,+ VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);+ if (kr != KERN_SUCCESS) {+ vcc_log(@" patch_word vm_protect+COPY failed: %d", kr);+ return 0;+ }+ ((uint32_t *)pc)[0] = new_word;+ kr = vm_protect(+ self_task,+ (vm_address_t)page_start,+ span,+ FALSE,+ VM_PROT_READ | VM_PROT_EXECUTE);+ if (kr != KERN_SUCCESS) {+ vcc_log(@" patch_word restore RX failed: %d", kr);+ return 0;+ }+ sys_icache_invalidate((void *)pc, 4);+ vcc_log(@" patch_word OK @ 0x%lx: 0x%08x -> 0x%08x",+ (unsigned long)pc,+ expected_word,+ new_word);+ return 1;+}++// Scan the image's __text for every occurrence of `needle` and rewrite+// each to `replacement` via vcc_patch_word. Replaces hardcoded image+// VMAs for patches whose addresses we don't know per-build but whose+// instruction encoding is a stable fingerprint (e.g. `mov w20, #-12783`+// MOVN encodings used for error-prep). Returns the count patched.+unsigned vcc_scan_and_patch(const vcc_image_t *img,+ uint32_t needle,+ uint32_t replacement,+ const char *what) {+ if (!img->text || !img->text_words) return 0;+ unsigned hits = 0;+ for (size_t i = 0; i < img->text_words; i++) {+ if (img->text[i] != needle) continue;+ uintptr_t pc = (uintptr_t)&img->text[i];+ if (vcc_patch_word(pc, needle, replacement)) hits++;+ }+ vcc_log(@" scan_and_patch %s (0x%08x -> 0x%08x): %u hit(s)",+ what ? what : "?",+ needle,+ replacement,+ hits);+ return hits;+}diff --git a/VPhoneGuestComponents/VCamCaptured/Image/VCamImage.h b/VPhoneGuestComponents/VCamCaptured/Image/VCamImage.hnew file mode 100644index 00000000..46f53ed4--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Image/VCamImage.h@@ -0,0 +1,94 @@+#ifndef VCAM_IMAGE_H+#define VCAM_IMAGE_H++// Runtime view of a loaded Mach-O image (CMCapture) and the structural+// scans and in-place code patches built on it. No image VMAs are+// hardcoded: everything is recovered from dlsym anchors, load commands and+// instruction patterns.++#include <mach-o/loader.h>+#include <mach-o/nlist.h>++#include "VCamCapturedPrivate.h"++#pragma GCC visibility push(hidden)++#define VCC_MAX_DATA_RANGES 8++typedef struct {+ uintptr_t start;+ uintptr_t end;+} vcc_range_t;++typedef struct {+ const struct mach_header_64 *mh;+ intptr_t slide;+ const uint32_t *text;+ size_t text_words; // count of 4-byte instructions+ vcc_range_t data_ranges[VCC_MAX_DATA_RANGES];+ unsigned data_range_count;+ // LC_SYMTAB pointers (may be 0 on DSC dylibs that strip private symbols+ // from the per-image symtab; callers must handle gracefully).+ const struct nlist_64 *symtab;+ const char *strtab;+ uint32_t nsyms;+} vcc_image_t;++// MARK: - resolution (VCamImageResolution.m)++// Resolves the image that defines `anchor_sym`. Returns 0 on success, a+// negative step number on failure.+int vcc_image_resolve(vcc_image_t *out, const char *anchor_sym);++// Walks the image's LC_SYMTAB for `name`. Returns the slid VMA, or 0.+uintptr_t vcc_lookup_lc_symtab(const vcc_image_t *img, const char *name);++// PC of the per-source ownership `cbz w0; cbz w25` pair inside+// _captureSourceServer_handleCopySourcesMessage, or 0.+uintptr_t vcc_find_per_source_filter(const vcc_image_t *img);++// PC of the client-allowlist filter chain, or 0. On a hit, writes the+// signing-identifier and prewarming-bundle function addresses.+uintptr_t vcc_find_filter_chain(const vcc_image_t *img,+ uintptr_t *si_fn_out,+ uintptr_t *prewarm_fn_out);++// Absolute page-aligned target of `adrp Xn, <page>` at adrp_pc.+uintptr_t vcc_adrp_target(uintptr_t adrp_pc, uint32_t adrp);++// YES when the pointer stored at slot_addr is a heap-allocated CFArray.+BOOL vcc_slot_value_is_cfarray(uintptr_t slot_addr);++// Collects every `bl <X>; adrp; str x0, [Xn, #imm]` store target in the+// first maxInsns instructions of func. Returns the count written.+unsigned vcc_collect_call_then_store_globals(uintptr_t func,+ unsigned maxInsns,+ unsigned lookahead,+ uintptr_t *out_addrs,+ unsigned cap);++// A CFString constant resolved by symbol name, or NULL.+CFStringRef vcc_cfconst(const char *symname);++// A function resolved by symbol name (PAC-signed for a C call), or NULL.+void *vcc_dlsym_fn(const char *name);++// MARK: - code patching (VCamCodePatch.m)++// Rewrites the two instructions at pc to NOP. Returns 1 on success.+int vcc_patch_two_nops(uintptr_t pc);++// Rewrites the word at pc from expected_word to new_word. Returns 1 on+// success, 0 when the word does not match or the page cannot be written.+int vcc_patch_word(uintptr_t pc, uint32_t expected_word, uint32_t new_word);++// Rewrites every `needle` word in __text to `replacement`. Returns the+// number of words patched.+unsigned vcc_scan_and_patch(const vcc_image_t *img,+ uint32_t needle,+ uint32_t replacement,+ const char *what);++#pragma GCC visibility pop++#endifdiff --git a/VPhoneGuestComponents/VCamCaptured/VCamImageResolution.inc b/VPhoneGuestComponents/VCamCaptured/Image/VCamImageResolution.msimilarity index 69%rename from VPhoneGuestComponents/VCamCaptured/VCamImageResolution.increname to VPhoneGuestComponents/VCamCaptured/Image/VCamImageResolution.mindex 124a0142..f5dc180b 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamImageResolution.inc+++ b/VPhoneGuestComponents/VCamCaptured/Image/VCamImageResolution.m@@ -1,27 +1,13 @@+#include <mach-o/dyld.h>+#include <mach-o/getsect.h>+#include <malloc/malloc.h>+#include <ptrauth.h>++#include "VCamImage.h"+// MARK: - runtime image resolution-#define VCC_MAX_DATA_RANGES 8--typedef struct {- uintptr_t start;- uintptr_t end;-} vcc_range_t;--typedef struct {- const struct mach_header_64 *mh;- intptr_t slide;- const uint32_t *text;- size_t text_words; // count of 4-byte instructions- vcc_range_t data_ranges[VCC_MAX_DATA_RANGES];- unsigned data_range_count;- // LC_SYMTAB pointers (may be 0 on DSC dylibs that strip private symbols- // from the per-image symtab; callers must handle gracefully).- const struct nlist_64 *symtab;- const char *strtab;- uint32_t nsyms;-} vcc_image_t;--static int vcc_image_resolve(vcc_image_t *out, const char *anchor_sym) {+int vcc_image_resolve(vcc_image_t *out, const char *anchor_sym) {memset(out, 0, sizeof(*out));void *anchor = dlsym(RTLD_DEFAULT, anchor_sym);if (!anchor) return -1;@@ -96,8 +82,7 @@ static int vcc_image_resolve(vcc_image_t *out, const char *anchor_sym) {// Walk CMCapture's LC_SYMTAB for `name`. Returns slid VMA on match, or 0.// On DSC dylibs the per-image symtab may be stripped of private symbols, in// which case this returns 0 even for symbols that exist statically.-static uintptr_t vcc_lookup_lc_symtab(const vcc_image_t *img,- const char *name) {+uintptr_t vcc_lookup_lc_symtab(const vcc_image_t *img, const char *name) {if (!img->symtab || !img->strtab || img->nsyms == 0) return 0;for (uint32_t i = 0; i < img->nsyms; i++) {if (img->symtab[i].n_un.n_strx == 0) continue;@@ -134,7 +119,7 @@ static uintptr_t vcc_bl_target(uintptr_t bl_pc, uint32_t bl) {// Both cbz branch targets are identical (same SKIP label). We patch both// to NOP so every source survives the per-client ownership + prewarming-// enabled filter and reaches the response serializer.-static uintptr_t vcc_find_per_source_filter(const vcc_image_t *img) {+uintptr_t vcc_find_per_source_filter(const vcc_image_t *img) {if (!img->text || img->text_words < 6) return 0;for (size_t i = 0; i + 5 < img->text_words; i++) {if (img->text[i] != 0xAA0003F9u) continue; // mov x25, x0@@ -163,182 +148,6 @@ static uintptr_t vcc_find_per_source_filter(const vcc_image_t *img) {return 0;}-// Patch two consecutive instructions starting at `pc` to NOP. iOS __TEXT-// is W^X-enforced + TXM-validated. Try in order:-// (a) vm_protect with VM_PROT_COPY: kernel COWs the page into an anon-// mapping and grants RW. Standard iOS-hooker recipe (libhooker etc).-// (b) vm_allocate scratch + memcpy + vm_remap(OVERWRITE|FIXED) overlay.-// Either way, scratch_writable -> patch -> set RX -> icache flush.-static int vcc_patch_two_nops(uintptr_t pc) {- uintptr_t page_size = (uintptr_t)getpagesize();- uintptr_t page_start = pc & ~(page_size - 1);- uintptr_t end = pc + 8;- uintptr_t page_end =- ((end + page_size - 1) & ~(page_size - 1));- vm_size_t span = (vm_size_t)(page_end - page_start);- mach_port_t self_task = mach_task_self();-- // (a) vm_protect with VM_PROT_COPY (= 0x10) to force COW.- kern_return_t kr = vm_protect(- self_task,- (vm_address_t)page_start,- span,- FALSE,- VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);- if (kr == KERN_SUCCESS) {- uint32_t nop = 0xD503201Fu;- ((uint32_t *)pc)[0] = nop;- ((uint32_t *)pc)[1] = nop;- kr = vm_protect(- self_task,- (vm_address_t)page_start,- span,- FALSE,- VM_PROT_READ | VM_PROT_EXECUTE);- if (kr == KERN_SUCCESS) {- sys_icache_invalidate((void *)pc, 8);- vcc_log(@" vm_protect+COPY patch OK @ 0x%lx",- (unsigned long)pc);- return 1;- }- vcc_log(@" vm_protect restore RX failed: %d (page=0x%lx)",- kr,- (unsigned long)page_start);- // Continue to try (b).- } else {- vcc_log(@" vm_protect+COPY failed: %d", kr);- }-- // (b) Scratch allocation + vm_remap with FIXED|OVERWRITE.- vm_address_t scratch = 0;- kr = vm_allocate(self_task, &scratch, span, VM_FLAGS_ANYWHERE);- if (kr != KERN_SUCCESS) {- vcc_log(@" vm_allocate failed: %d", kr);- return 0;- }- memcpy((void *)scratch, (const void *)page_start, span);- uint32_t nop = 0xD503201Fu;- uintptr_t scratch_pc = scratch + (pc - page_start);- ((uint32_t *)scratch_pc)[0] = nop;- ((uint32_t *)scratch_pc)[1] = nop;- kr = vm_protect(- self_task,- scratch,- span,- FALSE,- VM_PROT_READ | VM_PROT_EXECUTE);- if (kr != KERN_SUCCESS) {- vcc_log(@" vm_protect RX scratch failed: %d", kr);- vm_deallocate(self_task, scratch, span);- return 0;- }- vm_address_t target = (vm_address_t)page_start;- vm_prot_t cur_prot = 0, max_prot = 0;- kr = vm_remap(- self_task,- &target,- span,- 0,- VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,- self_task,- scratch,- FALSE,- &cur_prot,- &max_prot,- VM_INHERIT_NONE);- if (kr != KERN_SUCCESS) {- vcc_log(@" vm_remap FIXED|OVERWRITE failed: %d (cur=0x%x max=0x%x)",- kr,- cur_prot,- max_prot);- vm_deallocate(self_task, scratch, span);- return 0;- }- sys_icache_invalidate((void *)pc, 8);- vcc_log(@" vm_remap OK: page=0x%lx span=%zu (cur=0x%x max=0x%x)",- (unsigned long)page_start,- (size_t)span,- cur_prot,- max_prot);- return 1;-}--// Patch a single 32-bit ARM64 instruction word at `pc` to `new_word`.-// Uses the same vm_protect(VM_PROT_COPY) → write → vm_protect(RX) →-// icache flush dance as vcc_patch_two_nops. Verifies the original word-// matches `expected_word` before writing so an iOS version skew doesn't-// silently corrupt the wrong code. Returns 1 on success.-static int vcc_patch_word(uintptr_t pc,- uint32_t expected_word,- uint32_t new_word) {- uint32_t cur = ((const uint32_t *)pc)[0];- if (cur != expected_word) {- vcc_log(@" patch_word @ 0x%lx: expected 0x%08x, found 0x%08x — skip",- (unsigned long)pc,- expected_word,- cur);- return 0;- }- uintptr_t page_size = (uintptr_t)getpagesize();- uintptr_t page_start = pc & ~(page_size - 1);- uintptr_t end = pc + 4;- uintptr_t page_end = ((end + page_size - 1) & ~(page_size - 1));- vm_size_t span = (vm_size_t)(page_end - page_start);- mach_port_t self_task = mach_task_self();-- kern_return_t kr = vm_protect(- self_task,- (vm_address_t)page_start,- span,- FALSE,- VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);- if (kr != KERN_SUCCESS) {- vcc_log(@" patch_word vm_protect+COPY failed: %d", kr);- return 0;- }- ((uint32_t *)pc)[0] = new_word;- kr = vm_protect(- self_task,- (vm_address_t)page_start,- span,- FALSE,- VM_PROT_READ | VM_PROT_EXECUTE);- if (kr != KERN_SUCCESS) {- vcc_log(@" patch_word restore RX failed: %d", kr);- return 0;- }- sys_icache_invalidate((void *)pc, 4);- vcc_log(@" patch_word OK @ 0x%lx: 0x%08x -> 0x%08x",- (unsigned long)pc,- expected_word,- new_word);- return 1;-}--// Scan the image's __text for every occurrence of `needle` and rewrite-// each to `replacement` via vcc_patch_word. Replaces hardcoded image-// VMAs for patches whose addresses we don't know per-build but whose-// instruction encoding is a stable fingerprint (e.g. `mov w20, #-12783`-// MOVN encodings used for error-prep). Returns the count patched.-static unsigned vcc_scan_and_patch(const vcc_image_t *img,- uint32_t needle,- uint32_t replacement,- const char *what) {- if (!img->text || !img->text_words) return 0;- unsigned hits = 0;- for (size_t i = 0; i < img->text_words; i++) {- if (img->text[i] != needle) continue;- uintptr_t pc = (uintptr_t)&img->text[i];- if (vcc_patch_word(pc, needle, replacement)) hits++;- }- vcc_log(@" scan_and_patch %s (0x%08x -> 0x%08x): %u hit(s)",- what ? what : "?",- needle,- replacement,- hits);- return hits;-}-// Scan __text for the daemon's client-allowlist filter sequence:// bl <FigCaptureCopyClientCodeSigningIdentifier> ; X// bl <objc_autorelease_stub> ; (don't care about target)@@ -351,9 +160,9 @@ static unsigned vcc_scan_and_patch(const vcc_image_t *img,//// On a hit, writes the absolute (slid) target of the 1st and 4th BL into// `*si_fn_out` and `*prewarm_fn_out`. Returns the PC of the first BL.-static uintptr_t vcc_find_filter_chain(const vcc_image_t *img,- uintptr_t *si_fn_out,- uintptr_t *prewarm_fn_out) {+uintptr_t vcc_find_filter_chain(const vcc_image_t *img,+ uintptr_t *si_fn_out,+ uintptr_t *prewarm_fn_out) {if (!img->text || img->text_words < 8) return 0;*si_fn_out = 0;*prewarm_fn_out = 0;@@ -402,7 +211,7 @@ static int vcc_addr_in_data(const vcc_image_t *img, uintptr_t addr) {// Decode ARM64 ADRP immediate from the instruction word.// Returns the absolute page-aligned target of `adrp Xn, <page>`.-static uintptr_t vcc_adrp_target(uintptr_t adrp_pc, uint32_t adrp) {+uintptr_t vcc_adrp_target(uintptr_t adrp_pc, uint32_t adrp) {int64_t immlo = (adrp >> 29) & 0x3;int64_t immhi = (adrp >> 5) & 0x7ffff;int64_t imm = (immhi << 2) | immlo;@@ -492,7 +301,7 @@ static uintptr_t vcc_find_data_xref(const vcc_image_t *img, unsigned offset) {// 64-bit value; never dereferences) before reading its isa. CFArray// instances are always heap allocations from the default malloc zone,// so a non-heap value can't be _sSourceList.-static BOOL vcc_slot_value_is_cfarray(uintptr_t slot_addr) {+BOOL vcc_slot_value_is_cfarray(uintptr_t slot_addr) {if (!slot_addr) return NO;// The slot addresses read here are always in CMCapture's __DATA /// __DATA_CONST segments (filtered upstream by vcc_addr_in_data) so the read@@ -537,7 +346,7 @@ static BOOL vcc_slot_value_is_cfarray(uintptr_t slot_addr) {// a static global" site. The init block for CMCapture's source-list// statics emits one such pair per global it initializes; the first// one is `_sSourceList`.-static unsigned vcc_collect_call_then_store_globals(+unsigned vcc_collect_call_then_store_globals(uintptr_t func,unsigned maxInsns,unsigned lookahead,@@ -576,7 +385,7 @@ static unsigned vcc_collect_call_then_store_globals(}// Resolve a CFString constant by symbol name.-static CFStringRef vcc_cfconst(const char *symname) {+CFStringRef vcc_cfconst(const char *symname) {void **slot = dlsym(RTLD_DEFAULT, symname);if (!slot) {vcc_log(@" dlsym FAIL: %s", symname);@@ -588,7 +397,7 @@ static CFStringRef vcc_cfconst(const char *symname) {// Resolve a function symbol by name. On arm64e dlsym returns a PAC-signed// pointer signed with key IA + discriminator 0 (the standard C ABI signing// schema) — callable directly as a C function pointer. Returns NULL on miss.-static void *vcc_dlsym_fn(const char *name) {+void *vcc_dlsym_fn(const char *name) {void *p = dlsym(RTLD_DEFAULT, name);if (!p) vcc_log(@" dlsym FAIL: %s", name);return p;diff --git a/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSourceBacking.m b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSourceBacking.mnew file mode 100644index 00000000..fe2d901b--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSourceBacking.m@@ -0,0 +1,247 @@+#include "VCamImage.h"+#include "VCamSynthetic.h"+#include "vcam_dataplane.h"++// MARK: - synthetic source construction++NSString *const kVccSynthDeviceID = @"vphone:vcam:device:0";+NSString *const kVccSynthStreamID = @"vphone:vcam:stream:video:0";++id vcc_build_backing(void) {+ Class backingClass = NSClassFromString(@"FigCaptureSourceBacking");+ if (!backingClass) {+ vcc_log(@" FigCaptureSourceBacking class missing");+ return nil;+ }+ CFStringRef k_uid = vcc_cfconst("kFigCaptureSourceAttributeKey_UniqueID");+ CFStringRef k_dt = vcc_cfconst("kFigCaptureSourceAttributeKey_DeviceType");+ CFStringRef k_ln = vcc_cfconst("kFigCaptureSourceAttributeKey_LocalizedName");+ CFStringRef k_mid = vcc_cfconst("kFigCaptureSourceAttributeKey_ModelID");+ CFStringRef k_pos = vcc_cfconst("kFigCaptureSourceAttributeKey_Position");+ CFStringRef k_st = vcc_cfconst("kFigCaptureSourceAttributeKey_SourceType");+ CFStringRef k_cdid = vcc_cfconst("kFigCaptureSourceAttributeKey_CaptureDeviceID");+ if (!k_uid || !k_dt) {+ vcc_log(@" required attr keys missing");+ return nil;+ }++ NSMutableDictionary *attrs = [NSMutableDictionary dictionary];+ attrs[(__bridge NSString *)k_uid] = @"vphone:vcam:0";+ // DeviceType: empirical sweep+ // - 1 (BuiltInWideAngleCamera): Camera.app routes to the real ISP /+ // hardware path and fails immediately because no hardware backs+ // us — viewfinder stream pool isn't even spun up.+ // - 2: Camera.app's Photo/Video tabs use the generic / soft path+ // and at least start the FigCameraViewfinderStream init pool+ // (~2000/sec) before stalling at openWithDestination:. This is+ // where we have a chance to inject a destination.+ // Keep 2 until the inject-destination shape is figured out.+ attrs[(__bridge NSString *)k_dt] = @(2);+ if (k_ln) attrs[(__bridge NSString *)k_ln] = @"vphone Virtual Camera";+ if (k_mid) attrs[(__bridge NSString *)k_mid] = @"vphone-vcam-1";+ if (k_pos) attrs[(__bridge NSString *)k_pos] = @(1);+ // sourceType MUST be 1 (video) for the daemon's media-type filter to+ // include this source in a vide-typed FigCaptureSourceRemoteCopyCaptureSources+ // request. Without it, attrs[SourceType] = nil → intValue = 0 → fails+ // the tbnz w8 gate in handleCopySourcesMessage.+ if (k_st) attrs[(__bridge NSString *)k_st] = @(1);+ // captureSession_buildGraphWithConfiguration (CMCapture vmaddr 0x1ae2b1910)+ // iterates the source list and calls+ // _FigCaptureSourceGetAttribute(source, kFigCaptureSourceAttributeKey_CaptureDeviceID)+ // then -[NSMutableArray addObject:] with the result. If the key is missing+ // the call returns nil and the array throws NSInvalidArgumentException+ // ("object cannot be nil") inside -[__NSArrayM insertObject:atIndex:] —+ // which kills cameracaptured as soon as any AVCaptureSession references+ // our synth.+ //+ // The value must be a string, NOT a number. -[BWFigCaptureDeviceVendor+ // _createDevice:reason:clientPID:figCaptureDevice:] sends isEqualToString:+ // to it; an NSNumber raises "unrecognized selector". Use a unique opaque+ // string outside whatever pattern Apple uses for hardware cameras.+ if (k_cdid) attrs[(__bridge NSString *)k_cdid] = kVccSynthDeviceID;++ // After hooking _FigCaptureSourceGetAttribute via lldb we observed+ // Camera.app's graph builder also queries these 5 boolean / scheme keys+ // against our synth. Provide explicit values so the builder doesn't bail+ // on a nil capability check. Verified order observed via lldb:+ // SmartCameraSupported, StillImageNoiseReductionAndFusionScheme,+ // MidFrameSynchronizationNotSupported, TimeOfFlightAssistedAutoFocusSupported,+ // StructuredLightAssistedAutoFocusSupported+ // Resolve the canonical CFString constant by symbol name when possible (some+ // are defined in a private framework not in the dyld exports trie, so we fall+ // back to the plain string after the Fig naming convention:+ // kFigSupportedFormat_VideoMinFrameRate → "VideoMinFrameRate").+ NSString *(^figKeyN)(const char *) = ^NSString *(const char *symname) {+ void **slot = dlsym(RTLD_DEFAULT, symname);+ if (slot && *slot) return (__bridge NSString *)(CFStringRef)(*slot);+ const char *cs = symname;+ const char *u = strrchr(cs, '_');+ if (u) cs = u + 1;+ return [NSString stringWithUTF8String:cs];+ };+ attrs[figKeyN("kFigCaptureSourceAttributeKey_SmartCameraSupported")] = @NO;+ attrs[figKeyN("kFigCaptureSourceAttributeKey_StillImageNoiseReductionAndFusionScheme")] = @(0);+ attrs[figKeyN("kFigCaptureSourceAttributeKey_MidFrameSynchronizationNotSupported")] = @NO;+ attrs[figKeyN("kFigCaptureSourceAttributeKey_TimeOfFlightAssistedAutoFocusSupported")] = @NO;+ attrs[figKeyN("kFigCaptureSourceAttributeKey_StructuredLightAssistedAutoFocusSupported")] = @NO;++ // Construct a FigCaptureSourceVideoFormat via its private init taking a+ // stream format dictionary. Required keys recovered by reversing+ // `-[FigCaptureSourceFormat formatDescription]` + `-format` + `-dimensions`:+ // "Name" (NSString) -- required by base init's cbz guard+ // "Width" (NSNumber) -- becomes dimensions.width+ // "Height" (NSNumber) -- becomes dimensions.height+ // "PixelFormatType" (NSNumber) -- 4cc fed to CMVideoFormatDescriptionCreate+ // All other keys read by the subclass init fall through to default+ // (0/nil) values via objectForKeyedSubscript: chains.+ NSArray *formats = @[];+ Class fmtClass = NSClassFromString(@"FigCaptureSourceVideoFormat");+ if (fmtClass) {+ // Publish TWO formats: one BGRA, one 420v at the same 1280x720+ // dimensions. Clients that filter on BGRA (e.g. Loupe / Magnifier)+ // see one; clients that pick the canonical ISP pixel format (AVF's+ // _preferredFormatForPreset: matcher for AVCaptureSessionPresetHigh)+ // pick the 420v one. Same preset list + frame-rate range on both.+ // Phase 3: AVCaptureDeviceFormat consistency. Key names below are+ // grounded in Apple's shipped CMCapture/CMCaptureCore (extracted from+ // the dyld shared cache — same Fig framework family the guest runs);+ // semantics come from the public AVCaptureDeviceFormat API spec in+ // the SDK headers. Every value tells the same story as the delivered+ // samples (see vcam_dataplane.c):+ //+ // AVF property <- published key value+ // ---------------------------------------------------------------+ // videoFieldOfView <- VideoFieldOfView 63.0+ // (same constant derives the per-frame camera intrinsic matrix+ // focal length: fx = (w/2)/tan(FOV/2) — one knob, no drift)+ // videoSupportedFrameRateRanges <- MinFrameRate/MaxFrameRate 1..60+ // videoMaxZoomFactor <- VideoStabilizationTypeOverrideForStandard=3+ // (maxZoomFactor's dimension-table fast path; yields 16.0 for+ // 1280-wide — keeps setVideoZoomFactor: in range for Camera.app)+ // videoHDRSupported <- HDRSupported NO (8-bit SDR)+ // isCameraIntrinsicMatrixDeliverySupported+ // <- CameraCalibrationDataDeliverySupported YES+ // IntrinsicMatrixReferenceWidth/Height+ // (we attach kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix to+ // every delivered sample, so YES is truthful)+ // dimensions / formatDescription <- Width/Height/PixelFormatType 1280x720 '420v'|'BGRA'+ //+ // Deliberately NOT published (encoding unknown without runtime ground+ // truth; a wrong shape could break the FigCaptureSourceVideoFormat+ // init): HighResStillImageDimensions, supportedColorSpaces+ // ("ColorSpace"), stabilization mode lists, photo dimensions.+ NSDictionary *commonKeys = @{+ @"DefaultActiveFormat" : @NO, // overridden on the active one+ figKeyN("kFigSupportedFormat_VideoMinFrameRate") : @(1),+ figKeyN("kFigSupportedFormat_VideoMaxFrameRate") : @(60),+ // Horizontal field of view in degrees. Same constant that derives the+ // per-frame camera intrinsic matrix focal length, so AVCaptureDevice+ // format.fieldOfView and the CMSampleBuffer intrinsics tell one story.+ // Calibration knob — measure on a physical device before trusting.+ @"VideoFieldOfView" : @(VCC_VCAM_HFOV_DEG),+ // 8-bit SDR delivery: advertising HDR would be a lie AVF clients+ // would act on (tone mapping, EDR pipelines).+ @"HDRSupported" : @NO,+ // We attach kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix to+ // every delivered sample with the principal point at frame center —+ // so intrinsic delivery is supported, referenced to the full frame.+ @"CameraCalibrationDataDeliverySupported" : @YES,+ @"IntrinsicMatrixReferenceWidth" : @1280,+ @"IntrinsicMatrixReferenceHeight" : @720,+ // -[FigCaptureSourceVideoFormat maxZoomFactor] takes a fast path that+ // returns 1.0 for raw bayer formats; for BGRA (our case) it falls to+ // a fancy path that reads stabilizationTypeOverrideForCinematic / -ForStandard+ // and consults a dimension-table when either equals 3. Setting this+ // key to 3 makes the table apply (returns 16.0 for 1280-wide formats),+ // which keeps -[AVCaptureFigVideoDevice setVideoZoomFactor:] from+ // throwing "out-of-range [1, activeFormat.videoMaxZoomFactor]" when+ // Camera.app sets a default zoom > 1.0.+ @"VideoStabilizationTypeOverrideForStandard" : @(3),+ // Without AVCaptureSessionPresets, -[AVCaptureDevice+ // supportsAVCaptureSessionPreset:] returns NO for every preset+ // (canAddInput=False everywhere except InputPriority). Camera.app+ // uses High/Photo — list them all so common AVF clients pass.+ @"AVCaptureSessionPresets" : @[+ @"AVCaptureSessionPresetHigh",+ @"AVCaptureSessionPreset1280x720",+ @"AVCaptureSessionPreset640x480",+ @"AVCaptureSessionPresetMedium",+ @"AVCaptureSessionPresetLow",+ @"AVCaptureSessionPresetPhoto",+ @"AVCaptureSessionPreset352x288",+ ],+ };+ NSMutableDictionary *bgra_fmt = [@{+ @"Name" : @"vphone-vcam-720p-bgra",+ @"Width" : @1280,+ @"Height" : @720,+ @"PixelFormatType" : @(0x42475241u), // 'BGRA'+ } mutableCopy];+ [bgra_fmt addEntriesFromDictionary:commonKeys];++ NSMutableDictionary *y420v_fmt = [@{+ @"Name" : @"vphone-vcam-720p-420v",+ @"Width" : @1280,+ @"Height" : @720,+ @"PixelFormatType" : @(0x34323076u), // '420v'+ } mutableCopy];+ [y420v_fmt addEntriesFromDictionary:commonKeys];+ // Mark 420v as default — it's what AVF's preset matcher prefers when+ // picking _setActiveFormat: under standard presets.+ y420v_fmt[@"DefaultActiveFormat"] = @YES;++ SEL fmtInitSel = NSSelectorFromString(+ @"initWithFigCaptureStreamFormatDictionary:");+ NSMutableArray *formatObjs = [NSMutableArray array];+ for (NSDictionary *fmtDict in @[ bgra_fmt, y420v_fmt ]) {+ id fmtAlloc = ((id (*)(Class, SEL))objc_msgSend)(fmtClass,+ @selector(alloc));+ id fmtObj = nil;+ if (fmtAlloc) {+ fmtObj = ((id (*)(id, SEL, id))objc_msgSend)(+ fmtAlloc,+ fmtInitSel,+ fmtDict);+ }+ vcc_log(@" FigCaptureSourceVideoFormat (%@) = %p",+ fmtDict[@"Name"],+ fmtObj);+ if (fmtObj) [formatObjs addObject:fmtObj];+ }+ formats = formatObjs;+ } else {+ vcc_log(@" FigCaptureSourceVideoFormat class missing");+ }++ SEL initSel = NSSelectorFromString(+ @"initWithMediaType:attributes:cachedProperties:formats:"+ @"missingFormatNames:synchronizedStreamUniqueIDs:"+ @"unsynchronizedStreamUniqueIDs:");+ id alloced = ((id (*)(Class, SEL))objc_msgSend)(backingClass,+ @selector(alloc));+ if (!alloced) return nil;+ uint32_t mediaTypeVideo = 0x76696465; // 'vide'+ // Advertise one realtime (unsynchronized) video stream. Without a stream+ // uniqueID the client-side AVCaptureFigVideoDevice has no ports: sessions+ // never ask the vendor for streams, no capture graph is built for+ // third-party clients, and their AVCaptureVideoDataOutputs starve.+ // Camera.app never noticed because its viewfinder path is driven+ // independently.+ NSArray *unsyncStreams = @[ kVccSynthStreamID ];+ id backing = ((id (*)(id, SEL, uint32_t, id, id, id, id, id, id))objc_msgSend)(+ alloced,+ initSel,+ mediaTypeVideo,+ attrs,+ [NSMutableDictionary dictionary],+ formats,+ @[],+ @[],+ unsyncStreams);+ vcc_log(@" backing = %p (attrs.count=%lu formats.count=%lu streams=%@)",+ backing,+ (unsigned long)attrs.count,+ (unsigned long)formats.count,+ unsyncStreams);+ return backing;+}diff --git a/VPhoneGuestComponents/VCamCaptured/VCamSourceInstallation.inc b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSourceInstallation.msimilarity index 58%rename from VPhoneGuestComponents/VCamCaptured/VCamSourceInstallation.increname to VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSourceInstallation.mindex e5f18561..38cf5b12 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamSourceInstallation.inc+++ b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSourceInstallation.m@@ -1,251 +1,11 @@-// MARK: - synthetic source construction+#include <mach/mach.h>+#include <notify.h>+#include <ptrauth.h>-// Published as the synthetic source's CaptureDeviceID attribute, and matched-// against by the -[BWFigCaptureDeviceVendor copyDeviceWithID:…] hook. One-// declaration: the two must stay identical or the device lookup stops matching.-static NSString *const kVccSynthDeviceID = @"vphone:vcam:device:0";+#include "VCamImage.h"+#include "VCamSynthetic.h"-// Opaque unique ID of the one realtime video stream this source publishes.-static NSString *const kVccSynthStreamID = @"vphone:vcam:stream:video:0";--static id vcc_build_backing(void) {- Class backingClass = NSClassFromString(@"FigCaptureSourceBacking");- if (!backingClass) {- vcc_log(@" FigCaptureSourceBacking class missing");- return nil;- }- CFStringRef k_uid = vcc_cfconst("kFigCaptureSourceAttributeKey_UniqueID");- CFStringRef k_dt = vcc_cfconst("kFigCaptureSourceAttributeKey_DeviceType");- CFStringRef k_ln = vcc_cfconst("kFigCaptureSourceAttributeKey_LocalizedName");- CFStringRef k_mid = vcc_cfconst("kFigCaptureSourceAttributeKey_ModelID");- CFStringRef k_pos = vcc_cfconst("kFigCaptureSourceAttributeKey_Position");- CFStringRef k_st = vcc_cfconst("kFigCaptureSourceAttributeKey_SourceType");- CFStringRef k_cdid = vcc_cfconst("kFigCaptureSourceAttributeKey_CaptureDeviceID");- if (!k_uid || !k_dt) {- vcc_log(@" required attr keys missing");- return nil;- }-- NSMutableDictionary *attrs = [NSMutableDictionary dictionary];- attrs[(__bridge NSString *)k_uid] = @"vphone:vcam:0";- // DeviceType: empirical sweep- // - 1 (BuiltInWideAngleCamera): Camera.app routes to the real ISP /- // hardware path and fails immediately because no hardware backs- // us — viewfinder stream pool isn't even spun up.- // - 2: Camera.app's Photo/Video tabs use the generic / soft path- // and at least start the FigCameraViewfinderStream init pool- // (~2000/sec) before stalling at openWithDestination:. This is- // where we have a chance to inject a destination.- // Keep 2 until the inject-destination shape is figured out.- attrs[(__bridge NSString *)k_dt] = @(2);- if (k_ln) attrs[(__bridge NSString *)k_ln] = @"vphone Virtual Camera";- if (k_mid) attrs[(__bridge NSString *)k_mid] = @"vphone-vcam-1";- if (k_pos) attrs[(__bridge NSString *)k_pos] = @(1);- // sourceType MUST be 1 (video) for the daemon's media-type filter to- // include this source in a vide-typed FigCaptureSourceRemoteCopyCaptureSources- // request. Without it, attrs[SourceType] = nil → intValue = 0 → fails- // the tbnz w8 gate in handleCopySourcesMessage.- if (k_st) attrs[(__bridge NSString *)k_st] = @(1);- // captureSession_buildGraphWithConfiguration (CMCapture vmaddr 0x1ae2b1910)- // iterates the source list and calls- // _FigCaptureSourceGetAttribute(source, kFigCaptureSourceAttributeKey_CaptureDeviceID)- // then -[NSMutableArray addObject:] with the result. If the key is missing- // the call returns nil and the array throws NSInvalidArgumentException- // ("object cannot be nil") inside -[__NSArrayM insertObject:atIndex:] —- // which kills cameracaptured as soon as any AVCaptureSession references- // our synth.- //- // The value must be a string, NOT a number. -[BWFigCaptureDeviceVendor- // _createDevice:reason:clientPID:figCaptureDevice:] sends isEqualToString:- // to it; an NSNumber raises "unrecognized selector". Use a unique opaque- // string outside whatever pattern Apple uses for hardware cameras.- if (k_cdid) attrs[(__bridge NSString *)k_cdid] = kVccSynthDeviceID;-- // After hooking _FigCaptureSourceGetAttribute via lldb we observed- // Camera.app's graph builder also queries these 5 boolean / scheme keys- // against our synth. Provide explicit values so the builder doesn't bail- // on a nil capability check. Verified order observed via lldb:- // SmartCameraSupported, StillImageNoiseReductionAndFusionScheme,- // MidFrameSynchronizationNotSupported, TimeOfFlightAssistedAutoFocusSupported,- // StructuredLightAssistedAutoFocusSupported- // Resolve the canonical CFString constant by symbol name when possible (some- // are defined in a private framework not in the dyld exports trie, so we fall- // back to the plain string after the Fig naming convention:- // kFigSupportedFormat_VideoMinFrameRate → "VideoMinFrameRate").- NSString *(^figKeyN)(const char *) = ^NSString *(const char *symname) {- void **slot = dlsym(RTLD_DEFAULT, symname);- if (slot && *slot) return (__bridge NSString *)(CFStringRef)(*slot);- const char *cs = symname;- const char *u = strrchr(cs, '_');- if (u) cs = u + 1;- return [NSString stringWithUTF8String:cs];- };- attrs[figKeyN("kFigCaptureSourceAttributeKey_SmartCameraSupported")] = @NO;- attrs[figKeyN("kFigCaptureSourceAttributeKey_StillImageNoiseReductionAndFusionScheme")] = @(0);- attrs[figKeyN("kFigCaptureSourceAttributeKey_MidFrameSynchronizationNotSupported")] = @NO;- attrs[figKeyN("kFigCaptureSourceAttributeKey_TimeOfFlightAssistedAutoFocusSupported")] = @NO;- attrs[figKeyN("kFigCaptureSourceAttributeKey_StructuredLightAssistedAutoFocusSupported")] = @NO;-- // Construct a FigCaptureSourceVideoFormat via its private init taking a- // stream format dictionary. Required keys recovered by reversing- // `-[FigCaptureSourceFormat formatDescription]` + `-format` + `-dimensions`:- // "Name" (NSString) -- required by base init's cbz guard- // "Width" (NSNumber) -- becomes dimensions.width- // "Height" (NSNumber) -- becomes dimensions.height- // "PixelFormatType" (NSNumber) -- 4cc fed to CMVideoFormatDescriptionCreate- // All other keys read by the subclass init fall through to default- // (0/nil) values via objectForKeyedSubscript: chains.- NSArray *formats = @[];- Class fmtClass = NSClassFromString(@"FigCaptureSourceVideoFormat");- if (fmtClass) {- // Publish TWO formats: one BGRA, one 420v at the same 1280x720- // dimensions. Clients that filter on BGRA (e.g. Loupe / Magnifier)- // see one; clients that pick the canonical ISP pixel format (AVF's- // _preferredFormatForPreset: matcher for AVCaptureSessionPresetHigh)- // pick the 420v one. Same preset list + frame-rate range on both.- // Phase 3: AVCaptureDeviceFormat consistency. Key names below are- // grounded in Apple's shipped CMCapture/CMCaptureCore (extracted from- // the dyld shared cache — same Fig framework family the guest runs);- // semantics come from the public AVCaptureDeviceFormat API spec in- // the SDK headers. Every value tells the same story as the delivered- // samples (see vcam_dataplane.c):- //- // AVF property <- published key value- // ---------------------------------------------------------------- // videoFieldOfView <- VideoFieldOfView 63.0- // (same constant derives the per-frame camera intrinsic matrix- // focal length: fx = (w/2)/tan(FOV/2) — one knob, no drift)- // videoSupportedFrameRateRanges <- MinFrameRate/MaxFrameRate 1..60- // videoMaxZoomFactor <- VideoStabilizationTypeOverrideForStandard=3- // (maxZoomFactor's dimension-table fast path; yields 16.0 for- // 1280-wide — keeps setVideoZoomFactor: in range for Camera.app)- // videoHDRSupported <- HDRSupported NO (8-bit SDR)- // isCameraIntrinsicMatrixDeliverySupported- // <- CameraCalibrationDataDeliverySupported YES- // IntrinsicMatrixReferenceWidth/Height- // (we attach kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix to- // every delivered sample, so YES is truthful)- // dimensions / formatDescription <- Width/Height/PixelFormatType 1280x720 '420v'|'BGRA'- //- // Deliberately NOT published (encoding unknown without runtime ground- // truth; a wrong shape could break the FigCaptureSourceVideoFormat- // init): HighResStillImageDimensions, supportedColorSpaces- // ("ColorSpace"), stabilization mode lists, photo dimensions.- NSDictionary *commonKeys = @{- @"DefaultActiveFormat" : @NO, // overridden on the active one- figKeyN("kFigSupportedFormat_VideoMinFrameRate") : @(1),- figKeyN("kFigSupportedFormat_VideoMaxFrameRate") : @(60),- // Horizontal field of view in degrees. Same constant that derives the- // per-frame camera intrinsic matrix focal length, so AVCaptureDevice- // format.fieldOfView and the CMSampleBuffer intrinsics tell one story.- // Calibration knob — measure on a physical device before trusting.- @"VideoFieldOfView" : @(VCC_VCAM_HFOV_DEG),- // 8-bit SDR delivery: advertising HDR would be a lie AVF clients- // would act on (tone mapping, EDR pipelines).- @"HDRSupported" : @NO,- // We attach kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix to- // every delivered sample with the principal point at frame center —- // so intrinsic delivery is supported, referenced to the full frame.- @"CameraCalibrationDataDeliverySupported" : @YES,- @"IntrinsicMatrixReferenceWidth" : @1280,- @"IntrinsicMatrixReferenceHeight" : @720,- // -[FigCaptureSourceVideoFormat maxZoomFactor] takes a fast path that- // returns 1.0 for raw bayer formats; for BGRA (our case) it falls to- // a fancy path that reads stabilizationTypeOverrideForCinematic / -ForStandard- // and consults a dimension-table when either equals 3. Setting this- // key to 3 makes the table apply (returns 16.0 for 1280-wide formats),- // which keeps -[AVCaptureFigVideoDevice setVideoZoomFactor:] from- // throwing "out-of-range [1, activeFormat.videoMaxZoomFactor]" when- // Camera.app sets a default zoom > 1.0.- @"VideoStabilizationTypeOverrideForStandard" : @(3),- // Without AVCaptureSessionPresets, -[AVCaptureDevice- // supportsAVCaptureSessionPreset:] returns NO for every preset- // (canAddInput=False everywhere except InputPriority). Camera.app- // uses High/Photo — list them all so common AVF clients pass.- @"AVCaptureSessionPresets" : @[- @"AVCaptureSessionPresetHigh",- @"AVCaptureSessionPreset1280x720",- @"AVCaptureSessionPreset640x480",- @"AVCaptureSessionPresetMedium",- @"AVCaptureSessionPresetLow",- @"AVCaptureSessionPresetPhoto",- @"AVCaptureSessionPreset352x288",- ],- };- NSMutableDictionary *bgra_fmt = [@{- @"Name" : @"vphone-vcam-720p-bgra",- @"Width" : @1280,- @"Height" : @720,- @"PixelFormatType" : @(0x42475241u), // 'BGRA'- } mutableCopy];- [bgra_fmt addEntriesFromDictionary:commonKeys];-- NSMutableDictionary *y420v_fmt = [@{- @"Name" : @"vphone-vcam-720p-420v",- @"Width" : @1280,- @"Height" : @720,- @"PixelFormatType" : @(0x34323076u), // '420v'- } mutableCopy];- [y420v_fmt addEntriesFromDictionary:commonKeys];- // Mark 420v as default — it's what AVF's preset matcher prefers when- // picking _setActiveFormat: under standard presets.- y420v_fmt[@"DefaultActiveFormat"] = @YES;-- SEL fmtInitSel = NSSelectorFromString(- @"initWithFigCaptureStreamFormatDictionary:");- NSMutableArray *formatObjs = [NSMutableArray array];- for (NSDictionary *fmtDict in @[ bgra_fmt, y420v_fmt ]) {- id fmtAlloc = ((id (*)(Class, SEL))objc_msgSend)(fmtClass,- @selector(alloc));- id fmtObj = nil;- if (fmtAlloc) {- fmtObj = ((id (*)(id, SEL, id))objc_msgSend)(- fmtAlloc,- fmtInitSel,- fmtDict);- }- vcc_log(@" FigCaptureSourceVideoFormat (%@) = %p",- fmtDict[@"Name"],- fmtObj);- if (fmtObj) [formatObjs addObject:fmtObj];- }- formats = formatObjs;- } else {- vcc_log(@" FigCaptureSourceVideoFormat class missing");- }-- SEL initSel = NSSelectorFromString(- @"initWithMediaType:attributes:cachedProperties:formats:"- @"missingFormatNames:synchronizedStreamUniqueIDs:"- @"unsynchronizedStreamUniqueIDs:");- id alloced = ((id (*)(Class, SEL))objc_msgSend)(backingClass,- @selector(alloc));- if (!alloced) return nil;- uint32_t mediaTypeVideo = 0x76696465; // 'vide'- // Advertise one realtime (unsynchronized) video stream. Without a stream- // uniqueID the client-side AVCaptureFigVideoDevice has no ports: sessions- // never ask the vendor for streams, no capture graph is built for- // third-party clients, and their AVCaptureVideoDataOutputs starve.- // Camera.app never noticed because its viewfinder path is driven- // independently.- NSArray *unsyncStreams = @[ kVccSynthStreamID ];- id backing = ((id (*)(id, SEL, uint32_t, id, id, id, id, id, id))objc_msgSend)(- alloced,- initSel,- mediaTypeVideo,- attrs,- [NSMutableDictionary dictionary],- formats,- @[],- @[],- unsyncStreams);- vcc_log(@" backing = %p (attrs.count=%lu formats.count=%lu streams=%@)",- backing,- (unsigned long)attrs.count,- (unsigned long)formats.count,- unsyncStreams);- return backing;-}+// MARK: - synthetic source creation// FigCaptureSourceCreateFromBacking signature (5 args):// (CFAllocatorRef allocator, FigCaptureSourceBacking *backing,@@ -324,7 +84,7 @@ static void vcc_probe_contract(void *source) {// MARK: - _sSourceList manipulation-static void vcc_install_synthetic(void) {+void vcc_install_synthetic(void) {vcc_image_t img;int rc = vcc_image_resolve(&img, "FigCaptureSourceServerStart");if (rc != 0) {diff --git a/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSynthetic.h b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSynthetic.hnew file mode 100644index 00000000..cc9c0d88--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSynthetic.h@@ -0,0 +1,35 @@+#ifndef VCAM_SYNTHETIC_H+#define VCAM_SYNTHETIC_H++// The synthetic camera: its FigCaptureSourceBacking, the source appended+// to the daemon's source list, and the BWFigCaptureDevice /+// BWFigCaptureStream subclasses the device vendor hands out for it.++#include "VCamCapturedPrivate.h"++#pragma GCC visibility push(hidden)++// Published as the synthetic source's CaptureDeviceID attribute, and matched+// against by the -[BWFigCaptureDeviceVendor copyDeviceWithID:…] hook. One+// definition: the two must stay identical or the device lookup stops matching.+extern NSString *const kVccSynthDeviceID;++// Opaque unique ID of the one realtime video stream this source publishes.+extern NSString *const kVccSynthStreamID;++// The FigCaptureSourceBacking describing the synthetic source (attributes+// and formats), or nil when a required class or key is missing.+id vcc_build_backing(void);++// VccSynthDevice, registered on first use; Nil until then or on failure.+extern Class vcc_synth_device_class;++// Offset of BWFigCaptureDevice's deviceID ivar; -1 until resolved.+extern ptrdiff_t kBWFigCaptureDevice_deviceID_Offset;++// Offset of the first ivar in `names` (NULL-terminated) found on cls, or -1.+ptrdiff_t vcc_resolve_ivar(Class cls, const char *what, const char *const *names);++#pragma GCC visibility pop++#endifdiff --git a/VPhoneGuestComponents/VCamCaptured/VCamSyntheticDevice.inc b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSyntheticDevice.msimilarity index 61%rename from VPhoneGuestComponents/VCamCaptured/VCamSyntheticDevice.increname to VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSyntheticDevice.mindex 833c8b94..e3515660 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamSyntheticDevice.inc+++ b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSyntheticDevice.m@@ -1,3 +1,6 @@+#include "VCamImage.h"+#include "VCamSynthetic.h"+// MARK: - synthetic FigCaptureDevice subclass//// Subclass `BWFigCaptureDevice` at runtime via objc_allocateClassPair, then@@ -18,15 +21,17 @@// Resolved at synth-class init. -1 until populated; vcc_init_synth_device_class// refuses to register the synth class if resolution fails so we never write// to a stale offset.-static ptrdiff_t kBWFigCaptureDevice_deviceID_Offset = -1;+ptrdiff_t kBWFigCaptureDevice_deviceID_Offset = -1;++Class vcc_synth_device_class = Nil;// Resolve an instance-variable offset by name on `cls`, trying each// candidate in `names` (NULL-terminated) until one matches. Returns -1// if none match. Logs the outcome so a future iOS rename surfaces in// the install log instead of silently writing to a wrong slot.-static ptrdiff_t vcc_resolve_ivar(Class cls,- const char *what,- const char *const *names) {+ptrdiff_t vcc_resolve_ivar(Class cls,+ const char *what,+ const char *const *names) {if (!cls) {vcc_log(@" ivar %s: class missing", what ? what : "?");return -1;@@ -176,3 +181,101 @@ static void vcc_init_synth_device_class(void) {vcc_synth_device_class = cls;vcc_log(@" registered VccSynthDevice : BWFigCaptureDevice (cls=%p)", cls);}++__attribute__((ns_returns_retained))+static id vcc_make_synth_device(NSString *deviceID) {+ if (!vcc_synth_device_class) vcc_init_synth_device_class();+ if (!vcc_synth_device_class) return nil;++ // Use +alloc via runtime — ARC-safe. Function attributed+ // ns_returns_retained so ARC treats the alloc'd +1 correctly.+ id d = ((id (*)(Class, SEL))objc_msgSend)(vcc_synth_device_class,+ @selector(alloc));+ if (!d) {+ vcc_log(@" synth-dev: class_createInstance failed");+ return nil;+ }+ // Write deviceID at the runtime-resolved ivar offset via+ // CFBridgingRetain so our -dealloc override CFRelease's it.+ NSString *idCopy = [deviceID copy];+ void *slotBase = (__bridge void *)d;+ void **slot = (void **)((char *)slotBase + kBWFigCaptureDevice_deviceID_Offset);+ *slot = (void *)CFBridgingRetain(idCopy);++ // Pin so dealloc never runs — diagnostic to isolate the autorelease+ // pool drain crash. Each session start leaks one synth device.+ static NSMutableArray *vcc_synth_devs_strong_refs = nil;+ if (!vcc_synth_devs_strong_refs) {+ vcc_synth_devs_strong_refs = [NSMutableArray new];+ }+ [vcc_synth_devs_strong_refs addObject:d];++ vcc_log(@" synth-dev: %p deviceID=%@ (slot@+0x%lx=%p)",+ d, idCopy, (long)kBWFigCaptureDevice_deviceID_Offset, *slot);+ return d;+}++// MARK: - hook BWFigCaptureDeviceVendor copyDeviceWithID:+//+// Hook -[BWFigCaptureDeviceVendor copyDeviceWithID:forClient:informClientWhenDeviceAvailableAgain:error:]+// to see when an AVF client asks for our device, and hand out a+// VccSynthDevice for our synth's CaptureDeviceID. If this never fires, the+// client never reached the device-vendor stage (gated upstream).++static IMP vcc_copy_device_orig = NULL;++// Type encoding observed on iOS 26.5 cameracaptured:+// @40@0:8@16i24B28^i32+// => id (*)(id self, SEL _cmd, NSString *deviceID, int clientPID,+// BOOL informClient, int *err)+// The earlier disabled stub mis-typed clientPID as `id`, causing ARC to+// emit objc_retain on an integer register and crash inside the hook+// prologue.+//+// The selector starts with "copy", so the caller owns the result. Both the+// original and the hook are declared ns_returns_retained: without it ARC+// autoreleases the returned device, and the caller's release then drops+// one reference too many.++typedef id (*VccCopyDeviceFn)(id self,+ SEL _cmd,+ NSString *deviceID,+ int clientPID,+ BOOL informClient,+ int *err) __attribute__((ns_returns_retained));++__attribute__((ns_returns_retained))+static id vcc_copy_device_hook(id self,+ SEL _cmd,+ NSString *deviceID,+ int clientPID,+ BOOL informClient,+ int *err) {+ vcc_log(@" [copyDeviceWithID] deviceID=%@ clientPID=%d inform=%d",+ deviceID, clientPID, informClient);++ if ([deviceID isEqualToString:kVccSynthDeviceID]) {+ id synth = vcc_make_synth_device(deviceID);+ if (err) *err = 0;+ vcc_log(@" [copyDeviceWithID] -> synth %p (class=%s err=0)",+ synth, object_getClassName(synth));+ return synth;+ }++ VccCopyDeviceFn orig = (VccCopyDeviceFn)vcc_copy_device_orig;+ id ret = orig(self, _cmd, deviceID, clientPID, informClient, err);+ int errval = err ? *err : 0;+ vcc_log(@" [copyDeviceWithID] returned %p (err=%d)", ret, errval);+ return ret;+}++void vcc_install_device_vendor_hook(void) {+ Class cls = NSClassFromString(@"BWFigCaptureDeviceVendor");+ if (!cls) {+ vcc_log(@" device-vendor hook: class missing");+ return;+ }+ SEL sel = NSSelectorFromString(+ @"copyDeviceWithID:forClient:informClientWhenDeviceAvailableAgain:error:");+ vcc_swizzle_method(cls, sel, (IMP)vcc_copy_device_hook, &vcc_copy_device_orig);+}diff --git a/VPhoneGuestComponents/VCamCaptured/VCamSyntheticStreams.inc b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSyntheticStreams.msimilarity index 88%rename from VPhoneGuestComponents/VCamCaptured/VCamSyntheticStreams.increname to VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSyntheticStreams.mindex e09b0923..e0f9c09a 100644--- a/VPhoneGuestComponents/VCamCaptured/VCamSyntheticStreams.inc+++ b/VPhoneGuestComponents/VCamCaptured/Synthetic/VCamSyntheticStreams.m@@ -1,3 +1,6 @@+#include "VCamImage.h"+#include "VCamSynthetic.h"+// MARK: - synthetic FigCaptureStream subclass//// Subclass BWFigCaptureStream. Ivar offsets are resolved at synth-class@@ -236,6 +239,11 @@ static id vcc_make_synth_stream(NSString *uniqueID,// uniqueID so that the vendor's identity-matching inner loop in// `-_copyStreamsForAttributes:` produces a result array whose count matches// the input attributes count.+//+// Both vendor selectors start with "copy", so the caller owns the returned+// array. The originals and the hooks are declared ns_returns_retained;+// without it ARC autoreleases the array and the caller's release over-+// releases it when the autorelease pool drains.static IMP vcc_copy_streams_orig = NULL;typedef id (*VccCopyStreamsFn)(id self,@@ -243,8 +251,9 @@ typedef id (*VccCopyStreamsFn)(id self,NSArray *uniqueIDs,id forDevice,int priority,- int *err);+ int *err) __attribute__((ns_returns_retained));+__attribute__((ns_returns_retained))static id vcc_copy_streams_hook(id self,SEL _cmd,NSArray *uniqueIDs,@@ -269,7 +278,7 @@ static id vcc_copy_streams_hook(id self,return orig(self, _cmd, uniqueIDs, forDevice, priority, err);}-static void vcc_install_copy_streams_hook(void) {+void vcc_install_copy_streams_hook(void) {Class cls = NSClassFromString(@"BWFigCaptureDeviceVendor");if (!cls) return;SEL sel = NSSelectorFromString(@@ -285,8 +294,9 @@ typedef id (*VccCopyStreamsFromFn)(id self,NSArray *deviceTypes,int prio,BOOL allowsLoss,- int *err);+ int *err) __attribute__((ns_returns_retained));+__attribute__((ns_returns_retained))static id vcc_copy_streams_from_hook(id self,SEL _cmd,id fromDevice,@@ -318,7 +328,7 @@ static id vcc_copy_streams_from_hook(id self,prio, allowsLoss, err);}-static void vcc_install_copy_streams_from_hook(void) {+void vcc_install_copy_streams_from_hook(void) {Class cls = NSClassFromString(@"BWFigCaptureDeviceVendor");if (!cls) return;SEL sel = NSSelectorFromString(@@ -326,47 +336,3 @@ static void vcc_install_copy_streams_from_hook(void) {vcc_swizzle_method(cls, sel, (IMP)vcc_copy_streams_from_hook,&vcc_copy_streams_from_orig);}--__attribute__((ns_returns_retained))-static id vcc_make_synth_device(NSString *deviceID) {- if (!vcc_synth_device_class) vcc_init_synth_device_class();- if (!vcc_synth_device_class) return nil;-- // Use +alloc via runtime — ARC-safe. Function attributed- // ns_returns_retained so ARC treats the alloc'd +1 correctly.- id d = ((id (*)(Class, SEL))objc_msgSend)(vcc_synth_device_class,- @selector(alloc));- if (!d) {- vcc_log(@" synth-dev: class_createInstance failed");- return nil;- }- // Write deviceID at the runtime-resolved ivar offset via- // CFBridgingRetain so our -dealloc override CFRelease's it.- NSString *idCopy = [deviceID copy];- void *slotBase = (__bridge void *)d;- void **slot = (void **)((char *)slotBase + kBWFigCaptureDevice_deviceID_Offset);- *slot = (void *)CFBridgingRetain(idCopy);-- // Pin so dealloc never runs — diagnostic to isolate the autorelease- // pool drain crash. Each session start leaks one synth device.- static NSMutableArray *vcc_synth_devs_strong_refs = nil;- if (!vcc_synth_devs_strong_refs) {- vcc_synth_devs_strong_refs = [NSMutableArray new];- }- [vcc_synth_devs_strong_refs addObject:d];-- vcc_log(@" synth-dev: %p deviceID=%@ (slot@+0x%lx=%p)",- d, idCopy, (long)kBWFigCaptureDevice_deviceID_Offset, *slot);- return d;-}--static void vcc_install_device_vendor_hook(void) {- Class cls = NSClassFromString(@"BWFigCaptureDeviceVendor");- if (!cls) {- vcc_log(@" device-vendor hook: class missing");- return;- }- SEL sel = NSSelectorFromString(- @"copyDeviceWithID:forClient:informClientWhenDeviceAvailableAgain:error:");- vcc_swizzle_method(cls, sel, (IMP)vcc_copy_device_hook, &vcc_copy_device_orig);-}diff --git a/VPhoneGuestComponents/VCamCaptured/VCamCapturedPrivate.h b/VPhoneGuestComponents/VCamCaptured/VCamCapturedPrivate.hnew file mode 100644index 00000000..7601ffbd--- /dev/null+++ b/VPhoneGuestComponents/VCamCaptured/VCamCapturedPrivate.h@@ -0,0 +1,64 @@+#ifndef VCAM_CAPTURED_PRIVATE_H+#define VCAM_CAPTURED_PRIVATE_H++// Shared by every libvcamcaptured translation unit. Each subsystem folder+// has its own header for the state and helpers it owns; this one holds the+// dylib-wide helpers and the install entry points the constructor calls.+//+// Everything declared between the visibility pragmas is internal to the+// dylib: it links across files but stays out of the export table.++#import <CoreFoundation/CoreFoundation.h>+#import <CoreMedia/CoreMedia.h>+#import <CoreVideo/CoreVideo.h>+#import <Foundation/Foundation.h>+#import <objc/message.h>+#import <objc/runtime.h>+#include <dlfcn.h>+#include <errno.h>+#include <fcntl.h>+#include <pthread.h>+#include <stdint.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include "VCamFrameProtocol.h"++#pragma GCC visibility push(hidden)++// MARK: - logging and swizzling++// Appends one line to VPHONE_VCAM_CAPTURE_LOG_PATH and mirrors it to NSLog.+void vcc_log(NSString *fmt, ...);++// Replaces -[cls sel] with newImp and stores the previous IMP in *outOrig.+// Logs and leaves *outOrig untouched when the method is missing.+void vcc_swizzle_method(Class cls, SEL sel, IMP newImp, IMP *outOrig);++// MARK: - install entry points (called once from the constructor)++void vcc_install_synthetic(void);+void vcc_start_frame_receiver(void);+void vcc_install_endpoint_hook(void);+void vcc_install_sink_observation(void);+void vcc_install_still_sink_observation(void);+void vcc_install_session_graph_observation(void);+void vcc_install_still_coordinator_observation(void);+void vcc_install_still_coord_node_observation(void);+void vcc_install_still_pipeline_observation(void);+void vcc_install_pipelines_addStill_observation(void);+void vcc_install_parsed_cfg_observation(void);+void vcc_install_csp_requires_master_clock_hook(void);+void vcc_install_session_init_capture(void);+void vcc_construct_still_sink(void);+void vcc_drive_still_sink_once(void);+void vcc_install_viewfinder_hooks(void);+void vcc_dump_sink_node_methods(void);+void vcc_install_device_vendor_hook(void);+void vcc_install_copy_streams_hook(void);+void vcc_install_copy_streams_from_hook(void);++#pragma GCC visibility pop++#endifdiff --git a/VPhoneGuestComponents/VCamCaptured/libvcamcaptured.m b/VPhoneGuestComponents/VCamCaptured/libvcamcaptured.mindex 489d4ab0..b5485697 100644--- a/VPhoneGuestComponents/VCamCaptured/libvcamcaptured.m+++ b/VPhoneGuestComponents/VCamCaptured/libvcamcaptured.m@@ -33,48 +33,21 @@* matches; we accept only when all matches agree).*/-#import <CoreFoundation/CoreFoundation.h>-#import <CoreMedia/CoreMedia.h>-#import <CoreVideo/CoreVideo.h>-#import <Foundation/Foundation.h>-#import <objc/message.h>-#import <objc/runtime.h>-#include <dlfcn.h>-#include <errno.h>-#include <fcntl.h>-#include <libkern/OSCacheControl.h>-#include <mach-o/dyld.h>-#include <mach-o/getsect.h>-#include <mach-o/loader.h>-#include <mach-o/nlist.h>-#include <mach/mach.h>-#include <malloc/malloc.h>-#include <notify.h>-#include <ptrauth.h>-#include <pthread.h>#include <stdarg.h>-#include <stdatomic.h>-#include <stdint.h>-#include <stdlib.h>-#include <string.h>-#include <sys/mman.h>-#include <sys/stat.h>-#include <sys/types.h>-#include <unistd.h>-#include "VCamFrameProtocol.h"-#include "vcam_dataplane.h"-// MARK: - shared frame layout (matches vphoned_vcam.h)-//-// cameracaptured's sandbox blocks AF_VSOCK socket creation, so the frame-// receiver lives in vphoned (root, has vsock perms). vphoned writes-// frames into a shared mmap and posts a Darwin notification; we map the-// file read-only, subscribe to the notification, and copy out the latest-// frame on each fire.+#include "VCamCapturedPrivate.h"++// Each subsystem lives in its own folder and translation unit:+// Image/ CMCapture image resolution, structural scans, code patches+// Synthetic/ the synthetic source, device and stream classes+// Frames/ the shm frame reader and viewfinder / sink delivery+// Hooks/ observation and delivery hooks on the capture graph+// Cross-file symbols are declared in each folder's header with hidden+// visibility, so none becomes a public dylib interface.// MARK: - sentinel logging-static void vcc_log(NSString *fmt, ...) {+void vcc_log(NSString *fmt, ...) {va_list args;va_start(args, fmt);NSString *msg = [[NSString alloc] initWithFormat:fmt arguments:args];@@ -95,19 +68,19 @@ static void vcc_log(NSString *fmt, ...) {NSLog(@"vcamcaptured: %@", msg);}+// MARK: - method swizzling-// The implementation remains one translation unit because these hooks share-// private runtime state and order-sensitive symbol declarations. Each include-// owns one capture subsystem; none becomes a public dylib interface.-#include "VCamImageResolution.inc"-#include "VCamSourceInstallation.inc"-#include "VCamCaptureObservation.inc"-#include "VCamSyntheticDevice.inc"-#include "VCamSyntheticStreams.inc"-#include "VCamViewfinderHooks.inc"-#include "VCamSessionHooks.inc"-#include "VCamStillSink.inc"-#include "VCamFrameReceiver.inc"+void vcc_swizzle_method(Class cls, SEL sel, IMP newImp, IMP *outOrig) {+ Method m = class_getInstanceMethod(cls, sel);+ if (!m) {+ vcc_log(@" swizzle: -[%s %@] missing", class_getName(cls),+ NSStringFromSelector(sel));+ return;+ }+ *outOrig = method_setImplementation(m, newImp);+ vcc_log(@" swizzled -[%s %@] (orig imp=%p)", class_getName(cls),+ NSStringFromSelector(sel), *outOrig);+}// MARK: - constructor