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