Skip to content

Commit 8e832d1

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 0228e1d commit 8e832d1

17 files changed

+1233
-12
lines changed

modules/desktop_capture/mac/screen_capturer_mac.mm

+1-10
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,7 @@ DesktopRect GetExcludedWindowPixelBounds(CGWindowID window, float dip_to_pixel_s
297297
ScreenConfigurationChanged();
298298
}
299299

300-
// When screen is zoomed in/out, OSX only updates the part of Rects currently
301-
// displayed on screen, with relative location to current top-left on screen.
302-
// This will cause problems when we copy the dirty regions to the captured
303-
// image. So we invalidate the whole screen to copy all the screen contents.
304-
// With CGI method, the zooming will be ignored and the whole screen contents
305-
// will be captured as before.
306-
// With IOSurface method, the zoomed screen contents will be captured.
307-
if (UAZoomEnabled()) {
308-
helper_.InvalidateScreen(screen_pixel_bounds_.size());
309-
}
300+
helper_.InvalidateScreen(screen_pixel_bounds_.size());
310301

311302
DesktopRegion region;
312303
helper_.TakeInvalidRegion(&region);

sdk/BUILD.gn

+41
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,43 @@ if (is_ios || is_mac) {
700700
"../rtc_base/system:gcd_helpers",
701701
]
702702
}
703+
704+
rtc_library("desktopcapture_objc") {
705+
visibility = [ "*" ]
706+
sources = [
707+
"objc/components/capturer/RTCDesktopCapturer+Private.h",
708+
"objc/components/capturer/RTCDesktopCapturer.h",
709+
"objc/components/capturer/RTCDesktopCapturer.mm",
710+
"objc/components/capturer/RTCDesktopSource+Private.h",
711+
"objc/components/capturer/RTCDesktopSource.h",
712+
"objc/components/capturer/RTCDesktopSource.mm",
713+
"objc/components/capturer/RTCDesktopMediaList+Private.h",
714+
"objc/components/capturer/RTCDesktopMediaList.h",
715+
"objc/components/capturer/RTCDesktopMediaList.mm",
716+
"objc/native/src/objc_desktop_capture.h",
717+
"objc/native/src/objc_desktop_capture.mm",
718+
"objc/native/src/objc_desktop_media_list.h",
719+
"objc/native/src/objc_desktop_media_list.mm",
720+
]
721+
frameworks = [
722+
"AppKit.framework",
723+
]
724+
725+
configs += [ "..:common_objc" ]
726+
727+
public_configs = [ ":common_config_objc" ]
728+
729+
deps = [
730+
":base_objc",
731+
":helpers_objc",
732+
":videoframebuffer_objc",
733+
"../rtc_base/system:gcd_helpers",
734+
"../modules/desktop_capture",
735+
]
736+
if(is_mac) {
737+
deps += [ "//third_party:jpeg", ]
738+
}
739+
}
703740

704741
rtc_library("videocodec_objc") {
705742
visibility = [ "*" ]
@@ -1527,6 +1564,9 @@ if (is_ios || is_mac) {
15271564
"objc/base/RTCYUVPlanarBuffer.h",
15281565
"objc/components/capturer/RTCCameraVideoCapturer.h",
15291566
"objc/components/capturer/RTCFileVideoCapturer.h",
1567+
"objc/components/capturer/RTCDesktopCapturer.h",
1568+
"objc/components/capturer/RTCDesktopSource.h",
1569+
"objc/components/capturer/RTCDesktopMediaList.h",
15301570
"objc/components/renderer/metal/RTCMTLVideoView.h",
15311571
"objc/components/renderer/metal/RTCMTLNSVideoView.h",
15321572
"objc/components/renderer/opengl/RTCNSGLVideoView.h",
@@ -1561,6 +1601,7 @@ if (is_ios || is_mac) {
15611601
":opengl_ui_objc",
15621602
":peerconnectionfactory_base_objc",
15631603
":videocapture_objc",
1604+
":desktopcapture_objc",
15641605
":videocodec_objc",
15651606
":videotoolbox_objc",
15661607
]

sdk/objc/components/capturer/RTCCameraVideoCapturer.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,9 @@ - (void)updateDeviceCaptureFormat:(AVCaptureDeviceFormat *)format fps:(NSInteger
495495
@"updateDeviceCaptureFormat must be called on the capture queue.");
496496
@try {
497497
_currentDevice.activeFormat = format;
498-
_currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps);
498+
if(![NSStringFromClass([_currentDevice class]) isEqualToString:@"AVCaptureDALDevice"]) {
499+
_currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps);
500+
}
499501
} @catch (NSException *exception) {
500502
RTCLogError(@"Failed to set active format!\n User info:%@", exception.userInfo);
501503
return;
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
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
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
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)