tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +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 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 | #error "This file requires ARC support." |
| 30 | #endif |
| 31 | |
| 32 | #import "APPRTCViewController.h" |
| 33 | |
| 34 | #import <AVFoundation/AVFoundation.h> |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 35 | #import "ARDAppClient.h" |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 36 | #import "RTCEAGLVideoView.h" |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 37 | #import "RTCVideoTrack.h" |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 38 | |
| 39 | // Padding space for local video view with its parent. |
| 40 | static CGFloat const kLocalViewPadding = 20; |
| 41 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 42 | @interface APPRTCViewController () <ARDAppClientDelegate, |
| 43 | RTCEAGLVideoViewDelegate> |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 44 | @property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation; |
| 45 | @property(nonatomic, strong) RTCEAGLVideoView* localVideoView; |
| 46 | @property(nonatomic, strong) RTCEAGLVideoView* remoteVideoView; |
| 47 | @end |
| 48 | |
| 49 | @implementation APPRTCViewController { |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 50 | ARDAppClient *_client; |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 51 | RTCVideoTrack* _localVideoTrack; |
| 52 | RTCVideoTrack* _remoteVideoTrack; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 53 | CGSize _localVideoSize; |
| 54 | CGSize _remoteVideoSize; |
| 55 | } |
| 56 | |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 57 | - (void)viewDidLoad { |
| 58 | [super viewDidLoad]; |
tkchin@webrtc.org | 9075048 | 2014-09-02 20:50:00 +0000 | [diff] [blame] | 59 | |
| 60 | self.remoteVideoView = |
| 61 | [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds]; |
| 62 | self.remoteVideoView.delegate = self; |
| 63 | self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1); |
| 64 | [self.blackView addSubview:self.remoteVideoView]; |
| 65 | |
| 66 | self.localVideoView = |
| 67 | [[RTCEAGLVideoView alloc] initWithFrame:self.blackView.bounds]; |
| 68 | self.localVideoView.delegate = self; |
| 69 | [self.blackView addSubview:self.localVideoView]; |
| 70 | |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 71 | self.statusBarOrientation = |
| 72 | [UIApplication sharedApplication].statusBarOrientation; |
| 73 | self.roomInput.delegate = self; |
| 74 | [self.roomInput becomeFirstResponder]; |
| 75 | } |
| 76 | |
| 77 | - (void)viewDidLayoutSubviews { |
| 78 | if (self.statusBarOrientation != |
| 79 | [UIApplication sharedApplication].statusBarOrientation) { |
| 80 | self.statusBarOrientation = |
| 81 | [UIApplication sharedApplication].statusBarOrientation; |
| 82 | [[NSNotificationCenter defaultCenter] |
| 83 | postNotificationName:@"StatusBarOrientationDidChange" |
| 84 | object:nil]; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | - (void)applicationWillResignActive:(UIApplication*)application { |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 89 | [self disconnect]; |
| 90 | } |
| 91 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 92 | #pragma mark - ARDAppClientDelegate |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 93 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 94 | - (void)appClient:(ARDAppClient *)client |
| 95 | didChangeState:(ARDAppClientState)state { |
| 96 | switch (state) { |
| 97 | case kARDAppClientStateConnected: |
| 98 | NSLog(@"Client connected."); |
| 99 | break; |
| 100 | case kARDAppClientStateConnecting: |
| 101 | NSLog(@"Client connecting."); |
| 102 | break; |
| 103 | case kARDAppClientStateDisconnected: |
| 104 | NSLog(@"Client disconnected."); |
| 105 | [self resetUI]; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | - (void)appClient:(ARDAppClient *)client |
| 111 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 112 | _localVideoTrack = localVideoTrack; |
| 113 | [_localVideoTrack addRenderer:self.localVideoView]; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 114 | self.localVideoView.hidden = NO; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 115 | } |
| 116 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 117 | - (void)appClient:(ARDAppClient *)client |
| 118 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 119 | _remoteVideoTrack = remoteVideoTrack; |
| 120 | [_remoteVideoTrack addRenderer:self.remoteVideoView]; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 123 | - (void)appClient:(ARDAppClient *)client |
| 124 | didError:(NSError *)error { |
| 125 | [self showAlertWithMessage:[NSString stringWithFormat:@"%@", error]]; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 126 | [self disconnect]; |
| 127 | } |
| 128 | |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 129 | #pragma mark - RTCEAGLVideoViewDelegate |
| 130 | |
| 131 | - (void)videoView:(RTCEAGLVideoView*)videoView |
| 132 | didChangeVideoSize:(CGSize)size { |
| 133 | if (videoView == self.localVideoView) { |
| 134 | _localVideoSize = size; |
| 135 | } else if (videoView == self.remoteVideoView) { |
| 136 | _remoteVideoSize = size; |
| 137 | } else { |
| 138 | NSParameterAssert(NO); |
| 139 | } |
| 140 | [self updateVideoViewLayout]; |
| 141 | } |
| 142 | |
| 143 | #pragma mark - UITextFieldDelegate |
| 144 | |
| 145 | - (void)textFieldDidEndEditing:(UITextField*)textField { |
| 146 | NSString* room = textField.text; |
| 147 | if ([room length] == 0) { |
| 148 | return; |
| 149 | } |
| 150 | textField.hidden = YES; |
| 151 | self.instructionsView.hidden = YES; |
| 152 | self.logView.hidden = NO; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 153 | [_client disconnect]; |
| 154 | // TODO(tkchin): support reusing the same client object. |
| 155 | _client = [[ARDAppClient alloc] initWithDelegate:self]; |
| 156 | [_client connectToRoomWithId:room options:nil]; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 157 | [self setupCaptureSession]; |
| 158 | } |
| 159 | |
| 160 | - (BOOL)textFieldShouldReturn:(UITextField*)textField { |
| 161 | // There is no other control that can take focus, so manually resign focus |
| 162 | // when return (Join) is pressed to trigger |textFieldDidEndEditing|. |
| 163 | [textField resignFirstResponder]; |
| 164 | return YES; |
| 165 | } |
| 166 | |
| 167 | #pragma mark - Private |
| 168 | |
| 169 | - (void)disconnect { |
| 170 | [self resetUI]; |
tkchin@webrtc.org | 87776a8 | 2014-12-09 19:32:35 +0000 | [diff] [blame] | 171 | [_client disconnect]; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | - (void)showAlertWithMessage:(NSString*)message { |
| 175 | UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil |
| 176 | message:message |
| 177 | delegate:nil |
| 178 | cancelButtonTitle:@"OK" |
| 179 | otherButtonTitles:nil]; |
| 180 | [alertView show]; |
| 181 | } |
| 182 | |
| 183 | - (void)resetUI { |
| 184 | [self.roomInput resignFirstResponder]; |
| 185 | self.roomInput.text = nil; |
| 186 | self.roomInput.hidden = NO; |
| 187 | self.instructionsView.hidden = NO; |
| 188 | self.logView.hidden = YES; |
| 189 | self.logView.text = nil; |
tkchin@webrtc.org | 8125744 | 2014-11-04 23:06:15 +0000 | [diff] [blame] | 190 | if (_localVideoTrack) { |
| 191 | [_localVideoTrack removeRenderer:self.localVideoView]; |
| 192 | _localVideoTrack = nil; |
| 193 | [self.localVideoView renderFrame:nil]; |
| 194 | } |
| 195 | if (_remoteVideoTrack) { |
| 196 | [_remoteVideoTrack removeRenderer:self.remoteVideoView]; |
| 197 | _remoteVideoTrack = nil; |
| 198 | [self.remoteVideoView renderFrame:nil]; |
| 199 | } |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 200 | self.blackView.hidden = YES; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | - (void)setupCaptureSession { |
| 204 | self.blackView.hidden = NO; |
tkchin@webrtc.org | acca675 | 2014-05-30 22:26:06 +0000 | [diff] [blame] | 205 | [self updateVideoViewLayout]; |
| 206 | } |
| 207 | |
| 208 | - (void)updateVideoViewLayout { |
| 209 | // TODO(tkchin): handle rotation. |
| 210 | CGSize defaultAspectRatio = CGSizeMake(4, 3); |
| 211 | CGSize localAspectRatio = CGSizeEqualToSize(_localVideoSize, CGSizeZero) ? |
| 212 | defaultAspectRatio : _localVideoSize; |
| 213 | CGSize remoteAspectRatio = CGSizeEqualToSize(_remoteVideoSize, CGSizeZero) ? |
| 214 | defaultAspectRatio : _remoteVideoSize; |
| 215 | |
| 216 | CGRect remoteVideoFrame = |
| 217 | AVMakeRectWithAspectRatioInsideRect(remoteAspectRatio, |
| 218 | self.blackView.bounds); |
| 219 | self.remoteVideoView.frame = remoteVideoFrame; |
| 220 | |
| 221 | CGRect localVideoFrame = |
| 222 | AVMakeRectWithAspectRatioInsideRect(localAspectRatio, |
| 223 | self.blackView.bounds); |
| 224 | localVideoFrame.size.width = localVideoFrame.size.width / 3; |
| 225 | localVideoFrame.size.height = localVideoFrame.size.height / 3; |
| 226 | localVideoFrame.origin.x = CGRectGetMaxX(self.blackView.bounds) |
| 227 | - localVideoFrame.size.width - kLocalViewPadding; |
| 228 | localVideoFrame.origin.y = CGRectGetMaxY(self.blackView.bounds) |
| 229 | - localVideoFrame.size.height - kLocalViewPadding; |
| 230 | self.localVideoView.frame = localVideoFrame; |
| 231 | } |
| 232 | |
| 233 | @end |