OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import "ARDMainView.h" | 11 #import "ARDMainView.h" |
12 | 12 |
| 13 #import <AVFoundation/AVFoundation.h> |
| 14 |
13 #import "UIImage+ARDUtilities.h" | 15 #import "UIImage+ARDUtilities.h" |
14 | 16 |
15 // TODO(tkchin): retrieve status bar height dynamically. | 17 // TODO(tkchin): retrieve status bar height dynamically. |
16 static CGFloat const kStatusBarHeight = 20; | 18 static CGFloat const kStatusBarHeight = 20; |
17 | 19 |
18 static CGFloat const kRoomTextButtonSize = 40; | 20 static CGFloat const kRoomTextButtonSize = 40; |
19 static CGFloat const kRoomTextFieldHeight = 40; | 21 static CGFloat const kRoomTextFieldHeight = 40; |
20 static CGFloat const kRoomTextFieldMargin = 8; | 22 static CGFloat const kRoomTextFieldMargin = 8; |
21 static CGFloat const kCallControlMargin = 8; | 23 static CGFloat const kCallControlMargin = 8; |
22 static CGFloat const kAppLabelHeight = 20; | 24 static CGFloat const kAppLabelHeight = 20; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 118 |
117 @implementation ARDMainView { | 119 @implementation ARDMainView { |
118 UILabel *_appLabel; | 120 UILabel *_appLabel; |
119 ARDRoomTextField *_roomText; | 121 ARDRoomTextField *_roomText; |
120 UILabel *_callOptionsLabel; | 122 UILabel *_callOptionsLabel; |
121 UISwitch *_audioOnlySwitch; | 123 UISwitch *_audioOnlySwitch; |
122 UILabel *_audioOnlyLabel; | 124 UILabel *_audioOnlyLabel; |
123 UISwitch *_loopbackSwitch; | 125 UISwitch *_loopbackSwitch; |
124 UILabel *_loopbackLabel; | 126 UILabel *_loopbackLabel; |
125 UIButton *_startCallButton; | 127 UIButton *_startCallButton; |
| 128 UIButton *_audioLoopButton; |
| 129 AVAudioPlayer *_audioPlayer; |
126 } | 130 } |
127 | 131 |
128 @synthesize delegate = _delegate; | 132 @synthesize delegate = _delegate; |
129 | 133 |
130 - (instancetype)initWithFrame:(CGRect)frame { | 134 - (instancetype)initWithFrame:(CGRect)frame { |
131 if (self = [super initWithFrame:frame]) { | 135 if (self = [super initWithFrame:frame]) { |
| 136 NSString *audioFilePath = |
| 137 [[NSBundle mainBundle] pathForResource:@"mozart" ofType:@"mp3"]; |
| 138 NSURL *audioFileURL = [NSURL URLWithString:audioFilePath]; |
| 139 _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL |
| 140 error:nil]; |
| 141 _audioPlayer.numberOfLoops = -1; |
| 142 _audioPlayer.volume = 1.0; |
| 143 [_audioPlayer prepareToPlay]; |
| 144 |
132 _appLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 145 _appLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
133 _appLabel.text = @"AppRTCDemo"; | 146 _appLabel.text = @"AppRTCDemo"; |
134 _appLabel.font = [UIFont fontWithName:@"Roboto" size:34]; | 147 _appLabel.font = [UIFont fontWithName:@"Roboto" size:34]; |
135 _appLabel.textColor = [UIColor colorWithWhite:0 alpha:.2]; | 148 _appLabel.textColor = [UIColor colorWithWhite:0 alpha:.2]; |
136 [_appLabel sizeToFit]; | 149 [_appLabel sizeToFit]; |
137 [self addSubview:_appLabel]; | 150 [self addSubview:_appLabel]; |
138 | 151 |
139 _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero]; | 152 _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero]; |
140 [self addSubview:_roomText]; | 153 [self addSubview:_roomText]; |
141 | 154 |
(...skipping 22 matching lines...) Expand all Loading... |
164 [_loopbackSwitch sizeToFit]; | 177 [_loopbackSwitch sizeToFit]; |
165 [self addSubview:_loopbackSwitch]; | 178 [self addSubview:_loopbackSwitch]; |
166 | 179 |
167 _loopbackLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | 180 _loopbackLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
168 _loopbackLabel.text = @"Loopback mode"; | 181 _loopbackLabel.text = @"Loopback mode"; |
169 _loopbackLabel.font = controlFont; | 182 _loopbackLabel.font = controlFont; |
170 _loopbackLabel.textColor = controlFontColor; | 183 _loopbackLabel.textColor = controlFontColor; |
171 [_loopbackLabel sizeToFit]; | 184 [_loopbackLabel sizeToFit]; |
172 [self addSubview:_loopbackLabel]; | 185 [self addSubview:_loopbackLabel]; |
173 | 186 |
174 _startCallButton = [[UIButton alloc] initWithFrame:CGRectZero]; | |
175 | |
176 _startCallButton = [UIButton buttonWithType:UIButtonTypeSystem]; | 187 _startCallButton = [UIButton buttonWithType:UIButtonTypeSystem]; |
177 _startCallButton.backgroundColor = [UIColor blueColor]; | 188 _startCallButton.backgroundColor = [UIColor blueColor]; |
178 _startCallButton.layer.cornerRadius = 10; | 189 _startCallButton.layer.cornerRadius = 10; |
179 _startCallButton.clipsToBounds = YES; | 190 _startCallButton.clipsToBounds = YES; |
180 _startCallButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10); | 191 _startCallButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10); |
181 [_startCallButton setTitle:@"Start call" | 192 [_startCallButton setTitle:@"Start call" |
182 forState:UIControlStateNormal]; | 193 forState:UIControlStateNormal]; |
183 _startCallButton.titleLabel.font = controlFont; | 194 _startCallButton.titleLabel.font = controlFont; |
184 [_startCallButton setTitleColor:[UIColor whiteColor] | 195 [_startCallButton setTitleColor:[UIColor whiteColor] |
185 forState:UIControlStateNormal]; | 196 forState:UIControlStateNormal]; |
186 [_startCallButton setTitleColor:[UIColor lightGrayColor] | 197 [_startCallButton setTitleColor:[UIColor lightGrayColor] |
187 forState:UIControlStateSelected]; | 198 forState:UIControlStateSelected]; |
188 [_startCallButton sizeToFit]; | 199 [_startCallButton sizeToFit]; |
189 [_startCallButton addTarget:self | 200 [_startCallButton addTarget:self |
190 action:@selector(onStartCall:) | 201 action:@selector(onStartCall:) |
191 forControlEvents:UIControlEventTouchUpInside]; | 202 forControlEvents:UIControlEventTouchUpInside]; |
192 [self addSubview:_startCallButton]; | 203 [self addSubview:_startCallButton]; |
193 | 204 |
| 205 // Used to test what happens to sounds when calls are in progress. |
| 206 _audioLoopButton = [UIButton buttonWithType:UIButtonTypeSystem]; |
| 207 _audioLoopButton.layer.cornerRadius = 10; |
| 208 _audioLoopButton.clipsToBounds = YES; |
| 209 _audioLoopButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10); |
| 210 _audioLoopButton.titleLabel.font = controlFont; |
| 211 [_audioLoopButton setTitleColor:[UIColor whiteColor] |
| 212 forState:UIControlStateNormal]; |
| 213 [_audioLoopButton setTitleColor:[UIColor lightGrayColor] |
| 214 forState:UIControlStateSelected]; |
| 215 [self updateAudioLoopButton]; |
| 216 [_audioLoopButton addTarget:self |
| 217 action:@selector(onToggleAudioLoop:) |
| 218 forControlEvents:UIControlEventTouchUpInside]; |
| 219 [self addSubview:_audioLoopButton]; |
| 220 |
194 self.backgroundColor = [UIColor whiteColor]; | 221 self.backgroundColor = [UIColor whiteColor]; |
195 } | 222 } |
196 return self; | 223 return self; |
197 } | 224 } |
198 | 225 |
199 - (void)layoutSubviews { | 226 - (void)layoutSubviews { |
200 CGRect bounds = self.bounds; | 227 CGRect bounds = self.bounds; |
201 CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin; | 228 CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin; |
202 CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height; | 229 CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height; |
203 _roomText.frame = CGRectMake(kRoomTextFieldMargin, | 230 _roomText.frame = CGRectMake(kRoomTextFieldMargin, |
(...skipping 26 matching lines...) Expand all Loading... |
230 CGRect loopbackModeRect = CGRectMake(kCallControlMargin * 3, | 257 CGRect loopbackModeRect = CGRectMake(kCallControlMargin * 3, |
231 loopbackModeTop, | 258 loopbackModeTop, |
232 _loopbackSwitch.frame.size.width, | 259 _loopbackSwitch.frame.size.width, |
233 _loopbackSwitch.frame.size.height); | 260 _loopbackSwitch.frame.size.height); |
234 _loopbackSwitch.frame = loopbackModeRect; | 261 _loopbackSwitch.frame = loopbackModeRect; |
235 CGFloat loopbackModeLabelCenterX = CGRectGetMaxX(loopbackModeRect) + | 262 CGFloat loopbackModeLabelCenterX = CGRectGetMaxX(loopbackModeRect) + |
236 kCallControlMargin + _loopbackLabel.frame.size.width / 2; | 263 kCallControlMargin + _loopbackLabel.frame.size.width / 2; |
237 _loopbackLabel.center = CGPointMake(loopbackModeLabelCenterX, | 264 _loopbackLabel.center = CGPointMake(loopbackModeLabelCenterX, |
238 CGRectGetMidY(loopbackModeRect)); | 265 CGRectGetMidY(loopbackModeRect)); |
239 | 266 |
| 267 CGFloat audioLoopTop = |
| 268 CGRectGetMaxY(loopbackModeRect) + kCallControlMargin * 3; |
| 269 _audioLoopButton.frame = CGRectMake(kCallControlMargin, |
| 270 audioLoopTop, |
| 271 _audioLoopButton.frame.size.width, |
| 272 _audioLoopButton.frame.size.height); |
| 273 |
240 CGFloat startCallTop = | 274 CGFloat startCallTop = |
241 CGRectGetMaxY(loopbackModeRect) + kCallControlMargin * 3; | 275 CGRectGetMaxY(_audioLoopButton.frame) + kCallControlMargin * 3; |
242 _startCallButton.frame = CGRectMake(kCallControlMargin, | 276 _startCallButton.frame = CGRectMake(kCallControlMargin, |
243 startCallTop, | 277 startCallTop, |
244 _startCallButton.frame.size.width, | 278 _startCallButton.frame.size.width, |
245 _startCallButton.frame.size.height); | 279 _startCallButton.frame.size.height); |
246 } | 280 } |
247 | 281 |
248 #pragma mark - Private | 282 #pragma mark - Private |
249 | 283 |
| 284 - (void)updateAudioLoopButton { |
| 285 if (_audioPlayer.playing) { |
| 286 _audioLoopButton.backgroundColor = [UIColor redColor]; |
| 287 [_audioLoopButton setTitle:@"Stop sound" |
| 288 forState:UIControlStateNormal]; |
| 289 [_audioLoopButton sizeToFit]; |
| 290 } else { |
| 291 _audioLoopButton.backgroundColor = [UIColor greenColor]; |
| 292 [_audioLoopButton setTitle:@"Play sound" |
| 293 forState:UIControlStateNormal]; |
| 294 [_audioLoopButton sizeToFit]; |
| 295 } |
| 296 } |
| 297 |
| 298 - (void)onToggleAudioLoop:(id)sender { |
| 299 if (_audioPlayer.playing) { |
| 300 [_audioPlayer stop]; |
| 301 } else { |
| 302 [_audioPlayer play]; |
| 303 } |
| 304 [self updateAudioLoopButton]; |
| 305 } |
| 306 |
250 - (void)onStartCall:(id)sender { | 307 - (void)onStartCall:(id)sender { |
251 NSString *room = _roomText.roomText; | 308 NSString *room = _roomText.roomText; |
252 // If this is a loopback call, allow a generated room name. | 309 // If this is a loopback call, allow a generated room name. |
253 if (!room.length && _loopbackSwitch.isOn) { | 310 if (!room.length && _loopbackSwitch.isOn) { |
254 room = [[NSUUID UUID] UUIDString]; | 311 room = [[NSUUID UUID] UUIDString]; |
255 } | 312 } |
256 room = [room stringByReplacingOccurrencesOfString:@"-" withString:@""]; | 313 room = [room stringByReplacingOccurrencesOfString:@"-" withString:@""]; |
257 [_delegate mainView:self | 314 [_delegate mainView:self |
258 didInputRoom:room | 315 didInputRoom:room |
259 isLoopback:_loopbackSwitch.isOn | 316 isLoopback:_loopbackSwitch.isOn |
260 isAudioOnly:_audioOnlySwitch.isOn]; | 317 isAudioOnly:_audioOnlySwitch.isOn]; |
261 } | 318 } |
262 | 319 |
263 @end | 320 @end |
OLD | NEW |