11#import < AppKit/AppKit.h>
22#import < CoreMedia/CoreMedia.h>
3+ #import < ScreenCaptureKit/ScreenCaptureKit.h>
4+ #import < objc/runtime.h>
35@interface NSObject (OBFCaptureTimingFixture)
46@property (nonatomic ) CMTime lastObservedSample;
57@property (nonatomic ) CMTime inputBarrier;
68@end
79#include < stdio.h>
810#include < stdatomic.h>
11+ // The native bridge distinguishes an unavailable surface from a stopped
12+ // stream. Only status 1 initializes the frame and bounds output parameters.
13+ static const int captureOK = 1 ;
14+ static const int captureNoSurface = 2 ;
915static atomic_bool stoppedWithIdentity;
1016static atomic_uint stopCount;
1117static uint32_t expectedWindow;
12- static uint64_t expectedGeneration = 1 ;
18+ static atomic_uint_fast64_t expectedGeneration = 1 ;
19+ static atomic_uint_fast64_t expectedRevision = UINT64_MAX ;
1320extern uint32_t obf_capture_validate_target (int32_t , uint32_t , char *, size_t );
1421extern void *obf_capture_start (int32_t , uint32_t , uint64_t , char *, size_t );
22+ extern void obf_capture_bind_revision (void *, uint64_t );
1523extern void obf_capture_mark_input (void *);
1624extern int obf_capture_bounds (void *, double *, char *, size_t );
1725extern int obf_capture_frame (void *, uint8_t **, size_t *, uint32_t *, uint32_t *, uint32_t *, double *, uint64_t *, char *, size_t );
@@ -21,9 +29,24 @@ @interface NSObject (OBFCaptureTimingFixture)
2129extern void obf_pointer_hide (void );
2230void obf_control_native_stopped (uint64_t generation, uint64_t revision, int32_t pid, uint32_t windowID, const char *reason) {
2331 atomic_fetch_add (&stopCount, 1 );
24- if (generation == expectedGeneration && pid == getpid () && windowID == expectedWindow) atomic_store (&stoppedWithIdentity, true );
32+ if (generation == expectedGeneration && revision == expectedRevision &&
33+ pid == getpid () && windowID == expectedWindow) atomic_store (&stoppedWithIdentity, true );
2534 fprintf (stderr, " Stopped: %s \n " , reason);
2635}
36+ // Force an error from the reconfiguration completion while the real stream
37+ // is still live. The control owner must be notified before explicit Stop.
38+ static void rejectResize (id stream, SEL selector, SCStreamConfiguration *config, void (^completion)(NSError *)) {
39+ completion ([NSError errorWithDomain: @" OpenBitFunCaptureFixture" code: 1
40+ userInfo: @{NSLocalizedDescriptionKey : @" Fixture resize rejected" }]);
41+ }
42+ static BOOL windowIDStillExists (uint32_t windowID) {
43+ CFArrayRef windows = CGWindowListCopyWindowInfo (kCGWindowListOptionAll , kCGNullWindowID );
44+ for (NSDictionary *window in CFBridgingRelease (windows)) {
45+ if ([window[(id )kCGWindowNumber ] unsignedIntValue ] == windowID &&
46+ [window[(id )kCGWindowOwnerPID ] intValue ] == getpid ()) return YES ;
47+ }
48+ return NO ;
49+ }
2750// Opt-in diagnostic for a real application: only reads captured frames and
2851// prints window identity/geometry. No pixels are persisted and no input is sent.
2952static void observeApp (pid_t pid) {
@@ -34,7 +57,7 @@ static void observeApp(pid_t pid) {
3457 int result=0 ; uint32_t bound=0 ;
3558 for (int i=0 ;i<20 ;i++){
3659 uint8_t *bytes=NULL ;size_t length=0 ;uint32_t w=0 ,ht=0 ,wid=0 ;double bounds[4 ]={0 };uint64_t sequence=0 ;
37- if (! obf_capture_frame (h,&bytes,&length,&w,&ht,&wid,bounds,&sequence,error,sizeof (error))){fprintf (stderr," FRAME_ERROR %s \n " ,error);result=1 ;break ;}
60+ if (obf_capture_frame (h,&bytes,&length,&w,&ht,&wid,bounds,&sequence,error,sizeof (error)) != captureOK ){fprintf (stderr," FRAME_ERROR %s \n " ,error);result=1 ;break ;}
3861 if (i==0 )bound=wid;
3962 if (wid!=bound || atomic_load (&stopCount)){result=1 ;break ;}
4063 obf_capture_free (bytes);
@@ -82,9 +105,10 @@ int main(int argc, const char **argv) { @autoreleasepool {
82105 return 0 ;
83106 }
84107 BOOL minimize = argc > 1 && strcmp (argv[1 ], " --minimized" ) == 0 ;
108+ BOOL rejectConfiguration = argc > 1 && strcmp (argv[1 ], " --resize-error" ) == 0 ;
85109 NSApplication *app = NSApplication .sharedApplication ;
86110 [app setActivationPolicy: NSApplicationActivationPolicyAccessory];
87- NSWindow *target = [[NSWindow alloc] initWithContentRect:NSMakeRect (100 ,100 ,240 ,160 ) styleMask:(minimize ? NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable : NSWindowStyleMaskBorderless) backing:NSBackingStoreBuffered defer:NO ];
111+ __block NSWindow *target = [[NSWindow alloc] initWithContentRect:NSMakeRect (100 ,100 ,240 ,160 ) styleMask:(minimize ? NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable : NSWindowStyleMaskBorderless) backing:NSBackingStoreBuffered defer:NO ];
88112 target.backgroundColor = NSColor .redColor;
89113 target.releasedWhenClosed = NO ;
90114 [target orderFront: nil ];
@@ -99,9 +123,29 @@ int main(int argc, const char **argv) { @autoreleasepool {
99123 void *handle = obf_capture_start (getpid (), targetID, 1 , error, sizeof (error));
100124 int result = 1 ;
101125 if (!handle) fprintf (stderr," FAIL start: %s \n " ,error);
102- else {
126+ else if (rejectConfiguration) {
127+ expectedRevision = 4 ;
128+ obf_capture_bind_revision (handle, expectedRevision);
129+ Method update = class_getInstanceMethod (SCStream.class , @selector (updateConfiguration:completionHandler: ));
130+ IMP original = method_setImplementation (update, (IMP )rejectResize);
131+ dispatch_sync (dispatch_get_main_queue (), ^{
132+ [target setFrame: NSMakeRect (100 ,100 ,280 ,190 ) display: YES ];
133+ });
134+ [NSThread sleepForTimeInterval: 0.15 ];
135+ uint8_t *bytes = NULL ; size_t length = 0 ;
136+ uint32_t width = 0 , height = 0 , windowID = 0 ; uint64_t sequence = 0 ; double bounds[4 ] = {0 };
137+ int status = obf_capture_frame (handle, &bytes, &length, &width, &height, &windowID, bounds, &sequence, error, sizeof (error));
138+ method_setImplementation (update, original);
139+ if (status == 0 && !bytes && atomic_load (&stoppedWithIdentity) &&
140+ strstr (error, " CAPTURE_STOPPED:" ) && strstr (error, " Fixture resize rejected" )) {
141+ puts (" PASS failed resize revokes the live capture before explicit Stop" );
142+ result = 0 ;
143+ } else fprintf (stderr, " FAIL resize failure did not revoke capture: status=%d error=%s \n " , status, error);
144+ obf_capture_free (bytes);
145+ obf_capture_stop (handle);
146+ } else {
103147 uint8_t *bytes = NULL ; size_t length=0 ; uint32_t width=0 ,height=0 ,windowID=0 ; uint64_t sequence=0 ; double bounds[4 ]={0 };
104- if (! obf_capture_frame (handle,&bytes,&length,&width,&height,&windowID,bounds,&sequence,error,sizeof (error))) fprintf (stderr," FAIL frame: %s \n " ,error);
148+ if (obf_capture_frame (handle,&bytes,&length,&width,&height,&windowID,bounds,&sequence,error,sizeof (error)) != captureOK ) fprintf (stderr," FAIL frame: %s \n " ,error);
105149 else {
106150 size_t center = ((height/2 )*width + width/2 )*4 ;
107151 printf (" Frame %u x%u target=%u returned=%u sequence=%llu center RGBA=%u ,%u ,%u ,%u bounds=%.0f ,%.0f ,%.0f ,%.0f \n " ,width,height,targetID,windowID,sequence,bytes[center],bytes[center+1 ],bytes[center+2 ],bytes[center+3 ],bounds[0 ],bounds[1 ],bounds[2 ],bounds[3 ]);
@@ -116,8 +160,8 @@ int main(int argc, const char **argv) { @autoreleasepool {
116160 uint8_t *fresh = NULL ; size_t freshLength = 0 ;
117161 uint32_t freshWidth = 0 , freshHeight = 0 , freshWindow = 0 ;
118162 uint64_t freshSequence = 0 ; double freshBounds[4 ] = {0 };
119- if (! obf_capture_frame (handle, &fresh, &freshLength, &freshWidth, &freshHeight,
120- &freshWindow, freshBounds, &freshSequence, error, sizeof (error)) ||
163+ if (obf_capture_frame (handle, &fresh, &freshLength, &freshWidth, &freshHeight,
164+ &freshWindow, freshBounds, &freshSequence, error, sizeof (error)) != captureOK ||
121165 CMTimeCompare ([(__bridge id )handle lastObservedSample ], barrier) < 0 ) {
122166 fprintf (stderr, " FAIL capture returned a pre-input sample: %s \n " , error); result = 1 ;
123167 } else puts (" PASS post-input observation waits for a newer complete or idle sample" );
@@ -132,8 +176,8 @@ int main(int argc, const char **argv) { @autoreleasepool {
132176 uint8_t *repeat = NULL ; size_t repeatLength = 0 ;
133177 uint32_t repeatWidth = 0 , repeatHeight = 0 , repeatWindow = 0 ;
134178 uint64_t repeatSequence = 0 ; double repeatBounds[4 ] = {0 };
135- if (! obf_capture_frame (handle, &repeat, &repeatLength, &repeatWidth, &repeatHeight,
136- &repeatWindow, repeatBounds, &repeatSequence, error, sizeof (error)) ||
179+ if (obf_capture_frame (handle, &repeat, &repeatLength, &repeatWidth, &repeatHeight,
180+ &repeatWindow, repeatBounds, &repeatSequence, error, sizeof (error)) != captureOK ||
137181 repeatWindow != targetID || atomic_load (&stoppedWithIdentity)) {
138182 fprintf (stderr, " FAIL persistent observation %d : %s \n " , observation, error); result = 1 ;
139183 }
@@ -171,21 +215,21 @@ int main(int argc, const char **argv) { @autoreleasepool {
171215 dispatch_sync (dispatch_get_main_queue (), ^{ [target setFrameOrigin: NSMakePoint (110 ,100 )]; });
172216 [NSThread sleepForTimeInterval: 0.15 ]; // WindowServer commits the AppKit geometry asynchronously.
173217 double inputBounds[4 ] = {0 };
174- if (! obf_capture_bounds (handle, inputBounds, error, sizeof (error)) || inputBounds[0 ] != 110 ) {
218+ if (obf_capture_bounds (handle, inputBounds, error, sizeof (error)) != captureOK || inputBounds[0 ] != 110 ) {
175219 fprintf (stderr," FAIL input-only geometry retained stale origin: %s \n " ,error); result=1 ;
176220 } else puts (" PASS input geometry updates after movement without copying or encoding a frame" );
177221 uint8_t *movedBytes = NULL ; size_t movedLength = 0 ;
178222 uint32_t movedWidth=0 ,movedHeight=0 ,movedWindow=0 ; uint64_t movedSequence=0 ; double movedBounds[4 ]={0 };
179223 int moved = obf_capture_frame (handle,&movedBytes,&movedLength,&movedWidth,&movedHeight,&movedWindow,movedBounds,&movedSequence,error,sizeof (error));
180- if (! moved || movedBounds[0 ] != 110 ) {
224+ if (moved != captureOK || movedBounds[0 ] != 110 ) {
181225 fprintf (stderr," FAIL moved geometry not refreshed: %s \n " ,error); result=1 ;
182226 } else puts (" PASS moved window returns the new coordinate basis" );
183227 if (movedBytes) obf_capture_free (movedBytes);
184228 dispatch_sync (dispatch_get_main_queue (), ^{ [target setFrame: NSMakeRect (110 ,100 ,280 ,190 ) display: YES ]; });
185229 [NSThread sleepForTimeInterval: 0.15 ];
186230 movedBytes = NULL ;
187231 int resized = obf_capture_frame (handle,&movedBytes,&movedLength,&movedWidth,&movedHeight,&movedWindow,movedBounds,&movedSequence,error,sizeof (error));
188- if (! resized || movedWidth != 280 || movedHeight != 190 || movedBounds[2 ] != 280 || movedBounds[3 ] != 190 ) {
232+ if (resized != captureOK || movedWidth != 280 || movedHeight != 190 || movedBounds[2 ] != 280 || movedBounds[3 ] != 190 ) {
189233 fprintf (stderr," FAIL resized stream did not produce matching pixels: %s \n " ,error); result=1 ;
190234 } else puts (" PASS resized window updates SCStream and waits for matching frame dimensions" );
191235 if (movedBytes) obf_capture_free (movedBytes);
@@ -196,9 +240,8 @@ int main(int argc, const char **argv) { @autoreleasepool {
196240 uint8_t *hiddenBytes = NULL ; size_t hiddenLength = 0 ;
197241 uint32_t hiddenWidth=0 ,hiddenHeight=0 ,hiddenWindow=0 ; uint64_t hiddenSequence=0 ; double hiddenBounds[4 ]={0 };
198242 int hidden = obf_capture_frame (handle,&hiddenBytes,&hiddenLength,&hiddenWidth,&hiddenHeight,&hiddenWindow,hiddenBounds,&hiddenSequence,error,sizeof (error));
199- if (hidden) {
200- size_t center = ((hiddenHeight/2 )*hiddenWidth+hiddenWidth/2 )*4 ;
201- fprintf (stderr," HIDDEN frame center after changing background to green: %u ,%u ,%u \n " ,hiddenBytes[center],hiddenBytes[center+1 ],hiddenBytes[center+2 ]);
243+ if (hidden == captureOK || hiddenBytes) {
244+ fprintf (stderr," FAIL hidden target returned a frame\n " );
202245 result = 1 ;
203246 obf_capture_free (hiddenBytes);
204247 } else if (!strstr (error," TARGET_" )) {
@@ -214,12 +257,17 @@ int main(int argc, const char **argv) { @autoreleasepool {
214257 if (!handle) { fprintf (stderr," FAIL restored target restart: %s \n " ,error); result=1 ; }
215258 }
216259 if (result == 0 ) {
217- dispatch_sync (dispatch_get_main_queue (), ^{ [target close ]; });
260+ // A retained NSWindow can keep its WindowServer ID after
261+ // close. Release it so this exercises loss of the window.
262+ dispatch_sync (dispatch_get_main_queue (), ^{ [target close ]; target = nil ; });
218263 [NSThread sleepForTimeInterval: 0.2 ];
219264 uint8_t *closedBytes = NULL ; size_t closedLength = 0 ;
220265 uint32_t closedWidth=0 ,closedHeight=0 ,closedWindow=0 ; uint64_t closedSequence=0 ; double closedBounds[4 ]={0 };
221266 int closed = obf_capture_frame (handle,&closedBytes,&closedLength,&closedWidth,&closedHeight,&closedWindow,closedBounds,&closedSequence,error,sizeof (error));
222- if (closed || !atomic_load (&stoppedWithIdentity)) {
267+ if (closed == captureNoSurface && !closedBytes &&
268+ strstr (error, " TARGET_SURFACE_UNAVAILABLE:" ) && windowIDStillExists (targetID)) {
269+ puts (" PASS AppKit retained the closed window ID and capture returned no pixels" );
270+ } else if (closed != 0 || closedBytes || !atomic_load (&stoppedWithIdentity)) {
223271 fprintf (stderr," FAIL closed target did not revoke capture with its identity: %s \n " ,error); result=1 ;
224272 } else puts (" PASS closed window revokes capture with its original generation and target" );
225273 if (closedBytes) obf_capture_free (closedBytes);
0 commit comments