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