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