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