blob: e6e19b0f97453647583f548d993e892d7053b89a [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
tkchin@webrtc.org3a63a3c2015-01-06 07:21:34 +0000111 didChangeConnectionState:(RTCICEConnectionState)state {
112}
113
114- (void)appClient:(ARDAppClient *)client
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000115 didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000116 _localVideoTrack = localVideoTrack;
117 [_localVideoTrack addRenderer:self.localVideoView];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000118 self.localVideoView.hidden = NO;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000119}
120
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000121- (void)appClient:(ARDAppClient *)client
122 didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000123 _remoteVideoTrack = remoteVideoTrack;
124 [_remoteVideoTrack addRenderer:self.remoteVideoView];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000125}
126
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000127- (void)appClient:(ARDAppClient *)client
128 didError:(NSError *)error {
129 [self showAlertWithMessage:[NSString stringWithFormat:@"%@", error]];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000130 [self disconnect];
131}
132
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000133#pragma mark - RTCEAGLVideoViewDelegate
134
135- (void)videoView:(RTCEAGLVideoView*)videoView
136 didChangeVideoSize:(CGSize)size {
137 if (videoView == self.localVideoView) {
138 _localVideoSize = size;
139 } else if (videoView == self.remoteVideoView) {
140 _remoteVideoSize = size;
141 } else {
142 NSParameterAssert(NO);
143 }
144 [self updateVideoViewLayout];
145}
146
147#pragma mark - UITextFieldDelegate
148
149- (void)textFieldDidEndEditing:(UITextField*)textField {
150 NSString* room = textField.text;
151 if ([room length] == 0) {
152 return;
153 }
154 textField.hidden = YES;
155 self.instructionsView.hidden = YES;
156 self.logView.hidden = NO;
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000157 [_client disconnect];
158 // TODO(tkchin): support reusing the same client object.
159 _client = [[ARDAppClient alloc] initWithDelegate:self];
160 [_client connectToRoomWithId:room options:nil];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000161 [self setupCaptureSession];
162}
163
164- (BOOL)textFieldShouldReturn:(UITextField*)textField {
165 // There is no other control that can take focus, so manually resign focus
166 // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
167 [textField resignFirstResponder];
168 return YES;
169}
170
171#pragma mark - Private
172
173- (void)disconnect {
174 [self resetUI];
tkchin@webrtc.org87776a82014-12-09 19:32:35 +0000175 [_client disconnect];
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000176}
177
178- (void)showAlertWithMessage:(NSString*)message {
179 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil
180 message:message
181 delegate:nil
182 cancelButtonTitle:@"OK"
183 otherButtonTitles:nil];
184 [alertView show];
185}
186
187- (void)resetUI {
188 [self.roomInput resignFirstResponder];
189 self.roomInput.text = nil;
190 self.roomInput.hidden = NO;
191 self.instructionsView.hidden = NO;
192 self.logView.hidden = YES;
193 self.logView.text = nil;
tkchin@webrtc.org81257442014-11-04 23:06:15 +0000194 if (_localVideoTrack) {
195 [_localVideoTrack removeRenderer:self.localVideoView];
196 _localVideoTrack = nil;
197 [self.localVideoView renderFrame:nil];
198 }
199 if (_remoteVideoTrack) {
200 [_remoteVideoTrack removeRenderer:self.remoteVideoView];
201 _remoteVideoTrack = nil;
202 [self.remoteVideoView renderFrame:nil];
203 }
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000204 self.blackView.hidden = YES;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000205}
206
207- (void)setupCaptureSession {
208 self.blackView.hidden = NO;
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +0000209 [self updateVideoViewLayout];
210}
211
212- (void)updateVideoViewLayout {
213 // TODO(tkchin): handle rotation.
214 CGSize defaultAspectRatio = CGSizeMake(4, 3);
215 CGSize localAspectRatio = CGSizeEqualToSize(_localVideoSize, CGSizeZero) ?
216 defaultAspectRatio : _localVideoSize;
217 CGSize remoteAspectRatio = CGSizeEqualToSize(_remoteVideoSize, CGSizeZero) ?
218 defaultAspectRatio : _remoteVideoSize;
219
220 CGRect remoteVideoFrame =
221 AVMakeRectWithAspectRatioInsideRect(remoteAspectRatio,
222 self.blackView.bounds);
223 self.remoteVideoView.frame = remoteVideoFrame;
224
225 CGRect localVideoFrame =
226 AVMakeRectWithAspectRatioInsideRect(localAspectRatio,
227 self.blackView.bounds);
228 localVideoFrame.size.width = localVideoFrame.size.width / 3;
229 localVideoFrame.size.height = localVideoFrame.size.height / 3;
230 localVideoFrame.origin.x = CGRectGetMaxX(self.blackView.bounds)
231 - localVideoFrame.size.width - kLocalViewPadding;
232 localVideoFrame.origin.y = CGRectGetMaxY(self.blackView.bounds)
233 - localVideoFrame.size.height - kLocalViewPadding;
234 self.localVideoView.frame = localVideoFrame;
235}
236
237@end