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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "WebRTC/RTCEAGLVideoView.h" |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 12 | |
| 13 | #import <GLKit/GLKit.h> |
| 14 | |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 15 | #import "RTCDefaultShader.h" |
| 16 | #import "RTCI420TextureCache.h" |
| 17 | #import "RTCNV12TextureCache.h" |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 18 | #import "WebRTC/RTCLogging.h" |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 19 | #import "WebRTC/RTCVideoFrame.h" |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 20 | #import "WebRTC/RTCVideoFrameBuffer.h" |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 21 | |
| 22 | // RTCDisplayLinkTimer wraps a CADisplayLink and is set to fire every two screen |
| 23 | // refreshes, which should be 30fps. We wrap the display link in order to avoid |
| 24 | // a retain cycle since CADisplayLink takes a strong reference onto its target. |
| 25 | // The timer is paused by default. |
| 26 | @interface RTCDisplayLinkTimer : NSObject |
| 27 | |
| 28 | @property(nonatomic) BOOL isPaused; |
| 29 | |
| 30 | - (instancetype)initWithTimerHandler:(void (^)(void))timerHandler; |
| 31 | - (void)invalidate; |
| 32 | |
| 33 | @end |
| 34 | |
| 35 | @implementation RTCDisplayLinkTimer { |
| 36 | CADisplayLink *_displayLink; |
| 37 | void (^_timerHandler)(void); |
| 38 | } |
| 39 | |
| 40 | - (instancetype)initWithTimerHandler:(void (^)(void))timerHandler { |
| 41 | NSParameterAssert(timerHandler); |
| 42 | if (self = [super init]) { |
| 43 | _timerHandler = timerHandler; |
| 44 | _displayLink = |
| 45 | [CADisplayLink displayLinkWithTarget:self |
| 46 | selector:@selector(displayLinkDidFire:)]; |
| 47 | _displayLink.paused = YES; |
| 48 | // Set to half of screen refresh, which should be 30fps. |
| 49 | [_displayLink setFrameInterval:2]; |
| 50 | [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] |
| 51 | forMode:NSRunLoopCommonModes]; |
| 52 | } |
| 53 | return self; |
| 54 | } |
| 55 | |
| 56 | - (void)dealloc { |
| 57 | [self invalidate]; |
| 58 | } |
| 59 | |
| 60 | - (BOOL)isPaused { |
| 61 | return _displayLink.paused; |
| 62 | } |
| 63 | |
| 64 | - (void)setIsPaused:(BOOL)isPaused { |
| 65 | _displayLink.paused = isPaused; |
| 66 | } |
| 67 | |
| 68 | - (void)invalidate { |
| 69 | [_displayLink invalidate]; |
| 70 | } |
| 71 | |
| 72 | - (void)displayLinkDidFire:(CADisplayLink *)displayLink { |
| 73 | _timerHandler(); |
| 74 | } |
| 75 | |
| 76 | @end |
| 77 | |
| 78 | // RTCEAGLVideoView wraps a GLKView which is setup with |
| 79 | // enableSetNeedsDisplay = NO for the purpose of gaining control of |
| 80 | // exactly when to call -[GLKView display]. This need for extra |
| 81 | // control is required to avoid triggering method calls on GLKView |
| 82 | // that results in attempting to bind the underlying render buffer |
| 83 | // when the drawable size would be empty which would result in the |
| 84 | // error GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT. -[GLKView display] is |
| 85 | // the method that will trigger the binding of the render |
| 86 | // buffer. Because the standard behaviour of -[UIView setNeedsDisplay] |
| 87 | // is disabled for the reasons above, the RTCEAGLVideoView maintains |
| 88 | // its own |isDirty| flag. |
| 89 | |
| 90 | @interface RTCEAGLVideoView () <GLKViewDelegate> |
| 91 | // |videoFrame| is set when we receive a frame from a worker thread and is read |
| 92 | // from the display link callback so atomicity is required. |
| 93 | @property(atomic, strong) RTCVideoFrame *videoFrame; |
| 94 | @property(nonatomic, readonly) GLKView *glkView; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 95 | @end |
| 96 | |
| 97 | @implementation RTCEAGLVideoView { |
| 98 | RTCDisplayLinkTimer *_timer; |
tkchin | 41a3287 | 2016-08-17 16:02:58 -0700 | [diff] [blame] | 99 | EAGLContext *_glContext; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 100 | // This flag should only be set and read on the main thread (e.g. by |
| 101 | // setNeedsDisplay) |
| 102 | BOOL _isDirty; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 103 | id<RTCVideoViewShading> _shader; |
| 104 | RTCNV12TextureCache *_nv12TextureCache; |
| 105 | RTCI420TextureCache *_i420TextureCache; |
Maxim Pavlov | a72b7fc | 2018-04-10 16:57:43 +0300 | [diff] [blame^] | 106 | // As timestamps should be unique between frames, will store last |
| 107 | // drawn frame timestamp instead of the whole frame to reduce memory usage. |
| 108 | int64_t _lastDrawnFrameTimeStampNs; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @synthesize delegate = _delegate; |
| 112 | @synthesize videoFrame = _videoFrame; |
| 113 | @synthesize glkView = _glkView; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 114 | |
| 115 | - (instancetype)initWithFrame:(CGRect)frame { |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 116 | return [self initWithFrame:frame shader:[[RTCDefaultShader alloc] init]]; |
| 117 | } |
| 118 | |
| 119 | - (instancetype)initWithCoder:(NSCoder *)aDecoder { |
| 120 | return [self initWithCoder:aDecoder shader:[[RTCDefaultShader alloc] init]]; |
| 121 | } |
| 122 | |
| 123 | - (instancetype)initWithFrame:(CGRect)frame shader:(id<RTCVideoViewShading>)shader { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 124 | if (self = [super initWithFrame:frame]) { |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 125 | _shader = shader; |
magjed | 1c12b81 | 2017-07-31 09:11:46 -0700 | [diff] [blame] | 126 | if (![self configure]) { |
| 127 | return nil; |
| 128 | } |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 129 | } |
| 130 | return self; |
| 131 | } |
| 132 | |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 133 | - (instancetype)initWithCoder:(NSCoder *)aDecoder shader:(id<RTCVideoViewShading>)shader { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 134 | if (self = [super initWithCoder:aDecoder]) { |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 135 | _shader = shader; |
magjed | 1c12b81 | 2017-07-31 09:11:46 -0700 | [diff] [blame] | 136 | if (![self configure]) { |
| 137 | return nil; |
| 138 | } |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 139 | } |
| 140 | return self; |
| 141 | } |
| 142 | |
magjed | 1c12b81 | 2017-07-31 09:11:46 -0700 | [diff] [blame] | 143 | - (BOOL)configure { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 144 | EAGLContext *glContext = |
| 145 | [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; |
| 146 | if (!glContext) { |
| 147 | glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
| 148 | } |
magjed | 1c12b81 | 2017-07-31 09:11:46 -0700 | [diff] [blame] | 149 | if (!glContext) { |
| 150 | RTCLogError(@"Failed to create EAGLContext"); |
| 151 | return NO; |
| 152 | } |
tkchin | 41a3287 | 2016-08-17 16:02:58 -0700 | [diff] [blame] | 153 | _glContext = glContext; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 154 | |
| 155 | // GLKView manages a framebuffer for us. |
| 156 | _glkView = [[GLKView alloc] initWithFrame:CGRectZero |
tkchin | 41a3287 | 2016-08-17 16:02:58 -0700 | [diff] [blame] | 157 | context:_glContext]; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 158 | _glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; |
| 159 | _glkView.drawableDepthFormat = GLKViewDrawableDepthFormatNone; |
| 160 | _glkView.drawableStencilFormat = GLKViewDrawableStencilFormatNone; |
| 161 | _glkView.drawableMultisample = GLKViewDrawableMultisampleNone; |
| 162 | _glkView.delegate = self; |
| 163 | _glkView.layer.masksToBounds = YES; |
| 164 | _glkView.enableSetNeedsDisplay = NO; |
| 165 | [self addSubview:_glkView]; |
| 166 | |
| 167 | // Listen to application state in order to clean up OpenGL before app goes |
| 168 | // away. |
| 169 | NSNotificationCenter *notificationCenter = |
| 170 | [NSNotificationCenter defaultCenter]; |
| 171 | [notificationCenter addObserver:self |
| 172 | selector:@selector(willResignActive) |
| 173 | name:UIApplicationWillResignActiveNotification |
| 174 | object:nil]; |
| 175 | [notificationCenter addObserver:self |
| 176 | selector:@selector(didBecomeActive) |
| 177 | name:UIApplicationDidBecomeActiveNotification |
| 178 | object:nil]; |
| 179 | |
| 180 | // Frames are received on a separate thread, so we poll for current frame |
| 181 | // using a refresh rate proportional to screen refresh frequency. This |
| 182 | // occurs on the main thread. |
| 183 | __weak RTCEAGLVideoView *weakSelf = self; |
| 184 | _timer = [[RTCDisplayLinkTimer alloc] initWithTimerHandler:^{ |
| 185 | RTCEAGLVideoView *strongSelf = weakSelf; |
| 186 | [strongSelf displayLinkTimerDidFire]; |
| 187 | }]; |
Gustavo Garcia | 7281f92 | 2017-11-07 17:54:03 +0100 | [diff] [blame] | 188 | if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) { |
| 189 | [self setupGL]; |
| 190 | } |
magjed | 1c12b81 | 2017-07-31 09:11:46 -0700 | [diff] [blame] | 191 | return YES; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | - (void)dealloc { |
| 195 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 196 | UIApplicationState appState = |
| 197 | [UIApplication sharedApplication].applicationState; |
| 198 | if (appState == UIApplicationStateActive) { |
| 199 | [self teardownGL]; |
| 200 | } |
| 201 | [_timer invalidate]; |
Gustavo Garcia | 730add8 | 2018-01-04 02:45:38 +0100 | [diff] [blame] | 202 | [self ensureGLContext]; |
| 203 | _shader = nil; |
tkchin | 41a3287 | 2016-08-17 16:02:58 -0700 | [diff] [blame] | 204 | if (_glContext && [EAGLContext currentContext] == _glContext) { |
| 205 | [EAGLContext setCurrentContext:nil]; |
| 206 | } |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | #pragma mark - UIView |
| 210 | |
| 211 | - (void)setNeedsDisplay { |
| 212 | [super setNeedsDisplay]; |
| 213 | _isDirty = YES; |
| 214 | } |
| 215 | |
| 216 | - (void)setNeedsDisplayInRect:(CGRect)rect { |
| 217 | [super setNeedsDisplayInRect:rect]; |
| 218 | _isDirty = YES; |
| 219 | } |
| 220 | |
| 221 | - (void)layoutSubviews { |
| 222 | [super layoutSubviews]; |
| 223 | _glkView.frame = self.bounds; |
| 224 | } |
| 225 | |
| 226 | #pragma mark - GLKViewDelegate |
| 227 | |
| 228 | // This method is called when the GLKView's content is dirty and needs to be |
| 229 | // redrawn. This occurs on main thread. |
| 230 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { |
| 231 | // The renderer will draw the frame to the framebuffer corresponding to the |
| 232 | // one used by |view|. |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 233 | RTCVideoFrame *frame = self.videoFrame; |
Maxim Pavlov | a72b7fc | 2018-04-10 16:57:43 +0300 | [diff] [blame^] | 234 | if (!frame || frame.timeStampNs == _lastDrawnFrameTimeStampNs) { |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | [self ensureGLContext]; |
| 238 | glClear(GL_COLOR_BUFFER_BIT); |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 239 | if ([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) { |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 240 | if (!_nv12TextureCache) { |
| 241 | _nv12TextureCache = [[RTCNV12TextureCache alloc] initWithContext:_glContext]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 242 | } |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 243 | if (_nv12TextureCache) { |
| 244 | [_nv12TextureCache uploadFrameToTextures:frame]; |
Magnus Jedvert | 6b9653e | 2017-06-05 17:58:34 +0200 | [diff] [blame] | 245 | [_shader applyShadingForFrameWithWidth:frame.width |
| 246 | height:frame.height |
| 247 | rotation:frame.rotation |
| 248 | yPlane:_nv12TextureCache.yTexture |
| 249 | uvPlane:_nv12TextureCache.uvTexture]; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 250 | [_nv12TextureCache releaseTextures]; |
Maxim Pavlov | a72b7fc | 2018-04-10 16:57:43 +0300 | [diff] [blame^] | 251 | |
| 252 | _lastDrawnFrameTimeStampNs = self.videoFrame.timeStampNs; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 253 | } |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 254 | } else { |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 255 | if (!_i420TextureCache) { |
| 256 | _i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:_glContext]; |
| 257 | } |
| 258 | [_i420TextureCache uploadFrameToTextures:frame]; |
Magnus Jedvert | 6b9653e | 2017-06-05 17:58:34 +0200 | [diff] [blame] | 259 | [_shader applyShadingForFrameWithWidth:frame.width |
| 260 | height:frame.height |
| 261 | rotation:frame.rotation |
| 262 | yPlane:_i420TextureCache.yTexture |
| 263 | uPlane:_i420TextureCache.uTexture |
| 264 | vPlane:_i420TextureCache.vTexture]; |
Maxim Pavlov | a72b7fc | 2018-04-10 16:57:43 +0300 | [diff] [blame^] | 265 | |
| 266 | _lastDrawnFrameTimeStampNs = self.videoFrame.timeStampNs; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 267 | } |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | #pragma mark - RTCVideoRenderer |
| 271 | |
| 272 | // These methods may be called on non-main thread. |
| 273 | - (void)setSize:(CGSize)size { |
| 274 | __weak RTCEAGLVideoView *weakSelf = self; |
| 275 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 276 | RTCEAGLVideoView *strongSelf = weakSelf; |
| 277 | [strongSelf.delegate videoView:strongSelf didChangeVideoSize:size]; |
| 278 | }); |
| 279 | } |
| 280 | |
| 281 | - (void)renderFrame:(RTCVideoFrame *)frame { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 282 | self.videoFrame = frame; |
| 283 | } |
| 284 | |
| 285 | #pragma mark - Private |
| 286 | |
| 287 | - (void)displayLinkTimerDidFire { |
| 288 | // Don't render unless video frame have changed or the view content |
| 289 | // has explicitly been marked dirty. |
Maxim Pavlov | a72b7fc | 2018-04-10 16:57:43 +0300 | [diff] [blame^] | 290 | if (!_isDirty && _lastDrawnFrameTimeStampNs == self.videoFrame.timeStampNs) { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
| 294 | // Always reset isDirty at this point, even if -[GLKView display] |
| 295 | // won't be called in the case the drawable size is empty. |
| 296 | _isDirty = NO; |
| 297 | |
| 298 | // Only call -[GLKView display] if the drawable size is |
| 299 | // non-empty. Calling display will make the GLKView setup its |
| 300 | // render buffer if necessary, but that will fail with error |
| 301 | // GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT if size is empty. |
| 302 | if (self.bounds.size.width > 0 && self.bounds.size.height > 0) { |
| 303 | [_glkView display]; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | - (void)setupGL { |
| 308 | self.videoFrame = nil; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 309 | [self ensureGLContext]; |
| 310 | glDisable(GL_DITHER); |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 311 | _timer.isPaused = NO; |
| 312 | } |
| 313 | |
| 314 | - (void)teardownGL { |
| 315 | self.videoFrame = nil; |
| 316 | _timer.isPaused = YES; |
| 317 | [_glkView deleteDrawable]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 318 | [self ensureGLContext]; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 319 | _nv12TextureCache = nil; |
| 320 | _i420TextureCache = nil; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | - (void)didBecomeActive { |
| 324 | [self setupGL]; |
| 325 | } |
| 326 | |
| 327 | - (void)willResignActive { |
| 328 | [self teardownGL]; |
| 329 | } |
| 330 | |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 331 | - (void)ensureGLContext { |
| 332 | NSAssert(_glContext, @"context shouldn't be nil"); |
| 333 | if ([EAGLContext currentContext] != _glContext) { |
| 334 | [EAGLContext setCurrentContext:_glContext]; |
| 335 | } |
| 336 | } |
| 337 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 338 | @end |