blob: bdd8b50369e0da776a9a47fd423d058ee02b8cd4 [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
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000030#import <AVFoundation/AVFoundation.h>
31#import "RTCEAGLVideoView.h"
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000032
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033@interface APPRTCViewController ()
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000034@property(nonatomic, assign) UIInterfaceOrientation statusBarOrientation;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035@end
36
37@implementation APPRTCViewController
38
39- (void)viewDidLoad {
40 [super viewDidLoad];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000041 self.statusBarOrientation =
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000042 [UIApplication sharedApplication].statusBarOrientation;
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +000043 self.roomInput.delegate = self;
44 [self.roomInput becomeFirstResponder];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045}
46
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000047- (void)viewDidLayoutSubviews {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000048 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.orgd3d6bce2014-03-10 20:41:22 +000056}
57
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000058- (void)displayText:(NSString*)text {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 dispatch_async(dispatch_get_main_queue(), ^(void) {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000060 NSString* output =
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +000061 [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.org28e20752013-07-10 00:45:36 +000065 });
66}
67
68- (void)resetUI {
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +000069 [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.orgd3d6bce2014-03-10 20:41:22 +000075 self.blackView.hidden = YES;
76
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000077 [self.remoteVideoView removeFromSuperview];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000078 self.remoteVideoView = nil;
79
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000080 [self.localVideoView removeFromSuperview];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000081 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.
86enum {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000087 // 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.orgd3d6bce2014-03-10 20:41:22 +000092};
93
94- (void)setupCaptureSession {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000095 self.blackView.hidden = NO;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000096
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000097 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.orgd3d6bce2014-03-10 20:41:22 +0000111
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000112 self.remoteVideoView =
113 [[RTCEAGLVideoView alloc] initWithFrame:remoteVideoFrame];
114 [self.blackView addSubview:self.remoteVideoView];
115 self.remoteVideoView.transform = CGAffineTransformMakeScale(-1, 1);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000116
tkchin@webrtc.org1732a592014-05-19 23:26:01 +0000117 self.localVideoView =
118 [[RTCEAGLVideoView alloc] initWithFrame:localVideoFrame];
119 [self.blackView addSubview:self.localVideoView];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120}
121
122#pragma mark - UITextFieldDelegate
123
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000124- (void)textFieldDidEndEditing:(UITextField*)textField {
125 NSString* room = textField.text;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 if ([room length] == 0) {
127 return;
128 }
129 textField.hidden = YES;
fischman@webrtc.org61e78fc2014-03-31 20:16:49 +0000130 self.instructionsView.hidden = YES;
131 self.logView.hidden = NO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // 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.org7fa1fcb2014-03-25 00:11:56 +0000136 NSString* url =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 [NSString stringWithFormat:@"apprtc://apprtc.appspot.com/?r=%@", room];
138 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000139
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000140 dispatch_async(dispatch_get_main_queue(), ^{ [self setupCaptureSession]; });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141}
142
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000143- (BOOL)textFieldShouldReturn:(UITextField*)textField {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 // 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