blob: 3d60ee77f7d89ed1db6ff272bbbd2840924086dc [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>
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000035#import "ARDAppClient.h"
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000036#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
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000042@interface APPRTCViewController () <ARDAppClientDelegate,
43 RTCEAGLVideoViewDelegate>
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000044@property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
45@property(nonatomic, strong) RTCEAGLVideoView* localVideoView;
46@property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView;
47@end
48
49@implementation APPRTCViewController {
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000050 ARDAppClient *_client;
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
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000057- (void)viewDidLoad {
58 [super viewDidLoad];
tkchin@webrtc.org90750482014-09-02 20:50:00 +000059
60 self.remoteVideoView =
61 [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds];
62 self.remoteVideoView.delegate = self;
63 self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1);
64 [self.blackView addSubview:self.remoteVideoView];
65
66 self.localVideoView =
67 [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds];
68 self.localVideoView.delegate = self;
69 [self.blackView addSubview:self.localVideoView];
70
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000071 self.statusBarOrientation =
72 [UIApplication sharedApplication].statusBarOrientation;
73 self.roomInput.delegate = self;
74 [self.roomInput becomeFirstResponder];
75}
76
77- (void)viewDidLayoutSubviews {
78 if (self.statusBarOrientation !=
79 [UIApplication sharedApplication].statusBarOrientation) {
80 self.statusBarOrientation =
81 [UIApplication sharedApplication].statusBarOrientation;
82 [[NSNotificationCenter defaultCenter]
83 postNotificationName:@"StatusBarOrientationDidChange"
84 object:nil];
85 }
86}
87
88- (void)applicationWillResignActive:(UIApplication*)application {
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000089 [self disconnect];
90}
91
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000092#pragma mark - ARDAppClientDelegate
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000093
tkchin@webrtc.org87776a82014-12-09 19:32:35 +000094- (void)appClient:(ARDAppClient *)client
95 didChangeState:(ARDAppClientState)state {
96 switch (state) {
97 case kARDAppClientStateConnected:
98 NSLog(@"Client connected.");
99 break;
100 case kARDAppClientStateConnecting:
101 NSLog(@"Client connecting.");
102 break;
103 case kARDAppClientStateDisconnected:
104 NSLog(@"Client disconnected.");
105 [self resetUI];
106 break;
107 }
108}
109
110- (void)appClient:(ARDAppClient *)client
111 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000112 _localVideoTrack = localVideoTrack;
113 [_localVideoTrack addRenderer:self.localVideoView];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000114 self.localVideoView.hidden = NO;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000115}
116
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000117- (void)appClient:(ARDAppClient *)client
118 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000119 _remoteVideoTrack = remoteVideoTrack;
120 [_remoteVideoTrack addRenderer:self.remoteVideoView];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000121}
122
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000123- (void)appClient:(ARDAppClient *)client
124 didError:(NSError *)error {
125 [self showAlertWithMessage:[NSString stringWithFormat:@"%@", error]];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000126 [self disconnect];
127}
128
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000129#pragma mark - RTCEAGLVideoViewDelegate
130
131- (void)videoView:(RTCEAGLVideoView*)videoView
132 didChangeVideoSize:(CGSize)size {
133 if (videoView == self.localVideoView) {
134 _localVideoSize = size;
135 } else if (videoView == self.remoteVideoView) {
136 _remoteVideoSize = size;
137 } else {
138 NSParameterAssert(NO);
139 }
140 [self updateVideoViewLayout];
141}
142
143#pragma mark - UITextFieldDelegate
144
145- (void)textFieldDidEndEditing:(UITextField*)textField {
146 NSString* room = textField.text;
147 if ([room length] == 0) {
148 return;
149 }
150 textField.hidden = YES;
151 self.instructionsView.hidden = YES;
152 self.logView.hidden = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000153 [_client disconnect];
154 // TODO(tkchin): support reusing the same client object.
155 _client = [[ARDAppClient alloc] initWithDelegate:self];
156 [_client connectToRoomWithId:room options:nil];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000157 [self setupCaptureSession];
158}
159
160- (BOOL)textFieldShouldReturn:(UITextField*)textField {
161 // There is no other control that can take focus, so manually resign focus
162 // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
163 [textField resignFirstResponder];
164 return YES;
165}
166
167#pragma mark - Private
168
169- (void)disconnect {
170 [self resetUI];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000171 [_client disconnect];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000172}
173
174- (void)showAlertWithMessage:(NSString*)message {
175 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
176 message:message
177 delegate:nil
178 cancelButtonTitle:@"OK"
179 otherButtonTitles:nil];
180 [alertView show];
181}
182
183- (void)resetUI {
184 [self.roomInput resignFirstResponder];
185 self.roomInput.text = nil;
186 self.roomInput.hidden = NO;
187 self.instructionsView.hidden = NO;
188 self.logView.hidden = YES;
189 self.logView.text = nil;
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000190 if (_localVideoTrack) {
191 [_localVideoTrack removeRenderer:self.localVideoView];
192 _localVideoTrack = nil;
193 [self.localVideoView renderFrame:nil];
194 }
195 if (_remoteVideoTrack) {
196 [_remoteVideoTrack removeRenderer:self.remoteVideoView];
197 _remoteVideoTrack = nil;
198 [self.remoteVideoView renderFrame:nil];
199 }
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000200 self.blackView.hidden = YES;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000201}
202
203- (void)setupCaptureSession {
204 self.blackView.hidden = NO;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000205 [self updateVideoViewLayout];
206}
207
208- (void)updateVideoViewLayout {
209 // TODO(tkchin): handle rotation.
210 CGSize defaultAspectRatio = CGSizeMake(4, 3);
211 CGSize localAspectRatio = CGSizeEqualToSize(_localVideoSize, CGSizeZero) ?
212 defaultAspectRatio : _localVideoSize;
213 CGSize remoteAspectRatio = CGSizeEqualToSize(_remoteVideoSize, CGSizeZero) ?
214 defaultAspectRatio : _remoteVideoSize;
215
216 CGRect remoteVideoFrame =
217 AVMakeRectWithAspectRatioInsideRect(remoteAspectRatio,
218 self.blackView.bounds);
219 self.remoteVideoView.frame = remoteVideoFrame;
220
221 CGRect localVideoFrame =
222 AVMakeRectWithAspectRatioInsideRect(localAspectRatio,
223 self.blackView.bounds);
224 localVideoFrame.size.width = localVideoFrame.size.width / 3;
225 localVideoFrame.size.height = localVideoFrame.size.height / 3;
226 localVideoFrame.origin.x = CGRectGetMaxX(self.blackView.bounds)
227 - localVideoFrame.size.width - kLocalViewPadding;
228 localVideoFrame.origin.y = CGRectGetMaxY(self.blackView.bounds)
229 - localVideoFrame.size.height - kLocalViewPadding;
230 self.localVideoView.frame = localVideoFrame;
231}
232
233@end