|
| 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 |
0 commit comments