blob: 869d29cb6c4b02d4191a367160da11bf96bdde79 [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>
denicija154a7bb2017-03-03 06:11:10 -080014
15#import <WebRTC/RTCEAGLVideoView.h>
16#import <WebRTC/RTCMTLVideoView.h>
17
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000018#import "UIImage+ARDUtilities.h"
19
Zeke Chin57cc74e2015-05-05 07:52:31 -070020static CGFloat const kButtonPadding = 16;
21static CGFloat const kButtonSize = 48;
22static CGFloat const kLocalVideoViewSize = 120;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000023static CGFloat const kLocalVideoViewPadding = 8;
Zeke Chind3325802015-08-14 11:00:02 -070024static CGFloat const kStatusBarHeight = 20;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000025
26@interface ARDVideoCallView () <RTCEAGLVideoViewDelegate>
27@end
28
29@implementation ARDVideoCallView {
tkchin0ce3bf92016-03-12 16:52:04 -080030 UIButton *_routeChangeButton;
Zeke Chin57cc74e2015-05-05 07:52:31 -070031 UIButton *_cameraSwitchButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000032 UIButton *_hangupButton;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000033 CGSize _remoteVideoSize;
Zeke Chin57cc74e2015-05-05 07:52:31 -070034 BOOL _useRearCamera;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000035}
36
37@synthesize statusLabel = _statusLabel;
38@synthesize localVideoView = _localVideoView;
39@synthesize remoteVideoView = _remoteVideoView;
Zeke Chind3325802015-08-14 11:00:02 -070040@synthesize statsView = _statsView;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000041@synthesize delegate = _delegate;
42
43- (instancetype)initWithFrame:(CGRect)frame {
44 if (self = [super initWithFrame:frame]) {
denicija154a7bb2017-03-03 06:11:10 -080045
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.orgef2a5dd2015-01-15 22:38:21 +000054 [self addSubview:_remoteVideoView];
55
hayscedd8fef2015-12-08 11:08:39 -080056 _localVideoView = [[RTCCameraPreviewView alloc] initWithFrame:CGRectZero];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000057 [self addSubview:_localVideoView];
58
Zeke Chind3325802015-08-14 11:00:02 -070059 _statsView = [[ARDStatsView alloc] initWithFrame:CGRectZero];
60 _statsView.hidden = YES;
61 [self addSubview:_statsView];
62
tkchin0ce3bf92016-03-12 16:52:04 -080063 _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 Chin57cc74e2015-05-05 07:52:31 -070074 // 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;
tkchin0ce3bf92016-03-12 16:52:04 -080079 image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"];
Zeke Chin57cc74e2015-05-05 07:52:31 -070080 [_cameraSwitchButton setImage:image forState:UIControlStateNormal];
81 [_cameraSwitchButton addTarget:self
82 action:@selector(onCameraSwitch:)
83 forControlEvents:UIControlEventTouchUpInside];
84 [self addSubview:_cameraSwitchButton];
85
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000086 _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom];
87 _hangupButton.backgroundColor = [UIColor redColor];
Zeke Chin57cc74e2015-05-05 07:52:31 -070088 _hangupButton.layer.cornerRadius = kButtonSize / 2;
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000089 _hangupButton.layer.masksToBounds = YES;
Zeke Chin57cc74e2015-05-05 07:52:31 -070090 image = [UIImage imageForName:@"ic_call_end_black_24dp.png"
91 color:[UIColor whiteColor]];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +000092 [_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 Chind3325802015-08-14 11:00:02 -0700102
103 UITapGestureRecognizer *tapRecognizer =
104 [[UITapGestureRecognizer alloc]
105 initWithTarget:self
106 action:@selector(didTripleTap:)];
107 tapRecognizer.numberOfTapsRequired = 3;
108 [self addGestureRecognizer:tapRecognizer];
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000109 }
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
hayscedd8fef2015-12-08 11:08:39 -0800136 // 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 Chin57cc74e2015-05-05 07:52:31 -0700145
Zeke Chind3325802015-08-14 11:00:02 -0700146 // 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 Chin57cc74e2015-05-05 07:52:31 -0700152 // Place hangup button in the bottom left.
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000153 _hangupButton.frame =
Zeke Chin57cc74e2015-05-05 07:52:31 -0700154 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.orgef2a5dd2015-01-15 22:38:21 +0000165
tkchin0ce3bf92016-03-12 16:52:04 -0800166 // 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.orgef2a5dd2015-01-15 22:38:21 +0000172 [_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 {
hayscedd8fef2015-12-08 11:08:39 -0800180 if (videoView == _remoteVideoView) {
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000181 _remoteVideoSize = size;
182 }
183 [self setNeedsLayout];
184}
185
186#pragma mark - Private
187
Zeke Chin57cc74e2015-05-05 07:52:31 -0700188- (void)onCameraSwitch:(id)sender {
189 [_delegate videoCallViewDidSwitchCamera:self];
190}
191
tkchin0ce3bf92016-03-12 16:52:04 -0800192- (void)onRouteChange:(id)sender {
193 [_delegate videoCallViewDidChangeRoute:self];
194}
195
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000196- (void)onHangup:(id)sender {
197 [_delegate videoCallViewDidHangup:self];
198}
199
Zeke Chind3325802015-08-14 11:00:02 -0700200- (void)didTripleTap:(UITapGestureRecognizer *)recognizer {
201 [_delegate videoCallViewDidEnableStats:self];
202}
203
tkchin@webrtc.orgef2a5dd2015-01-15 22:38:21 +0000204@end