| 7 |
| // |
| 8 | #import <Cocoa/Cocoa.h> |
| 9 | #import "DesktopCaptureSourceViewMac.h" |
| 10 | #import "platform/darwin/VideoMetalViewMac.h" |
| 11 | #import "tgcalls/desktop_capturer/DesktopCaptureSource.h" |
| 12 | #import "tgcalls/desktop_capturer/DesktopCaptureSourceHelper.h" |
| 13 | #import "tgcalls/desktop_capturer/DesktopCaptureSourceManager.h" |
| 14 | #import "platform/darwin/VideoMetalViewMac.h" |
| 15 |
| 16 |
| 17 |
| 18 | @interface DesktopCaptureSourceViewMetal : VideoMetalView |
| 19 | @end |
| 20 |
| 21 |
| 22 | @implementation DesktopCaptureSourceViewMetal |
| 23 |
| 24 | -(id)initWithHelper:(tgcalls::DesktopCaptureSourceHelper)helper { |
| 25 | if (self = [super initWithFrame:CGRectZero]) { |
| 26 | std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink = [self getSink]; |
| 27 | helper.setOutput(sink); |
| 28 | [self setVideoContentMode:kCAGravityResizeAspectFill]; |
| 29 | } |
| 30 | return self; |
| 31 | } |
| 32 | @end |
| 33 |
| 34 |
| 35 | @implementation DesktopCaptureSourceDataMac |
| 36 | -(id)initWithSize:(CGSize)size fps:(double)fps captureMouse:(bool)captureMouse { |
| 37 | if (self = [super init]) { |
| 38 | self.aspectSize = size; |
| 39 | self.fps = fps; |
| 40 | self.captureMouse = captureMouse; |
| 41 | } |
| 42 | return self; |
| 43 | } |
| 44 |
| 45 | -(NSString *)cachedKey { |
| 46 | return [[NSString alloc] initWithFormat:@"%@:%f:%d", NSStringFromSize(self.aspectSize), self.fps, self.captureMouse]; |
| 47 | } |
| 48 | @end |
| 49 |
| 50 | @interface DesktopCaptureSourceMac () |
| 51 | { |
| 52 | absl::optional<tgcalls::DesktopCaptureSource> _source; |
| 53 | BOOL _isWindow; |
| 54 | } |
| 55 | -(id)initWithSource:(tgcalls::DesktopCaptureSource)source isWindow:(BOOL)isWindow; |
| 56 | -(tgcalls::DesktopCaptureSource)getSource; |
| 57 |
| 58 | @end |
| 59 |
| 60 | @implementation DesktopCaptureSourceMac |
| 61 |
| 62 | -(tgcalls::DesktopCaptureSource)getSource { |
| 63 | return _source.value(); |
| 64 | } |
| 65 |
| 66 | -(NSString *)title { |
| 67 | if (_isWindow) { |
| 68 | const tgcalls::DesktopCaptureSource source = _source.value(); |
| 69 | return [[NSString alloc] initWithCString:source.title().c_str() encoding:NSUTF8StringEncoding]; |
| 70 | } |
| 71 | else |
| 72 | return [[NSString alloc] initWithFormat:@"Screen"]; |
| 73 | } |
| 74 |
| 75 | -(long)uniqueId { |
| 76 | return _source.value().uniqueId(); |
| 77 | } |
| 78 | -(BOOL)isWindow { |
| 79 | return _isWindow; |
| 80 | } |
| 81 | -(NSString *)uniqueKey { |
| 82 | return [[NSString alloc] initWithFormat:@"%ld:%@", self.uniqueId, _isWindow ? @"Window" : @"Screen"]; |
| 83 | } |
| 84 |
| 85 | -(NSString *)deviceIdKey { |
| 86 | return [[NSString alloc] initWithFormat:@"desktop_capturer_%@_%ld", _isWindow ? @"window" : @"screen", self.uniqueId]; |
| 87 | } |
| 88 |
| 89 | -(BOOL)isEqual:(id)object { |
| 90 | return [[((DesktopCaptureSourceMac *)object) uniqueKey] isEqualToString:[self uniqueKey]]; |
| 91 | } |
| 92 | - (BOOL)isEqualTo:(id)object { |
| 93 | return [[((DesktopCaptureSourceMac *)object) uniqueKey] isEqualToString:[self uniqueKey]]; |
| 94 | } |
| 95 |
| 96 | -(id)initWithSource:(tgcalls::DesktopCaptureSource)source isWindow:(BOOL)isWindow { |
| 97 | if (self = [super init]) { |
| 98 | _source = source; |
| 99 | _isWindow = isWindow; |
| 100 | } |
| 101 | return self; |
| 102 | } |
| 103 |
| 104 | @end |
| 105 |
| 106 |
| 107 | @interface DesktopCaptureSourceScopeMac () |
| 108 | -(tgcalls::DesktopCaptureSourceData)getData; |
| 109 | -(tgcalls::DesktopCaptureSource)getSource; |
| 110 | @end |
| 111 |
| 112 | @implementation DesktopCaptureSourceScopeMac |
| 113 |
| 114 | -(id)initWithSource:(DesktopCaptureSourceMac *)source data:(DesktopCaptureSourceDataMac *)data { |
| 115 | if (self = [super init]) { |
| 116 | _data = data; |
| 117 | _source = source; |
| 118 | } |
| 119 | return self; |
| 120 | } |
| 121 |
| 122 | -(NSString *)cachedKey { |
| 123 | return [[NSString alloc] initWithFormat:@"%@:%@", _source.uniqueKey, _data.cachedKey]; |
| 124 | } |
| 125 |
| 126 | -(tgcalls::DesktopCaptureSourceData)getData { |
| 127 | tgcalls::DesktopCaptureSourceData data{ |
| 128 | /*.aspectSize = */{ (int)_data.aspectSize.width, (int)_data.aspectSize.height}, |
| 129 | /*.fps = */_data.fps, |
| 130 | /*.captureMouse = */_data.captureMouse, |
| 131 | }; |
| 132 | return data; |
| 133 | } |
| 134 | -(tgcalls::DesktopCaptureSource)getSource { |
| 135 | return [_source getSource]; |
| 136 | } |
| 137 |
| 138 | @end |
| 139 |
| 140 | @implementation DesktopCaptureSourceManagerMac |
| 141 | { |
| 142 | std::map<std::string, tgcalls::DesktopCaptureSourceHelper> _cached; |
| 143 | std::unique_ptr<tgcalls::DesktopCaptureSourceManager> _manager; |
| 144 | BOOL _isWindow; |
| 145 | } |
| 146 |
| 147 | -(instancetype)init_s { |
| 148 | if (self = [super init]) { |
| 149 | _manager = std::make_unique<tgcalls::DesktopCaptureSourceManager>(tgcalls::DesktopCaptureType::Screen); |
| 150 | _isWindow = NO; |
| 151 | } |
| 152 | return self; |
| 153 | } |
| 154 | -(instancetype)init_w { |
| 155 | if (self = [super init]) { |
| 156 | _manager = std::make_unique<tgcalls::DesktopCaptureSourceManager>(tgcalls::DesktopCaptureType::Window); |
| 157 | _isWindow = YES; |
| 158 | } |
| 159 | return self; |
| 160 | } |
| 161 |
| 162 | -(NSArray<DesktopCaptureSourceMac *> *)list { |
| 163 | std::vector<tgcalls::DesktopCaptureSource> sources = _manager->sources(); |
| 164 | NSMutableArray<DesktopCaptureSourceMac *> *macSources = [[NSMutableArray alloc] init]; |
| 165 | for (auto i = sources.begin(); i != sources.end(); ++i) { |
| 166 | [macSources addObject:[[DesktopCaptureSourceMac alloc] initWithSource:*i isWindow:_isWindow]]; |
| 167 | } |
| 168 | return macSources; |
| 169 | } |
| 170 |
| 171 | -(NSView *)createForScope:(DesktopCaptureSourceScopeMac*)scope { |
| 172 | auto i = _cached.find(std::string([scope.cachedKey UTF8String])); |
| 173 | if (i == _cached.end()) { |
| 174 | i = _cached.emplace( |
| 175 | std::string([scope.cachedKey UTF8String]), |
| 176 | tgcalls::DesktopCaptureSourceHelper([scope getSource], [scope getData])).first; |
| 177 | } |
| 178 |
| 179 | DesktopCaptureSourceViewMetal *view = [[DesktopCaptureSourceViewMetal alloc] initWithHelper:i->second]; |
| 180 | if (scope.data.captureMouse) { |
| 181 | [view setVideoContentMode:kCAGravityResizeAspect]; |
| 182 | } |
| 183 | return view; |
| 184 | } |
| 185 |
| 186 | -(void)start:(DesktopCaptureSourceScopeMac *)scope { |
| 187 | const auto i = _cached.find(std::string([scope.cachedKey UTF8String])); |
| 188 | if (i != _cached.end()) { |
| 189 | i->second.start(); |
| 190 | } |
| 191 | } |
| 192 |
| 193 | -(void)stop:(DesktopCaptureSourceScopeMac *)scope { |
| 194 | const auto i = _cached.find(std::string([scope.cachedKey UTF8String])); |
| 195 | if (i != _cached.end()) { |
| 196 | i->second.stop(); |
| 197 | } |
| 198 | } |
| 199 |
| 200 | -(void)dealloc { |
| 201 | for (auto &[key, helper] : _cached) { |
| 202 | helper.stop(); |
| 203 | } |
| 204 | _manager.reset(); |
| 205 | } |
| 206 |
| 207 | @end |
| 208 |