tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #import "ARDVideoCallView.h" |
| 12 | |
| 13 | #import <AVFoundation/AVFoundation.h> |
kthelgason | a2fb30c | 2017-03-09 03:34:27 -0800 | [diff] [blame] | 14 | |
| 15 | #import <WebRTC/RTCEAGLVideoView.h> |
Oleh Prypin | eec86cd | 2019-07-08 09:47:41 +0200 | [diff] [blame] | 16 | #if defined(RTC_SUPPORTS_METAL) |
kthelgason | a2fb30c | 2017-03-09 03:34:27 -0800 | [diff] [blame] | 17 | #import <WebRTC/RTCMTLVideoView.h> |
Oleh Prypin | eec86cd | 2019-07-08 09:47:41 +0200 | [diff] [blame] | 18 | #endif |
kthelgason | a2fb30c | 2017-03-09 03:34:27 -0800 | [diff] [blame] | 19 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 20 | #import "UIImage+ARDUtilities.h" |
| 21 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 22 | static CGFloat const kButtonPadding = 16; |
| 23 | static CGFloat const kButtonSize = 48; |
| 24 | static CGFloat const kLocalVideoViewSize = 120; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 25 | static CGFloat const kLocalVideoViewPadding = 8; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 26 | static CGFloat const kStatusBarHeight = 20; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 27 | |
Kári Tristan Helgason | 416018d | 2018-10-02 11:49:59 +0200 | [diff] [blame] | 28 | @interface ARDVideoCallView () <RTCVideoViewDelegate> |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 29 | @end |
| 30 | |
| 31 | @implementation ARDVideoCallView { |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 32 | UIButton *_routeChangeButton; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 33 | UIButton *_cameraSwitchButton; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 34 | UIButton *_hangupButton; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 35 | CGSize _remoteVideoSize; |
| 36 | } |
| 37 | |
| 38 | @synthesize statusLabel = _statusLabel; |
| 39 | @synthesize localVideoView = _localVideoView; |
| 40 | @synthesize remoteVideoView = _remoteVideoView; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 41 | @synthesize statsView = _statsView; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 42 | @synthesize delegate = _delegate; |
| 43 | |
| 44 | - (instancetype)initWithFrame:(CGRect)frame { |
| 45 | if (self = [super initWithFrame:frame]) { |
kthelgason | a2fb30c | 2017-03-09 03:34:27 -0800 | [diff] [blame] | 46 | |
| 47 | #if defined(RTC_SUPPORTS_METAL) |
JT Teh | a4888f0 | 2018-05-30 16:45:36 +0000 | [diff] [blame] | 48 | _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero]; |
kthelgason | a2fb30c | 2017-03-09 03:34:27 -0800 | [diff] [blame] | 49 | #else |
| 50 | RTCEAGLVideoView *remoteView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero]; |
| 51 | remoteView.delegate = self; |
| 52 | _remoteVideoView = remoteView; |
| 53 | #endif |
| 54 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 55 | [self addSubview:_remoteVideoView]; |
| 56 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 57 | _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 58 | [self addSubview:_localVideoView]; |
| 59 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 60 | _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero]; |
| 61 | _statsView.hidden = YES; |
| 62 | [self addSubview:_statsView]; |
| 63 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 64 | _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 Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 75 | // 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; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 80 | image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 81 | [_cameraSwitchButton setImage:image forState:UIControlStateNormal]; |
| 82 | [_cameraSwitchButton addTarget:self |
| 83 | action:@selector(onCameraSwitch:) |
| 84 | forControlEvents:UIControlEventTouchUpInside]; |
| 85 | [self addSubview:_cameraSwitchButton]; |
| 86 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 87 | _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 88 | _hangupButton.backgroundColor = [UIColor redColor]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 89 | _hangupButton.layer.cornerRadius = kButtonSize / 2; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 90 | _hangupButton.layer.masksToBounds = YES; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 91 | image = [UIImage imageForName:@"ic_call_end_black_24dp.png" |
| 92 | color:[UIColor whiteColor]]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 93 | [_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 Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 103 | |
| 104 | UITapGestureRecognizer *tapRecognizer = |
| 105 | [[UITapGestureRecognizer alloc] |
| 106 | initWithTarget:self |
| 107 | action:@selector(didTripleTap:)]; |
| 108 | tapRecognizer.numberOfTapsRequired = 3; |
| 109 | [self addGestureRecognizer:tapRecognizer]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 110 | } |
| 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 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 137 | // 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 Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 146 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 147 | // 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 Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 153 | // Place hangup button in the bottom left. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 154 | _hangupButton.frame = |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 155 | 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 166 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 167 | // 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 173 | [_statusLabel sizeToFit]; |
| 174 | _statusLabel.center = |
| 175 | CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); |
| 176 | } |
| 177 | |
Kári Tristan Helgason | 416018d | 2018-10-02 11:49:59 +0200 | [diff] [blame] | 178 | #pragma mark - RTCVideoViewDelegate |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 179 | |
Kári Tristan Helgason | 416018d | 2018-10-02 11:49:59 +0200 | [diff] [blame] | 180 | - (void)videoView:(id<RTCVideoRenderer>)videoView didChangeVideoSize:(CGSize)size { |
| 181 | if (videoView == _remoteVideoView) { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 182 | _remoteVideoSize = size; |
| 183 | } |
| 184 | [self setNeedsLayout]; |
| 185 | } |
| 186 | |
| 187 | #pragma mark - Private |
| 188 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 189 | - (void)onCameraSwitch:(id)sender { |
| 190 | [_delegate videoCallViewDidSwitchCamera:self]; |
| 191 | } |
| 192 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 193 | - (void)onRouteChange:(id)sender { |
| 194 | [_delegate videoCallViewDidChangeRoute:self]; |
| 195 | } |
| 196 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 197 | - (void)onHangup:(id)sender { |
| 198 | [_delegate videoCallViewDidHangup:self]; |
| 199 | } |
| 200 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 201 | - (void)didTripleTap:(UITapGestureRecognizer *)recognizer { |
| 202 | [_delegate videoCallViewDidEnableStats:self]; |
| 203 | } |
| 204 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 205 | @end |