forked from OwnGoalStudio/TrollVNC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenCapturer.h
More file actions
109 lines (86 loc) · 3.83 KB
/
Copy pathScreenCapturer.h
File metadata and controls
109 lines (86 loc) · 3.83 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
/*
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/>.
*/
#ifndef ScreenCapturer_h
#define ScreenCapturer_h
#import <CoreMedia/CoreMedia.h>
#import <CoreVideo/CoreVideo.h>
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/**
ScreenCapturer
----------------
A singleton that captures the device screen into an IOSurface and produces
CMSampleBufferRef frames on a CADisplayLink-driven cadence. Intended for use
by encoders/streamers that require CVPixelBuffer-backed sample buffers.
Threading & lifetime:
- startCapture/endCapture must be called on the main thread (internally uses CADisplayLink on main run loop).
- The provided frame handler is invoked on the main thread.
- ARC only.
Performance & format:
- Uses IOSurface + CoreAnimation render server to copy screen contents.
- Zero-copy wrapping via CVPixelBufferCreateWithIOSurface.
- Pixel format is ARGB as defined by sharedRenderProperties.
Debug stats (DEBUG builds only):
- Average FPS is periodically logged over a configurable window.
- Instantaneous FPS is computed from CADisplayLink.duration and can be smoothed with EMA.
*/
@interface ScreenCapturer : NSObject
/** Returns the shared singleton instance. */
+ (instancetype)sharedCapturer;
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
/**
Returns the IOSurface property dictionary used to create screen-sized surfaces
compatible with the current device configuration (size/orientation/format).
Consumers can use this to allocate compatible IOSurfaces.
*/
@property(nonatomic, strong, readonly) NSDictionary *renderProperties;
/**
Start screen capture. The frame handler will be called on the main thread for
each captured frame with a CMSampleBufferRef referencing a CVPixelBuffer backed
by the current IOSurface.
If capture is already active, this replaces the frame handler for subsequent frames
without restarting the underlying CADisplayLink.
*/
- (void)startCaptureWithFrameHandler:(void (^)(CMSampleBufferRef sampleBuffer))frameHandler;
/**
Stop screen capture and release internal resources (CADisplayLink, IOSurface).
Safe to call multiple times.
*/
- (void)endCapture;
/**
Set preferred frame rate range for the CADisplayLink driving capture.
Pass 0 to any of the arguments to leave it unspecified (system default).
On iOS 15+, preferredFrameRateRange will be used; on iOS 14, preferredFramesPerSecond uses maxFps.
*/
- (void)setPreferredFrameRateWithMin:(NSInteger)minFps preferred:(NSInteger)preferredFps max:(NSInteger)maxFps;
/**
Configure the logging window used for average capture FPS reporting (DEBUG only).
Defaults to 5.0 seconds. Values <= 0 disable periodic FPS logging.
*/
- (void)setStatsLogWindowSeconds:(NSTimeInterval)seconds;
/**
Configure smoothing factor (alpha) for instantaneous FPS based on CADisplayLink.duration (DEBUG only).
Uses exponential moving average: ema = alpha * current + (1 - alpha) * ema.
Defaults to 0.2; valid range [0.0, 1.0]. Out-of-range values are clamped.
*/
- (void)setInstantFpsSmoothingFactor:(double)alpha;
/**
Force the next frame to be treated as dirty, causing it to be captured and sent
to the frame handler even if no screen changes are detected.
*/
- (void)forceNextFrameUpdate;
@end
NS_ASSUME_NONNULL_END
#endif /* ScreenCapturer_h */