Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 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 | |
magjed | bf08452 | 2017-01-13 07:10:16 -0800 | [diff] [blame] | 11 | #import <Foundation/Foundation.h> |
| 12 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 13 | #if !TARGET_OS_IPHONE |
| 14 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 15 | #import "RTCNSGLVideoView.h" |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 16 | |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 17 | #import <AppKit/NSOpenGL.h> |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 18 | #import <CoreVideo/CVDisplayLink.h> |
| 19 | #import <OpenGL/gl3.h> |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 20 | |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 21 | #import "RTCDefaultShader.h" |
| 22 | #import "RTCI420TextureCache.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 23 | #import "base/RTCLogging.h" |
| 24 | #import "base/RTCVideoFrame.h" |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 25 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 26 | @interface RTC_OBJC_TYPE (RTCNSGLVideoView) |
| 27 | () |
| 28 | // |videoFrame| is set when we receive a frame from a worker thread and is read |
| 29 | // from the display link callback so atomicity is required. |
| 30 | @property(atomic, strong) RTC_OBJC_TYPE(RTCVideoFrame) * |
| 31 | videoFrame; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 32 | @property(atomic, strong) RTCI420TextureCache *i420TextureCache; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 33 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 34 | - (void)drawFrame; |
| 35 | @end |
| 36 | |
| 37 | static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
| 38 | const CVTimeStamp *now, |
| 39 | const CVTimeStamp *outputTime, |
| 40 | CVOptionFlags flagsIn, |
| 41 | CVOptionFlags *flagsOut, |
| 42 | void *displayLinkContext) { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 43 | RTC_OBJC_TYPE(RTCNSGLVideoView) *view = |
| 44 | (__bridge RTC_OBJC_TYPE(RTCNSGLVideoView) *)displayLinkContext; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 45 | [view drawFrame]; |
| 46 | return kCVReturnSuccess; |
| 47 | } |
| 48 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 49 | @implementation RTC_OBJC_TYPE (RTCNSGLVideoView) { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 50 | CVDisplayLinkRef _displayLink; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 51 | RTC_OBJC_TYPE(RTCVideoFrame) * _lastDrawnFrame; |
| 52 | id<RTC_OBJC_TYPE(RTCVideoViewShading)> _shader; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @synthesize delegate = _delegate; |
| 56 | @synthesize videoFrame = _videoFrame; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 57 | @synthesize i420TextureCache = _i420TextureCache; |
| 58 | |
| 59 | - (instancetype)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)format { |
| 60 | return [self initWithFrame:frame pixelFormat:format shader:[[RTCDefaultShader alloc] init]]; |
| 61 | } |
| 62 | |
| 63 | - (instancetype)initWithFrame:(NSRect)frame |
| 64 | pixelFormat:(NSOpenGLPixelFormat *)format |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 65 | shader:(id<RTC_OBJC_TYPE(RTCVideoViewShading)>)shader { |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 66 | if (self = [super initWithFrame:frame pixelFormat:format]) { |
| 67 | _shader = shader; |
| 68 | } |
| 69 | return self; |
| 70 | } |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 71 | |
| 72 | - (void)dealloc { |
| 73 | [self teardownDisplayLink]; |
| 74 | } |
| 75 | |
| 76 | - (void)drawRect:(NSRect)rect { |
| 77 | [self drawFrame]; |
| 78 | } |
| 79 | |
| 80 | - (void)reshape { |
| 81 | [super reshape]; |
| 82 | NSRect frame = [self frame]; |
adam.fedor | fee994c | 2017-06-09 05:16:10 -0700 | [diff] [blame] | 83 | [self ensureGLContext]; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 84 | CGLLockContext([[self openGLContext] CGLContextObj]); |
| 85 | glViewport(0, 0, frame.size.width, frame.size.height); |
| 86 | CGLUnlockContext([[self openGLContext] CGLContextObj]); |
| 87 | } |
| 88 | |
| 89 | - (void)lockFocus { |
| 90 | NSOpenGLContext *context = [self openGLContext]; |
| 91 | [super lockFocus]; |
| 92 | if ([context view] != self) { |
| 93 | [context setView:self]; |
| 94 | } |
| 95 | [context makeCurrentContext]; |
| 96 | } |
| 97 | |
| 98 | - (void)prepareOpenGL { |
| 99 | [super prepareOpenGL]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 100 | [self ensureGLContext]; |
| 101 | glDisable(GL_DITHER); |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 102 | [self setupDisplayLink]; |
| 103 | } |
| 104 | |
| 105 | - (void)clearGLContext { |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 106 | [self ensureGLContext]; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 107 | self.i420TextureCache = nil; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 108 | [super clearGLContext]; |
| 109 | } |
| 110 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 111 | #pragma mark - RTC_OBJC_TYPE(RTCVideoRenderer) |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 112 | |
| 113 | // These methods may be called on non-main thread. |
| 114 | - (void)setSize:(CGSize)size { |
| 115 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 116 | [self.delegate videoView:self didChangeVideoSize:size]; |
| 117 | }); |
| 118 | } |
| 119 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 120 | - (void)renderFrame:(RTC_OBJC_TYPE(RTCVideoFrame) *)frame { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 121 | self.videoFrame = frame; |
| 122 | } |
| 123 | |
| 124 | #pragma mark - Private |
| 125 | |
| 126 | - (void)drawFrame { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame^] | 127 | RTC_OBJC_TYPE(RTCVideoFrame) *frame = self.videoFrame; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 128 | if (!frame || frame == _lastDrawnFrame) { |
| 129 | return; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 130 | } |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 131 | // This method may be called from CVDisplayLink callback which isn't on the |
| 132 | // main thread so we have to lock the GL context before drawing. |
| 133 | NSOpenGLContext *context = [self openGLContext]; |
| 134 | CGLLockContext([context CGLContextObj]); |
| 135 | |
| 136 | [self ensureGLContext]; |
| 137 | glClear(GL_COLOR_BUFFER_BIT); |
| 138 | |
| 139 | // Rendering native CVPixelBuffer is not supported on OS X. |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 140 | // TODO(magjed): Add support for NV12 texture cache on OS X. |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 141 | frame = [frame newI420VideoFrame]; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 142 | if (!self.i420TextureCache) { |
| 143 | self.i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:context]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 144 | } |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 145 | RTCI420TextureCache *i420TextureCache = self.i420TextureCache; |
| 146 | if (i420TextureCache) { |
| 147 | [i420TextureCache uploadFrameToTextures:frame]; |
Magnus Jedvert | 6b9653e | 2017-06-05 17:58:34 +0200 | [diff] [blame] | 148 | [_shader applyShadingForFrameWithWidth:frame.width |
| 149 | height:frame.height |
| 150 | rotation:frame.rotation |
| 151 | yPlane:i420TextureCache.yTexture |
| 152 | uPlane:i420TextureCache.uTexture |
| 153 | vPlane:i420TextureCache.vTexture]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 154 | [context flushBuffer]; |
| 155 | _lastDrawnFrame = frame; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 156 | } |
| 157 | CGLUnlockContext([context CGLContextObj]); |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | - (void)setupDisplayLink { |
| 161 | if (_displayLink) { |
| 162 | return; |
| 163 | } |
| 164 | // Synchronize buffer swaps with vertical refresh rate. |
| 165 | GLint swapInt = 1; |
| 166 | [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
| 167 | |
| 168 | // Create display link. |
| 169 | CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); |
| 170 | CVDisplayLinkSetOutputCallback(_displayLink, |
| 171 | &OnDisplayLinkFired, |
| 172 | (__bridge void *)self); |
| 173 | // Set the display link for the current renderer. |
| 174 | CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; |
| 175 | CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; |
| 176 | CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext( |
| 177 | _displayLink, cglContext, cglPixelFormat); |
| 178 | CVDisplayLinkStart(_displayLink); |
| 179 | } |
| 180 | |
| 181 | - (void)teardownDisplayLink { |
| 182 | if (!_displayLink) { |
| 183 | return; |
| 184 | } |
| 185 | CVDisplayLinkRelease(_displayLink); |
| 186 | _displayLink = NULL; |
| 187 | } |
| 188 | |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 189 | - (void)ensureGLContext { |
| 190 | NSOpenGLContext* context = [self openGLContext]; |
| 191 | NSAssert(context, @"context shouldn't be nil"); |
| 192 | if ([NSOpenGLContext currentContext] != context) { |
| 193 | [context makeCurrentContext]; |
| 194 | } |
| 195 | } |
| 196 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 197 | @end |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 198 | |
| 199 | #endif // !TARGET_OS_IPHONE |