blob: b9703254ee28ca29cb12c4e9d0629c711eedd7b1 [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
32#import "RTCEAGLVideoView+Internal.h"
33
34#import <GLKit/GLKit.h>
35#import <QuartzCore/QuartzCore.h>
36
37#import "RTCEAGLVideoRenderer.h"
38#import "RTCVideoRenderer.h"
39#import "RTCVideoTrack.h"
40
41@interface RTCEAGLVideoView () <GLKViewDelegate>
42@property(atomic, strong) RTCI420Frame* i420Frame;
43@end
44
45@implementation RTCEAGLVideoView {
46 CADisplayLink* _displayLink;
47 GLKView* _glkView;
48 RTCEAGLVideoRenderer* _glRenderer;
49 RTCVideoRenderer* _videoRenderer;
50}
51
52- (instancetype)initWithFrame:(CGRect)frame {
53 if (self = [super initWithFrame:frame]) {
54 EAGLContext* glContext =
55 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
56 _glRenderer = [[RTCEAGLVideoRenderer alloc] initWithContext:glContext];
57
58 // GLKView manages a framebuffer for us.
59 _glkView = [[GLKView alloc] initWithFrame:CGRectZero
60 context:glContext];
61 _glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
62 _glkView.drawableDepthFormat = GLKViewDrawableDepthFormatNone;
63 _glkView.drawableStencilFormat = GLKViewDrawableStencilFormatNone;
64 _glkView.drawableMultisample = GLKViewDrawableMultisampleNone;
65 _glkView.delegate = self;
66 _glkView.layer.masksToBounds = YES;
67 [self addSubview:_glkView];
68
69 // Listen to application state in order to clean up OpenGL before app goes
70 // away.
71 NSNotificationCenter* notificationCenter =
72 [NSNotificationCenter defaultCenter];
73 [notificationCenter addObserver:self
74 selector:@selector(willResignActive)
75 name:UIApplicationWillResignActiveNotification
76 object:nil];
77 [notificationCenter addObserver:self
78 selector:@selector(didBecomeActive)
79 name:UIApplicationDidBecomeActiveNotification
80 object:nil];
81 _displayLink =
82 [CADisplayLink displayLinkWithTarget:self
83 selector:@selector(displayLinkDidFire:)];
84 _displayLink.paused = YES;
85 // Set to half of screen refresh, which should be 30fps.
86 [_displayLink setFrameInterval:2];
87 [_displayLink addToRunLoop:[NSRunLoop currentRunLoop]
88 forMode:NSRunLoopCommonModes];
89 _videoRenderer = [[RTCVideoRenderer alloc] initWithDelegate:self];
90 [self setupGL];
91 }
92 return self;
93}
94
95- (void)dealloc {
96 [[NSNotificationCenter defaultCenter] removeObserver:self];
97 UIApplicationState appState =
98 [UIApplication sharedApplication].applicationState;
99 if (appState == UIApplicationStateActive) {
100 [self teardownGL];
101 }
102}
103
104- (void)setVideoTrack:(RTCVideoTrack*)videoTrack {
105 if (_videoTrack == videoTrack) {
106 return;
107 }
108 [_videoTrack removeRenderer:_videoRenderer];
109 _videoTrack = videoTrack;
110 [_videoTrack addRenderer:_videoRenderer];
111 // TODO(tkchin): potentially handle changes in track state - e.g. render
112 // black if track fails.
113}
114
115#pragma mark - UIView
116
117- (void)layoutSubviews {
118 [super layoutSubviews];
119 _glkView.frame = self.bounds;
120}
121
122#pragma mark - GLKViewDelegate
123
124// This method is called when the GLKView's content is dirty and needs to be
125// redrawn. This occurs on main thread.
126- (void)glkView:(GLKView*)view drawInRect:(CGRect)rect {
127 if (self.i420Frame) {
128 // The renderer will draw the frame to the framebuffer corresponding to the
129 // one used by |view|.
130 [_glRenderer drawFrame:self.i420Frame];
131 }
132}
133
134#pragma mark - Private
135
136// Frames are received on a separate thread, so we poll for current frame
137// using a refresh rate proportional to screen refresh frequency. This occurs
138// on main thread.
139- (void)displayLinkDidFire:(CADisplayLink*)displayLink {
140 // Don't render if frame hasn't changed.
141 if (_glRenderer.lastDrawnFrame == self.i420Frame) {
142 return;
143 }
144 // This tells the GLKView that it's dirty, which will then call the the
145 // GLKViewDelegate method implemented above.
146 [_glkView setNeedsDisplay];
147}
148
149- (void)setupGL {
150 [_glRenderer setupGL];
151 _displayLink.paused = NO;
152}
153
154- (void)teardownGL {
155 _displayLink.paused = YES;
156 [_glkView deleteDrawable];
157 [_glRenderer teardownGL];
158}
159
160- (void)didBecomeActive {
161 [self setupGL];
162}
163
164- (void)willResignActive {
165 [self teardownGL];
166}
167
168@end
169
170@implementation RTCEAGLVideoView (Internal)
171
172#pragma mark - RTCVideoRendererDelegate
173
174// These methods are called when the video track has frame information to
175// provide. This occurs on non-main thread.
176- (void)renderer:(RTCVideoRenderer*)renderer
177 didSetSize:(CGSize)size {
178 // Size is checked in renderer as frames arrive, no need to do anything here.
179}
180
181- (void)renderer:(RTCVideoRenderer*)renderer
182 didReceiveFrame:(RTCI420Frame*)frame {
183 self.i420Frame = frame;
184}
185
186@end