-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src,build: port CGDisplayStream capturing to ScreenCaptureKit
Closes #10
- Loading branch information
Showing
4 changed files
with
196 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#import <ScreenCaptureKit/ScreenCaptureKit.h> | ||
#import <CoreGraphics/CoreGraphics.h> | ||
#import <CoreMedia/CoreMedia.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface ScreenCapturer : NSObject <SCStreamDelegate, SCStreamOutput> | ||
|
||
- (instancetype)initWithDisplay:(CGDirectDisplayID)displayID | ||
frameHandler:(nonnull void (^)(CMSampleBufferRef sampleBuffer))frameHandler | ||
errorHandler:(nonnull void (^)(NSError *error))errorHandler; | ||
|
||
- (void)startCapture; | ||
- (void)stopCapture; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#import "ScreenCapturer.h" | ||
|
||
@interface ScreenCapturer () | ||
|
||
@property (nonatomic, assign) CGDirectDisplayID displayID; | ||
@property (nonatomic, strong) SCStream *stream; | ||
|
||
// handlers | ||
@property (nonatomic, copy, nonnull) void (^frameHandler)(CMSampleBufferRef sampleBuffer); | ||
@property (nonatomic, copy, nonnull) void (^errorHandler)(NSError *error); | ||
|
||
@end | ||
|
||
|
||
@implementation ScreenCapturer | ||
|
||
- (instancetype)initWithDisplay:(CGDirectDisplayID)displayID | ||
frameHandler:(void (^)(CMSampleBufferRef))frameHandler | ||
errorHandler:(void (^)(NSError *))errorHandler { | ||
if (self = [super init]) { | ||
_displayID = displayID; | ||
_frameHandler = [frameHandler copy]; | ||
_errorHandler = [errorHandler copy]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)startCapture { | ||
[SCShareableContent getShareableContentWithCompletionHandler:^(SCShareableContent *content, NSError *error) { | ||
if (error) { | ||
self.errorHandler(error); | ||
return; | ||
} | ||
|
||
SCDisplay *display = content.displays[[content.displays indexOfObjectPassingTest:^BOOL(SCDisplay *_Nonnull d, NSUInteger idx, BOOL *_Nonnull stop) { | ||
return d.displayID == self.displayID; | ||
}]]; | ||
|
||
if (!display) { | ||
NSError *noDisplayError = [NSError errorWithDomain:@"ScreenCapturerErrorDomain" | ||
code:1 | ||
userInfo:@{NSLocalizedDescriptionKey : @"Display not available for capture"}]; | ||
self.errorHandler(noDisplayError); | ||
return; | ||
} | ||
|
||
SCStreamConfiguration *config = [[SCStreamConfiguration alloc] init]; | ||
// can later be adjusted for server-side scaling | ||
config.width = display.width; | ||
config.height = display.height; | ||
// set max frame rate to 60 FPS | ||
config.minimumFrameInterval = CMTimeMake(1, 60); | ||
config.pixelFormat = kCVPixelFormatType_32BGRA; | ||
|
||
SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:(display) excludingWindows:(@[])]; | ||
self.stream = [[SCStream alloc] initWithFilter:filter configuration:config delegate:self]; | ||
|
||
NSError *addOutputError = nil; | ||
[self.stream addStreamOutput:self | ||
type:SCStreamOutputTypeScreen | ||
sampleHandlerQueue:dispatch_queue_create("libvncserver.examples.mac", NULL) | ||
error:&addOutputError]; | ||
if (addOutputError) { | ||
self.errorHandler(addOutputError); | ||
return; | ||
} | ||
|
||
[self.stream startCaptureWithCompletionHandler:^(NSError * _Nullable startError) { | ||
if (startError) { | ||
self.errorHandler(startError); | ||
} | ||
}]; | ||
}]; | ||
} | ||
|
||
- (void)stopCapture { | ||
[self.stream stopCaptureWithCompletionHandler:^(NSError * _Nullable stopError) { | ||
if (stopError) { | ||
self.errorHandler(stopError); | ||
} | ||
self.stream = nil; | ||
}]; | ||
} | ||
|
||
|
||
/* | ||
SCStreamDelegate methods | ||
*/ | ||
|
||
- (void) stream:(SCStream *) stream didStopWithError:(NSError *) error { | ||
self.errorHandler(error); | ||
} | ||
|
||
|
||
/* | ||
SCStreamOutput methods | ||
*/ | ||
|
||
- (void)stream:(SCStream *)stream didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(SCStreamOutputType)type { | ||
if (type == SCStreamOutputTypeScreen) { | ||
self.frameHandler(sampleBuffer); | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters