| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #import "WebRTC/RTCEAGLVideoView.h" | 11 #import "WebRTC/RTCEAGLVideoView.h" |
| 12 | 12 |
| 13 #import <GLKit/GLKit.h> | 13 #import <GLKit/GLKit.h> |
| 14 | 14 |
| 15 #import "RTCShader+Private.h" | 15 #import "RTCDefaultShader.h" |
| 16 #import "RTCI420TextureCache.h" |
| 17 #import "RTCNV12TextureCache.h" |
| 16 #import "WebRTC/RTCLogging.h" | 18 #import "WebRTC/RTCLogging.h" |
| 17 #import "WebRTC/RTCVideoFrame.h" | 19 #import "WebRTC/RTCVideoFrame.h" |
| 18 | 20 |
| 19 // RTCDisplayLinkTimer wraps a CADisplayLink and is set to fire every two screen | 21 // RTCDisplayLinkTimer wraps a CADisplayLink and is set to fire every two screen |
| 20 // refreshes, which should be 30fps. We wrap the display link in order to avoid | 22 // refreshes, which should be 30fps. We wrap the display link in order to avoid |
| 21 // a retain cycle since CADisplayLink takes a strong reference onto its target. | 23 // a retain cycle since CADisplayLink takes a strong reference onto its target. |
| 22 // The timer is paused by default. | 24 // The timer is paused by default. |
| 23 @interface RTCDisplayLinkTimer : NSObject | 25 @interface RTCDisplayLinkTimer : NSObject |
| 24 | 26 |
| 25 @property(nonatomic) BOOL isPaused; | 27 @property(nonatomic) BOOL isPaused; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 @property(atomic, strong) RTCVideoFrame *videoFrame; | 92 @property(atomic, strong) RTCVideoFrame *videoFrame; |
| 91 @property(nonatomic, readonly) GLKView *glkView; | 93 @property(nonatomic, readonly) GLKView *glkView; |
| 92 @end | 94 @end |
| 93 | 95 |
| 94 @implementation RTCEAGLVideoView { | 96 @implementation RTCEAGLVideoView { |
| 95 RTCDisplayLinkTimer *_timer; | 97 RTCDisplayLinkTimer *_timer; |
| 96 EAGLContext *_glContext; | 98 EAGLContext *_glContext; |
| 97 // This flag should only be set and read on the main thread (e.g. by | 99 // This flag should only be set and read on the main thread (e.g. by |
| 98 // setNeedsDisplay) | 100 // setNeedsDisplay) |
| 99 BOOL _isDirty; | 101 BOOL _isDirty; |
| 100 id<RTCShader> _i420Shader; | 102 id<RTCVideoViewShading> _shader; |
| 101 id<RTCShader> _nv12Shader; | 103 RTCNV12TextureCache *_nv12TextureCache; |
| 104 RTCI420TextureCache *_i420TextureCache; |
| 102 RTCVideoFrame *_lastDrawnFrame; | 105 RTCVideoFrame *_lastDrawnFrame; |
| 103 } | 106 } |
| 104 | 107 |
| 105 @synthesize delegate = _delegate; | 108 @synthesize delegate = _delegate; |
| 106 @synthesize videoFrame = _videoFrame; | 109 @synthesize videoFrame = _videoFrame; |
| 107 @synthesize glkView = _glkView; | 110 @synthesize glkView = _glkView; |
| 108 | 111 |
| 109 - (instancetype)initWithFrame:(CGRect)frame { | 112 - (instancetype)initWithFrame:(CGRect)frame { |
| 113 return [self initWithFrame:frame shader:[[RTCDefaultShader alloc] init]]; |
| 114 } |
| 115 |
| 116 - (instancetype)initWithCoder:(NSCoder *)aDecoder { |
| 117 return [self initWithCoder:aDecoder shader:[[RTCDefaultShader alloc] init]]; |
| 118 } |
| 119 |
| 120 - (instancetype)initWithFrame:(CGRect)frame shader:(id<RTCVideoViewShading>)shad
er { |
| 110 if (self = [super initWithFrame:frame]) { | 121 if (self = [super initWithFrame:frame]) { |
| 122 _shader = shader; |
| 111 [self configure]; | 123 [self configure]; |
| 112 } | 124 } |
| 113 return self; | 125 return self; |
| 114 } | 126 } |
| 115 | 127 |
| 116 - (instancetype)initWithCoder:(NSCoder *)aDecoder { | 128 - (instancetype)initWithCoder:(NSCoder *)aDecoder shader:(id<RTCVideoViewShading
>)shader { |
| 117 if (self = [super initWithCoder:aDecoder]) { | 129 if (self = [super initWithCoder:aDecoder]) { |
| 130 _shader = shader; |
| 118 [self configure]; | 131 [self configure]; |
| 119 } | 132 } |
| 120 return self; | 133 return self; |
| 121 } | 134 } |
| 122 | 135 |
| 123 - (void)configure { | 136 - (void)configure { |
| 124 EAGLContext *glContext = | 137 EAGLContext *glContext = |
| 125 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; | 138 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; |
| 126 if (!glContext) { | 139 if (!glContext) { |
| 127 glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; | 140 glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // redrawn. This occurs on main thread. | 213 // redrawn. This occurs on main thread. |
| 201 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { | 214 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { |
| 202 // The renderer will draw the frame to the framebuffer corresponding to the | 215 // The renderer will draw the frame to the framebuffer corresponding to the |
| 203 // one used by |view|. | 216 // one used by |view|. |
| 204 RTCVideoFrame *frame = self.videoFrame; | 217 RTCVideoFrame *frame = self.videoFrame; |
| 205 if (!frame || frame == _lastDrawnFrame) { | 218 if (!frame || frame == _lastDrawnFrame) { |
| 206 return; | 219 return; |
| 207 } | 220 } |
| 208 [self ensureGLContext]; | 221 [self ensureGLContext]; |
| 209 glClear(GL_COLOR_BUFFER_BIT); | 222 glClear(GL_COLOR_BUFFER_BIT); |
| 210 id<RTCShader> shader = nil; | |
| 211 if (frame.nativeHandle) { | 223 if (frame.nativeHandle) { |
| 212 if (!_nv12Shader) { | 224 if (!_nv12TextureCache) { |
| 213 _nv12Shader = [[RTCNativeNV12Shader alloc] initWithContext:_glContext]; | 225 _nv12TextureCache = [[RTCNV12TextureCache alloc] initWithContext:_glContex
t]; |
| 214 } | 226 } |
| 215 shader = _nv12Shader; | 227 if (_nv12TextureCache) { |
| 228 [_nv12TextureCache uploadFrameToTextures:frame]; |
| 229 [_shader applyShadingForFrameWithRotation:frame.rotation |
| 230 yPlane:_nv12TextureCache.yTexture |
| 231 uvPlane:_nv12TextureCache.uvTexture]; |
| 232 [_nv12TextureCache releaseTextures]; |
| 233 } |
| 216 } else { | 234 } else { |
| 217 if (!_i420Shader) { | 235 if (!_i420TextureCache) { |
| 218 _i420Shader = [[RTCI420Shader alloc] initWithContext:_glContext]; | 236 _i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:_glContex
t]; |
| 219 } | 237 } |
| 220 shader = _i420Shader; | 238 [_i420TextureCache uploadFrameToTextures:frame]; |
| 221 } | 239 [_shader applyShadingForFrameWithRotation:frame.rotation |
| 222 if (shader && [shader drawFrame:frame]) { | 240 yPlane:_i420TextureCache.yTexture |
| 223 _lastDrawnFrame = frame; | 241 uPlane:_i420TextureCache.uTexture |
| 224 } else { | 242 vPlane:_i420TextureCache.vTexture]; |
| 225 RTCLog(@"Failed to draw frame."); | |
| 226 } | 243 } |
| 227 } | 244 } |
| 228 | 245 |
| 229 #pragma mark - RTCVideoRenderer | 246 #pragma mark - RTCVideoRenderer |
| 230 | 247 |
| 231 // These methods may be called on non-main thread. | 248 // These methods may be called on non-main thread. |
| 232 - (void)setSize:(CGSize)size { | 249 - (void)setSize:(CGSize)size { |
| 233 __weak RTCEAGLVideoView *weakSelf = self; | 250 __weak RTCEAGLVideoView *weakSelf = self; |
| 234 dispatch_async(dispatch_get_main_queue(), ^{ | 251 dispatch_async(dispatch_get_main_queue(), ^{ |
| 235 RTCEAGLVideoView *strongSelf = weakSelf; | 252 RTCEAGLVideoView *strongSelf = weakSelf; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 [self ensureGLContext]; | 285 [self ensureGLContext]; |
| 269 glDisable(GL_DITHER); | 286 glDisable(GL_DITHER); |
| 270 _timer.isPaused = NO; | 287 _timer.isPaused = NO; |
| 271 } | 288 } |
| 272 | 289 |
| 273 - (void)teardownGL { | 290 - (void)teardownGL { |
| 274 self.videoFrame = nil; | 291 self.videoFrame = nil; |
| 275 _timer.isPaused = YES; | 292 _timer.isPaused = YES; |
| 276 [_glkView deleteDrawable]; | 293 [_glkView deleteDrawable]; |
| 277 [self ensureGLContext]; | 294 [self ensureGLContext]; |
| 278 _i420Shader = nil; | 295 _nv12TextureCache = nil; |
| 279 _nv12Shader = nil; | 296 _i420TextureCache = nil; |
| 280 } | 297 } |
| 281 | 298 |
| 282 - (void)didBecomeActive { | 299 - (void)didBecomeActive { |
| 283 [self setupGL]; | 300 [self setupGL]; |
| 284 } | 301 } |
| 285 | 302 |
| 286 - (void)willResignActive { | 303 - (void)willResignActive { |
| 287 [self teardownGL]; | 304 [self teardownGL]; |
| 288 } | 305 } |
| 289 | 306 |
| 290 - (void)ensureGLContext { | 307 - (void)ensureGLContext { |
| 291 NSAssert(_glContext, @"context shouldn't be nil"); | 308 NSAssert(_glContext, @"context shouldn't be nil"); |
| 292 if ([EAGLContext currentContext] != _glContext) { | 309 if ([EAGLContext currentContext] != _glContext) { |
| 293 [EAGLContext setCurrentContext:_glContext]; | 310 [EAGLContext setCurrentContext:_glContext]; |
| 294 } | 311 } |
| 295 } | 312 } |
| 296 | 313 |
| 297 @end | 314 @end |
| OLD | NEW |