blob: 3cebb7239cbc34e99b2c072c622e454dc52c9b2a [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 self.textField.delegate = self;
fischman@webrtc.orgc31d4d02013-09-05 21:49:58 +000045 [self.textField 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 =
62 [NSString stringWithFormat:@"%@\n%@", self.textOutput.text, text];
63 self.textOutput.text = output;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 });
65}
66
67- (void)resetUI {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000068 [self.textField resignFirstResponder];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 self.textField.text = nil;
70 self.textField.hidden = NO;
71 self.textInstructions.hidden = NO;
72 self.textOutput.hidden = YES;
73 self.textOutput.text = nil;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000074 self.blackView.hidden = YES;
75
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000076 [_remoteVideoView renderVideoTrackInterface:nil];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000077 [_remoteVideoView removeFromSuperview];
78 self.remoteVideoView = nil;
79
fischman@webrtc.org385a7222014-03-25 05:16:29 +000080 [_localVideoView renderVideoTrackInterface:nil];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000081 [_localVideoView removeFromSuperview];
82 self.localVideoView = nil;
83}
84
85// TODO(fischman): Use video dimensions from the incoming video stream
86// and resize the Video View accordingly w.r.t. aspect ratio.
87enum {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000088 // Remote video view dimensions.
89 kRemoteVideoWidth = 640,
90 kRemoteVideoHeight = 480,
91 // Padding space for local video view with its parent.
92 kLocalViewPadding = 20
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000093};
94
95- (void)setupCaptureSession {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000096 self.blackView.hidden = NO;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000097
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000098 CGRect frame =
99 CGRectMake((self.blackView.bounds.size.width - kRemoteVideoWidth) / 2,
100 (self.blackView.bounds.size.height - kRemoteVideoHeight) / 2,
101 kRemoteVideoWidth,
102 kRemoteVideoHeight);
103 APPRTCVideoView* videoView = [[APPRTCVideoView alloc] initWithFrame:frame];
104 videoView.isRemote = TRUE;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000105
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000106 [self.blackView addSubview:videoView];
107 videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
108 UIViewAutoresizingFlexibleRightMargin |
109 UIViewAutoresizingFlexibleBottomMargin |
110 UIViewAutoresizingFlexibleTopMargin;
111 videoView.translatesAutoresizingMaskIntoConstraints = YES;
112 _remoteVideoView = videoView;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000113
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000114 CGSize screenSize = [[UIScreen mainScreen] bounds].size;
115 CGFloat localVideoViewWidth =
116 UIInterfaceOrientationIsPortrait(self.statusBarOrientation)
117 ? screenSize.width / 4
118 : screenSize.height / 4;
119 CGFloat localVideoViewHeight =
120 UIInterfaceOrientationIsPortrait(self.statusBarOrientation)
121 ? screenSize.height / 4
122 : screenSize.width / 4;
123 frame = CGRectMake(self.blackView.bounds.size.width - localVideoViewWidth -
124 kLocalViewPadding,
125 kLocalViewPadding,
126 localVideoViewWidth,
127 localVideoViewHeight);
128 videoView = [[APPRTCVideoView alloc] initWithFrame:frame];
129 videoView.isRemote = FALSE;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000130
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000131 [self.blackView addSubview:videoView];
132 videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
133 UIViewAutoresizingFlexibleBottomMargin |
134 UIViewAutoresizingFlexibleHeight |
135 UIViewAutoresizingFlexibleWidth;
136 videoView.translatesAutoresizingMaskIntoConstraints = YES;
137 _localVideoView = videoView;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138}
139
140#pragma mark - UITextFieldDelegate
141
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000142- (void)textFieldDidEndEditing:(UITextField*)textField {
143 NSString* room = textField.text;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 if ([room length] == 0) {
145 return;
146 }
147 textField.hidden = YES;
148 self.textInstructions.hidden = YES;
149 self.textOutput.hidden = NO;
150 // TODO(hughv): Instead of launching a URL with apprtc scheme, change to
151 // prepopulating the textField with a valid URL missing the room. This allows
152 // the user to have the simplicity of just entering the room or the ability to
153 // override to a custom appspot instance. Remove apprtc:// when this is done.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000154 NSString* url =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room];
156 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000157
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000158 dispatch_async(dispatch_get_main_queue(), ^{ [self setupCaptureSession]; });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159}
160
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000161- (BOOL)textFieldShouldReturn:(UITextField*)textField {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 // There is no other control that can take focus, so manually resign focus
163 // when return (Join) is pressed to trigger |textFieldDidEndEditing|.
164 [textField resignFirstResponder];
165 return YES;
166}
167
168@end