henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 30 | #import "VideoView.h" |
| 31 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | @interface APPRTCViewController () |
| 33 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 34 | @property (nonatomic, assign) UIInterfaceOrientation statusBarOrientation; |
| 35 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | @end |
| 37 | |
| 38 | @implementation APPRTCViewController |
| 39 | |
fischman@webrtc.org | 9ca93a8 | 2013-10-29 00:14:15 +0000 | [diff] [blame] | 40 | @synthesize textField = _textField; |
| 41 | @synthesize textInstructions = _textInstructions; |
| 42 | @synthesize textOutput = _textOutput; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 43 | @synthesize blackView = _blackView; |
| 44 | |
| 45 | @synthesize remoteVideoView = _remoteVideoView; |
| 46 | @synthesize localVideoView = _localVideoView; |
| 47 | |
| 48 | @synthesize statusBarOrientation = _statusBarOrientation; |
fischman@webrtc.org | 9ca93a8 | 2013-10-29 00:14:15 +0000 | [diff] [blame] | 49 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | - (void)viewDidLoad { |
| 51 | [super viewDidLoad]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 52 | self.statusBarOrientation = |
| 53 | [UIApplication sharedApplication].statusBarOrientation; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | self.textField.delegate = self; |
fischman@webrtc.org | c31d4d0 | 2013-09-05 21:49:58 +0000 | [diff] [blame] | 55 | [self.textField becomeFirstResponder]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | } |
| 57 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 58 | - (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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | - (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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 77 | [self.textField resignFirstResponder]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 83 | 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. |
| 96 | enum { |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | } |
| 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 165 | |
| 166 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 167 | [self setupCaptureSession]; |
| 168 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | } |
| 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 |