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 "ARDMainViewController.h" |
| 12 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 13 | #import <AVFoundation/AVFoundation.h> |
| 14 | |
denicija | 59ee91b | 2017-06-05 05:48:47 -0700 | [diff] [blame^] | 15 | #import "WebRTC/RTCAudioSession.h" |
| 16 | #import "WebRTC/RTCAudioSessionConfiguration.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 17 | #import "WebRTC/RTCDispatcher.h" |
| 18 | #import "WebRTC/RTCLogging.h" |
denicija | 59ee91b | 2017-06-05 05:48:47 -0700 | [diff] [blame^] | 19 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 20 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 21 | #import "ARDAppClient.h" |
| 22 | #import "ARDMainView.h" |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 23 | #import "ARDSettingsModel.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 24 | #import "ARDSettingsViewController.h" |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 25 | #import "ARDVideoCallViewController.h" |
| 26 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 27 | static NSString *const barButtonImageString = @"ic_settings_black_24dp.png"; |
denicija | 6d6762c | 2016-10-28 04:53:16 -0700 | [diff] [blame] | 28 | |
denicija | 5db450d | 2017-03-28 08:27:41 -0700 | [diff] [blame] | 29 | // Launch argument to be passed to indicate that the app should start loopback immediatly |
| 30 | static NSString *const loopbackLaunchProcessArgument = @"loopback"; |
| 31 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 32 | @interface ARDMainViewController () < |
| 33 | ARDMainViewDelegate, |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 34 | ARDVideoCallViewControllerDelegate, |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 35 | RTCAudioSessionDelegate> |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 36 | @end |
| 37 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 38 | @implementation ARDMainViewController { |
| 39 | ARDMainView *_mainView; |
| 40 | AVAudioPlayer *_audioPlayer; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 41 | BOOL _useManualAudio; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 42 | } |
| 43 | |
denicija | 5db450d | 2017-03-28 08:27:41 -0700 | [diff] [blame] | 44 | - (void)viewDidLoad { |
| 45 | [super viewDidLoad]; |
| 46 | if ([[[NSProcessInfo processInfo] arguments] containsObject:loopbackLaunchProcessArgument]) { |
| 47 | [self mainView:nil |
| 48 | didInputRoom:@"" |
| 49 | isLoopback:YES |
| 50 | isAudioOnly:NO |
| 51 | shouldMakeAecDump:NO |
| 52 | shouldUseLevelControl:NO |
| 53 | useManualAudio:NO]; |
| 54 | } |
| 55 | } |
| 56 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 57 | - (void)loadView { |
denicija | 6d6762c | 2016-10-28 04:53:16 -0700 | [diff] [blame] | 58 | self.title = @"AppRTC Mobile"; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 59 | _mainView = [[ARDMainView alloc] initWithFrame:CGRectZero]; |
| 60 | _mainView.delegate = self; |
| 61 | self.view = _mainView; |
denicija | 6d6762c | 2016-10-28 04:53:16 -0700 | [diff] [blame] | 62 | [self addSettingsBarButton]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 63 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 64 | RTCAudioSessionConfiguration *webRTCConfig = |
| 65 | [RTCAudioSessionConfiguration webRTCConfiguration]; |
| 66 | webRTCConfig.categoryOptions = webRTCConfig.categoryOptions | |
| 67 | AVAudioSessionCategoryOptionDefaultToSpeaker; |
| 68 | [RTCAudioSessionConfiguration setWebRTCConfiguration:webRTCConfig]; |
| 69 | |
| 70 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 71 | [session addDelegate:self]; |
| 72 | |
| 73 | [self configureAudioSession]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 74 | [self setupAudioPlayer]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
denicija | 6d6762c | 2016-10-28 04:53:16 -0700 | [diff] [blame] | 77 | - (void)addSettingsBarButton { |
| 78 | UIBarButtonItem *settingsButton = |
| 79 | [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:barButtonImageString] |
| 80 | style:UIBarButtonItemStylePlain |
| 81 | target:self |
| 82 | action:@selector(showSettings:)]; |
| 83 | self.navigationItem.rightBarButtonItem = settingsButton; |
| 84 | } |
| 85 | |
denicija | 5db450d | 2017-03-28 08:27:41 -0700 | [diff] [blame] | 86 | + (NSString *)loopbackRoomString { |
| 87 | NSString *loopbackRoomString = |
| 88 | [[NSUUID UUID].UUIDString stringByReplacingOccurrencesOfString:@"-" withString:@""]; |
| 89 | return loopbackRoomString; |
| 90 | } |
| 91 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 92 | #pragma mark - ARDMainViewDelegate |
| 93 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 94 | - (void)mainView:(ARDMainView *)mainView |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 95 | didInputRoom:(NSString *)room |
| 96 | isLoopback:(BOOL)isLoopback |
| 97 | isAudioOnly:(BOOL)isAudioOnly |
| 98 | shouldMakeAecDump:(BOOL)shouldMakeAecDump |
| 99 | shouldUseLevelControl:(BOOL)shouldUseLevelControl |
| 100 | useManualAudio:(BOOL)useManualAudio { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 101 | if (!room.length) { |
denicija | 5db450d | 2017-03-28 08:27:41 -0700 | [diff] [blame] | 102 | if (isLoopback) { |
| 103 | // If this is a loopback call, allow a generated room name. |
| 104 | room = [[self class] loopbackRoomString]; |
| 105 | } else { |
| 106 | [self showAlertWithMessage:@"Missing room name."]; |
| 107 | return; |
| 108 | } |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 109 | } |
| 110 | // Trim whitespaces. |
| 111 | NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet]; |
| 112 | NSString *trimmedRoom = [room stringByTrimmingCharactersInSet:whitespaceSet]; |
| 113 | |
| 114 | // Check that room name is valid. |
| 115 | NSError *error = nil; |
| 116 | NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive; |
| 117 | NSRegularExpression *regex = |
| 118 | [NSRegularExpression regularExpressionWithPattern:@"\\w+" |
| 119 | options:options |
| 120 | error:&error]; |
| 121 | if (error) { |
| 122 | [self showAlertWithMessage:error.localizedDescription]; |
| 123 | return; |
| 124 | } |
| 125 | NSRange matchRange = |
| 126 | [regex rangeOfFirstMatchInString:trimmedRoom |
| 127 | options:0 |
| 128 | range:NSMakeRange(0, trimmedRoom.length)]; |
| 129 | if (matchRange.location == NSNotFound || |
| 130 | matchRange.length != trimmedRoom.length) { |
| 131 | [self showAlertWithMessage:@"Invalid room name."]; |
| 132 | return; |
| 133 | } |
| 134 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 135 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 136 | session.useManualAudio = useManualAudio; |
| 137 | session.isAudioEnabled = NO; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 138 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 139 | // Kick off the video call. |
| 140 | ARDVideoCallViewController *videoCallViewController = |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 141 | [[ARDVideoCallViewController alloc] initForRoom:trimmedRoom |
| 142 | isLoopback:isLoopback |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 143 | isAudioOnly:isAudioOnly |
peah | 5085b0c | 2016-08-25 22:15:14 -0700 | [diff] [blame] | 144 | shouldMakeAecDump:shouldMakeAecDump |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 145 | shouldUseLevelControl:shouldUseLevelControl |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 146 | delegate:self]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 147 | videoCallViewController.modalTransitionStyle = |
| 148 | UIModalTransitionStyleCrossDissolve; |
| 149 | [self presentViewController:videoCallViewController |
| 150 | animated:YES |
| 151 | completion:nil]; |
| 152 | } |
| 153 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 154 | - (void)mainViewDidToggleAudioLoop:(ARDMainView *)mainView { |
| 155 | if (mainView.isAudioLoopPlaying) { |
| 156 | [_audioPlayer stop]; |
| 157 | } else { |
| 158 | [_audioPlayer play]; |
| 159 | } |
| 160 | mainView.isAudioLoopPlaying = _audioPlayer.playing; |
| 161 | } |
| 162 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 163 | #pragma mark - ARDVideoCallViewControllerDelegate |
| 164 | |
| 165 | - (void)viewControllerDidFinish:(ARDVideoCallViewController *)viewController { |
| 166 | if (![viewController isBeingDismissed]) { |
| 167 | RTCLog(@"Dismissing VC"); |
| 168 | [self dismissViewControllerAnimated:YES completion:^{ |
| 169 | [self restartAudioPlayerIfNeeded]; |
| 170 | }]; |
| 171 | } |
| 172 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 173 | session.isAudioEnabled = NO; |
| 174 | } |
| 175 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 176 | #pragma mark - RTCAudioSessionDelegate |
| 177 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 178 | - (void)audioSessionDidStartPlayOrRecord:(RTCAudioSession *)session { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 179 | // Stop playback on main queue and then configure WebRTC. |
| 180 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain |
| 181 | block:^{ |
| 182 | if (_mainView.isAudioLoopPlaying) { |
| 183 | RTCLog(@"Stopping audio loop due to WebRTC start."); |
| 184 | [_audioPlayer stop]; |
| 185 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 186 | RTCLog(@"Setting isAudioEnabled to YES."); |
| 187 | session.isAudioEnabled = YES; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 188 | }]; |
| 189 | } |
| 190 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 191 | - (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 192 | // WebRTC is done with the audio session. Restart playback. |
| 193 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain |
| 194 | block:^{ |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 195 | RTCLog(@"audioSessionDidStopPlayOrRecord"); |
| 196 | [self restartAudioPlayerIfNeeded]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 197 | }]; |
| 198 | } |
| 199 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 200 | #pragma mark - Private |
denicija | 6d6762c | 2016-10-28 04:53:16 -0700 | [diff] [blame] | 201 | - (void)showSettings:(id)sender { |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 202 | ARDSettingsViewController *settingsController = |
denicija | b04b5c2 | 2016-11-09 04:28:46 -0800 | [diff] [blame] | 203 | [[ARDSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 204 | settingsModel:[[ARDSettingsModel alloc] init]]; |
| 205 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 206 | UINavigationController *navigationController = |
| 207 | [[UINavigationController alloc] initWithRootViewController:settingsController]; |
| 208 | [self presentViewControllerAsModal:navigationController]; |
| 209 | } |
| 210 | |
| 211 | - (void)presentViewControllerAsModal:(UIViewController *)viewController { |
| 212 | [self presentViewController:viewController animated:YES completion:nil]; |
denicija | 6d6762c | 2016-10-28 04:53:16 -0700 | [diff] [blame] | 213 | } |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 214 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 215 | - (void)configureAudioSession { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 216 | RTCAudioSessionConfiguration *configuration = |
| 217 | [[RTCAudioSessionConfiguration alloc] init]; |
| 218 | configuration.category = AVAudioSessionCategoryAmbient; |
| 219 | configuration.categoryOptions = AVAudioSessionCategoryOptionDuckOthers; |
| 220 | configuration.mode = AVAudioSessionModeDefault; |
| 221 | |
| 222 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 223 | [session lockForConfiguration]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 224 | BOOL hasSucceeded = NO; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 225 | NSError *error = nil; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 226 | if (session.isActive) { |
| 227 | hasSucceeded = [session setConfiguration:configuration error:&error]; |
| 228 | } else { |
| 229 | hasSucceeded = [session setConfiguration:configuration |
| 230 | active:YES |
| 231 | error:&error]; |
| 232 | } |
| 233 | if (!hasSucceeded) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 234 | RTCLogError(@"Error setting configuration: %@", error.localizedDescription); |
| 235 | } |
| 236 | [session unlockForConfiguration]; |
| 237 | } |
| 238 | |
| 239 | - (void)setupAudioPlayer { |
| 240 | NSString *audioFilePath = |
| 241 | [[NSBundle mainBundle] pathForResource:@"mozart" ofType:@"mp3"]; |
| 242 | NSURL *audioFileURL = [NSURL URLWithString:audioFilePath]; |
| 243 | _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL |
| 244 | error:nil]; |
| 245 | _audioPlayer.numberOfLoops = -1; |
| 246 | _audioPlayer.volume = 1.0; |
| 247 | [_audioPlayer prepareToPlay]; |
| 248 | } |
| 249 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 250 | - (void)restartAudioPlayerIfNeeded { |
| 251 | if (_mainView.isAudioLoopPlaying && !self.presentedViewController) { |
| 252 | RTCLog(@"Starting audio loop due to WebRTC end."); |
| 253 | [self configureAudioSession]; |
| 254 | [_audioPlayer play]; |
| 255 | } |
| 256 | } |
| 257 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 258 | - (void)showAlertWithMessage:(NSString*)message { |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 259 | UIAlertController *alert = |
| 260 | [UIAlertController alertControllerWithTitle:nil |
| 261 | message:message |
| 262 | preferredStyle:UIAlertControllerStyleAlert]; |
| 263 | |
| 264 | UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" |
| 265 | style:UIAlertActionStyleDefault |
| 266 | handler:^(UIAlertAction *action){ |
| 267 | }]; |
| 268 | |
| 269 | [alert addAction:defaultAction]; |
| 270 | [self presentViewController:alert animated:YES completion:nil]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @end |