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