| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #import "GLVideoViewMac.h" |
| 12 |
| 13 | #import "TGRTCCVPixelBuffer.h" |
| 14 |
| 15 | #import <GLKit/GLKit.h> |
| 16 |
| 17 | #import "RTCDefaultShader.h" |
| 18 | #import "RTCDisplayLinkTimer.h" |
| 19 | #import "RTCI420TextureCache.h" |
| 20 | #import "base/RTCLogging.h" |
| 21 | #import "base/RTCVideoFrame.h" |
| 22 | #import "base/RTCVideoFrameBuffer.h" |
| 23 | #import "components/video_frame_buffer/RTCCVPixelBuffer.h" |
| 24 | #include "sdk/objc/native/api/video_frame.h" |
| 25 | #import "rtc_base/time_utils.h" |
| 26 | #include "sdk/objc/native/src/objc_frame_buffer.h" |
| 27 |
| 28 | namespace { |
| 29 |
| 30 | static RTCVideoFrame *customToObjCVideoFrame(const webrtc::VideoFrame &frame, RTCVideoRotation &rotation) { |
| 31 | rotation = RTCVideoRotation(frame.rotation()); |
| 32 | RTCVideoFrame *videoFrame = |
| 33 | [[RTCVideoFrame alloc] initWithBuffer:webrtc::ToObjCVideoFrameBuffer(frame.video_frame_buffer()) |
| 34 | rotation:rotation |
| 35 | timeStampNs:frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec]; |
| 36 | videoFrame.timeStamp = frame.timestamp(); |
| 37 |
| 38 | return videoFrame; |
| 39 | } |
| 40 |
| 41 | class VideoRendererAdapterImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 42 | public: |
| 43 | VideoRendererAdapterImpl(void (^frameReceived)(CGSize, RTCVideoFrame *, RTCVideoRotation)) { |
| 44 | _frameReceived = [frameReceived copy]; |
| 45 | } |
| 46 |
| 47 | void OnFrame(const webrtc::VideoFrame& nativeVideoFrame) override { |
| 48 | RTCVideoRotation rotation = RTCVideoRotation_0; |
| 49 | RTCVideoFrame* videoFrame = customToObjCVideoFrame(nativeVideoFrame, rotation); |
| 50 |
| 51 | CGSize currentSize = (videoFrame.rotation % 180 == 0) ? CGSizeMake(videoFrame.width, videoFrame.height) : CGSizeMake(videoFrame.height, videoFrame.width); |
| 52 |
| 53 | if (_frameReceived) { |
| 54 | _frameReceived(currentSize, videoFrame, rotation); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | private: |
| 59 | void (^_frameReceived)(CGSize, RTCVideoFrame *, RTCVideoRotation); |
| 60 | }; |
| 61 |
| 62 | } |
| 63 |
| 64 |
| 65 |
| 66 | static CGSize scaleToFillSize(CGSize size, CGSize maxSize) { |
| 67 | if (size.width < 1.0f) { |
| 68 | size.width = 1.0f; |
| 69 | } |
| 70 | if (size.height < 1.0f) { |
| 71 | size.height = 1.0f; |
| 72 | } |
| 73 | if (size.width < maxSize.width) { |
| 74 | size.height = floor(maxSize.width * size.height / MAX(1.0f, size.width)); |
| 75 | size.width = maxSize.width; |
| 76 | } |
| 77 | if (size.height < maxSize.height) { |
| 78 | size.width = floor(maxSize.height * size.width / MAX(1.0f, size.height)); |
| 79 | size.height = maxSize.height; |
| 80 | } |
| 81 | return size; |
| 82 | } |
| 83 |
| 84 | static CGSize aspectFilled(CGSize from, CGSize to) { |
| 85 | CGFloat scale = MAX(from.width / MAX(1.0, to.width), from.height / MAX(1.0, to.height)); |
| 86 | return NSMakeSize(ceil(to.width * scale), ceil(to.height * scale)); |
| 87 | } |
| 88 | static CGSize aspectFitted(CGSize from, CGSize to) { |
| 89 | CGFloat scale = MAX(from.width / MAX(1.0, to.width), from.height / MAX(1.0, to.height)); |
| 90 | return NSMakeSize(ceil(to.width * scale), ceil(to.height * scale)); |
| 91 | } |
| 92 |
| 93 | /* |
| 94 |
| 95 | func aspectFilled(_ size: CGSize) -> CGSize { |
| 96 | let scale = max(size.width / max(1.0, self.width), size.height / max(1.0, self.height)) |
| 97 | return CGSize(width: ceil(self.width * scale), height: ceil(self.height * scale)) |
| 98 | } |
| 99 | func fittedToWidthOrSmaller(_ width: CGFloat) -> CGSize { |
| 100 | let scale = min(1.0, width / max(1.0, self.width)) |
| 101 | return CGSize(width: floor(self.width * scale), height: floor(self.height * scale)) |
| 102 | } |
| 103 |
| 104 | func aspectFitted(_ size: CGSize) -> CGSize { |
| 105 | let scale = min(size.width / max(1.0, self.width), size.height / max(1.0, self.height)) |
| 106 | return CGSize(width: ceil(self.width * scale), height: ceil(self.height * scale)) |
| 107 | } |
| 108 | */ |
| 109 |
| 110 |
| 111 | #if !TARGET_OS_IPHONE |
| 112 |
| 113 | @interface OpenGLVideoView : NSOpenGLView |
| 114 | @property(atomic, strong) RTCVideoFrame *videoFrame; |
| 115 | @property(atomic, strong) RTCI420TextureCache *i420TextureCache; |
| 116 |
| 117 | - (void)drawFrame; |
| 118 | - (instancetype)initWithFrame:(NSRect)frame |
| 119 | pixelFormat:(NSOpenGLPixelFormat *)format |
| 120 | shader:(id<RTCVideoViewShading>)shader; |
| 121 | @end |
| 122 |
| 123 | static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
| 124 | const CVTimeStamp *now, |
| 125 | const CVTimeStamp *outputTime, |
| 126 | CVOptionFlags flagsIn, |
| 127 | CVOptionFlags *flagsOut, |
| 128 | void *displayLinkContext) { |
| 129 | OpenGLVideoView *view = |
| 130 | (__bridge OpenGLVideoView *)displayLinkContext; |
| 131 | [view drawFrame]; |
| 132 | return kCVReturnSuccess; |
| 133 | } |
| 134 |
| 135 |
| 136 | @implementation OpenGLVideoView { |
| 137 | CVDisplayLinkRef _displayLink; |
| 138 | RTCVideoFrame * _lastDrawnFrame; |
| 139 | id<RTCVideoViewShading> _shader; |
| 140 |
| 141 | int64_t _lastDrawnFrameTimeStampNs; |
| 142 | void (^_onFirstFrameReceived)(float); |
| 143 | bool _firstFrameReceivedReported; |
| 144 | } |
| 145 |
| 146 | @synthesize videoFrame = _videoFrame; |
| 147 | @synthesize i420TextureCache = _i420TextureCache; |
| 148 |
| 149 | - (instancetype)initWithFrame:(NSRect)frame |
| 150 | pixelFormat:(NSOpenGLPixelFormat *)format |
| 151 | shader:(id<RTCVideoViewShading>)shader { |
| 152 | if (self = [super initWithFrame:frame pixelFormat:format]) { |
| 153 | self->_shader = shader; |
| 154 | } |
| 155 | return self; |
| 156 | } |
| 157 |
| 158 | - (void)reshape { |
| 159 | [super reshape]; |
| 160 | NSRect frame = [self frame]; |
| 161 | [self ensureGLContext]; |
| 162 | CGLLockContext([[self openGLContext] CGLContextObj]); |
| 163 | glViewport(0, 0, frame.size.width, frame.size.height); |
| 164 | CGLUnlockContext([[self openGLContext] CGLContextObj]); |
| 165 | } |
| 166 |
| 167 | - (void)lockFocus { |
| 168 | NSOpenGLContext *context = [self openGLContext]; |
| 169 | [super lockFocus]; |
| 170 | if ([context view] != self) { |
| 171 | [context setView:self]; |
| 172 | } |
| 173 | [context makeCurrentContext]; |
| 174 | } |
| 175 |
| 176 | - (void)prepareOpenGL { |
| 177 | [super prepareOpenGL]; |
| 178 | [self ensureGLContext]; |
| 179 | glDisable(GL_DITHER); |
| 180 | [self setupDisplayLink]; |
| 181 | } |
| 182 |
| 183 | - (void)clearGLContext { |
| 184 | [self ensureGLContext]; |
| 185 | self.i420TextureCache = nil; |
| 186 | [super clearGLContext]; |
| 187 | } |
| 188 |
| 189 | - (void)drawRect:(NSRect)rect { |
| 190 | [self drawFrame]; |
| 191 | } |
| 192 |
| 193 | - (void)drawFrame { |
| 194 | RTCVideoFrame *frame = self.videoFrame; |
| 195 | if (!frame || frame == _lastDrawnFrame) { |
| 196 | return; |
| 197 | } |
| 198 | // This method may be called from CVDisplayLink callback which isn't on the |
| 199 | // main thread so we have to lock the GL context before drawing. |
| 200 | NSOpenGLContext *context = [self openGLContext]; |
| 201 | CGLLockContext([context CGLContextObj]); |
| 202 |
| 203 | [self ensureGLContext]; |
| 204 | glClear(GL_COLOR_BUFFER_BIT); |
| 205 |
| 206 |
| 207 | // Rendering native CVPixelBuffer is not supported on OS X. |
| 208 | // TODO(magjed): Add support for NV12 texture cache on OS X. |
| 209 | frame = [frame newI420VideoFrame]; |
| 210 | if (!self.i420TextureCache) { |
| 211 | self.i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:context]; |
| 212 | } |
| 213 | RTCVideoRotation rotation = frame.rotation; |
| 214 |
| 215 | RTCI420TextureCache *i420TextureCache = self.i420TextureCache; |
| 216 | if (i420TextureCache) { |
| 217 | [i420TextureCache uploadFrameToTextures:frame]; |
| 218 | [_shader applyShadingForFrameWithWidth:frame.width |
| 219 | height:frame.height |
| 220 | rotation:rotation |
| 221 | yPlane:i420TextureCache.yTexture |
| 222 | uPlane:i420TextureCache.uTexture |
| 223 | vPlane:i420TextureCache.vTexture]; |
| 224 | [context flushBuffer]; |
| 225 | _lastDrawnFrame = frame; |
| 226 | } |
| 227 | CGLUnlockContext([context CGLContextObj]); |
| 228 |
| 229 | if (!_firstFrameReceivedReported && _onFirstFrameReceived) { |
| 230 | _firstFrameReceivedReported = true; |
| 231 | float aspectRatio = (float)frame.width / (float)frame.height; |
| 232 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 233 | self->_onFirstFrameReceived(aspectRatio); |
| 234 | }); |
| 235 | } |
| 236 |
| 237 | } |
| 238 |
| 239 |
| 240 | - (void)setupDisplayLink { |
| 241 | if (_displayLink) { |
| 242 | return; |
| 243 | } |
| 244 | // Synchronize buffer swaps with vertical refresh rate. |
| 245 | GLint swapInt = 1; |
| 246 | [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
| 247 |
| 248 | // Create display link. |
| 249 | CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); |
| 250 | CVDisplayLinkSetOutputCallback(_displayLink, |
| 251 | &OnDisplayLinkFired, |
| 252 | (__bridge void *)self); |
| 253 | // Set the display link for the current renderer. |
| 254 | CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; |
| 255 | CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; |
| 256 | CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext( |
| 257 | _displayLink, cglContext, cglPixelFormat); |
| 258 | CVDisplayLinkStart(_displayLink); |
| 259 | } |
| 260 |
| 261 | -(void)setFrameOrigin:(NSPoint)newOrigin { |
| 262 | [super setFrameOrigin:newOrigin]; |
| 263 | } |
| 264 |
| 265 | - (void)teardownDisplayLink { |
| 266 | if (!_displayLink) { |
| 267 | return; |
| 268 | } |
| 269 | CVDisplayLinkRelease(_displayLink); |
| 270 | _displayLink = NULL; |
| 271 | } |
| 272 |
| 273 | - (void)ensureGLContext { |
| 274 | NSOpenGLContext* context = [self openGLContext]; |
| 275 | NSAssert(context, @"context shouldn't be nil"); |
| 276 | if ([NSOpenGLContext currentContext] != context) { |
| 277 | [context makeCurrentContext]; |
| 278 | } |
| 279 | } |
| 280 |
| 281 | - (void)dealloc { |
| 282 | [self teardownDisplayLink]; |
| 283 | } |
| 284 |
| 285 | - (void)setOnFirstFrameReceived:(void (^ _Nullable)(float))onFirstFrameReceived { |
| 286 | _onFirstFrameReceived = [onFirstFrameReceived copy]; |
| 287 | _firstFrameReceivedReported = false; |
| 288 | } |
| 289 |
| 290 |
| 291 | @end |
| 292 |
| 293 |
| 294 |
| 295 |
| 296 | @interface GLVideoView () |
| 297 | @property(nonatomic, strong) OpenGLVideoView *glView; |
| 298 | @end |
| 299 |
| 300 | @implementation GLVideoView { |
| 301 |
| 302 | CGSize _currentSize; |
| 303 |
| 304 | std::shared_ptr<VideoRendererAdapterImpl> _sink; |
| 305 |
| 306 | void (^_onOrientationUpdated)(int); |
| 307 | void (^_onIsMirroredUpdated)(bool); |
| 308 |
| 309 | bool _didSetShouldBeMirrored; |
| 310 | bool _shouldBeMirrored; |
| 311 | bool _forceMirrored; |
| 312 |
| 313 | } |
| 314 |
| 315 | @synthesize delegate = _delegate; |
| 316 |
| 317 | -(instancetype)initWithFrame:(NSRect)frameRect { |
| 318 | NSOpenGLPixelFormatAttribute attributes[] = { |
| 319 | NSOpenGLPFADoubleBuffer, |
| 320 | NSOpenGLPFADepthSize, 24, |
| 321 | NSOpenGLPFAOpenGLProfile, |
| 322 | NSOpenGLProfileVersion3_2Core, |
| 323 | 0 |
| 324 | }; |
| 325 | NSOpenGLPixelFormat* pixelFormat = |
| 326 | [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; |
| 327 | return [self initWithFrame:frameRect pixelFormat: pixelFormat]; |
| 328 | } |
| 329 |
| 330 | - (instancetype)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)format { |
| 331 | return [self initWithFrame:frame pixelFormat:format shader:[[RTCDefaultShader alloc] init]]; |
| 332 | } |
| 333 |
| 334 | - (instancetype)initWithFrame:(NSRect)frame |
| 335 | pixelFormat:(NSOpenGLPixelFormat *)format |
| 336 | shader:(id<RTCVideoViewShading>)shader { |
| 337 | if (self = [super initWithFrame:frame]) { |
| 338 |
| 339 | _glView = [[OpenGLVideoView alloc] initWithFrame:frame pixelFormat:format shader:shader]; |
| 340 | _glView.wantsLayer = YES; |
| 341 | self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize; |
| 342 | _glView.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize; |
| 343 |
| 344 | [self addSubview:_glView]; |
| 345 |
| 346 | __weak GLVideoView *weakSelf = self; |
| 347 |
| 348 | self.wantsLayer = YES; |
| 349 |
| 350 | _sink.reset(new VideoRendererAdapterImpl(^(CGSize size, RTCVideoFrame *videoFrame, RTCVideoRotation rotation) { |
| 351 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 352 | __strong GLVideoView *strongSelf = weakSelf; |
| 353 | if (strongSelf == nil) { |
| 354 | return; |
| 355 | } |
| 356 | if (!CGSizeEqualToSize(size, strongSelf->_currentSize)) { |
| 357 | strongSelf->_currentSize = size; |
| 358 | [strongSelf setSize:size]; |
| 359 | } |
| 360 |
| 361 | int mappedValue = 0; |
| 362 | switch (rotation) { |
| 363 | case RTCVideoRotation_90: |
| 364 | mappedValue = 0; |
| 365 | break; |
| 366 | case RTCVideoRotation_180: |
| 367 | mappedValue = 1; |
| 368 | break; |
| 369 | case RTCVideoRotation_270: |
| 370 | mappedValue = 2; |
| 371 | break; |
| 372 | default: |
| 373 | mappedValue = 0; |
| 374 | break; |
| 375 | } |
| 376 | [strongSelf setInternalOrientation:mappedValue]; |
| 377 |
| 378 | [strongSelf renderFrame:videoFrame]; |
| 379 | }); |
| 380 | })); |
| 381 | } |
| 382 | return self; |
| 383 | } |
| 384 |
| 385 |
| 386 |
| 387 | - (CALayerContentsGravity)videoContentMode { |
| 388 | return self.glView.layer.contentsGravity; |
| 389 | } |
| 390 |
| 391 | - (void)setVideoContentMode:(CALayerContentsGravity)mode { |
| 392 | self.glView.layer.contentsGravity = mode; |
| 393 | [self setNeedsLayout:YES]; |
| 394 | } |
| 395 |
| 396 | -(void)layout { |
| 397 | [super layout]; |
| 398 |
| 399 | if (self.bounds.size.width > 0.0f && _currentSize.width > 0) { |
| 400 |
| 401 | NSSize size = _currentSize; |
| 402 | NSSize frameSize = self.frame.size; |
| 403 | if ( self.glView.layer.contentsGravity == kCAGravityResizeAspectFill) { |
| 404 | size = aspectFitted(frameSize, _currentSize); |
| 405 | } else { |
| 406 | size = aspectFilled(frameSize, _currentSize); |
| 407 | } |
| 408 | _glView.frame = CGRectMake(floor((self.bounds.size.width - size.width) / 2.0), floor((self.bounds.size.height - size.height) / 2.0), size.width, size.height); |
| 409 | } |
| 410 |
| 411 | if (_shouldBeMirrored || _forceMirrored) { |
| 412 | self.glView.layer.anchorPoint = NSMakePoint(1, 0); |
| 413 | self.glView.layer.affineTransform = CGAffineTransformMakeScale(-1, 1); |
| 414 | } else { |
| 415 | self.glView.layer.anchorPoint = NSMakePoint(0, 0); |
| 416 | self.glView.layer.affineTransform = CGAffineTransformIdentity; |
| 417 | } |
| 418 | } |
| 419 |
| 420 | - (void)setSize:(CGSize)size { |
| 421 | [self.delegate videoView:self didChangeVideoSize:size]; |
| 422 | [self setNeedsLayout:YES]; |
| 423 | } |
| 424 |
| 425 | - (void)renderFrame:(RTCVideoFrame *)videoFrame { |
| 426 | self.glView.videoFrame = videoFrame; |
| 427 |
| 428 | if ([videoFrame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) { |
| 429 | RTCCVPixelBuffer *buffer = (RTCCVPixelBuffer*)videoFrame.buffer; |
| 430 | if ([buffer isKindOfClass:[TGRTCCVPixelBuffer class]]) { |
| 431 | bool shouldBeMirrored = ((TGRTCCVPixelBuffer *)buffer).shouldBeMirrored; |
| 432 | if (shouldBeMirrored != _shouldBeMirrored) { |
| 433 | _shouldBeMirrored = shouldBeMirrored; |
| 434 | if (shouldBeMirrored || _forceMirrored) { |
| 435 | self.glView.layer.anchorPoint = NSMakePoint(1, 0); |
| 436 | self.glView.layer.affineTransform = CGAffineTransformMakeScale(-1, 1); |
| 437 | } else { |
| 438 | self.glView.layer.anchorPoint = NSMakePoint(0, 0); |
| 439 | self.glView.layer.affineTransform = CGAffineTransformIdentity; |
| 440 | } |
| 441 | } |
| 442 |
| 443 | if (shouldBeMirrored != _shouldBeMirrored) { |
| 444 | if (_didSetShouldBeMirrored) { |
| 445 | if (_onIsMirroredUpdated) { |
| 446 | _onIsMirroredUpdated(_shouldBeMirrored); |
| 447 | } |
| 448 | } else { |
| 449 | _didSetShouldBeMirrored = true; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 |
| 456 | #pragma mark - Private |
| 457 |
| 458 |
| 459 |
| 460 | - (std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>)getSink { |
| 461 | assert([NSThread isMainThread]); |
| 462 |
| 463 | return _sink; |
| 464 | } |
| 465 |
| 466 | - (void)setOnFirstFrameReceived:(void (^ _Nullable)(float))onFirstFrameReceived { |
| 467 | [self.glView setOnFirstFrameReceived:onFirstFrameReceived]; |
| 468 | } |
| 469 |
| 470 | - (void)setInternalOrientation:(int)internalOrientation { |
| 471 | _internalOrientation = internalOrientation; |
| 472 | if (_onOrientationUpdated) { |
| 473 | _onOrientationUpdated(internalOrientation); |
| 474 | } |
| 475 | } |
| 476 |
| 477 | - (void)internalSetOnOrientationUpdated:(void (^ _Nullable)(int))onOrientationUpdated { |
| 478 | _onOrientationUpdated = [onOrientationUpdated copy]; |
| 479 | } |
| 480 |
| 481 | - (void)internalSetOnIsMirroredUpdated:(void (^ _Nullable)(bool))onIsMirroredUpdated { |
| 482 | } |
| 483 |
| 484 | - (void)setIsForceMirrored:(BOOL)forceMirrored { |
| 485 | _forceMirrored = forceMirrored; |
| 486 | [self setNeedsLayout:YES]; |
| 487 | } |
| 488 |
| 489 | @end |
| 490 |
| 491 | #endif // !TARGET_OS_IPHONE |
| 492 |