@factiii/runner 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,291 @@
1
+ // Native simulator input over the legacy Indigo HID path. Reads one JSON
2
+ // command per line on stdin. Unlike idb's input, this exposes a real move
3
+ // phase, so a drag tracks the pointer instead of being a canned swipe.
4
+ #import <Foundation/Foundation.h>
5
+ #import <AppKit/AppKit.h>
6
+ #import <dlfcn.h>
7
+ #import <mach/mach_time.h>
8
+
9
+ @interface SimServiceContext : NSObject
10
+ + (instancetype)sharedServiceContextForDeveloperDir:(NSString *)dir error:(NSError **)error;
11
+ - (id)defaultDeviceSetWithError:(NSError **)error;
12
+ @end
13
+
14
+ @interface NSObject (SimHID)
15
+ - (instancetype)initWithDevice:(id)device error:(NSError **)error;
16
+ - (BOOL)postDarwinNotification:(NSString *)name error:(NSError **)error;
17
+ - (BOOL)darwinNotificationSetState:(uint64_t)state
18
+ name:(NSString *)name
19
+ error:(NSError **)error;
20
+ - (void)sendWithMessage:(void *)message freeWhenDone:(BOOL)freeWhenDone
21
+ completionQueue:(dispatch_queue_t)queue completion:(void (^)(NSError *))completion;
22
+ @end
23
+
24
+ // SimulatorKit builds the Indigo wire messages; we only address and send them.
25
+ typedef void *(*MouseFn)(CGPoint *, CGPoint *, uint32_t, NSUInteger, CGSize, uint32_t);
26
+ typedef void *(*KeyFn)(uint32_t, uint32_t);
27
+ typedef void *(*ButtonFn)(uint32_t, uint32_t, uint32_t);
28
+ static MouseFn gMouse;
29
+ static KeyFn gKey;
30
+ static ButtonFn gButton;
31
+ static id gClient;
32
+ static id gDevice;
33
+
34
+ // The HID service the touchscreen lives behind, and a size of 1x1 because the
35
+ // builder divides the point by it and we already pass a 0..1 ratio.
36
+ static const uint32_t kTouchTarget = 0x32;
37
+ static const CGSize kUnit = { 1, 1 };
38
+ static const uint32_t kNoEdge = 0;
39
+
40
+ // Xcode is not always at /Applications/Xcode.app: betas, renamed installs and
41
+ // CI images all move it, and `xcode-select` is what decides which one is
42
+ // active. Resolving it at run time is the difference between working on this
43
+ // Mac and working on any Mac.
44
+ static NSString *developerDir(void) {
45
+ const char *override = getenv("DEVELOPER_DIR");
46
+ if (override && *override) return @(override);
47
+
48
+ NSTask *task = [NSTask new];
49
+ task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/xcode-select"];
50
+ task.arguments = @[ @"-p" ];
51
+ NSPipe *pipe = [NSPipe pipe];
52
+ task.standardOutput = pipe;
53
+ task.standardError = [NSFileHandle fileHandleWithNullDevice];
54
+ NSError *err = nil;
55
+ if ([task launchAndReturnError:&err]) {
56
+ NSData *out = [pipe.fileHandleForReading readDataToEndOfFile];
57
+ [task waitUntilExit];
58
+ NSString *path = [[NSString alloc] initWithData:out encoding:NSUTF8StringEncoding];
59
+ path = [path stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
60
+ if (path.length) return path;
61
+ }
62
+ return @"/Applications/Xcode.app/Contents/Developer";
63
+ }
64
+
65
+ /** CoreSimulator is installed outside Xcode.app, but a developer dir may carry
66
+ * its own copy; try both rather than assuming. */
67
+ static BOOL loadPrivateFrameworks(NSString *devDir, void **simulatorKit) {
68
+ NSArray<NSString *> *coreSim = @[
69
+ @"/Library/Developer/PrivateFrameworks/CoreSimulator.framework/CoreSimulator",
70
+ [devDir stringByAppendingString:@"/Library/PrivateFrameworks/CoreSimulator.framework/CoreSimulator"]
71
+ ];
72
+ BOOL loadedCore = NO;
73
+ for (NSString *path in coreSim) {
74
+ if (dlopen(path.UTF8String, RTLD_NOW)) { loadedCore = YES; break; }
75
+ }
76
+ NSString *kitPath = [devDir stringByAppendingString:
77
+ @"/Library/PrivateFrameworks/SimulatorKit.framework/SimulatorKit"];
78
+ void *kit = dlopen(kitPath.UTF8String, RTLD_NOW);
79
+ if (simulatorKit) *simulatorKit = kit;
80
+ if (!loadedCore || !kit) {
81
+ fprintf(stderr, "Could not load the simulator frameworks from %s\n", devDir.UTF8String);
82
+ return NO;
83
+ }
84
+ return YES;
85
+ }
86
+
87
+ static void sendMessage(void *message) {
88
+ if (!message) return;
89
+ [gClient sendWithMessage:message freeWhenDone:YES completionQueue:dispatch_get_main_queue()
90
+ completion:^(NSError *e) { if (e) fprintf(stderr, "send: %s\n", e.localizedDescription.UTF8String); }];
91
+ }
92
+
93
+ // Indigo wire offsets (Indigo.h, all structs packed). A message is a mach
94
+ // header, then innerSize/eventType, then payloads.
95
+ static const size_t kMachHeaderBytes = 0x18;
96
+ static const size_t kInnerSizeOffset = 0x18;
97
+ static const size_t kEventTypeOffset = 0x1c;
98
+ static const size_t kPayloadOffset = 0x20; // eventKind, +4 timestamp, +0x10 event
99
+ static const size_t kEventOffset = 0x30; // the IndigoTouch inside payload 1
100
+ static const size_t kTouchBytes = 0x74; // through the trailing field
101
+ static const size_t kPayloadSize = 0x90; // Swift's sizeof(IndigoPayload)
102
+ static const size_t kSecondPayload = 0xb0; // hand-built messages use the Swift stride
103
+ static const size_t kMessageBytes = 0x140; // sizeof(IndigoMessage) + sizeof(IndigoPayload)
104
+ static const uint8_t kEventTypeSingleTouch = 0x02;
105
+ static const uint32_t kEventKindTouch = 0x0b;
106
+
107
+ /**
108
+ * A single-touch Indigo message for one contact.
109
+ *
110
+ * SimulatorKit has no single-touch builder — `IndigoHIDMessageForMouseNSEvent`
111
+ * always emits a *multi*-touch message (eventType 0x03). Sending that raw looks
112
+ * like it works, because SpringBoard's lock screen accepts it, but an unlocked
113
+ * device's apps ignore it entirely. So take the valid IndigoTouch the builder
114
+ * produces and re-envelope it as single-touch (0x02) with a duplicated second
115
+ * contact, which is what Simulator.app itself sends.
116
+ */
117
+ static void touch(double x, double y, NSEventType type) {
118
+ CGPoint p = CGPointMake(x, y);
119
+ void *source = gMouse(&p, NULL, kTouchTarget, (NSUInteger)type, kUnit, kNoEdge);
120
+ if (!source) return;
121
+ uint8_t *src = (uint8_t *)source;
122
+
123
+ // The builder normalises by the NSSize it was handed; we pass a unit size and
124
+ // a ratio, so write the coordinates back explicitly.
125
+ memcpy(src + kEventOffset + 0x0c, &x, sizeof(double));
126
+ memcpy(src + kEventOffset + 0x14, &y, sizeof(double));
127
+
128
+ uint8_t *dst = (uint8_t *)calloc(1, kMessageBytes);
129
+ if (!dst) { free(source); return; }
130
+
131
+ // The mach header the builder produced, kept verbatim. A zeroed one is not
132
+ // rejected loudly — the message is simply never delivered, which is why
133
+ // buttons (sent raw, header intact) worked while touches disappeared.
134
+ memcpy(dst, src, kMachHeaderBytes);
135
+
136
+ const uint32_t innerSize = (uint32_t)kPayloadSize;
137
+ memcpy(dst + kInnerSizeOffset, &innerSize, sizeof(uint32_t));
138
+ dst[kEventTypeOffset] = kEventTypeSingleTouch;
139
+ memcpy(dst + kPayloadOffset, &kEventKindTouch, sizeof(uint32_t));
140
+ const uint64_t now = mach_absolute_time();
141
+ memcpy(dst + kPayloadOffset + 0x04, &now, sizeof(uint64_t));
142
+ memcpy(dst + kEventOffset, src + kEventOffset, kTouchBytes);
143
+ free(source);
144
+
145
+ // The second contact is a copy of the first, marked as the duplicate.
146
+ memcpy(dst + kSecondPayload, dst + kPayloadOffset, kPayloadSize);
147
+ const uint32_t one = 1, two = 2;
148
+ memcpy(dst + kSecondPayload + 0x10, &one, sizeof(uint32_t));
149
+ memcpy(dst + kSecondPayload + 0x14, &two, sizeof(uint32_t));
150
+
151
+ sendMessage(dst);
152
+ }
153
+
154
+ static void key(uint32_t code, BOOL down) { sendMessage(gKey(code, down ? 1 : 2)); }
155
+
156
+ /**
157
+ * Biometric auth, which is not HID at all — it is a Darwin notification, the
158
+ * same one Simulator.app's Features menu posts. Names taken from that binary:
159
+ * "pearl" is Face ID, "fingerTouch" is Touch ID.
160
+ *
161
+ * A match on an unenrolled device is ignored in silence, so enrolment is set
162
+ * first: a state flag, then the change notification BiometricKit watches for.
163
+ */
164
+ static void biometric(BOOL face, BOOL match) {
165
+ NSError *err = nil;
166
+ NSString *enrolledKey =
167
+ [NSString stringWithFormat:@"com.apple.BiometricKit_Sim.%@id_enrolled",
168
+ face ? @"pearl" : @"touch"];
169
+ [gDevice darwinNotificationSetState:1 name:enrolledKey error:&err];
170
+ [gDevice postDarwinNotification:@"com.apple.BiometricKit.enrollmentChanged"
171
+ error:&err];
172
+
173
+ NSString *name =
174
+ [NSString stringWithFormat:@"com.apple.BiometricKit_Sim.%@.%@",
175
+ face ? @"pearl" : @"fingerTouch",
176
+ match ? @"match" : @"nomatch"];
177
+ if (![gDevice postDarwinNotification:name error:&err]) {
178
+ fprintf(stderr, "biometric: %s\n", err.localizedDescription.UTF8String);
179
+ }
180
+ }
181
+
182
+ // ButtonEventSource values from Indigo.h. Not sequential, and not the same
183
+ // numbering any public API uses.
184
+ static uint32_t buttonSource(NSString *name) {
185
+ if ([name isEqual:@"HOME"]) return 0x0;
186
+ if ([name isEqual:@"LOCK"]) return 0x1;
187
+ if ([name isEqual:@"SIDE"]) return 0xbb8;
188
+ if ([name isEqual:@"SIRI"]) return 0x400002;
189
+ if ([name isEqual:@"APPLE_PAY"]) return 0x1f4;
190
+ return UINT32_MAX;
191
+ }
192
+
193
+ static void button(uint32_t source, BOOL down) {
194
+ sendMessage(gButton(source, down ? 1 : 2, kTouchTarget));
195
+ }
196
+
197
+ // USB HID usage codes. There is no "type a string" event at this level.
198
+ static BOOL keycodeFor(unichar c, uint32_t *code, BOOL *shift) {
199
+ *shift = NO;
200
+ if (c >= 'a' && c <= 'z') { *code = c - 'a' + 4; return YES; }
201
+ if (c >= 'A' && c <= 'Z') { *code = c - 'A' + 4; *shift = YES; return YES; }
202
+ if (c >= '1' && c <= '9') { *code = c - '1' + 30; return YES; }
203
+ if (c == '0') { *code = 39; return YES; }
204
+ static const unichar plain[] = { ' ', '\n', '\t', '-', '=', '[', ']', '\\', ';', '\'', '`', ',', '.', '/' };
205
+ static const uint32_t plainCode[] = { 44, 40, 43, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56 };
206
+ static const unichar shifted[] = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', ':', '"', '~', '<', '>', '?' };
207
+ static const uint32_t shiftedCode[] = { 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56 };
208
+ for (size_t i = 0; i < sizeof(plain) / sizeof(*plain); i++)
209
+ if (plain[i] == c) { *code = plainCode[i]; return YES; }
210
+ for (size_t i = 0; i < sizeof(shifted) / sizeof(*shifted); i++)
211
+ if (shifted[i] == c) { *code = shiftedCode[i]; *shift = YES; return YES; }
212
+ return NO;
213
+ }
214
+
215
+ int main(int argc, const char **argv) {
216
+ @autoreleasepool {
217
+ if (argc < 2) { fprintf(stderr, "usage: simhid <udid> (JSON commands on stdin)\n"); return 2; }
218
+ NSString *wanted = @(argv[1]);
219
+ NSString *devDir = developerDir();
220
+ void *kit = NULL;
221
+ if (!loadPrivateFrameworks(devDir, &kit)) return 1;
222
+
223
+ gMouse = (MouseFn)dlsym(kit, "IndigoHIDMessageForMouseNSEvent");
224
+ gKey = (KeyFn)dlsym(kit, "IndigoHIDMessageForKeyboardArbitrary");
225
+ gButton = (ButtonFn)dlsym(kit, "IndigoHIDMessageForButton");
226
+ if (!gMouse || !gKey || !gButton) { fprintf(stderr, "indigo builders missing\n"); return 1; }
227
+
228
+ NSError *err = nil;
229
+ id ctx = [NSClassFromString(@"SimServiceContext") sharedServiceContextForDeveloperDir:devDir error:&err];
230
+ id set = [ctx defaultDeviceSetWithError:&err];
231
+ id device = nil;
232
+ for (id d in [set performSelector:@selector(devices)])
233
+ if ([[[d performSelector:@selector(UDID)] description] caseInsensitiveCompare:wanted] == NSOrderedSame) { device = d; break; }
234
+ if (!device) { fprintf(stderr, "device not found\n"); return 1; }
235
+
236
+ gDevice = device;
237
+ gClient = [[NSClassFromString(@"SimulatorKit.SimDeviceLegacyHIDClient") alloc] initWithDevice:device error:&err];
238
+ if (!gClient) { fprintf(stderr, "hid client: %s\n", err.description.UTF8String); return 1; }
239
+ fprintf(stderr, "indigo hid ready\n");
240
+
241
+ NSFileHandle *in = [NSFileHandle fileHandleWithStandardInput];
242
+ NSMutableData *pending = [NSMutableData data];
243
+ while (YES) {
244
+ NSData *chunk = [in availableData];
245
+ if (!chunk.length) break;
246
+ [pending appendData:chunk];
247
+ for (;;) {
248
+ NSRange nl = [pending rangeOfData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]
249
+ options:0 range:NSMakeRange(0, pending.length)];
250
+ if (nl.location == NSNotFound) break;
251
+ NSData *line = [pending subdataWithRange:NSMakeRange(0, nl.location)];
252
+ [pending replaceBytesInRange:NSMakeRange(0, nl.location + 1) withBytes:NULL length:0];
253
+ NSDictionary *cmd = [NSJSONSerialization JSONObjectWithData:line options:0 error:NULL];
254
+ if (![cmd isKindOfClass:NSDictionary.class]) continue;
255
+ NSString *type = cmd[@"type"];
256
+
257
+ if ([type isEqual:@"touch"]) {
258
+ NSString *phase = cmd[@"phase"];
259
+ NSEventType e = [phase isEqual:@"down"] ? NSEventTypeLeftMouseDown
260
+ : [phase isEqual:@"up"] ? NSEventTypeLeftMouseUp
261
+ : NSEventTypeLeftMouseDragged;
262
+ touch([cmd[@"x"] doubleValue], [cmd[@"y"] doubleValue], e);
263
+ } else if ([type isEqual:@"tap"]) {
264
+ double x = [cmd[@"x"] doubleValue], y = [cmd[@"y"] doubleValue];
265
+ touch(x, y, NSEventTypeLeftMouseDown);
266
+ usleep(30000);
267
+ touch(x, y, NSEventTypeLeftMouseUp);
268
+ } else if ([type isEqual:@"text"]) {
269
+ NSString *text = cmd[@"text"];
270
+ for (NSUInteger i = 0; i < text.length; i++) {
271
+ uint32_t code; BOOL shift;
272
+ if (!keycodeFor([text characterAtIndex:i], &code, &shift)) continue;
273
+ if (shift) key(225, YES);
274
+ key(code, YES); usleep(6000); key(code, NO);
275
+ if (shift) key(225, NO);
276
+ usleep(6000);
277
+ }
278
+ } else if ([type isEqual:@"button"]) {
279
+ uint32_t source = buttonSource(cmd[@"button"]);
280
+ if (source == UINT32_MAX) { fprintf(stderr, "unknown button\n"); continue; }
281
+ button(source, YES); usleep(60000); button(source, NO);
282
+ } else if ([type isEqual:@"biometric"]) {
283
+ biometric(![cmd[@"kind"] isEqual:@"touch"], [cmd[@"match"] boolValue]);
284
+ } else if ([type isEqual:@"key"]) {
285
+ key((uint32_t)[cmd[@"code"] unsignedIntValue], [cmd[@"down"] boolValue]);
286
+ }
287
+ }
288
+ }
289
+ return 0;
290
+ }
291
+ }
@@ -0,0 +1,319 @@
1
+ // Native simulator capture + encode. Frames come from the display's own
2
+ // callbacks (no polling, no screenshots); each H.264 access unit is written to
3
+ // stdout as [4-byte BE length][Annex-B payload].
4
+ #import <Foundation/Foundation.h>
5
+ #import <IOSurface/IOSurface.h>
6
+ #import <VideoToolbox/VideoToolbox.h>
7
+ #import <dlfcn.h>
8
+ #import <stdatomic.h>
9
+ #import <os/lock.h>
10
+
11
+ @interface SimServiceContext : NSObject
12
+ + (instancetype)sharedServiceContextForDeveloperDir:(NSString *)dir error:(NSError **)error;
13
+ - (id)defaultDeviceSetWithError:(NSError **)error;
14
+ @end
15
+
16
+ @protocol SimDescriptorState <NSObject>
17
+ - (unsigned short)displayClass;
18
+ @end
19
+ @protocol SimPortDescriptor <NSObject>
20
+ - (id<SimDescriptorState>)state;
21
+ @end
22
+
23
+ @interface NSObject (SimPrivate)
24
+ - (id)io;
25
+ - (NSArray *)ioPorts;
26
+ - (id)descriptor;
27
+ - (id)framebufferSurface;
28
+ - (id)ioSurface;
29
+ - (void)registerScreenCallbacksWithUUID:(NSUUID *)uuid callbackQueue:(dispatch_queue_t)queue
30
+ frameCallback:(void (^)(void))frame surfacesChangedCallback:(void (^)(id, id))surfaces
31
+ propertiesChangedCallback:(void (^)(id))props;
32
+ - (void)registerCallbackWithUUID:(NSUUID *)uuid ioSurfacesChangeCallback:(void (^)(id))cb;
33
+ - (void)registerCallbackWithUUID:(NSUUID *)uuid damageRectanglesCallback:(void (^)(NSArray *))cb;
34
+ @end
35
+
36
+ // These port objects raise instead of answering respondsToSelector:, so every
37
+ // call has to be attempted rather than checked.
38
+ static NSError *guarded(void (^body)(void)) {
39
+ @try { body(); return nil; }
40
+ @catch (NSException *e) { return [NSError errorWithDomain:@"sim" code:1
41
+ userInfo:@{NSLocalizedDescriptionKey: e.reason ?: e.name}]; }
42
+ }
43
+
44
+ // Xcode is not always at /Applications/Xcode.app: betas, renamed installs and
45
+ // CI images all move it, and `xcode-select` is what decides which one is
46
+ // active. Resolving it at run time is the difference between working on this
47
+ // Mac and working on any Mac.
48
+ static NSString *developerDir(void) {
49
+ const char *override = getenv("DEVELOPER_DIR");
50
+ if (override && *override) return @(override);
51
+
52
+ NSTask *task = [NSTask new];
53
+ task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/xcode-select"];
54
+ task.arguments = @[ @"-p" ];
55
+ NSPipe *pipe = [NSPipe pipe];
56
+ task.standardOutput = pipe;
57
+ task.standardError = [NSFileHandle fileHandleWithNullDevice];
58
+ NSError *err = nil;
59
+ if ([task launchAndReturnError:&err]) {
60
+ NSData *out = [pipe.fileHandleForReading readDataToEndOfFile];
61
+ [task waitUntilExit];
62
+ NSString *path = [[NSString alloc] initWithData:out encoding:NSUTF8StringEncoding];
63
+ path = [path stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
64
+ if (path.length) return path;
65
+ }
66
+ return @"/Applications/Xcode.app/Contents/Developer";
67
+ }
68
+
69
+ /** CoreSimulator is installed outside Xcode.app, but a developer dir may carry
70
+ * its own copy; try both rather than assuming. */
71
+ static BOOL loadPrivateFrameworks(NSString *devDir, void **simulatorKit) {
72
+ NSArray<NSString *> *coreSim = @[
73
+ @"/Library/Developer/PrivateFrameworks/CoreSimulator.framework/CoreSimulator",
74
+ [devDir stringByAppendingString:@"/Library/PrivateFrameworks/CoreSimulator.framework/CoreSimulator"]
75
+ ];
76
+ BOOL loadedCore = NO;
77
+ for (NSString *path in coreSim) {
78
+ if (dlopen(path.UTF8String, RTLD_NOW)) { loadedCore = YES; break; }
79
+ }
80
+ NSString *kitPath = [devDir stringByAppendingString:
81
+ @"/Library/PrivateFrameworks/SimulatorKit.framework/SimulatorKit"];
82
+ void *kit = dlopen(kitPath.UTF8String, RTLD_NOW);
83
+ if (simulatorKit) *simulatorKit = kit;
84
+ if (!loadedCore || !kit) {
85
+ fprintf(stderr, "Could not load the simulator frameworks from %s\n", devDir.UTF8String);
86
+ return NO;
87
+ }
88
+ return YES;
89
+ }
90
+
91
+ static IOSurfaceRef gSurface = NULL;
92
+ static os_unfair_lock gLock = OS_UNFAIR_LOCK_INIT;
93
+ static int gFPS = 30, gWidth = 0;
94
+ static int gBitrate = 2500000;
95
+ static VTCompressionSessionRef gSession = NULL;
96
+ /** Source size the session was built for. A rotation changes the surface, and a
97
+ * session only accepts the dimensions it was created with — so the session has
98
+ * to be rebuilt rather than fed buffers it will reject. */
99
+ static size_t gSourceWidth = 0, gSourceHeight = 0;
100
+ static dispatch_queue_t gWrite;
101
+
102
+ static void emit(const void *bytes, size_t len) {
103
+ dispatch_sync(gWrite, ^{
104
+ uint8_t header[4] = { (uint8_t)(len >> 24), (uint8_t)(len >> 16),
105
+ (uint8_t)(len >> 8), (uint8_t)len };
106
+ fwrite(header, 1, 4, stdout);
107
+ fwrite(bytes, 1, len, stdout);
108
+ fflush(stdout);
109
+ });
110
+ }
111
+
112
+ // H.264 on the wire is Annex-B: each NAL prefixed with 00 00 00 01. VideoToolbox
113
+ // hands back AVCC (4-byte lengths), and parameter sets separately — they must be
114
+ // re-sent before every keyframe or a decoder joining late sees nothing.
115
+ static void appendAnnexB(NSMutableData *out, const uint8_t *nal, size_t len) {
116
+ const uint8_t start[4] = { 0, 0, 0, 1 };
117
+ [out appendBytes:start length:4];
118
+ [out appendBytes:nal length:len];
119
+ }
120
+
121
+ static void onEncoded(void *ctx, void *src, OSStatus status, VTEncodeInfoFlags flags,
122
+ CMSampleBufferRef sample) {
123
+ if (status != noErr) { fprintf(stderr, "encoder callback status %d\n", (int)status); return; }
124
+ if (!sample || !CMSampleBufferDataIsReady(sample)) { fprintf(stderr, "sample not ready\n"); return; }
125
+ NSMutableData *out = [NSMutableData data];
126
+
127
+ CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample, false);
128
+ BOOL keyframe = YES;
129
+ if (attachments && CFArrayGetCount(attachments)) {
130
+ CFDictionaryRef d = CFArrayGetValueAtIndex(attachments, 0);
131
+ keyframe = !CFDictionaryContainsKey(d, kCMSampleAttachmentKey_NotSync);
132
+ }
133
+ if (keyframe) {
134
+ CMFormatDescriptionRef fmt = CMSampleBufferGetFormatDescription(sample);
135
+ size_t count = 0;
136
+ CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, 0, NULL, NULL, &count, NULL);
137
+ for (size_t i = 0; i < count; i++) {
138
+ const uint8_t *ps = NULL; size_t psLen = 0;
139
+ if (CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fmt, i, &ps, &psLen, NULL, NULL) == noErr)
140
+ appendAnnexB(out, ps, psLen);
141
+ }
142
+ }
143
+
144
+ CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample);
145
+ size_t total = 0; char *data = NULL;
146
+ if (CMBlockBufferGetDataPointer(block, 0, NULL, &total, &data) != noErr) return;
147
+ size_t offset = 0;
148
+ while (offset + 4 <= total) {
149
+ uint32_t len = 0;
150
+ memcpy(&len, data + offset, 4);
151
+ len = CFSwapInt32BigToHost(len);
152
+ offset += 4;
153
+ if (len == 0 || offset + len > total) break;
154
+ appendAnnexB(out, (const uint8_t *)(data + offset), len);
155
+ offset += len;
156
+ }
157
+ emit(out.bytes, out.length);
158
+ }
159
+
160
+ static void encodeH264(IOSurfaceRef surface) {
161
+ CVPixelBufferRef pixels = NULL;
162
+ CVReturn cvr = CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, surface, NULL, &pixels);
163
+ if (cvr != kCVReturnSuccess) { fprintf(stderr, "CVPixelBufferCreateWithIOSurface failed: %d\n", cvr); return; }
164
+ size_t sourceWidth = IOSurfaceGetWidth(surface);
165
+ size_t sourceHeight = IOSurfaceGetHeight(surface);
166
+ if (gSession && (sourceWidth != gSourceWidth || sourceHeight != gSourceHeight)) {
167
+ fprintf(stderr, "source %zux%zu -> %zux%zu, rebuilding encoder\n",
168
+ gSourceWidth, gSourceHeight, sourceWidth, sourceHeight);
169
+ VTCompressionSessionCompleteFrames(gSession, kCMTimeInvalid);
170
+ VTCompressionSessionInvalidate(gSession);
171
+ CFRelease(gSession);
172
+ gSession = NULL;
173
+ }
174
+ if (!gSession) {
175
+ size_t w = sourceWidth, h = sourceHeight;
176
+ if (gWidth > 0 && w > (size_t)gWidth) {
177
+ h = h * gWidth / w;
178
+ w = gWidth;
179
+ }
180
+ // H.264 chroma is subsampled, so both axes must be even.
181
+ w -= w & 1;
182
+ h -= h & 1;
183
+ NSDictionary *props = @{ (id)kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder: @YES };
184
+ OSStatus cs;
185
+ if ((cs = VTCompressionSessionCreate(NULL, (int32_t)w, (int32_t)h, kCMVideoCodecType_H264,
186
+ (__bridge CFDictionaryRef)props, NULL, NULL, onEncoded, NULL, &gSession)) != noErr) {
187
+ fprintf(stderr, "VTCompressionSessionCreate failed: %d\n", (int)cs);
188
+ CVPixelBufferRelease(pixels); return;
189
+ }
190
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
191
+ // B-frames buy compression and cost a frame of latency. Wrong trade for a
192
+ // remote control.
193
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanFalse);
194
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_AutoLevel);
195
+ // One a second. A viewer joining mid-stream, or one that has lost a
196
+ // packet, decodes nothing until the next keyframe arrives — and the far end
197
+ // has no way to ask us for one, so the interval is the only recovery.
198
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameInterval,
199
+ (__bridge CFTypeRef)@(gFPS));
200
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration,
201
+ (__bridge CFTypeRef)@1.0);
202
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_ExpectedFrameRate, (__bridge CFTypeRef)@(gFPS));
203
+ // Without a ceiling the encoder spends whatever a full-screen scroll asks
204
+ // for — measured at 9.4 Mbps. A cap plus a one-second burst limit keeps a
205
+ // busy screen inside something a phone connection can carry.
206
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(gBitrate));
207
+ VTSessionSetProperty(gSession, kVTCompressionPropertyKey_DataRateLimits,
208
+ (__bridge CFArrayRef)@[@(gBitrate / 8 * 1.5), @1.0]);
209
+ gSourceWidth = sourceWidth;
210
+ gSourceHeight = sourceHeight;
211
+ fprintf(stderr, "h264 session %zux%zu\n", w, h);
212
+ }
213
+ static int64_t n = 0;
214
+ CMTime pts = CMTimeMake(n++, gFPS);
215
+ OSStatus es = VTCompressionSessionEncodeFrame(gSession, pixels, pts, kCMTimeInvalid, NULL, NULL, NULL);
216
+ if (es != noErr) fprintf(stderr, "encode failed: %d\n", (int)es);
217
+ CVPixelBufferRelease(pixels);
218
+ }
219
+
220
+ int main(int argc, const char **argv) {
221
+ @autoreleasepool {
222
+ if (argc < 2) { fprintf(stderr, "usage: simstream <udid> [--fps N] [--width N] [--bitrate N]\n"); return 2; }
223
+ NSString *wanted = @(argv[1]);
224
+ for (int i = 2; i < argc; i++) {
225
+ if (!strcmp(argv[i], "--fps") && i + 1 < argc) gFPS = atoi(argv[++i]);
226
+ else if (!strcmp(argv[i], "--width") && i + 1 < argc) gWidth = atoi(argv[++i]);
227
+ else if (!strcmp(argv[i], "--bitrate") && i + 1 < argc) gBitrate = atoi(argv[++i]);
228
+ }
229
+ setvbuf(stderr, NULL, _IONBF, 0);
230
+ gWrite = dispatch_queue_create("sim.write", DISPATCH_QUEUE_SERIAL);
231
+
232
+ NSString *devDir = developerDir();
233
+ if (!loadPrivateFrameworks(devDir, NULL)) return 1;
234
+
235
+ NSError *err = nil;
236
+ id ctx = [NSClassFromString(@"SimServiceContext") sharedServiceContextForDeveloperDir:devDir error:&err];
237
+ id set = [ctx defaultDeviceSetWithError:&err];
238
+ id device = nil;
239
+ for (id d in [set performSelector:@selector(devices)])
240
+ if ([[[d performSelector:@selector(UDID)] description] caseInsensitiveCompare:wanted] == NSOrderedSame) { device = d; break; }
241
+ if (!device) { fprintf(stderr, "device not found\n"); return 1; }
242
+ id ioClient = [device io];
243
+ if (!ioClient) { fprintf(stderr, "no io client\n"); return 1; }
244
+
245
+ id display = nil, fallback = nil;
246
+ for (id port in [ioClient ioPorts]) {
247
+ __block id descriptor = nil;
248
+ guarded(^{ descriptor = [port descriptor]; });
249
+ if (!descriptor) continue;
250
+ __block id<SimDescriptorState> st = nil;
251
+ id<SimPortDescriptor> typed = descriptor;
252
+ if (guarded(^{ st = [typed state]; }) || !st) continue;
253
+ __block int cls = -1;
254
+ if (guarded(^{ cls = [st displayClass]; })) continue;
255
+ if (cls == 0) { display = descriptor; break; }
256
+ if (!fallback) fallback = descriptor;
257
+ }
258
+ if (!display) display = fallback;
259
+ if (!display) { fprintf(stderr, "no display\n"); return 1; }
260
+
261
+ void (^onSurface)(id) = ^(id s) {
262
+ os_unfair_lock_lock(&gLock);
263
+ if (gSurface) { CFRelease(gSurface); gSurface = NULL; }
264
+ if (s) gSurface = (IOSurfaceRef)CFRetain((__bridge CFTypeRef)s);
265
+ os_unfair_lock_unlock(&gLock);
266
+ };
267
+
268
+ NSUUID *token = [NSUUID UUID];
269
+ dispatch_queue_t cbq = dispatch_queue_create("sim.cb", DISPATCH_QUEUE_SERIAL);
270
+ NSError *newStyle = guarded(^{
271
+ [display registerScreenCallbacksWithUUID:token callbackQueue:cbq
272
+ frameCallback:^{}
273
+ surfacesChangedCallback:^(id fb, id masked) { onSurface(fb); }
274
+ propertiesChangedCallback:^(id p) {}];
275
+ });
276
+ if (newStyle) {
277
+ guarded(^{ [display registerCallbackWithUUID:token ioSurfacesChangeCallback:^(id s) { onSurface(s); }]; });
278
+ guarded(^{ [display registerCallbackWithUUID:token damageRectanglesCallback:^(NSArray *r) {}]; });
279
+ fprintf(stderr, "old-style callbacks\n");
280
+ } else {
281
+ fprintf(stderr, "new-style SimScreen callbacks\n");
282
+ }
283
+ if (!gSurface) {
284
+ __block id s = nil;
285
+ guarded(^{ s = [display framebufferSurface]; });
286
+ if (!s) guarded(^{ s = [display ioSurface]; });
287
+ if (s) onSurface(s);
288
+ }
289
+
290
+ // Encode on a timer rather than per callback: the display fires ~60 times a
291
+ // second and the surface is a live buffer, so sampling it on our own cadence
292
+ // costs nothing and never queues work we would have to drop later.
293
+ dispatch_source_t tick = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
294
+ dispatch_queue_create("sim.encode", DISPATCH_QUEUE_SERIAL));
295
+ dispatch_source_set_timer(tick, dispatch_time(DISPATCH_TIME_NOW, 0),
296
+ (uint64_t)(NSEC_PER_SEC / gFPS), NSEC_PER_SEC / 100);
297
+ dispatch_source_set_event_handler(tick, ^{
298
+ os_unfair_lock_lock(&gLock);
299
+ IOSurfaceRef s = gSurface ? (IOSurfaceRef)CFRetain(gSurface) : NULL;
300
+ os_unfair_lock_unlock(&gLock);
301
+ // A blanked or sleeping display vends no surface, and the symptom is
302
+ // otherwise a black panel with no error anywhere. Logged on the change
303
+ // only — a heartbeat here is thousands of lines an hour.
304
+ static int had = -1;
305
+ const int has = s ? 1 : 0;
306
+ if (has != had) {
307
+ had = has;
308
+ fprintf(stderr, "display surface %s\n", has ? "available" : "gone");
309
+ }
310
+ if (!s) return;
311
+ encodeH264(s);
312
+ CFRelease(s);
313
+ });
314
+ dispatch_resume(tick);
315
+ fprintf(stderr, "streaming h264 at %d fps\n", gFPS);
316
+ [[NSRunLoop currentRunLoop] run];
317
+ return 0;
318
+ }
319
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@factiii/runner",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Factiii Runner, run Board AI agents on a machine you control. Pairs with the Factiii web/mobile clients over WebRTC.",
5
5
  "license": "ISC",
6
6
  "keywords": [
@@ -23,6 +23,7 @@
23
23
  },
24
24
  "files": [
25
25
  "dist/cli.js",
26
+ "native/",
26
27
  "plugin/",
27
28
  "index/index.js",
28
29
  "index/image-package.json",