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