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<RTCVideoViewShader> _shaderDelegate; |
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 shaderDelegate:[[RTCDefaultShader alloc] init ]]; | |
114 } | |
115 | |
116 - (instancetype)initWithCoder:(NSCoder *)aDecoder { | |
117 return [self initWithCoder:aDecoder shaderDelegate:[[RTCDefaultShader alloc] i nit]]; | |
118 } | |
119 | |
120 - (instancetype)initWithFrame:(CGRect)frame shaderDelegate:(id<RTCVideoViewShade r>)shaderDelegate { | |
daniela-webrtc
2017/05/26 13:48:28
The Delegate pre/suffix is not relevant any more.
magjed_webrtc
2017/05/28 10:26:07
Done.
| |
110 if (self = [super initWithFrame:frame]) { | 121 if (self = [super initWithFrame:frame]) { |
122 _shaderDelegate = shaderDelegate; | |
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 |
129 shaderDelegate:(id<RTCVideoViewShader>)shaderDelegate { | |
117 if (self = [super initWithCoder:aDecoder]) { | 130 if (self = [super initWithCoder:aDecoder]) { |
131 _shaderDelegate = shaderDelegate; | |
118 [self configure]; | 132 [self configure]; |
119 } | 133 } |
120 return self; | 134 return self; |
121 } | 135 } |
122 | 136 |
123 - (void)configure { | 137 - (void)configure { |
124 EAGLContext *glContext = | 138 EAGLContext *glContext = |
125 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; | 139 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; |
126 if (!glContext) { | 140 if (!glContext) { |
127 glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; | 141 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. | 214 // redrawn. This occurs on main thread. |
201 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { | 215 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { |
202 // The renderer will draw the frame to the framebuffer corresponding to the | 216 // The renderer will draw the frame to the framebuffer corresponding to the |
203 // one used by |view|. | 217 // one used by |view|. |
204 RTCVideoFrame *frame = self.videoFrame; | 218 RTCVideoFrame *frame = self.videoFrame; |
205 if (!frame || frame == _lastDrawnFrame) { | 219 if (!frame || frame == _lastDrawnFrame) { |
206 return; | 220 return; |
207 } | 221 } |
208 [self ensureGLContext]; | 222 [self ensureGLContext]; |
209 glClear(GL_COLOR_BUFFER_BIT); | 223 glClear(GL_COLOR_BUFFER_BIT); |
210 id<RTCShader> shader = nil; | |
211 if (frame.nativeHandle) { | 224 if (frame.nativeHandle) { |
212 if (!_nv12Shader) { | 225 if (!_nv12TextureCache) { |
213 _nv12Shader = [[RTCNativeNV12Shader alloc] initWithContext:_glContext]; | 226 _nv12TextureCache = [[RTCNV12TextureCache alloc] initWithContext:_glContex t]; |
214 } | 227 } |
215 shader = _nv12Shader; | 228 if (_nv12TextureCache) { |
229 [_nv12TextureCache uploadFrameToTextures:frame]; | |
230 [_shaderDelegate applyShadingForFrameWithRotation:frame.rotation | |
231 yPlane:_nv12TextureCache.yTextu re | |
232 uvPlane:_nv12TextureCache.uvText ure]; | |
233 [_nv12TextureCache releaseTextures]; | |
234 } | |
216 } else { | 235 } else { |
217 if (!_i420Shader) { | 236 if (!_i420TextureCache) { |
218 _i420Shader = [[RTCI420Shader alloc] initWithContext:_glContext]; | 237 _i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:_glContex t]; |
219 } | 238 } |
220 shader = _i420Shader; | 239 [_i420TextureCache uploadFrameToTextures:frame]; |
221 } | 240 [_shaderDelegate applyShadingForFrameWithRotation:frame.rotation |
222 if (shader && [shader drawFrame:frame]) { | 241 yPlane:_i420TextureCache.yTexture |
223 _lastDrawnFrame = frame; | 242 uPlane:_i420TextureCache.uTexture |
224 } else { | 243 vPlane:_i420TextureCache.vTexture ]; |
225 RTCLog(@"Failed to draw frame."); | |
226 } | 244 } |
227 } | 245 } |
228 | 246 |
229 #pragma mark - RTCVideoRenderer | 247 #pragma mark - RTCVideoRenderer |
230 | 248 |
231 // These methods may be called on non-main thread. | 249 // These methods may be called on non-main thread. |
232 - (void)setSize:(CGSize)size { | 250 - (void)setSize:(CGSize)size { |
233 __weak RTCEAGLVideoView *weakSelf = self; | 251 __weak RTCEAGLVideoView *weakSelf = self; |
234 dispatch_async(dispatch_get_main_queue(), ^{ | 252 dispatch_async(dispatch_get_main_queue(), ^{ |
235 RTCEAGLVideoView *strongSelf = weakSelf; | 253 RTCEAGLVideoView *strongSelf = weakSelf; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 [self ensureGLContext]; | 286 [self ensureGLContext]; |
269 glDisable(GL_DITHER); | 287 glDisable(GL_DITHER); |
270 _timer.isPaused = NO; | 288 _timer.isPaused = NO; |
271 } | 289 } |
272 | 290 |
273 - (void)teardownGL { | 291 - (void)teardownGL { |
274 self.videoFrame = nil; | 292 self.videoFrame = nil; |
275 _timer.isPaused = YES; | 293 _timer.isPaused = YES; |
276 [_glkView deleteDrawable]; | 294 [_glkView deleteDrawable]; |
277 [self ensureGLContext]; | 295 [self ensureGLContext]; |
278 _i420Shader = nil; | 296 _nv12TextureCache = nil; |
279 _nv12Shader = nil; | 297 _i420TextureCache = nil; |
280 } | 298 } |
281 | 299 |
282 - (void)didBecomeActive { | 300 - (void)didBecomeActive { |
283 [self setupGL]; | 301 [self setupGL]; |
284 } | 302 } |
285 | 303 |
286 - (void)willResignActive { | 304 - (void)willResignActive { |
287 [self teardownGL]; | 305 [self teardownGL]; |
288 } | 306 } |
289 | 307 |
290 - (void)ensureGLContext { | 308 - (void)ensureGLContext { |
291 NSAssert(_glContext, @"context shouldn't be nil"); | 309 NSAssert(_glContext, @"context shouldn't be nil"); |
292 if ([EAGLContext currentContext] != _glContext) { | 310 if ([EAGLContext currentContext] != _glContext) { |
293 [EAGLContext setCurrentContext:_glContext]; | 311 [EAGLContext setCurrentContext:_glContext]; |
294 } | 312 } |
295 } | 313 } |
296 | 314 |
297 @end | 315 @end |
OLD | NEW |