Skip to content

Commit 841d78f

Browse files
committed
Desktop Capture for macOS.
[Mac] feat: Support screen capture for macOS. (#24) (#36) fix: Get thumbnails asynchronously. (#37) fix: Use CVPixelBuffer to build DesktopCapture Frame, fix the crash caused by non-CVPixelBuffer frame in RTCVideoEncoderH264 that cannot be cropped. (#63) Fix the crash when setting the fps of the virtual camera. (#62)
1 parent a13ea17 commit 841d78f

17 files changed

+1233
-12
lines changed

modules/desktop_capture/mac/screen_capturer_mac.mm

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,7 @@ DesktopRect GetExcludedWindowPixelBounds(CGWindowID window, float dip_to_pixel_s
216216
ScreenConfigurationChanged();
217217
}
218218

219-
// When screen is zoomed in/out, OSX only updates the part of Rects currently
220-
// displayed on screen, with relative location to current top-left on screen.
221-
// This will cause problems when we copy the dirty regions to the captured
222-
// image. So we invalidate the whole screen to copy all the screen contents.
223-
// With CGI method, the zooming will be ignored and the whole screen contents
224-
// will be captured as before.
225-
// With IOSurface method, the zoomed screen contents will be captured.
226-
if (UAZoomEnabled()) {
227-
helper_.InvalidateScreen(screen_pixel_bounds_.size());
228-
}
219+
helper_.InvalidateScreen(screen_pixel_bounds_.size());
229220

230221
DesktopRegion region;
231222
helper_.TakeInvalidRegion(&region);

sdk/BUILD.gn

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,43 @@ if (is_ios || is_mac) {
682682
"../rtc_base/system:gcd_helpers",
683683
]
684684
}
685+
686+
rtc_library("desktopcapture_objc") {
687+
visibility = [ "*" ]
688+
sources = [
689+
"objc/components/capturer/RTCDesktopCapturer+Private.h",
690+
"objc/components/capturer/RTCDesktopCapturer.h",
691+
"objc/components/capturer/RTCDesktopCapturer.mm",
692+
"objc/components/capturer/RTCDesktopSource+Private.h",
693+
"objc/components/capturer/RTCDesktopSource.h",
694+
"objc/components/capturer/RTCDesktopSource.mm",
695+
"objc/components/capturer/RTCDesktopMediaList+Private.h",
696+
"objc/components/capturer/RTCDesktopMediaList.h",
697+
"objc/components/capturer/RTCDesktopMediaList.mm",
698+
"objc/native/src/objc_desktop_capture.h",
699+
"objc/native/src/objc_desktop_capture.mm",
700+
"objc/native/src/objc_desktop_media_list.h",
701+
"objc/native/src/objc_desktop_media_list.mm",
702+
]
703+
frameworks = [
704+
"AppKit.framework",
705+
]
706+
707+
configs += [ "..:common_objc" ]
708+
709+
public_configs = [ ":common_config_objc" ]
710+
711+
deps = [
712+
":base_objc",
713+
":helpers_objc",
714+
":videoframebuffer_objc",
715+
"../rtc_base/system:gcd_helpers",
716+
"../modules/desktop_capture",
717+
]
718+
if(is_mac) {
719+
deps += [ "//third_party:jpeg", ]
720+
}
721+
}
685722

