blob: 60b78c7340244fa906f9d32170bd28ddd5867d9b [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>
Oleh Prypineec86cd2019-07-08 09:47:41 +020016#if defined(RTC_SUPPORTS_METAL)
kthelgasona2fb30c2017-03-09 03:34:27 -080017#import <WebRTC/RTCMTLVideoView.h>
Oleh Prypineec86cd2019-07-08 09:47:41 +020018#endif
kthelgasona2fb30c2017-03-09 03:34:27 -080019
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000020#import "UIImage+ARDUtilities.h"
21
Zeke Chin57cc74e2015-05-05 07:52:31 -070022static CGFloat const kButtonPadding = 16;
23static CGFloat const kButtonSize = 48;
24static CGFloat const kLocalVideoViewSize = 120;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000025static CGFloat const kLocalVideoViewPadding = 8;
Zeke Chind3325802015-08-14 11:00:02 -070026static CGFloat const kStatusBarHeight = 20;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000027
Kári Tristan Helgason416018d2018-10-02 11:49:59 +020028@interface ARDVideoCallView () <RTCVideoViewDelegate>
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000029@end
30
31@implementation ARDVideoCallView {
tkchin0ce3bf92016-03-12 16:52:04 -080032 UIButton *_routeChangeButton;
Zeke Chin57cc74e2015-05-05 07:52:31 -070033 UIButton *_cameraSwitchButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000034 UIButton *_hangupButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000035 CGSize _remoteVideoSize;
36}
37
38@synthesize statusLabel = _statusLabel;
39@synthesize localVideoView = _localVideoView;
40@synthesize remoteVideoView = _remoteVideoView;
Zeke Chind3325802015-08-14 11:00:02 -070041@synthesize statsView = _statsView;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000042@synthesize delegate = _delegate;
43
44- (instancetype)initWithFrame:(CGRect)frame {
45 if (self = [super initWithFrame:frame]) {
kthelgasona2fb30c2017-03-09 03:34:27 -080046
47#if defined(RTC_SUPPORTS_METAL)
JT Teha4888f02018-05-30 16:45:36 +000048 _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
kthelgasona2fb30c2017-03-09 03:34:27 -080049#else
50 RTCEAGLVideoView *remoteView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero];
51 remoteView.delegate = self;
52 _remoteVideoView = remoteView;
53#endif
54
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000055 [self addSubview:_remoteVideoView];
56
hayscedd8fef2015-12-08 11:08:39 -080057 _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000058 [self addSubview:_localVideoView];
59
Zeke Chind3325802015-08-14 11:00:02 -070060 _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero];
61 _statsView.hidden = YES;
62 [self addSubview:_statsView];
63
tkchin0ce3bf92016-03-12 16:52:04 -080064 _routeChangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
65 _routeChangeButton.backgroundColor = [UIColor whiteColor];
66 _routeChangeButton.layer.cornerRadius = kButtonSize / 2;
67 _routeChangeButton.layer.masksToBounds = YES;
68 UIImage *image = [UIImage imageNamed:@"ic_surround_sound_black_24dp.png"];
69 [_routeChangeButton setImage:image forState:UIControlStateNormal];
70 [_routeChangeButton addTarget:self
71 action:@selector(onRouteChange:)
72 forControlEvents:UIControlEventTouchUpInside];
73 [self addSubview:_routeChangeButton];
74
Zeke Chin57cc74e2015-05-05 07:52:31 -070075 // TODO(tkchin): don't display this if we can't actually do camera switch.
76 _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
77 _cameraSwitchButton.backgroundColor = [UIColor whiteColor];
78 _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2;
79 _cameraSwitchButton.layer.masksToBounds = YES;
tkchin0ce3bf92016-03-12 16:52:04 -080080 image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"];
Zeke Chin57cc74e2015-05-05 07:52:31 -070081 [_cameraSwitchButton setImage:image forState:UIControlStateNormal];
82 [_cameraSwitchButton addTarget:self
83 action:@selector(onCameraSwitch:)
84 forControlEvents:UIControlEventTouchUpInside];
85 [self addSubview:_cameraSwitchButton];
86
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000087 _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom];
88 _hangupButton.backgroundColor = [UIColor redColor];
Zeke Chin57cc74e2015-05-05 07:52:31 -070089 _hangupButton.layer.cornerRadius = kButtonSize / 2;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000090 _hangupButton.layer.masksToBounds = YES;
Zeke Chin57cc74e2015-05-05 07:52:31 -070091 image = [UIImage imageForName:@"ic_call_end_black_24dp.png"
92 color:[UIColor whiteColor]];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000093 [_hangupButton setImage:image forState:UIControlStateNormal];
94 [_hangupButton addTarget:self
95 action:@selector(onHangup:)
96 forControlEvents:UIControlEventTouchUpInside];
97 [self addSubview:_hangupButton];
98
99 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero];
100 _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16];
101 _statusLabel.textColor = [UIColor whiteColor];
102 [self addSubview:_statusLabel];
Zeke Chind3325802015-08-14 11:00:02 -0700103
104 UITapGestureRecognizer *tapRecognizer =
105 [[UITapGestureRecognizer alloc]
106 initWithTarget:self
107 action:@selector(didTripleTap:)];
108 tapRecognizer.numberOfTapsRequired = 3;
109 [self addGestureRecognizer:tapRecognizer];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000110 }
111 return self;
112}
113
114- (void)layoutSubviews {
115 CGRect bounds = self.bounds;
116 if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
117 // Aspect fill remote video into bounds.
118 CGRect remoteVideoFrame =
119 AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
120 CGFloat scale = 1;
121 if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
122 // Scale by height.
123 scale = bounds.size.height / remoteVideoFrame.size.height;
124 } else {
125 // Scale by width.
126 scale = bounds.size.width / remoteVideoFrame.size.width;
127 }
128 remoteVideoFrame.size.height *= scale;
129 remoteVideoFrame.size.width *= scale;
130 _remoteVideoView.frame = remoteVideoFrame;
131 _remoteVideoView.center =
132 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
133 } else {
134 _remoteVideoView.frame = bounds;
135 }
136
hayscedd8fef2015-12-08 11:08:39 -0800137 // Aspect fit local video view into a square box.
138 CGRect localVideoFrame =
139 CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize);
140 // Place the view in the bottom right.
141 localVideoFrame.origin.x = CGRectGetMaxX(bounds)
142 - localVideoFrame.size.width - kLocalVideoViewPadding;
143 localVideoFrame.origin.y = CGRectGetMaxY(bounds)
144 - localVideoFrame.size.height - kLocalVideoViewPadding;
145 _localVideoView.frame = localVideoFrame;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700146
Zeke Chind3325802015-08-14 11:00:02 -0700147 // Place stats at the top.
148 CGSize statsSize = [_statsView sizeThatFits:bounds.size];
149 _statsView.frame = CGRectMake(CGRectGetMinX(bounds),
150 CGRectGetMinY(bounds) + kStatusBarHeight,
151 statsSize.width, statsSize.height);
152
Zeke Chin57cc74e2015-05-05 07:52:31 -0700153 // Place hangup button in the bottom left.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000154 _hangupButton.frame =
Zeke Chin57cc74e2015-05-05 07:52:31 -0700155 CGRectMake(CGRectGetMinX(bounds) + kButtonPadding,
156 CGRectGetMaxY(bounds) - kButtonPadding -
157 kButtonSize,
158 kButtonSize,
159 kButtonSize);
160
161 // Place button to the right of hangup button.
162 CGRect cameraSwitchFrame = _hangupButton.frame;
163 cameraSwitchFrame.origin.x =
164 CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding;
165 _cameraSwitchButton.frame = cameraSwitchFrame;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000166
tkchin0ce3bf92016-03-12 16:52:04 -0800167 // Place route button to the right of camera button.
168 CGRect routeChangeFrame = _cameraSwitchButton.frame;
169 routeChangeFrame.origin.x =
170 CGRectGetMaxX(routeChangeFrame) + kButtonPadding;
171 _routeChangeButton.frame = routeChangeFrame;
172
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000173 [_statusLabel sizeToFit];
174 _statusLabel.center =
175 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
176}
177
Kári Tristan Helgason416018d2018-10-02 11:49:59 +0200178#pragma mark - RTCVideoViewDelegate
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000179
Kári Tristan Helgason416018d2018-10-02 11:49:59 +0200180- (void)videoView:(id<RTCVideoRenderer>)videoView didChangeVideoSize:(CGSize)size {
181 if (videoView == _remoteVideoView) {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000182 _remoteVideoSize = size;
183 }
184 [self setNeedsLayout];
185}
186
187#pragma mark - Private
188
Zeke Chin57cc74e2015-05-05 07:52:31 -0700189- (void)onCameraSwitch:(id)sender {
190 [_delegate videoCallViewDidSwitchCamera:self];
191}
192
tkchin0ce3bf92016-03-12 16:52:04 -0800193- (void)onRouteChange:(id)sender {
194 [_delegate videoCallViewDidChangeRoute:self];
195}
196
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000197- (void)onHangup:(id)sender {
198 [_delegate videoCallViewDidHangup:self];
199}
200
Zeke Chind3325802015-08-14 11:00:02 -0700201- (void)didTripleTap:(UITapGestureRecognizer *)recognizer {
202 [_delegate videoCallViewDidEnableStats:self];
203}
204
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000205@end