blob: d19462c9d97576b841df86cd6f1872fc738de2ba [file] [log] [blame]
tkchin@webrtc.org1732a592014-05-19 23:26:01 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
tkchin@webrtc.org1732a592014-05-19 23:26:01 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#if !defined(__has_feature) || !__has_feature(objc_arc)
29#error "This file requires ARC support."
30#endif
31
tkchin@webrtc.org81257442014-11-04 23:06:15 +000032#import "RTCEAGLVideoView.h"
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000033
34#import <GLKit/GLKit.h>
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000035
tkchin@webrtc.org81257442014-11-04 23:06:15 +000036#import "RTCI420Frame.h"
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000037#import "RTCOpenGLVideoRenderer.h"
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000038
tkchin@webrtc.org738df892014-06-04 20:19:39 +000039// RTCDisplayLinkTimer wraps a CADisplayLink and is set to fire every two screen
40// refreshes, which should be 30fps. We wrap the display link in order to avoid
41// a retain cycle since CADisplayLink takes a strong reference onto its target.
42// The timer is paused by default.
43@interface RTCDisplayLinkTimer : NSObject
44
45@property(nonatomic) BOOL isPaused;
46
47- (instancetype)initWithTimerHandler:(void (^)(void))timerHandler;
48- (void)invalidate;
49
50@end
51
52@implementation RTCDisplayLinkTimer {
53 CADisplayLink* _displayLink;
54 void (^_timerHandler)(void);
55}
56
57- (instancetype)initWithTimerHandler:(void (^)(void))timerHandler {
58 NSParameterAssert(timerHandler);
59 if (self = [super init]) {
60 _timerHandler = timerHandler;
61 _displayLink =
62 [CADisplayLink displayLinkWithTarget:self
63 selector:@selector(displayLinkDidFire:)];
64 _displayLink.paused = YES;
65 // Set to half of screen refresh, which should be 30fps.
66 [_displayLink setFrameInterval:2];
67 [_displayLink addToRunLoop:[NSRunLoop currentRunLoop]
68 forMode:NSRunLoopCommonModes];
69 }
70 return self;
71}
72
73- (void)dealloc {
74 [self invalidate];
75}
76
77- (BOOL)isPaused {
78 return _displayLink.paused;
79}
80
81- (void)setIsPaused:(BOOL)isPaused {
82 _displayLink.paused = isPaused;
83}
84
85- (void)invalidate {
86 [_displayLink invalidate];
87}
88
89- (void)displayLinkDidFire:(CADisplayLink*)displayLink {
90 _timerHandler();
91}
92
93@end
94
christoffer88799d92015-09-25 06:57:50 -070095// RTCEAGLVideoView wraps a GLKView which is setup with
96// enableSetNeedsDisplay = NO for the purpose of gaining control of
97// exactly when to call -[GLKView display]. This need for extra
98// control is required to avoid triggering method calls on GLKView
99// that results in attempting to bind the underlying render buffer
100// when the drawable size would be empty which would result in the
101// error GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT. -[GLKView display] is
102// the method that will trigger the binding of the render
103// buffer. Because the standard behaviour of -[UIView setNeedsDisplay]
104// is disabled for the reasons above, the RTCEAGLVideoView maintains
105// its own |isDirty| flag.
106
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000107@interface RTCEAGLVideoView () <GLKViewDelegate>
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000108// |i420Frame| is set when we receive a frame from a worker thread and is read
109// from the display link callback so atomicity is required.
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000110@property(atomic, strong) RTCI420Frame* i420Frame;
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000111@property(nonatomic, readonly) GLKView* glkView;
112@property(nonatomic, readonly) RTCOpenGLVideoRenderer* glRenderer;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000113@end
114
115@implementation RTCEAGLVideoView {
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000116 RTCDisplayLinkTimer* _timer;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000117 GLKView* _glkView;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000118 RTCOpenGLVideoRenderer* _glRenderer;
christoffer88799d92015-09-25 06:57:50 -0700119 // This flag should only be set and read on the main thread (e.g. by
120 // setNeedsDisplay)
121 BOOL _isDirty;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000122}
123
124- (instancetype)initWithFrame:(CGRect)frame {
125 if (self = [super initWithFrame:frame]) {
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000126 [self configure];
127 }
128 return self;
129}
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000130
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000131- (instancetype)initWithCoder:(NSCoder *)aDecoder {
132 if (self = [super initWithCoder:aDecoder]) {
133 [self configure];
134 }
135 return self;
136}
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000137
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000138- (void)configure {
139 EAGLContext* glContext =
Zeke Chinac7d97f2015-04-20 14:33:25 -0700140 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
141 if (!glContext) {
142 glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
143 }
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000144 _glRenderer = [[RTCOpenGLVideoRenderer alloc] initWithContext:glContext];
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000145
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000146 // GLKView manages a framebuffer for us.
147 _glkView = [[GLKView alloc] initWithFrame:CGRectZero
148 context:glContext];
149 _glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
150 _glkView.drawableDepthFormat = GLKViewDrawableDepthFormatNone;
151 _glkView.drawableStencilFormat = GLKViewDrawableStencilFormatNone;
152 _glkView.drawableMultisample = GLKViewDrawableMultisampleNone;
153 _glkView.delegate = self;
154 _glkView.layer.masksToBounds = YES;
christoffer88799d92015-09-25 06:57:50 -0700155 _glkView.enableSetNeedsDisplay = NO;
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000156 [self addSubview:_glkView];
157
158 // Listen to application state in order to clean up OpenGL before app goes
159 // away.
160 NSNotificationCenter* notificationCenter =
161 [NSNotificationCenter defaultCenter];
162 [notificationCenter addObserver:self
163 selector:@selector(willResignActive)
164 name:UIApplicationWillResignActiveNotification
165 object:nil];
166 [notificationCenter addObserver:self
167 selector:@selector(didBecomeActive)
168 name:UIApplicationDidBecomeActiveNotification
169 object:nil];
170
171 // Frames are received on a separate thread, so we poll for current frame
172 // using a refresh rate proportional to screen refresh frequency. This
173 // occurs on the main thread.
174 __weak RTCEAGLVideoView* weakSelf = self;
175 _timer = [[RTCDisplayLinkTimer alloc] initWithTimerHandler:^{
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000176 RTCEAGLVideoView* strongSelf = weakSelf;
christoffer88799d92015-09-25 06:57:50 -0700177 [strongSelf displayLinkTimerDidFire];
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000178 }];
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000179 [self setupGL];
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000180}
181
182- (void)dealloc {
183 [[NSNotificationCenter defaultCenter] removeObserver:self];
184 UIApplicationState appState =
185 [UIApplication sharedApplication].applicationState;
186 if (appState == UIApplicationStateActive) {
187 [self teardownGL];
188 }
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000189 [_timer invalidate];
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000190}
191
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000192#pragma mark - UIView
193
christoffer88799d92015-09-25 06:57:50 -0700194- (void)setNeedsDisplay {
195 [super setNeedsDisplay];
196 _isDirty = YES;
197}
198
199- (void)setNeedsDisplayInRect:(CGRect)rect {
200 [super setNeedsDisplayInRect:rect];
201 _isDirty = YES;
202}
203
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000204- (void)layoutSubviews {
205 [super layoutSubviews];
206 _glkView.frame = self.bounds;
207}
208
209#pragma mark - GLKViewDelegate
210
211// This method is called when the GLKView's content is dirty and needs to be
212// redrawn. This occurs on main thread.
213- (void)glkView:(GLKView*)view drawInRect:(CGRect)rect {
tkchin@webrtc.org90750482014-09-02 20:50:00 +0000214 // The renderer will draw the frame to the framebuffer corresponding to the
215 // one used by |view|.
216 [_glRenderer drawFrame:self.i420Frame];
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000217}
218
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000219#pragma mark - RTCVideoRenderer
220
221// These methods may be called on non-main thread.
222- (void)setSize:(CGSize)size {
223 __weak RTCEAGLVideoView* weakSelf = self;
224 dispatch_async(dispatch_get_main_queue(), ^{
225 RTCEAGLVideoView* strongSelf = weakSelf;
226 [strongSelf.delegate videoView:strongSelf didChangeVideoSize:size];
227 });
228}
229
230- (void)renderFrame:(RTCI420Frame*)frame {
231 self.i420Frame = frame;
232}
233
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000234#pragma mark - Private
235
christoffer88799d92015-09-25 06:57:50 -0700236- (void)displayLinkTimerDidFire {
237 // Don't render unless video frame have changed or the view content
238 // has explicitly been marked dirty.
239 if (!_isDirty && _glRenderer.lastDrawnFrame == self.i420Frame) {
240 return;
241 }
242
243 // Always reset isDirty at this point, even if -[GLKView display]
244 // won't be called in the case the drawable size is empty.
245 _isDirty = NO;
246
247 // Only call -[GLKView display] if the drawable size is
248 // non-empty. Calling display will make the GLKView setup its
249 // render buffer if necessary, but that will fail with error
250 // GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT if size is empty.
251 if (self.bounds.size.width > 0 && self.bounds.size.height > 0) {
252 [_glkView display];
253 }
254}
255
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000256- (void)setupGL {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000257 self.i420Frame = nil;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000258 [_glRenderer setupGL];
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000259 _timer.isPaused = NO;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000260}
261
262- (void)teardownGL {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000263 self.i420Frame = nil;
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000264 _timer.isPaused = YES;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000265 [_glkView deleteDrawable];
266 [_glRenderer teardownGL];
267}
268
269- (void)didBecomeActive {
270 [self setupGL];
271}
272
273- (void)willResignActive {
274 [self teardownGL];
275}
276
277@end