blob: 4048b84bb2bf3a874fa941109bfab231df0a1b86 [file] [log] [blame]
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +00009 */
10
11#import "ARDVideoCallView.h"
12
13#import <AVFoundation/AVFoundation.h>
14#import "UIImage+ARDUtilities.h"
15
Zeke Chin57cc74e2015-05-05 07:52:31 -070016static CGFloat const kButtonPadding = 16;
17static CGFloat const kButtonSize = 48;
18static CGFloat const kLocalVideoViewSize = 120;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000019static CGFloat const kLocalVideoViewPadding = 8;
Zeke Chind3325802015-08-14 11:00:02 -070020static CGFloat const kStatusBarHeight = 20;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000021
22@interface ARDVideoCallView () <RTCEAGLVideoViewDelegate>
23@end
24
25@implementation ARDVideoCallView {
Zeke Chin57cc74e2015-05-05 07:52:31 -070026 UIButton *_cameraSwitchButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000027 UIButton *_hangupButton;
28 CGSize _localVideoSize;
29 CGSize _remoteVideoSize;
Zeke Chin57cc74e2015-05-05 07:52:31 -070030 BOOL _useRearCamera;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000031}
32
33@synthesize statusLabel = _statusLabel;
34@synthesize localVideoView = _localVideoView;
35@synthesize remoteVideoView = _remoteVideoView;
Zeke Chind3325802015-08-14 11:00:02 -070036@synthesize statsView = _statsView;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000037@synthesize delegate = _delegate;
38
39- (instancetype)initWithFrame:(CGRect)frame {
40 if (self = [super initWithFrame:frame]) {
41 _remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
42 _remoteVideoView.delegate = self;
43 [self addSubview:_remoteVideoView];
44
Zeke Chin57cc74e2015-05-05 07:52:31 -070045 // TODO(tkchin): replace this with a view that renders layer from
46 // AVCaptureSession.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000047 _localVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000048 _localVideoView.delegate = self;
49 [self addSubview:_localVideoView];
50
Zeke Chind3325802015-08-14 11:00:02 -070051 _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero];
52 _statsView.hidden = YES;
53 [self addSubview:_statsView];
54
Zeke Chin57cc74e2015-05-05 07:52:31 -070055 // TODO(tkchin): don't display this if we can't actually do camera switch.
56 _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
57 _cameraSwitchButton.backgroundColor = [UIColor whiteColor];
58 _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2;
59 _cameraSwitchButton.layer.masksToBounds = YES;
60 UIImage *image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"];
61 [_cameraSwitchButton setImage:image forState:UIControlStateNormal];
62 [_cameraSwitchButton addTarget:self
63 action:@selector(onCameraSwitch:)
64 forControlEvents:UIControlEventTouchUpInside];
65 [self addSubview:_cameraSwitchButton];
66
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000067 _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom];
68 _hangupButton.backgroundColor = [UIColor redColor];
Zeke Chin57cc74e2015-05-05 07:52:31 -070069 _hangupButton.layer.cornerRadius = kButtonSize / 2;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000070 _hangupButton.layer.masksToBounds = YES;
Zeke Chin57cc74e2015-05-05 07:52:31 -070071 image = [UIImage imageForName:@"ic_call_end_black_24dp.png"
72 color:[UIColor whiteColor]];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000073 [_hangupButton setImage:image forState:UIControlStateNormal];
74 [_hangupButton addTarget:self
75 action:@selector(onHangup:)
76 forControlEvents:UIControlEventTouchUpInside];
77 [self addSubview:_hangupButton];
78
79 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero];
80 _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16];
81 _statusLabel.textColor = [UIColor whiteColor];
82 [self addSubview:_statusLabel];
Zeke Chind3325802015-08-14 11:00:02 -070083
84 UITapGestureRecognizer *tapRecognizer =
85 [[UITapGestureRecognizer alloc]
86 initWithTarget:self
87 action:@selector(didTripleTap:)];
88 tapRecognizer.numberOfTapsRequired = 3;
89 [self addGestureRecognizer:tapRecognizer];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000090 }
91 return self;
92}
93
94- (void)layoutSubviews {
95 CGRect bounds = self.bounds;
96 if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
97 // Aspect fill remote video into bounds.
98 CGRect remoteVideoFrame =
99 AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
100 CGFloat scale = 1;
101 if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
102 // Scale by height.
103 scale = bounds.size.height / remoteVideoFrame.size.height;
104 } else {
105 // Scale by width.
106 scale = bounds.size.width / remoteVideoFrame.size.width;
107 }
108 remoteVideoFrame.size.height *= scale;
109 remoteVideoFrame.size.width *= scale;
110 _remoteVideoView.frame = remoteVideoFrame;
111 _remoteVideoView.center =
112 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
113 } else {
114 _remoteVideoView.frame = bounds;
115 }
116
Zeke Chin57cc74e2015-05-05 07:52:31 -0700117 if (_localVideoSize.width && _localVideoSize.height > 0) {
118 // Aspect fit local video view into a square box.
119 CGRect localVideoFrame =
120 CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize);
121 localVideoFrame =
122 AVMakeRectWithAspectRatioInsideRect(_localVideoSize, localVideoFrame);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000123
Zeke Chin57cc74e2015-05-05 07:52:31 -0700124 // Place the view in the bottom right.
125 localVideoFrame.origin.x = CGRectGetMaxX(bounds)
126 - localVideoFrame.size.width - kLocalVideoViewPadding;
127 localVideoFrame.origin.y = CGRectGetMaxY(bounds)
128 - localVideoFrame.size.height - kLocalVideoViewPadding;
129 _localVideoView.frame = localVideoFrame;
130 } else {
131 _localVideoView.frame = bounds;
132 }
133
Zeke Chind3325802015-08-14 11:00:02 -0700134 // Place stats at the top.
135 CGSize statsSize = [_statsView sizeThatFits:bounds.size];
136 _statsView.frame = CGRectMake(CGRectGetMinX(bounds),
137 CGRectGetMinY(bounds) + kStatusBarHeight,
138 statsSize.width, statsSize.height);
139
Zeke Chin57cc74e2015-05-05 07:52:31 -0700140 // Place hangup button in the bottom left.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000141 _hangupButton.frame =
Zeke Chin57cc74e2015-05-05 07:52:31 -0700142 CGRectMake(CGRectGetMinX(bounds) + kButtonPadding,
143 CGRectGetMaxY(bounds) - kButtonPadding -
144 kButtonSize,
145 kButtonSize,
146 kButtonSize);
147
148 // Place button to the right of hangup button.
149 CGRect cameraSwitchFrame = _hangupButton.frame;
150 cameraSwitchFrame.origin.x =
151 CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding;
152 _cameraSwitchButton.frame = cameraSwitchFrame;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000153
154 [_statusLabel sizeToFit];
155 _statusLabel.center =
156 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
157}
158
159#pragma mark - RTCEAGLVideoViewDelegate
160
161- (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size {
162 if (videoView == _localVideoView) {
163 _localVideoSize = size;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700164 _localVideoView.hidden = CGSizeEqualToSize(CGSizeZero, _localVideoSize);
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000165 } else if (videoView == _remoteVideoView) {
166 _remoteVideoSize = size;
167 }
168 [self setNeedsLayout];
169}
170
171#pragma mark - Private
172
Zeke Chin57cc74e2015-05-05 07:52:31 -0700173- (void)onCameraSwitch:(id)sender {
174 [_delegate videoCallViewDidSwitchCamera:self];
175}
176
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000177- (void)onHangup:(id)sender {
178 [_delegate videoCallViewDidHangup:self];
179}
180
Zeke Chind3325802015-08-14 11:00:02 -0700181- (void)didTripleTap:(UITapGestureRecognizer *)recognizer {
182 [_delegate videoCallViewDidEnableStats:self];
183}
184
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000185@end