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 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame^] | 30 | #import <AVFoundation/AVFoundation.h> |
| 31 | #import "RTCEAGLVideoView.h" |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 32 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | @interface APPRTCViewController () |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 34 | @property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | @end |
| 36 | |
| 37 | @implementation APPRTCViewController |
| 38 | |
| 39 | - (void)viewDidLoad { |
| 40 | [super viewDidLoad]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 41 | self.statusBarOrientation = |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 42 | [UIApplication sharedApplication].statusBarOrientation; |
fischman@webrtc.org | 61e78fc | 2014-03-31 20:16:49 +0000 | [diff] [blame] | 43 | self.roomInput.delegate = self; |
| 44 | [self.roomInput becomeFirstResponder]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | } |
| 46 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 47 | - (void)viewDidLayoutSubviews { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 48 | if (self.statusBarOrientation != |
| 49 | [UIApplication sharedApplication].statusBarOrientation) { |
| 50 | self.statusBarOrientation = |
| 51 | [UIApplication sharedApplication].statusBarOrientation; |
| 52 | [[NSNotificationCenter defaultCenter] |
| 53 | postNotificationName:@"StatusBarOrientationDidChange" |
| 54 | object:nil]; |
| 55 | } |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 56 | } |
| 57 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 58 | - (void)displayText:(NSString*)text { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | dispatch_async(dispatch_get_main_queue(), ^(void) { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 60 | NSString* output = |
fischman@webrtc.org | 61e78fc | 2014-03-31 20:16:49 +0000 | [diff] [blame] | 61 | [NSString stringWithFormat:@"%@\n%@", self.logView.text, text]; |
| 62 | self.logView.text = output; |
| 63 | [self.logView |
| 64 | scrollRangeToVisible:NSMakeRange([self.logView.text length], 0)]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | }); |
| 66 | } |
| 67 | |
| 68 | - (void)resetUI { |
fischman@webrtc.org | 61e78fc | 2014-03-31 20:16:49 +0000 | [diff] [blame] | 69 | [self.roomInput resignFirstResponder]; |
| 70 | self.roomInput.text = nil; |
| 71 | self.roomInput.hidden = NO; |
| 72 | self.instructionsView.hidden = NO; |
| 73 | self.logView.hidden = YES; |
| 74 | self.logView.text = nil; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 75 | self.blackView.hidden = YES; |
| 76 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame^] | 77 | [self.remoteVideoView removeFromSuperview]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 78 | self.remoteVideoView = nil; |
| 79 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame^] | 80 | [self.localVideoView removeFromSuperview]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 81 | self.localVideoView = nil; |
| 82 | } |
| 83 | |
| 84 | // TODO(fischman): Use video dimensions from the incoming video stream |
| 85 | // and resize the Video View accordingly w.r.t. aspect ratio. |
| 86 | enum { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 87 | // Remote video view dimensions. |
| 88 | kRemoteVideoWidth = 640, |
| 89 | kRemoteVideoHeight = 480, |
| 90 | // Padding space for local video view with its parent. |
| 91 | kLocalViewPadding = 20 |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | - (void)setupCaptureSession { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 95 | self.blackView.hidden = NO; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 96 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame^] | 97 | CGSize videoSize = |
| 98 | CGSizeMake(kRemoteVideoWidth, kRemoteVideoHeight); |
| 99 | CGRect remoteVideoFrame = |
| 100 | AVMakeRectWithAspectRatioInsideRect(videoSize, |
| 101 | self.blackView.bounds); |
| 102 | CGRect localVideoFrame = remoteVideoFrame; |
| 103 | // TODO(tkchin): use video dimensions from incoming video stream |
| 104 | // and handle rotation. |
| 105 | localVideoFrame.size.width = remoteVideoFrame.size.height / 4; |
| 106 | localVideoFrame.size.height = remoteVideoFrame.size.width / 4; |
| 107 | localVideoFrame.origin.x = CGRectGetMaxX(remoteVideoFrame) |
| 108 | - localVideoFrame.size.width - kLocalViewPadding; |
| 109 | localVideoFrame.origin.y = CGRectGetMaxY(remoteVideoFrame) |
| 110 | - localVideoFrame.size.height - kLocalViewPadding; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 111 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame^] | 112 | self.remoteVideoView = |
| 113 | [[RTCEAGLVideoView alloc] initWithFrame:remoteVideoFrame]; |
| 114 | [self.blackView addSubview:self.remoteVideoView]; |
| 115 | self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1); |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 116 | |
tkchin@webrtc.org | 1732a59 | 2014-05-19 23:26:01 +0000 | [diff] [blame^] | 117 | self.localVideoView = |
| 118 | [[RTCEAGLVideoView alloc] initWithFrame:localVideoFrame]; |
| 119 | [self.blackView addSubview:self.localVideoView]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | #pragma mark - UITextFieldDelegate |
| 123 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 124 | - (void)textFieldDidEndEditing:(UITextField*)textField { |
| 125 | NSString* room = textField.text; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | if ([room length] == 0) { |
| 127 | return; |
| 128 | } |
| 129 | textField.hidden = YES; |
fischman@webrtc.org | 61e78fc | 2014-03-31 20:16:49 +0000 | [diff] [blame] | 130 | self.instructionsView.hidden = YES; |
| 131 | self.logView.hidden = NO; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | // TODO(hughv): Instead of launching a URL with apprtc scheme, change to |
| 133 | // prepopulating the textField with a valid URL missing the room. This allows |
| 134 | // the user to have the simplicity of just entering the room or the ability to |
| 135 | // 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] | 136 | NSString* url = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room]; |
| 138 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 139 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 140 | dispatch_async(dispatch_get_main_queue(), ^{ [self setupCaptureSession]; }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | } |
| 142 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 143 | - (BOOL)textFieldShouldReturn:(UITextField*)textField { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | // There is no other control that can take focus, so manually resign focus |
| 145 | // when return (Join) is pressed to trigger |textFieldDidEndEditing|. |
| 146 | [textField resignFirstResponder]; |
| 147 | return YES; |
| 148 | } |
| 149 | |
| 150 | @end |