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]) { |
| 111 | EAGLContext* glContext = |
| 112 | [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 113 | _glRenderer = [[RTCOpenGLVideoRenderer alloc] initWithContext:glContext]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 114 | |
| 115 | // GLKView manages a framebuffer for us. |
| 116 | _glkView = [[GLKView alloc] initWithFrame:CGRectZero |
| 117 | context:glContext]; |
| 118 | _glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; |
| 119 | _glkView.drawableDepthFormat = GLKViewDrawableDepthFormatNone; |
| 120 | _glkView.drawableStencilFormat = GLKViewDrawableStencilFormatNone; |
| 121 | _glkView.drawableMultisample = GLKViewDrawableMultisampleNone; |
| 122 | _glkView.delegate = self; |
| 123 | _glkView.layer.masksToBounds = YES; |
| 124 | [self addSubview:_glkView]; |
| 125 | |
| 126 | // Listen to application state in order to clean up OpenGL before app goes |
| 127 | // away. |
| 128 | NSNotificationCenter* notificationCenter = |
| 129 | [NSNotificationCenter defaultCenter]; |
| 130 | [notificationCenter addObserver:self |
| 131 | selector:@selector(willResignActive) |
| 132 | name:UIApplicationWillResignActiveNotification |
| 133 | object:nil]; |
| 134 | [notificationCenter addObserver:self |
| 135 | selector:@selector(didBecomeActive) |
| 136 | name:UIApplicationDidBecomeActiveNotification |
| 137 | object:nil]; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 138 | |
| 139 | // Frames are received on a separate thread, so we poll for current frame |
| 140 | // using a refresh rate proportional to screen refresh frequency. This |
| 141 | // occurs on the main thread. |
| 142 | __weak RTCEAGLVideoView* weakSelf = self; |
| 143 | _timer = [[RTCDisplayLinkTimer alloc] initWithTimerHandler:^{ |
| 144 | RTCEAGLVideoView* strongSelf = weakSelf; |
| 145 | // Don't render if frame hasn't changed. |
| 146 | if (strongSelf.glRenderer.lastDrawnFrame == strongSelf.i420Frame) { |
| 147 | return; |
| 148 | } |
| 149 | // This tells the GLKView that it's dirty, which will then call the |
| 150 | // GLKViewDelegate method implemented below. |
| 151 | [strongSelf.glkView setNeedsDisplay]; |
| 152 | }]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 153 | [self setupGL]; |
| 154 | } |
| 155 | return self; |
| 156 | } |
| 157 | |
| 158 | - (void)dealloc { |
| 159 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 160 | UIApplicationState appState = |
| 161 | [UIApplication sharedApplication].applicationState; |
| 162 | if (appState == UIApplicationStateActive) { |
| 163 | [self teardownGL]; |
| 164 | } |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 165 | [_timer invalidate]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 166 | } |
| 167 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 168 | #pragma mark - UIView |
| 169 | |
| 170 | - (void)layoutSubviews { |
| 171 | [super layoutSubviews]; |
| 172 | _glkView.frame = self.bounds; |
| 173 | } |
| 174 | |
| 175 | #pragma mark - GLKViewDelegate |
| 176 | |
| 177 | // This method is called when the GLKView's content is dirty and needs to be |
| 178 | // redrawn. This occurs on main thread. |
| 179 | - (void)glkView:(GLKView*)view drawInRect:(CGRect)rect { |
tkchin@webrtc.org | 9075048 | 2014-09-02 20:50:00 +0000 | [diff] [blame] | 180 | // The renderer will draw the frame to the framebuffer corresponding to the |
| 181 | // one used by |view|. |
| 182 | [_glRenderer drawFrame:self.i420Frame]; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 183 | } |
| 184 | |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 185 | #pragma mark - RTCVideoRenderer |
| 186 | |
| 187 | // These methods may be called on non-main thread. |
| 188 | - (void)setSize:(CGSize)size { |
| 189 | __weak RTCEAGLVideoView* weakSelf = self; |
| 190 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 191 | RTCEAGLVideoView* strongSelf = weakSelf; |
| 192 | [strongSelf.delegate videoView:strongSelf didChangeVideoSize:size]; |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | - (void)renderFrame:(RTCI420Frame*)frame { |
| 197 | self.i420Frame = frame; |
| 198 | } |
| 199 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 200 | #pragma mark - Private |
| 201 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 202 | - (void)setupGL { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 203 | self.i420Frame = nil; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 204 | [_glRenderer setupGL]; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 205 | _timer.isPaused = NO; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | - (void)teardownGL { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 209 | self.i420Frame = nil; |
tkchin@webrtc.org | 738df89 | 2014-06-04 20:19:39 +0000 | [diff] [blame] | 210 | _timer.isPaused = YES; |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame] | 211 | [_glkView deleteDrawable]; |
| 212 | [_glRenderer teardownGL]; |
| 213 | } |
| 214 | |
| 215 | - (void)didBecomeActive { |
| 216 | [self setupGL]; |
| 217 | } |
| 218 | |
| 219 | - (void)willResignActive { |
| 220 | [self teardownGL]; |
| 221 | } |
| 222 | |
| 223 | @end |