OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2015 Google Inc. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright notice, | |
9 * this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 #import "ARDVideoCallView.h" | |
29 | |
30 #import <AVFoundation/AVFoundation.h> | |
31 #import "UIImage+ARDUtilities.h" | |
32 | |
33 static CGFloat const kButtonPadding = 16; | |
34 static CGFloat const kButtonSize = 48; | |
35 static CGFloat const kLocalVideoViewSize = 120; | |
36 static CGFloat const kLocalVideoViewPadding = 8; | |
37 | |
38 @interface ARDVideoCallView () <RTCEAGLVideoViewDelegate> | |
39 @end | |
40 | |
41 @implementation ARDVideoCallView { | |
42 UIButton *_cameraSwitchButton; | |
43 UIButton *_hangupButton; | |
44 CGSize _localVideoSize; | |
45 CGSize _remoteVideoSize; | |
46 BOOL _useRearCamera; | |
47 } | |
48 | |
49 @synthesize statusLabel = _statusLabel; | |
50 @synthesize localVideoView = _localVideoView; | |
51 @synthesize remoteVideoView = _remoteVideoView; | |
52 @synthesize delegate = _delegate; | |
53 | |
54 - (instancetype)initWithFrame:(CGRect)frame { | |
55 if (self = [super initWithFrame:frame]) { | |
56 _remoteVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero]; | |
57 _remoteVideoView.delegate = self; | |
58 [self addSubview:_remoteVideoView]; | |
59 | |
60 // TODO(tkchin): replace this with a view that renders layer from | |
61 // AVCaptureSession. | |
62 _localVideoView = [[RTCEAGLVideoView alloc] initWithFrame:CGRectZero]; | |
63 _localVideoView.delegate = self; | |
64 [self addSubview:_localVideoView]; | |
65 | |
66 // TODO(tkchin): don't display this if we can't actually do camera switch. | |
67 _cameraSwitchButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
68 _cameraSwitchButton.backgroundColor = [UIColor whiteColor]; | |
69 _cameraSwitchButton.layer.cornerRadius = kButtonSize / 2; | |
70 _cameraSwitchButton.layer.masksToBounds = YES; | |
71 UIImage *image = [UIImage imageNamed:@"ic_switch_video_black_24dp.png"]; | |
72 [_cameraSwitchButton setImage:image forState:UIControlStateNormal]; | |
73 [_cameraSwitchButton addTarget:self | |
74 action:@selector(onCameraSwitch:) | |
75 forControlEvents:UIControlEventTouchUpInside]; | |
76 [self addSubview:_cameraSwitchButton]; | |
77 | |
78 _hangupButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
79 _hangupButton.backgroundColor = [UIColor redColor]; | |
80 _hangupButton.layer.cornerRadius = kButtonSize / 2; | |
81 _hangupButton.layer.masksToBounds = YES; | |
82 image = [UIImage imageForName:@"ic_call_end_black_24dp.png" | |
83 color:[UIColor whiteColor]]; | |
84 [_hangupButton setImage:image forState:UIControlStateNormal]; | |
85 [_hangupButton addTarget:self | |
86 action:@selector(onHangup:) | |
87 forControlEvents:UIControlEventTouchUpInside]; | |
88 [self addSubview:_hangupButton]; | |
89 | |
90 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
91 _statusLabel.font = [UIFont fontWithName:@"Roboto" size:16]; | |
92 _statusLabel.textColor = [UIColor whiteColor]; | |
93 [self addSubview:_statusLabel]; | |
94 } | |
95 return self; | |
96 } | |
97 | |
98 - (void)layoutSubviews { | |
99 CGRect bounds = self.bounds; | |
100 if (_remoteVideoSize.width > 0 && _remoteVideoSize.height > 0) { | |
101 // Aspect fill remote video into bounds. | |
102 CGRect remoteVideoFrame = | |
103 AVMakeRectWithAspectRatioInsideRect(_remoteVideoSize, bounds); | |
104 CGFloat scale = 1; | |
105 if (remoteVideoFrame.size.width > remoteVideoFrame.size.height) { | |
106 // Scale by height. | |
107 scale = bounds.size.height / remoteVideoFrame.size.height; | |
108 } else { | |
109 // Scale by width. | |
110 scale = bounds.size.width / remoteVideoFrame.size.width; | |
111 } | |
112 remoteVideoFrame.size.height *= scale; | |
113 remoteVideoFrame.size.width *= scale; | |
114 _remoteVideoView.frame = remoteVideoFrame; | |
115 _remoteVideoView.center = | |
116 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); | |
117 } else { | |
118 _remoteVideoView.frame = bounds; | |
119 } | |
120 | |
121 if (_localVideoSize.width && _localVideoSize.height > 0) { | |
122 // Aspect fit local video view into a square box. | |
123 CGRect localVideoFrame = | |
124 CGRectMake(0, 0, kLocalVideoViewSize, kLocalVideoViewSize); | |
125 localVideoFrame = | |
126 AVMakeRectWithAspectRatioInsideRect(_localVideoSize, localVideoFrame); | |
127 | |
128 // Place the view in the bottom right. | |
129 localVideoFrame.origin.x = CGRectGetMaxX(bounds) | |
130 - localVideoFrame.size.width - kLocalVideoViewPadding; | |
131 localVideoFrame.origin.y = CGRectGetMaxY(bounds) | |
132 - localVideoFrame.size.height - kLocalVideoViewPadding; | |
133 _localVideoView.frame = localVideoFrame; | |
134 } else { | |
135 _localVideoView.frame = bounds; | |
136 } | |
137 | |
138 // Place hangup button in the bottom left. | |
139 _hangupButton.frame = | |
140 CGRectMake(CGRectGetMinX(bounds) + kButtonPadding, | |
141 CGRectGetMaxY(bounds) - kButtonPadding - | |
142 kButtonSize, | |
143 kButtonSize, | |
144 kButtonSize); | |
145 | |
146 // Place button to the right of hangup button. | |
147 CGRect cameraSwitchFrame = _hangupButton.frame; | |
148 cameraSwitchFrame.origin.x = | |
149 CGRectGetMaxX(cameraSwitchFrame) + kButtonPadding; | |
150 _cameraSwitchButton.frame = cameraSwitchFrame; | |
151 | |
152 [_statusLabel sizeToFit]; | |
153 _statusLabel.center = | |
154 CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); | |
155 } | |
156 | |
157 #pragma mark - RTCEAGLVideoViewDelegate | |
158 | |
159 - (void)videoView:(RTCEAGLVideoView*)videoView didChangeVideoSize:(CGSize)size { | |
160 if (videoView == _localVideoView) { | |
161 _localVideoSize = size; | |
162 _localVideoView.hidden = CGSizeEqualToSize(CGSizeZero, _localVideoSize); | |
163 } else if (videoView == _remoteVideoView) { | |
164 _remoteVideoSize = size; | |
165 } | |
166 [self setNeedsLayout]; | |
167 } | |
168 | |
169 #pragma mark - Private | |
170 | |
171 - (void)onCameraSwitch:(id)sender { | |
172 [_delegate videoCallViewDidSwitchCamera:self]; | |
173 } | |
174 | |
175 - (void)onHangup:(id)sender { | |
176 [_delegate videoCallViewDidHangup:self]; | |
177 } | |
178 | |
179 @end | |
OLD | NEW |