blob: 631fb1783ac78e0cb3372a05525924e49b16cc8f [file] [log] [blame]
tkchin@webrtc.org1732a592014-05-19 23:26:01 +00001/*
2 * libjingle
3 * Copyright 2014, Google Inc.
4 *
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
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000095@interface RTCEAGLVideoView () <GLKViewDelegate>
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000096// |i420Frame| is set when we receive a frame from a worker thread and is read
97// from the display link callback so atomicity is required.
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000098@property(atomic, strong) RTCI420Frame* i420Frame;
tkchin@webrtc.org738df892014-06-04 20:19:39 +000099@property(nonatomic, readonly) GLKView* glkView;
100@property(nonatomic, readonly) RTCOpenGLVideoRenderer* glRenderer;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000101@end
102
103@implementation RTCEAGLVideoView {
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000104 RTCDisplayLinkTimer* _timer;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000105 GLKView* _glkView;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000106 RTCOpenGLVideoRenderer* _glRenderer;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000107}
108
109- (instancetype)initWithFrame:(CGRect)frame {
110 if (self = [super initWithFrame:frame]) {
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000111 [self configure];
112 }
113 return self;
114}
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000115
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000116- (instancetype)initWithCoder:(NSCoder *)aDecoder {
117 if (self = [super initWithCoder:aDecoder]) {
118 [self configure];
119 }
120 return self;
121}
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000122
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000123- (void)configure {
124 EAGLContext* glContext =
125 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
126 _glRenderer = [[RTCOpenGLVideoRenderer alloc] initWithContext:glContext];
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000127
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000128 // GLKView manages a framebuffer for us.
129 _glkView = [[GLKView alloc] initWithFrame:CGRectZero
130 context:glContext];
131 _glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
132 _glkView.drawableDepthFormat = GLKViewDrawableDepthFormatNone;
133 _glkView.drawableStencilFormat = GLKViewDrawableStencilFormatNone;
134 _glkView.drawableMultisample = GLKViewDrawableMultisampleNone;
135 _glkView.delegate = self;
136 _glkView.layer.masksToBounds = YES;
137 [self addSubview:_glkView];
138
139 // Listen to application state in order to clean up OpenGL before app goes
140 // away.
141 NSNotificationCenter* notificationCenter =
142 [NSNotificationCenter defaultCenter];
143 [notificationCenter addObserver:self
144 selector:@selector(willResignActive)
145 name:UIApplicationWillResignActiveNotification
146 object:nil];
147 [notificationCenter addObserver:self
148 selector:@selector(didBecomeActive)
149 name:UIApplicationDidBecomeActiveNotification
150 object:nil];
151
152 // Frames are received on a separate thread, so we poll for current frame
153 // using a refresh rate proportional to screen refresh frequency. This
154 // occurs on the main thread.
155 __weak RTCEAGLVideoView* weakSelf = self;
156 _timer = [[RTCDisplayLinkTimer alloc] initWithTimerHandler:^{
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000157 RTCEAGLVideoView* strongSelf = weakSelf;
158 // Don't render if frame hasn't changed.
159 if (strongSelf.glRenderer.lastDrawnFrame == strongSelf.i420Frame) {
160 return;
161 }
162 // This tells the GLKView that it's dirty, which will then call the
163 // GLKViewDelegate method implemented below.
164 [strongSelf.glkView setNeedsDisplay];
165 }];
tkchin@webrtc.org7ce4a582014-12-19 20:47:35 +0000166 [self setupGL];
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000167}
168
169- (void)dealloc {
170 [[NSNotificationCenter defaultCenter] removeObserver:self];
171 UIApplicationState appState =
172 [UIApplication sharedApplication].applicationState;
173 if (appState == UIApplicationStateActive) {
174 [self teardownGL];
175 }
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000176 [_timer invalidate];
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000177}
178
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000179#pragma mark - UIView
180
181- (void)layoutSubviews {
182 [super layoutSubviews];
183 _glkView.frame = self.bounds;
184}
185
186#pragma mark - GLKViewDelegate
187
188// This method is called when the GLKView's content is dirty and needs to be
189// redrawn. This occurs on main thread.
190- (void)glkView:(GLKView*)view drawInRect:(CGRect)rect {
tkchin@webrtc.org90750482014-09-02 20:50:00 +0000191 // The renderer will draw the frame to the framebuffer corresponding to the
192 // one used by |view|.
193 [_glRenderer drawFrame:self.i420Frame];
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000194}
195
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000196#pragma mark - RTCVideoRenderer
197
198// These methods may be called on non-main thread.
199- (void)setSize:(CGSize)size {
200 __weak RTCEAGLVideoView* weakSelf = self;
201 dispatch_async(dispatch_get_main_queue(), ^{
202 RTCEAGLVideoView* strongSelf = weakSelf;
203 [strongSelf.delegate videoView:strongSelf didChangeVideoSize:size];
204 });
205}
206
207- (void)renderFrame:(RTCI420Frame*)frame {
208 self.i420Frame = frame;
209}
210
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000211#pragma mark - Private
212
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000213- (void)setupGL {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000214 self.i420Frame = nil;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000215 [_glRenderer setupGL];
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000216 _timer.isPaused = NO;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000217}
218
219- (void)teardownGL {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000220 self.i420Frame = nil;
tkchin@webrtc.org738df892014-06-04 20:19:39 +0000221 _timer.isPaused = YES;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000222 [_glkView deleteDrawable];
223 [_glRenderer teardownGL];
224}
225
226- (void)didBecomeActive {
227 [self setupGL];
228}
229
230- (void)willResignActive {
231 [self teardownGL];
232}
233
234@end