686723
rtc_library("videocodec_objc") {
687724
visibility = [ "*" ]
@@ -1534,6 +1571,9 @@ if (is_ios || is_mac) {
15341571
"objc/base/RTCYUVPlanarBuffer.h",
15351572
"objc/components/capturer/RTCCameraVideoCapturer.h",
15361573
"objc/components/capturer/RTCFileVideoCapturer.h",
1574+
"objc/components/capturer/RTCDesktopCapturer.h",
1575+
"objc/components/capturer/RTCDesktopSource.h",
1576+
"objc/components/capturer/RTCDesktopMediaList.h",
15371577
"objc/components/renderer/metal/RTCMTLVideoView.h",
15381578
"objc/components/renderer/metal/RTCMTLNSVideoView.h",
15391579
"objc/components/renderer/opengl/RTCVideoViewShading.h",
@@ -1566,6 +1606,7 @@ if (is_ios || is_mac) {
15661606
":native_video",
15671607
":peerconnectionfactory_base_objc",
15681608
":videocapture_objc",
1609+
":desktopcapture_objc",
15691610
":videocodec_objc",
15701611
":videotoolbox_objc",
15711612
":darwin_privacy_info",

sdk/objc/components/capturer/RTCCameraVideoCapturer.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ - (void)updateDeviceCaptureFormat:(AVCaptureDeviceFormat *)format fps:(NSInteger
506506
@"updateDeviceCaptureFormat must be called on the capture queue.");
507507
@try {
508508
_currentDevice.activeFormat = format;
509-
_currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps);
509+
if(![NSStringFromClass([_currentDevice class]) isEqualToString:@"AVCaptureDALDevice"]) {
510+
_currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps);
511+
}
510512
} @catch (NSException *exception) {
511513
RTCLogError(@"Failed to set active format!\n User info:%@", exception.userInfo);
512514
return;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "RTCDesktopCapturer.h"
18+
19+
#include "sdk/objc/native/src/objc_desktop_capture.h"
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
RTC_OBJC_EXPORT
24+
@protocol RTC_OBJC_TYPE
25+
(DesktopCapturerDelegate)<NSObject>
26+
-(void)didCaptureVideoFrame:(RTC_OBJC_TYPE(RTCVideoFrame) *) frame;
27+
-(void)didSourceCaptureStart;
28+
-(void)didSourceCapturePaused;
29+
-(void)didSourceCaptureStop;
30+
-(void)didSourceCaptureError;
31+
@end
32+
33+
@interface RTCDesktopCapturer ()
34+
35+
@property(nonatomic, readonly)std::shared_ptr<webrtc::ObjCDesktopCapturer> nativeCapturer;
36+
37+
- (void)didCaptureVideoFrame:(RTC_OBJC_TYPE(RTCVideoFrame) *)frame;
38+
39+
-(void)didSourceCaptureStart;
40+
41+
-(void)didSourceCapturePaused;
42+
43+
-(void)didSourceCaptureStop;
44+
45+
-(void)didSourceCaptureError;
46+
47+
@end
48+
49+
NS_ASSUME_NONNULL_END
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <AVFoundation/AVFoundation.h>
18+
#import <Foundation/Foundation.h>
19+
20+
#import "RTCMacros.h"
21+
#import "RTCVideoCapturer.h"
22+
#import "RTCDesktopSource.h"
23+
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
@class RTCDesktopCapturer;
27+
28+
RTC_OBJC_EXPORT
29+
@protocol RTC_OBJC_TYPE
30+
(RTCDesktopCapturerDelegate)<NSObject>
31+
-(void)didSourceCaptureStart:(RTCDesktopCapturer *) capturer;
32+
33+
-(void)didSourceCapturePaused:(RTCDesktopCapturer *) capturer;
34+
35+
-(void)didSourceCaptureStop:(RTCDesktopCapturer *) capturer;
36+
37+
-(void)didSourceCaptureError:(RTCDesktopCapturer *) capturer;
38+
@end
39+
40+
RTC_OBJC_EXPORT
41+
// Screen capture that implements RTCVideoCapturer. Delivers frames to a
42+
// RTCVideoCapturerDelegate (usually RTCVideoSource).
43+
@interface RTC_OBJC_TYPE (RTCDesktopCapturer) : RTC_OBJC_TYPE(RTCVideoCapturer)
44+
45+
@property(nonatomic, readonly) RTCDesktopSource *source;
46+
47+
- (instancetype)initWithSource:(RTCDesktopSource*)source delegate:(__weak id<RTC_OBJC_TYPE(RTCDesktopCapturerDelegate)>)delegate captureDelegate:(__weak id<RTC_OBJC_TYPE(RTCVideoCapturerDelegate)>)captureDelegate;
48+
49+
- (instancetype)initWithDefaultScreen:(__weak id<RTC_OBJC_TYPE(RTCDesktopCapturerDelegate)>)delegate captureDelegate:(__weak id<RTC_OBJC_TYPE(RTCVideoCapturerDelegate)>)captureDelegate;
50+
51+
- (void)startCapture;
52+
53+
- (void)startCaptureWithFPS:(NSInteger)fps;
54+
55+
- (void)stopCapture;
56+
57+
- (void)stopCaptureWithCompletionHandler:(nullable void (^)(void))completionHandler;
58+
59+
@end
60+
61+
NS_ASSUME_NONNULL_END
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "base/RTCLogging.h"
20+
#import "base/RTCVideoFrameBuffer.h"
21+
22+
#import "components/video_frame_buffer/RTCCVPixelBuffer.h"
23+
24+
#import "RTCDesktopCapturer.h"
25+
#import "RTCDesktopCapturer+Private.h"
26+
#import "RTCDesktopSource+Private.h"
27+
28+
@implementation RTC_OBJC_TYPE (RTCDesktopCapturer) {
29+
__weak id<RTC_OBJC_TYPE(RTCDesktopCapturerDelegate)> _delegate;
30+
}
31+
32+
@synthesize nativeCapturer = _nativeCapturer;
33+
@synthesize source = _source;
34+
35+
- (instancetype)initWithSource:(RTCDesktopSource*)source delegate:(__weak id<RTC_OBJC_TYPE(RTCDesktopCapturerDelegate)>)delegate captureDelegate:(__weak id<RTC_OBJC_TYPE(RTCVideoCapturerDelegate)>)captureDelegate {
36+
if (self = [super initWithDelegate:captureDelegate]) {
37+
webrtc::DesktopType captureType = webrtc::kScreen;
38+
if(source.sourceType == RTCDesktopSourceTypeWindow) {
39+
captureType = webrtc::kWindow;
40+
}
41+
_nativeCapturer = std::make_shared<webrtc::ObjCDesktopCapturer>(captureType, source.nativeMediaSource->id(), self);
42+
_source = source;
43+
_delegate = delegate;
44+
}
45+
return self;
46+
}
47+
48+
- (instancetype)initWithDefaultScreen:(__weak id<RTC_OBJC_TYPE(RTCDesktopCapturerDelegate)>)delegate captureDelegate:(__weak id<RTC_OBJC_TYPE(RTCVideoCapturerDelegate)>)captureDelegate {
49+
if (self = [super initWithDelegate:captureDelegate]) {
50+
_nativeCapturer = std::make_unique<webrtc::ObjCDesktopCapturer>(webrtc::kScreen, -1, self);
51+
_source = nil;
52+
_delegate = delegate;
53+
}
54+
return self;
55+
}
56+
57+
58+
-(void)dealloc {
59+
_nativeCapturer->Stop();
60+
_nativeCapturer = nullptr;
61+
}
62+
63+
- (void)startCapture {
64+
[self didSourceCaptureStart];
65+
_nativeCapturer->Start(30);
66+
}
67+
68+
- (void)startCaptureWithFPS:(NSInteger)fps {
69+
_nativeCapturer->Start(fps);
70+
}
71+
72+
- (void)didCaptureVideoFrame
73+
: (RTC_OBJC_TYPE(RTCVideoFrame) *)frame {
74+
[self.delegate capturer:self didCaptureVideoFrame:frame];
75+
}
76+
77+
- (void)stopCapture {
78+
_nativeCapturer->Stop();
79+
}
80+
81+
- (void)stopCaptureWithCompletionHandler:(nullable void (^)(void))completionHandler {
82+
[self stopCapture];
83+
if(completionHandler != nil) {
84+
completionHandler();
85+
}
86+
}
87+
88+
-(void)didSourceCaptureStart {
89+
[_delegate didSourceCaptureStart:self];
90+
}
91+
92+
-(void)didSourceCapturePaused {
93+
[_delegate didSourceCapturePaused:self];
94+
}
95+
96+
-(void)didSourceCaptureStop {
97+
[_delegate didSourceCaptureStop:self];
98+
}
99+
100+
-(void)didSourceCaptureError {
101+
[_delegate didSourceCaptureError:self];
102+
}
103+
104+
@end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "RTCDesktopMediaList.h"
18+
19+
namespace webrtc {
20+
class ObjCDesktopMediaList;
21+
class MediaSource;
22+
}
23+
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
@interface RTCDesktopMediaList ()
27+
28+
@property(nonatomic, readonly)std::shared_ptr<webrtc::ObjCDesktopMediaList> nativeMediaList;
29+
30+
-(void)mediaSourceAdded:(webrtc::MediaSource *) source;
31+
32+
-(void)mediaSourceRemoved:(webrtc::MediaSource *) source;
33+
34+
-(void)mediaSourceNameChanged:(webrtc::MediaSource *) source;
35+
36+
-(void)mediaSourceThumbnailChanged:(webrtc::MediaSource *) source;
37+
38+
@end
39+
40+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)