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