blob: 0ac92823f9371dcfdbe5cdb6198e9ca4667bb9c7 [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
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000030#import "APPRTCVideoView.h"
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000031
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032@interface APPRTCViewController ()
33
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000034@property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000035
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036@end
37
38@implementation APPRTCViewController
39
40- (void)viewDidLoad {
41 [super viewDidLoad];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000042 self.statusBarOrientation =
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000043 [UIApplication sharedApplication].statusBarOrientation;
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +000044 self.roomInput.delegate = self;
45 [self.roomInput becomeFirstResponder];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046}
47
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000048- (void)viewDidLayoutSubviews {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000049 if (self.statusBarOrientation !=
50 [UIApplication sharedApplication].statusBarOrientation) {
51 self.statusBarOrientation =
52 [UIApplication sharedApplication].statusBarOrientation;
53 [[NSNotificationCenter defaultCenter]
54 postNotificationName:@"StatusBarOrientationDidChange"
55 object:nil];
56 }
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000057}
58
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000059- (void)displayText:(NSString*)text {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 dispatch_async(dispatch_get_main_queue(), ^(void) {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000061 NSString* output =
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +000062 [NSString stringWithFormat:@"%@\n%@", self.logView.text, text];
63 self.logView.text = output;
64 [self.logView
65 scrollRangeToVisible:NSMakeRange([self.logView.text length], 0)];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 });
67}
68
69- (void)resetUI {
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +000070 [self.roomInput resignFirstResponder];
71 self.roomInput.text = nil;
72 self.roomInput.hidden = NO;
73 self.instructionsView.hidden = NO;
74 self.logView.hidden = YES;
75 self.logView.text = nil;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000076 self.blackView.hidden = YES;
77
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000078 [_remoteVideoView renderVideoTrackInterface:nil];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000079 [_remoteVideoView removeFromSuperview];
80 self.remoteVideoView = nil;
81
fischman@webrtc.org385a7222014-03-25 05:16:29 +000082 [_localVideoView renderVideoTrackInterface:nil];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000083 [_localVideoView removeFromSuperview];
84 self.localVideoView = nil;
85}
86
87// TODO(fischman): Use video dimensions from the incoming video stream
88// and resize the Video View accordingly w.r.t. aspect ratio.
89enum {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000090 // Remote video view dimensions.
91 kRemoteVideoWidth = 640,
92 kRemoteVideoHeight = 480,
93 // Padding space for local video view with its parent.
94 kLocalViewPadding = 20
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000095};
96
97- (void)setupCaptureSession {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000098 self.blackView.hidden = NO;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000099
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000100 CGRect frame =
101 CGRectMake((self.blackView.bounds.size.width - kRemoteVideoWidth) / 2,
102 (self.blackView.bounds.size.height - kRemoteVideoHeight) / 2,
103 kRemoteVideoWidth,
104 kRemoteVideoHeight);
105 APPRTCVideoView* videoView = [[APPRTCVideoView alloc] initWithFrame:frame];
106 videoView.isRemote = TRUE;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000107
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000108 [self.blackView addSubview:videoView];
109 videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
110 UIViewAutoresizingFlexibleRightMargin |
111 UIViewAutoresizingFlexibleBottomMargin |
112 UIViewAutoresizingFlexibleTopMargin;
113 videoView.translatesAutoresizingMaskIntoConstraints = YES;
114 _remoteVideoView = videoView;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000115
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000116 CGSize screenSize = [[UIScreen mainScreen] bounds].size;
117 CGFloat localVideoViewWidth =
118 UIInterfaceOrientationIsPortrait(self.statusBarOrientation)
119 ? screenSize.width / 4
120 : screenSize.height / 4;
121 CGFloat localVideoViewHeight =
122 UIInterfaceOrientationIsPortrait(self.statusBarOrientation)
123 ? screenSize.height / 4
124 : screenSize.width / 4;
125 frame = CGRectMake(self.blackView.bounds.size.width - localVideoViewWidth -
126 kLocalViewPadding,
127 kLocalViewPadding,
128 localVideoViewWidth,
129 localVideoViewHeight);
130 videoView = [[APPRTCVideoView alloc] initWithFrame:frame];
131 videoView.isRemote = FALSE;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000132
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000133 [self.blackView addSubview:videoView];
134 videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
135 UIViewAutoresizingFlexibleBottomMargin |
136 UIViewAutoresizingFlexibleHeight |
137 UIViewAutoresizingFlexibleWidth;
138 videoView.translatesAutoresizingMaskIntoConstraints = YES;
139 _localVideoView = videoView;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140}
141
142#pragma mark - UITextFieldDelegate
143
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000144- (void)textFieldDidEndEditing:(UITextField*)textField {
145 NSString* room = textField.text;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 if ([room length] == 0) {
147 return;
148 }
149 textField.hidden = YES;
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +0000150 self.instructionsView.hidden = YES;
151 self.logView.hidden = NO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 // TODO(hughv): Instead of launching a URL with apprtc scheme, change to
153 // prepopulating the textField with a valid URL missing the room. This allows
154 // the user to have the simplicity of just entering the room or the ability to
155 // override to a custom appspot instance. Remove apprtc:// when this is done.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000156 NSString* url =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room];
158 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000159
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000160 dispatch_async(dispatch_get_main_queue(), ^{ [self setupCaptureSession]; });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161}
162
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000163- (BOOL)textFieldShouldReturn:(UITextField*)textField {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 // There is no other control that can take focus, so manually resign focus
165 // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
166 [textField resignFirstResponder];
167 return YES;
168}
169
170@end