blob: d8d9714962e2806102fd18599bcdf9ff5b86068c [file] [log] [blame]
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +00001/*
2 * libjingle
3 * Copyright 2013, 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 "APPRTCViewController.h"
33
34#import <AVFoundation/AVFoundation.h>
35#import "APPRTCConnectionManager.h"
36#import "RTCEAGLVideoView.h"
tkchin@webrtc.org81257442014-11-04 23:06:15 +000037#import "RTCVideoTrack.h"
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000038
39// Padding space for local video view with its parent.
40static CGFloat const kLocalViewPadding = 20;
41
42@interface APPRTCViewController ()
43<APPRTCConnectionManagerDelegate, APPRTCLogger, RTCEAGLVideoViewDelegate>
44@property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
45@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
46@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
47@end
48
49@implementation APPRTCViewController {
50 APPRTCConnectionManager* _connectionManager;
tkchin@webrtc.org81257442014-11-04 23:06:15 +000051 RTCVideoTrack* _localVideoTrack;
52 RTCVideoTrack* _remoteVideoTrack;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000053 CGSize _localVideoSize;
54 CGSize _remoteVideoSize;
55}
56
57- (instancetype)initWithNibName:(NSString*)nibName
58 bundle:(NSBundle*)bundle {
59 if (self = [super initWithNibName:nibName bundle:bundle]) {
60 _connectionManager =
61 [[APPRTCConnectionManager alloc] initWithDelegate:self
62 logger:self];
63 }
64 return self;
65}
66
67- (void)viewDidLoad {
68 [super viewDidLoad];
tkchin@webrtc.org90750482014-09-02 20:50:00 +000069
70 self.remoteVideoView =
71 [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds];
72 self.remoteVideoView.delegate = self;
73 self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1);
74 [self.blackView addSubview:self.remoteVideoView];
75
76 self.localVideoView =
77 [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds];
78 self.localVideoView.delegate = self;
79 [self.blackView addSubview:self.localVideoView];
80
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000081 self.statusBarOrientation =
82 [UIApplication sharedApplication].statusBarOrientation;
83 self.roomInput.delegate = self;
84 [self.roomInput becomeFirstResponder];
85}
86
87- (void)viewDidLayoutSubviews {
88 if (self.statusBarOrientation !=
89 [UIApplication sharedApplication].statusBarOrientation) {
90 self.statusBarOrientation =
91 [UIApplication sharedApplication].statusBarOrientation;
92 [[NSNotificationCenter defaultCenter]
93 postNotificationName:@"StatusBarOrientationDidChange"
94 object:nil];
95 }
96}
97
98- (void)applicationWillResignActive:(UIApplication*)application {
99 [self logMessage:@"Application lost focus, connection broken."];
100 [self disconnect];
101}
102
103#pragma mark - APPRTCConnectionManagerDelegate
104
105- (void)connectionManager:(APPRTCConnectionManager*)manager
106 didReceiveLocalVideoTrack:(RTCVideoTrack*)localVideoTrack {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000107 _localVideoTrack = localVideoTrack;
108 [_localVideoTrack addRenderer:self.localVideoView];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000109 self.localVideoView.hidden = NO;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000110}
111
112- (void)connectionManager:(APPRTCConnectionManager*)manager
113 didReceiveRemoteVideoTrack:(RTCVideoTrack*)remoteVideoTrack {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000114 _remoteVideoTrack = remoteVideoTrack;
115 [_remoteVideoTrack addRenderer:self.remoteVideoView];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000116}
117
118- (void)connectionManagerDidReceiveHangup:(APPRTCConnectionManager*)manager {
119 [self showAlertWithMessage:@"Remote hung up."];
120 [self disconnect];
121}
122
123- (void)connectionManager:(APPRTCConnectionManager*)manager
124 didErrorWithMessage:(NSString*)message {
125 [self showAlertWithMessage:message];
126 [self disconnect];
127}
128
129#pragma mark - APPRTCLogger
130
131- (void)logMessage:(NSString*)message {
132 dispatch_async(dispatch_get_main_queue(), ^{
133 NSString* output =
134 [NSString stringWithFormat:@"%@\n%@", self.logView.text, message];
135 self.logView.text = output;
136 [self.logView
137 scrollRangeToVisible:NSMakeRange([self.logView.text length], 0)];
138 });
139}
140
141#pragma mark - RTCEAGLVideoViewDelegate
142
143- (void)videoView:(RTCEAGLVideoView*)videoView
144 didChangeVideoSize:(CGSize)size {
145 if (videoView == self.localVideoView) {
146 _localVideoSize = size;
147 } else if (videoView == self.remoteVideoView) {
148 _remoteVideoSize = size;
149 } else {
150 NSParameterAssert(NO);
151 }
152 [self updateVideoViewLayout];
153}
154
155#pragma mark - UITextFieldDelegate
156
157- (void)textFieldDidEndEditing:(UITextField*)textField {
158 NSString* room = textField.text;
159 if ([room length] == 0) {
160 return;
161 }
162 textField.hidden = YES;
163 self.instructionsView.hidden = YES;
164 self.logView.hidden = NO;
165 NSString* url =
166 [NSString stringWithFormat:@"https://apprtc.appspot.com/?r=%@", room];
167 [_connectionManager connectToRoomWithURL:[NSURL URLWithString:url]];
168 [self setupCaptureSession];
169}
170
171- (BOOL)textFieldShouldReturn:(UITextField*)textField {
172 // There is no other control that can take focus, so manually resign focus
173 // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
174 [textField resignFirstResponder];
175 return YES;
176}
177
178#pragma mark - Private
179
180- (void)disconnect {
181 [self resetUI];
182 [_connectionManager disconnect];
183}
184
185- (void)showAlertWithMessage:(NSString*)message {
186 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
187 message:message
188 delegate:nil
189 cancelButtonTitle:@"OK"
190 otherButtonTitles:nil];
191 [alertView show];
192}
193
194- (void)resetUI {
195 [self.roomInput resignFirstResponder];
196 self.roomInput.text = nil;
197 self.roomInput.hidden = NO;
198 self.instructionsView.hidden = NO;
199 self.logView.hidden = YES;
200 self.logView.text = nil;
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000201 if (_localVideoTrack) {
202 [_localVideoTrack removeRenderer:self.localVideoView];
203 _localVideoTrack = nil;
204 [self.localVideoView renderFrame:nil];
205 }
206 if (_remoteVideoTrack) {
207 [_remoteVideoTrack removeRenderer:self.remoteVideoView];
208 _remoteVideoTrack = nil;
209 [self.remoteVideoView renderFrame:nil];
210 }
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000211 self.blackView.hidden = YES;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000212}
213
214- (void)setupCaptureSession {
215 self.blackView.hidden = NO;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000216 [self updateVideoViewLayout];
217}
218
219- (void)updateVideoViewLayout {
220 // TODO(tkchin): handle rotation.
221 CGSize defaultAspectRatio = CGSizeMake(4, 3);
222 CGSize localAspectRatio = CGSizeEqualToSize(_localVideoSize, CGSizeZero) ?
223 defaultAspectRatio : _localVideoSize;
224 CGSize remoteAspectRatio = CGSizeEqualToSize(_remoteVideoSize, CGSizeZero) ?
225 defaultAspectRatio : _remoteVideoSize;
226
227 CGRect remoteVideoFrame =
228 AVMakeRectWithAspectRatioInsideRect(remoteAspectRatio,
229 self.blackView.bounds);
230 self.remoteVideoView.frame = remoteVideoFrame;
231
232 CGRect localVideoFrame =
233 AVMakeRectWithAspectRatioInsideRect(localAspectRatio,
234 self.blackView.bounds);
235 localVideoFrame.size.width = localVideoFrame.size.width / 3;
236 localVideoFrame.size.height = localVideoFrame.size.height / 3;
237 localVideoFrame.origin.x = CGRectGetMaxX(self.blackView.bounds)
238 - localVideoFrame.size.width - kLocalViewPadding;
239 localVideoFrame.origin.y = CGRectGetMaxY(self.blackView.bounds)
240 - localVideoFrame.size.height - kLocalViewPadding;
241 self.localVideoView.frame = localVideoFrame;
242}
243
244@end