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" |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame^] | 16 | #import "ARDCaptureController.h" |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 17 | #import "ARDSettingsModel.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 18 | #import "ARDVideoCallView.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 19 | #import "WebRTC/RTCAVFoundationVideoSource.h" |
| 20 | #import "WebRTC/RTCDispatcher.h" |
| 21 | #import "WebRTC/RTCLogging.h" |
denicija | d17d536 | 2016-11-02 02:56:09 -0700 | [diff] [blame] | 22 | #import "WebRTC/RTCMediaConstraints.h" |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 23 | |
| 24 | @interface ARDVideoCallViewController () <ARDAppClientDelegate, |
| 25 | ARDVideoCallViewDelegate> |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 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; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame^] | 33 | ARDCaptureController *_captureController; |
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 remoteVideoTrack = _remoteVideoTrack; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 39 | @synthesize delegate = _delegate; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 40 | |
haysc | 913e645 | 2015-10-02 11:44:03 -0700 | [diff] [blame] | 41 | - (instancetype)initForRoom:(NSString *)room |
| 42 | isLoopback:(BOOL)isLoopback |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 43 | isAudioOnly:(BOOL)isAudioOnly |
peah | 5085b0c | 2016-08-25 22:15:14 -0700 | [diff] [blame] | 44 | shouldMakeAecDump:(BOOL)shouldMakeAecDump |
tkchin | ab1293a | 2016-08-30 12:35:05 -0700 | [diff] [blame] | 45 | shouldUseLevelControl:(BOOL)shouldUseLevelControl |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 46 | delegate:(id<ARDVideoCallViewControllerDelegate>)delegate { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 47 | if (self = [super init]) { |
denicija | 2256e04 | 2016-11-09 06:26:18 -0800 | [diff] [blame] | 48 | ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; |
sakal | 68b5df9 | 2017-03-17 09:01:59 -0700 | [diff] [blame] | 49 | _delegate = delegate; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame^] | 50 | |
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 |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame^] | 100 | didCreateLocalCapturer:(RTCCameraVideoCapturer *)localCapturer { |
| 101 | _videoCallView.localVideoView.captureSession = localCapturer.captureSession; |
| 102 | ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; |
| 103 | _captureController = |
| 104 | [[ARDCaptureController alloc] initWithCapturer:localCapturer settings:settingsModel]; |
| 105 | [_captureController startCapture]; |
| 106 | } |
| 107 | |
| 108 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 109 | didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | - (void)appClient:(ARDAppClient *)client |
| 113 | didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 114 | self.remoteVideoTrack = remoteVideoTrack; |
| 115 | _videoCallView.statusLabel.hidden = YES; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | - (void)appClient:(ARDAppClient *)client |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 119 | didGetStats:(NSArray *)stats { |
| 120 | _videoCallView.statsView.stats = stats; |
| 121 | [_videoCallView setNeedsLayout]; |
| 122 | } |
| 123 | |
| 124 | - (void)appClient:(ARDAppClient *)client |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 125 | didError:(NSError *)error { |
| 126 | NSString *message = |
| 127 | [NSString stringWithFormat:@"%@", error.localizedDescription]; |
| 128 | [self showAlertWithMessage:message]; |
| 129 | [self hangup]; |
| 130 | } |
| 131 | |
| 132 | #pragma mark - ARDVideoCallViewDelegate |
| 133 | |
| 134 | - (void)videoCallViewDidHangup:(ARDVideoCallView *)view { |
| 135 | [self hangup]; |
| 136 | } |
| 137 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 138 | - (void)videoCallViewDidSwitchCamera:(ARDVideoCallView *)view { |
| 139 | // TODO(tkchin): Rate limit this so you can't tap continously on it. |
| 140 | // Probably through an animation. |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame^] | 141 | [_captureController switchCamera]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 142 | } |
| 143 | |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 144 | - (void)videoCallViewDidChangeRoute:(ARDVideoCallView *)view { |
| 145 | AVAudioSessionPortOverride override = AVAudioSessionPortOverrideNone; |
| 146 | if (_portOverride == AVAudioSessionPortOverrideNone) { |
| 147 | override = AVAudioSessionPortOverrideSpeaker; |
| 148 | } |
| 149 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeAudioSession |
| 150 | block:^{ |
| 151 | RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 152 | [session lockForConfiguration]; |
| 153 | NSError *error = nil; |
| 154 | if ([session overrideOutputAudioPort:override error:&error]) { |
| 155 | _portOverride = override; |
| 156 | } else { |
| 157 | RTCLogError(@"Error overriding output port: %@", |
| 158 | error.localizedDescription); |
| 159 | } |
| 160 | [session unlockForConfiguration]; |
| 161 | }]; |
| 162 | } |
| 163 | |
Zeke Chin | d332580 | 2015-08-14 11:00:02 -0700 | [diff] [blame] | 164 | - (void)videoCallViewDidEnableStats:(ARDVideoCallView *)view { |
| 165 | _client.shouldGetStats = YES; |
| 166 | _videoCallView.statsView.hidden = NO; |
| 167 | } |
| 168 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 169 | #pragma mark - Private |
| 170 | |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 171 | - (void)setRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack { |
| 172 | if (_remoteVideoTrack == remoteVideoTrack) { |
| 173 | return; |
| 174 | } |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 175 | [_remoteVideoTrack removeRenderer:_videoCallView.remoteVideoView]; |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 176 | _remoteVideoTrack = nil; |
| 177 | [_videoCallView.remoteVideoView renderFrame:nil]; |
| 178 | _remoteVideoTrack = remoteVideoTrack; |
| 179 | [_remoteVideoTrack addRenderer:_videoCallView.remoteVideoView]; |
| 180 | } |
| 181 | |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 182 | - (void)hangup { |
Zeke Chin | 57cc74e | 2015-05-05 07:52:31 -0700 | [diff] [blame] | 183 | self.remoteVideoTrack = nil; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame^] | 184 | _videoCallView.localVideoView.captureSession = nil; |
| 185 | [_captureController stopCapture]; |
| 186 | _captureController = nil; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 187 | [_client disconnect]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 188 | [_delegate viewControllerDidFinish:self]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 189 | } |
| 190 | |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 191 | - (NSString *)statusTextForState:(RTCIceConnectionState)state { |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 192 | switch (state) { |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 193 | case RTCIceConnectionStateNew: |
| 194 | case RTCIceConnectionStateChecking: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 195 | return @"Connecting..."; |
hjon | 79858f8 | 2016-03-13 22:08:26 -0700 | [diff] [blame] | 196 | case RTCIceConnectionStateConnected: |
| 197 | case RTCIceConnectionStateCompleted: |
| 198 | case RTCIceConnectionStateFailed: |
| 199 | case RTCIceConnectionStateDisconnected: |
| 200 | case RTCIceConnectionStateClosed: |
hjon | 8bbbf2c | 2016-03-14 13:15:44 -0700 | [diff] [blame] | 201 | case RTCIceConnectionStateCount: |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 202 | return nil; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | - (void)showAlertWithMessage:(NSString*)message { |
kthelgason | b13237b | 2017-03-30 04:56:05 -0700 | [diff] [blame] | 207 | UIAlertController *alert = |
| 208 | [UIAlertController alertControllerWithTitle:nil |
| 209 | message:message |
| 210 | preferredStyle:UIAlertControllerStyleAlert]; |
| 211 | |
| 212 | UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" |
| 213 | style:UIAlertActionStyleDefault |
| 214 | handler:^(UIAlertAction *action){ |
| 215 | }]; |
| 216 | |
| 217 | [alert addAction:defaultAction]; |
| 218 | [self presentViewController:alert animated:YES completion:nil]; |
tkchin@webrtc.org | ef2a5dd | 2015-01-15 22:38:21 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @end |