blob: 2241930d4736e53523f73e28addcecadd43a8030 [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>
kthelgasona2fb30c2017-03-09 03:34:27 -080014
15#import <WebRTC/RTCEAGLVideoView.h>
16#import <WebRTC/RTCMTLVideoView.h>
17
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000018#import "UIImage+ARDUtilities.h"
19
Zeke Chin57cc74e2015-05-05 07:52:31 -070020static CGFloat const kButtonPadding = 16;
21static CGFloat const kButtonSize = 48;
22static CGFloat const kLocalVideoViewSize = 120;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000023static CGFloat const kLocalVideoViewPadding = 8;
Zeke Chind3325802015-08-14 11:00:02 -070024static CGFloat const kStatusBarHeight = 20;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000025
26@interface ARDVideoCallView () <RTCEAGLVideoViewDelegate>
27@end
28
29@implementation ARDVideoCallView {
tkchin0ce3bf92016-03-12 16:52:04 -080030 UIButton *_routeChangeButton;
Zeke Chin57cc74e2015-05-05 07:52:31 -070031 UIButton *_cameraSwitchButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000032 UIButton *_hangupButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000033 CGSize _remoteVideoSize;
34}
35
36@synthesize statusLabel = _statusLabel;
37@synthesize localVideoView = _localVideoView;
38@synthesize remoteVideoView = _remoteVideoView;
Zeke Chind3325802015-08-14 11:00:02 -070039@synthesize statsView = _statsView;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000040@synthesize delegate = _delegate;
41
42- (instancetype)initWithFrame:(CGRect)frame {
43 if (self = [super initWithFrame:frame]) {
kthelgasona2fb30c2017-03-09 03:34:27 -080044
45#if defined(RTC_SUPPORTS_METAL)
46 _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
47#else
48 RTCEAGLVideoView *remoteView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
49 remoteView.delegate = self;
50 _remoteVideoView = remoteView;
51#endif
52
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000053 [self addSubview:_remoteVideoView];
54
hayscedd8fef2015-12-08 11:08:39 -080055 _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000056 [self addSubview:_localVideoView];
57
Zeke Chind3325802015-08-14 11:00:02 -070058 _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero];
59 _statsView.hidden = YES;
60 [self addSubview:_statsView];
61
tkchin0ce3bf92016-03-12 16:52:04 -080062 _routeChangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
63 _routeChangeButton.backgroundColor = [UIColor whiteColor];
64 _routeChangeButton.layer.cornerRadius = kButtonSize / 2;
65 _routeChangeButton.layer.masksToBounds = YES;
66 UIImage *image = [UIImage imageNamed:@"ic_surround_sound_black_24dp.png"];
67 [_routeChangeButton setImage:image forState:UIControlStateNormal];
68 [_routeChangeButton addTarget:self
69 action:@selector(onRouteChange:)
70 forControlEvents:UIControlEventTouchUpInside];
71 [self addSubview:_routeChangeButton];
72
Zeke Chin57cc74e2015-05-05 07:52:31 -070073 // TODO(tkchin): don't display this if we can't actually do camera switch.
74 _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
75 _cameraSwitchButton.backgroundColor = [UIColor whiteColor];
76 _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2;
77 _cameraSwitchButton.layer.masksToBounds = YES;
tkchin0ce3bf92016-03-12 16:52:04 -080078 image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"];
Zeke Chin57cc74e2015-05-05 07:52:31 -070079 [_cameraSwitchButton setImage:image forState:UIControlStateNormal];
80 [_cameraSwitchButton addTarget:self
81 action:@selector(onCameraSwitch:)
82 forControlEvents:UIControlEventTouchUpInside];
83 [self addSubview:_cameraSwitchButton];
84
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000085 _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom];
86 _hangupButton.backgroundColor = [UIColor redColor];
Zeke Chin57cc74e2015-05-05 07:52:31 -070087 _hangupButton.layer.cornerRadius = kButtonSize / 2;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000088 _hangupButton.layer.masksToBounds = YES;
Zeke Chin57cc74e2015-05-05 07:52:31 -070089 image = [UIImage imageForName:@"ic_call_end_black_24dp.png"
90 color:[UIColor whiteColor]];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000091 [_hangupButton setImage:image forState:UIControlStateNormal];
92 [_hangupButton addTarget:self
93 action:@selector(onHangup:)
94 forControlEvents:UIControlEventTouchUpInside];
95 [self addSubview:_hangupButton];
96
97 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero];
98 _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16];
99 _statusLabel.textColor = [UIColor whiteColor];
100 [self addSubview:_statusLabel];
Zeke Chind3325802015-08-14 11:00:02 -0700101
102 UITapGestureRecognizer *tapRecognizer =
103 [[UITapGestureRecognizer alloc]
104 initWithTarget:self
105 action:@selector(didTripleTap:)];
106 tapRecognizer.numberOfTapsRequired = 3;
107 [self addGestureRecognizer:tapRecognizer];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000108 }
109 return self;
110}
111
112- (void)layoutSubviews {
113 CGRect bounds = self.bounds;
114 if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
115 // Aspect fill remote video into bounds.
116 CGRect remoteVideoFrame =
117 AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
118 CGFloat scale = 1;
119 if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
120 // Scale by height.
121 scale = bounds.size.height / remoteVideoFrame.size.height;
122 } else {
123 // Scale by width.
124 scale = bounds.size.width / remoteVideoFrame.size.width;
125 }
126 remoteVideoFrame.size.height *= scale;
127 remoteVideoFrame.size.width *= scale;
128 _remoteVideoView.frame = remoteVideoFrame;
129 _remoteVideoView.center =
130 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
131 } else {
132 _remoteVideoView.frame = bounds;
133 }
134
hayscedd8fef2015-12-08 11:08:39 -0800135 // Aspect fit local video view into a square box.
136 CGRect localVideoFrame =
137 CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize);
138 // Place the view in the bottom right.
139 localVideoFrame.origin.x = CGRectGetMaxX(bounds)
140 - localVideoFrame.size.width - kLocalVideoViewPadding;
141 localVideoFrame.origin.y = CGRectGetMaxY(bounds)
142 - localVideoFrame.size.height - kLocalVideoViewPadding;
143 _localVideoView.frame = localVideoFrame;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700144
Zeke Chind3325802015-08-14 11:00:02 -0700145 // Place stats at the top.
146 CGSize statsSize = [_statsView sizeThatFits:bounds.size];
147 _statsView.frame = CGRectMake(CGRectGetMinX(bounds),
148 CGRectGetMinY(bounds) + kStatusBarHeight,
149 statsSize.width, statsSize.height);
150
Zeke Chin57cc74e2015-05-05 07:52:31 -0700151 // Place hangup button in the bottom left.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000152 _hangupButton.frame =
Zeke Chin57cc74e2015-05-05 07:52:31 -0700153 CGRectMake(CGRectGetMinX(bounds) + kButtonPadding,
154 CGRectGetMaxY(bounds) - kButtonPadding -
155 kButtonSize,
156 kButtonSize,
157 kButtonSize);
158
159 // Place button to the right of hangup button.
160 CGRect cameraSwitchFrame = _hangupButton.frame;
161 cameraSwitchFrame.origin.x =
162 CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding;
163 _cameraSwitchButton.frame = cameraSwitchFrame;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000164
tkchin0ce3bf92016-03-12 16:52:04 -0800165 // Place route button to the right of camera button.
166 CGRect routeChangeFrame = _cameraSwitchButton.frame;
167 routeChangeFrame.origin.x =
168 CGRectGetMaxX(routeChangeFrame) + kButtonPadding;
169 _routeChangeButton.frame = routeChangeFrame;
170
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000171 [_statusLabel sizeToFit];
172 _statusLabel.center =
173 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
174}
175
176#pragma mark - RTCEAGLVideoViewDelegate
177
178- (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size {
hayscedd8fef2015-12-08 11:08:39 -0800179 if (videoView == _remoteVideoView) {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000180 _remoteVideoSize = size;
181 }
182 [self setNeedsLayout];
183}
184
185#pragma mark - Private
186
Zeke Chin57cc74e2015-05-05 07:52:31 -0700187- (void)onCameraSwitch:(id)sender {
188 [_delegate videoCallViewDidSwitchCamera:self];
189}
190
tkchin0ce3bf92016-03-12 16:52:04 -0800191- (void)onRouteChange:(id)sender {
192 [_delegate videoCallViewDidChangeRoute:self];
193}
194
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000195- (void)onHangup:(id)sender {
196 [_delegate videoCallViewDidHangup:self];
197}
198
Zeke Chind3325802015-08-14 11:00:02 -0700199- (void)didTripleTap:(UITapGestureRecognizer *)recognizer {
200 [_delegate videoCallViewDidEnableStats:self];
201}
202
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000203@end