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