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