forked from OwnGoalStudio/TrollVNC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenCapturer.mm
More file actions
421 lines (349 loc) · 13.7 KB
/
Copy pathScreenCapturer.mm
File metadata and controls
421 lines (349 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
This file is part of TrollVNC
Copyright (c) 2025 82Flex <82flex@gmail.com> and contributors
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#if !__has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag.
#endif
#import <UIKit/UIDevice.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIScreen.h>
#import <mach/mach.h>
#import "IOKitSPI.h"
#import "IOSurfaceSPI.h"
#import "Logging.h"
#import "ScreenCapturer.h"
#import "UIScreen+Private.h"
#ifdef __cplusplus
extern "C" {
#endif
CFIndex CARenderServerGetDirtyFrameCount(void *);
void CARenderServerRenderDisplay(kern_return_t a, CFStringRef b, IOSurfaceRef surface, int x, int y);
#ifdef __cplusplus
}
#endif
@implementation ScreenCapturer {
NSDictionary *mRenderProperties;
IOSurfaceRef mScreenSurface;
CADisplayLink *mDisplayLink;
void (^mFrameHandler)(CMSampleBufferRef sampleBuffer);
NSInteger mMinFps;
NSInteger mPreferredFps;
NSInteger mMaxFps;
// Stats configuration (effective in DEBUG only)
NSTimeInterval mStatsWindowSeconds; // average FPS logging window
double mInstFpsAlpha; // EMA smoothing factor for instantaneous FPS
}
+ (instancetype)sharedCapturer {
static ScreenCapturer *_inst = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_inst = [[self alloc] init];
// Defaults for stats logging
#if DEBUG
[_inst setStatsLogWindowSeconds:5.0];
[_inst setInstantFpsSmoothingFactor:0.2];
#endif
});
return _inst;
}
- (instancetype)init {
self = [super init];
if (!self)
return nil;
int width, height;
CGSize screenSize = [[UIScreen mainScreen] _unjailedReferenceBoundsInPixels].size;
#if !TARGET_IPHONE_SIMULATOR
BOOL isPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
if (isPad) {
width = (int)round(screenSize.height);
height = (int)round(screenSize.width);
} else {
#endif
width = (int)round(screenSize.width);
height = (int)round(screenSize.height);
#if !TARGET_IPHONE_SIMULATOR
}
#endif
// Pixel format for Alpha, Red, Green and Blue
unsigned pixelFormat = 0x42475241; // 'ARGB'
// 1 or 2 bytes per component
int bytesPerComponent = sizeof(uint8_t);
// 8 bytes per pixel
int bytesPerElement = bytesPerComponent * 4;
// Bytes per row (must be aligned)
int bytesPerRow = (int)IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, bytesPerElement * width);
// Properties included:
// BytesPerElement, BytesPerRow, Width, Height, PixelFormat, AllocSize
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CFPropertyListRef colorSpacePropertyList = CGColorSpaceCopyPropertyList(colorSpace);
CGColorSpaceRelease(colorSpace);
mRenderProperties = @{
(__bridge NSString *)kIOSurfaceBytesPerElement : @(bytesPerElement),
(__bridge NSString *)kIOSurfaceBytesPerRow : @(bytesPerRow),
(__bridge NSString *)kIOSurfaceWidth : @(width),
(__bridge NSString *)kIOSurfaceHeight : @(height),
(__bridge NSString *)kIOSurfacePixelFormat : @(pixelFormat),
(__bridge NSString *)kIOSurfaceAllocSize : @(bytesPerRow * height),
(__bridge NSString *)kIOSurfaceColorSpace : CFBridgingRelease(colorSpacePropertyList),
};
#if DEBUG
TVLog(@"render properties %@", mRenderProperties);
#endif
mScreenSurface = IOSurfaceCreate((__bridge CFDictionaryRef)mRenderProperties);
mDisplayLink = nil;
mFrameHandler = NULL;
mMinFps = 0;
mPreferredFps = 0;
mMaxFps = 0;
mStatsWindowSeconds = 0.0;
mInstFpsAlpha = 0.0;
return self;
}
#pragma mark - Testing
+ (unsigned long)__getMemoryUsedInBytes {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
if (kerr == KERN_SUCCESS) {
return info.resident_size;
} else {
return 0;
}
}
// Human-readable memory usage description based on __getMemoryUsedInBytes
+ (NSString *)_getMemoryUsageDescription {
long long bytes = (long long)[self __getMemoryUsedInBytes];
return [NSByteCountFormatter stringFromByteCount:bytes countStyle:NSByteCountFormatterCountStyleBinary];
}
#pragma mark - Rendering
- (BOOL)renderDisplayToScreenSurface:(IOSurfaceRef)dstSurface {
#if TARGET_OS_SIMULATOR
CARenderServerRenderDisplay(0, CFSTR("LCD"), dstSurface, 0, 0);
return YES; // Assume always changed: dirty detection does not work for simulator
#else
CFRunLoopRef runLoop = CFRunLoopGetMain();
static IOSurfaceRef srcSurface;
static IOSurfaceAcceleratorRef accelerator;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@autoreleasepool {
srcSurface = IOSurfaceCreate((__bridge CFDictionaryRef)mRenderProperties);
IOSurfaceAcceleratorCreate(kCFAllocatorDefault, nil, &accelerator);
CFRunLoopSourceRef runLoopSource = IOSurfaceAcceleratorGetRunLoopSource(accelerator);
CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopDefaultMode);
}
});
static CFIndex previousDirtyFrameCount = 0;
CFIndex dirtyFrameCount = CARenderServerGetDirtyFrameCount(NULL);
if (dirtyFrameCount == previousDirtyFrameCount) {
return NO; // No change
}
// Fast ~20ms, sRGB, while the image is GOOD. Recommended.
CARenderServerRenderDisplay(0 /* Main Display */, CFSTR("LCD"), srcSurface, 0, 0);
IOSurfaceAcceleratorTransferSurface(accelerator, srcSurface, dstSurface, NULL, NULL, NULL, NULL);
previousDirtyFrameCount = dirtyFrameCount;
return YES;
#endif
}
- (BOOL)updateDisplay:(CADisplayLink *)displayLink {
#if DEBUG
__uint64_t beginAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#endif
BOOL surfaceChanged = [self renderDisplayToScreenSurface:mScreenSurface];
#if DEBUG
__uint64_t endAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
static double sLastLogAtMs = 0.0;
static __uint64_t sFpsWindowStartNs = 0; // FPS window start (ns)
static unsigned long long sFpsFrames = 0; // Accumulated frames in window
static double sInstFpsEma = 0.0; // Smoothed instantaneous FPS (EMA)
// Accumulate frame count
if (surfaceChanged) {
sFpsFrames++;
}
if (sFpsWindowStartNs == 0) {
sFpsWindowStartNs = endAt;
}
// Instantaneous FPS sourced from CADisplayLink.duration; fallback to inter-frame delta if needed
double instFps = 0.0;
CFTimeInterval duration = displayLink.duration;
if (duration > 0.0) {
instFps = 1.0 / duration;
}
double nowMs = (double)endAt / NSEC_PER_MSEC;
// EMA smoothing for instantaneous FPS
if (instFps > 0.0) {
double alpha = mInstFpsAlpha;
sInstFpsEma = (sInstFpsEma == 0.0) ? instFps : (alpha * instFps + (1.0 - alpha) * sInstFpsEma);
}
// Periodic logging based on configurable window
double windowMs = (mStatsWindowSeconds > 0.0) ? (mStatsWindowSeconds * 1000.0) : 0.0;
if (windowMs > 0.0 && (nowMs - sLastLogAtMs >= windowMs)) {
double used = (double)(endAt - beginAt) / NSEC_PER_MSEC;
double windowSec = (double)(endAt - sFpsWindowStartNs) / 1e9; // ns -> s
double fps = (windowSec > 0.0) ? (sFpsFrames / windowSec) : 0.0;
double instOut = (sInstFpsEma > 0.0) ? sInstFpsEma : instFps;
TVLog(@"elapsed %.2fms, real fps %.2f (frames=%llu, window=%.2fs), inst fps %.2f, memory used %@", used, fps,
sFpsFrames, windowSec, instOut, [ScreenCapturer _getMemoryUsageDescription]);
sLastLogAtMs = nowMs;
// Reset FPS window
sFpsWindowStartNs = endAt;
sFpsFrames = 0;
sInstFpsEma = 0.0;
}
#endif
return surfaceChanged;
}
#pragma mark - Public Methods
- (NSDictionary *)renderProperties {
return mRenderProperties;
}
- (void)startCaptureWithFrameHandler:(void (^)(CMSampleBufferRef _Nonnull))frameHandler {
// Store/replace handler
mFrameHandler = [frameHandler copy];
if (mDisplayLink) {
// Already running; nothing else to do
return;
}
// Create display link on main run loop
void (^startBlock)(void) = ^{
mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLink:)];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
if (@available(iOS 15.0, *)) {
CAFrameRateRange range;
range.minimum = (mMinFps > 0) ? mMinFps : 0.0;
range.maximum = (mMaxFps > 0) ? mMaxFps : 0.0;
range.preferred = (mPreferredFps > 0) ? mPreferredFps : 0.0;
mDisplayLink.preferredFrameRateRange = range;
} else {
#endif
// iOS 14 fallback: use preferredFramesPerSecond; choose max in the provided range
NSInteger setFps = (mMaxFps > 0) ? mMaxFps : mPreferredFps;
if ([mDisplayLink respondsToSelector:@selector(preferredFramesPerSecond)])
mDisplayLink.preferredFramesPerSecond = (int)setFps; // 0 uses native/system default
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
}
#endif
[mDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
};
if ([NSThread isMainThread]) {
startBlock();
} else {
dispatch_async(dispatch_get_main_queue(), startBlock);
}
}
- (void)endCapture {
void (^stopBlock)(void) = ^{
if (mDisplayLink) {
[mDisplayLink invalidate];
mDisplayLink = nil;
}
mFrameHandler = nil;
};
if ([NSThread isMainThread]) {
stopBlock();
} else {
dispatch_async(dispatch_get_main_queue(), stopBlock);
}
}
- (void)setPreferredFrameRateWithMin:(NSInteger)minFps preferred:(NSInteger)preferredFps max:(NSInteger)maxFps {
// Normalize: if preferred is 0, but max/min provided, pick a reasonable default
mMinFps = MAX(0, minFps);
mMaxFps = MAX(0, maxFps);
mPreferredFps = MAX(0, preferredFps);
if (mPreferredFps == 0) {
if (mMaxFps > 0)
mPreferredFps = mMaxFps;
else if (mMinFps > 0)
mPreferredFps = mMinFps;
else
mPreferredFps = 0;
}
// If display link is already running, update it on main thread
if (mDisplayLink) {
void (^applyBlock)(void) = ^{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
if (@available(iOS 15.0, *)) {
CAFrameRateRange range;
range.minimum = (mMinFps > 0) ? mMinFps : 0.0;
range.maximum = (mMaxFps > 0) ? mMaxFps : 0.0;
range.preferred = (mPreferredFps > 0) ? mPreferredFps : 0.0;
mDisplayLink.preferredFrameRateRange = range;
} else {
#endif
// iOS 14 path: only preferredFramesPerSecond is available, use max/preferred
NSInteger setFps = (mMaxFps > 0) ? mMaxFps : mPreferredFps;
mDisplayLink.preferredFramesPerSecond = (int)setFps; // 0 means system default
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
}
#endif
};
if ([NSThread isMainThread])
applyBlock();
else
dispatch_async(dispatch_get_main_queue(), applyBlock);
}
}
- (void)setStatsLogWindowSeconds:(NSTimeInterval)seconds {
mStatsWindowSeconds = seconds;
}
- (void)setInstantFpsSmoothingFactor:(double)alpha {
if (alpha < 0.0)
alpha = 0.0;
if (alpha > 1.0)
alpha = 1.0;
mInstFpsAlpha = alpha;
}
#pragma mark - Private Methods
- (void)onDisplayLink:(CADisplayLink *)link {
if (!mFrameHandler)
return;
// Update the screen contents into our IOSurface
BOOL displayChanged = [self updateDisplay:link];
if (!displayChanged) {
return; // No change, nothing to do
}
// Wrap IOSurface in a CVPixelBuffer (zero-copy)
CVPixelBufferRef pixelBuffer = NULL;
NSDictionary *attrs = @{(NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{}};
CVReturn cvret = CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, mScreenSurface,
(__bridge CFDictionaryRef)attrs, &pixelBuffer);
if (cvret != kCVReturnSuccess || !pixelBuffer) {
return;
}
// Create format description from the pixel buffer
CMVideoFormatDescriptionRef formatDesc = NULL;
OSStatus status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &formatDesc);
if (status != noErr || !formatDesc) {
CVPixelBufferRelease(pixelBuffer);
return;
}
// Build timing from CADisplayLink
int32_t timescale = 1000000000; // 1 ns
CMSampleTimingInfo timing;
timing.duration = CMTimeMakeWithSeconds(link.duration, timescale);
timing.presentationTimeStamp = CMTimeMakeWithSeconds(link.timestamp, timescale);
timing.decodeTimeStamp = kCMTimeInvalid;
CMSampleBufferRef sampleBuffer = NULL;
status = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, formatDesc, &timing,
&sampleBuffer);
if (status == noErr && sampleBuffer) {
mFrameHandler(sampleBuffer);
CFRelease(sampleBuffer);
}
if (formatDesc)
CFRelease(formatDesc);
if (pixelBuffer)
CVPixelBufferRelease(pixelBuffer);
}
@end