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 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 30 | #import "APPRTCVideoView.h" |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 31 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | @interface APPRTCViewController () |
| 33 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 34 | @property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 35 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | @end |
| 37 | |
| 38 | @implementation APPRTCViewController |
| 39 | |
| 40 | - (void)viewDidLoad { |
| 41 | [super viewDidLoad]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 42 | self.statusBarOrientation = |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 43 | [UIApplication sharedApplication].statusBarOrientation; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | self.textField.delegate = self; |
fischman@webrtc.org | c31d4d0 | 2013-09-05 21:49:58 +0000 | [diff] [blame] | 45 | [self.textField becomeFirstResponder]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | } |
| 47 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 48 | - (void)viewDidLayoutSubviews { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 49 | 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 57 | } |
| 58 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 59 | - (void)displayText:(NSString*)text { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | dispatch_async(dispatch_get_main_queue(), ^(void) { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 61 | NSString* output = |
| 62 | [NSString stringWithFormat:@"%@\n%@", self.textOutput.text, text]; |
| 63 | self.textOutput.text = output; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | }); |
| 65 | } |
| 66 | |
| 67 | - (void)resetUI { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 68 | [self.textField resignFirstResponder]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 74 | self.blackView.hidden = YES; |
| 75 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 76 | [_remoteVideoView renderVideoTrackInterface:nil]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 77 | [_remoteVideoView removeFromSuperview]; |
| 78 | self.remoteVideoView = nil; |
| 79 | |
fischman@webrtc.org | 385a722 | 2014-03-25 05:16:29 +0000 | [diff] [blame] | 80 | [_localVideoView renderVideoTrackInterface:nil]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 81 | [_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. |
| 87 | enum { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 88 | // 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | - (void)setupCaptureSession { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 96 | self.blackView.hidden = NO; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 97 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 98 | 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 105 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 106 | [self.blackView addSubview:videoView]; |
| 107 | videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | |
| 108 | UIViewAutoresizingFlexibleRightMargin | |
| 109 | UIViewAutoresizingFlexibleBottomMargin | |
| 110 | UIViewAutoresizingFlexibleTopMargin; |
| 111 | videoView.translatesAutoresizingMaskIntoConstraints = YES; |
| 112 | _remoteVideoView = videoView; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 113 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 114 | 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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 130 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 131 | [self.blackView addSubview:videoView]; |
| 132 | videoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | |
| 133 | UIViewAutoresizingFlexibleBottomMargin | |
| 134 | UIViewAutoresizingFlexibleHeight | |
| 135 | UIViewAutoresizingFlexibleWidth; |
| 136 | videoView.translatesAutoresizingMaskIntoConstraints = YES; |
| 137 | _localVideoView = videoView; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #pragma mark - UITextFieldDelegate |
| 141 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 142 | - (void)textFieldDidEndEditing:(UITextField*)textField { |
| 143 | NSString* room = textField.text; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | 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.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 154 | NSString* url = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room]; |
| 156 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 157 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 158 | dispatch_async(dispatch_get_main_queue(), ^{ [self setupCaptureSession]; }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 159 | } |
| 160 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 161 | - (BOOL)textFieldShouldReturn:(UITextField*)textField { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | // 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 |