blob: 437aea8d5647c519cf35e2f62da10c165ea38ebb [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
Jake Bromberg28e582d2021-06-22 11:34:29 -070015#import "sdk/objc/components/renderer/metal/RTCMTLVideoView.h"
kthelgasona2fb30c2017-03-09 03:34:27 -080016
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000017#import "UIImage+ARDUtilities.h"
18
Zeke Chin57cc74e2015-05-05 07:52:31 -070019static CGFloat const kButtonPadding = 16;
20static CGFloat const kButtonSize = 48;
21static CGFloat const kLocalVideoViewSize = 120;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000022static CGFloat const kLocalVideoViewPadding = 8;
Zeke Chind3325802015-08-14 11:00:02 -070023static CGFloat const kStatusBarHeight = 20;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000024
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020025@interface ARDVideoCallView () <RTC_OBJC_TYPE (RTCVideoViewDelegate)>
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000026@end
27
28@implementation ARDVideoCallView {
tkchin0ce3bf92016-03-12 16:52:04 -080029 UIButton *_routeChangeButton;
Zeke Chin57cc74e2015-05-05 07:52:31 -070030 UIButton *_cameraSwitchButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000031 UIButton *_hangupButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000032 CGSize _remoteVideoSize;
33}
34
35@synthesize statusLabel = _statusLabel;
36@synthesize localVideoView = _localVideoView;
37@synthesize remoteVideoView = _remoteVideoView;
Zeke Chind3325802015-08-14 11:00:02 -070038@synthesize statsView = _statsView;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000039@synthesize delegate = _delegate;
40
41- (instancetype)initWithFrame:(CGRect)frame {
42 if (self = [super initWithFrame:frame]) {
kthelgasona2fb30c2017-03-09 03:34:27 -080043
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020044 _remoteVideoView = [[RTC_OBJC_TYPE(RTCMTLVideoView) alloc] initWithFrame:CGRectZero];
kthelgasona2fb30c2017-03-09 03:34:27 -080045
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000046 [self addSubview:_remoteVideoView];
47
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020048 _localVideoView = [[RTC_OBJC_TYPE(RTCCameraPreviewView) alloc] initWithFrame:CGRectZero];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000049 [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
tkchin0ce3bf92016-03-12 16:52:04 -080055 _routeChangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
dharmeshf824ef82020-07-20 14:40:28 +053056 _routeChangeButton.backgroundColor = [UIColor grayColor];
tkchin0ce3bf92016-03-12 16:52:04 -080057 _routeChangeButton.layer.cornerRadius = kButtonSize / 2;
58 _routeChangeButton.layer.masksToBounds = YES;
dharmeshf824ef82020-07-20 14:40:28 +053059 UIImage *image = [UIImage imageForName:@"ic_surround_sound_black_24dp.png"
60 color:[UIColor whiteColor]];
tkchin0ce3bf92016-03-12 16:52:04 -080061 [_routeChangeButton setImage:image forState:UIControlStateNormal];
62 [_routeChangeButton addTarget:self
63 action:@selector(onRouteChange:)
64 forControlEvents:UIControlEventTouchUpInside];
65 [self addSubview:_routeChangeButton];
66
Zeke Chin57cc74e2015-05-05 07:52:31 -070067 // TODO(tkchin): don't display this if we can't actually do camera switch.
68 _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom];
dharmeshf824ef82020-07-20 14:40:28 +053069 _cameraSwitchButton.backgroundColor = [UIColor grayColor];
Zeke Chin57cc74e2015-05-05 07:52:31 -070070 _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2;
71 _cameraSwitchButton.layer.masksToBounds = YES;
dharmeshf824ef82020-07-20 14:40:28 +053072 image = [UIImage imageForName:@"ic_switch_video_black_24dp.png" color:[UIColor whiteColor]];
Zeke Chin57cc74e2015-05-05 07:52:31 -070073 [_cameraSwitchButton setImage:image forState:UIControlStateNormal];
74 [_cameraSwitchButton addTarget:self
75 action:@selector(onCameraSwitch:)
76 forControlEvents:UIControlEventTouchUpInside];
77 [self addSubview:_cameraSwitchButton];
78
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000079 _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom];
80 _hangupButton.backgroundColor = [UIColor redColor];
Zeke Chin57cc74e2015-05-05 07:52:31 -070081 _hangupButton.layer.cornerRadius = kButtonSize / 2;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000082 _hangupButton.layer.masksToBounds = YES;
Zeke Chin57cc74e2015-05-05 07:52:31 -070083 image = [UIImage imageForName:@"ic_call_end_black_24dp.png"
84 color:[UIColor whiteColor]];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000085 [_hangupButton setImage:image forState:UIControlStateNormal];
86 [_hangupButton addTarget:self
87 action:@selector(onHangup:)
88 forControlEvents:UIControlEventTouchUpInside];
89 [self addSubview:_hangupButton];
90
91 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero];
92 _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16];
93 _statusLabel.textColor = [UIColor whiteColor];
94 [self addSubview:_statusLabel];
Zeke Chind3325802015-08-14 11:00:02 -070095
96 UITapGestureRecognizer *tapRecognizer =
97 [[UITapGestureRecognizer alloc]
98 initWithTarget:self
99 action:@selector(didTripleTap:)];
100 tapRecognizer.numberOfTapsRequired = 3;
101 [self addGestureRecognizer:tapRecognizer];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000102 }
103 return self;
104}
105
106- (void)layoutSubviews {
107 CGRect bounds = self.bounds;
108 if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) {
109 // Aspect fill remote video into bounds.
110 CGRect remoteVideoFrame =
111 AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds);
112 CGFloat scale = 1;
113 if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) {
114 // Scale by height.
115 scale = bounds.size.height / remoteVideoFrame.size.height;
116 } else {
117 // Scale by width.
118 scale = bounds.size.width / remoteVideoFrame.size.width;
119 }
120 remoteVideoFrame.size.height *= scale;
121 remoteVideoFrame.size.width *= scale;
122 _remoteVideoView.frame = remoteVideoFrame;
123 _remoteVideoView.center =
124 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
125 } else {
126 _remoteVideoView.frame = bounds;
127 }
128
hayscedd8fef2015-12-08 11:08:39 -0800129 // Aspect fit local video view into a square box.
130 CGRect localVideoFrame =
131 CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize);
132 // Place the view in the bottom right.
133 localVideoFrame.origin.x = CGRectGetMaxX(bounds)
134 - localVideoFrame.size.width - kLocalVideoViewPadding;
135 localVideoFrame.origin.y = CGRectGetMaxY(bounds)
136 - localVideoFrame.size.height - kLocalVideoViewPadding;
137 _localVideoView.frame = localVideoFrame;
Zeke Chin57cc74e2015-05-05 07:52:31 -0700138
Zeke Chind3325802015-08-14 11:00:02 -0700139 // Place stats at the top.
140 CGSize statsSize = [_statsView sizeThatFits:bounds.size];
141 _statsView.frame = CGRectMake(CGRectGetMinX(bounds),
142 CGRectGetMinY(bounds) + kStatusBarHeight,
143 statsSize.width, statsSize.height);
144
Zeke Chin57cc74e2015-05-05 07:52:31 -0700145 // Place hangup button in the bottom left.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000146 _hangupButton.frame =
Zeke Chin57cc74e2015-05-05 07:52:31 -0700147 CGRectMake(CGRectGetMinX(bounds) + kButtonPadding,
148 CGRectGetMaxY(bounds) - kButtonPadding -
149 kButtonSize,
150 kButtonSize,
151 kButtonSize);
152
153 // Place button to the right of hangup button.
154 CGRect cameraSwitchFrame = _hangupButton.frame;
155 cameraSwitchFrame.origin.x =
156 CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding;
157 _cameraSwitchButton.frame = cameraSwitchFrame;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000158
tkchin0ce3bf92016-03-12 16:52:04 -0800159 // Place route button to the right of camera button.
160 CGRect routeChangeFrame = _cameraSwitchButton.frame;
161 routeChangeFrame.origin.x =
162 CGRectGetMaxX(routeChangeFrame) + kButtonPadding;
163 _routeChangeButton.frame = routeChangeFrame;
164
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000165 [_statusLabel sizeToFit];
166 _statusLabel.center =
167 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
168}
169
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200170#pragma mark - RTC_OBJC_TYPE(RTCVideoViewDelegate)
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000171
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200172- (void)videoView:(id<RTC_OBJC_TYPE(RTCVideoRenderer)>)videoView didChangeVideoSize:(CGSize)size {
Kári Tristan Helgason416018d2018-10-02 11:49:59 +0200173 if (videoView == _remoteVideoView) {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000174 _remoteVideoSize = size;
175 }
176 [self setNeedsLayout];
177}
178
179#pragma mark - Private
180
dharmeshf824ef82020-07-20 14:40:28 +0530181- (void)onCameraSwitch:(UIButton *)sender {
182 sender.enabled = false;
183 [_delegate videoCallView:self
184 shouldSwitchCameraWithCompletion:^(NSError *error) {
185 dispatch_async(dispatch_get_main_queue(), ^(void) {
186 sender.enabled = true;
187 });
188 }];
Zeke Chin57cc74e2015-05-05 07:52:31 -0700189}
190
dharmeshf824ef82020-07-20 14:40:28 +0530191- (void)onRouteChange:(UIButton *)sender {
192 sender.enabled = false;
193 __weak ARDVideoCallView *weakSelf = self;
194 [_delegate videoCallView:self
195 shouldChangeRouteWithCompletion:^(void) {
196 ARDVideoCallView *strongSelf = weakSelf;
197 if (strongSelf) {
198 dispatch_async(dispatch_get_main_queue(), ^(void) {
199 sender.enabled = true;
200 });
201 }
202 }];
tkchin0ce3bf92016-03-12 16:52:04 -0800203}
204
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000205- (void)onHangup:(id)sender {
206 [_delegate videoCallViewDidHangup:self];
207}
208
Zeke Chind3325802015-08-14 11:00:02 -0700209- (void)didTripleTap:(UITapGestureRecognizer *)recognizer {
210 [_delegate videoCallViewDidEnableStats:self];
211}
212
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000213@end