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