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> |
denicija | 154a7bb | 2017-03-03 06:11:10 -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; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 34 | BOOL _useRearCamera; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @synthesize statusLabel = _statusLabel; |
| 38 | @synthesize localVideoView = _localVideoView; |
| 39 | @synthesize remoteVideoView = _remoteVideoView; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 40 | @synthesize statsView = _statsView; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 41 | @synthesize delegate = _delegate; |
| 42 | |
| 43 | - (instancetype)initWithFrame:(CGRect)frame { |
| 44 | if (self = [super initWithFrame:frame]) { |
denicija | 154a7bb | 2017-03-03 06:11:10 -0800 | [diff] [blame] | 45 | |
| 46 | #if defined(RTC_SUPPORTS_METAL) |
| 47 | _remoteVideoView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero]; |
| 48 | #else |
| 49 | RTCEAGLVideoView *remoteView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero]; |
| 50 | remoteView.delegate = self; |
| 51 | _remoteVideoView = remoteView; |
| 52 | #endif |
| 53 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 54 | [self addSubview:_remoteVideoView]; |
| 55 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 56 | _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 57 | [self addSubview:_localVideoView]; |
| 58 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 59 | _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero]; |
| 60 | _statsView.hidden = YES; |
| 61 | [self addSubview:_statsView]; |
| 62 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 63 | _routeChangeButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 64 | _routeChangeButton.backgroundColor = [UIColor whiteColor]; |
| 65 | _routeChangeButton.layer.cornerRadius = kButtonSize / 2; |
| 66 | _routeChangeButton.layer.masksToBounds = YES; |
| 67 | UIImage *image = [UIImage imageNamed:@"ic_surround_sound_black_24dp.png"]; |
| 68 | [_routeChangeButton setImage:image forState:UIControlStateNormal]; |
| 69 | [_routeChangeButton addTarget:self |
| 70 | action:@selector(onRouteChange:) |
| 71 | forControlEvents:UIControlEventTouchUpInside]; |
| 72 | [self addSubview:_routeChangeButton]; |
| 73 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 74 | // TODO(tkchin): don't display this if we can't actually do camera switch. |
| 75 | _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 76 | _cameraSwitchButton.backgroundColor = [UIColor whiteColor]; |
| 77 | _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2; |
| 78 | _cameraSwitchButton.layer.masksToBounds = YES; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 79 | image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 80 | [_cameraSwitchButton setImage:image forState:UIControlStateNormal]; |
| 81 | [_cameraSwitchButton addTarget:self |
| 82 | action:@selector(onCameraSwitch:) |
| 83 | forControlEvents:UIControlEventTouchUpInside]; |
| 84 | [self addSubview:_cameraSwitchButton]; |
| 85 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 86 | _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 87 | _hangupButton.backgroundColor = [UIColor redColor]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 88 | _hangupButton.layer.cornerRadius = kButtonSize / 2; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 89 | _hangupButton.layer.masksToBounds = YES; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 90 | image = [UIImage imageForName:@"ic_call_end_black_24dp.png" |
| 91 | color:[UIColor whiteColor]]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 92 | [_hangupButton setImage:image forState:UIControlStateNormal]; |
| 93 | [_hangupButton addTarget:self |
| 94 | action:@selector(onHangup:) |
| 95 | forControlEvents:UIControlEventTouchUpInside]; |
| 96 | [self addSubview:_hangupButton]; |
| 97 | |
| 98 | _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 99 | _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16]; |
| 100 | _statusLabel.textColor = [UIColor whiteColor]; |
| 101 | [self addSubview:_statusLabel]; |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 102 | |
| 103 | UITapGestureRecognizer *tapRecognizer = |
| 104 | [[UITapGestureRecognizer alloc] |
| 105 | initWithTarget:self |
| 106 | action:@selector(didTripleTap:)]; |
| 107 | tapRecognizer.numberOfTapsRequired = 3; |
| 108 | [self addGestureRecognizer:tapRecognizer]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 109 | } |
| 110 | return self; |
| 111 | } |
| 112 | |
| 113 | - (void)layoutSubviews { |
| 114 | CGRect bounds = self.bounds; |
| 115 | if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) { |
| 116 | // Aspect fill remote video into bounds. |
| 117 | CGRect remoteVideoFrame = |
| 118 | AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds); |
| 119 | CGFloat scale = 1; |
| 120 | if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) { |
| 121 | // Scale by height. |
| 122 | scale = bounds.size.height / remoteVideoFrame.size.height; |
| 123 | } else { |
| 124 | // Scale by width. |
| 125 | scale = bounds.size.width / remoteVideoFrame.size.width; |
| 126 | } |
| 127 | remoteVideoFrame.size.height *= scale; |
| 128 | remoteVideoFrame.size.width *= scale; |
| 129 | _remoteVideoView.frame = remoteVideoFrame; |
| 130 | _remoteVideoView.center = |
| 131 | CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); |
| 132 | } else { |
| 133 | _remoteVideoView.frame = bounds; |
| 134 | } |
| 135 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 136 | // Aspect fit local video view into a square box. |
| 137 | CGRect localVideoFrame = |
| 138 | CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize); |
| 139 | // Place the view in the bottom right. |
| 140 | localVideoFrame.origin.x = CGRectGetMaxX(bounds) |
| 141 | - localVideoFrame.size.width - kLocalVideoViewPadding; |
| 142 | localVideoFrame.origin.y = CGRectGetMaxY(bounds) |
| 143 | - localVideoFrame.size.height - kLocalVideoViewPadding; |
| 144 | _localVideoView.frame = localVideoFrame; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 145 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 146 | // Place stats at the top. |
| 147 | CGSize statsSize = [_statsView sizeThatFits:bounds.size]; |
| 148 | _statsView.frame = CGRectMake(CGRectGetMinX(bounds), |
| 149 | CGRectGetMinY(bounds) + kStatusBarHeight, |
| 150 | statsSize.width, statsSize.height); |
| 151 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 152 | // Place hangup button in the bottom left. |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 153 | _hangupButton.frame = |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 154 | CGRectMake(CGRectGetMinX(bounds) + kButtonPadding, |
| 155 | CGRectGetMaxY(bounds) - kButtonPadding - |
| 156 | kButtonSize, |
| 157 | kButtonSize, |
| 158 | kButtonSize); |
| 159 | |
| 160 | // Place button to the right of hangup button. |
| 161 | CGRect cameraSwitchFrame = _hangupButton.frame; |
| 162 | cameraSwitchFrame.origin.x = |
| 163 | CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding; |
| 164 | _cameraSwitchButton.frame = cameraSwitchFrame; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 165 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 166 | // Place route button to the right of camera button. |
| 167 | CGRect routeChangeFrame = _cameraSwitchButton.frame; |
| 168 | routeChangeFrame.origin.x = |
| 169 | CGRectGetMaxX(routeChangeFrame) + kButtonPadding; |
| 170 | _routeChangeButton.frame = routeChangeFrame; |
| 171 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 172 | [_statusLabel sizeToFit]; |
| 173 | _statusLabel.center = |
| 174 | CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); |
| 175 | } |
| 176 | |
| 177 | #pragma mark - RTCEAGLVideoViewDelegate |
| 178 | |
| 179 | - (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size { |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 180 | if (videoView == _remoteVideoView) { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 181 | _remoteVideoSize = size; |
| 182 | } |
| 183 | [self setNeedsLayout]; |
| 184 | } |
| 185 | |
| 186 | #pragma mark - Private |
| 187 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 188 | - (void)onCameraSwitch:(id)sender { |
| 189 | [_delegate videoCallViewDidSwitchCamera:self]; |
| 190 | } |
| 191 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 192 | - (void)onRouteChange:(id)sender { |
| 193 | [_delegate videoCallViewDidChangeRoute:self]; |
| 194 | } |
| 195 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 196 | - (void)onHangup:(id)sender { |
| 197 | [_delegate videoCallViewDidHangup:self]; |
| 198 | } |
| 199 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 200 | - (void)didTripleTap:(UITapGestureRecognizer *)recognizer { |
| 201 | [_delegate videoCallViewDidEnableStats:self]; |
| 202 | } |
| 203 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 204 | @end |