tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 32 | #import "RTCEAGLVideoView.h" |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 33 | |
| 34 | #import <GLKit/GLKit.h> |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 35 | |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 36 | #import "RTCI420Frame.h" |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 37 | #import "RTCOpenGLVideoRenderer.h" |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 38 | |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 39 | // 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.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 95 | @interface RTCEAGLVideoView () <GLKViewDelegate> |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 96 | // |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.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 98 | @property(atomic, strong) RTCI420Frame* i420Frame; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 99 | @property(nonatomic, readonly) GLKView* glkView; |
| 100 | @property(nonatomic, readonly) RTCOpenGLVideoRenderer* glRenderer; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 101 | @end |
| 102 | |
| 103 | @implementation RTCEAGLVideoView { |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 104 | RTCDisplayLinkTimer* _timer; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 105 | GLKView* _glkView; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 106 | RTCOpenGLVideoRenderer* _glRenderer; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | - (instancetype)initWithFrame:(CGRect)frame { |
| 110 | if (self = [super initWithFrame:frame]) { |
tkchin@webrtc.org | 7ce4a58 | 2014-12-19 20:47:35 +0000 | [diff] [blame^] | 111 | [self configure]; |
| 112 | } |
| 113 | return self; |
| 114 | } |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 115 | |
tkchin@webrtc.org | 7ce4a58 | 2014-12-19 20:47:35 +0000 | [diff] [blame^] | 116 | - (instancetype)initWithCoder:(NSCoder *)aDecoder { |
| 117 | if (self = [super initWithCoder:aDecoder]) { |
| 118 | [self configure]; |
| 119 | } |
| 120 | return self; |
| 121 | } |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 122 | |
tkchin@webrtc.org | 7ce4a58 | 2014-12-19 20:47:35 +0000 | [diff] [blame^] | 123 | - (void)configure { |
| 124 | EAGLContext* glContext = |
| 125 | [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
| 126 | _glRenderer = [[RTCOpenGLVideoRenderer alloc] initWithContext:glContext]; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 127 | |
tkchin@webrtc.org | 7ce4a58 | 2014-12-19 20:47:35 +0000 | [diff] [blame^] | 128 | // 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.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 157 | 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.org | 7ce4a58 | 2014-12-19 20:47:35 +0000 | [diff] [blame^] | 166 | [self setupGL]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 167 | } |
| 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.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 176 | [_timer invalidate]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 177 | } |
| 178 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 179 | #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.org | 9075048 | 2014-09-02 20:50:00 +0000 | [diff] [blame] | 191 | // The renderer will draw the frame to the framebuffer corresponding to the |
| 192 | // one used by |view|. |
| 193 | [_glRenderer drawFrame:self.i420Frame]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 194 | } |
| 195 | |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 196 | #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.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 211 | #pragma mark - Private |
| 212 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 213 | - (void)setupGL { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 214 | self.i420Frame = nil; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 215 | [_glRenderer setupGL]; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 216 | _timer.isPaused = NO; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | - (void)teardownGL { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 220 | self.i420Frame = nil; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 221 | _timer.isPaused = YES; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 222 | [_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 |