blob: cacac14a4d9bce2677f697c9fa8cb2864edf7978 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +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#import "APPRTCViewController.h"
29
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000030#import "VideoView.h"
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032@interface APPRTCViewController ()
33
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000034@property (nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
35
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036@end
37
38@implementation APPRTCViewController
39
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000040@synthesize textField = _textField;
41@synthesize textInstructions = _textInstructions;
42@synthesize textOutput = _textOutput;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000043@synthesize blackView = _blackView;
44
45@synthesize remoteVideoView = _remoteVideoView;
46@synthesize localVideoView = _localVideoView;
47
48@synthesize statusBarOrientation = _statusBarOrientation;
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000049
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050- (void)viewDidLoad {
51 [super viewDidLoad];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000052 self.statusBarOrientation =
53 [UIApplication sharedApplication].statusBarOrientation;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 self.textField.delegate = self;
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +000055 [self.textField becomeFirstResponder];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056}
57
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000058- (void)viewDidLayoutSubviews {
59 if (self.statusBarOrientation !=
60 [UIApplication sharedApplication].statusBarOrientation) {
61 self.statusBarOrientation =
62 [UIApplication sharedApplication].statusBarOrientation;
63 [[NSNotificationCenter defaultCenter]
64 postNotificationName:@"StatusBarOrientationDidChange" object:nil];
65 }
66}
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068- (void)displayText:(NSString *)text {
69 dispatch_async(dispatch_get_main_queue(), ^(void) {
70 NSString *output =
71 [NSString stringWithFormat:@"%@\n%@", self.textOutput.text, text];
72 self.textOutput.text = output;
73 });
74}
75
76- (void)resetUI {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000077 [self.textField resignFirstResponder];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 self.textField.text = nil;
79 self.textField.hidden = NO;
80 self.textInstructions.hidden = NO;
81 self.textOutput.hidden = YES;
82 self.textOutput.text = nil;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000083 self.blackView.hidden = YES;
84
85 [_remoteVideoView stop];
86 [_remoteVideoView removeFromSuperview];
87 self.remoteVideoView = nil;
88
89 [_localVideoView stop];
90 [_localVideoView removeFromSuperview];
91 self.localVideoView = nil;
92}
93
94// TODO(fischman): Use video dimensions from the incoming video stream
95// and resize the Video View accordingly w.r.t. aspect ratio.
96enum {
97 // Remote video view dimensions.
98 kRemoteVideoWidth = 640,
99 kRemoteVideoHeight = 480,
100 // Padding space for local video view with its parent.
101 kLocalViewPadding = 20
102};
103
104- (void)setupCaptureSession {
105 self.blackView.hidden = NO;
106
107 CGRect frame = CGRectMake((self.blackView.bounds.size.width
108 -kRemoteVideoWidth)/2,
109 (self.blackView.bounds.size.height
110 -kRemoteVideoHeight)/2,
111 kRemoteVideoWidth,
112 kRemoteVideoHeight);
113 VideoView *videoView = [[VideoView alloc] initWithFrame:frame];
114 videoView.isRemote = TRUE;
115
116 [self.blackView addSubview:videoView];
117 videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
118 UIViewAutoresizingFlexibleRightMargin |
119 UIViewAutoresizingFlexibleBottomMargin |
120 UIViewAutoresizingFlexibleTopMargin;
121 videoView.translatesAutoresizingMaskIntoConstraints = YES;
122 _remoteVideoView = videoView;
123
124 CGSize screenSize = [[UIScreen mainScreen] bounds].size;
125 CGFloat localVideoViewWidth =
126 UIInterfaceOrientationIsPortrait(self.statusBarOrientation) ?
127 screenSize.width/4 : screenSize.height/4;
128 CGFloat localVideoViewHeight =
129 UIInterfaceOrientationIsPortrait(self.statusBarOrientation) ?
130 screenSize.height/4 : screenSize.width/4;
131 frame = CGRectMake(self.blackView.bounds.size.width
132 -localVideoViewWidth-kLocalViewPadding,
133 kLocalViewPadding,
134 localVideoViewWidth,
135 localVideoViewHeight);
136 videoView = [[VideoView alloc] initWithFrame:frame];
137 videoView.isRemote = FALSE;
138
139 [self.blackView addSubview:videoView];
140 videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
141 UIViewAutoresizingFlexibleBottomMargin |
142 UIViewAutoresizingFlexibleHeight |
143 UIViewAutoresizingFlexibleWidth;
144 videoView.translatesAutoresizingMaskIntoConstraints = YES;
145 _localVideoView = videoView;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146}
147
148#pragma mark - UITextFieldDelegate
149
150- (void)textFieldDidEndEditing:(UITextField *)textField {
151 NSString *room = textField.text;
152 if ([room length] == 0) {
153 return;
154 }
155 textField.hidden = YES;
156 self.textInstructions.hidden = YES;
157 self.textOutput.hidden = NO;
158 // TODO(hughv): Instead of launching a URL with apprtc scheme, change to
159 // prepopulating the textField with a valid URL missing the room. This allows
160 // the user to have the simplicity of just entering the room or the ability to
161 // override to a custom appspot instance. Remove apprtc:// when this is done.
162 NSString *url =
163 [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room];
164 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000165
166 dispatch_async(dispatch_get_main_queue(), ^{
167 [self setupCaptureSession];
168 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169}
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@end