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> |
| 16 | #import <WebRTC/RTCMTLVideoView.h> |
| 17 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 18 | #import "UIImage+ARDUtilities.h" |
| 19 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 20 | static CGFloat const kButtonPadding = 16; |
| 21 | static CGFloat const kButtonSize = 48; |
| 22 | static CGFloat const kLocalVideoViewSize = 120; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 23 | static CGFloat const kLocalVideoViewPadding = 8; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 24 | static CGFloat const kStatusBarHeight = 20; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 25 | |
| 26 | @interface ARDVideoCallView () <RTCEAGLVideoViewDelegate> |
| 27 | @end |
| 28 | |
| 29 | @implementation ARDVideoCallView { |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 30 | UIButton *_routeChangeButton; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 31 | UIButton *_cameraSwitchButton; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 32 | UIButton *_hangupButton; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 33 | CGSize _remoteVideoSize; |
| 34 | } |
| 35 | |
| 36 | @synthesize statusLabel = _statusLabel; |
| 37 | @synthesize localVideoView = _localVideoView; |
| 38 | @synthesize remoteVideoView = _remoteVideoView; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 39 | @synthesize statsView = _statsView; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 40 | @synthesize delegate = _delegate; |
| 41 | |
| 42 | - (instancetype)initWithFrame:(CGRect)frame { |
| 43 | if (self = [super initWithFrame:frame]) { |
kthelgason | a2fb30c | 2017-03-09 03:34:27 -0800 | [diff] [blame] | 44 | |
| 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 53 | [self addSubview:_remoteVideoView]; |
| 54 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 55 | _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 56 | [self addSubview:_localVideoView]; |
| 57 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 58 | _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero]; |
| 59 | _statsView.hidden = YES; |
| 60 | [self addSubview:_statsView]; |
| 61 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 62 | _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 Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 73 | // 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; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 78 | image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 79 | [_cameraSwitchButton setImage:image forState:UIControlStateNormal]; |
| 80 | [_cameraSwitchButton addTarget:self |
| 81 | action:@selector(onCameraSwitch:) |
| 82 | forControlEvents:UIControlEventTouchUpInside]; |
| 83 | [self addSubview:_cameraSwitchButton]; |
| 84 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 85 | _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 86 | _hangupButton.backgroundColor = [UIColor redColor]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 87 | _hangupButton.layer.cornerRadius = kButtonSize / 2; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 88 | _hangupButton.layer.masksToBounds = YES; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 89 | image = [UIImage imageForName:@"ic_call_end_black_24dp.png" |
| 90 | color:[UIColor whiteColor]]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 91 | [_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 Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 101 | |
| 102 | UITapGestureRecognizer *tapRecognizer = |
| 103 | [[UITapGestureRecognizer alloc] |
| 104 | initWithTarget:self |
| 105 | action:@selector(didTripleTap:)]; |
| 106 | tapRecognizer.numberOfTapsRequired = 3; |
| 107 | [self addGestureRecognizer:tapRecognizer]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 108 | } |
| 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 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 135 | // 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 Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 144 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 145 | // 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 Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 151 | // Place hangup button in the bottom left. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 152 | _hangupButton.frame = |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 153 | 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 164 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 165 | // 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.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 171 | [_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 { |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 179 | if (videoView == _remoteVideoView) { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 180 | _remoteVideoSize = size; |
| 181 | } |
| 182 | [self setNeedsLayout]; |
| 183 | } |
| 184 | |
| 185 | #pragma mark - Private |
| 186 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 187 | - (void)onCameraSwitch:(id)sender { |
| 188 | [_delegate videoCallViewDidSwitchCamera:self]; |
| 189 | } |
| 190 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 191 | - (void)onRouteChange:(id)sender { |
| 192 | [_delegate videoCallViewDidChangeRoute:self]; |
| 193 | } |
| 194 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 195 | - (void)onHangup:(id)sender { |
| 196 | [_delegate videoCallViewDidHangup:self]; |
| 197 | } |
| 198 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 199 | - (void)didTripleTap:(UITapGestureRecognizer *)recognizer { |
| 200 | [_delegate videoCallViewDidEnableStats:self]; |
| 201 | } |
| 202 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 203 | @end |