Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Run cameracaptured's frame drive only while a client needs it

Jacky22bd75274a98
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 5a7fb9ab..4bbb4fa9 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -61,3 +61,4 @@ jobs:
make -C VPhoneGuestComponents test-loader-links
make -C VPhoneGuestComponents test-vcam-dataplane
make -C VPhoneGuestComponents test-vcam-prewarm
+ make -C VPhoneGuestComponents test-vcam-drive-gate
diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefile
index 699da39b..dfea3377 100644
--- a/VPhoneGuestComponents/Makefile
+++ b/VPhoneGuestComponents/Makefile
@@ -360,6 +360,16 @@ test-vcam-prewarm:
Tests/VCamPrewarmScanTests.c VCamCaptured/Prewarm/VCamPrewarmScan.c
@$(OUT)/VCamPrewarmScanTests
+# Host proof harness for the gate that runs cameracaptured's frame drive only
+# while a camera client exists.
+.PHONY: test-vcam-drive-gate
+test-vcam-drive-gate:
+ @mkdir -p $(OUT)
+ xcrun --sdk macosx clang -O1 -Wall -Wextra -Werror -IVCamCaptured/Frames \
+ -o $(OUT)/VCamDriveGateTests \
+ Tests/VCamDriveGateTests.c VCamCaptured/Frames/VCamDriveGate.c
+ @$(OUT)/VCamDriveGateTests
+
.PHONY: test-installcoordination-persona
test-installcoordination-persona:
@mkdir -p $(OUT)
diff --git a/VPhoneGuestComponents/Tests/VCamDriveGateTests.c b/VPhoneGuestComponents/Tests/VCamDriveGateTests.c
new file mode 100644
index 00000000..11be3eaf
--- /dev/null
+++ b/VPhoneGuestComponents/Tests/VCamDriveGateTests.c
@@ -0,0 +1,173 @@
+/*
+ * Host proof harness for the gate that keeps cameracaptured's 30 Hz frame
+ * drive running only while a viewfinder stream or video sink exists
+ * (VCamCaptured/Frames/VCamDriveGate.c).
+ *
+ * Build/run: make -C VPhoneGuestComponents test-vcam-drive-gate
+ */
+
+#include "VCamDriveGate.h"
+
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdio.h>
+
+static int g_checks = 0;
+static int g_fails = 0;
+
+#define CHECK(cond, ...) \
+ do { \
+ g_checks++; \
+ if (!(cond)) { \
+ g_fails++; \
+ printf("FAIL %s:%d: ", __FILE__, __LINE__); \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ } \
+ } while (0)
+
+// The timer as the callbacks see it: armed or not, and how often each
+// transition happened. A start while armed or a stop while disarmed would be
+// a lost or doubled transition.
+typedef struct {
+ int armed;
+ int starts;
+ int stops;
+ int bad;
+} fake_timer_t;
+
+static void fake_start(void *context) {
+ fake_timer_t *t = context;
+ if (t->armed) t->bad++;
+ t->armed = 1;
+ t->starts++;
+}
+
+static void fake_stop(void *context) {
+ fake_timer_t *t = context;
+ if (!t->armed) t->bad++;
+ t->armed = 0;
+ t->stops++;
+}
+
+static void test_starts_stopped(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ CHECK(!vcc_drive_gate_is_running(&gate), "a new gate must not run");
+ CHECK(t.starts == 0 && t.stops == 0, "init must not touch the timer");
+}
+
+static void test_wake_starts_once(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ vcc_drive_gate_wake(&gate);
+ vcc_drive_gate_wake(&gate);
+ vcc_drive_gate_wake(&gate);
+ CHECK(t.starts == 1, "three wakes started the timer %d times", t.starts);
+ CHECK(t.armed && vcc_drive_gate_is_running(&gate), "the timer must run");
+}
+
+static void test_busy_tick_keeps_running(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ vcc_drive_gate_wake(&gate);
+ for (int i = 0; i < 100; i++) {
+ uint64_t seen = vcc_drive_gate_begin_tick(&gate);
+ CHECK(!vcc_drive_gate_end_tick(&gate, seen, true), "a tick with consumers stopped the timer");
+ }
+ CHECK(t.armed && t.stops == 0, "the timer must still run");
+}
+
+static void test_idle_tick_stops(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ vcc_drive_gate_wake(&gate);
+ uint64_t seen = vcc_drive_gate_begin_tick(&gate);
+ CHECK(vcc_drive_gate_end_tick(&gate, seen, false), "an idle tick must stop the timer");
+ CHECK(!t.armed && t.stops == 1, "the timer must be stopped once");
+ // A late tick already queued when the timer stopped changes nothing.
+ seen = vcc_drive_gate_begin_tick(&gate);
+ CHECK(!vcc_drive_gate_end_tick(&gate, seen, false), "a stopped gate stopped again");
+ CHECK(t.stops == 1 && t.bad == 0, "stops %d, bad transitions %d", t.stops, t.bad);
+}
+
+static void test_wake_during_tick_keeps_running(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ vcc_drive_gate_wake(&gate);
+ // The tick snapshots no consumer, then a sink is built before it ends.
+ uint64_t seen = vcc_drive_gate_begin_tick(&gate);
+ vcc_drive_gate_wake(&gate);
+ CHECK(!vcc_drive_gate_end_tick(&gate, seen, false),
+ "a consumer that arrived during the tick was dropped");
+ CHECK(t.armed && t.starts == 1 && t.stops == 0, "the timer must keep running");
+ // The next tick sees it gone again and stops.
+ seen = vcc_drive_gate_begin_tick(&gate);
+ CHECK(vcc_drive_gate_end_tick(&gate, seen, false), "the following idle tick must stop");
+}
+
+static void test_restart_after_stop(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ for (int round = 0; round < 5; round++) {
+ vcc_drive_gate_wake(&gate);
+ uint64_t seen = vcc_drive_gate_begin_tick(&gate);
+ vcc_drive_gate_end_tick(&gate, seen, true);
+ seen = vcc_drive_gate_begin_tick(&gate);
+ vcc_drive_gate_end_tick(&gate, seen, false);
+ }
+ CHECK(t.starts == 5 && t.stops == 5 && !t.armed && t.bad == 0,
+ "five camera sessions: starts %d, stops %d, armed %d, bad %d", t.starts, t.stops, t.armed,
+ t.bad);
+}
+
+// Wakes from graph-build threads race idle ticks from the drive queue. Every
+// transition must alternate, and a wake must never be left with the timer off.
+typedef struct {
+ vcc_drive_gate_t *gate;
+ atomic_int *done;
+} race_arg_t;
+
+static void *waker(void *arg) {
+ race_arg_t *r = arg;
+ for (int i = 0; i < 20000; i++) vcc_drive_gate_wake(r->gate);
+ atomic_store(r->done, 1);
+ return NULL;
+}
+
+static void test_concurrent_wakes_and_ticks(void) {
+ fake_timer_t t = {0};
+ vcc_drive_gate_t gate;
+ vcc_drive_gate_init(&gate, fake_start, fake_stop, &t);
+ atomic_int done = 0;
+ race_arg_t arg = {&gate, &done};
+ pthread_t thread;
+ pthread_create(&thread, NULL, waker, &arg);
+ while (!atomic_load(&done)) {
+ uint64_t seen = vcc_drive_gate_begin_tick(&gate);
+ vcc_drive_gate_end_tick(&gate, seen, false);
+ }
+ pthread_join(thread, NULL);
+ CHECK(t.bad == 0, "%d transitions out of order", t.bad);
+ CHECK(t.starts == t.stops + (t.armed ? 1 : 0), "starts %d, stops %d, armed %d", t.starts,
+ t.stops, t.armed);
+ CHECK(vcc_drive_gate_is_running(&gate) == (t.armed != 0), "gate and timer disagree");
+}
+
+int main(void) {
+ test_starts_stopped();
+ test_wake_starts_once();
+ test_busy_tick_keeps_running();
+ test_idle_tick_stops();
+ test_wake_during_tick_keeps_running();
+ test_restart_after_stop();
+ test_concurrent_wakes_and_ticks();
+ printf("VCamDriveGateTests: %d checks, %d failures\n", g_checks, g_fails);
+ return g_fails == 0 ? 0 : 1;
+}
diff --git a/VPhoneGuestComponents/VCamCaptured/Frames/VCamDriveGate.c b/VPhoneGuestComponents/VCamCaptured/Frames/VCamDriveGate.c
new file mode 100644
index 00000000..511fc10c
--- /dev/null
+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamDriveGate.c
@@ -0,0 +1,52 @@
+#include "VCamDriveGate.h"
+
+void vcc_drive_gate_init(vcc_drive_gate_t *gate,
+ vcc_drive_gate_fn start,
+ vcc_drive_gate_fn stop,
+ void *context) {
+ pthread_mutex_init(&gate->lock, NULL);
+ gate->demand = 0;
+ gate->running = false;
+ gate->start = start;
+ gate->stop = stop;
+ gate->context = context;
+}
+
+void vcc_drive_gate_wake(vcc_drive_gate_t *gate) {
+ pthread_mutex_lock(&gate->lock);
+ gate->demand++;
+ if (!gate->running) {
+ gate->running = true;
+ gate->start(gate->context);
+ }
+ pthread_mutex_unlock(&gate->lock);
+}
+
+uint64_t vcc_drive_gate_begin_tick(vcc_drive_gate_t *gate) {
+ pthread_mutex_lock(&gate->lock);
+ uint64_t seen = gate->demand;
+ pthread_mutex_unlock(&gate->lock);
+ return seen;
+}
+
+bool vcc_drive_gate_end_tick(vcc_drive_gate_t *gate, uint64_t seen, bool found) {
+ if (found) return false;
+ bool stopped = false;
+ pthread_mutex_lock(&gate->lock);
+ // A consumer that arrived during the tick may not have been in its
+ // snapshot; the next tick sees it.
+ if (gate->running && gate->demand == seen) {
+ gate->running = false;
+ gate->stop(gate->context);
+ stopped = true;
+ }
+ pthread_mutex_unlock(&gate->lock);
+ return stopped;
+}
+
+bool vcc_drive_gate_is_running(vcc_drive_gate_t *gate) {
+ pthread_mutex_lock(&gate->lock);
+ bool running = gate->running;
+ pthread_mutex_unlock(&gate->lock);
+ return running;
+}
diff --git a/VPhoneGuestComponents/VCamCaptured/Frames/VCamDriveGate.h b/VPhoneGuestComponents/VCamCaptured/Frames/VCamDriveGate.h
new file mode 100644
index 00000000..a34f0d9a
--- /dev/null
+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamDriveGate.h
@@ -0,0 +1,51 @@
+#ifndef VCAM_DRIVE_GATE_H
+#define VCAM_DRIVE_GATE_H
+
+// Keeps the 30 Hz frame drive timer running only while something can take a
+// frame. The timer used to fire for the daemon's whole life, waking an idle
+// cameracaptured 30 times a second with no camera client at all.
+//
+// Every new consumer (an opened viewfinder stream, a new video sink) wakes the
+// gate, which starts the timer if it was stopped. A tick that finds nothing to
+// drive stops it again, unless a consumer arrived while that tick ran. The
+// start and stop callbacks run under the gate's lock, so a stop can never
+// overtake a later start.
+
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#pragma GCC visibility push(hidden)
+
+typedef void (*vcc_drive_gate_fn)(void *context);
+
+typedef struct vcc_drive_gate_s {
+ pthread_mutex_t lock;
+ // Bumped by every wake; a tick compares it with what it saw at its start.
+ uint64_t demand;
+ bool running;
+ vcc_drive_gate_fn start;
+ vcc_drive_gate_fn stop;
+ void *context;
+} vcc_drive_gate_t;
+
+void vcc_drive_gate_init(vcc_drive_gate_t *gate,
+ vcc_drive_gate_fn start,
+ vcc_drive_gate_fn stop,
+ void *context);
+
+// A consumer appeared. Starts the timer when it is stopped.
+void vcc_drive_gate_wake(vcc_drive_gate_t *gate);
+
+// Called at the beginning of a tick; pass the result to vcc_drive_gate_end_tick.
+uint64_t vcc_drive_gate_begin_tick(vcc_drive_gate_t *gate);
+
+// Called at the end of a tick. `found` is whether the tick had any consumer
+// to drive. Returns true when the gate stopped the timer.
+bool vcc_drive_gate_end_tick(vcc_drive_gate_t *gate, uint64_t seen, bool found);
+
+bool vcc_drive_gate_is_running(vcc_drive_gate_t *gate);
+
+#pragma GCC visibility pop
+
+#endif
diff --git a/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.h b/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.h
index 1db1ee47..6de89136 100644
--- a/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.h
+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamFrames.h
@@ -30,6 +30,10 @@ extern vcc_latest_frame_t vcc_latest_frame;
// NULL when no frame has arrived. Caller must CFRelease.
CMSampleBufferRef vcc_build_cmsb_from_shm_fmt(uint32_t fmt_out) CF_RETURNS_RETAINED;
+// A frame consumer (viewfinder stream or video sink) appeared: run the 30 Hz
+// drive until none is left (VCamDriveGate.h).
+void vcc_frame_drive_wake(void);
+
#pragma GCC visibility pop
#endif
diff --git a/VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.m b/VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.m
index 6e67e708..914ffd2c 100644
--- a/VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.m
+++ b/VPhoneGuestComponents/VCamCaptured/Frames/VCamViewfinderHooks.m
@@ -1,5 +1,6 @@
#include <stdatomic.h>
+#include "VCamDriveGate.h"
#include "VCamFrames.h"
#include "VCamHooks.h"
#include "vcam_dataplane.h"
@@ -36,11 +37,12 @@ static uint64_t vcc_sink_drive_ok = 0;
// viewfinder drive. Graph-built sinks expect the active advertised format's
// samples — BGRA is what clients see selected (camfix substitutes the first
// format for third-party sessions).
-static void vcc_drive_sinks_once(void) {
+// Returns whether any sink exists, delivered to or not.
+static bool vcc_drive_sinks_once(void) {
NSArray *sinks = vcc_driven_sinks_snapshot();
- if (sinks.count == 0) return;
+ if (sinks.count == 0) return false;
CMSampleBufferRef cmsb = vcc_build_cmsb_from_shm_fmt(VCC_FMT_BGRA);
- if (!cmsb) return;
+ if (!cmsb) return true;
SEL sel = @selector(renderSampleBuffer:forInput:);
for (id sink in sinks) {
if (![sink respondsToSelector:sel]) continue;
@@ -62,6 +64,7 @@ static void vcc_drive_sinks_once(void) {
(unsigned long)sinks.count,
(unsigned long long)vcc_sink_drive_ok);
}
+ return true;
}
typedef id (*VccVfsInitFn)(id self, SEL _cmd);
@@ -93,6 +96,7 @@ static void vcc_vfs_open_hook(id self, SEL _cmd, id dest) {
[vcc_vf_streams addObject:self];
NSUInteger total = vcc_vf_streams.count;
pthread_mutex_unlock(&vcc_vf_streams_lock);
+ vcc_frame_drive_wake();
vcc_log(@" [VFS open] captured stream %p (total=%lu)", self, (unsigned long)total);
}
@@ -168,13 +172,14 @@ static CMSampleBufferRef vcc_build_cmsb_from_shm(void) {
return vcc_build_cmsb_from_shm_fmt(VCC_FMT_420V);
}
-static void vcc_vf_drive_once(void) {
+// Returns whether any stream is open, delivered to or not.
+static bool vcc_vf_drive_once(void) {
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;
+ if (streams.count == 0) return false;
CMSampleBufferRef cmsb = vcc_build_cmsb_from_shm();
- if (!cmsb) return;
+ if (!cmsb) return true;
SEL sel = NSSelectorFromString(@"enqueueVideoSampleBuffer:");
for (id stream in streams) {
int ret = ((int (*)(id, SEL, CMSampleBufferRef))objc_msgSend)(
@@ -189,6 +194,60 @@ static void vcc_vf_drive_once(void) {
}
}
CFRelease(cmsb);
+ return true;
+}
+
+// MARK: - drive timer
+//
+// 30 Hz while a viewfinder stream is open or a video sink exists, stopped
+// otherwise (VCamDriveGate.h). Created on first use: a sink can be built
+// before the viewfinder hooks are installed.
+
+static const uint64_t vcc_vf_interval_ns = 33333333ull;
+static const uint64_t vcc_vf_leeway_ns = 2000000ull;
+static vcc_drive_gate_t vcc_vf_gate;
+static uint64_t vcc_vf_starts = 0;
+
+static void vcc_vf_timer_start(void *context) {
+ (void)context;
+ dispatch_source_set_timer(vcc_vf_timer, dispatch_time(DISPATCH_TIME_NOW, 0),
+ vcc_vf_interval_ns, vcc_vf_leeway_ns);
+ vcc_vf_starts++;
+ vcc_log(@" viewfinder drive started (30 Hz, start #%llu)",
+ (unsigned long long)vcc_vf_starts);
+}
+
+static void vcc_vf_timer_stop(void *context) {
+ (void)context;
+ dispatch_source_set_timer(vcc_vf_timer, DISPATCH_TIME_FOREVER, 0, 0);
+ vcc_log(@" viewfinder drive stopped: no stream open, no sink left");
+}
+
+static void vcc_vf_drive_setup(void) {
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ vcc_vf_q = dispatch_queue_create("com.vphone.vcam.vfdrive",
+ DISPATCH_QUEUE_SERIAL);
+ vcc_vf_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
+ vcc_vf_q);
+ dispatch_source_set_timer(vcc_vf_timer, DISPATCH_TIME_FOREVER, 0, 0);
+ dispatch_source_set_event_handler(vcc_vf_timer, ^{
+ @autoreleasepool {
+ uint64_t seen = vcc_drive_gate_begin_tick(&vcc_vf_gate);
+ bool streams = vcc_vf_drive_once();
+ bool sinks = vcc_drive_sinks_once();
+ vcc_drive_gate_end_tick(&vcc_vf_gate, seen, streams || sinks);
+ }
+ });
+ vcc_drive_gate_init(&vcc_vf_gate, vcc_vf_timer_start, vcc_vf_timer_stop,
+ NULL);
+ dispatch_resume(vcc_vf_timer);
+ });
+}
+
+void vcc_frame_drive_wake(void) {
+ vcc_vf_drive_setup();
+ vcc_drive_gate_wake(&vcc_vf_gate);
}
void vcc_install_viewfinder_hooks(void) {
@@ -212,22 +271,7 @@ void vcc_install_viewfinder_hooks(void) {
}
vcc_log(@" swizzled FigCameraViewfinderStream init/open/close");
- // Drive at 30 Hz. Frames are enqueued only when at least one stream is
- // open AND a fresh shm frame exists (covered by build_cmsb_from_shm).
- vcc_vf_q = dispatch_queue_create("com.vphone.vcam.vfdrive",
- DISPATCH_QUEUE_SERIAL);
- vcc_vf_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
- vcc_vf_q);
- dispatch_source_set_timer(vcc_vf_timer,
- dispatch_time(DISPATCH_TIME_NOW, 0),
- 33333333ull, 2000000ull);
- dispatch_source_set_event_handler(vcc_vf_timer, ^{
- @autoreleasepool {
- vcc_vf_drive_once();
- vcc_drive_sinks_once();
- }
- });
- dispatch_resume(vcc_vf_timer);
- vcc_log(@" viewfinder drive timer armed (30 Hz, delivering '420v' "
- @"+ camera metadata)");
+ vcc_vf_drive_setup();
+ vcc_log(@" viewfinder drive ready (30 Hz while a stream or sink exists, "
+ @"delivering '420v' + camera metadata)");
}
diff --git a/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.m b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.m
index db6afe25..9fff6067 100644
--- a/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.m
+++ b/VPhoneGuestComponents/VCamCaptured/Hooks/VCamSinkObservation.m
@@ -1,3 +1,4 @@
+#include "VCamFrames.h"
#include "VCamHooks.h"
// MARK: - frame-sender endpoint observation
@@ -174,6 +175,7 @@ static void vcc_track_driven_sink(id sink) {
if (!vcc_driven_sinks) vcc_driven_sinks = [NSHashTable weakObjectsHashTable];
[vcc_driven_sinks addObject:sink];
pthread_mutex_unlock(&vcc_driven_sinks_lock);
+ vcc_frame_drive_wake();
}
NSArray *vcc_driven_sinks_snapshot(void) {