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 "ARDVideoCallViewController.h" |
| 12 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 13 | #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" |
| 14 | |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 15 | #import "ARDAppClient.h" |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 16 | #import "ARDSettingsModel.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 17 | #import "ARDVideoCallView.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 18 | #import "WebRTC/RTCAVFoundationVideoSource.h" |
| 19 | #import "WebRTC/RTCDispatcher.h" |
| 20 | #import "WebRTC/RTCLogging.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 21 | #import "WebRTC/RTCMediaConstraints.h" |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 22 | |
| 23 | @interface ARDVideoCallViewController () <ARDAppClientDelegate, |
| 24 | ARDVideoCallViewDelegate> |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 25 | @property(nonatomic, strong) RTCVideoTrack *localVideoTrack; |
| 26 | @property(nonatomic, strong) RTCVideoTrack *remoteVideoTrack; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 27 | @property(nonatomic, readonly) ARDVideoCallView *videoCallView; |
| 28 | @end |
| 29 | |
| 30 | @implementation ARDVideoCallViewController { |
| 31 | ARDAppClient *_client; |
| 32 | RTCVideoTrack *_remoteVideoTrack; |
| 33 | RTCVideoTrack *_localVideoTrack; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 34 | AVAudioSessionPortOverride _portOverride; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @synthesize videoCallView = _videoCallView; |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 38 | @synthesize localVideoTrack = _localVideoTrack; |
| 39 | @synthesize remoteVideoTrack = _remoteVideoTrack; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 40 | @synthesize delegate = _delegate; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 41 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 42 | - (instancetype)initForRoom:(NSString *)room |
| 43 | isLoopback:(BOOL)isLoopback |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 44 | isAudioOnly:(BOOL)isAudioOnly |
peah | 5085b0c | 2016-08-25 22:15:14 -0700 | [diff] [blame] | 45 | shouldMakeAecDump:(BOOL)shouldMakeAecDump |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 46 | shouldUseLevelControl:(BOOL)shouldUseLevelControl |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 47 | delegate:(id<ARDVideoCallViewControllerDelegate>)delegate { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 48 | if (self = [super init]) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 49 | ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame] | 50 | _delegate = delegate; |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 51 | _client = [[ARDAppClient alloc] initWithDelegate:self]; |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 52 | [_client connectToRoomWithId:room |
sakal | c4adacf | 2017-03-28 01:22:48 -0700 | [diff] [blame] | 53 | settings:settingsModel |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 54 | isLoopback:isLoopback |
peah | 5085b0c | 2016-08-25 22:15:14 -0700 | [diff] [blame] | 55 | isAudioOnly:isAudioOnly |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 56 | shouldMakeAecDump:shouldMakeAecDump |
| 57 | shouldUseLevelControl:shouldUseLevelControl]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 58 | } |
| 59 | return self; |
| 60 | } |
| 61 | |
| 62 | - (void)loadView { |
| 63 | _videoCallView = [[ARDVideoCallView alloc] initWithFrame:CGRectZero]; |
| 64 | _videoCallView.delegate = self; |
| 65 | _videoCallView.statusLabel.text = |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 66 | [self statusTextForState:RTCIceConnectionStateNew]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 67 | self.view = _videoCallView; |
| 68 | } |
| 69 | |
| 70 | #pragma mark - ARDAppClientDelegate |
| 71 | |
| 72 | - (void)appClient:(ARDAppClient *)client |
| 73 | didChangeState:(ARDAppClientState)state { |
| 74 | switch (state) { |
| 75 | case kARDAppClientStateConnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 76 | RTCLog(@"Client connected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 77 | break; |
| 78 | case kARDAppClientStateConnecting: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 79 | RTCLog(@"Client connecting."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 80 | break; |
| 81 | case kARDAppClientStateDisconnected: |
tkchin | c3f46a9 | 2015-07-23 12:50:55 -0700 | [diff] [blame] | 82 | RTCLog(@"Client disconnected."); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 83 | [self hangup]; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | - (void)appClient:(ARDAppClient *)client |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 89 | didChangeConnectionState:(RTCIceConnectionState)state { |
| 90 | RTCLog(@"ICE state changed: %ld", (long)state); |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 91 | __weak ARDVideoCallViewController *weakSelf = self; |
| 92 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 93 | ARDVideoCallViewController *strongSelf = weakSelf; |
| 94 | strongSelf.videoCallView.statusLabel.text = |
| 95 | [strongSelf statusTextForState:state]; |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | - (void)appClient:(ARDAppClient *)client |
| 100 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 101 | self.localVideoTrack = localVideoTrack; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | - (void)appClient:(ARDAppClient *)client |
| 105 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 106 | self.remoteVideoTrack = remoteVideoTrack; |
| 107 | _videoCallView.statusLabel.hidden = YES; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | - (void)appClient:(ARDAppClient *)client |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 111 | didGetStats:(NSArray *)stats { |
| 112 | _videoCallView.statsView.stats = stats; |
| 113 | [_videoCallView setNeedsLayout]; |
| 114 | } |
| 115 | |
| 116 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 117 | didError:(NSError *)error { |
| 118 | NSString *message = |
| 119 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
| 120 | [self showAlertWithMessage:message]; |
| 121 | [self hangup]; |
| 122 | } |
| 123 | |
| 124 | #pragma mark - ARDVideoCallViewDelegate |
| 125 | |
| 126 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 127 | [self hangup]; |
| 128 | } |
| 129 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 130 | - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view { |
| 131 | // TODO(tkchin): Rate limit this so you can't tap continously on it. |
| 132 | // Probably through an animation. |
| 133 | [self switchCamera]; |
| 134 | } |
| 135 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 136 | - (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view { |
| 137 | AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone; |
| 138 | if (_portOverride == AVAudioSessionPortOverrideNone) { |
| 139 | override = AVAudioSessionPortOverrideSpeaker; |
| 140 | } |
| 141 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession |
| 142 | block:^{ |
| 143 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 144 | [session lockForConfiguration]; |
| 145 | NSError *error = nil; |
| 146 | if ([session overrideOutputAudioPort:override error:&error]) { |
| 147 | _portOverride = override; |
| 148 | } else { |
| 149 | RTCLogError(@"Error overriding output port: %@", |
| 150 | error.localizedDescription); |
| 151 | } |
| 152 | [session unlockForConfiguration]; |
| 153 | }]; |
| 154 | } |
| 155 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 156 | - (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view { |
| 157 | _client.shouldGetStats = YES; |
| 158 | _videoCallView.statsView.hidden = NO; |
| 159 | } |
| 160 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 161 | #pragma mark - Private |
| 162 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 163 | - (void)setLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
| 164 | if (_localVideoTrack == localVideoTrack) { |
| 165 | return; |
| 166 | } |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 167 | _localVideoTrack = nil; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 168 | _localVideoTrack = localVideoTrack; |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 169 | RTCAVFoundationVideoSource *source = nil; |
| 170 | if ([localVideoTrack.source |
| 171 | isKindOfClass:[RTCAVFoundationVideoSource class]]) { |
| 172 | source = (RTCAVFoundationVideoSource*)localVideoTrack.source; |
| 173 | } |
| 174 | _videoCallView.localVideoView.captureSession = source.captureSession; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 178 | if (_remoteVideoTrack == remoteVideoTrack) { |
| 179 | return; |
| 180 | } |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 181 | [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 182 | _remoteVideoTrack = nil; |
| 183 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 184 | _remoteVideoTrack = remoteVideoTrack; |
| 185 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 186 | } |
| 187 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 188 | - (void)hangup { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 189 | self.remoteVideoTrack = nil; |
| 190 | self.localVideoTrack = nil; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 191 | [_client disconnect]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 192 | [_delegate viewControllerDidFinish:self]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 195 | - (void)switchCamera { |
| 196 | RTCVideoSource* source = self.localVideoTrack.source; |
| 197 | if ([source isKindOfClass:[RTCAVFoundationVideoSource class]]) { |
| 198 | RTCAVFoundationVideoSource* avSource = (RTCAVFoundationVideoSource*)source; |
| 199 | avSource.useBackCamera = !avSource.useBackCamera; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 203 | - (NSString *)statusTextForState:(RTCIceConnectionState)state { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 204 | switch (state) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 205 | case RTCIceConnectionStateNew: |
| 206 | case RTCIceConnectionStateChecking: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 207 | return @"Connecting..."; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 208 | case RTCIceConnectionStateConnected: |
| 209 | case RTCIceConnectionStateCompleted: |
| 210 | case RTCIceConnectionStateFailed: |
| 211 | case RTCIceConnectionStateDisconnected: |
| 212 | case RTCIceConnectionStateClosed: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 213 | case RTCIceConnectionStateCount: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 214 | return nil; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | - (void)showAlertWithMessage:(NSString*)message { |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 219 | UIAlertController *alert = |
| 220 | [UIAlertController alertControllerWithTitle:nil |
| 221 | message:message |
| 222 | preferredStyle:UIAlertControllerStyleAlert]; |
| 223 | |
| 224 | UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" |
| 225 | style:UIAlertActionStyleDefault |
| 226 | handler:^(UIAlertAction *action){ |
| 227 | }]; |
| 228 | |
| 229 | [alert addAction:defaultAction]; |
| 230 | [self presentViewController:alert animated:YES completion:nil]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @end |