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