| 7 | #import "base/RTCVideoFrameBuffer.h" |
| 8 | #import "components/video_frame_buffer/RTCCVPixelBuffer.h" |
| 9 | #include "sdk/objc/native/api/video_frame.h" |
| 10 | #include "sdk/objc/native/src/objc_frame_buffer.h" |
| 11 |
| 12 | #import "api/video/video_sink_interface.h" |
| 13 | #import "api/media_stream_interface.h" |
| 14 | #import "rtc_base/time_utils.h" |
| 15 |
| 16 | #import "SQueueLocalObject.h" |
| 17 |
| 18 |
| 19 | #import "api/video/video_sink_interface.h" |
| 20 | #import "api/media_stream_interface.h" |
| 21 |
| 22 | #import "tgcalls/platform/darwin/macOS/TGRTCMTLI420Renderer.h" |
| 23 |
| 24 | #define MTKViewClass NSClassFromString(@"MTKView") |
| 25 | #define TGRTCMTLI420RendererClass NSClassFromString(@"TGRTCMTLI420Renderer") |
| 26 |
| 27 | SQueue *renderQueue = [[SQueue alloc] init]; |
| 28 |
| 29 | namespace { |
| 30 |
| 31 | static RTCVideoFrame *customToObjCVideoFrame(const webrtc::VideoFrame &frame, RTCVideoRotation &rotation) { |
| 32 | rotation = RTCVideoRotation(frame.rotation()); |
| 33 | RTCVideoFrame *videoFrame = |
| 34 | [[RTCVideoFrame alloc] initWithBuffer:webrtc::ToObjCVideoFrameBuffer(frame.video_frame_buffer()) |
| 35 | rotation:rotation |
| 36 | timeStampNs:frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec]; |
| 37 | videoFrame.timeStamp = frame.timestamp(); |
| 38 |
| 39 | return videoFrame; |
| 40 | } |
| 41 |
| 42 | class VideoRendererAdapterImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 43 | public: |
| 44 | VideoRendererAdapterImpl(void (^frameReceived)(CGSize, RTCVideoFrame *, RTCVideoRotation)) { |
| 45 | _frameReceived = [frameReceived copy]; |
| 46 | } |
| 47 |
| 48 | void OnFrame(const webrtc::VideoFrame& nativeVideoFrame) override { |
| 49 | RTCVideoRotation rotation = RTCVideoRotation_0; |
| 50 | RTCVideoFrame* videoFrame = customToObjCVideoFrame(nativeVideoFrame, rotation); |
| 51 |
| 52 | CGSize currentSize = (videoFrame.rotation % 180 == 0) ? CGSizeMake(videoFrame.width, videoFrame.height) : CGSizeMake(videoFrame.height, videoFrame.width); |
| 53 |
| 54 | if (_frameReceived) { |
| 55 | _frameReceived(currentSize, videoFrame, rotation); |
| 56 | } |
| 57 | } |
| 58 |
| 59 | private: |
| 60 | void (^_frameReceived)(CGSize, RTCVideoFrame *, RTCVideoRotation); |
| 61 | }; |
| 62 |
| 63 | } |
| 64 |
| 65 | @interface TGCAMetalLayer : CAMetalLayer |
| 66 |
| 67 | @end |
| 68 |
| 69 | @implementation TGCAMetalLayer |
| 70 |
| 71 |
| 72 | -(void)dealloc { |
| 73 | } |
| 74 | @end |
| 75 |
| 76 | @interface VideoMetalView () { |
| 77 | SQueueLocalObject *_rendererI420; |
| 78 |
| 79 | CAMetalLayer *_metalView; |
| 80 | NSView *_foregroundView; |
| 81 |
| 82 | CGSize _videoFrameSize; |
| 83 | RTCVideoRotation _rotation; |
| 84 | int64_t _lastFrameTimeNs; |
| 85 |
| 86 | CGSize _currentSize; |
| 87 | std::shared_ptr<VideoRendererAdapterImpl> _sink; |
| 88 |
| 89 | void (^_onFirstFrameReceived)(float); |
| 90 | bool _firstFrameReceivedReported; |
| 91 | void (^_onOrientationUpdated)(int, CGFloat); |
| 92 | void (^_onIsMirroredUpdated)(bool); |
| 93 |
| 94 | bool _didSetShouldBeMirrored; |
| 95 | bool _shouldBeMirrored; |
| 96 | bool _forceMirrored; |
| 97 |
| 98 | bool _isPaused; |
| 99 |
| 100 | NSMutableArray<RTCVideoFrame *> *_frames; |
| 101 | RTCVideoFrame *_videoFrame; |
| 102 | BOOL _drawing; |
| 103 |
| 104 | BOOL _deleteForegroundOnNextDrawing; |
| 105 | } |
| 106 |
| 107 | @end |
| 108 |
| 109 | @implementation VideoMetalView |
| 110 |
| 111 | + (bool)isSupported { |
| 112 | return [VideoMetalView isMetalAvailable]; |
| 113 | } |
| 114 |
| 115 | - (instancetype)initWithFrame:(CGRect)frameRect { |
| 116 | self = [super initWithFrame:frameRect]; |
| 117 | if (self) { |
| 118 | [self configure]; |
| 119 | _lastFrameTimeNs = INT32_MAX; |
| 120 | _currentSize = CGSizeZero; |
| 121 | _frames = [[NSMutableArray alloc] init]; |
| 122 | _drawing = false; |
| 123 | _isPaused = false; |
| 124 | _deleteForegroundOnNextDrawing = false; |
| 125 | __weak VideoMetalView *weakSelf = self; |
| 126 | _sink.reset(new VideoRendererAdapterImpl(^(CGSize size, RTCVideoFrame *videoFrame, RTCVideoRotation rotation) { |
| 127 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 128 | __strong VideoMetalView *strongSelf = weakSelf; |
| 129 | if (strongSelf == nil) { |
| 130 | return; |
| 131 | } |
| 132 | strongSelf->_rotation = videoFrame.rotation; |
| 133 | if (!CGSizeEqualToSize(size, strongSelf->_currentSize)) { |
| 134 | strongSelf->_currentSize = size; |
| 135 | [strongSelf setSize:size]; |
| 136 | } |
| 137 |
| 138 | int mappedValue = 0; |
| 139 | switch (rotation) { |
| 140 | case RTCVideoRotation_90: |
| 141 | mappedValue = 0; |
| 142 | break; |
| 143 | case RTCVideoRotation_180: |
| 144 | mappedValue = 1; |
| 145 | break; |
| 146 | case RTCVideoRotation_270: |
| 147 | mappedValue = 2; |
| 148 | break; |
| 149 | default: |
| 150 | mappedValue = 0; |
| 151 | break; |
| 152 | } |
| 153 | [strongSelf setInternalOrientation:mappedValue]; |
| 154 |
| 155 | [strongSelf renderFrame:videoFrame]; |
| 156 |
| 157 | if ([videoFrame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) { |
| 158 | RTCCVPixelBuffer *buffer = (RTCCVPixelBuffer*)videoFrame.buffer; |
| 159 |
| 160 | if ([buffer isKindOfClass:[TGRTCCVPixelBuffer class]]) { |
| 161 | bool shouldBeMirrored = ((TGRTCCVPixelBuffer *)buffer).shouldBeMirrored; |
| 162 | if (shouldBeMirrored != strongSelf->_shouldBeMirrored) { |
| 163 | strongSelf->_shouldBeMirrored = shouldBeMirrored; |
| 164 | if (strongSelf->_onIsMirroredUpdated) { |
| 165 | strongSelf->_onIsMirroredUpdated(strongSelf->_shouldBeMirrored); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 |
| 171 | if (!strongSelf->_firstFrameReceivedReported && strongSelf->_onFirstFrameReceived) { |
| 172 | strongSelf->_firstFrameReceivedReported = true; |
| 173 | strongSelf->_onFirstFrameReceived((float)videoFrame.width / (float)videoFrame.height); |
| 174 | } |
| 175 | }); |
| 176 | })); |
| 177 |
| 178 | } |
| 179 | return self; |
| 180 | } |
| 181 |
| 182 | - (BOOL)isEnabled { |
| 183 | return !_isPaused; |
| 184 | } |
| 185 |
| 186 | - (void)setEnabled:(BOOL)enabled { |
| 187 | _isPaused = enabled; |
| 188 | } |
| 189 | -(void)setIsPaused:(bool)paused { |
| 190 | _isPaused = paused; |
| 191 | [self updateDrawingSize:self.frame.size]; |
| 192 | } |
| 193 | -(void)renderToSize:(NSSize)size animated: (bool)animated { |
| 194 | [CATransaction begin]; |
| 195 | [CATransaction setDisableActions:YES]; |
| 196 | if (animated) { |
| 197 | if (!_foregroundView) { |
| 198 | _foregroundView = [[NSView alloc] initWithFrame:self.bounds]; |
| 199 | _foregroundView.wantsLayer = YES; |
| 200 | _foregroundView.autoresizingMask = 0; |
| 201 | _foregroundView.layer = [VideoMetalView createMetalView:self.bounds]; |
| 202 | _foregroundView.layer.contentsGravity = kCAGravityResizeAspect; |
| 203 | [self addSubview:_foregroundView]; |
| 204 | } |
| 205 |
| 206 | CAMetalLayer *layer = _metalView; |
| 207 | CAMetalLayer *foreground = (CAMetalLayer *)_foregroundView.layer; |
| 208 |
| 209 | [_rendererI420 with:^(TGRTCMTLI420Renderer * renderer) { |
| 210 | [renderer setDoubleRendering:layer foreground:foreground]; |
| 211 | }]; |
| 212 | _deleteForegroundOnNextDrawing = false; |
| 213 | } else { |
| 214 | _deleteForegroundOnNextDrawing = true; |
| 215 | CAMetalLayer *layer = _metalView; |
| 216 |
| 217 | [_rendererI420 with:^(TGRTCMTLI420Renderer * renderer) { |
| 218 | [renderer setSingleRendering:layer]; |
| 219 | }]; |
| 220 | } |
| 221 | [self updateDrawingSize:size]; |
| 222 | [CATransaction commit]; |
| 223 | } |
| 224 |
| 225 | - (CALayerContentsGravity)videoContentMode { |
| 226 | return _metalView.contentsGravity; |
| 227 | } |
| 228 |
| 229 | - (void)setVideoContentMode:(CALayerContentsGravity)mode { |
| 230 | // _metalView.contentsGravity = mode; |
| 231 | } |
| 232 |
| 233 | #pragma mark - Private |
| 234 |
| 235 | + (BOOL)isMetalAvailable { |
| 236 | return initMetal(); |
| 237 | } |
| 238 |
| 239 | + (CAMetalLayer *)createMetalView:(CGRect)frame { |
| 240 | CAMetalLayer *layer = [[TGCAMetalLayer alloc] init]; |
| 241 | [CATransaction begin]; |
| 242 | [CATransaction setDisableActions:true]; |
| 243 | layer.framebufferOnly = true; |
| 244 | layer.opaque = false; |
| 245 | // layer.cornerRadius = 4; |
| 246 | if (@available(macOS 10.13, *)) { |
| 247 | layer.displaySyncEnabled = NO; |
| 248 | } |
| 249 | // layer.presentsWithTransaction = YES; |
| 250 | layer.backgroundColor = [NSColor clearColor].CGColor; |
| 251 | layer.contentsGravity = kCAGravityResizeAspectFill; |
| 252 | layer.frame = frame; |
| 253 |
| 254 | [CATransaction commit]; |
| 255 | return layer; |
| 256 | } |
| 257 |
| 258 | + (TGRTCMTLI420Renderer *)createI420Renderer { |
| 259 | return [[TGRTCMTLI420RendererClass alloc] init]; |
| 260 | } |
| 261 |
| 262 |
| 263 | - (void)configure { |
| 264 | NSAssert([VideoMetalView isMetalAvailable], @"Metal not availiable on this device"); |
| 265 | self.wantsLayer = YES; |
| 266 | self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay; |
| 267 | _metalView = [VideoMetalView createMetalView:self.bounds]; |
| 268 |
| 269 |
| 270 | self.layer = _metalView; |
| 271 | _videoFrameSize = CGSizeZero; |
| 272 |
| 273 | CAMetalLayer *layer = _metalView; |
| 274 |
| 275 | _rendererI420 = [[SQueueLocalObject alloc] initWithQueue:renderQueue generate: ^{ |
| 276 | TGRTCMTLI420Renderer *renderer = [VideoMetalView createI420Renderer]; |
| 277 | [renderer setSingleRendering:layer]; |
| 278 | return renderer; |
| 279 | }]; |
| 280 | } |
| 281 |
| 282 |
| 283 | -(void)setFrameSize:(NSSize)newSize { |
| 284 | [super setFrameSize:newSize]; |
| 285 |
| 286 | [self updateDrawingSize: newSize]; |
| 287 |
| 288 | } |
| 289 | - (void)layout { |
| 290 | [super layout]; |
| 291 | } |
| 292 |
| 293 | -(void)updateDrawingSize:(NSSize)size { |
| 294 | if (_isPaused) { |
| 295 | return; |
| 296 | } |
| 297 | _metalView.frame = CGRectMake(0, 0, size.width, size.height); |
| 298 | _foregroundView.frame = self.bounds; |
| 299 | if (!CGSizeEqualToSize(_videoFrameSize, CGSizeZero)) { |
| 300 | _metalView.drawableSize = [self drawableSize:size]; |
| 301 | ((CAMetalLayer *)_foregroundView.layer).drawableSize = _videoFrameSize; |
| 302 | } else { |
| 303 | _metalView.drawableSize = size; |
| 304 | ((CAMetalLayer *)_foregroundView.layer).drawableSize = size; |
| 305 | } |
| 306 |
| 307 | if(!_isPaused) { |
| 308 | RTCVideoFrame *frame = [_frames lastObject]; |
| 309 | if (frame == nil) { |
| 310 | frame = _videoFrame; |
| 311 | } |
| 312 | if (frame) { |
| 313 | [self renderFrame:frame]; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 |
| 318 |
| 319 | -(void)dealloc { |
| 320 | int bp = 0; |
| 321 | bp += 1; |
| 322 | } |
| 323 |
| 324 | #pragma mark - |
| 325 |
| 326 | - (void)setRotationOverride:(NSValue *)rotationOverride { |
| 327 | _rotationOverride = rotationOverride; |
| 328 |
| 329 | [self setNeedsLayout:YES]; |
| 330 | } |
| 331 |
| 332 | - (RTCVideoRotation)rtcFrameRotation { |
| 333 | if (_rotationOverride) { |
| 334 | RTCVideoRotation rotation; |
| 335 | if (@available(macOS 10.13, *)) { |
| 336 | [_rotationOverride getValue:&rotation size:sizeof(rotation)]; |
| 337 | } else { |
| 338 | [_rotationOverride getValue:&rotation]; |
| 339 | } |
| 340 | return rotation; |
| 341 | } |
| 342 |
| 343 | return _rotation; |
| 344 | } |
| 345 |
| 346 | - (CGSize)drawableSize:(NSSize)forSize { |
| 347 |
| 348 | MTLFrameSize from; |
| 349 | MTLFrameSize to; |
| 350 |
| 351 | from.width = _videoFrameSize.width; |
| 352 | from.height = _videoFrameSize.height; |
| 353 |
| 354 | if (CGSizeEqualToSize(CGSizeZero, forSize)) { |
| 355 | to.width = _videoFrameSize.width; |
| 356 | to.height = _videoFrameSize.height; |
| 357 | } else { |
| 358 | to.width = forSize.width; |
| 359 | to.height = forSize.height; |
| 360 | } |
| 361 |
| 362 |
| 363 | MTLFrameSize size = MTLAspectFilled(to, from); |
| 364 |
| 365 | return CGSizeMake(size.width, size.height); |
| 366 | } |
| 367 |
| 368 | #pragma mark - RTCVideoRenderer |
| 369 |
| 370 | - (void)setSize:(CGSize)size { |
| 371 | assert([NSThread isMainThread]); |
| 372 |
| 373 | _videoFrameSize = size; |
| 374 | [self updateDrawingSize:self.frame.size]; |
| 375 |
| 376 | _internalAspect = _videoFrameSize.width / _videoFrameSize.height; |
| 377 | } |
| 378 |
| 379 | - (void)renderFrame:(nullable RTCVideoFrame *)frame { |
| 380 | assert([NSThread isMainThread]); |
| 381 |
| 382 | if (!self.isEnabled) { |
| 383 | return; |
| 384 | } |
| 385 |
| 386 | if (frame == nil) { |
| 387 | RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); |
| 388 | return; |
| 389 | } |
| 390 |
| 391 | RTCVideoFrame *videoFrame = frame; |
| 392 | // Skip rendering if we've already rendered this frame. |
| 393 | if (!videoFrame) { |
| 394 | return; |
| 395 | } |
| 396 |
| 397 | if (CGRectIsEmpty(self.bounds)) { |
| 398 | return; |
| 399 | } |
| 400 | if (CGRectIsEmpty(self.visibleRect)) { |
| 401 | return; |
| 402 | } |
| 403 | if (self.window == nil || self.superview == nil) { |
| 404 | return; |
| 405 | } |
| 406 | if ((self.window.occlusionState & NSWindowOcclusionStateVisible) == 0) { |
| 407 | return; |
| 408 | } |
| 409 |
| 410 |
| 411 | if (_frames.count >= 5) { |
| 412 | [_frames removeAllObjects]; |
| 413 | [_frames addObject:videoFrame]; |
| 414 | [self enqueue]; |
| 415 | return; |
| 416 | } |
| 417 |
| 418 | [_frames addObject:videoFrame]; |
| 419 |
| 420 | [self enqueue]; |
| 421 |
| 422 | } |
| 423 |
| 424 | -(void)enqueue { |
| 425 | if(_frames.count > 0 && !_drawing) { |
| 426 | RTCVideoFrame *videoFrame = [_frames firstObject]; |
| 427 |
| 428 | NSValue * rotationOverride = _rotationOverride; |
| 429 |
| 430 |
| 431 | int64_t timeStampNs = videoFrame.timeStampNs; |
| 432 |
| 433 | __weak VideoMetalView *weakSelf = self; |
| 434 | dispatch_block_t completion = ^{ |
| 435 | __strong VideoMetalView *strongSelf = weakSelf; |
| 436 | if (strongSelf && strongSelf->_frames.count > 0) { |
| 437 | [strongSelf->_frames removeObjectAtIndex:0]; |
| 438 | strongSelf->_drawing = false; |
| 439 | strongSelf->_lastFrameTimeNs = timeStampNs; |
| 440 | if (strongSelf->_deleteForegroundOnNextDrawing) { |
| 441 | [strongSelf->_foregroundView removeFromSuperview]; |
| 442 | strongSelf->_foregroundView = nil; |
| 443 | strongSelf->_deleteForegroundOnNextDrawing = false; |
| 444 | } |
| 445 | [strongSelf enqueue]; |
| 446 | } |
| 447 | }; |
| 448 |
| 449 | _videoFrame = videoFrame; |
| 450 |
| 451 | self->_drawing = true; |
| 452 |
| 453 | [_rendererI420 with:^(TGRTCMTLI420Renderer * object) { |
| 454 | object.rotationOverride = rotationOverride; |
| 455 | [object drawFrame:videoFrame]; |
| 456 | dispatch_async(dispatch_get_main_queue(), completion); |
| 457 | }]; |
| 458 |
| 459 | } |
| 460 | } |
| 461 |
| 462 | - (std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>)getSink { |
| 463 | assert([NSThread isMainThread]); |
| 464 |
| 465 | return _sink; |
| 466 | } |
| 467 |
| 468 | - (void)setOnFirstFrameReceived:(void (^ _Nullable)(float))onFirstFrameReceived { |
| 469 | _onFirstFrameReceived = [onFirstFrameReceived copy]; |
| 470 | _firstFrameReceivedReported = false; |
| 471 | } |
| 472 |
| 473 | - (void)setInternalOrientationAndSize:(int)internalOrientation size:(CGSize)size { |
| 474 | CGFloat aspect = 1.0f; |
| 475 | if (size.width > 1.0f && size.height > 1.0f) { |
| 476 | aspect = size.width / size.height; |
| 477 | } |
| 478 | if (_internalOrientation != internalOrientation || ABS(_internalAspect - aspect) > 0.001) { |
| 479 | RTCLogInfo(@"VideoMetalView@%lx orientation: %d, aspect: %f", (intptr_t)self, internalOrientation, (float)aspect); |
| 480 |
| 481 | _internalOrientation = internalOrientation; |
| 482 | _internalAspect = aspect; |
| 483 | if (_onOrientationUpdated) { |
| 484 | _onOrientationUpdated(internalOrientation, aspect); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 |
| 489 | - (void)internalSetOnOrientationUpdated:(void (^ _Nullable)(int, CGFloat))onOrientationUpdated { |
| 490 | _onOrientationUpdated = [onOrientationUpdated copy]; |
| 491 | } |
| 492 |
| 493 | - (void)internalSetOnIsMirroredUpdated:(void (^ _Nullable)(bool))onIsMirroredUpdated { |
| 494 | _onIsMirroredUpdated = [onIsMirroredUpdated copy]; |
| 495 | } |
| 496 |
| 497 | - (void)setForceMirrored:(BOOL)forceMirrored { |
| 498 | _forceMirrored = forceMirrored; |
| 499 | [self setNeedsLayout:YES]; |
| 500 | } |
| 501 |
| 502 |
| 503 | @end |
| 504 |